LLVM API Documentation

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 "InstCombine.h"
00015 #include "llvm/ADT/STLExtras.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/Analysis/InstructionSimplify.h"
00018 #include "llvm/IR/DataLayout.h"
00019 using namespace llvm;
00020 
00021 /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(a,c)]
00022 /// and if a/b/c and the add's all have a single use, turn this into a phi
00023 /// 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 0;
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 0;
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 = 0;
00067     if (I->getOperand(1) != RHSVal) RHSVal = 0;
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 0;
00076 
00077   // Otherwise, this is safe to transform!
00078 
00079   Value *InLHS = FirstInst->getOperand(0);
00080   Value *InRHS = FirstInst->getOperand(1);
00081   PHINode *NewLHS = 0, *NewRHS = 0;
00082   if (LHSVal == 0) {
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 == 0) {
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 0;
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 0;
00174 
00175       if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType())
00176         return 0;
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 0;
00184 
00185       FixedOperands[op] = 0;  // 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 0;
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(Base, makeArrayRef(FixedOperands).slice(1));
00233   if (AllInBounds) NewGEP->setIsInBounds();
00234   NewGEP->setDebugLoc(FirstInst->getDebugLoc());
00235   return NewGEP;
00236 }
00237 
00238 
00239 /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to
00240 /// sink the load out of the block that defines it.  This means that it must be
00241 /// obvious the value of the load is not changed from the point of the load to
00242 /// 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 0;
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 0;
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 0;
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 0;
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 0;
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 0;
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 0;
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 = 0;
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 (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
00377       cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false);
00378 
00379   LoadInst *NewLI = new LoadInst(PhiVal, "", isVolatile, LoadAlignment);
00380   NewLI->setDebugLoc(FirstLI->getDebugLoc());
00381   return NewLI;
00382 }
00383 
00384 
00385 
00386 /// FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
00387 /// operator and they all are only used by the PHI, PHI together their
00388 /// inputs, and do the operation once, 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 = 0;
00402   Type *CastSrcTy = 0;
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 0;
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 == 0)
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 0;  // 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 == 0 || !I->hasOneUse() || !I->isSameOperationAs(FirstInst))
00436       return 0;
00437     if (CastSrcTy) {
00438       if (I->getOperand(0)->getType() != CastSrcTy)
00439         return 0;  // Cast operation must match.
00440     } else if (I->getOperand(1) != ConstantOp) {
00441       return 0;
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 = 0;
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 /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle
00505 /// that is dead.
00506 static bool DeadPHICycle(PHINode *PN,
00507                          SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) {
00508   if (PN->use_empty()) return true;
00509   if (!PN->hasOneUse()) return false;
00510 
00511   // Remember this node, and if we find the cycle, return.
00512   if (!PotentiallyDeadPHIs.insert(PN))
00513     return true;
00514 
00515   // Don't scan crazily complex things.
00516   if (PotentiallyDeadPHIs.size() == 16)
00517     return false;
00518 
00519   if (PHINode *PU = dyn_cast<PHINode>(PN->user_back()))
00520     return DeadPHICycle(PU, PotentiallyDeadPHIs);
00521 
00522   return false;
00523 }
00524 
00525 /// PHIsEqualValue - Return true if this phi node is always equal to
00526 /// NonPhiInVal.  This happens with mutually cyclic phi nodes like:
00527 ///   z = some value; x = phi (y, z); y = phi (x, z)
00528 static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
00529                            SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
00530   // See if we already saw this PHI node.
00531   if (!ValueEqualPHIs.insert(PN))
00532     return true;
00533 
00534   // Don't scan crazily complex things.
00535   if (ValueEqualPHIs.size() == 16)
00536     return false;
00537 
00538   // Scan the operands to see if they are either phi nodes or are equal to
00539   // the value.
00540   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00541     Value *Op = PN->getIncomingValue(i);
00542     if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
00543       if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
00544         return false;
00545     } else if (Op != NonPhiInVal)
00546       return false;
00547   }
00548 
00549   return true;
00550 }
00551 
00552 
00553 namespace {
00554 struct PHIUsageRecord {
00555   unsigned PHIId;     // The ID # of the PHI (something determinstic to sort on)
00556   unsigned Shift;     // The amount shifted.
00557   Instruction *Inst;  // The trunc instruction.
00558 
00559   PHIUsageRecord(unsigned pn, unsigned Sh, Instruction *User)
00560     : PHIId(pn), Shift(Sh), Inst(User) {}
00561 
00562   bool operator<(const PHIUsageRecord &RHS) const {
00563     if (PHIId < RHS.PHIId) return true;
00564     if (PHIId > RHS.PHIId) return false;
00565     if (Shift < RHS.Shift) return true;
00566     if (Shift > RHS.Shift) return false;
00567     return Inst->getType()->getPrimitiveSizeInBits() <
00568            RHS.Inst->getType()->getPrimitiveSizeInBits();
00569   }
00570 };
00571 
00572 struct LoweredPHIRecord {
00573   PHINode *PN;        // The PHI that was lowered.
00574   unsigned Shift;     // The amount shifted.
00575   unsigned Width;     // The width extracted.
00576 
00577   LoweredPHIRecord(PHINode *pn, unsigned Sh, Type *Ty)
00578     : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
00579 
00580   // Ctor form used by DenseMap.
00581   LoweredPHIRecord(PHINode *pn, unsigned Sh)
00582     : PN(pn), Shift(Sh), Width(0) {}
00583 };
00584 }
00585 
00586 namespace llvm {
00587   template<>
00588   struct DenseMapInfo<LoweredPHIRecord> {
00589     static inline LoweredPHIRecord getEmptyKey() {
00590       return LoweredPHIRecord(0, 0);
00591     }
00592     static inline LoweredPHIRecord getTombstoneKey() {
00593       return LoweredPHIRecord(0, 1);
00594     }
00595     static unsigned getHashValue(const LoweredPHIRecord &Val) {
00596       return DenseMapInfo<PHINode*>::getHashValue(Val.PN) ^ (Val.Shift>>3) ^
00597              (Val.Width>>3);
00598     }
00599     static bool isEqual(const LoweredPHIRecord &LHS,
00600                         const LoweredPHIRecord &RHS) {
00601       return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift &&
00602              LHS.Width == RHS.Width;
00603     }
00604   };
00605 }
00606 
00607 
00608 /// SliceUpIllegalIntegerPHI - This is an integer PHI and we know that it has an
00609 /// illegal type: see if it is only used by trunc or trunc(lshr) operations.  If
00610 /// so, we split the PHI into the various pieces being extracted.  This sort of
00611 /// thing is introduced when SROA promotes an aggregate to large integer values.
00612 ///
00613 /// TODO: The user of the trunc may be an bitcast to float/double/vector or an
00614 /// inttoptr.  We should produce new PHIs in the right type.
00615 ///
00616 Instruction *InstCombiner::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
00617   // PHIUsers - Keep track of all of the truncated values extracted from a set
00618   // of PHIs, along with their offset.  These are the things we want to rewrite.
00619   SmallVector<PHIUsageRecord, 16> PHIUsers;
00620 
00621   // PHIs are often mutually cyclic, so we keep track of a whole set of PHI
00622   // nodes which are extracted from. PHIsToSlice is a set we use to avoid
00623   // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to
00624   // check the uses of (to ensure they are all extracts).
00625   SmallVector<PHINode*, 8> PHIsToSlice;
00626   SmallPtrSet<PHINode*, 8> PHIsInspected;
00627 
00628   PHIsToSlice.push_back(&FirstPhi);
00629   PHIsInspected.insert(&FirstPhi);
00630 
00631   for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) {
00632     PHINode *PN = PHIsToSlice[PHIId];
00633 
00634     // Scan the input list of the PHI.  If any input is an invoke, and if the
00635     // input is defined in the predecessor, then we won't be split the critical
00636     // edge which is required to insert a truncate.  Because of this, we have to
00637     // bail out.
00638     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00639       InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i));
00640       if (II == 0) continue;
00641       if (II->getParent() != PN->getIncomingBlock(i))
00642         continue;
00643 
00644       // If we have a phi, and if it's directly in the predecessor, then we have
00645       // a critical edge where we need to put the truncate.  Since we can't
00646       // split the edge in instcombine, we have to bail out.
00647       return 0;
00648     }
00649 
00650     for (User *U : PN->users()) {
00651       Instruction *UserI = cast<Instruction>(U);
00652 
00653       // If the user is a PHI, inspect its uses recursively.
00654       if (PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
00655         if (PHIsInspected.insert(UserPN))
00656           PHIsToSlice.push_back(UserPN);
00657         continue;
00658       }
00659 
00660       // Truncates are always ok.
00661       if (isa<TruncInst>(UserI)) {
00662         PHIUsers.push_back(PHIUsageRecord(PHIId, 0, UserI));
00663         continue;
00664       }
00665 
00666       // Otherwise it must be a lshr which can only be used by one trunc.
00667       if (UserI->getOpcode() != Instruction::LShr ||
00668           !UserI->hasOneUse() || !isa<TruncInst>(UserI->user_back()) ||
00669           !isa<ConstantInt>(UserI->getOperand(1)))
00670         return 0;
00671 
00672       unsigned Shift = cast<ConstantInt>(UserI->getOperand(1))->getZExtValue();
00673       PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, UserI->user_back()));
00674     }
00675   }
00676 
00677   // If we have no users, they must be all self uses, just nuke the PHI.
00678   if (PHIUsers.empty())
00679     return ReplaceInstUsesWith(FirstPhi, UndefValue::get(FirstPhi.getType()));
00680 
00681   // If this phi node is transformable, create new PHIs for all the pieces
00682   // extracted out of it.  First, sort the users by their offset and size.
00683   array_pod_sort(PHIUsers.begin(), PHIUsers.end());
00684 
00685   DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n';
00686         for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
00687           dbgs() << "AND USER PHI #" << i << ": " << *PHIsToSlice[i] << '\n';
00688     );
00689 
00690   // PredValues - This is a temporary used when rewriting PHI nodes.  It is
00691   // hoisted out here to avoid construction/destruction thrashing.
00692   DenseMap<BasicBlock*, Value*> PredValues;
00693 
00694   // ExtractedVals - Each new PHI we introduce is saved here so we don't
00695   // introduce redundant PHIs.
00696   DenseMap<LoweredPHIRecord, PHINode*> ExtractedVals;
00697 
00698   for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) {
00699     unsigned PHIId = PHIUsers[UserI].PHIId;
00700     PHINode *PN = PHIsToSlice[PHIId];
00701     unsigned Offset = PHIUsers[UserI].Shift;
00702     Type *Ty = PHIUsers[UserI].Inst->getType();
00703 
00704     PHINode *EltPHI;
00705 
00706     // If we've already lowered a user like this, reuse the previously lowered
00707     // value.
00708     if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == 0) {
00709 
00710       // Otherwise, Create the new PHI node for this user.
00711       EltPHI = PHINode::Create(Ty, PN->getNumIncomingValues(),
00712                                PN->getName()+".off"+Twine(Offset), PN);
00713       assert(EltPHI->getType() != PN->getType() &&
00714              "Truncate didn't shrink phi?");
00715 
00716       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00717         BasicBlock *Pred = PN->getIncomingBlock(i);
00718         Value *&PredVal = PredValues[Pred];
00719 
00720         // If we already have a value for this predecessor, reuse it.
00721         if (PredVal) {
00722           EltPHI->addIncoming(PredVal, Pred);
00723           continue;
00724         }
00725 
00726         // Handle the PHI self-reuse case.
00727         Value *InVal = PN->getIncomingValue(i);
00728         if (InVal == PN) {
00729           PredVal = EltPHI;
00730           EltPHI->addIncoming(PredVal, Pred);
00731           continue;
00732         }
00733 
00734         if (PHINode *InPHI = dyn_cast<PHINode>(PN)) {
00735           // If the incoming value was a PHI, and if it was one of the PHIs we
00736           // already rewrote it, just use the lowered value.
00737           if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
00738             PredVal = Res;
00739             EltPHI->addIncoming(PredVal, Pred);
00740             continue;
00741           }
00742         }
00743 
00744         // Otherwise, do an extract in the predecessor.
00745         Builder->SetInsertPoint(Pred, Pred->getTerminator());
00746         Value *Res = InVal;
00747         if (Offset)
00748           Res = Builder->CreateLShr(Res, ConstantInt::get(InVal->getType(),
00749                                                           Offset), "extract");
00750         Res = Builder->CreateTrunc(Res, Ty, "extract.t");
00751         PredVal = Res;
00752         EltPHI->addIncoming(Res, Pred);
00753 
00754         // If the incoming value was a PHI, and if it was one of the PHIs we are
00755         // rewriting, we will ultimately delete the code we inserted.  This
00756         // means we need to revisit that PHI to make sure we extract out the
00757         // needed piece.
00758         if (PHINode *OldInVal = dyn_cast<PHINode>(PN->getIncomingValue(i)))
00759           if (PHIsInspected.count(OldInVal)) {
00760             unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(),
00761                                           OldInVal)-PHIsToSlice.begin();
00762             PHIUsers.push_back(PHIUsageRecord(RefPHIId, Offset,
00763                                               cast<Instruction>(Res)));
00764             ++UserE;
00765           }
00766       }
00767       PredValues.clear();
00768 
00769       DEBUG(dbgs() << "  Made element PHI for offset " << Offset << ": "
00770                    << *EltPHI << '\n');
00771       ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
00772     }
00773 
00774     // Replace the use of this piece with the PHI node.
00775     ReplaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI);
00776   }
00777 
00778   // Replace all the remaining uses of the PHI nodes (self uses and the lshrs)
00779   // with undefs.
00780   Value *Undef = UndefValue::get(FirstPhi.getType());
00781   for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
00782     ReplaceInstUsesWith(*PHIsToSlice[i], Undef);
00783   return ReplaceInstUsesWith(FirstPhi, Undef);
00784 }
00785 
00786 // PHINode simplification
00787 //
00788 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
00789   if (Value *V = SimplifyInstruction(&PN, DL, TLI))
00790     return ReplaceInstUsesWith(PN, V);
00791 
00792   // If all PHI operands are the same operation, pull them through the PHI,
00793   // reducing code size.
00794   if (isa<Instruction>(PN.getIncomingValue(0)) &&
00795       isa<Instruction>(PN.getIncomingValue(1)) &&
00796       cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
00797       cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
00798       // FIXME: The hasOneUse check will fail for PHIs that use the value more
00799       // than themselves more than once.
00800       PN.getIncomingValue(0)->hasOneUse())
00801     if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
00802       return Result;
00803 
00804   // If this is a trivial cycle in the PHI node graph, remove it.  Basically, if
00805   // this PHI only has a single use (a PHI), and if that PHI only has one use (a
00806   // PHI)... break the cycle.
00807   if (PN.hasOneUse()) {
00808     Instruction *PHIUser = cast<Instruction>(PN.user_back());
00809     if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
00810       SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
00811       PotentiallyDeadPHIs.insert(&PN);
00812       if (DeadPHICycle(PU, PotentiallyDeadPHIs))
00813         return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
00814     }
00815 
00816     // If this phi has a single use, and if that use just computes a value for
00817     // the next iteration of a loop, delete the phi.  This occurs with unused
00818     // induction variables, e.g. "for (int j = 0; ; ++j);".  Detecting this
00819     // common case here is good because the only other things that catch this
00820     // are induction variable analysis (sometimes) and ADCE, which is only run
00821     // late.
00822     if (PHIUser->hasOneUse() &&
00823         (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
00824         PHIUser->user_back() == &PN) {
00825       return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
00826     }
00827   }
00828 
00829   // We sometimes end up with phi cycles that non-obviously end up being the
00830   // same value, for example:
00831   //   z = some value; x = phi (y, z); y = phi (x, z)
00832   // where the phi nodes don't necessarily need to be in the same block.  Do a
00833   // quick check to see if the PHI node only contains a single non-phi value, if
00834   // so, scan to see if the phi cycle is actually equal to that value.
00835   {
00836     unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues();
00837     // Scan for the first non-phi operand.
00838     while (InValNo != NumIncomingVals &&
00839            isa<PHINode>(PN.getIncomingValue(InValNo)))
00840       ++InValNo;
00841 
00842     if (InValNo != NumIncomingVals) {
00843       Value *NonPhiInVal = PN.getIncomingValue(InValNo);
00844 
00845       // Scan the rest of the operands to see if there are any conflicts, if so
00846       // there is no need to recursively scan other phis.
00847       for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
00848         Value *OpVal = PN.getIncomingValue(InValNo);
00849         if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
00850           break;
00851       }
00852 
00853       // If we scanned over all operands, then we have one unique value plus
00854       // phi values.  Scan PHI nodes to see if they all merge in each other or
00855       // the value.
00856       if (InValNo == NumIncomingVals) {
00857         SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
00858         if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
00859           return ReplaceInstUsesWith(PN, NonPhiInVal);
00860       }
00861     }
00862   }
00863 
00864   // If there are multiple PHIs, sort their operands so that they all list
00865   // the blocks in the same order. This will help identical PHIs be eliminated
00866   // by other passes. Other passes shouldn't depend on this for correctness
00867   // however.
00868   PHINode *FirstPN = cast<PHINode>(PN.getParent()->begin());
00869   if (&PN != FirstPN)
00870     for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++i) {
00871       BasicBlock *BBA = PN.getIncomingBlock(i);
00872       BasicBlock *BBB = FirstPN->getIncomingBlock(i);
00873       if (BBA != BBB) {
00874         Value *VA = PN.getIncomingValue(i);
00875         unsigned j = PN.getBasicBlockIndex(BBB);
00876         Value *VB = PN.getIncomingValue(j);
00877         PN.setIncomingBlock(i, BBB);
00878         PN.setIncomingValue(i, VB);
00879         PN.setIncomingBlock(j, BBA);
00880         PN.setIncomingValue(j, VA);
00881         // NOTE: Instcombine normally would want us to "return &PN" if we
00882         // modified any of the operands of an instruction.  However, since we
00883         // aren't adding or removing uses (just rearranging them) we don't do
00884         // this in this case.
00885       }
00886     }
00887 
00888   // If this is an integer PHI and we know that it has an illegal type, see if
00889   // it is only used by trunc or trunc(lshr) operations.  If so, we split the
00890   // PHI into the various pieces being extracted.  This sort of thing is
00891   // introduced when SROA promotes an aggregate to a single large integer type.
00892   if (PN.getType()->isIntegerTy() && DL &&
00893       !DL->isLegalInteger(PN.getType()->getPrimitiveSizeInBits()))
00894     if (Instruction *Res = SliceUpIllegalIntegerPHI(PN))
00895       return Res;
00896 
00897   return 0;
00898 }