LLVM API Documentation
00001 //===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines various functions that are used to clone chunks of LLVM 00011 // code for various purposes. This varies from copying whole modules into new 00012 // modules, to cloning functions with different arguments, to inlining 00013 // functions, to copying basic blocks to support loop unrolling or superblock 00014 // formation, etc. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H 00019 #define LLVM_TRANSFORMS_UTILS_CLONING_H 00020 00021 #include <vector> 00022 #include "llvm/ADT/DenseMap.h" 00023 00024 namespace llvm { 00025 00026 class Module; 00027 class Function; 00028 class Pass; 00029 class LPPassManager; 00030 class BasicBlock; 00031 class Value; 00032 class CallInst; 00033 class InvokeInst; 00034 class ReturnInst; 00035 class CallSite; 00036 class Trace; 00037 class CallGraph; 00038 class TargetData; 00039 class LoopInfo; 00040 template<class N> class LoopBase; 00041 typedef LoopBase<BasicBlock> Loop; 00042 00043 /// CloneModule - Return an exact copy of the specified module 00044 /// 00045 Module *CloneModule(const Module *M); 00046 Module *CloneModule(const Module *M, DenseMap<const Value*, Value*> &ValueMap); 00047 00048 /// ClonedCodeInfo - This struct can be used to capture information about code 00049 /// being cloned, while it is being cloned. 00050 struct ClonedCodeInfo { 00051 /// ContainsCalls - This is set to true if the cloned code contains a normal 00052 /// call instruction. 00053 bool ContainsCalls; 00054 00055 /// ContainsUnwinds - This is set to true if the cloned code contains an 00056 /// unwind instruction. 00057 bool ContainsUnwinds; 00058 00059 /// ContainsDynamicAllocas - This is set to true if the cloned code contains 00060 /// a 'dynamic' alloca. Dynamic allocas are allocas that are either not in 00061 /// the entry block or they are in the entry block but are not a constant 00062 /// size. 00063 bool ContainsDynamicAllocas; 00064 00065 ClonedCodeInfo() { 00066 ContainsCalls = false; 00067 ContainsUnwinds = false; 00068 ContainsDynamicAllocas = false; 00069 } 00070 }; 00071 00072 00073 /// CloneBasicBlock - Return a copy of the specified basic block, but without 00074 /// embedding the block into a particular function. The block returned is an 00075 /// exact copy of the specified basic block, without any remapping having been 00076 /// performed. Because of this, this is only suitable for applications where 00077 /// the basic block will be inserted into the same function that it was cloned 00078 /// from (loop unrolling would use this, for example). 00079 /// 00080 /// Also, note that this function makes a direct copy of the basic block, and 00081 /// can thus produce illegal LLVM code. In particular, it will copy any PHI 00082 /// nodes from the original block, even though there are no predecessors for the 00083 /// newly cloned block (thus, phi nodes will have to be updated). Also, this 00084 /// block will branch to the old successors of the original block: these 00085 /// successors will have to have any PHI nodes updated to account for the new 00086 /// incoming edges. 00087 /// 00088 /// The correlation between instructions in the source and result basic blocks 00089 /// is recorded in the ValueMap map. 00090 /// 00091 /// If you have a particular suffix you'd like to use to add to any cloned 00092 /// names, specify it as the optional third parameter. 00093 /// 00094 /// If you would like the basic block to be auto-inserted into the end of a 00095 /// function, you can specify it as the optional fourth parameter. 00096 /// 00097 /// If you would like to collect additional information about the cloned 00098 /// function, you can specify a ClonedCodeInfo object with the optional fifth 00099 /// parameter. 00100 /// 00101 BasicBlock *CloneBasicBlock(const BasicBlock *BB, 00102 DenseMap<const Value*, Value*> &ValueMap, 00103 const char *NameSuffix = "", Function *F = 0, 00104 ClonedCodeInfo *CodeInfo = 0); 00105 00106 00107 /// CloneLoop - Clone Loop. Clone dominator info for loop insiders. Populate ValueMap 00108 /// using old blocks to new blocks mapping. 00109 Loop *CloneLoop(Loop *L, LPPassManager *LPM, LoopInfo *LI, 00110 DenseMap<const Value *, Value *> &ValueMap, Pass *P); 00111 00112 /// CloneFunction - Return a copy of the specified function, but without 00113 /// embedding the function into another module. Also, any references specified 00114 /// in the ValueMap are changed to refer to their mapped value instead of the 00115 /// original one. If any of the arguments to the function are in the ValueMap, 00116 /// the arguments are deleted from the resultant function. The ValueMap is 00117 /// updated to include mappings from all of the instructions and basicblocks in 00118 /// the function from their old to new values. The final argument captures 00119 /// information about the cloned code if non-null. 00120 /// 00121 Function *CloneFunction(const Function *F, 00122 DenseMap<const Value*, Value*> &ValueMap, 00123 ClonedCodeInfo *CodeInfo = 0); 00124 00125 /// CloneFunction - Version of the function that doesn't need the ValueMap. 00126 /// 00127 inline Function *CloneFunction(const Function *F, ClonedCodeInfo *CodeInfo = 0){ 00128 DenseMap<const Value*, Value*> ValueMap; 00129 return CloneFunction(F, ValueMap, CodeInfo); 00130 } 00131 00132 /// Clone OldFunc into NewFunc, transforming the old arguments into references 00133 /// to ArgMap values. Note that if NewFunc already has basic blocks, the ones 00134 /// cloned into it will be added to the end of the function. This function 00135 /// fills in a list of return instructions, and can optionally append the 00136 /// specified suffix to all values cloned. 00137 /// 00138 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 00139 DenseMap<const Value*, Value*> &ValueMap, 00140 std::vector<ReturnInst*> &Returns, 00141 const char *NameSuffix = "", 00142 ClonedCodeInfo *CodeInfo = 0); 00143 00144 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, 00145 /// except that it does some simple constant prop and DCE on the fly. The 00146 /// effect of this is to copy significantly less code in cases where (for 00147 /// example) a function call with constant arguments is inlined, and those 00148 /// constant arguments cause a significant amount of code in the callee to be 00149 /// dead. Since this doesn't produce an exactly copy of the input, it can't be 00150 /// used for things like CloneFunction or CloneModule. 00151 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 00152 DenseMap<const Value*, Value*> &ValueMap, 00153 std::vector<ReturnInst*> &Returns, 00154 const char *NameSuffix = "", 00155 ClonedCodeInfo *CodeInfo = 0, 00156 const TargetData *TD = 0); 00157 00158 00159 /// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is 00160 /// saved in ValueMap. 00161 /// 00162 void CloneTraceInto(Function *NewFunc, Trace &T, 00163 DenseMap<const Value*, Value*> &ValueMap, 00164 const char *NameSuffix); 00165 00166 /// CloneTrace - Returns a copy of the specified trace. 00167 /// It takes a vector of basic blocks clones the basic blocks, removes internal 00168 /// phi nodes, adds it to the same function as the original (although there is 00169 /// no jump to it) and returns the new vector of basic blocks. 00170 std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace); 00171 00172 /// InlineFunction - This function inlines the called function into the basic 00173 /// block of the caller. This returns false if it is not possible to inline 00174 /// this call. The program is still in a well defined state if this occurs 00175 /// though. 00176 /// 00177 /// Note that this only does one level of inlining. For example, if the 00178 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 00179 /// exists in the instruction stream. Similiarly this will inline a recursive 00180 /// function by one level. 00181 /// 00182 /// If a non-null callgraph pointer is provided, these functions update the 00183 /// CallGraph to represent the program after inlining. 00184 /// 00185 bool InlineFunction(CallInst *C, CallGraph *CG = 0, const TargetData *TD = 0); 00186 bool InlineFunction(InvokeInst *II, CallGraph *CG = 0, const TargetData *TD =0); 00187 bool InlineFunction(CallSite CS, CallGraph *CG = 0, const TargetData *TD = 0); 00188 00189 } // End llvm namespace 00190 00191 #endif