Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 1 | //===- InstCombinePHI.cpp -------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visitPHINode function. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "InstCombine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/SmallPtrSet.h" |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(a,c)] |
| 22 | /// and if a/b/c and the add's all have a single use, turn this into a phi |
| 23 | /// and a single binop. |
| 24 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 25 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 26 | assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)); |
| 27 | unsigned Opc = FirstInst->getOpcode(); |
| 28 | Value *LHSVal = FirstInst->getOperand(0); |
| 29 | Value *RHSVal = FirstInst->getOperand(1); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 31 | Type *LHSType = LHSVal->getType(); |
| 32 | Type *RHSType = RHSVal->getType(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 33 | |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 34 | bool isNUW = false, isNSW = false, isExact = false; |
| 35 | if (OverflowingBinaryOperator *BO = |
| 36 | dyn_cast<OverflowingBinaryOperator>(FirstInst)) { |
| 37 | isNUW = BO->hasNoUnsignedWrap(); |
| 38 | isNSW = BO->hasNoSignedWrap(); |
| 39 | } else if (PossiblyExactOperator *PEO = |
| 40 | dyn_cast<PossiblyExactOperator>(FirstInst)) |
| 41 | isExact = PEO->isExact(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 43 | // Scan to see if all operands are the same opcode, and all have one use. |
| 44 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
| 45 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
| 46 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
| 47 | // Verify type of the LHS matches so we don't fold cmp's of different |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 48 | // types. |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 49 | I->getOperand(0)->getType() != LHSType || |
| 50 | I->getOperand(1)->getType() != RHSType) |
| 51 | return 0; |
| 52 | |
| 53 | // If they are CmpInst instructions, check their predicates |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 54 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 55 | if (CI->getPredicate() != cast<CmpInst>(FirstInst)->getPredicate()) |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 56 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 57 | |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 58 | if (isNUW) |
| 59 | isNUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap(); |
| 60 | if (isNSW) |
| 61 | isNSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
| 62 | if (isExact) |
| 63 | isExact = cast<PossiblyExactOperator>(I)->isExact(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 65 | // Keep track of which operand needs a phi node. |
| 66 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 67 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
| 68 | } |
| 69 | |
| 70 | // If both LHS and RHS would need a PHI, don't do this transformation, |
| 71 | // because it would increase the number of PHIs entering the block, |
| 72 | // which leads to higher register pressure. This is especially |
| 73 | // bad when the PHIs are in the header of a loop. |
| 74 | if (!LHSVal && !RHSVal) |
| 75 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 76 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 77 | // Otherwise, this is safe to transform! |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 78 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 79 | Value *InLHS = FirstInst->getOperand(0); |
| 80 | Value *InRHS = FirstInst->getOperand(1); |
| 81 | PHINode *NewLHS = 0, *NewRHS = 0; |
| 82 | if (LHSVal == 0) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 83 | NewLHS = PHINode::Create(LHSType, PN.getNumIncomingValues(), |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 84 | FirstInst->getOperand(0)->getName() + ".pn"); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 85 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
| 86 | InsertNewInstBefore(NewLHS, PN); |
| 87 | LHSVal = NewLHS; |
| 88 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 90 | if (RHSVal == 0) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 91 | NewRHS = PHINode::Create(RHSType, PN.getNumIncomingValues(), |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 92 | FirstInst->getOperand(1)->getName() + ".pn"); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 93 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
| 94 | InsertNewInstBefore(NewRHS, PN); |
| 95 | RHSVal = NewRHS; |
| 96 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 97 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 98 | // Add all operands to the new PHIs. |
| 99 | if (NewLHS || NewRHS) { |
| 100 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 101 | Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i)); |
| 102 | if (NewLHS) { |
| 103 | Value *NewInLHS = InInst->getOperand(0); |
| 104 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 105 | } |
| 106 | if (NewRHS) { |
| 107 | Value *NewInRHS = InInst->getOperand(1); |
| 108 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 109 | } |
| 110 | } |
| 111 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 112 | |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 113 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) { |
| 114 | CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 115 | LHSVal, RHSVal); |
| 116 | NewCI->setDebugLoc(FirstInst->getDebugLoc()); |
| 117 | return NewCI; |
| 118 | } |
| 119 | |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 120 | BinaryOperator *BinOp = cast<BinaryOperator>(FirstInst); |
| 121 | BinaryOperator *NewBinOp = |
| 122 | BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
| 123 | if (isNUW) NewBinOp->setHasNoUnsignedWrap(); |
| 124 | if (isNSW) NewBinOp->setHasNoSignedWrap(); |
| 125 | if (isExact) NewBinOp->setIsExact(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 126 | NewBinOp->setDebugLoc(FirstInst->getDebugLoc()); |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 127 | return NewBinOp; |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) { |
| 131 | GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 132 | |
| 133 | SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(), |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 134 | FirstInst->op_end()); |
| 135 | // This is true if all GEP bases are allocas and if all indices into them are |
| 136 | // constants. |
| 137 | bool AllBasePointersAreAllocas = true; |
| 138 | |
| 139 | // We don't want to replace this phi if the replacement would require |
| 140 | // more than one phi, which leads to higher register pressure. This is |
| 141 | // especially bad when the PHIs are in the header of a loop. |
| 142 | bool NeededPhi = false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | abb8eb2 | 2011-02-17 22:21:26 +0000 | [diff] [blame] | 144 | bool AllInBounds = true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 145 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 146 | // Scan to see if all operands are the same opcode, and all have one use. |
| 147 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
| 148 | GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 149 | if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() || |
| 150 | GEP->getNumOperands() != FirstInst->getNumOperands()) |
| 151 | return 0; |
| 152 | |
Chris Lattner | abb8eb2 | 2011-02-17 22:21:26 +0000 | [diff] [blame] | 153 | AllInBounds &= GEP->isInBounds(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 154 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 155 | // Keep track of whether or not all GEPs are of alloca pointers. |
| 156 | if (AllBasePointersAreAllocas && |
| 157 | (!isa<AllocaInst>(GEP->getOperand(0)) || |
| 158 | !GEP->hasAllConstantIndices())) |
| 159 | AllBasePointersAreAllocas = false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 160 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 161 | // Compare the operand lists. |
| 162 | for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) { |
| 163 | if (FirstInst->getOperand(op) == GEP->getOperand(op)) |
| 164 | continue; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 165 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 166 | // Don't merge two GEPs when two operands differ (introducing phi nodes) |
| 167 | // if one of the PHIs has a constant for the index. The index may be |
| 168 | // substantially cheaper to compute for the constants, so making it a |
| 169 | // variable index could pessimize the path. This also handles the case |
| 170 | // for struct indices, which must always be constant. |
| 171 | if (isa<ConstantInt>(FirstInst->getOperand(op)) || |
| 172 | isa<ConstantInt>(GEP->getOperand(op))) |
| 173 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 175 | if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType()) |
| 176 | return 0; |
| 177 | |
| 178 | // If we already needed a PHI for an earlier operand, and another operand |
| 179 | // also requires a PHI, we'd be introducing more PHIs than we're |
| 180 | // eliminating, which increases register pressure on entry to the PHI's |
| 181 | // block. |
| 182 | if (NeededPhi) |
| 183 | return 0; |
| 184 | |
| 185 | FixedOperands[op] = 0; // Needs a PHI. |
| 186 | NeededPhi = true; |
| 187 | } |
| 188 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 189 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 190 | // If all of the base pointers of the PHI'd GEPs are from allocas, don't |
| 191 | // bother doing this transformation. At best, this will just save a bit of |
| 192 | // offset calculation, but all the predecessors will have to materialize the |
| 193 | // stack address into a register anyway. We'd actually rather *clone* the |
| 194 | // load up into the predecessors so that we have a load of a gep of an alloca, |
| 195 | // which can usually all be folded into the load. |
| 196 | if (AllBasePointersAreAllocas) |
| 197 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 198 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 199 | // Otherwise, this is safe to transform. Insert PHI nodes for each operand |
| 200 | // that is variable. |
| 201 | SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size()); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 202 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 203 | bool HasAnyPHIs = false; |
| 204 | for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) { |
| 205 | if (FixedOperands[i]) continue; // operand doesn't need a phi. |
| 206 | Value *FirstOp = FirstInst->getOperand(i); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 207 | PHINode *NewPN = PHINode::Create(FirstOp->getType(), e, |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 208 | FirstOp->getName()+".pn"); |
| 209 | InsertNewInstBefore(NewPN, PN); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 211 | NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0)); |
| 212 | OperandPhis[i] = NewPN; |
| 213 | FixedOperands[i] = NewPN; |
| 214 | HasAnyPHIs = true; |
| 215 | } |
| 216 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 217 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 218 | // Add all operands to the new PHIs. |
| 219 | if (HasAnyPHIs) { |
| 220 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 221 | GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 222 | BasicBlock *InBB = PN.getIncomingBlock(i); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 223 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 224 | for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op) |
| 225 | if (PHINode *OpPhi = OperandPhis[op]) |
| 226 | OpPhi->addIncoming(InGEP->getOperand(op), InBB); |
| 227 | } |
| 228 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 230 | Value *Base = FixedOperands[0]; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 231 | GetElementPtrInst *NewGEP = |
Frits van Bommel | ede0dc6 | 2011-07-25 15:13:01 +0000 | [diff] [blame] | 232 | GetElementPtrInst::Create(Base, makeArrayRef(FixedOperands).slice(1)); |
Chris Lattner | 75ae5a4 | 2011-02-17 22:32:54 +0000 | [diff] [blame] | 233 | if (AllInBounds) NewGEP->setIsInBounds(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 234 | NewGEP->setDebugLoc(FirstInst->getDebugLoc()); |
Chris Lattner | abb8eb2 | 2011-02-17 22:21:26 +0000 | [diff] [blame] | 235 | return NewGEP; |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to |
| 240 | /// sink the load out of the block that defines it. This means that it must be |
| 241 | /// obvious the value of the load is not changed from the point of the load to |
| 242 | /// the end of the block it is in. |
| 243 | /// |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 244 | /// Finally, it is safe, but not profitable, to sink a load targeting a |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 245 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 246 | /// to a register. |
| 247 | static bool isSafeAndProfitableToSinkLoad(LoadInst *L) { |
| 248 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 249 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 250 | for (++BBI; BBI != E; ++BBI) |
| 251 | if (BBI->mayWriteToMemory()) |
| 252 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 253 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 254 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 255 | // profitable to do this xform. |
| 256 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 257 | bool isAddressTaken = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 258 | for (User *U : AI->users()) { |
Gabor Greif | 96fedcb | 2010-07-12 14:15:58 +0000 | [diff] [blame] | 259 | if (isa<LoadInst>(U)) continue; |
| 260 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 261 | // If storing TO the alloca, then the address isn't taken. |
| 262 | if (SI->getOperand(1) == AI) continue; |
| 263 | } |
| 264 | isAddressTaken = true; |
| 265 | break; |
| 266 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 267 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 268 | if (!isAddressTaken && AI->isStaticAlloca()) |
| 269 | return false; |
| 270 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 272 | // If this load is a load from a GEP with a constant offset from an alloca, |
| 273 | // then we don't want to sink it. In its present form, it will be |
| 274 | // load [constant stack offset]. Sinking it will cause us to have to |
| 275 | // materialize the stack addresses in each predecessor in a register only to |
| 276 | // do a shared load from register in the successor. |
| 277 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0))) |
| 278 | if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0))) |
| 279 | if (AI->isStaticAlloca() && GEP->hasAllConstantIndices()) |
| 280 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 281 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
| 285 | Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) { |
| 286 | LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0)); |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 287 | |
| 288 | // FIXME: This is overconservative; this transform is allowed in some cases |
| 289 | // for atomic operations. |
| 290 | if (FirstLI->isAtomic()) |
| 291 | return 0; |
| 292 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 293 | // When processing loads, we need to propagate two bits of information to the |
| 294 | // sunk load: whether it is volatile, and what its alignment is. We currently |
| 295 | // don't sink loads when some have their alignment specified and some don't. |
| 296 | // visitLoadInst will propagate an alignment onto the load when TD is around, |
| 297 | // and if TD isn't around, we can't handle the mixed case. |
| 298 | bool isVolatile = FirstLI->isVolatile(); |
| 299 | unsigned LoadAlignment = FirstLI->getAlignment(); |
Chris Lattner | f6befff | 2010-03-05 18:53:28 +0000 | [diff] [blame] | 300 | unsigned LoadAddrSpace = FirstLI->getPointerAddressSpace(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 301 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 302 | // We can't sink the load if the loaded value could be modified between the |
| 303 | // load and the PHI. |
| 304 | if (FirstLI->getParent() != PN.getIncomingBlock(0) || |
| 305 | !isSafeAndProfitableToSinkLoad(FirstLI)) |
| 306 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 307 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 308 | // If the PHI is of volatile loads and the load block has multiple |
| 309 | // successors, sinking it would remove a load of the volatile value from |
| 310 | // the path through the other successor. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 311 | if (isVolatile && |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 312 | FirstLI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 313 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 315 | // Check to see if all arguments are the same operation. |
| 316 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 317 | LoadInst *LI = dyn_cast<LoadInst>(PN.getIncomingValue(i)); |
| 318 | if (!LI || !LI->hasOneUse()) |
| 319 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 320 | |
| 321 | // We can't sink the load if the loaded value could be modified between |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 322 | // the load and the PHI. |
| 323 | if (LI->isVolatile() != isVolatile || |
| 324 | LI->getParent() != PN.getIncomingBlock(i) || |
Chris Lattner | f6befff | 2010-03-05 18:53:28 +0000 | [diff] [blame] | 325 | LI->getPointerAddressSpace() != LoadAddrSpace || |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 326 | !isSafeAndProfitableToSinkLoad(LI)) |
| 327 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 328 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 329 | // If some of the loads have an alignment specified but not all of them, |
| 330 | // we can't do the transformation. |
| 331 | if ((LoadAlignment != 0) != (LI->getAlignment() != 0)) |
| 332 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 333 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 334 | LoadAlignment = std::min(LoadAlignment, LI->getAlignment()); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 335 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 336 | // If the PHI is of volatile loads and the load block has multiple |
| 337 | // successors, sinking it would remove a load of the volatile value from |
| 338 | // the path through the other successor. |
| 339 | if (isVolatile && |
| 340 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 341 | return 0; |
| 342 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 343 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 344 | // Okay, they are all the same operation. Create a new PHI node of the |
| 345 | // correct type, and PHI together all of the LHS's of the instructions. |
| 346 | PHINode *NewPN = PHINode::Create(FirstLI->getOperand(0)->getType(), |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 347 | PN.getNumIncomingValues(), |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 348 | PN.getName()+".in"); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 349 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 350 | Value *InVal = FirstLI->getOperand(0); |
| 351 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 352 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 353 | // Add all operands to the new PHI. |
| 354 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 355 | Value *NewInVal = cast<LoadInst>(PN.getIncomingValue(i))->getOperand(0); |
| 356 | if (NewInVal != InVal) |
| 357 | InVal = 0; |
| 358 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 359 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 360 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 361 | Value *PhiVal; |
| 362 | if (InVal) { |
| 363 | // The new PHI unions all of the same values together. This is really |
| 364 | // common, so we handle it intelligently here for compile-time speed. |
| 365 | PhiVal = InVal; |
| 366 | delete NewPN; |
| 367 | } else { |
| 368 | InsertNewInstBefore(NewPN, PN); |
| 369 | PhiVal = NewPN; |
| 370 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 371 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 372 | // If this was a volatile load that we are merging, make sure to loop through |
| 373 | // and mark all the input loads as non-volatile. If we don't do this, we will |
| 374 | // insert a new volatile load and the old ones will not be deletable. |
| 375 | if (isVolatile) |
| 376 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 377 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 378 | |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 379 | LoadInst *NewLI = new LoadInst(PhiVal, "", isVolatile, LoadAlignment); |
| 380 | NewLI->setDebugLoc(FirstLI->getDebugLoc()); |
| 381 | return NewLI; |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | /// FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 387 | /// operator and they all are only used by the PHI, PHI together their |
| 388 | /// inputs, and do the operation once, to the result of the PHI. |
| 389 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 390 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 391 | |
| 392 | if (isa<GetElementPtrInst>(FirstInst)) |
| 393 | return FoldPHIArgGEPIntoPHI(PN); |
| 394 | if (isa<LoadInst>(FirstInst)) |
| 395 | return FoldPHIArgLoadIntoPHI(PN); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 397 | // Scan the instruction, looking for input operations that can be folded away. |
| 398 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 399 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 400 | // code size and simplifying code. |
| 401 | Constant *ConstantOp = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 402 | Type *CastSrcTy = 0; |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 403 | bool isNUW = false, isNSW = false, isExact = false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 404 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 405 | if (isa<CastInst>(FirstInst)) { |
| 406 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 407 | |
| 408 | // Be careful about transforming integer PHIs. We don't want to pessimize |
| 409 | // the code by turning an i32 into an i1293. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 410 | if (PN.getType()->isIntegerTy() && CastSrcTy->isIntegerTy()) { |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 411 | if (!ShouldChangeType(PN.getType(), CastSrcTy)) |
| 412 | return 0; |
| 413 | } |
| 414 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 415 | // Can fold binop, compare or shift here if the RHS is a constant, |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 416 | // otherwise call FoldPHIArgBinOpIntoPHI. |
| 417 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 418 | if (ConstantOp == 0) |
| 419 | return FoldPHIArgBinOpIntoPHI(PN); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 421 | if (OverflowingBinaryOperator *BO = |
| 422 | dyn_cast<OverflowingBinaryOperator>(FirstInst)) { |
| 423 | isNUW = BO->hasNoUnsignedWrap(); |
| 424 | isNSW = BO->hasNoSignedWrap(); |
| 425 | } else if (PossiblyExactOperator *PEO = |
| 426 | dyn_cast<PossiblyExactOperator>(FirstInst)) |
| 427 | isExact = PEO->isExact(); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 428 | } else { |
| 429 | return 0; // Cannot fold this operation. |
| 430 | } |
| 431 | |
| 432 | // Check to see if all arguments are the same operation. |
| 433 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 434 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
| 435 | if (I == 0 || !I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
| 436 | return 0; |
| 437 | if (CastSrcTy) { |
| 438 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 439 | return 0; // Cast operation must match. |
| 440 | } else if (I->getOperand(1) != ConstantOp) { |
| 441 | return 0; |
| 442 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 443 | |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 444 | if (isNUW) |
| 445 | isNUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap(); |
| 446 | if (isNSW) |
| 447 | isNSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
| 448 | if (isExact) |
| 449 | isExact = cast<PossiblyExactOperator>(I)->isExact(); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Okay, they are all the same operation. Create a new PHI node of the |
| 453 | // correct type, and PHI together all of the LHS's of the instructions. |
| 454 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 455 | PN.getNumIncomingValues(), |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 456 | PN.getName()+".in"); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 457 | |
| 458 | Value *InVal = FirstInst->getOperand(0); |
| 459 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
| 460 | |
| 461 | // Add all operands to the new PHI. |
| 462 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 463 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 464 | if (NewInVal != InVal) |
| 465 | InVal = 0; |
| 466 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 467 | } |
| 468 | |
| 469 | Value *PhiVal; |
| 470 | if (InVal) { |
| 471 | // The new PHI unions all of the same values together. This is really |
| 472 | // common, so we handle it intelligently here for compile-time speed. |
| 473 | PhiVal = InVal; |
| 474 | delete NewPN; |
| 475 | } else { |
| 476 | InsertNewInstBefore(NewPN, PN); |
| 477 | PhiVal = NewPN; |
| 478 | } |
| 479 | |
| 480 | // Insert and return the new operation. |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 481 | if (CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) { |
| 482 | CastInst *NewCI = CastInst::Create(FirstCI->getOpcode(), PhiVal, |
| 483 | PN.getType()); |
| 484 | NewCI->setDebugLoc(FirstInst->getDebugLoc()); |
| 485 | return NewCI; |
| 486 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 487 | |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 488 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) { |
| 489 | BinOp = BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
| 490 | if (isNUW) BinOp->setHasNoUnsignedWrap(); |
| 491 | if (isNSW) BinOp->setHasNoSignedWrap(); |
| 492 | if (isExact) BinOp->setIsExact(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 493 | BinOp->setDebugLoc(FirstInst->getDebugLoc()); |
Chris Lattner | a8fed47 | 2011-02-17 23:01:49 +0000 | [diff] [blame] | 494 | return BinOp; |
| 495 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 496 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 497 | CmpInst *CIOp = cast<CmpInst>(FirstInst); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 498 | CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 499 | PhiVal, ConstantOp); |
| 500 | NewCI->setDebugLoc(FirstInst->getDebugLoc()); |
| 501 | return NewCI; |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 505 | /// that is dead. |
| 506 | static bool DeadPHICycle(PHINode *PN, |
| 507 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
| 508 | if (PN->use_empty()) return true; |
| 509 | if (!PN->hasOneUse()) return false; |
| 510 | |
| 511 | // Remember this node, and if we find the cycle, return. |
| 512 | if (!PotentiallyDeadPHIs.insert(PN)) |
| 513 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 514 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 515 | // Don't scan crazily complex things. |
| 516 | if (PotentiallyDeadPHIs.size() == 16) |
| 517 | return false; |
| 518 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 519 | if (PHINode *PU = dyn_cast<PHINode>(PN->user_back())) |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 520 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
| 521 | |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 526 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 527 | /// z = some value; x = phi (y, z); y = phi (x, z) |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 528 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 529 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 530 | // See if we already saw this PHI node. |
| 531 | if (!ValueEqualPHIs.insert(PN)) |
| 532 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 533 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 534 | // Don't scan crazily complex things. |
| 535 | if (ValueEqualPHIs.size() == 16) |
| 536 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 538 | // Scan the operands to see if they are either phi nodes or are equal to |
| 539 | // the value. |
| 540 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 541 | Value *Op = PN->getIncomingValue(i); |
| 542 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 543 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 544 | return false; |
| 545 | } else if (Op != NonPhiInVal) |
| 546 | return false; |
| 547 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 548 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 549 | return true; |
| 550 | } |
| 551 | |
| 552 | |
| 553 | namespace { |
| 554 | struct PHIUsageRecord { |
| 555 | unsigned PHIId; // The ID # of the PHI (something determinstic to sort on) |
| 556 | unsigned Shift; // The amount shifted. |
| 557 | Instruction *Inst; // The trunc instruction. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 559 | PHIUsageRecord(unsigned pn, unsigned Sh, Instruction *User) |
| 560 | : PHIId(pn), Shift(Sh), Inst(User) {} |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 561 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 562 | bool operator<(const PHIUsageRecord &RHS) const { |
| 563 | if (PHIId < RHS.PHIId) return true; |
| 564 | if (PHIId > RHS.PHIId) return false; |
| 565 | if (Shift < RHS.Shift) return true; |
| 566 | if (Shift > RHS.Shift) return false; |
| 567 | return Inst->getType()->getPrimitiveSizeInBits() < |
| 568 | RHS.Inst->getType()->getPrimitiveSizeInBits(); |
| 569 | } |
| 570 | }; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 572 | struct LoweredPHIRecord { |
| 573 | PHINode *PN; // The PHI that was lowered. |
| 574 | unsigned Shift; // The amount shifted. |
| 575 | unsigned Width; // The width extracted. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 577 | LoweredPHIRecord(PHINode *pn, unsigned Sh, Type *Ty) |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 578 | : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {} |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 580 | // Ctor form used by DenseMap. |
| 581 | LoweredPHIRecord(PHINode *pn, unsigned Sh) |
| 582 | : PN(pn), Shift(Sh), Width(0) {} |
| 583 | }; |
| 584 | } |
| 585 | |
| 586 | namespace llvm { |
| 587 | template<> |
| 588 | struct DenseMapInfo<LoweredPHIRecord> { |
| 589 | static inline LoweredPHIRecord getEmptyKey() { |
| 590 | return LoweredPHIRecord(0, 0); |
| 591 | } |
| 592 | static inline LoweredPHIRecord getTombstoneKey() { |
| 593 | return LoweredPHIRecord(0, 1); |
| 594 | } |
| 595 | static unsigned getHashValue(const LoweredPHIRecord &Val) { |
| 596 | return DenseMapInfo<PHINode*>::getHashValue(Val.PN) ^ (Val.Shift>>3) ^ |
| 597 | (Val.Width>>3); |
| 598 | } |
| 599 | static bool isEqual(const LoweredPHIRecord &LHS, |
| 600 | const LoweredPHIRecord &RHS) { |
| 601 | return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift && |
| 602 | LHS.Width == RHS.Width; |
| 603 | } |
| 604 | }; |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | |
| 608 | /// SliceUpIllegalIntegerPHI - This is an integer PHI and we know that it has an |
| 609 | /// illegal type: see if it is only used by trunc or trunc(lshr) operations. If |
| 610 | /// so, we split the PHI into the various pieces being extracted. This sort of |
| 611 | /// thing is introduced when SROA promotes an aggregate to large integer values. |
| 612 | /// |
| 613 | /// TODO: The user of the trunc may be an bitcast to float/double/vector or an |
| 614 | /// inttoptr. We should produce new PHIs in the right type. |
| 615 | /// |
| 616 | Instruction *InstCombiner::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) { |
| 617 | // PHIUsers - Keep track of all of the truncated values extracted from a set |
| 618 | // of PHIs, along with their offset. These are the things we want to rewrite. |
| 619 | SmallVector<PHIUsageRecord, 16> PHIUsers; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 620 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 621 | // PHIs are often mutually cyclic, so we keep track of a whole set of PHI |
| 622 | // nodes which are extracted from. PHIsToSlice is a set we use to avoid |
| 623 | // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to |
| 624 | // check the uses of (to ensure they are all extracts). |
| 625 | SmallVector<PHINode*, 8> PHIsToSlice; |
| 626 | SmallPtrSet<PHINode*, 8> PHIsInspected; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 627 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 628 | PHIsToSlice.push_back(&FirstPhi); |
| 629 | PHIsInspected.insert(&FirstPhi); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 630 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 631 | for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) { |
| 632 | PHINode *PN = PHIsToSlice[PHIId]; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 633 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 634 | // Scan the input list of the PHI. If any input is an invoke, and if the |
| 635 | // input is defined in the predecessor, then we won't be split the critical |
| 636 | // edge which is required to insert a truncate. Because of this, we have to |
| 637 | // bail out. |
| 638 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 639 | InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i)); |
| 640 | if (II == 0) continue; |
| 641 | if (II->getParent() != PN->getIncomingBlock(i)) |
| 642 | continue; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 643 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 644 | // If we have a phi, and if it's directly in the predecessor, then we have |
| 645 | // a critical edge where we need to put the truncate. Since we can't |
| 646 | // split the edge in instcombine, we have to bail out. |
| 647 | return 0; |
| 648 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 649 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 650 | for (User *U : PN->users()) { |
| 651 | Instruction *UserI = cast<Instruction>(U); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 652 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 653 | // If the user is a PHI, inspect its uses recursively. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 654 | if (PHINode *UserPN = dyn_cast<PHINode>(UserI)) { |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 655 | if (PHIsInspected.insert(UserPN)) |
| 656 | PHIsToSlice.push_back(UserPN); |
| 657 | continue; |
| 658 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 659 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 660 | // Truncates are always ok. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 661 | if (isa<TruncInst>(UserI)) { |
| 662 | PHIUsers.push_back(PHIUsageRecord(PHIId, 0, UserI)); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 663 | continue; |
| 664 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 665 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 666 | // Otherwise it must be a lshr which can only be used by one trunc. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 667 | if (UserI->getOpcode() != Instruction::LShr || |
| 668 | !UserI->hasOneUse() || !isa<TruncInst>(UserI->user_back()) || |
| 669 | !isa<ConstantInt>(UserI->getOperand(1))) |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 670 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 671 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 672 | unsigned Shift = cast<ConstantInt>(UserI->getOperand(1))->getZExtValue(); |
| 673 | PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, UserI->user_back())); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 676 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 677 | // If we have no users, they must be all self uses, just nuke the PHI. |
| 678 | if (PHIUsers.empty()) |
| 679 | return ReplaceInstUsesWith(FirstPhi, UndefValue::get(FirstPhi.getType())); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 680 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 681 | // If this phi node is transformable, create new PHIs for all the pieces |
| 682 | // extracted out of it. First, sort the users by their offset and size. |
| 683 | array_pod_sort(PHIUsers.begin(), PHIUsers.end()); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 684 | |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 685 | DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n'; |
| 686 | for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i) |
| 687 | dbgs() << "AND USER PHI #" << i << ": " << *PHIsToSlice[i] << '\n'; |
| 688 | ); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 689 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 690 | // PredValues - This is a temporary used when rewriting PHI nodes. It is |
| 691 | // hoisted out here to avoid construction/destruction thrashing. |
| 692 | DenseMap<BasicBlock*, Value*> PredValues; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 693 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 694 | // ExtractedVals - Each new PHI we introduce is saved here so we don't |
| 695 | // introduce redundant PHIs. |
| 696 | DenseMap<LoweredPHIRecord, PHINode*> ExtractedVals; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 697 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 698 | for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) { |
| 699 | unsigned PHIId = PHIUsers[UserI].PHIId; |
| 700 | PHINode *PN = PHIsToSlice[PHIId]; |
| 701 | unsigned Offset = PHIUsers[UserI].Shift; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 702 | Type *Ty = PHIUsers[UserI].Inst->getType(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 703 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 704 | PHINode *EltPHI; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 705 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 706 | // If we've already lowered a user like this, reuse the previously lowered |
| 707 | // value. |
| 708 | if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == 0) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 709 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 710 | // Otherwise, Create the new PHI node for this user. |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 711 | EltPHI = PHINode::Create(Ty, PN->getNumIncomingValues(), |
| 712 | PN->getName()+".off"+Twine(Offset), PN); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 713 | assert(EltPHI->getType() != PN->getType() && |
| 714 | "Truncate didn't shrink phi?"); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 715 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 716 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 717 | BasicBlock *Pred = PN->getIncomingBlock(i); |
| 718 | Value *&PredVal = PredValues[Pred]; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 719 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 720 | // If we already have a value for this predecessor, reuse it. |
| 721 | if (PredVal) { |
| 722 | EltPHI->addIncoming(PredVal, Pred); |
| 723 | continue; |
| 724 | } |
| 725 | |
| 726 | // Handle the PHI self-reuse case. |
| 727 | Value *InVal = PN->getIncomingValue(i); |
| 728 | if (InVal == PN) { |
| 729 | PredVal = EltPHI; |
| 730 | EltPHI->addIncoming(PredVal, Pred); |
| 731 | continue; |
| 732 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 733 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 734 | if (PHINode *InPHI = dyn_cast<PHINode>(PN)) { |
| 735 | // If the incoming value was a PHI, and if it was one of the PHIs we |
| 736 | // already rewrote it, just use the lowered value. |
| 737 | if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) { |
| 738 | PredVal = Res; |
| 739 | EltPHI->addIncoming(PredVal, Pred); |
| 740 | continue; |
| 741 | } |
| 742 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 743 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 744 | // Otherwise, do an extract in the predecessor. |
| 745 | Builder->SetInsertPoint(Pred, Pred->getTerminator()); |
| 746 | Value *Res = InVal; |
| 747 | if (Offset) |
| 748 | Res = Builder->CreateLShr(Res, ConstantInt::get(InVal->getType(), |
| 749 | Offset), "extract"); |
| 750 | Res = Builder->CreateTrunc(Res, Ty, "extract.t"); |
| 751 | PredVal = Res; |
| 752 | EltPHI->addIncoming(Res, Pred); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 753 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 754 | // If the incoming value was a PHI, and if it was one of the PHIs we are |
| 755 | // rewriting, we will ultimately delete the code we inserted. This |
| 756 | // means we need to revisit that PHI to make sure we extract out the |
| 757 | // needed piece. |
| 758 | if (PHINode *OldInVal = dyn_cast<PHINode>(PN->getIncomingValue(i))) |
| 759 | if (PHIsInspected.count(OldInVal)) { |
| 760 | unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(), |
| 761 | OldInVal)-PHIsToSlice.begin(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 762 | PHIUsers.push_back(PHIUsageRecord(RefPHIId, Offset, |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 763 | cast<Instruction>(Res))); |
| 764 | ++UserE; |
| 765 | } |
| 766 | } |
| 767 | PredValues.clear(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 768 | |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 769 | DEBUG(dbgs() << " Made element PHI for offset " << Offset << ": " |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 770 | << *EltPHI << '\n'); |
| 771 | ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI; |
| 772 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 773 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 774 | // Replace the use of this piece with the PHI node. |
| 775 | ReplaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI); |
| 776 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 777 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 778 | // Replace all the remaining uses of the PHI nodes (self uses and the lshrs) |
| 779 | // with undefs. |
| 780 | Value *Undef = UndefValue::get(FirstPhi.getType()); |
| 781 | for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i) |
| 782 | ReplaceInstUsesWith(*PHIsToSlice[i], Undef); |
| 783 | return ReplaceInstUsesWith(FirstPhi, Undef); |
| 784 | } |
| 785 | |
| 786 | // PHINode simplification |
| 787 | // |
| 788 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 789 | if (Value *V = SimplifyInstruction(&PN, DL, TLI)) |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 790 | return ReplaceInstUsesWith(PN, V); |
| 791 | |
| 792 | // If all PHI operands are the same operation, pull them through the PHI, |
| 793 | // reducing code size. |
| 794 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 795 | isa<Instruction>(PN.getIncomingValue(1)) && |
| 796 | cast<Instruction>(PN.getIncomingValue(0))->getOpcode() == |
| 797 | cast<Instruction>(PN.getIncomingValue(1))->getOpcode() && |
| 798 | // FIXME: The hasOneUse check will fail for PHIs that use the value more |
| 799 | // than themselves more than once. |
| 800 | PN.getIncomingValue(0)->hasOneUse()) |
| 801 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 802 | return Result; |
| 803 | |
| 804 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 805 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 806 | // PHI)... break the cycle. |
| 807 | if (PN.hasOneUse()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 808 | Instruction *PHIUser = cast<Instruction>(PN.user_back()); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 809 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
| 810 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
| 811 | PotentiallyDeadPHIs.insert(&PN); |
| 812 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 813 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 814 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 815 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 816 | // If this phi has a single use, and if that use just computes a value for |
| 817 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 818 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 819 | // common case here is good because the only other things that catch this |
| 820 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 821 | // late. |
| 822 | if (PHIUser->hasOneUse() && |
| 823 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame^] | 824 | PHIUser->user_back() == &PN) { |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 825 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 830 | // same value, for example: |
| 831 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 832 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 833 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 834 | // so, scan to see if the phi cycle is actually equal to that value. |
| 835 | { |
Frits van Bommel | d6d4f98 | 2011-04-16 14:32:34 +0000 | [diff] [blame] | 836 | unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues(); |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 837 | // Scan for the first non-phi operand. |
Frits van Bommel | d6d4f98 | 2011-04-16 14:32:34 +0000 | [diff] [blame] | 838 | while (InValNo != NumIncomingVals && |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 839 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 840 | ++InValNo; |
| 841 | |
Frits van Bommel | d6d4f98 | 2011-04-16 14:32:34 +0000 | [diff] [blame] | 842 | if (InValNo != NumIncomingVals) { |
Jay Foad | 7d03e9b | 2011-04-16 14:17:37 +0000 | [diff] [blame] | 843 | Value *NonPhiInVal = PN.getIncomingValue(InValNo); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 844 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 845 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 846 | // there is no need to recursively scan other phis. |
Frits van Bommel | d6d4f98 | 2011-04-16 14:32:34 +0000 | [diff] [blame] | 847 | for (++InValNo; InValNo != NumIncomingVals; ++InValNo) { |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 848 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 849 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 850 | break; |
| 851 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 852 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 853 | // If we scanned over all operands, then we have one unique value plus |
| 854 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 855 | // the value. |
Frits van Bommel | d6d4f98 | 2011-04-16 14:32:34 +0000 | [diff] [blame] | 856 | if (InValNo == NumIncomingVals) { |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 857 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 858 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 859 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // If there are multiple PHIs, sort their operands so that they all list |
| 865 | // the blocks in the same order. This will help identical PHIs be eliminated |
| 866 | // by other passes. Other passes shouldn't depend on this for correctness |
| 867 | // however. |
| 868 | PHINode *FirstPN = cast<PHINode>(PN.getParent()->begin()); |
| 869 | if (&PN != FirstPN) |
| 870 | for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++i) { |
| 871 | BasicBlock *BBA = PN.getIncomingBlock(i); |
| 872 | BasicBlock *BBB = FirstPN->getIncomingBlock(i); |
| 873 | if (BBA != BBB) { |
| 874 | Value *VA = PN.getIncomingValue(i); |
| 875 | unsigned j = PN.getBasicBlockIndex(BBB); |
| 876 | Value *VB = PN.getIncomingValue(j); |
| 877 | PN.setIncomingBlock(i, BBB); |
| 878 | PN.setIncomingValue(i, VB); |
| 879 | PN.setIncomingBlock(j, BBA); |
| 880 | PN.setIncomingValue(j, VA); |
| 881 | // NOTE: Instcombine normally would want us to "return &PN" if we |
| 882 | // modified any of the operands of an instruction. However, since we |
| 883 | // aren't adding or removing uses (just rearranging them) we don't do |
| 884 | // this in this case. |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | // If this is an integer PHI and we know that it has an illegal type, see if |
| 889 | // it is only used by trunc or trunc(lshr) operations. If so, we split the |
| 890 | // PHI into the various pieces being extracted. This sort of thing is |
| 891 | // introduced when SROA promotes an aggregate to a single large integer type. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 892 | if (PN.getType()->isIntegerTy() && DL && |
| 893 | !DL->isLegalInteger(PN.getType()->getPrimitiveSizeInBits())) |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 894 | if (Instruction *Res = SliceUpIllegalIntegerPHI(PN)) |
| 895 | return Res; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 896 | |
Chris Lattner | de1fede | 2010-01-05 05:31:55 +0000 | [diff] [blame] | 897 | return 0; |
Benjamin Kramer | f7cc698 | 2010-01-05 13:32:48 +0000 | [diff] [blame] | 898 | } |