Chris Lattner | 6c2e2e5 | 2002-11-19 22:04:49 +0000 | [diff] [blame] | 1 | //===- CloneFunction.cpp - Clone a function into another function ---------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2e2e5 | 2002-11-19 22:04:49 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the CloneFunctionInto interface, which is used as the |
| 11 | // low-level function cloner. This is used by the CloneFunction and function |
| 12 | // inliner to do the dirty work of copying the body of a function around. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 309f193 | 2002-11-19 20:59:41 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Devang Patel | f66d7b5 | 2009-02-10 07:48:18 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Nate Begeman | 4be30ac | 2008-04-25 06:37:06 +0000 | [diff] [blame] | 21 | #include "llvm/GlobalVariable.h" |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 23 | #include "llvm/LLVMContext.h" |
Chris Lattner | f0908a3 | 2009-12-31 03:02:08 +0000 | [diff] [blame] | 24 | #include "llvm/Metadata.h" |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Dan Gohman | 05ea54e | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ConstantFolding.h" |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/DebugInfo.h" |
Chris Lattner | 9fa038d | 2007-01-30 23:13:49 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 30 | #include <map> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 33 | // CloneBasicBlock - See comments in Cloning.h |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 34 | BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 35 | ValueToValueMapTy &VMap, |
Benjamin Kramer | 5deb57c | 2010-01-27 19:58:47 +0000 | [diff] [blame] | 36 | const Twine &NameSuffix, Function *F, |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 37 | ClonedCodeInfo *CodeInfo) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 38 | BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F); |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 39 | if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); |
| 40 | |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 41 | bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; |
| 42 | |
| 43 | // Loop over all instructions, and copy them over. |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 44 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); |
| 45 | II != IE; ++II) { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 46 | Instruction *NewInst = II->clone(); |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 47 | if (II->hasName()) |
| 48 | NewInst->setName(II->getName()+NameSuffix); |
| 49 | NewBB->getInstList().push_back(NewInst); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 50 | VMap[II] = NewInst; // Add instruction map to value. |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 51 | |
Dale Johannesen | 8aa90fe | 2009-03-10 22:20:02 +0000 | [diff] [blame] | 52 | hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 53 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
| 54 | if (isa<ConstantInt>(AI->getArraySize())) |
| 55 | hasStaticAllocas = true; |
| 56 | else |
| 57 | hasDynamicAllocas = true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (CodeInfo) { |
| 62 | CodeInfo->ContainsCalls |= hasCalls; |
| 63 | CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator()); |
| 64 | CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; |
| 65 | CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 66 | BB != &BB->getParent()->getEntryBlock(); |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 67 | } |
| 68 | return NewBB; |
| 69 | } |
| 70 | |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 71 | // Clone OldFunc into NewFunc, transforming the old arguments into references to |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 72 | // VMap values. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 73 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 74 | void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 75 | ValueToValueMapTy &VMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 76 | bool ModuleLevelChanges, |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 77 | SmallVectorImpl<ReturnInst*> &Returns, |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 78 | const char *NameSuffix, ClonedCodeInfo *CodeInfo) { |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 79 | assert(NameSuffix && "NameSuffix cannot be null!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 80 | |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 81 | #ifndef NDEBUG |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 82 | for (Function::const_arg_iterator I = OldFunc->arg_begin(), |
| 83 | E = OldFunc->arg_end(); I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 84 | assert(VMap.count(I) && "No mapping from source argument specified!"); |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 85 | #endif |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 86 | |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 87 | // Clone any attributes. |
Andrew Lenharth | 82cf32e | 2008-10-07 18:08:38 +0000 | [diff] [blame] | 88 | if (NewFunc->arg_size() == OldFunc->arg_size()) |
| 89 | NewFunc->copyAttributesFrom(OldFunc); |
| 90 | else { |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 91 | //Some arguments were deleted with the VMap. Copy arguments one by one |
Andrew Lenharth | 82cf32e | 2008-10-07 18:08:38 +0000 | [diff] [blame] | 92 | for (Function::const_arg_iterator I = OldFunc->arg_begin(), |
| 93 | E = OldFunc->arg_end(); I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 94 | if (Argument* Anew = dyn_cast<Argument>(VMap[I])) |
Andrew Lenharth | 82cf32e | 2008-10-07 18:08:38 +0000 | [diff] [blame] | 95 | Anew->addAttr( OldFunc->getAttributes() |
| 96 | .getParamAttributes(I->getArgNo() + 1)); |
| 97 | NewFunc->setAttributes(NewFunc->getAttributes() |
| 98 | .addAttr(0, OldFunc->getAttributes() |
| 99 | .getRetAttributes())); |
| 100 | NewFunc->setAttributes(NewFunc->getAttributes() |
| 101 | .addAttr(~0, OldFunc->getAttributes() |
| 102 | .getFnAttributes())); |
| 103 | |
| 104 | } |
Anton Korobeynikov | 9e49f1b | 2008-03-23 16:03:00 +0000 | [diff] [blame] | 105 | |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 106 | // Loop over all of the basic blocks in the function, cloning them as |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 107 | // appropriate. Note that we save BE this way in order to handle cloning of |
| 108 | // recursive functions into themselves. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 109 | // |
| 110 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 111 | BI != BE; ++BI) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 112 | const BasicBlock &BB = *BI; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 114 | // Create a new basic block and copy instructions into it! |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 115 | BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 116 | VMap[&BB] = CBB; // Add basic block mapping. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 117 | |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 118 | if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) |
| 119 | Returns.push_back(RI); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 122 | // Loop over all of the instructions in the function, fixing up operand |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 123 | // references as we go. This uses VMap to do all the hard work. |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 124 | for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]), |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 125 | BE = NewFunc->end(); BB != BE; ++BB) |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 126 | // Loop over all instructions, fixing each one as we find it... |
Chris Lattner | a33ceaa | 2004-02-04 21:44:26 +0000 | [diff] [blame] | 127 | for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 128 | RemapInstruction(II, VMap, |
| 129 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 131 | |
| 132 | /// CloneFunction - Return a copy of the specified function, but without |
| 133 | /// embedding the function into another module. Also, any references specified |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 134 | /// in the VMap are changed to refer to their mapped value instead of the |
| 135 | /// original one. If any of the arguments to the function are in the VMap, |
| 136 | /// the arguments are deleted from the resultant function. The VMap is |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 137 | /// updated to include mappings from all of the instructions and basicblocks in |
| 138 | /// the function from their old to new values. |
| 139 | /// |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 140 | Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 141 | bool ModuleLevelChanges, |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 142 | ClonedCodeInfo *CodeInfo) { |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 143 | std::vector<const Type*> ArgTypes; |
| 144 | |
| 145 | // The user might be deleting arguments to the function by specifying them in |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 146 | // the VMap. If so, we need to not add the arguments to the arg ty vector |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 147 | // |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 148 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 149 | I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 150 | if (VMap.count(I) == 0) // Haven't mapped the argument to anything yet? |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 151 | ArgTypes.push_back(I->getType()); |
| 152 | |
| 153 | // Create a new function type... |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 154 | FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 155 | ArgTypes, F->getFunctionType()->isVarArg()); |
| 156 | |
| 157 | // Create the new function... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 158 | Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 160 | // Loop over the arguments, copying the names of the mapped arguments over... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 161 | Function::arg_iterator DestI = NewF->arg_begin(); |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 162 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 163 | I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 164 | if (VMap.count(I) == 0) { // Is this argument preserved? |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 165 | DestI->setName(I->getName()); // Copy the name over... |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 166 | VMap[I] = DestI++; // Add mapping to VMap |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 169 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 170 | CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 171 | return NewF; |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 172 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | namespace { |
| 177 | /// PruningFunctionCloner - This class is a private class used to implement |
| 178 | /// the CloneAndPruneFunctionInto method. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 179 | struct PruningFunctionCloner { |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 180 | Function *NewFunc; |
| 181 | const Function *OldFunc; |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 182 | ValueToValueMapTy &VMap; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 183 | bool ModuleLevelChanges; |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 184 | SmallVectorImpl<ReturnInst*> &Returns; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 185 | const char *NameSuffix; |
| 186 | ClonedCodeInfo *CodeInfo; |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 187 | const TargetData *TD; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 188 | public: |
| 189 | PruningFunctionCloner(Function *newFunc, const Function *oldFunc, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 190 | ValueToValueMapTy &valueMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 191 | bool moduleLevelChanges, |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 192 | SmallVectorImpl<ReturnInst*> &returns, |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 193 | const char *nameSuffix, |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 194 | ClonedCodeInfo *codeInfo, |
| 195 | const TargetData *td) |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 196 | : NewFunc(newFunc), OldFunc(oldFunc), |
| 197 | VMap(valueMap), ModuleLevelChanges(moduleLevelChanges), |
| 198 | Returns(returns), NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) { |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /// CloneBlock - The specified block is found to be reachable, clone it and |
| 202 | /// anything that it can reach. |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 203 | void CloneBlock(const BasicBlock *BB, |
| 204 | std::vector<const BasicBlock*> &ToClone); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 205 | |
| 206 | public: |
| 207 | /// ConstantFoldMappedInstruction - Constant fold the specified instruction, |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 208 | /// mapping its operands through VMap if they are available. |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 209 | Constant *ConstantFoldMappedInstruction(const Instruction *I); |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | /// CloneBlock - The specified block is found to be reachable, clone it and |
| 214 | /// anything that it can reach. |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 215 | void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, |
| 216 | std::vector<const BasicBlock*> &ToClone){ |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 217 | TrackingVH<Value> &BBEntry = VMap[BB]; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 218 | |
| 219 | // Have we already cloned this block? |
| 220 | if (BBEntry) return; |
| 221 | |
| 222 | // Nope, clone it now. |
| 223 | BasicBlock *NewBB; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 224 | BBEntry = NewBB = BasicBlock::Create(BB->getContext()); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 225 | if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); |
| 226 | |
| 227 | bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; |
| 228 | |
| 229 | // Loop over all instructions, and copy them over, DCE'ing as we go. This |
| 230 | // loop doesn't include the terminator. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 231 | for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end(); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 232 | II != IE; ++II) { |
| 233 | // If this instruction constant folds, don't bother cloning the instruction, |
| 234 | // instead, just add the constant to the value map. |
| 235 | if (Constant *C = ConstantFoldMappedInstruction(II)) { |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 236 | VMap[II] = C; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 237 | continue; |
| 238 | } |
Devang Patel | f66d7b5 | 2009-02-10 07:48:18 +0000 | [diff] [blame] | 239 | |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 240 | Instruction *NewInst = II->clone(); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 241 | if (II->hasName()) |
| 242 | NewInst->setName(II->getName()+NameSuffix); |
| 243 | NewBB->getInstList().push_back(NewInst); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 244 | VMap[II] = NewInst; // Add instruction map to value. |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 245 | |
Dale Johannesen | 8aa90fe | 2009-03-10 22:20:02 +0000 | [diff] [blame] | 246 | hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 247 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
| 248 | if (isa<ConstantInt>(AI->getArraySize())) |
| 249 | hasStaticAllocas = true; |
| 250 | else |
| 251 | hasDynamicAllocas = true; |
| 252 | } |
| 253 | } |
| 254 | |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 255 | // Finally, clone over the terminator. |
| 256 | const TerminatorInst *OldTI = BB->getTerminator(); |
| 257 | bool TerminatorDone = false; |
| 258 | if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { |
| 259 | if (BI->isConditional()) { |
| 260 | // If the condition was a known constant in the callee... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 261 | ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); |
| 262 | // Or is a known constant in the caller... |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 263 | if (Cond == 0) { |
| 264 | Value *V = VMap[BI->getCondition()]; |
| 265 | Cond = dyn_cast_or_null<ConstantInt>(V); |
| 266 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 267 | |
| 268 | // Constant fold to uncond branch! |
| 269 | if (Cond) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 270 | BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 271 | VMap[OldTI] = BranchInst::Create(Dest, NewBB); |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 272 | ToClone.push_back(Dest); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 273 | TerminatorDone = true; |
| 274 | } |
| 275 | } |
| 276 | } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { |
| 277 | // If switching on a value known constant in the caller. |
| 278 | ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 279 | if (Cond == 0) { // Or known constant after constant prop in the callee... |
| 280 | Value *V = VMap[SI->getCondition()]; |
| 281 | Cond = dyn_cast_or_null<ConstantInt>(V); |
| 282 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 283 | if (Cond) { // Constant fold to uncond branch! |
| 284 | BasicBlock *Dest = SI->getSuccessor(SI->findCaseValue(Cond)); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 285 | VMap[OldTI] = BranchInst::Create(Dest, NewBB); |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 286 | ToClone.push_back(Dest); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 287 | TerminatorDone = true; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (!TerminatorDone) { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 292 | Instruction *NewInst = OldTI->clone(); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 293 | if (OldTI->hasName()) |
| 294 | NewInst->setName(OldTI->getName()+NameSuffix); |
| 295 | NewBB->getInstList().push_back(NewInst); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 296 | VMap[OldTI] = NewInst; // Add instruction map to value. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 297 | |
| 298 | // Recursively clone any reachable successor blocks. |
| 299 | const TerminatorInst *TI = BB->getTerminator(); |
| 300 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 301 | ToClone.push_back(TI->getSuccessor(i)); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 304 | if (CodeInfo) { |
| 305 | CodeInfo->ContainsCalls |= hasCalls; |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 306 | CodeInfo->ContainsUnwinds |= isa<UnwindInst>(OldTI); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 307 | CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; |
| 308 | CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && |
| 309 | BB != &BB->getParent()->front(); |
| 310 | } |
| 311 | |
| 312 | if (ReturnInst *RI = dyn_cast<ReturnInst>(NewBB->getTerminator())) |
| 313 | Returns.push_back(RI); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /// ConstantFoldMappedInstruction - Constant fold the specified instruction, |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 317 | /// mapping its operands through VMap if they are available. |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 318 | Constant *PruningFunctionCloner:: |
| 319 | ConstantFoldMappedInstruction(const Instruction *I) { |
Chris Lattner | 9fa038d | 2007-01-30 23:13:49 +0000 | [diff] [blame] | 320 | SmallVector<Constant*, 8> Ops; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 321 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 322 | if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i), |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 323 | VMap, |
| 324 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges))) |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 325 | Ops.push_back(Op); |
| 326 | else |
| 327 | return 0; // All operands not constant! |
| 328 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 329 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 330 | return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], |
| 331 | TD); |
Nate Begeman | 4be30ac | 2008-04-25 06:37:06 +0000 | [diff] [blame] | 332 | |
Nate Begeman | 4182db4 | 2008-04-25 17:45:52 +0000 | [diff] [blame] | 333 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 334 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) |
Nate Begeman | 4be30ac | 2008-04-25 06:37:06 +0000 | [diff] [blame] | 335 | if (!LI->isVolatile() && CE->getOpcode() == Instruction::GetElementPtr) |
| 336 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Duncan Sands | 64da940 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 337 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
Nate Begeman | 4be30ac | 2008-04-25 06:37:06 +0000 | [diff] [blame] | 338 | return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 339 | CE); |
Nate Begeman | 4be30ac | 2008-04-25 06:37:06 +0000 | [diff] [blame] | 340 | |
| 341 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0], |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 342 | Ops.size(), TD); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 345 | static DebugLoc |
| 346 | UpdateInlinedAtInfo(const DebugLoc &InsnDL, const DebugLoc &TheCallDL, |
| 347 | LLVMContext &Ctx) { |
| 348 | DebugLoc NewLoc = TheCallDL; |
| 349 | if (MDNode *IA = InsnDL.getInlinedAt(Ctx)) |
| 350 | NewLoc = UpdateInlinedAtInfo(DebugLoc::getFromDILocation(IA), TheCallDL, |
| 351 | Ctx); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 352 | |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 353 | return DebugLoc::get(InsnDL.getLine(), InsnDL.getCol(), |
| 354 | InsnDL.getScope(Ctx), NewLoc.getAsMDNode(Ctx)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 357 | /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, |
| 358 | /// except that it does some simple constant prop and DCE on the fly. The |
| 359 | /// effect of this is to copy significantly less code in cases where (for |
| 360 | /// example) a function call with constant arguments is inlined, and those |
| 361 | /// constant arguments cause a significant amount of code in the callee to be |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 362 | /// dead. Since this doesn't produce an exact copy of the input, it can't be |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 363 | /// used for things like CloneFunction or CloneModule. |
| 364 | void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 365 | ValueToValueMapTy &VMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 366 | bool ModuleLevelChanges, |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 367 | SmallVectorImpl<ReturnInst*> &Returns, |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 368 | const char *NameSuffix, |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 369 | ClonedCodeInfo *CodeInfo, |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 370 | const TargetData *TD, |
| 371 | Instruction *TheCall) { |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 372 | assert(NameSuffix && "NameSuffix cannot be null!"); |
| 373 | |
| 374 | #ifndef NDEBUG |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 375 | for (Function::const_arg_iterator II = OldFunc->arg_begin(), |
| 376 | E = OldFunc->arg_end(); II != E; ++II) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 377 | assert(VMap.count(II) && "No mapping from source argument specified!"); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 378 | #endif |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 379 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 380 | PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, |
| 381 | Returns, NameSuffix, CodeInfo, TD); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 382 | |
| 383 | // Clone the entry block, and anything recursively reachable from it. |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 384 | std::vector<const BasicBlock*> CloneWorklist; |
| 385 | CloneWorklist.push_back(&OldFunc->getEntryBlock()); |
| 386 | while (!CloneWorklist.empty()) { |
| 387 | const BasicBlock *BB = CloneWorklist.back(); |
| 388 | CloneWorklist.pop_back(); |
| 389 | PFC.CloneBlock(BB, CloneWorklist); |
| 390 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 391 | |
| 392 | // Loop over all of the basic blocks in the old function. If the block was |
| 393 | // reachable, we have cloned it and the old block is now in the value map: |
| 394 | // insert it into the new function in the right order. If not, ignore it. |
| 395 | // |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 396 | // Defer PHI resolution until rest of function is resolved. |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 397 | SmallVector<const PHINode*, 16> PHIToResolve; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 398 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 399 | BI != BE; ++BI) { |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 400 | Value *V = VMap[BI]; |
| 401 | BasicBlock *NewBB = cast_or_null<BasicBlock>(V); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 402 | if (NewBB == 0) continue; // Dead block. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 404 | // Add the new block to the new function. |
| 405 | NewFunc->getBasicBlockList().push_back(NewBB); |
| 406 | |
| 407 | // Loop over all of the instructions in the block, fixing up operand |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 408 | // references as we go. This uses VMap to do all the hard work. |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 409 | // |
| 410 | BasicBlock::iterator I = NewBB->begin(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 411 | |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 412 | DebugLoc TheCallDL; |
| 413 | if (TheCall) |
| 414 | TheCallDL = TheCall->getDebugLoc(); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 415 | |
| 416 | // Handle PHI nodes specially, as we have to remove references to dead |
| 417 | // blocks. |
| 418 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 419 | // Skip over all PHI nodes, remembering them for later. |
| 420 | BasicBlock::const_iterator OldI = BI->begin(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 421 | for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI) { |
Daniel Dunbar | dfa9261 | 2009-11-12 02:52:56 +0000 | [diff] [blame] | 422 | if (I->hasMetadata()) { |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 423 | if (!TheCallDL.isUnknown()) { |
| 424 | DebugLoc IDL = I->getDebugLoc(); |
| 425 | if (!IDL.isUnknown()) { |
| 426 | DebugLoc NewDL = UpdateInlinedAtInfo(IDL, TheCallDL, |
| 427 | I->getContext()); |
| 428 | I->setDebugLoc(NewDL); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 429 | } |
Daniel Dunbar | dfa9261 | 2009-11-12 02:52:56 +0000 | [diff] [blame] | 430 | } else { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 431 | // The cloned instruction has dbg info but the call instruction |
| 432 | // does not have dbg info. Remove dbg info from cloned instruction. |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 433 | I->setDebugLoc(DebugLoc()); |
Daniel Dunbar | dfa9261 | 2009-11-12 02:52:56 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 436 | PHIToResolve.push_back(cast<PHINode>(OldI)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 437 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 440 | // FIXME: |
| 441 | // FIXME: |
| 442 | // FIXME: Unclone all this metadata stuff. |
| 443 | // FIXME: |
| 444 | // FIXME: |
| 445 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 446 | // Otherwise, remap the rest of the instructions normally. |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 447 | for (; I != NewBB->end(); ++I) { |
Daniel Dunbar | dfa9261 | 2009-11-12 02:52:56 +0000 | [diff] [blame] | 448 | if (I->hasMetadata()) { |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 449 | if (!TheCallDL.isUnknown()) { |
| 450 | DebugLoc IDL = I->getDebugLoc(); |
| 451 | if (!IDL.isUnknown()) { |
| 452 | DebugLoc NewDL = UpdateInlinedAtInfo(IDL, TheCallDL, |
| 453 | I->getContext()); |
| 454 | I->setDebugLoc(NewDL); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 455 | } |
Daniel Dunbar | dfa9261 | 2009-11-12 02:52:56 +0000 | [diff] [blame] | 456 | } else { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 457 | // The cloned instruction has dbg info but the call instruction |
| 458 | // does not have dbg info. Remove dbg info from cloned instruction. |
Dan Gohman | 333a947 | 2010-07-20 23:49:05 +0000 | [diff] [blame] | 459 | I->setDebugLoc(DebugLoc()); |
Daniel Dunbar | dfa9261 | 2009-11-12 02:52:56 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 462 | RemapInstruction(I, VMap, |
| 463 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 464 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 465 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 466 | |
| 467 | // Defer PHI resolution until rest of function is resolved, PHI resolution |
| 468 | // requires the CFG to be up-to-date. |
| 469 | for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { |
| 470 | const PHINode *OPN = PHIToResolve[phino]; |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 471 | unsigned NumPreds = OPN->getNumIncomingValues(); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 472 | const BasicBlock *OldBB = OPN->getParent(); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 473 | BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 474 | |
| 475 | // Map operands for blocks that are live and remove operands for blocks |
| 476 | // that are dead. |
| 477 | for (; phino != PHIToResolve.size() && |
| 478 | PHIToResolve[phino]->getParent() == OldBB; ++phino) { |
| 479 | OPN = PHIToResolve[phino]; |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 480 | PHINode *PN = cast<PHINode>(VMap[OPN]); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 481 | for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 482 | Value *V = VMap[PN->getIncomingBlock(pred)]; |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 483 | if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 484 | Value *InVal = MapValue(PN->getIncomingValue(pred), |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 485 | VMap, |
| 486 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 487 | assert(InVal && "Unknown input value?"); |
| 488 | PN->setIncomingValue(pred, InVal); |
| 489 | PN->setIncomingBlock(pred, MappedBlock); |
| 490 | } else { |
| 491 | PN->removeIncomingValue(pred, false); |
| 492 | --pred, --e; // Revisit the next entry. |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // The loop above has removed PHI entries for those blocks that are dead |
| 498 | // and has updated others. However, if a block is live (i.e. copied over) |
| 499 | // but its terminator has been changed to not go to this block, then our |
| 500 | // phi nodes will have invalid entries. Update the PHI nodes in this |
| 501 | // case. |
| 502 | PHINode *PN = cast<PHINode>(NewBB->begin()); |
| 503 | NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); |
| 504 | if (NumPreds != PN->getNumIncomingValues()) { |
| 505 | assert(NumPreds < PN->getNumIncomingValues()); |
| 506 | // Count how many times each predecessor comes to this block. |
| 507 | std::map<BasicBlock*, unsigned> PredCount; |
| 508 | for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); |
| 509 | PI != E; ++PI) |
| 510 | --PredCount[*PI]; |
| 511 | |
| 512 | // Figure out how many entries to remove from each PHI. |
| 513 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 514 | ++PredCount[PN->getIncomingBlock(i)]; |
| 515 | |
| 516 | // At this point, the excess predecessor entries are positive in the |
| 517 | // map. Loop over all of the PHIs and remove excess predecessor |
| 518 | // entries. |
| 519 | BasicBlock::iterator I = NewBB->begin(); |
| 520 | for (; (PN = dyn_cast<PHINode>(I)); ++I) { |
| 521 | for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), |
| 522 | E = PredCount.end(); PCI != E; ++PCI) { |
| 523 | BasicBlock *Pred = PCI->first; |
| 524 | for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) |
| 525 | PN->removeIncomingValue(Pred, false); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | // If the loops above have made these phi nodes have 0 or 1 operand, |
| 531 | // replace them with undef or the input value. We must do this for |
| 532 | // correctness, because 0-operand phis are not valid. |
| 533 | PN = cast<PHINode>(NewBB->begin()); |
| 534 | if (PN->getNumIncomingValues() == 0) { |
| 535 | BasicBlock::iterator I = NewBB->begin(); |
| 536 | BasicBlock::const_iterator OldI = OldBB->begin(); |
| 537 | while ((PN = dyn_cast<PHINode>(I++))) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 538 | Value *NV = UndefValue::get(PN->getType()); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 539 | PN->replaceAllUsesWith(NV); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 540 | assert(VMap[OldI] == PN && "VMap mismatch"); |
| 541 | VMap[OldI] = NV; |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 542 | PN->eraseFromParent(); |
| 543 | ++OldI; |
| 544 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 545 | } |
Chris Lattner | 8e8eda7 | 2007-02-01 18:48:38 +0000 | [diff] [blame] | 546 | // NOTE: We cannot eliminate single entry phi nodes here, because of |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 547 | // VMap. Single entry phi nodes can have multiple VMap entries |
| 548 | // pointing at them. Thus, deleting one would require scanning the VMap |
Chris Lattner | 8e8eda7 | 2007-02-01 18:48:38 +0000 | [diff] [blame] | 549 | // to update any entries in it that would require that. This would be |
| 550 | // really slow. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 551 | } |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 552 | |
| 553 | // Now that the inlined function body has been fully constructed, go through |
| 554 | // and zap unconditional fall-through branches. This happen all the time when |
| 555 | // specializing code: code specialization turns conditional branches into |
| 556 | // uncond branches, and this code folds them. |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 557 | Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]); |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 558 | while (I != NewFunc->end()) { |
| 559 | BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); |
| 560 | if (!BI || BI->isConditional()) { ++I; continue; } |
| 561 | |
Chris Lattner | 8e8eda7 | 2007-02-01 18:48:38 +0000 | [diff] [blame] | 562 | // Note that we can't eliminate uncond branches if the destination has |
| 563 | // single-entry PHI nodes. Eliminating the single-entry phi nodes would |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 564 | // require scanning the VMap to update any entries that point to the phi |
Chris Lattner | 8e8eda7 | 2007-02-01 18:48:38 +0000 | [diff] [blame] | 565 | // node. |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 566 | BasicBlock *Dest = BI->getSuccessor(0); |
Chris Lattner | 8e8eda7 | 2007-02-01 18:48:38 +0000 | [diff] [blame] | 567 | if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) { |
| 568 | ++I; continue; |
| 569 | } |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 570 | |
| 571 | // We know all single-entry PHI nodes in the inlined function have been |
| 572 | // removed, so we just need to splice the blocks. |
| 573 | BI->eraseFromParent(); |
| 574 | |
Eric Christopher | e59fbc0 | 2011-06-23 06:24:52 +0000 | [diff] [blame] | 575 | // Make all PHI nodes that referred to Dest now refer to I as their source. |
| 576 | Dest->replaceAllUsesWith(I); |
| 577 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame^] | 578 | // Move all the instructions in the succ to the pred. |
| 579 | I->getInstList().splice(I->end(), Dest->getInstList()); |
| 580 | |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 581 | // Remove the dest block. |
| 582 | Dest->eraseFromParent(); |
| 583 | |
| 584 | // Do not increment I, iteratively merge all things this block branches to. |
| 585 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 586 | } |