Cloning stuff doesn't modify the source module
[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 /// CloneFunction - Return a copy of the specified function, but without
28 /// embedding the function into another module.  Also, any references specified
29 /// in the ValueMap are changed to refer to their mapped value instead of the
30 /// original one.  If any of the arguments to the function are in the ValueMap,
31 /// the arguments are deleted from the resultant function.  The ValueMap is
32 /// updated to include mappings from all of the instructions and basicblocks in
33 /// the function from their old to new values.
34 ///
35 Function *CloneFunction(const Function *F,
36                         std::map<const Value*, Value*> &ValueMap);
37
38 /// CloneFunction - Version of the function that doesn't need the ValueMap.
39 ///
40 inline Function *CloneFunction(const Function *F) {
41   std::map<const Value*, Value*> ValueMap;
42   return CloneFunction(F, ValueMap);
43 }
44
45 // Clone OldFunc into NewFunc, transforming the old arguments into references to
46 // ArgMap values.  Note that if NewFunc already has basic blocks, the ones
47 // cloned into it will be added to the end of the function.  This function fills
48 // in a list of return instructions, and can optionally append the specified
49 // suffix to all values cloned.
50 //
51 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
52                        std::map<const Value*, Value*> &ValueMap,
53                        std::vector<ReturnInst*> &Returns,
54                        const char *NameSuffix = "");
55
56
57 // InlineFunction - This function forcibly inlines the called function into the
58 // basic block of the caller.  This returns true if it is not possible to inline
59 // this call.  The program is still in a well defined state if this occurs 
60 // though.
61 //
62 // Note that this only does one level of inlining.  For example, if the 
63 // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 
64 // exists in the instruction stream.  Similiarly this will inline a recursive
65 // function by one level.
66 //
67 bool InlineFunction(CallInst *C);
68
69 #endif