Cloning.h revision cb2b3e5005c09f24afeb0dab49ac8bc1967e491a
184bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner//===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===//
26fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
56fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// This file was developed by the LLVM research group and is distributed under
66fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
76fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
984bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner//
1084bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// This file defines various functions that are used to clone chunks of LLVM
1184bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// code for various purposes.  This varies from copying whole modules into new
1284bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// modules, to cloning functions with different arguments, to inlining
1384bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// functions, to copying basic blocks to support loop unrolling or superblock
1484bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// formation, etc.
1584bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner//
1684bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner//===----------------------------------------------------------------------===//
1784bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner
18a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner#ifndef LLVM_TRANSFORMS_UTILS_CLONING_H
19a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner#define LLVM_TRANSFORMS_UTILS_CLONING_H
2084bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner
2184bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner#include <vector>
22d18015599cbe09dd327b5f73501581a865bf27daChris Lattner#include <map>
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
2684bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass Module;
2784bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass Function;
2884bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass BasicBlock;
2984bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass Value;
3084bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass CallInst;
319d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerclass InvokeInst;
321c9985067bfedc219c1a6128bfc703cf3894b866Chris Lattnerclass ReturnInst;
339d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerclass CallSite;
3484bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner
35d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// CloneModule - Return an exact copy of the specified module
36d18015599cbe09dd327b5f73501581a865bf27daChris Lattner///
37d4fd3978056928b156bc51bff7cd4eb825bffb1aChris LattnerModule *CloneModule(const Module *M);
38d18015599cbe09dd327b5f73501581a865bf27daChris Lattner
39a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// CloneBasicBlock - Return a copy of the specified basic block, but without
40a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// embedding the block into a particular function.  The block returned is an
41a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// exact copy of the specified basic block, without any remapping having been
42a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// performed.  Because of this, this is only suitable for applications where
43a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// the basic block will be inserted into the same function that it was cloned
44a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// from (loop unrolling would use this, for example).
45a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner///
46a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// Also, note that this function makes a direct copy of the basic block, and
47a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// can thus produce illegal LLVM code.  In particular, it will copy any PHI
48a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// nodes from the original block, even though there are no predecessors for the
49a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// newly cloned block (thus, phi nodes will have to be updated).  Also, this
50a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// block will branch to the old successors of the original block: these
51a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// successors will have to have any PHI nodes updated to account for the new
52a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// incoming edges.
53a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner///
54a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// The correlation between instructions in the source and result basic blocks
55a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// is recorded in the ValueMap map.
56a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner///
57a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// If you have a particular suffix you'd like to use to add to any cloned
58cb2b3e5005c09f24afeb0dab49ac8bc1967e491aChris Lattner/// names, specify it as the optional third parameter.
59cb2b3e5005c09f24afeb0dab49ac8bc1967e491aChris Lattner///
60cb2b3e5005c09f24afeb0dab49ac8bc1967e491aChris Lattner/// If you would like the basic block to be auto-inserted into the end of a
61cb2b3e5005c09f24afeb0dab49ac8bc1967e491aChris Lattner/// function, you can specify it as the optional fourth parameter.
62a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner///
63a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris LattnerBasicBlock *CloneBasicBlock(const BasicBlock *BB,
64a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner                            std::map<const Value*, Value*> &ValueMap,
65cb2b3e5005c09f24afeb0dab49ac8bc1967e491aChris Lattner                            const char *NameSuffix = "", Function *F = 0);
66a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner
67a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner
68d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// CloneFunction - Return a copy of the specified function, but without
69d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// embedding the function into another module.  Also, any references specified
70d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// in the ValueMap are changed to refer to their mapped value instead of the
71d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// original one.  If any of the arguments to the function are in the ValueMap,
72d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// the arguments are deleted from the resultant function.  The ValueMap is
73d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// updated to include mappings from all of the instructions and basicblocks in
74d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// the function from their old to new values.
75d18015599cbe09dd327b5f73501581a865bf27daChris Lattner///
76d18015599cbe09dd327b5f73501581a865bf27daChris LattnerFunction *CloneFunction(const Function *F,
77d18015599cbe09dd327b5f73501581a865bf27daChris Lattner                        std::map<const Value*, Value*> &ValueMap);
78d18015599cbe09dd327b5f73501581a865bf27daChris Lattner
79d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// CloneFunction - Version of the function that doesn't need the ValueMap.
80d18015599cbe09dd327b5f73501581a865bf27daChris Lattner///
81d18015599cbe09dd327b5f73501581a865bf27daChris Lattnerinline Function *CloneFunction(const Function *F) {
82d18015599cbe09dd327b5f73501581a865bf27daChris Lattner  std::map<const Value*, Value*> ValueMap;
83d18015599cbe09dd327b5f73501581a865bf27daChris Lattner  return CloneFunction(F, ValueMap);
84d18015599cbe09dd327b5f73501581a865bf27daChris Lattner}
85d18015599cbe09dd327b5f73501581a865bf27daChris Lattner
86aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// Clone OldFunc into NewFunc, transforming the old arguments into references
87aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// to ArgMap values.  Note that if NewFunc already has basic blocks, the ones
88aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// cloned into it will be added to the end of the function.  This function
89aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// fills in a list of return instructions, and can optionally append the
90aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// specified suffix to all values cloned.
91aa101c3147debcba3f0441c80b477782e456a03bChris Lattner///
9284bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnervoid CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
93d18015599cbe09dd327b5f73501581a865bf27daChris Lattner                       std::map<const Value*, Value*> &ValueMap,
941c9985067bfedc219c1a6128bfc703cf3894b866Chris Lattner                       std::vector<ReturnInst*> &Returns,
951c9985067bfedc219c1a6128bfc703cf3894b866Chris Lattner                       const char *NameSuffix = "");
9684bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner
9784bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner
98aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// InlineFunction - This function inlines the called function into the basic
99a04e51f4c0dd981af9b8f6e8b219df495cccaa45Chris Lattner/// block of the caller.  This returns false if it is not possible to inline
100a04e51f4c0dd981af9b8f6e8b219df495cccaa45Chris Lattner/// this call.  The program is still in a well defined state if this occurs
101a04e51f4c0dd981af9b8f6e8b219df495cccaa45Chris Lattner/// though.
102aa101c3147debcba3f0441c80b477782e456a03bChris Lattner///
103aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// Note that this only does one level of inlining.  For example, if the
104aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
105aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// exists in the instruction stream.  Similiarly this will inline a recursive
106aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// function by one level.
107aa101c3147debcba3f0441c80b477782e456a03bChris Lattner///
10884bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerbool InlineFunction(CallInst *C);
1099d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerbool InlineFunction(InvokeInst *II);
1109d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerbool InlineFunction(CallSite CS);
1112c49fc023ee0885c8c577829cd40c00ef48581fcTanya Lattner
112658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// CloneTrace - Returns a copy of the specified trace.
113658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// It takes a vector of basic blocks clones the basic blocks, removes internal
114658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// phi nodes, adds it to the same function as the original (although there is
115658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// no jump to it) and returns the new vector of basic blocks.
116658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattnerstd::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace);
1172c49fc023ee0885c8c577829cd40c00ef48581fcTanya Lattner
118d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
119d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
12084bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner#endif
121