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 "InstCombine.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 == 0) return SPF_UNKNOWN;
00030 
00031   ICmpInst *ICI = dyn_cast<ICmpInst>(SI->getCondition());
00032   if (ICI == 0) return SPF_UNKNOWN;
00033 
00034   LHS = ICI->getOperand(0);
00035   RHS = ICI->getOperand(1);
00036 
00037   // (icmp X, Y) ? X : Y
00038   if (SI->getTrueValue() == ICI->getOperand(0) &&
00039       SI->getFalseValue() == ICI->getOperand(1)) {
00040     switch (ICI->getPredicate()) {
00041     default: return SPF_UNKNOWN; // Equality.
00042     case ICmpInst::ICMP_UGT:
00043     case ICmpInst::ICMP_UGE: return SPF_UMAX;
00044     case ICmpInst::ICMP_SGT:
00045     case ICmpInst::ICMP_SGE: return SPF_SMAX;
00046     case ICmpInst::ICMP_ULT:
00047     case ICmpInst::ICMP_ULE: return SPF_UMIN;
00048     case ICmpInst::ICMP_SLT:
00049     case ICmpInst::ICMP_SLE: return SPF_SMIN;
00050     }
00051   }
00052 
00053   // (icmp X, Y) ? Y : X
00054   if (SI->getTrueValue() == ICI->getOperand(1) &&
00055       SI->getFalseValue() == ICI->getOperand(0)) {
00056     switch (ICI->getPredicate()) {
00057       default: return SPF_UNKNOWN; // Equality.
00058       case ICmpInst::ICMP_UGT:
00059       case ICmpInst::ICMP_UGE: return SPF_UMIN;
00060       case ICmpInst::ICMP_SGT:
00061       case ICmpInst::ICMP_SGE: return SPF_SMIN;
00062       case ICmpInst::ICMP_ULT:
00063       case ICmpInst::ICMP_ULE: return SPF_UMAX;
00064       case ICmpInst::ICMP_SLT:
00065       case ICmpInst::ICMP_SLE: return SPF_SMAX;
00066     }
00067   }
00068 
00069   // TODO: (X > 4) ? X : 5   -->  (X >= 5) ? X : 5  -->  MAX(X, 5)
00070 
00071   return SPF_UNKNOWN;
00072 }
00073 
00074 
00075 /// GetSelectFoldableOperands - We want to turn code that looks like this:
00076 ///   %C = or %A, %B
00077 ///   %D = select %cond, %C, %A
00078 /// into:
00079 ///   %C = select %cond, %B, 0
00080 ///   %D = or %A, %C
00081 ///
00082 /// Assuming that the specified instruction is an operand to the select, return
00083 /// a bitmask indicating which operands of this instruction are foldable if they
00084 /// equal the other incoming value of the select.
00085 ///
00086 static unsigned GetSelectFoldableOperands(Instruction *I) {
00087   switch (I->getOpcode()) {
00088   case Instruction::Add:
00089   case Instruction::Mul:
00090   case Instruction::And:
00091   case Instruction::Or:
00092   case Instruction::Xor:
00093     return 3;              // Can fold through either operand.
00094   case Instruction::Sub:   // Can only fold on the amount subtracted.
00095   case Instruction::Shl:   // Can only fold on the shift amount.
00096   case Instruction::LShr:
00097   case Instruction::AShr:
00098     return 1;
00099   default:
00100     return 0;              // Cannot fold
00101   }
00102 }
00103 
00104 /// GetSelectFoldableConstant - For the same transformation as the previous
00105 /// function, return the identity constant that goes into the select.
00106 static Constant *GetSelectFoldableConstant(Instruction *I) {
00107   switch (I->getOpcode()) {
00108   default: llvm_unreachable("This cannot happen!");
00109   case Instruction::Add:
00110   case Instruction::Sub:
00111   case Instruction::Or:
00112   case Instruction::Xor:
00113   case Instruction::Shl:
00114   case Instruction::LShr:
00115   case Instruction::AShr:
00116     return Constant::getNullValue(I->getType());
00117   case Instruction::And:
00118     return Constant::getAllOnesValue(I->getType());
00119   case Instruction::Mul:
00120     return ConstantInt::get(I->getType(), 1);
00121   }
00122 }
00123 
00124 /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
00125 /// have the same opcode and only one use each.  Try to simplify this.
00126 Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
00127                                           Instruction *FI) {
00128   if (TI->getNumOperands() == 1) {
00129     // If this is a non-volatile load or a cast from the same type,
00130     // merge.
00131     if (TI->isCast()) {
00132       Type *FIOpndTy = FI->getOperand(0)->getType();
00133       if (TI->getOperand(0)->getType() != FIOpndTy)
00134         return 0;
00135       // The select condition may be a vector. We may only change the operand
00136       // type if the vector width remains the same (and matches the condition).
00137       Type *CondTy = SI.getCondition()->getType();
00138       if (CondTy->isVectorTy() && (!FIOpndTy->isVectorTy() ||
00139           CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()))
00140         return 0;
00141     } else {
00142       return 0;  // unknown unary op.
00143     }
00144 
00145     // Fold this by inserting a select from the input values.
00146     Value *NewSI = Builder->CreateSelect(SI.getCondition(), TI->getOperand(0),
00147                                          FI->getOperand(0), SI.getName()+".v");
00148     return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
00149                             TI->getType());
00150   }
00151 
00152   // Only handle binary operators here.
00153   if (!isa<BinaryOperator>(TI))
00154     return 0;
00155 
00156   // Figure out if the operations have any operands in common.
00157   Value *MatchOp, *OtherOpT, *OtherOpF;
00158   bool MatchIsOpZero;
00159   if (TI->getOperand(0) == FI->getOperand(0)) {
00160     MatchOp  = TI->getOperand(0);
00161     OtherOpT = TI->getOperand(1);
00162     OtherOpF = FI->getOperand(1);
00163     MatchIsOpZero = true;
00164   } else if (TI->getOperand(1) == FI->getOperand(1)) {
00165     MatchOp  = TI->getOperand(1);
00166     OtherOpT = TI->getOperand(0);
00167     OtherOpF = FI->getOperand(0);
00168     MatchIsOpZero = false;
00169   } else if (!TI->isCommutative()) {
00170     return 0;
00171   } else if (TI->getOperand(0) == FI->getOperand(1)) {
00172     MatchOp  = TI->getOperand(0);
00173     OtherOpT = TI->getOperand(1);
00174     OtherOpF = FI->getOperand(0);
00175     MatchIsOpZero = true;
00176   } else if (TI->getOperand(1) == FI->getOperand(0)) {
00177     MatchOp  = TI->getOperand(1);
00178     OtherOpT = TI->getOperand(0);
00179     OtherOpF = FI->getOperand(1);
00180     MatchIsOpZero = true;
00181   } else {
00182     return 0;
00183   }
00184 
00185   // If we reach here, they do have operations in common.
00186   Value *NewSI = Builder->CreateSelect(SI.getCondition(), OtherOpT,
00187                                        OtherOpF, SI.getName()+".v");
00188 
00189   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
00190     if (MatchIsOpZero)
00191       return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
00192     else
00193       return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
00194   }
00195   llvm_unreachable("Shouldn't get here");
00196 }
00197 
00198 static bool isSelect01(Constant *C1, Constant *C2) {
00199   ConstantInt *C1I = dyn_cast<ConstantInt>(C1);
00200   if (!C1I)
00201     return false;
00202   ConstantInt *C2I = dyn_cast<ConstantInt>(C2);
00203   if (!C2I)
00204     return false;
00205   if (!C1I->isZero() && !C2I->isZero()) // One side must be zero.
00206     return false;
00207   return C1I->isOne() || C1I->isAllOnesValue() ||
00208          C2I->isOne() || C2I->isAllOnesValue();
00209 }
00210 
00211 /// FoldSelectIntoOp - Try fold the select into one of the operands to
00212 /// facilitate further optimization.
00213 Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
00214                                             Value *FalseVal) {
00215   // See the comment above GetSelectFoldableOperands for a description of the
00216   // transformation we are doing here.
00217   if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
00218     if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
00219         !isa<Constant>(FalseVal)) {
00220       if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
00221         unsigned OpToFold = 0;
00222         if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
00223           OpToFold = 1;
00224         } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
00225           OpToFold = 2;
00226         }
00227 
00228         if (OpToFold) {
00229           Constant *C = GetSelectFoldableConstant(TVI);
00230           Value *OOp = TVI->getOperand(2-OpToFold);
00231           // Avoid creating select between 2 constants unless it's selecting
00232           // between 0, 1 and -1.
00233           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
00234             Value *NewSel = Builder->CreateSelect(SI.getCondition(), OOp, C);
00235             NewSel->takeName(TVI);
00236             BinaryOperator *TVI_BO = cast<BinaryOperator>(TVI);
00237             BinaryOperator *BO = BinaryOperator::Create(TVI_BO->getOpcode(),
00238                                                         FalseVal, NewSel);
00239             if (isa<PossiblyExactOperator>(BO))
00240               BO->setIsExact(TVI_BO->isExact());
00241             if (isa<OverflowingBinaryOperator>(BO)) {
00242               BO->setHasNoUnsignedWrap(TVI_BO->hasNoUnsignedWrap());
00243               BO->setHasNoSignedWrap(TVI_BO->hasNoSignedWrap());
00244             }
00245             return BO;
00246           }
00247         }
00248       }
00249     }
00250   }
00251 
00252   if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
00253     if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
00254         !isa<Constant>(TrueVal)) {
00255       if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
00256         unsigned OpToFold = 0;
00257         if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
00258           OpToFold = 1;
00259         } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
00260           OpToFold = 2;
00261         }
00262 
00263         if (OpToFold) {
00264           Constant *C = GetSelectFoldableConstant(FVI);
00265           Value *OOp = FVI->getOperand(2-OpToFold);
00266           // Avoid creating select between 2 constants unless it's selecting
00267           // between 0, 1 and -1.
00268           if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
00269             Value *NewSel = Builder->CreateSelect(SI.getCondition(), C, OOp);
00270             NewSel->takeName(FVI);
00271             BinaryOperator *FVI_BO = cast<BinaryOperator>(FVI);
00272             BinaryOperator *BO = BinaryOperator::Create(FVI_BO->getOpcode(),
00273                                                         TrueVal, NewSel);
00274             if (isa<PossiblyExactOperator>(BO))
00275               BO->setIsExact(FVI_BO->isExact());
00276             if (isa<OverflowingBinaryOperator>(BO)) {
00277               BO->setHasNoUnsignedWrap(FVI_BO->hasNoUnsignedWrap());
00278               BO->setHasNoSignedWrap(FVI_BO->hasNoSignedWrap());
00279             }
00280             return BO;
00281           }
00282         }
00283       }
00284     }
00285   }
00286 
00287   return 0;
00288 }
00289 
00290 /// SimplifyWithOpReplaced - See if V simplifies when its operand Op is
00291 /// replaced with RepOp.
00292 static Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
00293                                      const DataLayout *TD,
00294                                      const TargetLibraryInfo *TLI) {
00295   // Trivial replacement.
00296   if (V == Op)
00297     return RepOp;
00298 
00299   Instruction *I = dyn_cast<Instruction>(V);
00300   if (!I)
00301     return 0;
00302 
00303   // If this is a binary operator, try to simplify it with the replaced op.
00304   if (BinaryOperator *B = dyn_cast<BinaryOperator>(I)) {
00305     if (B->getOperand(0) == Op)
00306       return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), TD, TLI);
00307     if (B->getOperand(1) == Op)
00308       return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, TD, TLI);
00309   }
00310 
00311   // Same for CmpInsts.
00312   if (CmpInst *C = dyn_cast<CmpInst>(I)) {
00313     if (C->getOperand(0) == Op)
00314       return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), TD,
00315                              TLI);
00316     if (C->getOperand(1) == Op)
00317       return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, TD,
00318                              TLI);
00319   }
00320 
00321   // TODO: We could hand off more cases to instsimplify here.
00322 
00323   // If all operands are constant after substituting Op for RepOp then we can
00324   // constant fold the instruction.
00325   if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
00326     // Build a list of all constant operands.
00327     SmallVector<Constant*, 8> ConstOps;
00328     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
00329       if (I->getOperand(i) == Op)
00330         ConstOps.push_back(CRepOp);
00331       else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i)))
00332         ConstOps.push_back(COp);
00333       else
00334         break;
00335     }
00336 
00337     // All operands were constants, fold it.
00338     if (ConstOps.size() == I->getNumOperands()) {
00339       if (CmpInst *C = dyn_cast<CmpInst>(I))
00340         return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0],
00341                                                ConstOps[1], TD, TLI);
00342 
00343       if (LoadInst *LI = dyn_cast<LoadInst>(I))
00344         if (!LI->isVolatile())
00345           return ConstantFoldLoadFromConstPtr(ConstOps[0], TD);
00346 
00347       return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
00348                                       ConstOps, TD, TLI);
00349     }
00350   }
00351 
00352   return 0;
00353 }
00354 
00355 /// foldSelectICmpAndOr - We want to turn:
00356 ///   (select (icmp eq (and X, C1), 0), Y, (or Y, C2))
00357 /// into:
00358 ///   (or (shl (and X, C1), C3), y)
00359 /// iff:
00360 ///   C1 and C2 are both powers of 2
00361 /// where:
00362 ///   C3 = Log(C2) - Log(C1)
00363 ///
00364 /// This transform handles cases where:
00365 /// 1. The icmp predicate is inverted
00366 /// 2. The select operands are reversed
00367 /// 3. The magnitude of C2 and C1 are flipped
00368 static Value *foldSelectICmpAndOr(const SelectInst &SI, Value *TrueVal,
00369                                   Value *FalseVal,
00370                                   InstCombiner::BuilderTy *Builder) {
00371   const ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition());
00372   if (!IC || !IC->isEquality() || !SI.getType()->isIntegerTy())
00373     return 0;
00374 
00375   Value *CmpLHS = IC->getOperand(0);
00376   Value *CmpRHS = IC->getOperand(1);
00377 
00378   if (!match(CmpRHS, m_Zero()))
00379     return 0;
00380 
00381   Value *X;
00382   const APInt *C1;
00383   if (!match(CmpLHS, m_And(m_Value(X), m_Power2(C1))))
00384     return 0;
00385 
00386   const APInt *C2;
00387   bool OrOnTrueVal = false;
00388   bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)));
00389   if (!OrOnFalseVal)
00390     OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)));
00391 
00392   if (!OrOnFalseVal && !OrOnTrueVal)
00393     return 0;
00394 
00395   Value *V = CmpLHS;
00396   Value *Y = OrOnFalseVal ? TrueVal : FalseVal;
00397 
00398   unsigned C1Log = C1->logBase2();
00399   unsigned C2Log = C2->logBase2();
00400   if (C2Log > C1Log) {
00401     V = Builder->CreateZExtOrTrunc(V, Y->getType());
00402     V = Builder->CreateShl(V, C2Log - C1Log);
00403   } else if (C1Log > C2Log) {
00404     V = Builder->CreateLShr(V, C1Log - C2Log);
00405     V = Builder->CreateZExtOrTrunc(V, Y->getType());
00406   } else
00407     V = Builder->CreateZExtOrTrunc(V, Y->getType());
00408 
00409   ICmpInst::Predicate Pred = IC->getPredicate();
00410   if ((Pred == ICmpInst::ICMP_NE && OrOnFalseVal) ||
00411       (Pred == ICmpInst::ICMP_EQ && OrOnTrueVal))
00412     V = Builder->CreateXor(V, *C2);
00413 
00414   return Builder->CreateOr(V, Y);
00415 }
00416 
00417 /// visitSelectInstWithICmp - Visit a SelectInst that has an
00418 /// ICmpInst as its first operand.
00419 ///
00420 Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
00421                                                    ICmpInst *ICI) {
00422   bool Changed = false;
00423   ICmpInst::Predicate Pred = ICI->getPredicate();
00424   Value *CmpLHS = ICI->getOperand(0);
00425   Value *CmpRHS = ICI->getOperand(1);
00426   Value *TrueVal = SI.getTrueValue();
00427   Value *FalseVal = SI.getFalseValue();
00428 
00429   // Check cases where the comparison is with a constant that
00430   // can be adjusted to fit the min/max idiom. We may move or edit ICI
00431   // here, so make sure the select is the only user.
00432   if (ICI->hasOneUse())
00433     if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
00434       // X < MIN ? T : F  -->  F
00435       if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT)
00436           && CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
00437         return ReplaceInstUsesWith(SI, FalseVal);
00438       // X > MAX ? T : F  -->  F
00439       else if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_UGT)
00440                && CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
00441         return ReplaceInstUsesWith(SI, FalseVal);
00442       switch (Pred) {
00443       default: break;
00444       case ICmpInst::ICMP_ULT:
00445       case ICmpInst::ICMP_SLT:
00446       case ICmpInst::ICMP_UGT:
00447       case ICmpInst::ICMP_SGT: {
00448         // These transformations only work for selects over integers.
00449         IntegerType *SelectTy = dyn_cast<IntegerType>(SI.getType());
00450         if (!SelectTy)
00451           break;
00452 
00453         Constant *AdjustedRHS;
00454         if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
00455           AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1);
00456         else // (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT)
00457           AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() - 1);
00458 
00459         // X > C ? X : C+1  -->  X < C+1 ? C+1 : X
00460         // X < C ? X : C-1  -->  X > C-1 ? C-1 : X
00461         if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
00462             (CmpLHS == FalseVal && AdjustedRHS == TrueVal))
00463           ; // Nothing to do here. Values match without any sign/zero extension.
00464 
00465         // Types do not match. Instead of calculating this with mixed types
00466         // promote all to the larger type. This enables scalar evolution to
00467         // analyze this expression.
00468         else if (CmpRHS->getType()->getScalarSizeInBits()
00469                  < SelectTy->getBitWidth()) {
00470           Constant *sextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy);
00471 
00472           // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
00473           // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
00474           // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X
00475           // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X
00476           if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) &&
00477                 sextRHS == FalseVal) {
00478             CmpLHS = TrueVal;
00479             AdjustedRHS = sextRHS;
00480           } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) &&
00481                      sextRHS == TrueVal) {
00482             CmpLHS = FalseVal;
00483             AdjustedRHS = sextRHS;
00484           } else if (ICI->isUnsigned()) {
00485             Constant *zextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy);
00486             // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
00487             // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
00488             // zext + signed compare cannot be changed:
00489             //    0xff <s 0x00, but 0x00ff >s 0x0000
00490             if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) &&
00491                 zextRHS == FalseVal) {
00492               CmpLHS = TrueVal;
00493               AdjustedRHS = zextRHS;
00494             } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) &&
00495                        zextRHS == TrueVal) {
00496               CmpLHS = FalseVal;
00497               AdjustedRHS = zextRHS;
00498             } else
00499               break;
00500           } else
00501             break;
00502         } else
00503           break;
00504 
00505         Pred = ICmpInst::getSwappedPredicate(Pred);
00506         CmpRHS = AdjustedRHS;
00507         std::swap(FalseVal, TrueVal);
00508         ICI->setPredicate(Pred);
00509         ICI->setOperand(0, CmpLHS);
00510         ICI->setOperand(1, CmpRHS);
00511         SI.setOperand(1, TrueVal);
00512         SI.setOperand(2, FalseVal);
00513 
00514         // Move ICI instruction right before the select instruction. Otherwise
00515         // the sext/zext value may be defined after the ICI instruction uses it.
00516         ICI->moveBefore(&SI);
00517 
00518         Changed = true;
00519         break;
00520       }
00521       }
00522     }
00523 
00524   // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1
00525   // and       (X <s  0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1
00526   // FIXME: Type and constness constraints could be lifted, but we have to
00527   //        watch code size carefully. We should consider xor instead of
00528   //        sub/add when we decide to do that.
00529   if (IntegerType *Ty = dyn_cast<IntegerType>(CmpLHS->getType())) {
00530     if (TrueVal->getType() == Ty) {
00531       if (ConstantInt *Cmp = dyn_cast<ConstantInt>(CmpRHS)) {
00532         ConstantInt *C1 = NULL, *C2 = NULL;
00533         if (Pred == ICmpInst::ICMP_SGT && Cmp->isAllOnesValue()) {
00534           C1 = dyn_cast<ConstantInt>(TrueVal);
00535           C2 = dyn_cast<ConstantInt>(FalseVal);
00536         } else if (Pred == ICmpInst::ICMP_SLT && Cmp->isNullValue()) {
00537           C1 = dyn_cast<ConstantInt>(FalseVal);
00538           C2 = dyn_cast<ConstantInt>(TrueVal);
00539         }
00540         if (C1 && C2) {
00541           // This shift results in either -1 or 0.
00542           Value *AShr = Builder->CreateAShr(CmpLHS, Ty->getBitWidth()-1);
00543 
00544           // Check if we can express the operation with a single or.
00545           if (C2->isAllOnesValue())
00546             return ReplaceInstUsesWith(SI, Builder->CreateOr(AShr, C1));
00547 
00548           Value *And = Builder->CreateAnd(AShr, C2->getValue()-C1->getValue());
00549           return ReplaceInstUsesWith(SI, Builder->CreateAdd(And, C1));
00550         }
00551       }
00552     }
00553   }
00554 
00555   // If we have an equality comparison then we know the value in one of the
00556   // arms of the select. See if substituting this value into the arm and
00557   // simplifying the result yields the same value as the other arm.
00558   if (Pred == ICmpInst::ICMP_EQ) {
00559     if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, DL, TLI) == TrueVal ||
00560         SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, DL, TLI) == TrueVal)
00561       return ReplaceInstUsesWith(SI, FalseVal);
00562     if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, DL, TLI) == FalseVal ||
00563         SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, DL, TLI) == FalseVal)
00564       return ReplaceInstUsesWith(SI, FalseVal);
00565   } else if (Pred == ICmpInst::ICMP_NE) {
00566     if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, DL, TLI) == FalseVal ||
00567         SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, DL, TLI) == FalseVal)
00568       return ReplaceInstUsesWith(SI, TrueVal);
00569     if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, DL, TLI) == TrueVal ||
00570         SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, DL, TLI) == TrueVal)
00571       return ReplaceInstUsesWith(SI, TrueVal);
00572   }
00573 
00574   // NOTE: if we wanted to, this is where to detect integer MIN/MAX
00575 
00576   if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
00577     if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
00578       // Transform (X == C) ? X : Y -> (X == C) ? C : Y
00579       SI.setOperand(1, CmpRHS);
00580       Changed = true;
00581     } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
00582       // Transform (X != C) ? Y : X -> (X != C) ? Y : C
00583       SI.setOperand(2, CmpRHS);
00584       Changed = true;
00585     }
00586   }
00587 
00588   if (Value *V = foldSelectICmpAndOr(SI, TrueVal, FalseVal, Builder))
00589     return ReplaceInstUsesWith(SI, V);
00590 
00591   return Changed ? &SI : 0;
00592 }
00593 
00594 
00595 /// CanSelectOperandBeMappingIntoPredBlock - SI is a select whose condition is a
00596 /// PHI node (but the two may be in different blocks).  See if the true/false
00597 /// values (V) are live in all of the predecessor blocks of the PHI.  For
00598 /// example, cases like this cannot be mapped:
00599 ///
00600 ///   X = phi [ C1, BB1], [C2, BB2]
00601 ///   Y = add
00602 ///   Z = select X, Y, 0
00603 ///
00604 /// because Y is not live in BB1/BB2.
00605 ///
00606 static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V,
00607                                                    const SelectInst &SI) {
00608   // If the value is a non-instruction value like a constant or argument, it
00609   // can always be mapped.
00610   const Instruction *I = dyn_cast<Instruction>(V);
00611   if (I == 0) return true;
00612 
00613   // If V is a PHI node defined in the same block as the condition PHI, we can
00614   // map the arguments.
00615   const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
00616 
00617   if (const PHINode *VP = dyn_cast<PHINode>(I))
00618     if (VP->getParent() == CondPHI->getParent())
00619       return true;
00620 
00621   // Otherwise, if the PHI and select are defined in the same block and if V is
00622   // defined in a different block, then we can transform it.
00623   if (SI.getParent() == CondPHI->getParent() &&
00624       I->getParent() != CondPHI->getParent())
00625     return true;
00626 
00627   // Otherwise we have a 'hard' case and we can't tell without doing more
00628   // detailed dominator based analysis, punt.
00629   return false;
00630 }
00631 
00632 /// FoldSPFofSPF - We have an SPF (e.g. a min or max) of an SPF of the form:
00633 ///   SPF2(SPF1(A, B), C)
00634 Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner,
00635                                         SelectPatternFlavor SPF1,
00636                                         Value *A, Value *B,
00637                                         Instruction &Outer,
00638                                         SelectPatternFlavor SPF2, Value *C) {
00639   if (C == A || C == B) {
00640     // MAX(MAX(A, B), B) -> MAX(A, B)
00641     // MIN(MIN(a, b), a) -> MIN(a, b)
00642     if (SPF1 == SPF2)
00643       return ReplaceInstUsesWith(Outer, Inner);
00644 
00645     // MAX(MIN(a, b), a) -> a
00646     // MIN(MAX(a, b), a) -> a
00647     if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
00648         (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
00649         (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
00650         (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
00651       return ReplaceInstUsesWith(Outer, C);
00652   }
00653 
00654   // TODO: MIN(MIN(A, 23), 97)
00655   return 0;
00656 }
00657 
00658 
00659 /// foldSelectICmpAnd - If one of the constants is zero (we know they can't
00660 /// both be) and we have an icmp instruction with zero, and we have an 'and'
00661 /// with the non-constant value and a power of two we can turn the select
00662 /// into a shift on the result of the 'and'.
00663 static Value *foldSelectICmpAnd(const SelectInst &SI, ConstantInt *TrueVal,
00664                                 ConstantInt *FalseVal,
00665                                 InstCombiner::BuilderTy *Builder) {
00666   const ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition());
00667   if (!IC || !IC->isEquality() || !SI.getType()->isIntegerTy())
00668     return 0;
00669 
00670   if (!match(IC->getOperand(1), m_Zero()))
00671     return 0;
00672 
00673   ConstantInt *AndRHS;
00674   Value *LHS = IC->getOperand(0);
00675   if (!match(LHS, m_And(m_Value(), m_ConstantInt(AndRHS))))
00676     return 0;
00677 
00678   // If both select arms are non-zero see if we have a select of the form
00679   // 'x ? 2^n + C : C'. Then we can offset both arms by C, use the logic
00680   // for 'x ? 2^n : 0' and fix the thing up at the end.
00681   ConstantInt *Offset = 0;
00682   if (!TrueVal->isZero() && !FalseVal->isZero()) {
00683     if ((TrueVal->getValue() - FalseVal->getValue()).isPowerOf2())
00684       Offset = FalseVal;
00685     else if ((FalseVal->getValue() - TrueVal->getValue()).isPowerOf2())
00686       Offset = TrueVal;
00687     else
00688       return 0;
00689 
00690     // Adjust TrueVal and FalseVal to the offset.
00691     TrueVal = ConstantInt::get(Builder->getContext(),
00692                                TrueVal->getValue() - Offset->getValue());
00693     FalseVal = ConstantInt::get(Builder->getContext(),
00694                                 FalseVal->getValue() - Offset->getValue());
00695   }
00696 
00697   // Make sure the mask in the 'and' and one of the select arms is a power of 2.
00698   if (!AndRHS->getValue().isPowerOf2() ||
00699       (!TrueVal->getValue().isPowerOf2() &&
00700        !FalseVal->getValue().isPowerOf2()))
00701     return 0;
00702 
00703   // Determine which shift is needed to transform result of the 'and' into the
00704   // desired result.
00705   ConstantInt *ValC = !TrueVal->isZero() ? TrueVal : FalseVal;
00706   unsigned ValZeros = ValC->getValue().logBase2();
00707   unsigned AndZeros = AndRHS->getValue().logBase2();
00708 
00709   // If types don't match we can still convert the select by introducing a zext
00710   // or a trunc of the 'and'. The trunc case requires that all of the truncated
00711   // bits are zero, we can figure that out by looking at the 'and' mask.
00712   if (AndZeros >= ValC->getBitWidth())
00713     return 0;
00714 
00715   Value *V = Builder->CreateZExtOrTrunc(LHS, SI.getType());
00716   if (ValZeros > AndZeros)
00717     V = Builder->CreateShl(V, ValZeros - AndZeros);
00718   else if (ValZeros < AndZeros)
00719     V = Builder->CreateLShr(V, AndZeros - ValZeros);
00720 
00721   // Okay, now we know that everything is set up, we just don't know whether we
00722   // have a icmp_ne or icmp_eq and whether the true or false val is the zero.
00723   bool ShouldNotVal = !TrueVal->isZero();
00724   ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
00725   if (ShouldNotVal)
00726     V = Builder->CreateXor(V, ValC);
00727 
00728   // Apply an offset if needed.
00729   if (Offset)
00730     V = Builder->CreateAdd(V, Offset);
00731   return V;
00732 }
00733 
00734 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
00735   Value *CondVal = SI.getCondition();
00736   Value *TrueVal = SI.getTrueValue();
00737   Value *FalseVal = SI.getFalseValue();
00738 
00739   if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, DL))
00740     return ReplaceInstUsesWith(SI, V);
00741 
00742   if (SI.getType()->isIntegerTy(1)) {
00743     if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
00744       if (C->getZExtValue()) {
00745         // Change: A = select B, true, C --> A = or B, C
00746         return BinaryOperator::CreateOr(CondVal, FalseVal);
00747       }
00748       // Change: A = select B, false, C --> A = and !B, C
00749       Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00750       return BinaryOperator::CreateAnd(NotCond, FalseVal);
00751     }
00752     if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
00753       if (C->getZExtValue() == false) {
00754         // Change: A = select B, C, false --> A = and B, C
00755         return BinaryOperator::CreateAnd(CondVal, TrueVal);
00756       }
00757       // Change: A = select B, C, true --> A = or !B, C
00758       Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00759       return BinaryOperator::CreateOr(NotCond, TrueVal);
00760     }
00761 
00762     // select a, b, a  -> a&b
00763     // select a, a, b  -> a|b
00764     if (CondVal == TrueVal)
00765       return BinaryOperator::CreateOr(CondVal, FalseVal);
00766     if (CondVal == FalseVal)
00767       return BinaryOperator::CreateAnd(CondVal, TrueVal);
00768 
00769     // select a, ~a, b -> (~a)&b
00770     // select a, b, ~a -> (~a)|b
00771     if (match(TrueVal, m_Not(m_Specific(CondVal))))
00772       return BinaryOperator::CreateAnd(TrueVal, FalseVal);
00773     if (match(FalseVal, m_Not(m_Specific(CondVal))))
00774       return BinaryOperator::CreateOr(TrueVal, FalseVal);
00775   }
00776 
00777   // Selecting between two integer constants?
00778   if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
00779     if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
00780       // select C, 1, 0 -> zext C to int
00781       if (FalseValC->isZero() && TrueValC->getValue() == 1)
00782         return new ZExtInst(CondVal, SI.getType());
00783 
00784       // select C, -1, 0 -> sext C to int
00785       if (FalseValC->isZero() && TrueValC->isAllOnesValue())
00786         return new SExtInst(CondVal, SI.getType());
00787 
00788       // select C, 0, 1 -> zext !C to int
00789       if (TrueValC->isZero() && FalseValC->getValue() == 1) {
00790         Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00791         return new ZExtInst(NotCond, SI.getType());
00792       }
00793 
00794       // select C, 0, -1 -> sext !C to int
00795       if (TrueValC->isZero() && FalseValC->isAllOnesValue()) {
00796         Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
00797         return new SExtInst(NotCond, SI.getType());
00798       }
00799 
00800       if (Value *V = foldSelectICmpAnd(SI, TrueValC, FalseValC, Builder))
00801         return ReplaceInstUsesWith(SI, V);
00802     }
00803 
00804   // See if we are selecting two values based on a comparison of the two values.
00805   if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
00806     if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
00807       // Transform (X == Y) ? X : Y  -> Y
00808       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
00809         // This is not safe in general for floating point:
00810         // consider X== -0, Y== +0.
00811         // It becomes safe if either operand is a nonzero constant.
00812         ConstantFP *CFPt, *CFPf;
00813         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
00814               !CFPt->getValueAPF().isZero()) ||
00815             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
00816              !CFPf->getValueAPF().isZero()))
00817         return ReplaceInstUsesWith(SI, FalseVal);
00818       }
00819       // Transform (X une Y) ? X : Y  -> X
00820       if (FCI->getPredicate() == FCmpInst::FCMP_UNE) {
00821         // This is not safe in general for floating point:
00822         // consider X== -0, Y== +0.
00823         // It becomes safe if either operand is a nonzero constant.
00824         ConstantFP *CFPt, *CFPf;
00825         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
00826               !CFPt->getValueAPF().isZero()) ||
00827             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
00828              !CFPf->getValueAPF().isZero()))
00829         return ReplaceInstUsesWith(SI, TrueVal);
00830       }
00831       // NOTE: if we wanted to, this is where to detect MIN/MAX
00832 
00833     } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
00834       // Transform (X == Y) ? Y : X  -> X
00835       if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
00836         // This is not safe in general for floating point:
00837         // consider X== -0, Y== +0.
00838         // It becomes safe if either operand is a nonzero constant.
00839         ConstantFP *CFPt, *CFPf;
00840         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
00841               !CFPt->getValueAPF().isZero()) ||
00842             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
00843              !CFPf->getValueAPF().isZero()))
00844           return ReplaceInstUsesWith(SI, FalseVal);
00845       }
00846       // Transform (X une Y) ? Y : X  -> Y
00847       if (FCI->getPredicate() == FCmpInst::FCMP_UNE) {
00848         // This is not safe in general for floating point:
00849         // consider X== -0, Y== +0.
00850         // It becomes safe if either operand is a nonzero constant.
00851         ConstantFP *CFPt, *CFPf;
00852         if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
00853               !CFPt->getValueAPF().isZero()) ||
00854             ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
00855              !CFPf->getValueAPF().isZero()))
00856           return ReplaceInstUsesWith(SI, TrueVal);
00857       }
00858       // NOTE: if we wanted to, this is where to detect MIN/MAX
00859     }
00860     // NOTE: if we wanted to, this is where to detect ABS
00861   }
00862 
00863   // See if we are selecting two values based on a comparison of the two values.
00864   if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
00865     if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
00866       return Result;
00867 
00868   if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
00869     if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
00870       if (TI->hasOneUse() && FI->hasOneUse()) {
00871         Instruction *AddOp = 0, *SubOp = 0;
00872 
00873         // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
00874         if (TI->getOpcode() == FI->getOpcode())
00875           if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
00876             return IV;
00877 
00878         // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is
00879         // even legal for FP.
00880         if ((TI->getOpcode() == Instruction::Sub &&
00881              FI->getOpcode() == Instruction::Add) ||
00882             (TI->getOpcode() == Instruction::FSub &&
00883              FI->getOpcode() == Instruction::FAdd)) {
00884           AddOp = FI; SubOp = TI;
00885         } else if ((FI->getOpcode() == Instruction::Sub &&
00886                     TI->getOpcode() == Instruction::Add) ||
00887                    (FI->getOpcode() == Instruction::FSub &&
00888                     TI->getOpcode() == Instruction::FAdd)) {
00889           AddOp = TI; SubOp = FI;
00890         }
00891 
00892         if (AddOp) {
00893           Value *OtherAddOp = 0;
00894           if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
00895             OtherAddOp = AddOp->getOperand(1);
00896           } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
00897             OtherAddOp = AddOp->getOperand(0);
00898           }
00899 
00900           if (OtherAddOp) {
00901             // So at this point we know we have (Y -> OtherAddOp):
00902             //        select C, (add X, Y), (sub X, Z)
00903             Value *NegVal;  // Compute -Z
00904             if (SI.getType()->isFPOrFPVectorTy()) {
00905               NegVal = Builder->CreateFNeg(SubOp->getOperand(1));
00906               if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
00907                 FastMathFlags Flags = AddOp->getFastMathFlags();
00908                 Flags &= SubOp->getFastMathFlags();
00909                 NegInst->setFastMathFlags(Flags);
00910               }
00911             } else {
00912               NegVal = Builder->CreateNeg(SubOp->getOperand(1));
00913             }
00914 
00915             Value *NewTrueOp = OtherAddOp;
00916             Value *NewFalseOp = NegVal;
00917             if (AddOp != TI)
00918               std::swap(NewTrueOp, NewFalseOp);
00919             Value *NewSel =
00920               Builder->CreateSelect(CondVal, NewTrueOp,
00921                                     NewFalseOp, SI.getName() + ".p");
00922 
00923             if (SI.getType()->isFPOrFPVectorTy()) {
00924               Instruction *RI =
00925                 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
00926 
00927               FastMathFlags Flags = AddOp->getFastMathFlags();
00928               Flags &= SubOp->getFastMathFlags();
00929               RI->setFastMathFlags(Flags);
00930               return RI;
00931             } else
00932               return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
00933           }
00934         }
00935       }
00936 
00937   // See if we can fold the select into one of our operands.
00938   if (SI.getType()->isIntegerTy()) {
00939     if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
00940       return FoldI;
00941 
00942     // MAX(MAX(a, b), a) -> MAX(a, b)
00943     // MIN(MIN(a, b), a) -> MIN(a, b)
00944     // MAX(MIN(a, b), a) -> a
00945     // MIN(MAX(a, b), a) -> a
00946     Value *LHS, *RHS, *LHS2, *RHS2;
00947     if (SelectPatternFlavor SPF = MatchSelectPattern(&SI, LHS, RHS)) {
00948       if (SelectPatternFlavor SPF2 = MatchSelectPattern(LHS, LHS2, RHS2))
00949         if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
00950                                           SI, SPF, RHS))
00951           return R;
00952       if (SelectPatternFlavor SPF2 = MatchSelectPattern(RHS, LHS2, RHS2))
00953         if (Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
00954                                           SI, SPF, LHS))
00955           return R;
00956     }
00957 
00958     // TODO.
00959     // ABS(-X) -> ABS(X)
00960     // ABS(ABS(X)) -> ABS(X)
00961   }
00962 
00963   // See if we can fold the select into a phi node if the condition is a select.
00964   if (isa<PHINode>(SI.getCondition()))
00965     // The true/false values have to be live in the PHI predecessor's blocks.
00966     if (CanSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
00967         CanSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
00968       if (Instruction *NV = FoldOpIntoPhi(SI))
00969         return NV;
00970 
00971   if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
00972     if (TrueSI->getCondition() == CondVal) {
00973       if (SI.getTrueValue() == TrueSI->getTrueValue())
00974         return 0;
00975       SI.setOperand(1, TrueSI->getTrueValue());
00976       return &SI;
00977     }
00978   }
00979   if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
00980     if (FalseSI->getCondition() == CondVal) {
00981       if (SI.getFalseValue() == FalseSI->getFalseValue())
00982         return 0;
00983       SI.setOperand(2, FalseSI->getFalseValue());
00984       return &SI;
00985     }
00986   }
00987 
00988   if (BinaryOperator::isNot(CondVal)) {
00989     SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
00990     SI.setOperand(1, FalseVal);
00991     SI.setOperand(2, TrueVal);
00992     return &SI;
00993   }
00994 
00995   if (VectorType* VecTy = dyn_cast<VectorType>(SI.getType())) {
00996     unsigned VWidth = VecTy->getNumElements();
00997     APInt UndefElts(VWidth, 0);
00998     APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
00999     if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
01000       if (V != &SI)
01001         return ReplaceInstUsesWith(SI, V);
01002       return &SI;
01003     }
01004 
01005     if (isa<ConstantAggregateZero>(CondVal)) {
01006       return ReplaceInstUsesWith(SI, FalseVal);
01007     }
01008   }
01009 
01010   return 0;
01011 }