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
23 /// CloneModule - Return an exact copy of the specified module
25 Module *CloneModule(const Module *M);
27 /// CloneBasicBlock - Return a copy of the specified basic block, but without
28 /// embedding the block into a particular function. The block returned is an
29 /// exact copy of the specified basic block, without any remapping having been
30 /// performed. Because of this, this is only suitable for applications where
31 /// the basic block will be inserted into the same function that it was cloned
32 /// from (loop unrolling would use this, for example).
34 /// Also, note that this function makes a direct copy of the basic block, and
35 /// can thus produce illegal LLVM code. In particular, it will copy any PHI
36 /// nodes from the original block, even though there are no predecessors for the
37 /// newly cloned block (thus, phi nodes will have to be updated). Also, this
38 /// block will branch to the old successors of the original block: these
39 /// successors will have to have any PHI nodes updated to account for the new
42 /// The correlation between instructions in the source and result basic blocks
43 /// is recorded in the ValueMap map.
45 /// If you have a particular suffix you'd like to use to add to any cloned
46 /// names, specify it as the optional second parameter.
48 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
49 std::map<const Value*, Value*> &ValueMap,
50 const char *NameSuffix = "");
53 /// CloneFunction - Return a copy of the specified function, but without
54 /// embedding the function into another module. Also, any references specified
55 /// in the ValueMap are changed to refer to their mapped value instead of the
56 /// original one. If any of the arguments to the function are in the ValueMap,
57 /// the arguments are deleted from the resultant function. The ValueMap is
58 /// updated to include mappings from all of the instructions and basicblocks in
59 /// the function from their old to new values.
61 Function *CloneFunction(const Function *F,
62 std::map<const Value*, Value*> &ValueMap);
64 /// CloneFunction - Version of the function that doesn't need the ValueMap.
66 inline Function *CloneFunction(const Function *F) {
67 std::map<const Value*, Value*> ValueMap;
68 return CloneFunction(F, ValueMap);
71 /// Clone OldFunc into NewFunc, transforming the old arguments into references
72 /// to ArgMap values. Note that if NewFunc already has basic blocks, the ones
73 /// cloned into it will be added to the end of the function. This function
74 /// fills in a list of return instructions, and can optionally append the
75 /// specified suffix to all values cloned.
77 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
78 std::map<const Value*, Value*> &ValueMap,
79 std::vector<ReturnInst*> &Returns,
80 const char *NameSuffix = "");
83 /// InlineFunction - This function inlines the called function into the basic
84 /// block of the caller. This returns true if it is not possible to inline this
85 /// call. The program is still in a well defined state if this occurs though.
87 /// Note that this only does one level of inlining. For example, if the
88 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
89 /// exists in the instruction stream. Similiarly this will inline a recursive
90 /// function by one level.
92 bool InlineFunction(CallInst *C);