LLVM API Documentation

Cloning.h
Go to the documentation of this file.
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 "llvm/ADT/SmallVector.h"
00022 #include "llvm/ADT/Twine.h"
00023 #include "llvm/IR/ValueHandle.h"
00024 #include "llvm/IR/ValueMap.h"
00025 #include "llvm/Transforms/Utils/ValueMapper.h"
00026 
00027 namespace llvm {
00028 
00029 class Module;
00030 class Function;
00031 class Instruction;
00032 class Pass;
00033 class LPPassManager;
00034 class BasicBlock;
00035 class Value;
00036 class CallInst;
00037 class InvokeInst;
00038 class ReturnInst;
00039 class CallSite;
00040 class Trace;
00041 class CallGraph;
00042 class DataLayout;
00043 class Loop;
00044 class LoopInfo;
00045 class AllocaInst;
00046 class AliasAnalysis;
00047 class AssumptionTracker;
00048 
00049 /// CloneModule - Return an exact copy of the specified module
00050 ///
00051 Module *CloneModule(const Module *M);
00052 Module *CloneModule(const Module *M, ValueToValueMapTy &VMap);
00053 
00054 /// ClonedCodeInfo - This struct can be used to capture information about code
00055 /// being cloned, while it is being cloned.
00056 struct ClonedCodeInfo {
00057   /// ContainsCalls - This is set to true if the cloned code contains a normal
00058   /// call instruction.
00059   bool ContainsCalls;
00060 
00061   /// ContainsDynamicAllocas - This is set to true if the cloned code contains
00062   /// a 'dynamic' alloca.  Dynamic allocas are allocas that are either not in
00063   /// the entry block or they are in the entry block but are not a constant
00064   /// size.
00065   bool ContainsDynamicAllocas;
00066 
00067   ClonedCodeInfo() : ContainsCalls(false), ContainsDynamicAllocas(false) {}
00068 };
00069 
00070 /// CloneBasicBlock - Return a copy of the specified basic block, but without
00071 /// embedding the block into a particular function.  The block returned is an
00072 /// exact copy of the specified basic block, without any remapping having been
00073 /// performed.  Because of this, this is only suitable for applications where
00074 /// the basic block will be inserted into the same function that it was cloned
00075 /// from (loop unrolling would use this, for example).
00076 ///
00077 /// Also, note that this function makes a direct copy of the basic block, and
00078 /// can thus produce illegal LLVM code.  In particular, it will copy any PHI
00079 /// nodes from the original block, even though there are no predecessors for the
00080 /// newly cloned block (thus, phi nodes will have to be updated).  Also, this
00081 /// block will branch to the old successors of the original block: these
00082 /// successors will have to have any PHI nodes updated to account for the new
00083 /// incoming edges.
00084 ///
00085 /// The correlation between instructions in the source and result basic blocks
00086 /// is recorded in the VMap map.
00087 ///
00088 /// If you have a particular suffix you'd like to use to add to any cloned
00089 /// names, specify it as the optional third parameter.
00090 ///
00091 /// If you would like the basic block to be auto-inserted into the end of a
00092 /// function, you can specify it as the optional fourth parameter.
00093 ///
00094 /// If you would like to collect additional information about the cloned
00095 /// function, you can specify a ClonedCodeInfo object with the optional fifth
00096 /// parameter.
00097 ///
00098 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
00099                             ValueToValueMapTy &VMap,
00100                             const Twine &NameSuffix = "", Function *F = nullptr,
00101                             ClonedCodeInfo *CodeInfo = nullptr);
00102 
00103 /// CloneFunction - Return a copy of the specified function, but without
00104 /// embedding the function into another module.  Also, any references specified
00105 /// in the VMap are changed to refer to their mapped value instead of the
00106 /// original one.  If any of the arguments to the function are in the VMap,
00107 /// the arguments are deleted from the resultant function.  The VMap is
00108 /// updated to include mappings from all of the instructions and basicblocks in
00109 /// the function from their old to new values.  The final argument captures
00110 /// information about the cloned code if non-null.
00111 ///
00112 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
00113 /// mappings, and debug info metadata will not be cloned.
00114 ///
00115 Function *CloneFunction(const Function *F,
00116                         ValueToValueMapTy &VMap,
00117                         bool ModuleLevelChanges,
00118                         ClonedCodeInfo *CodeInfo = nullptr);
00119 
00120 /// Clone OldFunc into NewFunc, transforming the old arguments into references
00121 /// to VMap 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 remap types
00124 /// and/or append the specified suffix to all values cloned.
00125 ///
00126 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
00127 /// mappings.
00128 ///
00129 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
00130                        ValueToValueMapTy &VMap,
00131                        bool ModuleLevelChanges,
00132                        SmallVectorImpl<ReturnInst*> &Returns,
00133                        const char *NameSuffix = "",
00134                        ClonedCodeInfo *CodeInfo = nullptr,
00135                        ValueMapTypeRemapper *TypeMapper = nullptr,
00136                        ValueMaterializer *Materializer = nullptr);
00137 
00138 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
00139 /// except that it does some simple constant prop and DCE on the fly.  The
00140 /// effect of this is to copy significantly less code in cases where (for
00141 /// example) a function call with constant arguments is inlined, and those
00142 /// constant arguments cause a significant amount of code in the callee to be
00143 /// dead.  Since this doesn't produce an exactly copy of the input, it can't be
00144 /// used for things like CloneFunction or CloneModule.
00145 ///
00146 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
00147 /// mappings.
00148 ///
00149 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
00150                                ValueToValueMapTy &VMap,
00151                                bool ModuleLevelChanges,
00152                                SmallVectorImpl<ReturnInst*> &Returns,
00153                                const char *NameSuffix = "",
00154                                ClonedCodeInfo *CodeInfo = nullptr,
00155                                const DataLayout *DL = nullptr,
00156                                Instruction *TheCall = nullptr);
00157 
00158 /// InlineFunctionInfo - This class captures the data input to the
00159 /// InlineFunction call, and records the auxiliary results produced by it.
00160 class InlineFunctionInfo {
00161 public:
00162   explicit InlineFunctionInfo(CallGraph *cg = nullptr,
00163                               const DataLayout *DL = nullptr,
00164                               AliasAnalysis *AA = nullptr,
00165                               AssumptionTracker *AT = nullptr)
00166     : CG(cg), DL(DL), AA(AA), AT(AT) {}
00167 
00168   /// CG - If non-null, InlineFunction will update the callgraph to reflect the
00169   /// changes it makes.
00170   CallGraph *CG;
00171   const DataLayout *DL;
00172   AliasAnalysis *AA;
00173   AssumptionTracker *AT;
00174 
00175   /// StaticAllocas - InlineFunction fills this in with all static allocas that
00176   /// get copied into the caller.
00177   SmallVector<AllocaInst*, 4> StaticAllocas;
00178 
00179   /// InlinedCalls - InlineFunction fills this in with callsites that were
00180   /// inlined from the callee.  This is only filled in if CG is non-null.
00181   SmallVector<WeakVH, 8> InlinedCalls;
00182 
00183   void reset() {
00184     StaticAllocas.clear();
00185     InlinedCalls.clear();
00186   }
00187 };
00188 
00189 /// InlineFunction - This function inlines the called function into the basic
00190 /// block of the caller.  This returns false if it is not possible to inline
00191 /// this call.  The program is still in a well defined state if this occurs
00192 /// though.
00193 ///
00194 /// Note that this only does one level of inlining.  For example, if the
00195 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
00196 /// exists in the instruction stream.  Similarly this will inline a recursive
00197 /// function by one level.
00198 ///
00199 bool InlineFunction(CallInst *C, InlineFunctionInfo &IFI, bool InsertLifetime = true);
00200 bool InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI, bool InsertLifetime = true);
00201 bool InlineFunction(CallSite CS, InlineFunctionInfo &IFI, bool InsertLifetime = true);
00202 
00203 } // End llvm namespace
00204 
00205 #endif