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 00036 /// CloneModule - Return an exact copy of the specified module 00037 /// 00038 Module *CloneModule(const Module *M); 00039 00040 /// CloneBasicBlock - Return a copy of the specified basic block, but without 00041 /// embedding the block into a particular function. The block returned is an 00042 /// exact copy of the specified basic block, without any remapping having been 00043 /// performed. Because of this, this is only suitable for applications where 00044 /// the basic block will be inserted into the same function that it was cloned 00045 /// from (loop unrolling would use this, for example). 00046 /// 00047 /// Also, note that this function makes a direct copy of the basic block, and 00048 /// can thus produce illegal LLVM code. In particular, it will copy any PHI 00049 /// nodes from the original block, even though there are no predecessors for the 00050 /// newly cloned block (thus, phi nodes will have to be updated). Also, this 00051 /// block will branch to the old successors of the original block: these 00052 /// successors will have to have any PHI nodes updated to account for the new 00053 /// incoming edges. 00054 /// 00055 /// The correlation between instructions in the source and result basic blocks 00056 /// is recorded in the ValueMap map. 00057 /// 00058 /// If you have a particular suffix you'd like to use to add to any cloned 00059 /// names, specify it as the optional third parameter. 00060 /// 00061 /// If you would like the basic block to be auto-inserted into the end of a 00062 /// function, you can specify it as the optional fourth parameter. 00063 /// 00064 BasicBlock *CloneBasicBlock(const BasicBlock *BB, 00065 std::map<const Value*, Value*> &ValueMap, 00066 const char *NameSuffix = "", Function *F = 0); 00067 00068 00069 /// CloneFunction - Return a copy of the specified function, but without 00070 /// embedding the function into another module. Also, any references specified 00071 /// in the ValueMap are changed to refer to their mapped value instead of the 00072 /// original one. If any of the arguments to the function are in the ValueMap, 00073 /// the arguments are deleted from the resultant function. The ValueMap is 00074 /// updated to include mappings from all of the instructions and basicblocks in 00075 /// the function from their old to new values. 00076 /// 00077 Function *CloneFunction(const Function *F, 00078 std::map<const Value*, Value*> &ValueMap); 00079 00080 /// CloneFunction - Version of the function that doesn't need the ValueMap. 00081 /// 00082 inline Function *CloneFunction(const Function *F) { 00083 std::map<const Value*, Value*> ValueMap; 00084 return CloneFunction(F, ValueMap); 00085 } 00086 00087 /// Clone OldFunc into NewFunc, transforming the old arguments into references 00088 /// to ArgMap values. Note that if NewFunc already has basic blocks, the ones 00089 /// cloned into it will be added to the end of the function. This function 00090 /// fills in a list of return instructions, and can optionally append the 00091 /// specified suffix to all values cloned. 00092 /// 00093 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 00094 std::map<const Value*, Value*> &ValueMap, 00095 std::vector<ReturnInst*> &Returns, 00096 const char *NameSuffix = ""); 00097 00098 00099 /// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is 00100 /// saved in ValueMap. 00101 /// 00102 void CloneTraceInto(Function *NewFunc, Trace &T, 00103 std::map<const Value*, Value*> &ValueMap, 00104 const char *NameSuffix); 00105 00106 /// InlineFunction - This function inlines the called function into the basic 00107 /// block of the caller. This returns false if it is not possible to inline 00108 /// this call. The program is still in a well defined state if this occurs 00109 /// though. 00110 /// 00111 /// Note that this only does one level of inlining. For example, if the 00112 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 00113 /// exists in the instruction stream. Similiarly this will inline a recursive 00114 /// function by one level. 00115 /// 00116 bool InlineFunction(CallInst *C); 00117 bool InlineFunction(InvokeInst *II); 00118 bool InlineFunction(CallSite CS); 00119 00120 /// CloneTrace - Returns a copy of the specified trace. 00121 /// It takes a vector of basic blocks clones the basic blocks, removes internal 00122 /// phi nodes, adds it to the same function as the original (although there is 00123 /// no jump to it) and returns the new vector of basic blocks. 00124 std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace); 00125 00126 } // End llvm namespace 00127 00128 #endif