1193323Sed//===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines various functions that are used to clone chunks of LLVM 11193323Sed// code for various purposes. This varies from copying whole modules into new 12193323Sed// modules, to cloning functions with different arguments, to inlining 13193323Sed// functions, to copying basic blocks to support loop unrolling or superblock 14193323Sed// formation, etc. 15193323Sed// 16193323Sed//===----------------------------------------------------------------------===// 17193323Sed 18193323Sed#ifndef LLVM_TRANSFORMS_UTILS_CLONING_H 19193323Sed#define LLVM_TRANSFORMS_UTILS_CLONING_H 20193323Sed 21207618Srdivacky#include "llvm/ADT/SmallVector.h" 22203954Srdivacky#include "llvm/ADT/Twine.h" 23252723Sdim#include "llvm/ADT/ValueMap.h" 24207618Srdivacky#include "llvm/Support/ValueHandle.h" 25218893Sdim#include "llvm/Transforms/Utils/ValueMapper.h" 26193323Sed 27193323Sednamespace llvm { 28193323Sed 29193323Sedclass Module; 30193323Sedclass Function; 31199481Srdivackyclass Instruction; 32193323Sedclass Pass; 33193323Sedclass LPPassManager; 34193323Sedclass BasicBlock; 35193323Sedclass Value; 36193323Sedclass CallInst; 37193323Sedclass InvokeInst; 38193323Sedclass ReturnInst; 39193323Sedclass CallSite; 40193323Sedclass Trace; 41193323Sedclass CallGraph; 42245431Sdimclass DataLayout; 43198090Srdivackyclass Loop; 44193323Sedclass LoopInfo; 45198090Srdivackyclass AllocaInst; 46193323Sed 47193323Sed/// CloneModule - Return an exact copy of the specified module 48193323Sed/// 49193323SedModule *CloneModule(const Module *M); 50218893SdimModule *CloneModule(const Module *M, ValueToValueMapTy &VMap); 51193323Sed 52193323Sed/// ClonedCodeInfo - This struct can be used to capture information about code 53193323Sed/// being cloned, while it is being cloned. 54193323Sedstruct ClonedCodeInfo { 55193323Sed /// ContainsCalls - This is set to true if the cloned code contains a normal 56193323Sed /// call instruction. 57193323Sed bool ContainsCalls; 58193323Sed 59193323Sed /// ContainsDynamicAllocas - This is set to true if the cloned code contains 60193323Sed /// a 'dynamic' alloca. Dynamic allocas are allocas that are either not in 61193323Sed /// the entry block or they are in the entry block but are not a constant 62193323Sed /// size. 63193323Sed bool ContainsDynamicAllocas; 64193323Sed 65235633Sdim ClonedCodeInfo() : ContainsCalls(false), ContainsDynamicAllocas(false) {} 66193323Sed}; 67193323Sed 68193323Sed 69193323Sed/// CloneBasicBlock - Return a copy of the specified basic block, but without 70193323Sed/// embedding the block into a particular function. The block returned is an 71193323Sed/// exact copy of the specified basic block, without any remapping having been 72193323Sed/// performed. Because of this, this is only suitable for applications where 73193323Sed/// the basic block will be inserted into the same function that it was cloned 74193323Sed/// from (loop unrolling would use this, for example). 75193323Sed/// 76193323Sed/// Also, note that this function makes a direct copy of the basic block, and 77193323Sed/// can thus produce illegal LLVM code. In particular, it will copy any PHI 78193323Sed/// nodes from the original block, even though there are no predecessors for the 79193323Sed/// newly cloned block (thus, phi nodes will have to be updated). Also, this 80193323Sed/// block will branch to the old successors of the original block: these 81193323Sed/// successors will have to have any PHI nodes updated to account for the new 82193323Sed/// incoming edges. 83193323Sed/// 84193323Sed/// The correlation between instructions in the source and result basic blocks 85210299Sed/// is recorded in the VMap map. 86193323Sed/// 87193323Sed/// If you have a particular suffix you'd like to use to add to any cloned 88193323Sed/// names, specify it as the optional third parameter. 89193323Sed/// 90193323Sed/// If you would like the basic block to be auto-inserted into the end of a 91193323Sed/// function, you can specify it as the optional fourth parameter. 92193323Sed/// 93193323Sed/// If you would like to collect additional information about the cloned 94193323Sed/// function, you can specify a ClonedCodeInfo object with the optional fifth 95193323Sed/// parameter. 96193323Sed/// 97193323SedBasicBlock *CloneBasicBlock(const BasicBlock *BB, 98218893Sdim ValueToValueMapTy &VMap, 99203954Srdivacky const Twine &NameSuffix = "", Function *F = 0, 100193323Sed ClonedCodeInfo *CodeInfo = 0); 101193323Sed 102193323Sed/// CloneFunction - Return a copy of the specified function, but without 103193323Sed/// embedding the function into another module. Also, any references specified 104210299Sed/// in the VMap are changed to refer to their mapped value instead of the 105210299Sed/// original one. If any of the arguments to the function are in the VMap, 106210299Sed/// the arguments are deleted from the resultant function. The VMap is 107193323Sed/// updated to include mappings from all of the instructions and basicblocks in 108193323Sed/// the function from their old to new values. The final argument captures 109193323Sed/// information about the cloned code if non-null. 110193323Sed/// 111212904Sdim/// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue 112212904Sdim/// mappings. 113212904Sdim/// 114193323SedFunction *CloneFunction(const Function *F, 115218893Sdim ValueToValueMapTy &VMap, 116212904Sdim bool ModuleLevelChanges, 117193323Sed ClonedCodeInfo *CodeInfo = 0); 118193323Sed 119193323Sed/// Clone OldFunc into NewFunc, transforming the old arguments into references 120212904Sdim/// to VMap values. Note that if NewFunc already has basic blocks, the ones 121193323Sed/// cloned into it will be added to the end of the function. This function 122235633Sdim/// fills in a list of return instructions, and can optionally remap types 123235633Sdim/// and/or append the specified suffix to all values cloned. 124193323Sed/// 125212904Sdim/// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue 126212904Sdim/// mappings. 127212904Sdim/// 128193323Sedvoid CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 129218893Sdim ValueToValueMapTy &VMap, 130212904Sdim bool ModuleLevelChanges, 131198090Srdivacky SmallVectorImpl<ReturnInst*> &Returns, 132193323Sed const char *NameSuffix = "", 133235633Sdim ClonedCodeInfo *CodeInfo = 0, 134263509Sdim ValueMapTypeRemapper *TypeMapper = 0, 135263509Sdim ValueMaterializer *Materializer = 0); 136193323Sed 137193323Sed/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, 138193323Sed/// except that it does some simple constant prop and DCE on the fly. The 139193323Sed/// effect of this is to copy significantly less code in cases where (for 140193323Sed/// example) a function call with constant arguments is inlined, and those 141193323Sed/// constant arguments cause a significant amount of code in the callee to be 142193323Sed/// dead. Since this doesn't produce an exactly copy of the input, it can't be 143193323Sed/// used for things like CloneFunction or CloneModule. 144212904Sdim/// 145212904Sdim/// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue 146212904Sdim/// mappings. 147212904Sdim/// 148193323Sedvoid CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 149218893Sdim ValueToValueMapTy &VMap, 150212904Sdim bool ModuleLevelChanges, 151198090Srdivacky SmallVectorImpl<ReturnInst*> &Returns, 152193323Sed const char *NameSuffix = "", 153193323Sed ClonedCodeInfo *CodeInfo = 0, 154245431Sdim const DataLayout *TD = 0, 155199481Srdivacky Instruction *TheCall = 0); 156193323Sed 157207618Srdivacky 158207618Srdivacky/// InlineFunctionInfo - This class captures the data input to the 159207618Srdivacky/// InlineFunction call, and records the auxiliary results produced by it. 160207618Srdivackyclass InlineFunctionInfo { 161207618Srdivackypublic: 162245431Sdim explicit InlineFunctionInfo(CallGraph *cg = 0, const DataLayout *td = 0) 163207618Srdivacky : CG(cg), TD(td) {} 164207618Srdivacky 165207618Srdivacky /// CG - If non-null, InlineFunction will update the callgraph to reflect the 166207618Srdivacky /// changes it makes. 167207618Srdivacky CallGraph *CG; 168245431Sdim const DataLayout *TD; 169207618Srdivacky 170207618Srdivacky /// StaticAllocas - InlineFunction fills this in with all static allocas that 171207618Srdivacky /// get copied into the caller. 172207618Srdivacky SmallVector<AllocaInst*, 4> StaticAllocas; 173207618Srdivacky 174207618Srdivacky /// InlinedCalls - InlineFunction fills this in with callsites that were 175207618Srdivacky /// inlined from the callee. This is only filled in if CG is non-null. 176207618Srdivacky SmallVector<WeakVH, 8> InlinedCalls; 177207618Srdivacky 178207618Srdivacky void reset() { 179207618Srdivacky StaticAllocas.clear(); 180207618Srdivacky InlinedCalls.clear(); 181207618Srdivacky } 182207618Srdivacky}; 183207618Srdivacky 184193323Sed/// InlineFunction - This function inlines the called function into the basic 185193323Sed/// block of the caller. This returns false if it is not possible to inline 186193323Sed/// this call. The program is still in a well defined state if this occurs 187193323Sed/// though. 188193323Sed/// 189193323Sed/// Note that this only does one level of inlining. For example, if the 190193323Sed/// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 191221345Sdim/// exists in the instruction stream. Similarly this will inline a recursive 192193323Sed/// function by one level. 193193323Sed/// 194235633Sdimbool InlineFunction(CallInst *C, InlineFunctionInfo &IFI, bool InsertLifetime = true); 195235633Sdimbool InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI, bool InsertLifetime = true); 196235633Sdimbool InlineFunction(CallSite CS, InlineFunctionInfo &IFI, bool InsertLifetime = true); 197193323Sed 198193323Sed} // End llvm namespace 199193323Sed 200193323Sed#endif 201