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