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