Cloning.h revision a04e51f4c0dd981af9b8f6e8b219df495cccaa45
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> 16class Module; 17class Function; 18class BasicBlock; 19class Value; 20class CallInst; 21class InvokeInst; 22class ReturnInst; 23class CallSite; 24 25/// CloneModule - Return an exact copy of the specified module 26/// 27Module *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/// 50BasicBlock *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/// 63Function *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/// 68inline 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/// 79void 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 false if it is not possible to inline 87/// this call. The program is still in a well defined state if this occurs 88/// though. 89/// 90/// Note that this only does one level of inlining. For example, if the 91/// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 92/// exists in the instruction stream. Similiarly this will inline a recursive 93/// function by one level. 94/// 95bool InlineFunction(CallInst *C); 96bool InlineFunction(InvokeInst *II); 97bool InlineFunction(CallSite CS); 98 99/// CloneTrace - Returns a copy of the specified trace. 100/// It takes a vector of basic blocks clones the basic blocks, removes internal 101/// phi nodes, adds it to the same function as the original (although there is 102/// no jump to it) and returns the new vector of basic blocks. 103std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace); 104 105#endif 106