1 //===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===//
3 // This file defines various functions that are used to clone chunks of LLVM
4 // code for various purposes. This varies from copying whole modules into new
5 // modules, to cloning functions with different arguments, to inlining
6 // functions, to copying basic blocks to support loop unrolling or superblock
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H
12 #define LLVM_TRANSFORMS_UTILS_CLONING_H
25 /// CloneModule - Return an exact copy of the specified module
27 Module *CloneModule(const Module *M);
29 /// CloneBasicBlock - Return a copy of the specified basic block, but without
30 /// embedding the block into a particular function. The block returned is an
31 /// exact copy of the specified basic block, without any remapping having been
32 /// performed. Because of this, this is only suitable for applications where
33 /// the basic block will be inserted into the same function that it was cloned
34 /// from (loop unrolling would use this, for example).
36 /// Also, note that this function makes a direct copy of the basic block, and
37 /// can thus produce illegal LLVM code. In particular, it will copy any PHI
38 /// nodes from the original block, even though there are no predecessors for the
39 /// newly cloned block (thus, phi nodes will have to be updated). Also, this
40 /// block will branch to the old successors of the original block: these
41 /// successors will have to have any PHI nodes updated to account for the new
44 /// The correlation between instructions in the source and result basic blocks
45 /// is recorded in the ValueMap map.
47 /// If you have a particular suffix you'd like to use to add to any cloned
48 /// names, specify it as the optional second parameter.
50 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
51 std::map<const Value*, Value*> &ValueMap,
52 const char *NameSuffix = "");
55 /// CloneFunction - Return a copy of the specified function, but without
56 /// embedding the function into another module. Also, any references specified
57 /// in the ValueMap are changed to refer to their mapped value instead of the
58 /// original one. If any of the arguments to the function are in the ValueMap,
59 /// the arguments are deleted from the resultant function. The ValueMap is
60 /// updated to include mappings from all of the instructions and basicblocks in
61 /// the function from their old to new values.
63 Function *CloneFunction(const Function *F,
64 std::map<const Value*, Value*> &ValueMap);
66 /// CloneFunction - Version of the function that doesn't need the ValueMap.
68 inline Function *CloneFunction(const Function *F) {
69 std::map<const Value*, Value*> ValueMap;
70 return CloneFunction(F, ValueMap);
73 /// Clone OldFunc into NewFunc, transforming the old arguments into references
74 /// to ArgMap values. Note that if NewFunc already has basic blocks, the ones
75 /// cloned into it will be added to the end of the function. This function
76 /// fills in a list of return instructions, and can optionally append the
77 /// specified suffix to all values cloned.
79 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
80 std::map<const Value*, Value*> &ValueMap,
81 std::vector<ReturnInst*> &Returns,
82 const char *NameSuffix = "");
85 /// InlineFunction - This function inlines the called function into the basic
86 /// block of the caller. This returns false if it is not possible to inline
87 /// this call. The program is still in a well defined state if this occurs
90 /// Note that this only does one level of inlining. For example, if the
91 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
92 /// exists in the instruction stream. Similiarly this will inline a recursive
93 /// function by one level.
95 bool InlineFunction(CallInst *C);
96 bool InlineFunction(InvokeInst *II);
97 bool InlineFunction(CallSite CS);
99 /// CloneTrace - Returns a copy of the specified trace.
100 /// It takes a vector of basic blocks clones the basic blocks, removes internal
101 /// phi nodes, adds it to the same function as the original (although there is
102 /// no jump to it) and returns the new vector of basic blocks.
103 std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace);