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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/Analysis/ConstantFolding.h" |
| 19 | #include "llvm/Analysis/InstructionSimplify.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 20 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 22 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/Function.h" |
| 25 | #include "llvm/IR/GlobalVariable.h" |
| 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/IntrinsicInst.h" |
| 28 | #include "llvm/IR/LLVMContext.h" |
| 29 | #include "llvm/IR/Metadata.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 30 | #include "llvm/IR/Module.h" |
Chandler Carruth | afff330 | 2012-03-28 08:38:27 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 32 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 05ea54e | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 34 | #include <map> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 37 | // CloneBasicBlock - See comments in Cloning.h |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 38 | BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 39 | ValueToValueMapTy &VMap, |
Benjamin Kramer | 5deb57c | 2010-01-27 19:58:47 +0000 | [diff] [blame] | 40 | const Twine &NameSuffix, Function *F, |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 41 | ClonedCodeInfo *CodeInfo) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 42 | BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F); |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 43 | if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); |
| 44 | |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 45 | bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; |
| 46 | |
| 47 | // Loop over all instructions, and copy them over. |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 48 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); |
| 49 | II != IE; ++II) { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 50 | Instruction *NewInst = II->clone(); |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 51 | if (II->hasName()) |
| 52 | NewInst->setName(II->getName()+NameSuffix); |
| 53 | NewBB->getInstList().push_back(NewInst); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 54 | VMap[II] = NewInst; // Add instruction map to value. |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 55 | |
Dale Johannesen | 8aa90fe | 2009-03-10 22:20:02 +0000 | [diff] [blame] | 56 | hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 57 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
| 58 | if (isa<ConstantInt>(AI->getArraySize())) |
| 59 | hasStaticAllocas = true; |
| 60 | else |
| 61 | hasDynamicAllocas = true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (CodeInfo) { |
| 66 | CodeInfo->ContainsCalls |= hasCalls; |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 67 | CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; |
| 68 | CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 69 | BB != &BB->getParent()->getEntryBlock(); |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 70 | } |
| 71 | return NewBB; |
| 72 | } |
| 73 | |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 74 | // Clone OldFunc into NewFunc, transforming the old arguments into references to |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 75 | // VMap values. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 76 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 77 | void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 78 | ValueToValueMapTy &VMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 79 | bool ModuleLevelChanges, |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 80 | SmallVectorImpl<ReturnInst*> &Returns, |
Mon P Wang | d24397a | 2011-12-23 02:18:32 +0000 | [diff] [blame] | 81 | const char *NameSuffix, ClonedCodeInfo *CodeInfo, |
James Molloy | a84a83b | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 82 | ValueMapTypeRemapper *TypeMapper, |
| 83 | ValueMaterializer *Materializer) { |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 84 | assert(NameSuffix && "NameSuffix cannot be null!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 85 | |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 86 | #ifndef NDEBUG |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 87 | for (Function::const_arg_iterator I = OldFunc->arg_begin(), |
| 88 | E = OldFunc->arg_end(); I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 89 | assert(VMap.count(I) && "No mapping from source argument specified!"); |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 90 | #endif |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 91 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 92 | // Copy all attributes other than those stored in the AttributeSet. We need |
| 93 | // to remap the parameter indices of the AttributeSet. |
| 94 | AttributeSet NewAttrs = NewFunc->getAttributes(); |
| 95 | NewFunc->copyAttributesFrom(OldFunc); |
| 96 | NewFunc->setAttributes(NewAttrs); |
| 97 | |
Joey Gouly | 0f57a98 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 98 | AttributeSet OldAttrs = OldFunc->getAttributes(); |
| 99 | // Clone any argument attributes that are present in the VMap. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 100 | for (const Argument &OldArg : OldFunc->args()) |
| 101 | if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) { |
Joey Gouly | 0f57a98 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 102 | AttributeSet attrs = |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 103 | OldAttrs.getParamAttributes(OldArg.getArgNo() + 1); |
Joey Gouly | 0f57a98 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 104 | if (attrs.getNumSlots() > 0) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 105 | NewArg->addAttr(attrs); |
Joey Gouly | 0f57a98 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 106 | } |
Andrew Lenharth | 82cf32e | 2008-10-07 18:08:38 +0000 | [diff] [blame] | 107 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 108 | NewFunc->setAttributes( |
| 109 | NewFunc->getAttributes() |
| 110 | .addAttributes(NewFunc->getContext(), AttributeSet::ReturnIndex, |
| 111 | OldAttrs.getRetAttributes()) |
| 112 | .addAttributes(NewFunc->getContext(), AttributeSet::FunctionIndex, |
| 113 | OldAttrs.getFnAttributes())); |
Anton Korobeynikov | 9e49f1b | 2008-03-23 16:03:00 +0000 | [diff] [blame] | 114 | |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 115 | // 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] | 116 | // appropriate. Note that we save BE this way in order to handle cloning of |
| 117 | // recursive functions into themselves. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 118 | // |
| 119 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 120 | BI != BE; ++BI) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 121 | const BasicBlock &BB = *BI; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 17d145d | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 123 | // Create a new basic block and copy instructions into it! |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 124 | BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 125 | |
Eli Friedman | 4090e1c | 2011-10-21 20:45:19 +0000 | [diff] [blame] | 126 | // Add basic block mapping. |
| 127 | VMap[&BB] = CBB; |
| 128 | |
| 129 | // It is only legal to clone a function if a block address within that |
| 130 | // function is never referenced outside of the function. Given that, we |
| 131 | // want to map block addresses from the old function to block addresses in |
| 132 | // the clone. (This is different from the generic ValueMapper |
| 133 | // implementation, which generates an invalid blockaddress when |
| 134 | // cloning a function.) |
| 135 | if (BB.hasAddressTaken()) { |
| 136 | Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), |
| 137 | const_cast<BasicBlock*>(&BB)); |
| 138 | VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); |
| 139 | } |
| 140 | |
| 141 | // Note return instructions for the caller. |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 142 | if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) |
| 143 | Returns.push_back(RI); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 146 | // Loop over all of the instructions in the function, fixing up operand |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 147 | // 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] | 148 | for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]), |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 149 | BE = NewFunc->end(); BB != BE; ++BB) |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 150 | // Loop over all instructions, fixing each one as we find it... |
Chris Lattner | a33ceaa | 2004-02-04 21:44:26 +0000 | [diff] [blame] | 151 | for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 152 | RemapInstruction(II, VMap, |
Mon P Wang | d24397a | 2011-12-23 02:18:32 +0000 | [diff] [blame] | 153 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, |
James Molloy | a84a83b | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 154 | TypeMapper, Materializer); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 155 | } |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 156 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 157 | // Find the MDNode which corresponds to the DISubprogram data that described F. |
| 158 | static MDNode* FindSubprogram(const Function *F, DebugInfoFinder &Finder) { |
| 159 | for (DISubprogram Subprogram : Finder.subprograms()) { |
| 160 | if (Subprogram.describes(F)) return Subprogram; |
| 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | // Add an operand to an existing MDNode. The new operand will be added at the |
| 166 | // back of the operand list. |
| 167 | static void AddOperand(MDNode *Node, Value *Operand) { |
| 168 | SmallVector<Value*, 16> Operands; |
| 169 | for (unsigned i = 0; i < Node->getNumOperands(); i++) { |
| 170 | Operands.push_back(Node->getOperand(i)); |
| 171 | } |
| 172 | Operands.push_back(Operand); |
| 173 | MDNode *NewNode = MDNode::get(Node->getContext(), Operands); |
| 174 | Node->replaceAllUsesWith(NewNode); |
| 175 | } |
| 176 | |
| 177 | // Clone the module-level debug info associated with OldFunc. The cloned data |
| 178 | // will point to NewFunc instead. |
| 179 | static void CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc, |
| 180 | ValueToValueMapTy &VMap) { |
| 181 | DebugInfoFinder Finder; |
| 182 | Finder.processModule(*OldFunc->getParent()); |
| 183 | |
| 184 | const MDNode *OldSubprogramMDNode = FindSubprogram(OldFunc, Finder); |
| 185 | if (!OldSubprogramMDNode) return; |
| 186 | |
| 187 | // Ensure that OldFunc appears in the map. |
| 188 | // (if it's already there it must point to NewFunc anyway) |
| 189 | VMap[OldFunc] = NewFunc; |
| 190 | DISubprogram NewSubprogram(MapValue(OldSubprogramMDNode, VMap)); |
| 191 | |
| 192 | for (DICompileUnit CU : Finder.compile_units()) { |
| 193 | DIArray Subprograms(CU.getSubprograms()); |
| 194 | |
| 195 | // If the compile unit's function list contains the old function, it should |
| 196 | // also contain the new one. |
| 197 | for (unsigned i = 0; i < Subprograms.getNumElements(); i++) { |
| 198 | if ((MDNode*)Subprograms.getElement(i) == OldSubprogramMDNode) { |
| 199 | AddOperand(Subprograms, NewSubprogram); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 205 | /// CloneFunction - Return a copy of the specified function, but without |
| 206 | /// embedding the function into another module. Also, any references specified |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 207 | /// in the VMap are changed to refer to their mapped value instead of the |
| 208 | /// original one. If any of the arguments to the function are in the VMap, |
| 209 | /// the arguments are deleted from the resultant function. The VMap is |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 210 | /// updated to include mappings from all of the instructions and basicblocks in |
| 211 | /// the function from their old to new values. |
| 212 | /// |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 213 | Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 214 | bool ModuleLevelChanges, |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 215 | ClonedCodeInfo *CodeInfo) { |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 216 | std::vector<Type*> ArgTypes; |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 217 | |
| 218 | // 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] | 219 | // 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] | 220 | // |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 221 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 222 | I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 223 | 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] | 224 | ArgTypes.push_back(I->getType()); |
| 225 | |
| 226 | // Create a new function type... |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 227 | FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 228 | ArgTypes, F->getFunctionType()->isVarArg()); |
| 229 | |
| 230 | // Create the new function... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 231 | Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 233 | // Loop over the arguments, copying the names of the mapped arguments over... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 234 | Function::arg_iterator DestI = NewF->arg_begin(); |
Chris Lattner | a4c29d2 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 235 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 236 | I != E; ++I) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 237 | if (VMap.count(I) == 0) { // Is this argument preserved? |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 238 | DestI->setName(I->getName()); // Copy the name over... |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 239 | VMap[I] = DestI++; // Add mapping to VMap |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 242 | if (ModuleLevelChanges) |
| 243 | CloneDebugInfoMetadata(NewF, F, VMap); |
| 244 | |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 245 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 246 | CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 247 | return NewF; |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 248 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 250 | |
| 251 | |
| 252 | namespace { |
| 253 | /// PruningFunctionCloner - This class is a private class used to implement |
| 254 | /// the CloneAndPruneFunctionInto method. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 255 | struct PruningFunctionCloner { |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 256 | Function *NewFunc; |
| 257 | const Function *OldFunc; |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 258 | ValueToValueMapTy &VMap; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 259 | bool ModuleLevelChanges; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 260 | const char *NameSuffix; |
| 261 | ClonedCodeInfo *CodeInfo; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 262 | const DataLayout *DL; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 263 | public: |
| 264 | PruningFunctionCloner(Function *newFunc, const Function *oldFunc, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 265 | ValueToValueMapTy &valueMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 266 | bool moduleLevelChanges, |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 267 | const char *nameSuffix, |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 268 | ClonedCodeInfo *codeInfo, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 269 | const DataLayout *DL) |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 270 | : NewFunc(newFunc), OldFunc(oldFunc), |
| 271 | VMap(valueMap), ModuleLevelChanges(moduleLevelChanges), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 272 | NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL) { |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /// CloneBlock - The specified block is found to be reachable, clone it and |
| 276 | /// anything that it can reach. |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 277 | void CloneBlock(const BasicBlock *BB, |
| 278 | std::vector<const BasicBlock*> &ToClone); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 279 | }; |
| 280 | } |
| 281 | |
| 282 | /// CloneBlock - The specified block is found to be reachable, clone it and |
| 283 | /// anything that it can reach. |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 284 | void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, |
| 285 | std::vector<const BasicBlock*> &ToClone){ |
Chandler Carruth | afff330 | 2012-03-28 08:38:27 +0000 | [diff] [blame] | 286 | WeakVH &BBEntry = VMap[BB]; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 287 | |
| 288 | // Have we already cloned this block? |
| 289 | if (BBEntry) return; |
| 290 | |
| 291 | // Nope, clone it now. |
| 292 | BasicBlock *NewBB; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 293 | BBEntry = NewBB = BasicBlock::Create(BB->getContext()); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 294 | if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); |
| 295 | |
Eli Friedman | 4090e1c | 2011-10-21 20:45:19 +0000 | [diff] [blame] | 296 | // It is only legal to clone a function if a block address within that |
| 297 | // function is never referenced outside of the function. Given that, we |
| 298 | // want to map block addresses from the old function to block addresses in |
| 299 | // the clone. (This is different from the generic ValueMapper |
| 300 | // implementation, which generates an invalid blockaddress when |
| 301 | // cloning a function.) |
| 302 | // |
| 303 | // Note that we don't need to fix the mapping for unreachable blocks; |
| 304 | // the default mapping there is safe. |
| 305 | if (BB->hasAddressTaken()) { |
| 306 | Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), |
| 307 | const_cast<BasicBlock*>(BB)); |
| 308 | VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB); |
| 309 | } |
| 310 | |
| 311 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 312 | bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; |
| 313 | |
| 314 | // Loop over all instructions, and copy them over, DCE'ing as we go. This |
| 315 | // loop doesn't include the terminator. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 316 | for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end(); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 317 | II != IE; ++II) { |
Chandler Carruth | d54f9a4 | 2012-03-25 04:03:40 +0000 | [diff] [blame] | 318 | Instruction *NewInst = II->clone(); |
| 319 | |
| 320 | // Eagerly remap operands to the newly cloned instruction, except for PHI |
| 321 | // nodes for which we defer processing until we update the CFG. |
| 322 | if (!isa<PHINode>(NewInst)) { |
| 323 | RemapInstruction(NewInst, VMap, |
| 324 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); |
| 325 | |
| 326 | // If we can simplify this instruction to some other value, simply add |
| 327 | // a mapping to that value rather than inserting a new instruction into |
| 328 | // the basic block. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 329 | if (Value *V = SimplifyInstruction(NewInst, DL)) { |
Chandler Carruth | d54f9a4 | 2012-03-25 04:03:40 +0000 | [diff] [blame] | 330 | // On the off-chance that this simplifies to an instruction in the old |
| 331 | // function, map it back into the new function. |
| 332 | if (Value *MappedV = VMap.lookup(V)) |
| 333 | V = MappedV; |
| 334 | |
| 335 | VMap[II] = V; |
| 336 | delete NewInst; |
| 337 | continue; |
| 338 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 339 | } |
Devang Patel | f66d7b5 | 2009-02-10 07:48:18 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 341 | if (II->hasName()) |
| 342 | NewInst->setName(II->getName()+NameSuffix); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 343 | VMap[II] = NewInst; // Add instruction map to value. |
Chandler Carruth | d54f9a4 | 2012-03-25 04:03:40 +0000 | [diff] [blame] | 344 | NewBB->getInstList().push_back(NewInst); |
Dale Johannesen | 8aa90fe | 2009-03-10 22:20:02 +0000 | [diff] [blame] | 345 | hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 346 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
| 347 | if (isa<ConstantInt>(AI->getArraySize())) |
| 348 | hasStaticAllocas = true; |
| 349 | else |
| 350 | hasDynamicAllocas = true; |
| 351 | } |
| 352 | } |
| 353 | |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 354 | // Finally, clone over the terminator. |
| 355 | const TerminatorInst *OldTI = BB->getTerminator(); |
| 356 | bool TerminatorDone = false; |
| 357 | if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { |
| 358 | if (BI->isConditional()) { |
| 359 | // If the condition was a known constant in the callee... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 360 | ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); |
| 361 | // Or is a known constant in the caller... |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 362 | if (Cond == 0) { |
| 363 | Value *V = VMap[BI->getCondition()]; |
| 364 | Cond = dyn_cast_or_null<ConstantInt>(V); |
| 365 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 366 | |
| 367 | // Constant fold to uncond branch! |
| 368 | if (Cond) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 369 | BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 370 | VMap[OldTI] = BranchInst::Create(Dest, NewBB); |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 371 | ToClone.push_back(Dest); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 372 | TerminatorDone = true; |
| 373 | } |
| 374 | } |
| 375 | } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { |
| 376 | // If switching on a value known constant in the caller. |
| 377 | ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 378 | if (Cond == 0) { // Or known constant after constant prop in the callee... |
| 379 | Value *V = VMap[SI->getCondition()]; |
| 380 | Cond = dyn_cast_or_null<ConstantInt>(V); |
| 381 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 382 | if (Cond) { // Constant fold to uncond branch! |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 383 | SwitchInst::ConstCaseIt Case = SI->findCaseValue(Cond); |
| 384 | BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor()); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 385 | VMap[OldTI] = BranchInst::Create(Dest, NewBB); |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 386 | ToClone.push_back(Dest); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 387 | TerminatorDone = true; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (!TerminatorDone) { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 392 | Instruction *NewInst = OldTI->clone(); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 393 | if (OldTI->hasName()) |
| 394 | NewInst->setName(OldTI->getName()+NameSuffix); |
| 395 | NewBB->getInstList().push_back(NewInst); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 396 | VMap[OldTI] = NewInst; // Add instruction map to value. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 397 | |
| 398 | // Recursively clone any reachable successor blocks. |
| 399 | const TerminatorInst *TI = BB->getTerminator(); |
| 400 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 401 | ToClone.push_back(TI->getSuccessor(i)); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 404 | if (CodeInfo) { |
| 405 | CodeInfo->ContainsCalls |= hasCalls; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 406 | CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; |
| 407 | CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && |
| 408 | BB != &BB->getParent()->front(); |
| 409 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 412 | /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, |
| 413 | /// except that it does some simple constant prop and DCE on the fly. The |
| 414 | /// effect of this is to copy significantly less code in cases where (for |
| 415 | /// example) a function call with constant arguments is inlined, and those |
| 416 | /// 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] | 417 | /// 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] | 418 | /// used for things like CloneFunction or CloneModule. |
| 419 | void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, |
Devang Patel | 774cca7 | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 420 | ValueToValueMapTy &VMap, |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 421 | bool ModuleLevelChanges, |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 422 | SmallVectorImpl<ReturnInst*> &Returns, |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 423 | const char *NameSuffix, |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 424 | ClonedCodeInfo *CodeInfo, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 425 | const DataLayout *DL, |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 426 | Instruction *TheCall) { |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 427 | assert(NameSuffix && "NameSuffix cannot be null!"); |
| 428 | |
| 429 | #ifndef NDEBUG |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 430 | for (Function::const_arg_iterator II = OldFunc->arg_begin(), |
| 431 | E = OldFunc->arg_end(); II != E; ++II) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 432 | assert(VMap.count(II) && "No mapping from source argument specified!"); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 433 | #endif |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 434 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 435 | PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 436 | NameSuffix, CodeInfo, DL); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 437 | |
| 438 | // Clone the entry block, and anything recursively reachable from it. |
Chris Lattner | 67ef241 | 2007-03-02 03:11:20 +0000 | [diff] [blame] | 439 | std::vector<const BasicBlock*> CloneWorklist; |
| 440 | CloneWorklist.push_back(&OldFunc->getEntryBlock()); |
| 441 | while (!CloneWorklist.empty()) { |
| 442 | const BasicBlock *BB = CloneWorklist.back(); |
| 443 | CloneWorklist.pop_back(); |
| 444 | PFC.CloneBlock(BB, CloneWorklist); |
| 445 | } |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 446 | |
| 447 | // Loop over all of the basic blocks in the old function. If the block was |
| 448 | // reachable, we have cloned it and the old block is now in the value map: |
| 449 | // insert it into the new function in the right order. If not, ignore it. |
| 450 | // |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 451 | // Defer PHI resolution until rest of function is resolved. |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 452 | SmallVector<const PHINode*, 16> PHIToResolve; |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 453 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 454 | BI != BE; ++BI) { |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 455 | Value *V = VMap[BI]; |
| 456 | BasicBlock *NewBB = cast_or_null<BasicBlock>(V); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 457 | if (NewBB == 0) continue; // Dead block. |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 459 | // Add the new block to the new function. |
| 460 | NewFunc->getBasicBlockList().push_back(NewBB); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 462 | // Handle PHI nodes specially, as we have to remove references to dead |
| 463 | // blocks. |
Chandler Carruth | d54f9a4 | 2012-03-25 04:03:40 +0000 | [diff] [blame] | 464 | for (BasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) |
| 465 | if (const PHINode *PN = dyn_cast<PHINode>(I)) |
| 466 | PHIToResolve.push_back(PN); |
| 467 | else |
| 468 | break; |
| 469 | |
| 470 | // Finally, remap the terminator instructions, as those can't be remapped |
| 471 | // until all BBs are mapped. |
| 472 | RemapInstruction(NewBB->getTerminator(), VMap, |
| 473 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 474 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 475 | |
| 476 | // Defer PHI resolution until rest of function is resolved, PHI resolution |
| 477 | // requires the CFG to be up-to-date. |
| 478 | for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { |
| 479 | const PHINode *OPN = PHIToResolve[phino]; |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 480 | unsigned NumPreds = OPN->getNumIncomingValues(); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 481 | const BasicBlock *OldBB = OPN->getParent(); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 482 | BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 483 | |
| 484 | // Map operands for blocks that are live and remove operands for blocks |
| 485 | // that are dead. |
| 486 | for (; phino != PHIToResolve.size() && |
| 487 | PHIToResolve[phino]->getParent() == OldBB; ++phino) { |
| 488 | OPN = PHIToResolve[phino]; |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 489 | PHINode *PN = cast<PHINode>(VMap[OPN]); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 490 | for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame] | 491 | Value *V = VMap[PN->getIncomingBlock(pred)]; |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 492 | if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 493 | Value *InVal = MapValue(PN->getIncomingValue(pred), |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 494 | VMap, |
| 495 | ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 496 | assert(InVal && "Unknown input value?"); |
| 497 | PN->setIncomingValue(pred, InVal); |
| 498 | PN->setIncomingBlock(pred, MappedBlock); |
| 499 | } else { |
| 500 | PN->removeIncomingValue(pred, false); |
| 501 | --pred, --e; // Revisit the next entry. |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // The loop above has removed PHI entries for those blocks that are dead |
| 507 | // and has updated others. However, if a block is live (i.e. copied over) |
| 508 | // but its terminator has been changed to not go to this block, then our |
| 509 | // phi nodes will have invalid entries. Update the PHI nodes in this |
| 510 | // case. |
| 511 | PHINode *PN = cast<PHINode>(NewBB->begin()); |
| 512 | NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); |
| 513 | if (NumPreds != PN->getNumIncomingValues()) { |
| 514 | assert(NumPreds < PN->getNumIncomingValues()); |
| 515 | // Count how many times each predecessor comes to this block. |
| 516 | std::map<BasicBlock*, unsigned> PredCount; |
| 517 | for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); |
| 518 | PI != E; ++PI) |
| 519 | --PredCount[*PI]; |
| 520 | |
| 521 | // Figure out how many entries to remove from each PHI. |
| 522 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 523 | ++PredCount[PN->getIncomingBlock(i)]; |
| 524 | |
| 525 | // At this point, the excess predecessor entries are positive in the |
| 526 | // map. Loop over all of the PHIs and remove excess predecessor |
| 527 | // entries. |
| 528 | BasicBlock::iterator I = NewBB->begin(); |
| 529 | for (; (PN = dyn_cast<PHINode>(I)); ++I) { |
| 530 | for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), |
| 531 | E = PredCount.end(); PCI != E; ++PCI) { |
| 532 | BasicBlock *Pred = PCI->first; |
| 533 | for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) |
| 534 | PN->removeIncomingValue(Pred, false); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // If the loops above have made these phi nodes have 0 or 1 operand, |
| 540 | // replace them with undef or the input value. We must do this for |
| 541 | // correctness, because 0-operand phis are not valid. |
| 542 | PN = cast<PHINode>(NewBB->begin()); |
| 543 | if (PN->getNumIncomingValues() == 0) { |
| 544 | BasicBlock::iterator I = NewBB->begin(); |
| 545 | BasicBlock::const_iterator OldI = OldBB->begin(); |
| 546 | while ((PN = dyn_cast<PHINode>(I++))) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 547 | Value *NV = UndefValue::get(PN->getType()); |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 548 | PN->replaceAllUsesWith(NV); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 549 | assert(VMap[OldI] == PN && "VMap mismatch"); |
| 550 | VMap[OldI] = NV; |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 551 | PN->eraseFromParent(); |
| 552 | ++OldI; |
| 553 | } |
Chris Lattner | 35033ef | 2006-06-01 19:19:23 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
Chandler Carruth | f8c8a9c | 2012-03-25 10:34:54 +0000 | [diff] [blame] | 556 | |
| 557 | // Make a second pass over the PHINodes now that all of them have been |
| 558 | // remapped into the new function, simplifying the PHINode and performing any |
| 559 | // recursive simplifications exposed. This will transparently update the |
Chandler Carruth | afff330 | 2012-03-28 08:38:27 +0000 | [diff] [blame] | 560 | // WeakVH in the VMap. Notably, we rely on that so that if we coalesce |
Chandler Carruth | f8c8a9c | 2012-03-25 10:34:54 +0000 | [diff] [blame] | 561 | // two PHINodes, the iteration over the old PHIs remains valid, and the |
| 562 | // mapping will just map us to the new node (which may not even be a PHI |
| 563 | // node). |
| 564 | for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx) |
| 565 | if (PHINode *PN = dyn_cast<PHINode>(VMap[PHIToResolve[Idx]])) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 566 | recursivelySimplifyInstruction(PN, DL); |
Chandler Carruth | f8c8a9c | 2012-03-25 10:34:54 +0000 | [diff] [blame] | 567 | |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 568 | // Now that the inlined function body has been fully constructed, go through |
| 569 | // and zap unconditional fall-through branches. This happen all the time when |
| 570 | // specializing code: code specialization turns conditional branches into |
| 571 | // uncond branches, and this code folds them. |
Chandler Carruth | afff330 | 2012-03-28 08:38:27 +0000 | [diff] [blame] | 572 | Function::iterator Begin = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]); |
| 573 | Function::iterator I = Begin; |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 574 | while (I != NewFunc->end()) { |
Chandler Carruth | afff330 | 2012-03-28 08:38:27 +0000 | [diff] [blame] | 575 | // Check if this block has become dead during inlining or other |
| 576 | // simplifications. Note that the first block will appear dead, as it has |
| 577 | // not yet been wired up properly. |
| 578 | if (I != Begin && (pred_begin(I) == pred_end(I) || |
| 579 | I->getSinglePredecessor() == I)) { |
| 580 | BasicBlock *DeadBB = I++; |
| 581 | DeleteDeadBlock(DeadBB); |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | // We need to simplify conditional branches and switches with a constant |
| 586 | // operand. We try to prune these out when cloning, but if the |
| 587 | // simplification required looking through PHI nodes, those are only |
| 588 | // available after forming the full basic block. That may leave some here, |
| 589 | // and we still want to prune the dead code as early as possible. |
| 590 | ConstantFoldTerminator(I); |
| 591 | |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 592 | BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); |
| 593 | if (!BI || BI->isConditional()) { ++I; continue; } |
| 594 | |
| 595 | BasicBlock *Dest = BI->getSuccessor(0); |
Chandler Carruth | f8c8a9c | 2012-03-25 10:34:54 +0000 | [diff] [blame] | 596 | if (!Dest->getSinglePredecessor()) { |
Chris Lattner | 8e8eda7 | 2007-02-01 18:48:38 +0000 | [diff] [blame] | 597 | ++I; continue; |
| 598 | } |
Chandler Carruth | f8c8a9c | 2012-03-25 10:34:54 +0000 | [diff] [blame] | 599 | |
| 600 | // We shouldn't be able to get single-entry PHI nodes here, as instsimplify |
| 601 | // above should have zapped all of them.. |
| 602 | assert(!isa<PHINode>(Dest->begin())); |
| 603 | |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 604 | // We know all single-entry PHI nodes in the inlined function have been |
| 605 | // removed, so we just need to splice the blocks. |
| 606 | BI->eraseFromParent(); |
| 607 | |
Eric Christopher | e59fbc0 | 2011-06-23 06:24:52 +0000 | [diff] [blame] | 608 | // Make all PHI nodes that referred to Dest now refer to I as their source. |
| 609 | Dest->replaceAllUsesWith(I); |
| 610 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 611 | // Move all the instructions in the succ to the pred. |
| 612 | I->getInstList().splice(I->end(), Dest->getInstList()); |
| 613 | |
Chris Lattner | a4646b6 | 2006-09-13 21:27:00 +0000 | [diff] [blame] | 614 | // Remove the dest block. |
| 615 | Dest->eraseFromParent(); |
| 616 | |
| 617 | // Do not increment I, iteratively merge all things this block branches to. |
| 618 | } |
Chandler Carruth | 9ceebb7 | 2012-04-06 17:21:31 +0000 | [diff] [blame] | 619 | |
| 620 | // Make a final pass over the basic blocks from theh old function to gather |
| 621 | // any return instructions which survived folding. We have to do this here |
| 622 | // because we can iteratively remove and merge returns above. |
| 623 | for (Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]), |
| 624 | E = NewFunc->end(); |
| 625 | I != E; ++I) |
| 626 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) |
| 627 | Returns.push_back(RI); |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 628 | } |