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