Add versions of InlineFunction which work on Invoke instructions and general call...
[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 InvokeInst;
22 class ReturnInst;
23 class CallSite;
24
25 /// CloneModule - Return an exact copy of the specified module
26 ///
27 Module *CloneModule(const Module *M);
28
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).
35 ///
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
42 /// incoming edges.
43 ///
44 /// The correlation between instructions in the source and result basic blocks
45 /// is recorded in the ValueMap map.
46 ///
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.
49 ///
50 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
51                             std::map<const Value*, Value*> &ValueMap,
52                             const char *NameSuffix = "");
53
54
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.
62 ///
63 Function *CloneFunction(const Function *F,
64                         std::map<const Value*, Value*> &ValueMap);
65
66 /// CloneFunction - Version of the function that doesn't need the ValueMap.
67 ///
68 inline Function *CloneFunction(const Function *F) {
69   std::map<const Value*, Value*> ValueMap;
70   return CloneFunction(F, ValueMap);
71 }
72
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.
78 ///
79 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
80                        std::map<const Value*, Value*> &ValueMap,
81                        std::vector<ReturnInst*> &Returns,
82                        const char *NameSuffix = "");
83
84
85 /// InlineFunction - This function inlines the called function into the basic
86 /// block of the caller.  This returns true if it is not possible to inline this
87 /// call.  The program is still in a well defined state if this occurs though.
88 ///
89 /// Note that this only does one level of inlining.  For example, if the 
90 /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
91 /// exists in the instruction stream.  Similiarly this will inline a recursive
92 /// function by one level.
93 ///
94 bool InlineFunction(CallInst *C);
95 bool InlineFunction(InvokeInst *II);
96 bool InlineFunction(CallSite CS);
97
98 /// CloneTrace - Returns a copy of the specified trace. 
99 /// It takes a vector of basic blocks clones the basic blocks, removes internal 
100 /// phi nodes, adds it to the same function as the original (although there is 
101 /// no jump to it) and returns the new vector of basic blocks.
102 std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace);
103
104 #endif