LLVM API Documentation

InstCombineSelect.cpp
Go to the documentation of this file.
00001 //===- InstCombineSelect.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 visitSelect function.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "InstCombineInternal.h"
00015 #include "llvm/Analysis/ConstantFolding.h"
00016 #include "llvm/Analysis/InstructionSimplify.h"
00017 #include "llvm/IR/PatternMatch.h"
00018 using namespace llvm;
00019 using namespace PatternMatch;
00020 
00021 #define DEBUG_TYPE "instcombine"
00022 
00023 /// MatchSelectPattern - Pattern match integer [SU]MIN, [SU]MAX, and ABS idioms,
00024 /// returning the kind and providing the out parameter results if we
00025 /// successfully match.
00026 static SelectPatternFlavor
00027 MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS) {
00028   SelectInst *SI = dyn_cast<SelectInst>(V);
00029   if (!SI) return SPF_UNKNOWN;
00030 
00031   ICmpInst *ICI = dyn_cast<ICmpInst>(SI->getCondition());
00032   if (!ICI) return SPF_UNKNOWN;
00033 
00034   ICmpInst::Predicate Pred = ICI->getPredicate();
00035   Value *CmpLHS = ICI->getOperand(0);
00036   Value *CmpRHS = ICI->getOperand(1);
00037   Value *TrueVal = SI->getTrueValue();
00038   Value *FalseVal = SI->getFalseValue();
00039 
00040   LHS = CmpLHS;
00041   RHS = CmpRHS;
00042 
00043   // (icmp X, Y) ? X : Y
00044   if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
00045     switch (Pred) {
00046     default: return SPF_UNKNOWN; // Equality.
00047     case ICmpInst::ICMP_UGT:
00048     case ICmpInst::ICMP_UGE: return SPF_UMAX;
00049     case ICmpInst::ICMP_SGT:
00050     case ICmpInst::ICMP_SGE: return SPF_SMAX;
00051     case ICmpInst::ICMP_ULT:
00052     case ICmpInst::ICMP_ULE: return SPF_UMIN;
00053     case ICmpInst::ICMP_SLT:
00054     case ICmpInst::ICMP_SLE: return SPF_SMIN;
00055     }
00056   }
00057 
00058   // (icmp X, Y) ? Y : X
00059   if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
00060     switch (Pred) {
00061     default: return SPF_UNKNOWN; // Equality.
00062     case ICmpInst::ICMP_UGT:
00063     case ICmpInst::ICMP_UGE: return SPF_UMIN;
00064     case ICmpInst::ICMP_SGT:
00065     case ICmpInst::ICMP_SGE: return SPF_SMIN;
00066     case ICmpInst::ICMP_ULT:
00067     case ICmpInst::ICMP_ULE: return SPF_UMAX;
00068     case ICmpInst::ICMP_SLT:
00069     case ICmpInst::ICMP_SLE: return SPF_SMAX;
00070     }
00071   }
00072 
00073   if (ConstantInt *C1 = dyn_cast<ConstantInt>(CmpRHS)) {
00074     if ((CmpLHS == TrueVal && match(FalseVal, m_Neg(m_Specific(CmpLHS)))) ||
00075         (CmpLHS == FalseVal && match(TrueVal, m_Neg(m_Specific(CmpLHS))))) {
00076 
00077       // ABS(X) ==> (X >s 0) ? X : -X and (X >s -1) ? X : -X
00078       // NABS(X) ==> (X >s 0) ? -X : X and (X >s -1) ? -X : X
00079       if (Pred == ICmpInst::ICMP_SGT && (C1->isZero() || C1->isMinusOne())) {
00080         return (CmpLHS == TrueVal) ? SPF_ABS : SPF_NABS;
00081       }
00082 
00083       // ABS(X) ==> (X <s 0) ? -X : X and (X <s 1) ? -X : X
00084       // NABS(X) ==> (X <s 0) ? X : -X and (X <s 1) ? X : -X
00085       if (Pred == ICmpInst::ICMP_SLT && (C1->isZero() || C1->isOne())) {
00086         return (CmpLHS == FalseVal) ? SPF_ABS : SPF_NABS;
00087       }
00088     }
00089   }
00090 
00091   // TODO: (X > 4) ? X : 5   -->  (X >= 5) ? X : 5  -->  MAX(X, 5)
00092 
00093   return SPF_UNKNOWN;
00094 }
00095 
00096 
00097 /// GetSelectFoldableOperands - We want to turn code that looks like this:
00098 ///   %C = or %A, %B
00099 ///   %D = select %cond, %C, %A
00100 /// into:
00101 ///   %C = select %cond, %B, 0
00102 ///   %D = or %A, %C
00103 ///
00104 /// Assuming that the specified instruction is an operand to the select, return
00105 /// a bitmask indicating which operands of this instruction are foldable if they
00106 /// equal the other incoming value of the select.
00107 ///
00108 static unsigned GetSelectFoldableOperands(Instruction *I) {
00109   switch (I->getOpcode()) {
00110   case Instruction::Add:
00111   case Instruction::Mul:
00112   case Instruction::And:
00113   case Instruction::Or:
00114   case Instruction::Xor:
00115     return 3;              // Can fold through either operand.
00116   case Instruction::Sub:   // Can only fold on the amount subtracted.
00117   case Instruction::Shl:   // Can only fold on the shift amount.
00118   case Instruction::LShr:
00119   case Instruction::AShr:
00120     return 1;
00121   default:
00122     return 0;              // Cannot fold
00123   }
00124 }
00125 
00126 /// GetSelectFoldableConstant - For the same transformation as the previous
00127 /// function, return the identity constant that goes into the select.
00128 static Constant *GetSelectFoldableConstant(Instruction *I) {
00129   switch (I->getOpcode()) {
00130   default: llvm_unreachable("This cannot happen!");
00131   case Instruction::Add:
00132   case Instruction::Sub:
00133   case Instruction::Or:
00134   case Instruction::Xor:
00135   case Instruction::Shl:
00136   case Instruction::LShr:
00137   case Instruction::AShr:
00138     return Constant::getNullValue(I->getType());
00139   case Instruction::And:
00140     return Constant::getAllOnesValue(I->getType());
00141   case Instruction::Mul:
00142     return ConstantInt::get(I->getType(), 1);
00143   }
00144 }
00145 
00146 /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
00147 /// have the same opcode and only one use each.  Try to simplify this.
00148 Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
00149                                           Instruction *FI) {
00150   if (TI->getNumOperands() == 1) {
00151     // If this is a non-volatile load or a cast from the same type,
00152     // merge.
00153     if (TI->isCast()) {
00154       Type *FIOpndTy = FI->getOperand(0)->getType();
00155       if (TI->getOperand(0)->getType() != FIOpndTy)
00156         return nullptr;
00157       // The select condition may be a vector. We may only change the operand
00158       // type if the vector width remains the same (and matches the condition).
00159       Type *CondTy = SI.getCondition()->getType();
00160       if (CondTy->isVectorTy() && (!FIOpndTy->isVectorTy() ||
00161           CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()))
00162         return nullptr;
00163     } else {
00164       return nullptr;  // unknown unary op.
00165     }
00166 
00167     // Fold this by inserting a select from the input values.
00168     Value *NewSI = Builder->CreateSelect(SI.getCondition(), TI->getOperand(0),
00169                                          FI->getOperand(0), SI.getName()+".v");
00170     return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
00171                             TI->getType());
00172   }
00173 
00174   // Only handle binary operators here.
00175   if (!isa<BinaryOperator>(TI))
00176     return nullptr;
00177 
00178   // Figure out if the operations have any operands in common.
00179   Value *MatchOp, *OtherOpT, *OtherOpF;
00180   bool MatchIsOpZero;
00181   if (TI->getOperand(0) == FI->getOperand(0)) {
00182     MatchOp  = TI->getOperand(0);
00183     OtherOpT = TI->getOperand(1);
00184     OtherOpF = FI->getOperand(1);
00185     MatchIsOpZero = true;
00186   } else if (TI->getOperand(1) == FI->getOperand(1)) {
00187     MatchOp  = TI->getOperand(1);
00188     OtherOpT = TI->getOperand(0);
00189     OtherOpF = FI->getOperand(0);
00190     MatchIsOpZero = false;
00191   } else if (!TI->isCommutative()) {
00192     return nullptr;
00193   } else if (TI->getOperand(0) == FI->getOperand(1)) {
00194     MatchOp  = TI->getOperand(0);
00195     OtherOpT = TI->getOperand(1);
00196     OtherOpF = FI->getOperand(0);
00197     MatchIsOpZero = true;
00198   } else if (TI->getOperand(1) == FI->getOperand(0)) {
00199     MatchOp  = TI->getOperand(1);
00200     OtherOpT = TI->getOperand(0);
00201     OtherOpF = FI->getOperand(1);
00202     MatchIsOpZero = true;
00203   } else {
00204     return nullptr;
00205   }
00206 
00207   // If we reach here, they do have operations in common.
00208   Value *NewSI = Builder->CreateSelect(SI.getCondition(), OtherOpT,
00209                                        OtherOpF, SI.getName()+".v");
00210 
00211   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
00212     if (MatchIsOpZero)
00213       return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
00214     else
00215       return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
00216   }
00217   llvm_unreachable("Shouldn't get here");
00218 }
00219 
00220 static bool isSelect01(Constant *C1, Constant *C2) {
00221   ConstantInt *C1I = dyn_cast<ConstantInt>(C1);
00222   if (!C1I)
00223     return false;
00224   ConstantInt *C2I = dyn_cast<ConstantInt>(C2);
00225   if (!C2I)
00226     return false;
00227   if (!C1I->isZero() && !C2I->isZero()) // One side must be zero.
00228     return false;
00229   return C1I->isOne() || C1I->isAllOnesValue() ||
00230          C2I->isOne() || C2I->isAllOnesValue();
00231 }
00232 
00233 /// FoldSelectIntoOp - Try fold the select into one of the operands to
00234 /// facilitate further optimization.
00235 Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
00236                                             Value *FalseVal) {
00237   // See the comment above GetSelectFoldableOperands for a description of the
00238   // transformation we are doing here.
00239   if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
00240     if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
00241         !isa<Constant>(FalseVal)) {
00242       if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
00243         unsigned OpToFold = 0;
00244         if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
00245           OpToFold = 1;
00246         } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
00247           OpToFold = 2;
00248         }
00249 
00250         if (OpToFold) {
00251           Constant *C = GetSelectFoldableConstant(TVI);
00252           Value *OOp = TVI->getOperand(2-OpToFold);
00253           // Avoid creating select between 2 constants unless it's selecting
00254           // between 0, 1 and -1.
00255           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
00256             Value *NewSel = Builder->CreateSelect(SI.getCondition(), OOp, C);
00257             NewSel->takeName(TVI);
00258             BinaryOperator *TVI_BO = cast<BinaryOperator>(TVI);
00259             BinaryOperator *BO = BinaryOperator::Create(TVI_BO->getOpcode(),
00260                                                         FalseVal, NewSel);
00261             if (isa<PossiblyExactOperator>(BO))
00262               BO->setIsExact(TVI_BO->isExact());
00263             if (isa<OverflowingBinaryOperator>(BO)) {
00264               BO->setHasNoUnsignedWrap(TVI_BO->hasNoUnsignedWrap());
00265               BO->setHasNoSignedWrap(TVI_BO->hasNoSignedWrap());
00266             }
00267             return BO;
00268           }
00269         }
00270       }
00271     }
00272   }
00273 
00274   if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
00275     if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
00276         !isa<Constant>(TrueVal)) {
00277       if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
00278         unsigned OpToFold = 0;
00279         if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
00280           OpToFold = 1;
00281         } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
00282           OpToFold = 2;
00283         }
00284 
00285         if (OpToFold) {
00286           Constant *C = GetSelectFoldableConstant(FVI);
00287           Value *OOp = FVI->getOperand(2-OpToFold);
00288           // Avoid creating select between 2 constants unless it's selecting
00289           // between 0, 1 and -1.
00290           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
00291             Value *NewSel = Builder->CreateSelect(SI.getCondition(), C, OOp);
00292             NewSel->takeName(FVI);
00293             BinaryOperator *FVI_BO = cast<BinaryOperator>(FVI);
00294             BinaryOperator *BO = BinaryOperator::Create(FVI_BO->getOpcode(),
00295                                                         TrueVal, NewSel);
00296             if (isa<PossiblyExactOperator>(BO))
00297               BO->setIsExact(FVI_BO->isExact());
00298             if (isa<OverflowingBinaryOperator>(BO)) {
00299               BO->setHasNoUnsignedWrap(FVI_BO->hasNoUnsignedWrap());
00300               BO->setHasNoSignedWrap(FVI_BO->hasNoSignedWrap());
00301             }
00302             return BO;
00303           }
00304         }
00305       }
00306     }
00307   }
00308 
00309   return nullptr;
00310 }
00311 
00312 /// SimplifyWithOpReplaced - See if V simplifies when its operand Op is
00313 /// replaced with RepOp.
00314 static Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
00315                                      const DataLayout *TD,
00316                                      const TargetLibraryInfo *TLI,
00317                                      DominatorTree *DT, AssumptionCache *AC) {
00318   // Trivial replacement.
00319   if (V == Op)
00320     return RepOp;
00321 
00322   Instruction *I = dyn_cast<Instruction>(V);
00323   if (!I)
00324     return nullptr;
00325 
00326   // If this is a binary operator, try to simplify it with the replaced op.
00327   if (BinaryOperator *B = dyn_cast<BinaryOperator>(I)) {
00328     if (B->getOperand(0) == Op)
00329       return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), TD, TLI);
00330     if (B->getOperand(1) == Op)
00331       return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, TD, TLI);
00332   }
00333 
00334   // Same for CmpInsts.
00335   if (CmpInst *C = dyn_cast<CmpInst>(I)) {
00336     if (C->getOperand(0) == Op)
00337       return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), TD,
00338                              TLI, DT, AC);
00339     if (C->getOperand(1) == Op)
00340       return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, TD,
00341                              TLI, DT, AC);
00342   }
00343 
00344   // TODO: We could hand off more cases to instsimplify here.
00345 
00346   // If all operands are constant after substituting Op for RepOp then we can
00347   // constant fold the instruction.
00348   if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
00349     // Build a list of all constant operands.
00350     SmallVector<Constant*, 8> ConstOps;
00351     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
00352       if (I->getOperand(i) == Op)
00353         ConstOps.push_back(CRepOp);
00354       else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i)))
00355         ConstOps.push_back(COp);
00356       else
00357         break;
00358     }
00359 
00360     // All operands were constants, fold it.
00361     if (ConstOps.size() == I->getNumOperands()) {
00362       if (CmpInst *C = dyn_cast<CmpInst>(I))
00363         return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0],
00364                                                ConstOps[1], TD, TLI);
00365 
00366       if (LoadInst *LI = dyn_cast<LoadInst>(I))
00367         if (!LI->isVolatile())
00368           return ConstantFoldLoadFromConstPtr(ConstOps[0], TD);
00369 
00370       return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
00371                                       ConstOps, TD, TLI);
00372     }
00373   }
00374 
00375   return nullptr;
00376 }
00377 
00378 /// foldSelectICmpAndOr - We want to turn:
00379 ///   (select (icmp eq (and X, C1), 0), Y, (or Y, C2))
00380 /// into:
00381 ///   (or (shl (and X, C1), C3), y)
00382 /// iff:
00383 ///   C1 and C2 are both powers of 2
00384 /// where:
00385 ///   C3 = Log(C2) - Log(C1)
00386 ///
00387 /// This transform handles cases where:
00388 /// 1. The icmp predicate is inverted
00389 /// 2. The select operands are reversed
00390 /// 3. The magnitude of C2 and C1 are flipped
00391 static Value *foldSelectICmpAndOr(const SelectInst &SI, Value *TrueVal,
00392                                   Value *FalseVal,
00393                                   InstCombiner::BuilderTy *Builder) {
00394   const ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition());
00395   if (!IC || !IC->isEquality() || !SI.getType()->isIntegerTy())
00396     return nullptr;
00397 
00398   Value *CmpLHS = IC->getOperand(0);
00399   Value *CmpRHS = IC->getOperand(1);
00400 
00401   if (!match(CmpRHS, m_Zero()))
00402     return nullptr;
00403 
00404   Value *X;
00405   const APInt *C1;
00406   if (!match(CmpLHS, m_And(m_Value(X), m_Power2(C1))))
00407     return nullptr;
00408 
00409   const APInt *C2;
00410   bool OrOnTrueVal = false;
00411   bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)));
00412   if (!OrOnFalseVal)
00413     OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)));
00414 
00415   if (!OrOnFalseVal && !OrOnTrueVal)
00416     return nullptr;
00417 
00418   Value *V = CmpLHS;
00419   Value *Y = OrOnFalseVal ? TrueVal : FalseVal;
00420 
00421   unsigned C1Log = C1->logBase2();
00422   unsigned C2Log = C2->logBase2();
00423   if (C2Log > C1Log) {
00424     V = Builder->CreateZExtOrTrunc(V, Y->getType());
00425     V = Builder->CreateShl(V, C2Log - C1Log);
00426   } else if (C1Log > C2Log) {
00427     V = Builder->CreateLShr(V, C1Log - C2Log);
00428     V = Builder->CreateZExtOrTrunc(V, Y->getType());
00429   } else
00430     V = Builder->CreateZExtOrTrunc(V, Y->getType());
00431 
00432   ICmpInst::Predicate Pred = IC->getPredicate();
00433   if ((Pred == ICmpInst::ICMP_NE && OrOnFalseVal) ||
00434       (Pred == ICmpInst::ICMP_EQ && OrOnTrueVal))
00435     V = Builder->CreateXor(V, *C2);
00436 
00437   return Builder->CreateOr(V, Y);
00438 }
00439 
00440 /// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single
00441 /// call to cttz/ctlz with flag 'is_zero_undef' cleared.
00442 ///
00443 /// For example, we can fold the following code sequence:
00444 /// \code
00445 ///   %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true)
00446 ///   %1 = icmp ne i32 %x, 0
00447 ///   %2 = select i1 %1, i32 %0, i32 32
00448 /// \code
00449 /// 
00450 /// into:
00451 ///   %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
00452 static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
00453                                   InstCombiner::BuilderTy *Builder) {
00454   ICmpInst::Predicate Pred = ICI->getPredicate();
00455   Value *CmpLHS = ICI->getOperand(0);
00456   Value *CmpRHS = ICI->getOperand(1);
00457 
00458   // Check if the condition value compares a value for equality against zero.
00459   if (!ICI->isEquality() || !match(CmpRHS, m_Zero()))
00460     return nullptr;
00461 
00462   Value *Count = FalseVal;
00463   Value *ValueOnZero = TrueVal;
00464   if (Pred == ICmpInst::ICMP_NE)
00465     std::swap(Count, ValueOnZero);
00466 
00467   // Skip zero extend/truncate.
00468   Value *V = nullptr;
00469   if (match(Count, m_ZExt(m_Value(V))) ||
00470       match(Count, m_Trunc(m_Value(V))))
00471     Count = V;
00472 
00473   // Check if the value propagated on zero is a constant number equal to the
00474   // sizeof in bits of 'Count'.
00475   unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits();
00476   if (!match(ValueOnZero, m_SpecificInt(SizeOfInBits)))
00477     return nullptr;
00478 
00479   // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the
00480   // input to the cttz/ctlz is used as LHS for the compare instruction.
00481   if (match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) ||
00482       match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS)))) {
00483     IntrinsicInst *II = cast<IntrinsicInst>(Count);
00484     IRBuilder<> Builder(II);
00485     // Explicitly clear the 'undef_on_zero' flag.
00486     IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone());
00487     Type *Ty = NewI->getArgOperand(1)->getType();
00488     NewI->setArgOperand(1, Constant::getNullValue(Ty));
00489     Builder.Insert(NewI);
00490     return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType());
00491   }
00492 
00493   return nullptr;
00494 }
00495 
00496 /// visitSelectInstWithICmp - Visit a SelectInst that has an
00497 /// ICmpInst as its first operand.
00498 ///
00499 Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
00500                                                    ICmpInst *ICI) {
00501   bool Changed = false;
00502   ICmpInst::Predicate Pred = ICI->getPredicate();
00503   Value *CmpLHS = ICI->getOperand(0);
00504   Value *CmpRHS = ICI->getOperand(1);
00505   Value *TrueVal = SI.getTrueValue();
00506   Value *FalseVal = SI.getFalseValue();
00507 
00508   // Check cases where the comparison is with a constant that
00509   // can be adjusted to fit the min/max idiom. We may move or edit ICI
00510   // here, so make sure the select is the only user.
00511   if (ICI->hasOneUse())
00512     if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
00513       // X < MIN ? T : F  -->  F
00514       if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT)
00515           && CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
00516         return ReplaceInstUsesWith(SI, FalseVal);
00517       // X > MAX ? T : F  -->  F
00518       else if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_UGT)
00519                && CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
00520         return ReplaceInstUsesWith(SI, FalseVal);
00521       switch (Pred) {
00522       default: break;
00523       case ICmpInst::ICMP_ULT:
00524       case ICmpInst::ICMP_SLT:
00525       case ICmpInst::ICMP_UGT:
00526       case ICmpInst::ICMP_SGT: {
00527         // These transformations only work for selects over integers.
00528         IntegerType *SelectTy = dyn_cast<IntegerType>(SI.getType());
00529         if (!SelectTy)
00530           break;
00531 
00532         Constant *AdjustedRHS;
00533         if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
00534           AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1);
00535         else // (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT)
00536           AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() - 1);
00537 
00538         // X > C ? X : C+1  -->  X < C+1 ? C+1 : X
00539         // X < C ? X : C-1  -->  X > C-1 ? C-1 : X
00540         if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
00541             (CmpLHS == FalseVal && AdjustedRHS == TrueVal))
00542           ; // Nothing to do here. Values match without any sign/zero extension.
00543 
00544         // Types do not match. Instead of calculating this with mixed types
00545         // promote all to the larger type. This enables scalar evolution to
00546         // analyze this expression.
00547         else if (CmpRHS->getType()->getScalarSizeInBits()
00548                  < SelectTy->getBitWidth()) {
00549           Constant *sextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy);
00550 
00551           // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
00552           // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
00553           // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X
00554           // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X
00555           if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) &&
00556                 sextRHS == FalseVal) {
00557             CmpLHS = TrueVal;
00558             AdjustedRHS = sextRHS;
00559           } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) &&
00560                      sextRHS == TrueVal) {
00561             CmpLHS = FalseVal;
00562             AdjustedRHS = sextRHS;
00563           } else if (ICI->isUnsigned()) {
00564             Constant *zextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy);
00565             // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
00566             // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
00567             // zext + signed compare cannot be changed:
00568             //    0xff <s 0x00, but 0x00ff >s 0x0000
00569             if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) &&
00570                 zextRHS == FalseVal) {
00571               CmpLHS = TrueVal;
00572               AdjustedRHS = zextRHS;
00573             } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) &&
00574                        zextRHS == TrueVal) {
00575               CmpLHS = FalseVal;
00576               AdjustedRHS = zextRHS;
00577             } else
00578               break;
00579           } else
00580             break;
00581         } else
00582           break;
00583 
00584         Pred = ICmpInst::getSwappedPredicate(Pred);
00585         CmpRHS = AdjustedRHS;
00586         std::swap(FalseVal, TrueVal);
00587         ICI->setPredicate(Pred);
00588         ICI->setOperand(0, CmpLHS);
00589         ICI->setOperand(1, CmpRHS);
00590         SI.setOperand(1, TrueVal);
00591         SI.setOperand(2, FalseVal);
00592 
00593         // Move ICI instruction right before the select instruction. Otherwise
00594         // the sext/zext value may be defined after the ICI instruction uses it.
00595         ICI->moveBefore(&SI);
00596 
00597         Changed = true;
00598         break;
00599       }
00600       }
00601     }
00602 
00603   // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1
00604   // and       (X <s  0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1
00605   // FIXME: Type and constness constraints could be lifted, but we have to
00606   //        watch code size carefully. We should consider xor instead of
00607   //        sub/add when we decide to do that.
00608   if (IntegerType *Ty = dyn_cast<IntegerType>(CmpLHS->getType())) {
00609     if (TrueVal->getType() == Ty) {
00610       if (ConstantInt *Cmp = dyn_cast<ConstantInt>(CmpRHS)) {
00611         ConstantInt *C1 = nullptr, *C2 = nullptr;
00612         if (Pred == ICmpInst::ICMP_SGT && Cmp->isAllOnesValue()) {
00613           C1 = dyn_cast<ConstantInt>(TrueVal);
00614           C2 = dyn_cast<ConstantInt>(FalseVal);
00615         } else if (Pred == ICmpInst::ICMP_SLT && Cmp->isNullValue()) {
00616           C1 = dyn_cast<ConstantInt>(FalseVal);
00617           C2 = dyn_cast<ConstantInt>(TrueVal);
00618         }
00619         if (C1 && C2) {
00620           // This shift results in either -1 or 0.
00621           Value *AShr = Builder->CreateAShr(CmpLHS, Ty->getBitWidth()-1);
00622 
00623           // Check if we can express the operation with a single or.
00624           if (C2->isAllOnesValue())
00625             return ReplaceInstUsesWith(SI, Builder->CreateOr(AShr, C1));
00626 
00627           Value *And = Builder->CreateAnd(AShr, C2->getValue()-C1->getValue());
00628           return ReplaceInstUsesWith(SI, Builder->CreateAdd(And, C1));
00629         }
00630       }
00631     }
00632   }
00633 
00634   // If we have an equality comparison then we know the value in one of the
00635   // arms of the select. See if substituting this value into the arm and
00636   // simplifying the result yields the same value as the other arm.
00637   if (Pred == ICmpInst::ICMP_EQ) {
00638     if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, DL, TLI, DT, AC) ==
00639             TrueVal ||
00640         SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, DL, TLI, DT, AC) ==
00641             TrueVal)
00642       return ReplaceInstUsesWith(SI, FalseVal);
00643     if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, DL, TLI, DT, AC) ==
00644             FalseVal ||
00645         SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, DL, TLI, DT, AC) ==
00646             FalseVal)
00647       return ReplaceInstUsesWith(SI, FalseVal);
00648   } else if (Pred == ICmpInst::ICMP_NE) {
00649     if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, DL, TLI, DT, AC) ==
00650             FalseVal ||
00651         SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, DL, TLI, DT, AC) ==
00652             FalseVal)
00653       return ReplaceInstUsesWith(SI, TrueVal);
00654     if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, DL, TLI, DT, AC) ==
00655             TrueVal ||
00656         SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, DL, TLI, DT, AC) ==
00657             TrueVal)
00658       return ReplaceInstUsesWith(SI, TrueVal);
00659   }
00660 
00661   // NOTE: if we wanted to, this is where to detect integer MIN/MAX
00662 
00663   if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
00664     if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
00665       // Transform (X == C) ? X : Y -> (X == C) ? C : Y
00666       SI.setOperand(1, CmpRHS);
00667       Changed = true;
00668     } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
00669       // Transform (X != C) ? Y : X -> (X != C) ? Y : C
00670       SI.setOperand(2, CmpRHS);
00671       Changed = true;
00672     }
00673   }
00674 
00675   if (unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits()) {
00676     APInt MinSignedValue = APInt::getSignBit(BitWidth);
00677     Value *X;
00678     const APInt *Y, *C;
00679     bool TrueWhenUnset;
00680     bool IsBitTest = false;
00681     if (ICmpInst::isEquality(Pred) &&
00682         match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) &&
00683         match(CmpRHS, m_Zero())) {
00684       IsBitTest = true;
00685       TrueWhenUnset = Pred == ICmpInst::ICMP_EQ;
00686     } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) {
00687       X = CmpLHS;
00688       Y = &MinSignedValue;
00689       IsBitTest = true;
00690       TrueWhenUnset = false;
00691     } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) {
00692       X = CmpLHS;
00693       Y = &MinSignedValue;
00694       IsBitTest = true;
00695       TrueWhenUnset = true;
00696     }
00697     if (IsBitTest) {
00698       Value *V = nullptr;
00699       // (X & Y) == 0 ? X : X ^ Y  --> X & ~Y
00700       if (TrueWhenUnset && TrueVal == X &&
00701           match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
00702         V = Builder->CreateAnd(X, ~(*Y));
00703       // (X & Y) != 0 ? X ^ Y : X  --> X & ~Y
00704       else if (!TrueWhenUnset && FalseVal == X &&
00705                match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
00706         V = Builder->CreateAnd(X, ~(*Y));
00707       // (X & Y) == 0 ? X ^ Y : X  --> X | Y
00708       else if (TrueWhenUnset && FalseVal == X &&
00709                match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
00710         V = Builder->CreateOr(X, *Y);
00711       // (X & Y) != 0 ? X : X ^ Y  --> X | Y
00712       else if (!TrueWhenUnset && TrueVal == X &&
00713                match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
00714         V = Builder->CreateOr(X, *Y);
00715 
00716       if (V)
00717         return ReplaceInstUsesWith(SI, V);
00718     }
00719   }
00720 
00721   if (Value *V = foldSelectICmpAndOr(SI, TrueVal, FalseVal, Builder))
00722     return ReplaceInstUsesWith(SI, V);
00723 
00724   if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
00725     return ReplaceInstUsesWith(SI, V);
00726 
00727   return Changed ? &SI : nullptr;
00728 }
00729 
00730 
00731 /// CanSelectOperandBeMappingIntoPredBlock - SI is a select whose condition is a
00732 /// PHI node (but the two may be in different blocks).  See if the true/false
00733 /// values (V) are live in all of the predecessor blocks of the PHI.  For
00734 /// example, cases like this cannot be mapped:
00735 ///
00736 ///   X = phi [ C1, BB1], [C2, BB2]
00737 ///   Y = add
00738 ///   Z = select X, Y, 0
00739 ///
00740 /// because Y is not live in BB1/BB2.
00741 ///
00742 static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V,
00743                                                    const SelectInst &SI) {
00744   // If the value is a non-instruction value like a constant or argument, it
00745   // can always be mapped.
00746   const Instruction *I = dyn_cast<Instruction>(V);
00747   if (!I) return true;
00748 
00749   // If V is a PHI node defined in the same block as the condition PHI, we can
00750   // map the arguments.
00751   const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
00752 
00753   if (const PHINode *VP = dyn_cast<PHINode>(I))
00754     if (VP->getParent() == CondPHI->getParent())
00755       return true;
00756 
00757   // Otherwise, if the PHI and select are defined in the same block and if V is
00758   // defined in a different block, then we can transform it.
00759   if (SI.getParent() == CondPHI->getParent() &&
00760       I->getParent() != CondPHI->getParent())
00761     return true;
00762 
00763   // Otherwise we have a 'hard' case and we can't tell without doing more
00764   // detailed dominator based analysis, punt.
00765   return false;
00766 }
00767 
00768 /// FoldSPFofSPF - We have an SPF (e.g. a min or max) of an SPF of the form:
00769 ///   SPF2(SPF1(A, B), C)
00770 Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner,
00771                                         SelectPatternFlavor SPF1,
00772                                         Value *A, Value *B,
00773                                         Instruction &Outer,
00774                                         SelectPatternFlavor SPF2, Value *C) {
00775   if (C == A || C == B) {
00776     // MAX(MAX(A, B), B) -> MAX(A, B)
00777     // MIN(MIN(a, b), a) -> MIN(a, b)
00778     if (SPF1 == SPF2)
00779       return ReplaceInstUsesWith(Outer, Inner);
00780 
00781     // MAX(MIN(a, b), a) -> a
00782     // MIN(MAX(a, b), a) -> a
00783     if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
00784         (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
00785         (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
00786         (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
00787       return ReplaceInstUsesWith(Outer, C);
00788   }
00789 
00790   if (SPF1 == SPF2) {
00791     if (ConstantInt *CB = dyn_cast<ConstantInt>(B)) {
00792       if (ConstantInt *CC = dyn_cast<ConstantInt>(C)) {
00793         APInt ACB = CB->getValue();
00794         APInt ACC = CC->getValue();
00795 
00796         // MIN(MIN(A, 23), 97) -> MIN(A, 23)
00797         // MAX(MAX(A, 97), 23) -> MAX(A, 97)
00798         if ((SPF1 == SPF_UMIN && ACB.ule(ACC)) ||
00799             (SPF1 == SPF_SMIN && ACB.sle(ACC)) ||
00800             (SPF1 == SPF_UMAX && ACB.uge(ACC)) ||
00801             (SPF1 == SPF_SMAX && ACB.sge(ACC)))
00802           return ReplaceInstUsesWith(Outer, Inner);
00803 
00804         // MIN(MIN(A, 97), 23) -> MIN(A, 23)
00805         // MAX(MAX(A, 23), 97) -> MAX(A, 97)
00806         if ((SPF1 == SPF_UMIN && ACB.ugt(ACC)) ||
00807             (SPF1 == SPF_SMIN && ACB.sgt(ACC)) ||
00808             (SPF1 == SPF_UMAX && ACB.ult(ACC)) ||
00809             (SPF1 == SPF_SMAX && ACB.slt(ACC))) {
00810           Outer.replaceUsesOfWith(Inner, A);
00811           return &Outer;
00812         }
00813       }
00814     }
00815   }
00816 
00817   // ABS(ABS(X)) -> ABS(X)
00818   // NABS(NABS(X)) -> NABS(X)
00819   if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) {
00820     return ReplaceInstUsesWith(Outer, Inner);
00821   }
00822 
00823   // ABS(NABS(X)) -> ABS(X)
00824   // NABS(ABS(X)) -> NABS(X)
00825   if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) ||
00826       (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) {
00827     SelectInst *SI = cast<SelectInst>(Inner);
00828     Value *NewSI = Builder->CreateSelect(
00829         SI->getCondition(), SI->getFalseValue(), SI->getTrueValue());
00830     return ReplaceInstUsesWith(Outer, NewSI);
00831   }
00832   return nullptr;
00833 }
00834 
00835 /// foldSelectICmpAnd - If one of the constants is zero (we know they can't
00836 /// both be) and we have an icmp instruction with zero, and we have an 'and'
00837 /// with the non-constant value and a power of two we can turn the select
00838 /// into a shift on the result of the 'and'.
00839 static Value *foldSelectICmpAnd(const SelectInst &SI, ConstantInt *TrueVal,
00840                                 ConstantInt *FalseVal,
00841                                 InstCombiner::BuilderTy *Builder) {
00842   const ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition());
00843   if (!IC || !IC->isEquality() || !SI.getType()->isIntegerTy())
00844     return nullptr;
00845 
00846   if (!match(IC->getOperand(1), m_Zero()))
00847     return nullptr;
00848 
00849   ConstantInt *AndRHS;
00850   Value *LHS = IC->getOperand(0);
00851   if (!match(LHS, m_And(m_Value(), m_ConstantInt(AndRHS))))
00852     return nullptr;
00853 
00854   // If both select arms are non-zero see if we have a select of the form
00855   // 'x ? 2^n + C : C'. Then we can offset both arms by C, use the logic
00856   // for 'x ? 2^n : 0' and fix the thing up at the end.
00857   ConstantInt *Offset = nullptr;
00858   if (!TrueVal->isZero() && !FalseVal->isZero()) {
00859     if ((TrueVal->getValue() - FalseVal->getValue()).isPowerOf2())
00860       Offset = FalseVal;
00861     else if ((FalseVal->getValue() - TrueVal->getValue()).isPowerOf2())
00862       Offset = TrueVal;
00863     else
00864       return nullptr;
00865 
00866     // Adjust TrueVal and FalseVal to the offset.
00867     TrueVal = ConstantInt::get(Builder->getContext(),
00868                                TrueVal->getValue() - Offset->getValue());
00869     FalseVal = ConstantInt::get(Builder->getContext(),
00870                                 FalseVal->getValue() - Offset->getValue());
00871   }
00872 
00873   // Make sure the mask in the 'and' and one of the select arms is a power of 2.
00874   if (!AndRHS->getValue().isPowerOf2() ||
00875       (!TrueVal->getValue().isPowerOf2() &&
00876        !FalseVal->getValue().isPowerOf2()))
00877     return nullptr;
00878 
00879   // Determine which shift is needed to transform result of the 'and' into the
00880   // desired result.
00881   ConstantInt *ValC = !TrueVal->isZero() ? TrueVal : FalseVal;
00882   unsigned ValZeros = ValC->getValue().logBase2();
00883   unsigned AndZeros = AndRHS->getValue().logBase2();
00884 
00885   // If types don't match we can still convert the select by introducing a zext
00886   // or a trunc of the 'and'. The trunc case requires that all of the truncated
00887   // bits are zero, we can figure that out by looking at the 'and' mask.
00888   if (AndZeros >= ValC->getBitWidth())
00889     return nullptr;
00890 
00891   Value *V = Builder->CreateZExtOrTrunc(LHS, SI.getType());
00892   if (ValZeros > AndZeros)
00893     V = Builder->CreateShl(V, ValZeros - AndZeros);
00894   else if (ValZeros < AndZeros)
00895     V = Builder->CreateLShr(V, AndZeros - ValZeros);
00896 
00897   // Okay, now we know that everything is set up, we just don't know whether we
00898   // have a icmp_ne or icmp_eq and whether the true or false val is the zero.
00899   bool ShouldNotVal = !TrueVal->isZero();
00900   ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
00901   if (ShouldNotVal)
00902     V = Builder->CreateXor(V, ValC);
00903 
00904   // Apply an offset if needed.
00905   if (Offset)
00906     V = Builder->CreateAdd(V, Offset);
00907   return V;
00908 }
00909 
00910 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
00911   Value *CondVal = SI.getCondition();
00912   Value *TrueVal = SI.getTrueValue();
00913   Value *FalseVal = SI.getFalseValue();
00914 
00915   if (Value *V =
00916           SimplifySelectInst(CondVal, TrueVal, FalseVal, DL, TLI, DT, AC))
00917     return ReplaceInstUsesWith(SI, V);
00918 
00919   if (SI.getType()->isIntegerTy(1)) {
00920     if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
00921       if (C->getZExtValue()) {
00922         // Change: A = select B, true, C --> A = or B, C
00923         return BinaryOperator::CreateOr(CondVal, FalseVal);
00924       }
00925       // Change: A = select B, false, C --> A = and !B, C
00926       Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00927       return BinaryOperator::CreateAnd(NotCond, FalseVal);
00928     }
00929     if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
00930       if (C->getZExtValue() == false) {
00931         // Change: A = select B, C, false --> A = and B, C
00932         return BinaryOperator::CreateAnd(CondVal, TrueVal);
00933       }
00934       // Change: A = select B, C, true --> A = or !B, C
00935       Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00936       return BinaryOperator::CreateOr(NotCond, TrueVal);
00937     }
00938 
00939     // select a, b, a  -> a&b
00940     // select a, a, b  -> a|b
00941     if (CondVal == TrueVal)
00942       return BinaryOperator::CreateOr(CondVal, FalseVal);
00943     if (CondVal == FalseVal)
00944       return BinaryOperator::CreateAnd(CondVal, TrueVal);
00945 
00946     // select a, ~a, b -> (~a)&b
00947     // select a, b, ~a -> (~a)|b
00948     if (match(TrueVal, m_Not(m_Specific(CondVal))))
00949       return BinaryOperator::CreateAnd(TrueVal, FalseVal);
00950     if (match(FalseVal, m_Not(m_Specific(CondVal))))
00951       return BinaryOperator::CreateOr(TrueVal, FalseVal);
00952   }
00953 
00954   // Selecting between two integer constants?
00955   if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
00956     if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
00957       // select C, 1, 0 -> zext C to int
00958       if (FalseValC->isZero() && TrueValC->getValue() == 1)
00959         return new ZExtInst(CondVal, SI.getType());
00960 
00961       // select C, -1, 0 -> sext C to int
00962       if (FalseValC->isZero() && TrueValC->isAllOnesValue())
00963         return new SExtInst(CondVal, SI.getType());
00964 
00965       // select C, 0, 1 -> zext !C to int
00966       if (TrueValC->isZero() && FalseValC->getValue() == 1) {
00967         Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00968         return new ZExtInst(NotCond, SI.getType());
00969       }
00970 
00971       // select C, 0, -1 -> sext !C to int
00972       if (TrueValC->isZero() && FalseValC->isAllOnesValue()) {
00973         Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00974         return new SExtInst(NotCond, SI.getType());
00975       }
00976 
00977       if (Value *V = foldSelectICmpAnd(SI, TrueValC, FalseValC, Builder))
00978         return ReplaceInstUsesWith(SI, V);
00979     }
00980 
00981   // See if we are selecting two values based on a comparison of the two values.
00982   if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
00983     if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
00984       // Transform (X == Y) ? X : Y  -> Y
00985       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
00986         // This is not safe in general for floating point:
00987         // consider X== -0, Y== +0.
00988         // It becomes safe if either operand is a nonzero constant.
00989         ConstantFP *CFPt, *CFPf;
00990         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
00991               !CFPt->getValueAPF().isZero()) ||
00992             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
00993              !CFPf->getValueAPF().isZero()))
00994         return ReplaceInstUsesWith(SI, FalseVal);
00995       }
00996       // Transform (X une Y) ? X : Y  -> X
00997       if (FCI->getPredicate() == FCmpInst::FCMP_UNE) {
00998         // This is not safe in general for floating point:
00999         // consider X== -0, Y== +0.
01000         // It becomes safe if either operand is a nonzero constant.
01001         ConstantFP *CFPt, *CFPf;
01002         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
01003               !CFPt->getValueAPF().isZero()) ||
01004             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
01005              !CFPf->getValueAPF().isZero()))
01006         return ReplaceInstUsesWith(SI, TrueVal);
01007       }
01008 
01009       // Canonicalize to use ordered comparisons by swapping the select
01010       // operands.
01011       //
01012       // e.g.
01013       // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X
01014       if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) {
01015         FCmpInst::Predicate InvPred = FCI->getInversePredicate();
01016         Value *NewCond = Builder->CreateFCmp(InvPred, TrueVal, FalseVal,
01017                                              FCI->getName() + ".inv");
01018 
01019         return SelectInst::Create(NewCond, FalseVal, TrueVal,
01020                                   SI.getName() + ".p");
01021       }
01022 
01023       // NOTE: if we wanted to, this is where to detect MIN/MAX
01024     } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
01025       // Transform (X == Y) ? Y : X  -> X
01026       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
01027         // This is not safe in general for floating point:
01028         // consider X== -0, Y== +0.
01029         // It becomes safe if either operand is a nonzero constant.
01030         ConstantFP *CFPt, *CFPf;
01031         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
01032               !CFPt->getValueAPF().isZero()) ||
01033             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
01034              !CFPf->getValueAPF().isZero()))
01035           return ReplaceInstUsesWith(SI, FalseVal);
01036       }
01037       // Transform (X une Y) ? Y : X  -> Y
01038       if (FCI->getPredicate() == FCmpInst::FCMP_UNE) {
01039         // This is not safe in general for floating point:
01040         // consider X== -0, Y== +0.
01041         // It becomes safe if either operand is a nonzero constant.
01042         ConstantFP *CFPt, *CFPf;
01043         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
01044               !CFPt->getValueAPF().isZero()) ||
01045             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
01046              !CFPf->getValueAPF().isZero()))
01047           return ReplaceInstUsesWith(SI, TrueVal);
01048       }
01049 
01050       // Canonicalize to use ordered comparisons by swapping the select
01051       // operands.
01052       //
01053       // e.g.
01054       // (X ugt Y) ? X : Y -> (X ole Y) ? X : Y
01055       if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) {
01056         FCmpInst::Predicate InvPred = FCI->getInversePredicate();
01057         Value *NewCond = Builder->CreateFCmp(InvPred, FalseVal, TrueVal,
01058                                              FCI->getName() + ".inv");
01059 
01060         return SelectInst::Create(NewCond, FalseVal, TrueVal,
01061                                   SI.getName() + ".p");
01062       }
01063 
01064       // NOTE: if we wanted to, this is where to detect MIN/MAX
01065     }
01066     // NOTE: if we wanted to, this is where to detect ABS
01067   }
01068 
01069   // See if we are selecting two values based on a comparison of the two values.
01070   if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
01071     if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
01072       return Result;
01073 
01074   if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
01075     if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
01076       if (TI->hasOneUse() && FI->hasOneUse()) {
01077         Instruction *AddOp = nullptr, *SubOp = nullptr;
01078 
01079         // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
01080         if (TI->getOpcode() == FI->getOpcode())
01081           if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
01082             return IV;
01083 
01084         // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is
01085         // even legal for FP.
01086         if ((TI->getOpcode() == Instruction::Sub &&
01087              FI->getOpcode() == Instruction::Add) ||
01088             (TI->getOpcode() == Instruction::FSub &&
01089              FI->getOpcode() == Instruction::FAdd)) {
01090           AddOp = FI; SubOp = TI;
01091         } else if ((FI->getOpcode() == Instruction::Sub &&
01092                     TI->getOpcode() == Instruction::Add) ||
01093                    (FI->getOpcode() == Instruction::FSub &&
01094                     TI->getOpcode() == Instruction::FAdd)) {
01095           AddOp = TI; SubOp = FI;
01096         }
01097 
01098         if (AddOp) {
01099           Value *OtherAddOp = nullptr;
01100           if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
01101             OtherAddOp = AddOp->getOperand(1);
01102           } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
01103             OtherAddOp = AddOp->getOperand(0);
01104           }
01105 
01106           if (OtherAddOp) {
01107             // So at this point we know we have (Y -> OtherAddOp):
01108             //        select C, (add X, Y), (sub X, Z)
01109             Value *NegVal;  // Compute -Z
01110             if (SI.getType()->isFPOrFPVectorTy()) {
01111               NegVal = Builder->CreateFNeg(SubOp->getOperand(1));
01112               if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
01113                 FastMathFlags Flags = AddOp->getFastMathFlags();
01114                 Flags &= SubOp->getFastMathFlags();
01115                 NegInst->setFastMathFlags(Flags);
01116               }
01117             } else {
01118               NegVal = Builder->CreateNeg(SubOp->getOperand(1));
01119             }
01120 
01121             Value *NewTrueOp = OtherAddOp;
01122             Value *NewFalseOp = NegVal;
01123             if (AddOp != TI)
01124               std::swap(NewTrueOp, NewFalseOp);
01125             Value *NewSel =
01126               Builder->CreateSelect(CondVal, NewTrueOp,
01127                                     NewFalseOp, SI.getName() + ".p");
01128 
01129             if (SI.getType()->isFPOrFPVectorTy()) {
01130               Instruction *RI =
01131                 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
01132 
01133               FastMathFlags Flags = AddOp->getFastMathFlags();
01134               Flags &= SubOp->getFastMathFlags();
01135               RI->setFastMathFlags(Flags);
01136               return RI;
01137             } else
01138               return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
01139           }
01140         }
01141       }
01142 
01143   // See if we can fold the select into one of our operands.
01144   if (SI.getType()->isIntegerTy()) {
01145     if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
01146       return FoldI;
01147 
01148     Value *LHS, *RHS, *LHS2, *RHS2;
01149     SelectPatternFlavor SPF = MatchSelectPattern(&SI, LHS, RHS);
01150 
01151     // MAX(MAX(a, b), a) -> MAX(a, b)
01152     // MIN(MIN(a, b), a) -> MIN(a, b)
01153     // MAX(MIN(a, b), a) -> a
01154     // MIN(MAX(a, b), a) -> a
01155     if (SPF) {
01156       if (SelectPatternFlavor SPF2 = MatchSelectPattern(LHS, LHS2, RHS2))
01157         if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
01158                                           SI, SPF, RHS))
01159           return R;
01160       if (SelectPatternFlavor SPF2 = MatchSelectPattern(RHS, LHS2, RHS2))
01161         if (Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
01162                                           SI, SPF, LHS))
01163           return R;
01164     }
01165 
01166     // MAX(~a, ~b) -> ~MIN(a, b)
01167     if (SPF == SPF_SMAX || SPF == SPF_UMAX) {
01168       if (IsFreeToInvert(LHS, LHS->hasNUses(2)) &&
01169           IsFreeToInvert(RHS, RHS->hasNUses(2))) {
01170 
01171         // This transform adds a xor operation and that extra cost needs to be
01172         // justified.  We look for simplifications that will result from
01173         // applying this rule:
01174 
01175         bool Profitable =
01176             (LHS->hasNUses(2) && match(LHS, m_Not(m_Value()))) ||
01177             (RHS->hasNUses(2) && match(RHS, m_Not(m_Value()))) ||
01178             (SI.hasOneUse() && match(*SI.user_begin(), m_Not(m_Value())));
01179 
01180         if (Profitable) {
01181           Value *NewLHS = Builder->CreateNot(LHS);
01182           Value *NewRHS = Builder->CreateNot(RHS);
01183           Value *NewCmp = SPF == SPF_SMAX
01184                               ? Builder->CreateICmpSLT(NewLHS, NewRHS)
01185                               : Builder->CreateICmpULT(NewLHS, NewRHS);
01186           Value *NewSI =
01187               Builder->CreateNot(Builder->CreateSelect(NewCmp, NewLHS, NewRHS));
01188           return ReplaceInstUsesWith(SI, NewSI);
01189         }
01190       }
01191     }
01192 
01193     // TODO.
01194     // ABS(-X) -> ABS(X)
01195   }
01196 
01197   // See if we can fold the select into a phi node if the condition is a select.
01198   if (isa<PHINode>(SI.getCondition()))
01199     // The true/false values have to be live in the PHI predecessor's blocks.
01200     if (CanSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
01201         CanSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
01202       if (Instruction *NV = FoldOpIntoPhi(SI))
01203         return NV;
01204 
01205   if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
01206     if (TrueSI->getCondition()->getType() == CondVal->getType()) {
01207       // select(C, select(C, a, b), c) -> select(C, a, c)
01208       if (TrueSI->getCondition() == CondVal) {
01209         if (SI.getTrueValue() == TrueSI->getTrueValue())
01210           return nullptr;
01211         SI.setOperand(1, TrueSI->getTrueValue());
01212         return &SI;
01213       }
01214       // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b)
01215       // We choose this as normal form to enable folding on the And and shortening
01216       // paths for the values (this helps GetUnderlyingObjects() for example).
01217       if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) {
01218         Value *And = Builder->CreateAnd(CondVal, TrueSI->getCondition());
01219         SI.setOperand(0, And);
01220         SI.setOperand(1, TrueSI->getTrueValue());
01221         return &SI;
01222       }
01223     }
01224   }
01225   if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
01226     if (FalseSI->getCondition()->getType() == CondVal->getType()) {
01227       // select(C, a, select(C, b, c)) -> select(C, a, c)
01228       if (FalseSI->getCondition() == CondVal) {
01229         if (SI.getFalseValue() == FalseSI->getFalseValue())
01230           return nullptr;
01231         SI.setOperand(2, FalseSI->getFalseValue());
01232         return &SI;
01233       }
01234       // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b)
01235       if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) {
01236         Value *Or = Builder->CreateOr(CondVal, FalseSI->getCondition());
01237         SI.setOperand(0, Or);
01238         SI.setOperand(2, FalseSI->getFalseValue());
01239         return &SI;
01240       }
01241     }
01242   }
01243 
01244   if (BinaryOperator::isNot(CondVal)) {
01245     SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
01246     SI.setOperand(1, FalseVal);
01247     SI.setOperand(2, TrueVal);
01248     return &SI;
01249   }
01250 
01251   if (VectorType* VecTy = dyn_cast<VectorType>(SI.getType())) {
01252     unsigned VWidth = VecTy->getNumElements();
01253     APInt UndefElts(VWidth, 0);
01254     APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
01255     if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
01256       if (V != &SI)
01257         return ReplaceInstUsesWith(SI, V);
01258       return &SI;
01259     }
01260 
01261     if (isa<ConstantAggregateZero>(CondVal)) {
01262       return ReplaceInstUsesWith(SI, FalseVal);
01263     }
01264   }
01265 
01266   return nullptr;
01267 }