Cloning.h revision a04e51f4c0dd981af9b8f6e8b219df495cccaa45
184bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner//===- Cloning.h - Clone various parts of LLVM programs ---------*- C++ -*-===// 284bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// 384bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// This file defines various functions that are used to clone chunks of LLVM 484bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// code for various purposes. This varies from copying whole modules into new 584bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// modules, to cloning functions with different arguments, to inlining 684bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// functions, to copying basic blocks to support loop unrolling or superblock 784bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// formation, etc. 884bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner// 984bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner//===----------------------------------------------------------------------===// 1084bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner 11a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner#ifndef LLVM_TRANSFORMS_UTILS_CLONING_H 12a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner#define LLVM_TRANSFORMS_UTILS_CLONING_H 1384bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner 1484bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner#include <vector> 15d18015599cbe09dd327b5f73501581a865bf27daChris Lattner#include <map> 1684bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass Module; 1784bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass Function; 1884bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass BasicBlock; 1984bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass Value; 2084bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerclass CallInst; 219d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerclass InvokeInst; 221c9985067bfedc219c1a6128bfc703cf3894b866Chris Lattnerclass ReturnInst; 239d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerclass CallSite; 2484bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner 25d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// CloneModule - Return an exact copy of the specified module 26d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// 27d4fd3978056928b156bc51bff7cd4eb825bffb1aChris LattnerModule *CloneModule(const Module *M); 28d18015599cbe09dd327b5f73501581a865bf27daChris Lattner 29a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// CloneBasicBlock - Return a copy of the specified basic block, but without 30a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// embedding the block into a particular function. The block returned is an 31a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// exact copy of the specified basic block, without any remapping having been 32a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// performed. Because of this, this is only suitable for applications where 33a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// the basic block will be inserted into the same function that it was cloned 34a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// from (loop unrolling would use this, for example). 35a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// 36a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// Also, note that this function makes a direct copy of the basic block, and 37a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// can thus produce illegal LLVM code. In particular, it will copy any PHI 38a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// nodes from the original block, even though there are no predecessors for the 39a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// newly cloned block (thus, phi nodes will have to be updated). Also, this 40a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// block will branch to the old successors of the original block: these 41a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// successors will have to have any PHI nodes updated to account for the new 42a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// incoming edges. 43a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// 44a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// The correlation between instructions in the source and result basic blocks 45a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// is recorded in the ValueMap map. 46a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// 47a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// If you have a particular suffix you'd like to use to add to any cloned 48a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// names, specify it as the optional second parameter. 49a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner/// 50a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris LattnerBasicBlock *CloneBasicBlock(const BasicBlock *BB, 51a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner std::map<const Value*, Value*> &ValueMap, 52a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner const char *NameSuffix = ""); 53a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner 54a107e5f11cf7aa362d2fdd5cebc7e64bb09ed22dChris Lattner 55d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// CloneFunction - Return a copy of the specified function, but without 56d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// embedding the function into another module. Also, any references specified 57d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// in the ValueMap are changed to refer to their mapped value instead of the 58d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// original one. If any of the arguments to the function are in the ValueMap, 59d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// the arguments are deleted from the resultant function. The ValueMap is 60d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// updated to include mappings from all of the instructions and basicblocks in 61d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// the function from their old to new values. 62d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// 63d18015599cbe09dd327b5f73501581a865bf27daChris LattnerFunction *CloneFunction(const Function *F, 64d18015599cbe09dd327b5f73501581a865bf27daChris Lattner std::map<const Value*, Value*> &ValueMap); 65d18015599cbe09dd327b5f73501581a865bf27daChris Lattner 66d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// CloneFunction - Version of the function that doesn't need the ValueMap. 67d18015599cbe09dd327b5f73501581a865bf27daChris Lattner/// 68d18015599cbe09dd327b5f73501581a865bf27daChris Lattnerinline Function *CloneFunction(const Function *F) { 69d18015599cbe09dd327b5f73501581a865bf27daChris Lattner std::map<const Value*, Value*> ValueMap; 70d18015599cbe09dd327b5f73501581a865bf27daChris Lattner return CloneFunction(F, ValueMap); 71d18015599cbe09dd327b5f73501581a865bf27daChris Lattner} 72d18015599cbe09dd327b5f73501581a865bf27daChris Lattner 73aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// Clone OldFunc into NewFunc, transforming the old arguments into references 74aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// to ArgMap values. Note that if NewFunc already has basic blocks, the ones 75aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// cloned into it will be added to the end of the function. This function 76aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// fills in a list of return instructions, and can optionally append the 77aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// specified suffix to all values cloned. 78aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// 7984bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnervoid CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 80d18015599cbe09dd327b5f73501581a865bf27daChris Lattner std::map<const Value*, Value*> &ValueMap, 811c9985067bfedc219c1a6128bfc703cf3894b866Chris Lattner std::vector<ReturnInst*> &Returns, 821c9985067bfedc219c1a6128bfc703cf3894b866Chris Lattner const char *NameSuffix = ""); 8384bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner 8484bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner 85aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// InlineFunction - This function inlines the called function into the basic 86a04e51f4c0dd981af9b8f6e8b219df495cccaa45Chris Lattner/// block of the caller. This returns false if it is not possible to inline 87a04e51f4c0dd981af9b8f6e8b219df495cccaa45Chris Lattner/// this call. The program is still in a well defined state if this occurs 88a04e51f4c0dd981af9b8f6e8b219df495cccaa45Chris Lattner/// though. 89aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// 90aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// Note that this only does one level of inlining. For example, if the 91aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 92aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// exists in the instruction stream. Similiarly this will inline a recursive 93aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// function by one level. 94aa101c3147debcba3f0441c80b477782e456a03bChris Lattner/// 9584bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattnerbool InlineFunction(CallInst *C); 969d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerbool InlineFunction(InvokeInst *II); 979d3a1b2d972baf72b7da371a1ae72126bc5c6c04Chris Lattnerbool InlineFunction(CallSite CS); 982c49fc023ee0885c8c577829cd40c00ef48581fcTanya Lattner 99658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// CloneTrace - Returns a copy of the specified trace. 100658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// It takes a vector of basic blocks clones the basic blocks, removes internal 101658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// phi nodes, adds it to the same function as the original (although there is 102658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattner/// no jump to it) and returns the new vector of basic blocks. 103658c5bcdb27fe7e7936b91fc9ccc80738e3bfd22Tanya Lattnerstd::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace); 1042c49fc023ee0885c8c577829cd40c00ef48581fcTanya Lattner 10584bf9880eaf57f00cc01b4ade40179553ea6fd08Chris Lattner#endif 106