Add prototype for new CloneBasicBlock function
[oota-llvm.git] / include / llvm / Transforms / Utils / Cloning.h
1 //===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===//
2 //
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
7 // formation, etc.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_TRANSFORMS_UTILS_CLONING_H
12 #define LLVM_TRANSFORMS_UTILS_CLONING_H
13
14 #include <vector>
15 #include <map>
16 class Module;
17 class Function;
18 class BasicBlock;
19 class Value;
20 class CallInst;
21 class ReturnInst;
22
23 /// CloneModule - Return an exact copy of the specified module
24 ///
25 Module *CloneModule(const Module *M);
26
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).
33 ///
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
40 /// incoming edges.
41 ///
42 /// The correlation between instructions in the source and result basic blocks
43 /// is recorded in the ValueMap map.
44 ///
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.
47 ///
48 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
49                             std::map<const Value*, Value*> &ValueMap,
50                             const char *NameSuffix = "");
51
52
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.
60 ///
61 Function *CloneFunction(const Function *F,
62                         std::map<const Value*, Value*> &ValueMap);
63
64 /// CloneFunction - Version of the function that doesn't need the ValueMap.
65 ///
66 inline Function *CloneFunction(const Function *F) {
67   std::map<const Value*, Value*> ValueMap;
68   return CloneFunction(F, ValueMap);
69 }
70
71 // Clone OldFunc into NewFunc, transforming the old arguments into references to
72 // 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 fills
74 // in a list of return instructions, and can optionally append the specified
75 // suffix to all values cloned.
76 //
77 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
78                        std::map<const Value*, Value*> &ValueMap,
79                        std::vector<ReturnInst*> &Returns,
80                        const char *NameSuffix = "");
81
82
83 // InlineFunction - This function forcibly inlines the called function into the
84 // basic block of the caller.  This returns true if it is not possible to inline
85 // this call.  The program is still in a well defined state if this occurs 
86 // though.
87 //
88 // Note that this only does one level of inlining.  For example, if the 
89 // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 
90 // exists in the instruction stream.  Similiarly this will inline a recursive
91 // function by one level.
92 //
93 bool InlineFunction(CallInst *C);
94
95 #endif