LLVM  mainline
InstCombinePHI.cpp
Go to the documentation of this file.
00001 //===- InstCombinePHI.cpp -------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the visitPHINode function.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "InstCombineInternal.h"
00015 #include "llvm/ADT/STLExtras.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/Analysis/InstructionSimplify.h"
00018 using namespace llvm;
00019 
00020 #define DEBUG_TYPE "instcombine"
00021 
00022 /// If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the
00023 /// adds all have a single use, turn this into a phi and a single binop.
00024 Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) {
00025   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
00026   assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
00027   unsigned Opc = FirstInst->getOpcode();
00028   Value *LHSVal = FirstInst->getOperand(0);
00029   Value *RHSVal = FirstInst->getOperand(1);
00030 
00031   Type *LHSType = LHSVal->getType();
00032   Type *RHSType = RHSVal->getType();
00033 
00034   bool isNUW = false, isNSW = false, isExact = false;
00035   if (OverflowingBinaryOperator *BO =
00036         dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
00037     isNUW = BO->hasNoUnsignedWrap();
00038     isNSW = BO->hasNoSignedWrap();
00039   } else if (PossiblyExactOperator *PEO =
00040                dyn_cast<PossiblyExactOperator>(FirstInst))
00041     isExact = PEO->isExact();
00042 
00043   // Scan to see if all operands are the same opcode, and all have one use.
00044   for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
00045     Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
00046     if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
00047         // Verify type of the LHS matches so we don't fold cmp's of different
00048         // types.
00049         I->getOperand(0)->getType() != LHSType ||
00050         I->getOperand(1)->getType() != RHSType)
00051       return nullptr;
00052 
00053     // If they are CmpInst instructions, check their predicates
00054     if (CmpInst *CI = dyn_cast<CmpInst>(I))
00055       if (CI->getPredicate() != cast<CmpInst>(FirstInst)->getPredicate())
00056         return nullptr;
00057 
00058     if (isNUW)
00059       isNUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
00060     if (isNSW)
00061       isNSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
00062     if (isExact)
00063       isExact = cast<PossiblyExactOperator>(I)->isExact();
00064 
00065     // Keep track of which operand needs a phi node.
00066     if (I->getOperand(0) != LHSVal) LHSVal = nullptr;
00067     if (I->getOperand(1) != RHSVal) RHSVal = nullptr;
00068   }
00069 
00070   // If both LHS and RHS would need a PHI, don't do this transformation,
00071   // because it would increase the number of PHIs entering the block,
00072   // which leads to higher register pressure. This is especially
00073   // bad when the PHIs are in the header of a loop.
00074   if (!LHSVal && !RHSVal)
00075     return nullptr;
00076 
00077   // Otherwise, this is safe to transform!
00078 
00079   Value *InLHS = FirstInst->getOperand(0);
00080   Value *InRHS = FirstInst->getOperand(1);
00081   PHINode *NewLHS = nullptr, *NewRHS = nullptr;
00082   if (!LHSVal) {
00083     NewLHS = PHINode::Create(LHSType, PN.getNumIncomingValues(),
00084                              FirstInst->getOperand(0)->getName() + ".pn");
00085     NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
00086     InsertNewInstBefore(NewLHS, PN);
00087     LHSVal = NewLHS;
00088   }
00089 
00090   if (!RHSVal) {
00091     NewRHS = PHINode::Create(RHSType, PN.getNumIncomingValues(),
00092                              FirstInst->getOperand(1)->getName() + ".pn");
00093     NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
00094     InsertNewInstBefore(NewRHS, PN);
00095     RHSVal = NewRHS;
00096   }
00097 
00098   // Add all operands to the new PHIs.
00099   if (NewLHS || NewRHS) {
00100     for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
00101       Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i));
00102       if (NewLHS) {
00103         Value *NewInLHS = InInst->getOperand(0);
00104         NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
00105       }
00106       if (NewRHS) {
00107         Value *NewInRHS = InInst->getOperand(1);
00108         NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
00109       }
00110     }
00111   }
00112 
00113   if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) {
00114     CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
00115                                      LHSVal, RHSVal);
00116     NewCI->setDebugLoc(FirstInst->getDebugLoc());
00117     return NewCI;
00118   }
00119 
00120   BinaryOperator *BinOp = cast<BinaryOperator>(FirstInst);
00121   BinaryOperator *NewBinOp =
00122     BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
00123   if (isNUW) NewBinOp->setHasNoUnsignedWrap();
00124   if (isNSW) NewBinOp->setHasNoSignedWrap();
00125   if (isExact) NewBinOp->setIsExact();
00126   NewBinOp->setDebugLoc(FirstInst->getDebugLoc());
00127   return NewBinOp;
00128 }
00129 
00130 Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
00131   GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0));
00132 
00133   SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(),
00134                                         FirstInst->op_end());
00135   // This is true if all GEP bases are allocas and if all indices into them are
00136   // constants.
00137   bool AllBasePointersAreAllocas = true;
00138 
00139   // We don't want to replace this phi if the replacement would require
00140   // more than one phi, which leads to higher register pressure. This is
00141   // especially bad when the PHIs are in the header of a loop.
00142   bool NeededPhi = false;
00143 
00144   bool AllInBounds = true;
00145 
00146   // Scan to see if all operands are the same opcode, and all have one use.
00147   for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
00148     GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i));
00149     if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() ||
00150       GEP->getNumOperands() != FirstInst->getNumOperands())
00151       return nullptr;
00152 
00153     AllInBounds &= GEP->isInBounds();
00154 
00155     // Keep track of whether or not all GEPs are of alloca pointers.
00156     if (AllBasePointersAreAllocas &&
00157         (!isa<AllocaInst>(GEP->getOperand(0)) ||
00158          !GEP->hasAllConstantIndices()))
00159       AllBasePointersAreAllocas = false;
00160 
00161     // Compare the operand lists.
00162     for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) {
00163       if (FirstInst->getOperand(op) == GEP->getOperand(op))
00164         continue;
00165 
00166       // Don't merge two GEPs when two operands differ (introducing phi nodes)
00167       // if one of the PHIs has a constant for the index.  The index may be
00168       // substantially cheaper to compute for the constants, so making it a
00169       // variable index could pessimize the path.  This also handles the case
00170       // for struct indices, which must always be constant.
00171       if (isa<ConstantInt>(FirstInst->getOperand(op)) ||
00172           isa<ConstantInt>(GEP->getOperand(op)))
00173         return nullptr;
00174 
00175       if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType())
00176         return nullptr;
00177 
00178       // If we already needed a PHI for an earlier operand, and another operand
00179       // also requires a PHI, we'd be introducing more PHIs than we're
00180       // eliminating, which increases register pressure on entry to the PHI's
00181       // block.
00182       if (NeededPhi)
00183         return nullptr;
00184 
00185       FixedOperands[op] = nullptr;  // Needs a PHI.
00186       NeededPhi = true;
00187     }
00188   }
00189 
00190   // If all of the base pointers of the PHI'd GEPs are from allocas, don't
00191   // bother doing this transformation.  At best, this will just save a bit of
00192   // offset calculation, but all the predecessors will have to materialize the
00193   // stack address into a register anyway.  We'd actually rather *clone* the
00194   // load up into the predecessors so that we have a load of a gep of an alloca,
00195   // which can usually all be folded into the load.
00196   if (AllBasePointersAreAllocas)
00197     return nullptr;
00198 
00199   // Otherwise, this is safe to transform.  Insert PHI nodes for each operand
00200   // that is variable.
00201   SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
00202 
00203   bool HasAnyPHIs = false;
00204   for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) {
00205     if (FixedOperands[i]) continue;  // operand doesn't need a phi.
00206     Value *FirstOp = FirstInst->getOperand(i);
00207     PHINode *NewPN = PHINode::Create(FirstOp->getType(), e,
00208                                      FirstOp->getName()+".pn");
00209     InsertNewInstBefore(NewPN, PN);
00210 
00211     NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0));
00212     OperandPhis[i] = NewPN;
00213     FixedOperands[i] = NewPN;
00214     HasAnyPHIs = true;
00215   }
00216 
00217 
00218   // Add all operands to the new PHIs.
00219   if (HasAnyPHIs) {
00220     for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
00221       GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i));
00222       BasicBlock *InBB = PN.getIncomingBlock(i);
00223 
00224       for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op)
00225         if (PHINode *OpPhi = OperandPhis[op])
00226           OpPhi->addIncoming(InGEP->getOperand(op), InBB);
00227     }
00228   }
00229 
00230   Value *Base = FixedOperands[0];
00231   GetElementPtrInst *NewGEP =
00232       GetElementPtrInst::Create(FirstInst->getSourceElementType(), Base,
00233                                 makeArrayRef(FixedOperands).slice(1));
00234   if (AllInBounds) NewGEP->setIsInBounds();
00235   NewGEP->setDebugLoc(FirstInst->getDebugLoc());
00236   return NewGEP;
00237 }
00238 
00239 
00240 /// Return true if we know that it is safe to sink the load out of the block
00241 /// that defines it. This means that it must be obvious the value of the load is
00242 /// not changed from the point of the load to the end of the block it is in.
00243 ///
00244 /// Finally, it is safe, but not profitable, to sink a load targeting a
00245 /// non-address-taken alloca.  Doing so will cause us to not promote the alloca
00246 /// to a register.
00247 static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
00248   BasicBlock::iterator BBI = L, E = L->getParent()->end();
00249 
00250   for (++BBI; BBI != E; ++BBI)
00251     if (BBI->mayWriteToMemory())
00252       return false;
00253 
00254   // Check for non-address taken alloca.  If not address-taken already, it isn't
00255   // profitable to do this xform.
00256   if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
00257     bool isAddressTaken = false;
00258     for (User *U : AI->users()) {
00259       if (isa<LoadInst>(U)) continue;
00260       if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
00261         // If storing TO the alloca, then the address isn't taken.
00262         if (SI->getOperand(1) == AI) continue;
00263       }
00264       isAddressTaken = true;
00265       break;
00266     }
00267 
00268     if (!isAddressTaken && AI->isStaticAlloca())
00269       return false;
00270   }
00271 
00272   // If this load is a load from a GEP with a constant offset from an alloca,
00273   // then we don't want to sink it.  In its present form, it will be
00274   // load [constant stack offset].  Sinking it will cause us to have to
00275   // materialize the stack addresses in each predecessor in a register only to
00276   // do a shared load from register in the successor.
00277   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0)))
00278     if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0)))
00279       if (AI->isStaticAlloca() && GEP->hasAllConstantIndices())
00280         return false;
00281 
00282   return true;
00283 }
00284 
00285 Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) {
00286   LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0));
00287 
00288   // FIXME: This is overconservative; this transform is allowed in some cases
00289   // for atomic operations.
00290   if (FirstLI->isAtomic())
00291     return nullptr;
00292 
00293   // When processing loads, we need to propagate two bits of information to the
00294   // sunk load: whether it is volatile, and what its alignment is.  We currently
00295   // don't sink loads when some have their alignment specified and some don't.
00296   // visitLoadInst will propagate an alignment onto the load when TD is around,
00297   // and if TD isn't around, we can't handle the mixed case.
00298   bool isVolatile = FirstLI->isVolatile();
00299   unsigned LoadAlignment = FirstLI->getAlignment();
00300   unsigned LoadAddrSpace = FirstLI->getPointerAddressSpace();
00301 
00302   // We can't sink the load if the loaded value could be modified between the
00303   // load and the PHI.
00304   if (FirstLI->getParent() != PN.getIncomingBlock(0) ||
00305       !isSafeAndProfitableToSinkLoad(FirstLI))
00306     return nullptr;
00307 
00308   // If the PHI is of volatile loads and the load block has multiple
00309   // successors, sinking it would remove a load of the volatile value from
00310   // the path through the other successor.
00311   if (isVolatile &&
00312       FirstLI->getParent()->getTerminator()->getNumSuccessors() != 1)
00313     return nullptr;
00314 
00315   // Check to see if all arguments are the same operation.
00316   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
00317     LoadInst *LI = dyn_cast<LoadInst>(PN.getIncomingValue(i));
00318     if (!LI || !LI->hasOneUse())
00319       return nullptr;
00320 
00321     // We can't sink the load if the loaded value could be modified between
00322     // the load and the PHI.
00323     if (LI->isVolatile() != isVolatile ||
00324         LI->getParent() != PN.getIncomingBlock(i) ||
00325         LI->getPointerAddressSpace() != LoadAddrSpace ||
00326         !isSafeAndProfitableToSinkLoad(LI))
00327       return nullptr;
00328 
00329     // If some of the loads have an alignment specified but not all of them,
00330     // we can't do the transformation.
00331     if ((LoadAlignment != 0) != (LI->getAlignment() != 0))
00332       return nullptr;
00333 
00334     LoadAlignment = std::min(LoadAlignment, LI->getAlignment());
00335 
00336     // If the PHI is of volatile loads and the load block has multiple
00337     // successors, sinking it would remove a load of the volatile value from
00338     // the path through the other successor.
00339     if (isVolatile &&
00340         LI->getParent()->getTerminator()->getNumSuccessors() != 1)
00341       return nullptr;
00342   }
00343 
00344   // Okay, they are all the same operation.  Create a new PHI node of the
00345   // correct type, and PHI together all of the LHS's of the instructions.
00346   PHINode *NewPN = PHINode::Create(FirstLI->getOperand(0)->getType(),
00347                                    PN.getNumIncomingValues(),
00348                                    PN.getName()+".in");
00349 
00350   Value *InVal = FirstLI->getOperand(0);
00351   NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
00352 
00353   // Add all operands to the new PHI.
00354   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
00355     Value *NewInVal = cast<LoadInst>(PN.getIncomingValue(i))->getOperand(0);
00356     if (NewInVal != InVal)
00357       InVal = nullptr;
00358     NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
00359   }
00360 
00361   Value *PhiVal;
00362   if (InVal) {
00363     // The new PHI unions all of the same values together.  This is really
00364     // common, so we handle it intelligently here for compile-time speed.
00365     PhiVal = InVal;
00366     delete NewPN;
00367   } else {
00368     InsertNewInstBefore(NewPN, PN);
00369     PhiVal = NewPN;
00370   }
00371 
00372   // If this was a volatile load that we are merging, make sure to loop through
00373   // and mark all the input loads as non-volatile.  If we don't do this, we will
00374   // insert a new volatile load and the old ones will not be deletable.
00375   if (isVolatile)
00376     for (Value *IncValue : PN.incoming_values())
00377       cast<LoadInst>(IncValue)->setVolatile(false);
00378 
00379   LoadInst *NewLI = new LoadInst(PhiVal, "", isVolatile, LoadAlignment);
00380   NewLI->setDebugLoc(FirstLI->getDebugLoc());
00381   return NewLI;
00382 }
00383 
00384 
00385 
00386 /// If all operands to a PHI node are the same "unary" operator and they all are
00387 /// only used by the PHI, PHI together their inputs, and do the operation once,
00388 /// to the result of the PHI.
00389 Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
00390   Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
00391 
00392   if (isa<GetElementPtrInst>(FirstInst))
00393     return FoldPHIArgGEPIntoPHI(PN);
00394   if (isa<LoadInst>(FirstInst))
00395     return FoldPHIArgLoadIntoPHI(PN);
00396 
00397   // Scan the instruction, looking for input operations that can be folded away.
00398   // If all input operands to the phi are the same instruction (e.g. a cast from
00399   // the same type or "+42") we can pull the operation through the PHI, reducing
00400   // code size and simplifying code.
00401   Constant *ConstantOp = nullptr;
00402   Type *CastSrcTy = nullptr;
00403   bool isNUW = false, isNSW = false, isExact = false;
00404 
00405   if (isa<CastInst>(FirstInst)) {
00406     CastSrcTy = FirstInst->getOperand(0)->getType();
00407 
00408     // Be careful about transforming integer PHIs.  We don't want to pessimize
00409     // the code by turning an i32 into an i1293.
00410     if (PN.getType()->isIntegerTy() && CastSrcTy->isIntegerTy()) {
00411       if (!ShouldChangeType(PN.getType(), CastSrcTy))
00412         return nullptr;
00413     }
00414   } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
00415     // Can fold binop, compare or shift here if the RHS is a constant,
00416     // otherwise call FoldPHIArgBinOpIntoPHI.
00417     ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
00418     if (!ConstantOp)
00419       return FoldPHIArgBinOpIntoPHI(PN);
00420 
00421     if (OverflowingBinaryOperator *BO =
00422         dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
00423       isNUW = BO->hasNoUnsignedWrap();
00424       isNSW = BO->hasNoSignedWrap();
00425     } else if (PossiblyExactOperator *PEO =
00426                dyn_cast<PossiblyExactOperator>(FirstInst))
00427       isExact = PEO->isExact();
00428   } else {
00429     return nullptr;  // Cannot fold this operation.
00430   }
00431 
00432   // Check to see if all arguments are the same operation.
00433   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
00434     Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
00435     if (!I || !I->hasOneUse() || !I->isSameOperationAs(FirstInst))
00436       return nullptr;
00437     if (CastSrcTy) {
00438       if (I->getOperand(0)->getType() != CastSrcTy)
00439         return nullptr;  // Cast operation must match.
00440     } else if (I->getOperand(1) != ConstantOp) {
00441       return nullptr;
00442     }
00443 
00444     if (isNUW)
00445       isNUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
00446     if (isNSW)
00447       isNSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
00448     if (isExact)
00449       isExact = cast<PossiblyExactOperator>(I)->isExact();
00450   }
00451 
00452   // Okay, they are all the same operation.  Create a new PHI node of the
00453   // correct type, and PHI together all of the LHS's of the instructions.
00454   PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
00455                                    PN.getNumIncomingValues(),
00456                                    PN.getName()+".in");
00457 
00458   Value *InVal = FirstInst->getOperand(0);
00459   NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
00460 
00461   // Add all operands to the new PHI.
00462   for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
00463     Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
00464     if (NewInVal != InVal)
00465       InVal = nullptr;
00466     NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
00467   }
00468 
00469   Value *PhiVal;
00470   if (InVal) {
00471     // The new PHI unions all of the same values together.  This is really
00472     // common, so we handle it intelligently here for compile-time speed.
00473     PhiVal = InVal;
00474     delete NewPN;
00475   } else {
00476     InsertNewInstBefore(NewPN, PN);
00477     PhiVal = NewPN;
00478   }
00479 
00480   // Insert and return the new operation.
00481   if (CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
00482     CastInst *NewCI = CastInst::Create(FirstCI->getOpcode(), PhiVal,
00483                                        PN.getType());
00484     NewCI->setDebugLoc(FirstInst->getDebugLoc());
00485     return NewCI;
00486   }
00487 
00488   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
00489     BinOp = BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
00490     if (isNUW) BinOp->setHasNoUnsignedWrap();
00491     if (isNSW) BinOp->setHasNoSignedWrap();
00492     if (isExact) BinOp->setIsExact();
00493     BinOp->setDebugLoc(FirstInst->getDebugLoc());
00494     return BinOp;
00495   }
00496 
00497   CmpInst *CIOp = cast<CmpInst>(FirstInst);
00498   CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
00499                                    PhiVal, ConstantOp);
00500   NewCI->setDebugLoc(FirstInst->getDebugLoc());
00501   return NewCI;
00502 }
00503 
00504 /// Return true if this PHI node is only used by a PHI node cycle that is dead.
00505 static bool DeadPHICycle(PHINode *PN,
00506                          SmallPtrSetImpl<PHINode*> &PotentiallyDeadPHIs) {
00507   if (PN->use_empty()) return true;
00508   if (!PN->hasOneUse()) return false;
00509 
00510   // Remember this node, and if we find the cycle, return.
00511   if (!PotentiallyDeadPHIs.insert(PN).second)
00512     return true;
00513 
00514   // Don't scan crazily complex things.
00515   if (PotentiallyDeadPHIs.size() == 16)
00516     return false;
00517 
00518   if (PHINode *PU = dyn_cast<PHINode>(PN->user_back()))
00519     return DeadPHICycle(PU, PotentiallyDeadPHIs);
00520 
00521   return false;
00522 }
00523 
00524 /// Return true if this phi node is always equal to NonPhiInVal.
00525 /// This happens with mutually cyclic phi nodes like:
00526 ///   z = some value; x = phi (y, z); y = phi (x, z)
00527 static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
00528                            SmallPtrSetImpl<PHINode*> &ValueEqualPHIs) {
00529   // See if we already saw this PHI node.
00530   if (!ValueEqualPHIs.insert(PN).second)
00531     return true;
00532 
00533   // Don't scan crazily complex things.
00534   if (ValueEqualPHIs.size() == 16)
00535     return false;
00536 
00537   // Scan the operands to see if they are either phi nodes or are equal to
00538   // the value.
00539   for (Value *Op : PN->incoming_values()) {
00540     if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
00541       if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
00542         return false;
00543     } else if (Op != NonPhiInVal)
00544       return false;
00545   }
00546 
00547   return true;
00548 }
00549 
00550 
00551 namespace {
00552 struct PHIUsageRecord {
00553   unsigned PHIId;     // The ID # of the PHI (something determinstic to sort on)
00554   unsigned Shift;     // The amount shifted.
00555   Instruction *Inst;  // The trunc instruction.
00556 
00557   PHIUsageRecord(unsigned pn, unsigned Sh, Instruction *User)
00558     : PHIId(pn), Shift(Sh), Inst(User) {}
00559 
00560   bool operator<(const PHIUsageRecord &RHS) const {
00561     if (PHIId < RHS.PHIId) return true;
00562     if (PHIId > RHS.PHIId) return false;
00563     if (Shift < RHS.Shift) return true;
00564     if (Shift > RHS.Shift) return false;
00565     return Inst->getType()->getPrimitiveSizeInBits() <
00566            RHS.Inst->getType()->getPrimitiveSizeInBits();
00567   }
00568 };
00569 
00570 struct LoweredPHIRecord {
00571   PHINode *PN;        // The PHI that was lowered.
00572   unsigned Shift;     // The amount shifted.
00573   unsigned Width;     // The width extracted.
00574 
00575   LoweredPHIRecord(PHINode *pn, unsigned Sh, Type *Ty)
00576     : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
00577 
00578   // Ctor form used by DenseMap.
00579   LoweredPHIRecord(PHINode *pn, unsigned Sh)
00580     : PN(pn), Shift(Sh), Width(0) {}
00581 };
00582 }
00583 
00584 namespace llvm {
00585   template<>
00586   struct DenseMapInfo<LoweredPHIRecord> {
00587     static inline LoweredPHIRecord getEmptyKey() {
00588       return LoweredPHIRecord(nullptr, 0);
00589     }
00590     static inline LoweredPHIRecord getTombstoneKey() {
00591       return LoweredPHIRecord(nullptr, 1);
00592     }
00593     static unsigned getHashValue(const LoweredPHIRecord &Val) {
00594       return DenseMapInfo<PHINode*>::getHashValue(Val.PN) ^ (Val.Shift>>3) ^
00595              (Val.Width>>3);
00596     }
00597     static bool isEqual(const LoweredPHIRecord &LHS,
00598                         const LoweredPHIRecord &RHS) {
00599       return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift &&
00600              LHS.Width == RHS.Width;
00601     }
00602   };
00603 }
00604 
00605 
00606 /// This is an integer PHI and we know that it has an illegal type: see if it is
00607 /// only used by trunc or trunc(lshr) operations. If so, we split the PHI into
00608 /// the various pieces being extracted. This sort of thing is introduced when
00609 /// SROA promotes an aggregate to large integer values.
00610 ///
00611 /// TODO: The user of the trunc may be an bitcast to float/double/vector or an
00612 /// inttoptr.  We should produce new PHIs in the right type.
00613 ///
00614 Instruction *InstCombiner::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
00615   // PHIUsers - Keep track of all of the truncated values extracted from a set
00616   // of PHIs, along with their offset.  These are the things we want to rewrite.
00617   SmallVector<PHIUsageRecord, 16> PHIUsers;
00618 
00619   // PHIs are often mutually cyclic, so we keep track of a whole set of PHI
00620   // nodes which are extracted from. PHIsToSlice is a set we use to avoid
00621   // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to
00622   // check the uses of (to ensure they are all extracts).
00623   SmallVector<PHINode*, 8> PHIsToSlice;
00624   SmallPtrSet<PHINode*, 8> PHIsInspected;
00625 
00626   PHIsToSlice.push_back(&FirstPhi);
00627   PHIsInspected.insert(&FirstPhi);
00628 
00629   for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) {
00630     PHINode *PN = PHIsToSlice[PHIId];
00631 
00632     // Scan the input list of the PHI.  If any input is an invoke, and if the
00633     // input is defined in the predecessor, then we won't be split the critical
00634     // edge which is required to insert a truncate.  Because of this, we have to
00635     // bail out.
00636     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00637       InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i));
00638       if (!II) continue;
00639       if (II->getParent() != PN->getIncomingBlock(i))
00640         continue;
00641 
00642       // If we have a phi, and if it's directly in the predecessor, then we have
00643       // a critical edge where we need to put the truncate.  Since we can't
00644       // split the edge in instcombine, we have to bail out.
00645       return nullptr;
00646     }
00647 
00648     for (User *U : PN->users()) {
00649       Instruction *UserI = cast<Instruction>(U);
00650 
00651       // If the user is a PHI, inspect its uses recursively.
00652       if (PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
00653         if (PHIsInspected.insert(UserPN).second)
00654           PHIsToSlice.push_back(UserPN);
00655         continue;
00656       }
00657 
00658       // Truncates are always ok.
00659       if (isa<TruncInst>(UserI)) {
00660         PHIUsers.push_back(PHIUsageRecord(PHIId, 0, UserI));
00661         continue;
00662       }
00663 
00664       // Otherwise it must be a lshr which can only be used by one trunc.
00665       if (UserI->getOpcode() != Instruction::LShr ||
00666           !UserI->hasOneUse() || !isa<TruncInst>(UserI->user_back()) ||
00667           !isa<ConstantInt>(UserI->getOperand(1)))
00668         return nullptr;
00669 
00670       unsigned Shift = cast<ConstantInt>(UserI->getOperand(1))->getZExtValue();
00671       PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, UserI->user_back()));
00672     }
00673   }
00674 
00675   // If we have no users, they must be all self uses, just nuke the PHI.
00676   if (PHIUsers.empty())
00677     return ReplaceInstUsesWith(FirstPhi, UndefValue::get(FirstPhi.getType()));
00678 
00679   // If this phi node is transformable, create new PHIs for all the pieces
00680   // extracted out of it.  First, sort the users by their offset and size.
00681   array_pod_sort(PHIUsers.begin(), PHIUsers.end());
00682 
00683   DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n';
00684         for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
00685           dbgs() << "AND USER PHI #" << i << ": " << *PHIsToSlice[i] << '\n';
00686     );
00687 
00688   // PredValues - This is a temporary used when rewriting PHI nodes.  It is
00689   // hoisted out here to avoid construction/destruction thrashing.
00690   DenseMap<BasicBlock*, Value*> PredValues;
00691 
00692   // ExtractedVals - Each new PHI we introduce is saved here so we don't
00693   // introduce redundant PHIs.
00694   DenseMap<LoweredPHIRecord, PHINode*> ExtractedVals;
00695 
00696   for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) {
00697     unsigned PHIId = PHIUsers[UserI].PHIId;
00698     PHINode *PN = PHIsToSlice[PHIId];
00699     unsigned Offset = PHIUsers[UserI].Shift;
00700     Type *Ty = PHIUsers[UserI].Inst->getType();
00701 
00702     PHINode *EltPHI;
00703 
00704     // If we've already lowered a user like this, reuse the previously lowered
00705     // value.
00706     if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == nullptr) {
00707 
00708       // Otherwise, Create the new PHI node for this user.
00709       EltPHI = PHINode::Create(Ty, PN->getNumIncomingValues(),
00710                                PN->getName()+".off"+Twine(Offset), PN);
00711       assert(EltPHI->getType() != PN->getType() &&
00712              "Truncate didn't shrink phi?");
00713 
00714       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00715         BasicBlock *Pred = PN->getIncomingBlock(i);
00716         Value *&PredVal = PredValues[Pred];
00717 
00718         // If we already have a value for this predecessor, reuse it.
00719         if (PredVal) {
00720           EltPHI->addIncoming(PredVal, Pred);
00721           continue;
00722         }
00723 
00724         // Handle the PHI self-reuse case.
00725         Value *InVal = PN->getIncomingValue(i);
00726         if (InVal == PN) {
00727           PredVal = EltPHI;
00728           EltPHI->addIncoming(PredVal, Pred);
00729           continue;
00730         }
00731 
00732         if (PHINode *InPHI = dyn_cast<PHINode>(PN)) {
00733           // If the incoming value was a PHI, and if it was one of the PHIs we
00734           // already rewrote it, just use the lowered value.
00735           if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
00736             PredVal = Res;
00737             EltPHI->addIncoming(PredVal, Pred);
00738             continue;
00739           }
00740         }
00741 
00742         // Otherwise, do an extract in the predecessor.
00743         Builder->SetInsertPoint(Pred, Pred->getTerminator());
00744         Value *Res = InVal;
00745         if (Offset)
00746           Res = Builder->CreateLShr(Res, ConstantInt::get(InVal->getType(),
00747                                                           Offset), "extract");
00748         Res = Builder->CreateTrunc(Res, Ty, "extract.t");
00749         PredVal = Res;
00750         EltPHI->addIncoming(Res, Pred);
00751 
00752         // If the incoming value was a PHI, and if it was one of the PHIs we are
00753         // rewriting, we will ultimately delete the code we inserted.  This
00754         // means we need to revisit that PHI to make sure we extract out the
00755         // needed piece.
00756         if (PHINode *OldInVal = dyn_cast<PHINode>(PN->getIncomingValue(i)))
00757           if (PHIsInspected.count(OldInVal)) {
00758             unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(),
00759                                           OldInVal)-PHIsToSlice.begin();
00760             PHIUsers.push_back(PHIUsageRecord(RefPHIId, Offset,
00761                                               cast<Instruction>(Res)));
00762             ++UserE;
00763           }
00764       }
00765       PredValues.clear();
00766 
00767       DEBUG(dbgs() << "  Made element PHI for offset " << Offset << ": "
00768                    << *EltPHI << '\n');
00769       ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
00770     }
00771 
00772     // Replace the use of this piece with the PHI node.
00773     ReplaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI);
00774   }
00775 
00776   // Replace all the remaining uses of the PHI nodes (self uses and the lshrs)
00777   // with undefs.
00778   Value *Undef = UndefValue::get(FirstPhi.getType());
00779   for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
00780     ReplaceInstUsesWith(*PHIsToSlice[i], Undef);
00781   return ReplaceInstUsesWith(FirstPhi, Undef);
00782 }
00783 
00784 // PHINode simplification
00785 //
00786 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
00787   if (Value *V = SimplifyInstruction(&PN, DL, TLI, DT, AC))
00788     return ReplaceInstUsesWith(PN, V);
00789 
00790   // If all PHI operands are the same operation, pull them through the PHI,
00791   // reducing code size.
00792   if (isa<Instruction>(PN.getIncomingValue(0)) &&
00793       isa<Instruction>(PN.getIncomingValue(1)) &&
00794       cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
00795       cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
00796       // FIXME: The hasOneUse check will fail for PHIs that use the value more
00797       // than themselves more than once.
00798       PN.getIncomingValue(0)->hasOneUse())
00799     if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
00800       return Result;
00801 
00802   // If this is a trivial cycle in the PHI node graph, remove it.  Basically, if
00803   // this PHI only has a single use (a PHI), and if that PHI only has one use (a
00804   // PHI)... break the cycle.
00805   if (PN.hasOneUse()) {
00806     Instruction *PHIUser = cast<Instruction>(PN.user_back());
00807     if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
00808       SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
00809       PotentiallyDeadPHIs.insert(&PN);
00810       if (DeadPHICycle(PU, PotentiallyDeadPHIs))
00811         return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
00812     }
00813 
00814     // If this phi has a single use, and if that use just computes a value for
00815     // the next iteration of a loop, delete the phi.  This occurs with unused
00816     // induction variables, e.g. "for (int j = 0; ; ++j);".  Detecting this
00817     // common case here is good because the only other things that catch this
00818     // are induction variable analysis (sometimes) and ADCE, which is only run
00819     // late.
00820     if (PHIUser->hasOneUse() &&
00821         (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
00822         PHIUser->user_back() == &PN) {
00823       return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
00824     }
00825   }
00826 
00827   // We sometimes end up with phi cycles that non-obviously end up being the
00828   // same value, for example:
00829   //   z = some value; x = phi (y, z); y = phi (x, z)
00830   // where the phi nodes don't necessarily need to be in the same block.  Do a
00831   // quick check to see if the PHI node only contains a single non-phi value, if
00832   // so, scan to see if the phi cycle is actually equal to that value.
00833   {
00834     unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues();
00835     // Scan for the first non-phi operand.
00836     while (InValNo != NumIncomingVals &&
00837            isa<PHINode>(PN.getIncomingValue(InValNo)))
00838       ++InValNo;
00839 
00840     if (InValNo != NumIncomingVals) {
00841       Value *NonPhiInVal = PN.getIncomingValue(InValNo);
00842 
00843       // Scan the rest of the operands to see if there are any conflicts, if so
00844       // there is no need to recursively scan other phis.
00845       for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
00846         Value *OpVal = PN.getIncomingValue(InValNo);
00847         if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
00848           break;
00849       }
00850 
00851       // If we scanned over all operands, then we have one unique value plus
00852       // phi values.  Scan PHI nodes to see if they all merge in each other or
00853       // the value.
00854       if (InValNo == NumIncomingVals) {
00855         SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
00856         if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
00857           return ReplaceInstUsesWith(PN, NonPhiInVal);
00858       }
00859     }
00860   }
00861 
00862   // If there are multiple PHIs, sort their operands so that they all list
00863   // the blocks in the same order. This will help identical PHIs be eliminated
00864   // by other passes. Other passes shouldn't depend on this for correctness
00865   // however.
00866   PHINode *FirstPN = cast<PHINode>(PN.getParent()->begin());
00867   if (&PN != FirstPN)
00868     for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++i) {
00869       BasicBlock *BBA = PN.getIncomingBlock(i);
00870       BasicBlock *BBB = FirstPN->getIncomingBlock(i);
00871       if (BBA != BBB) {
00872         Value *VA = PN.getIncomingValue(i);
00873         unsigned j = PN.getBasicBlockIndex(BBB);
00874         Value *VB = PN.getIncomingValue(j);
00875         PN.setIncomingBlock(i, BBB);
00876         PN.setIncomingValue(i, VB);
00877         PN.setIncomingBlock(j, BBA);
00878         PN.setIncomingValue(j, VA);
00879         // NOTE: Instcombine normally would want us to "return &PN" if we
00880         // modified any of the operands of an instruction.  However, since we
00881         // aren't adding or removing uses (just rearranging them) we don't do
00882         // this in this case.
00883       }
00884     }
00885 
00886   // If this is an integer PHI and we know that it has an illegal type, see if
00887   // it is only used by trunc or trunc(lshr) operations.  If so, we split the
00888   // PHI into the various pieces being extracted.  This sort of thing is
00889   // introduced when SROA promotes an aggregate to a single large integer type.
00890   if (PN.getType()->isIntegerTy() &&
00891       !DL.isLegalInteger(PN.getType()->getPrimitiveSizeInBits()))
00892     if (Instruction *Res = SliceUpIllegalIntegerPHI(PN))
00893       return Res;
00894 
00895   return nullptr;
00896 }