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