Home | History | Annotate | Download | only in Analysis
      1 //===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements routines for folding instructions into simpler forms
     11 // that do not require creating new instructions.  This does constant folding
     12 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
     13 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
     14 // ("and i32 %x, %x" -> "%x").  All operands are assumed to have already been
     15 // simplified: This is usually true and assuming it simplifies the logic (if
     16 // they have not been simplified then results are correct but maybe suboptimal).
     17 //
     18 //===----------------------------------------------------------------------===//
     19 
     20 #define DEBUG_TYPE "instsimplify"
     21 #include "llvm/Analysis/InstructionSimplify.h"
     22 #include "llvm/ADT/SetVector.h"
     23 #include "llvm/ADT/Statistic.h"
     24 #include "llvm/Analysis/ConstantFolding.h"
     25 #include "llvm/Analysis/Dominators.h"
     26 #include "llvm/Analysis/ValueTracking.h"
     27 #include "llvm/Analysis/MemoryBuiltins.h"
     28 #include "llvm/IR/DataLayout.h"
     29 #include "llvm/IR/GlobalAlias.h"
     30 #include "llvm/IR/Operator.h"
     31 #include "llvm/Support/ConstantRange.h"
     32 #include "llvm/Support/GetElementPtrTypeIterator.h"
     33 #include "llvm/Support/PatternMatch.h"
     34 #include "llvm/Support/ValueHandle.h"
     35 using namespace llvm;
     36 using namespace llvm::PatternMatch;
     37 
     38 enum { RecursionLimit = 3 };
     39 
     40 STATISTIC(NumExpand,  "Number of expansions");
     41 STATISTIC(NumFactor , "Number of factorizations");
     42 STATISTIC(NumReassoc, "Number of reassociations");
     43 
     44 struct Query {
     45   const DataLayout *TD;
     46   const TargetLibraryInfo *TLI;
     47   const DominatorTree *DT;
     48 
     49   Query(const DataLayout *td, const TargetLibraryInfo *tli,
     50         const DominatorTree *dt) : TD(td), TLI(tli), DT(dt) {}
     51 };
     52 
     53 static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned);
     54 static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &,
     55                             unsigned);
     56 static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &,
     57                               unsigned);
     58 static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned);
     59 static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned);
     60 static Value *SimplifyTruncInst(Value *, Type *, const Query &, unsigned);
     61 
     62 /// getFalse - For a boolean type, or a vector of boolean type, return false, or
     63 /// a vector with every element false, as appropriate for the type.
     64 static Constant *getFalse(Type *Ty) {
     65   assert(Ty->getScalarType()->isIntegerTy(1) &&
     66          "Expected i1 type or a vector of i1!");
     67   return Constant::getNullValue(Ty);
     68 }
     69 
     70 /// getTrue - For a boolean type, or a vector of boolean type, return true, or
     71 /// a vector with every element true, as appropriate for the type.
     72 static Constant *getTrue(Type *Ty) {
     73   assert(Ty->getScalarType()->isIntegerTy(1) &&
     74          "Expected i1 type or a vector of i1!");
     75   return Constant::getAllOnesValue(Ty);
     76 }
     77 
     78 /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
     79 static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
     80                           Value *RHS) {
     81   CmpInst *Cmp = dyn_cast<CmpInst>(V);
     82   if (!Cmp)
     83     return false;
     84   CmpInst::Predicate CPred = Cmp->getPredicate();
     85   Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
     86   if (CPred == Pred && CLHS == LHS && CRHS == RHS)
     87     return true;
     88   return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
     89     CRHS == LHS;
     90 }
     91 
     92 /// ValueDominatesPHI - Does the given value dominate the specified phi node?
     93 static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
     94   Instruction *I = dyn_cast<Instruction>(V);
     95   if (!I)
     96     // Arguments and constants dominate all instructions.
     97     return true;
     98 
     99   // If we are processing instructions (and/or basic blocks) that have not been
    100   // fully added to a function, the parent nodes may still be null. Simply
    101   // return the conservative answer in these cases.
    102   if (!I->getParent() || !P->getParent() || !I->getParent()->getParent())
    103     return false;
    104 
    105   // If we have a DominatorTree then do a precise test.
    106   if (DT) {
    107     if (!DT->isReachableFromEntry(P->getParent()))
    108       return true;
    109     if (!DT->isReachableFromEntry(I->getParent()))
    110       return false;
    111     return DT->dominates(I, P);
    112   }
    113 
    114   // Otherwise, if the instruction is in the entry block, and is not an invoke,
    115   // then it obviously dominates all phi nodes.
    116   if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() &&
    117       !isa<InvokeInst>(I))
    118     return true;
    119 
    120   return false;
    121 }
    122 
    123 /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning
    124 /// it into "(A op B) op' (A op C)".  Here "op" is given by Opcode and "op'" is
    125 /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
    126 /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
    127 /// Returns the simplified value, or null if no simplification was performed.
    128 static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS,
    129                           unsigned OpcToExpand, const Query &Q,
    130                           unsigned MaxRecurse) {
    131   Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand;
    132   // Recursion is always used, so bail out at once if we already hit the limit.
    133   if (!MaxRecurse--)
    134     return 0;
    135 
    136   // Check whether the expression has the form "(A op' B) op C".
    137   if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
    138     if (Op0->getOpcode() == OpcodeToExpand) {
    139       // It does!  Try turning it into "(A op C) op' (B op C)".
    140       Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
    141       // Do "A op C" and "B op C" both simplify?
    142       if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse))
    143         if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
    144           // They do! Return "L op' R" if it simplifies or is already available.
    145           // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
    146           if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand)
    147                                      && L == B && R == A)) {
    148             ++NumExpand;
    149             return LHS;
    150           }
    151           // Otherwise return "L op' R" if it simplifies.
    152           if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) {
    153             ++NumExpand;
    154             return V;
    155           }
    156         }
    157     }
    158 
    159   // Check whether the expression has the form "A op (B op' C)".
    160   if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
    161     if (Op1->getOpcode() == OpcodeToExpand) {
    162       // It does!  Try turning it into "(A op B) op' (A op C)".
    163       Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
    164       // Do "A op B" and "A op C" both simplify?
    165       if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse))
    166         if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) {
    167           // They do! Return "L op' R" if it simplifies or is already available.
    168           // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
    169           if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand)
    170                                      && L == C && R == B)) {
    171             ++NumExpand;
    172             return RHS;
    173           }
    174           // Otherwise return "L op' R" if it simplifies.
    175           if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) {
    176             ++NumExpand;
    177             return V;
    178           }
    179         }
    180     }
    181 
    182   return 0;
    183 }
    184 
    185 /// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term
    186 /// using the operation OpCodeToExtract.  For example, when Opcode is Add and
    187 /// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)".
    188 /// Returns the simplified value, or null if no simplification was performed.
    189 static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS,
    190                              unsigned OpcToExtract, const Query &Q,
    191                              unsigned MaxRecurse) {
    192   Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract;
    193   // Recursion is always used, so bail out at once if we already hit the limit.
    194   if (!MaxRecurse--)
    195     return 0;
    196 
    197   BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
    198   BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
    199 
    200   if (!Op0 || Op0->getOpcode() != OpcodeToExtract ||
    201       !Op1 || Op1->getOpcode() != OpcodeToExtract)
    202     return 0;
    203 
    204   // The expression has the form "(A op' B) op (C op' D)".
    205   Value *A = Op0->getOperand(0), *B = Op0->getOperand(1);
    206   Value *C = Op1->getOperand(0), *D = Op1->getOperand(1);
    207 
    208   // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)".
    209   // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
    210   // commutative case, "(A op' B) op (C op' A)"?
    211   if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) {
    212     Value *DD = A == C ? D : C;
    213     // Form "A op' (B op DD)" if it simplifies completely.
    214     // Does "B op DD" simplify?
    215     if (Value *V = SimplifyBinOp(Opcode, B, DD, Q, MaxRecurse)) {
    216       // It does!  Return "A op' V" if it simplifies or is already available.
    217       // If V equals B then "A op' V" is just the LHS.  If V equals DD then
    218       // "A op' V" is just the RHS.
    219       if (V == B || V == DD) {
    220         ++NumFactor;
    221         return V == B ? LHS : RHS;
    222       }
    223       // Otherwise return "A op' V" if it simplifies.
    224       if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, Q, MaxRecurse)) {
    225         ++NumFactor;
    226         return W;
    227       }
    228     }
    229   }
    230 
    231   // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)".
    232   // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
    233   // commutative case, "(A op' B) op (B op' D)"?
    234   if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) {
    235     Value *CC = B == D ? C : D;
    236     // Form "(A op CC) op' B" if it simplifies completely..
    237     // Does "A op CC" simplify?
    238     if (Value *V = SimplifyBinOp(Opcode, A, CC, Q, MaxRecurse)) {
    239       // It does!  Return "V op' B" if it simplifies or is already available.
    240       // If V equals A then "V op' B" is just the LHS.  If V equals CC then
    241       // "V op' B" is just the RHS.
    242       if (V == A || V == CC) {
    243         ++NumFactor;
    244         return V == A ? LHS : RHS;
    245       }
    246       // Otherwise return "V op' B" if it simplifies.
    247       if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, Q, MaxRecurse)) {
    248         ++NumFactor;
    249         return W;
    250       }
    251     }
    252   }
    253 
    254   return 0;
    255 }
    256 
    257 /// SimplifyAssociativeBinOp - Generic simplifications for associative binary
    258 /// operations.  Returns the simpler value, or null if none was found.
    259 static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS,
    260                                        const Query &Q, unsigned MaxRecurse) {
    261   Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc;
    262   assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
    263 
    264   // Recursion is always used, so bail out at once if we already hit the limit.
    265   if (!MaxRecurse--)
    266     return 0;
    267 
    268   BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
    269   BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
    270 
    271   // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
    272   if (Op0 && Op0->getOpcode() == Opcode) {
    273     Value *A = Op0->getOperand(0);
    274     Value *B = Op0->getOperand(1);
    275     Value *C = RHS;
    276 
    277     // Does "B op C" simplify?
    278     if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
    279       // It does!  Return "A op V" if it simplifies or is already available.
    280       // If V equals B then "A op V" is just the LHS.
    281       if (V == B) return LHS;
    282       // Otherwise return "A op V" if it simplifies.
    283       if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
    284         ++NumReassoc;
    285         return W;
    286       }
    287     }
    288   }
    289 
    290   // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
    291   if (Op1 && Op1->getOpcode() == Opcode) {
    292     Value *A = LHS;
    293     Value *B = Op1->getOperand(0);
    294     Value *C = Op1->getOperand(1);
    295 
    296     // Does "A op B" simplify?
    297     if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
    298       // It does!  Return "V op C" if it simplifies or is already available.
    299       // If V equals B then "V op C" is just the RHS.
    300       if (V == B) return RHS;
    301       // Otherwise return "V op C" if it simplifies.
    302       if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
    303         ++NumReassoc;
    304         return W;
    305       }
    306     }
    307   }
    308 
    309   // The remaining transforms require commutativity as well as associativity.
    310   if (!Instruction::isCommutative(Opcode))
    311     return 0;
    312 
    313   // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
    314   if (Op0 && Op0->getOpcode() == Opcode) {
    315     Value *A = Op0->getOperand(0);
    316     Value *B = Op0->getOperand(1);
    317     Value *C = RHS;
    318 
    319     // Does "C op A" simplify?
    320     if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
    321       // It does!  Return "V op B" if it simplifies or is already available.
    322       // If V equals A then "V op B" is just the LHS.
    323       if (V == A) return LHS;
    324       // Otherwise return "V op B" if it simplifies.
    325       if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
    326         ++NumReassoc;
    327         return W;
    328       }
    329     }
    330   }
    331 
    332   // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
    333   if (Op1 && Op1->getOpcode() == Opcode) {
    334     Value *A = LHS;
    335     Value *B = Op1->getOperand(0);
    336     Value *C = Op1->getOperand(1);
    337 
    338     // Does "C op A" simplify?
    339     if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
    340       // It does!  Return "B op V" if it simplifies or is already available.
    341       // If V equals C then "B op V" is just the RHS.
    342       if (V == C) return RHS;
    343       // Otherwise return "B op V" if it simplifies.
    344       if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
    345         ++NumReassoc;
    346         return W;
    347       }
    348     }
    349   }
    350 
    351   return 0;
    352 }
    353 
    354 /// ThreadBinOpOverSelect - In the case of a binary operation with a select
    355 /// instruction as an operand, try to simplify the binop by seeing whether
    356 /// evaluating it on both branches of the select results in the same value.
    357 /// Returns the common value if so, otherwise returns null.
    358 static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
    359                                     const Query &Q, unsigned MaxRecurse) {
    360   // Recursion is always used, so bail out at once if we already hit the limit.
    361   if (!MaxRecurse--)
    362     return 0;
    363 
    364   SelectInst *SI;
    365   if (isa<SelectInst>(LHS)) {
    366     SI = cast<SelectInst>(LHS);
    367   } else {
    368     assert(isa<SelectInst>(RHS) && "No select instruction operand!");
    369     SI = cast<SelectInst>(RHS);
    370   }
    371 
    372   // Evaluate the BinOp on the true and false branches of the select.
    373   Value *TV;
    374   Value *FV;
    375   if (SI == LHS) {
    376     TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
    377     FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
    378   } else {
    379     TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
    380     FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
    381   }
    382 
    383   // If they simplified to the same value, then return the common value.
    384   // If they both failed to simplify then return null.
    385   if (TV == FV)
    386     return TV;
    387 
    388   // If one branch simplified to undef, return the other one.
    389   if (TV && isa<UndefValue>(TV))
    390     return FV;
    391   if (FV && isa<UndefValue>(FV))
    392     return TV;
    393 
    394   // If applying the operation did not change the true and false select values,
    395   // then the result of the binop is the select itself.
    396   if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
    397     return SI;
    398 
    399   // If one branch simplified and the other did not, and the simplified
    400   // value is equal to the unsimplified one, return the simplified value.
    401   // For example, select (cond, X, X & Z) & Z -> X & Z.
    402   if ((FV && !TV) || (TV && !FV)) {
    403     // Check that the simplified value has the form "X op Y" where "op" is the
    404     // same as the original operation.
    405     Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
    406     if (Simplified && Simplified->getOpcode() == Opcode) {
    407       // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
    408       // We already know that "op" is the same as for the simplified value.  See
    409       // if the operands match too.  If so, return the simplified value.
    410       Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
    411       Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
    412       Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
    413       if (Simplified->getOperand(0) == UnsimplifiedLHS &&
    414           Simplified->getOperand(1) == UnsimplifiedRHS)
    415         return Simplified;
    416       if (Simplified->isCommutative() &&
    417           Simplified->getOperand(1) == UnsimplifiedLHS &&
    418           Simplified->getOperand(0) == UnsimplifiedRHS)
    419         return Simplified;
    420     }
    421   }
    422 
    423   return 0;
    424 }
    425 
    426 /// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
    427 /// try to simplify the comparison by seeing whether both branches of the select
    428 /// result in the same value.  Returns the common value if so, otherwise returns
    429 /// null.
    430 static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
    431                                   Value *RHS, const Query &Q,
    432                                   unsigned MaxRecurse) {
    433   // Recursion is always used, so bail out at once if we already hit the limit.
    434   if (!MaxRecurse--)
    435     return 0;
    436 
    437   // Make sure the select is on the LHS.
    438   if (!isa<SelectInst>(LHS)) {
    439     std::swap(LHS, RHS);
    440     Pred = CmpInst::getSwappedPredicate(Pred);
    441   }
    442   assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
    443   SelectInst *SI = cast<SelectInst>(LHS);
    444   Value *Cond = SI->getCondition();
    445   Value *TV = SI->getTrueValue();
    446   Value *FV = SI->getFalseValue();
    447 
    448   // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
    449   // Does "cmp TV, RHS" simplify?
    450   Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse);
    451   if (TCmp == Cond) {
    452     // It not only simplified, it simplified to the select condition.  Replace
    453     // it with 'true'.
    454     TCmp = getTrue(Cond->getType());
    455   } else if (!TCmp) {
    456     // It didn't simplify.  However if "cmp TV, RHS" is equal to the select
    457     // condition then we can replace it with 'true'.  Otherwise give up.
    458     if (!isSameCompare(Cond, Pred, TV, RHS))
    459       return 0;
    460     TCmp = getTrue(Cond->getType());
    461   }
    462 
    463   // Does "cmp FV, RHS" simplify?
    464   Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse);
    465   if (FCmp == Cond) {
    466     // It not only simplified, it simplified to the select condition.  Replace
    467     // it with 'false'.
    468     FCmp = getFalse(Cond->getType());
    469   } else if (!FCmp) {
    470     // It didn't simplify.  However if "cmp FV, RHS" is equal to the select
    471     // condition then we can replace it with 'false'.  Otherwise give up.
    472     if (!isSameCompare(Cond, Pred, FV, RHS))
    473       return 0;
    474     FCmp = getFalse(Cond->getType());
    475   }
    476 
    477   // If both sides simplified to the same value, then use it as the result of
    478   // the original comparison.
    479   if (TCmp == FCmp)
    480     return TCmp;
    481 
    482   // The remaining cases only make sense if the select condition has the same
    483   // type as the result of the comparison, so bail out if this is not so.
    484   if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy())
    485     return 0;
    486   // If the false value simplified to false, then the result of the compare
    487   // is equal to "Cond && TCmp".  This also catches the case when the false
    488   // value simplified to false and the true value to true, returning "Cond".
    489   if (match(FCmp, m_Zero()))
    490     if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse))
    491       return V;
    492   // If the true value simplified to true, then the result of the compare
    493   // is equal to "Cond || FCmp".
    494   if (match(TCmp, m_One()))
    495     if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse))
    496       return V;
    497   // Finally, if the false value simplified to true and the true value to
    498   // false, then the result of the compare is equal to "!Cond".
    499   if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
    500     if (Value *V =
    501         SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
    502                         Q, MaxRecurse))
    503       return V;
    504 
    505   return 0;
    506 }
    507 
    508 /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that
    509 /// is a PHI instruction, try to simplify the binop by seeing whether evaluating
    510 /// it on the incoming phi values yields the same result for every value.  If so
    511 /// returns the common value, otherwise returns null.
    512 static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
    513                                  const Query &Q, unsigned MaxRecurse) {
    514   // Recursion is always used, so bail out at once if we already hit the limit.
    515   if (!MaxRecurse--)
    516     return 0;
    517 
    518   PHINode *PI;
    519   if (isa<PHINode>(LHS)) {
    520     PI = cast<PHINode>(LHS);
    521     // Bail out if RHS and the phi may be mutually interdependent due to a loop.
    522     if (!ValueDominatesPHI(RHS, PI, Q.DT))
    523       return 0;
    524   } else {
    525     assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
    526     PI = cast<PHINode>(RHS);
    527     // Bail out if LHS and the phi may be mutually interdependent due to a loop.
    528     if (!ValueDominatesPHI(LHS, PI, Q.DT))
    529       return 0;
    530   }
    531 
    532   // Evaluate the BinOp on the incoming phi values.
    533   Value *CommonValue = 0;
    534   for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
    535     Value *Incoming = PI->getIncomingValue(i);
    536     // If the incoming value is the phi node itself, it can safely be skipped.
    537     if (Incoming == PI) continue;
    538     Value *V = PI == LHS ?
    539       SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) :
    540       SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse);
    541     // If the operation failed to simplify, or simplified to a different value
    542     // to previously, then give up.
    543     if (!V || (CommonValue && V != CommonValue))
    544       return 0;
    545     CommonValue = V;
    546   }
    547 
    548   return CommonValue;
    549 }
    550 
    551 /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try
    552 /// try to simplify the comparison by seeing whether comparing with all of the
    553 /// incoming phi values yields the same result every time.  If so returns the
    554 /// common result, otherwise returns null.
    555 static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
    556                                const Query &Q, unsigned MaxRecurse) {
    557   // Recursion is always used, so bail out at once if we already hit the limit.
    558   if (!MaxRecurse--)
    559     return 0;
    560 
    561   // Make sure the phi is on the LHS.
    562   if (!isa<PHINode>(LHS)) {
    563     std::swap(LHS, RHS);
    564     Pred = CmpInst::getSwappedPredicate(Pred);
    565   }
    566   assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
    567   PHINode *PI = cast<PHINode>(LHS);
    568 
    569   // Bail out if RHS and the phi may be mutually interdependent due to a loop.
    570   if (!ValueDominatesPHI(RHS, PI, Q.DT))
    571     return 0;
    572 
    573   // Evaluate the BinOp on the incoming phi values.
    574   Value *CommonValue = 0;
    575   for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
    576     Value *Incoming = PI->getIncomingValue(i);
    577     // If the incoming value is the phi node itself, it can safely be skipped.
    578     if (Incoming == PI) continue;
    579     Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse);
    580     // If the operation failed to simplify, or simplified to a different value
    581     // to previously, then give up.
    582     if (!V || (CommonValue && V != CommonValue))
    583       return 0;
    584     CommonValue = V;
    585   }
    586 
    587   return CommonValue;
    588 }
    589 
    590 /// SimplifyAddInst - Given operands for an Add, see if we can
    591 /// fold the result.  If not, this returns null.
    592 static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
    593                               const Query &Q, unsigned MaxRecurse) {
    594   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
    595     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
    596       Constant *Ops[] = { CLHS, CRHS };
    597       return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), Ops,
    598                                       Q.TD, Q.TLI);
    599     }
    600 
    601     // Canonicalize the constant to the RHS.
    602     std::swap(Op0, Op1);
    603   }
    604 
    605   // X + undef -> undef
    606   if (match(Op1, m_Undef()))
    607     return Op1;
    608 
    609   // X + 0 -> X
    610   if (match(Op1, m_Zero()))
    611     return Op0;
    612 
    613   // X + (Y - X) -> Y
    614   // (Y - X) + X -> Y
    615   // Eg: X + -X -> 0
    616   Value *Y = 0;
    617   if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
    618       match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
    619     return Y;
    620 
    621   // X + ~X -> -1   since   ~X = -X-1
    622   if (match(Op0, m_Not(m_Specific(Op1))) ||
    623       match(Op1, m_Not(m_Specific(Op0))))
    624     return Constant::getAllOnesValue(Op0->getType());
    625 
    626   /// i1 add -> xor.
    627   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
    628     if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
    629       return V;
    630 
    631   // Try some generic simplifications for associative operations.
    632   if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q,
    633                                           MaxRecurse))
    634     return V;
    635 
    636   // Mul distributes over Add.  Try some generic simplifications based on this.
    637   if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul,
    638                                 Q, MaxRecurse))
    639     return V;
    640 
    641   // Threading Add over selects and phi nodes is pointless, so don't bother.
    642   // Threading over the select in "A + select(cond, B, C)" means evaluating
    643   // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
    644   // only if B and C are equal.  If B and C are equal then (since we assume
    645   // that operands have already been simplified) "select(cond, B, C)" should
    646   // have been simplified to the common value of B and C already.  Analysing
    647   // "A+B" and "A+C" thus gains nothing, but costs compile time.  Similarly
    648   // for threading over phi nodes.
    649 
    650   return 0;
    651 }
    652 
    653 Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
    654                              const DataLayout *TD, const TargetLibraryInfo *TLI,
    655                              const DominatorTree *DT) {
    656   return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query (TD, TLI, DT),
    657                            RecursionLimit);
    658 }
    659 
    660 /// \brief Compute the base pointer and cumulative constant offsets for V.
    661 ///
    662 /// This strips all constant offsets off of V, leaving it the base pointer, and
    663 /// accumulates the total constant offset applied in the returned constant. It
    664 /// returns 0 if V is not a pointer, and returns the constant '0' if there are
    665 /// no constant offsets applied.
    666 ///
    667 /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't
    668 /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc.
    669 /// folding.
    670 static Constant *stripAndComputeConstantOffsets(const DataLayout *TD,
    671                                                 Value *&V) {
    672   assert(V->getType()->getScalarType()->isPointerTy());
    673 
    674   // Without DataLayout, just be conservative for now. Theoretically, more could
    675   // be done in this case.
    676   if (!TD)
    677     return ConstantInt::get(IntegerType::get(V->getContext(), 64), 0);
    678 
    679   unsigned IntPtrWidth = TD->getPointerSizeInBits();
    680   APInt Offset = APInt::getNullValue(IntPtrWidth);
    681 
    682   // Even though we don't look through PHI nodes, we could be called on an
    683   // instruction in an unreachable block, which may be on a cycle.
    684   SmallPtrSet<Value *, 4> Visited;
    685   Visited.insert(V);
    686   do {
    687     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
    688       if (!GEP->isInBounds() || !GEP->accumulateConstantOffset(*TD, Offset))
    689         break;
    690       V = GEP->getPointerOperand();
    691     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
    692       V = cast<Operator>(V)->getOperand(0);
    693     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
    694       if (GA->mayBeOverridden())
    695         break;
    696       V = GA->getAliasee();
    697     } else {
    698       break;
    699     }
    700     assert(V->getType()->getScalarType()->isPointerTy() &&
    701            "Unexpected operand type!");
    702   } while (Visited.insert(V));
    703 
    704   Type *IntPtrTy = TD->getIntPtrType(V->getContext());
    705   Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset);
    706   if (V->getType()->isVectorTy())
    707     return ConstantVector::getSplat(V->getType()->getVectorNumElements(),
    708                                     OffsetIntPtr);
    709   return OffsetIntPtr;
    710 }
    711 
    712 /// \brief Compute the constant difference between two pointer values.
    713 /// If the difference is not a constant, returns zero.
    714 static Constant *computePointerDifference(const DataLayout *TD,
    715                                           Value *LHS, Value *RHS) {
    716   Constant *LHSOffset = stripAndComputeConstantOffsets(TD, LHS);
    717   Constant *RHSOffset = stripAndComputeConstantOffsets(TD, RHS);
    718 
    719   // If LHS and RHS are not related via constant offsets to the same base
    720   // value, there is nothing we can do here.
    721   if (LHS != RHS)
    722     return 0;
    723 
    724   // Otherwise, the difference of LHS - RHS can be computed as:
    725   //    LHS - RHS
    726   //  = (LHSOffset + Base) - (RHSOffset + Base)
    727   //  = LHSOffset - RHSOffset
    728   return ConstantExpr::getSub(LHSOffset, RHSOffset);
    729 }
    730 
    731 /// SimplifySubInst - Given operands for a Sub, see if we can
    732 /// fold the result.  If not, this returns null.
    733 static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
    734                               const Query &Q, unsigned MaxRecurse) {
    735   if (Constant *CLHS = dyn_cast<Constant>(Op0))
    736     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
    737       Constant *Ops[] = { CLHS, CRHS };
    738       return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(),
    739                                       Ops, Q.TD, Q.TLI);
    740     }
    741 
    742   // X - undef -> undef
    743   // undef - X -> undef
    744   if (match(Op0, m_Undef()) || match(Op1, m_Undef()))
    745     return UndefValue::get(Op0->getType());
    746 
    747   // X - 0 -> X
    748   if (match(Op1, m_Zero()))
    749     return Op0;
    750 
    751   // X - X -> 0
    752   if (Op0 == Op1)
    753     return Constant::getNullValue(Op0->getType());
    754 
    755   // (X*2) - X -> X
    756   // (X<<1) - X -> X
    757   Value *X = 0;
    758   if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
    759       match(Op0, m_Shl(m_Specific(Op1), m_One())))
    760     return Op1;
    761 
    762   // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
    763   // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
    764   Value *Y = 0, *Z = Op1;
    765   if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
    766     // See if "V === Y - Z" simplifies.
    767     if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1))
    768       // It does!  Now see if "X + V" simplifies.
    769       if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) {
    770         // It does, we successfully reassociated!
    771         ++NumReassoc;
    772         return W;
    773       }
    774     // See if "V === X - Z" simplifies.
    775     if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
    776       // It does!  Now see if "Y + V" simplifies.
    777       if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) {
    778         // It does, we successfully reassociated!
    779         ++NumReassoc;
    780         return W;
    781       }
    782   }
    783 
    784   // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
    785   // For example, X - (X + 1) -> -1
    786   X = Op0;
    787   if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
    788     // See if "V === X - Y" simplifies.
    789     if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
    790       // It does!  Now see if "V - Z" simplifies.
    791       if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) {
    792         // It does, we successfully reassociated!
    793         ++NumReassoc;
    794         return W;
    795       }
    796     // See if "V === X - Z" simplifies.
    797     if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
    798       // It does!  Now see if "V - Y" simplifies.
    799       if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) {
    800         // It does, we successfully reassociated!
    801         ++NumReassoc;
    802         return W;
    803       }
    804   }
    805 
    806   // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
    807   // For example, X - (X - Y) -> Y.
    808   Z = Op0;
    809   if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
    810     // See if "V === Z - X" simplifies.
    811     if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1))
    812       // It does!  Now see if "V + Y" simplifies.
    813       if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) {
    814         // It does, we successfully reassociated!
    815         ++NumReassoc;
    816         return W;
    817       }
    818 
    819   // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
    820   if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
    821       match(Op1, m_Trunc(m_Value(Y))))
    822     if (X->getType() == Y->getType())
    823       // See if "V === X - Y" simplifies.
    824       if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
    825         // It does!  Now see if "trunc V" simplifies.
    826         if (Value *W = SimplifyTruncInst(V, Op0->getType(), Q, MaxRecurse-1))
    827           // It does, return the simplified "trunc V".
    828           return W;
    829 
    830   // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
    831   if (match(Op0, m_PtrToInt(m_Value(X))) &&
    832       match(Op1, m_PtrToInt(m_Value(Y))))
    833     if (Constant *Result = computePointerDifference(Q.TD, X, Y))
    834       return ConstantExpr::getIntegerCast(Result, Op0->getType(), true);
    835 
    836   // Mul distributes over Sub.  Try some generic simplifications based on this.
    837   if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
    838                                 Q, MaxRecurse))
    839     return V;
    840 
    841   // i1 sub -> xor.
    842   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
    843     if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
    844       return V;
    845 
    846   // Threading Sub over selects and phi nodes is pointless, so don't bother.
    847   // Threading over the select in "A - select(cond, B, C)" means evaluating
    848   // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
    849   // only if B and C are equal.  If B and C are equal then (since we assume
    850   // that operands have already been simplified) "select(cond, B, C)" should
    851   // have been simplified to the common value of B and C already.  Analysing
    852   // "A-B" and "A-C" thus gains nothing, but costs compile time.  Similarly
    853   // for threading over phi nodes.
    854 
    855   return 0;
    856 }
    857 
    858 Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
    859                              const DataLayout *TD, const TargetLibraryInfo *TLI,
    860                              const DominatorTree *DT) {
    861   return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query (TD, TLI, DT),
    862                            RecursionLimit);
    863 }
    864 
    865 /// Given operands for an FAdd, see if we can fold the result.  If not, this
    866 /// returns null.
    867 static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
    868                               const Query &Q, unsigned MaxRecurse) {
    869   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
    870     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
    871       Constant *Ops[] = { CLHS, CRHS };
    872       return ConstantFoldInstOperands(Instruction::FAdd, CLHS->getType(),
    873                                       Ops, Q.TD, Q.TLI);
    874     }
    875 
    876     // Canonicalize the constant to the RHS.
    877     std::swap(Op0, Op1);
    878   }
    879 
    880   // fadd X, -0 ==> X
    881   if (match(Op1, m_NegZero()))
    882     return Op0;
    883 
    884   // fadd X, 0 ==> X, when we know X is not -0
    885   if (match(Op1, m_Zero()) &&
    886       (FMF.noSignedZeros() || CannotBeNegativeZero(Op0)))
    887     return Op0;
    888 
    889   // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0
    890   //   where nnan and ninf have to occur at least once somewhere in this
    891   //   expression
    892   Value *SubOp = 0;
    893   if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0))))
    894     SubOp = Op1;
    895   else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1))))
    896     SubOp = Op0;
    897   if (SubOp) {
    898     Instruction *FSub = cast<Instruction>(SubOp);
    899     if ((FMF.noNaNs() || FSub->hasNoNaNs()) &&
    900         (FMF.noInfs() || FSub->hasNoInfs()))
    901       return Constant::getNullValue(Op0->getType());
    902   }
    903 
    904   return 0;
    905 }
    906 
    907 /// Given operands for an FSub, see if we can fold the result.  If not, this
    908 /// returns null.
    909 static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
    910                               const Query &Q, unsigned MaxRecurse) {
    911   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
    912     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
    913       Constant *Ops[] = { CLHS, CRHS };
    914       return ConstantFoldInstOperands(Instruction::FSub, CLHS->getType(),
    915                                       Ops, Q.TD, Q.TLI);
    916     }
    917   }
    918 
    919   // fsub X, 0 ==> X
    920   if (match(Op1, m_Zero()))
    921     return Op0;
    922 
    923   // fsub X, -0 ==> X, when we know X is not -0
    924   if (match(Op1, m_NegZero()) &&
    925       (FMF.noSignedZeros() || CannotBeNegativeZero(Op0)))
    926     return Op0;
    927 
    928   // fsub 0, (fsub -0.0, X) ==> X
    929   Value *X;
    930   if (match(Op0, m_AnyZero())) {
    931     if (match(Op1, m_FSub(m_NegZero(), m_Value(X))))
    932       return X;
    933     if (FMF.noSignedZeros() && match(Op1, m_FSub(m_AnyZero(), m_Value(X))))
    934       return X;
    935   }
    936 
    937   // fsub nnan ninf x, x ==> 0.0
    938   if (FMF.noNaNs() && FMF.noInfs() && Op0 == Op1)
    939     return Constant::getNullValue(Op0->getType());
    940 
    941   return 0;
    942 }
    943 
    944 /// Given the operands for an FMul, see if we can fold the result
    945 static Value *SimplifyFMulInst(Value *Op0, Value *Op1,
    946                                FastMathFlags FMF,
    947                                const Query &Q,
    948                                unsigned MaxRecurse) {
    949  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
    950     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
    951       Constant *Ops[] = { CLHS, CRHS };
    952       return ConstantFoldInstOperands(Instruction::FMul, CLHS->getType(),
    953                                       Ops, Q.TD, Q.TLI);
    954     }
    955 
    956     // Canonicalize the constant to the RHS.
    957     std::swap(Op0, Op1);
    958  }
    959 
    960  // fmul X, 1.0 ==> X
    961  if (match(Op1, m_FPOne()))
    962    return Op0;
    963 
    964  // fmul nnan nsz X, 0 ==> 0
    965  if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero()))
    966    return Op1;
    967 
    968  return 0;
    969 }
    970 
    971 /// SimplifyMulInst - Given operands for a Mul, see if we can
    972 /// fold the result.  If not, this returns null.
    973 static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q,
    974                               unsigned MaxRecurse) {
    975   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
    976     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
    977       Constant *Ops[] = { CLHS, CRHS };
    978       return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(),
    979                                       Ops, Q.TD, Q.TLI);
    980     }
    981 
    982     // Canonicalize the constant to the RHS.
    983     std::swap(Op0, Op1);
    984   }
    985 
    986   // X * undef -> 0
    987   if (match(Op1, m_Undef()))
    988     return Constant::getNullValue(Op0->getType());
    989 
    990   // X * 0 -> 0
    991   if (match(Op1, m_Zero()))
    992     return Op1;
    993 
    994   // X * 1 -> X
    995   if (match(Op1, m_One()))
    996     return Op0;
    997 
    998   // (X / Y) * Y -> X if the division is exact.
    999   Value *X = 0;
   1000   if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
   1001       match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))   // Y * (X / Y)
   1002     return X;
   1003 
   1004   // i1 mul -> and.
   1005   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
   1006     if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1))
   1007       return V;
   1008 
   1009   // Try some generic simplifications for associative operations.
   1010   if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q,
   1011                                           MaxRecurse))
   1012     return V;
   1013 
   1014   // Mul distributes over Add.  Try some generic simplifications based on this.
   1015   if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
   1016                              Q, MaxRecurse))
   1017     return V;
   1018 
   1019   // If the operation is with the result of a select instruction, check whether
   1020   // operating on either branch of the select always yields the same value.
   1021   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
   1022     if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q,
   1023                                          MaxRecurse))
   1024       return V;
   1025 
   1026   // If the operation is with the result of a phi instruction, check whether
   1027   // operating on all incoming values of the phi always yields the same value.
   1028   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
   1029     if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q,
   1030                                       MaxRecurse))
   1031       return V;
   1032 
   1033   return 0;
   1034 }
   1035 
   1036 Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
   1037                              const DataLayout *TD, const TargetLibraryInfo *TLI,
   1038                              const DominatorTree *DT) {
   1039   return ::SimplifyFAddInst(Op0, Op1, FMF, Query (TD, TLI, DT), RecursionLimit);
   1040 }
   1041 
   1042 Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
   1043                              const DataLayout *TD, const TargetLibraryInfo *TLI,
   1044                              const DominatorTree *DT) {
   1045   return ::SimplifyFSubInst(Op0, Op1, FMF, Query (TD, TLI, DT), RecursionLimit);
   1046 }
   1047 
   1048 Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1,
   1049                               FastMathFlags FMF,
   1050                               const DataLayout *TD,
   1051                               const TargetLibraryInfo *TLI,
   1052                               const DominatorTree *DT) {
   1053   return ::SimplifyFMulInst(Op0, Op1, FMF, Query (TD, TLI, DT), RecursionLimit);
   1054 }
   1055 
   1056 Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1057                              const TargetLibraryInfo *TLI,
   1058                              const DominatorTree *DT) {
   1059   return ::SimplifyMulInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1060 }
   1061 
   1062 /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can
   1063 /// fold the result.  If not, this returns null.
   1064 static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
   1065                           const Query &Q, unsigned MaxRecurse) {
   1066   if (Constant *C0 = dyn_cast<Constant>(Op0)) {
   1067     if (Constant *C1 = dyn_cast<Constant>(Op1)) {
   1068       Constant *Ops[] = { C0, C1 };
   1069       return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.TD, Q.TLI);
   1070     }
   1071   }
   1072 
   1073   bool isSigned = Opcode == Instruction::SDiv;
   1074 
   1075   // X / undef -> undef
   1076   if (match(Op1, m_Undef()))
   1077     return Op1;
   1078 
   1079   // undef / X -> 0
   1080   if (match(Op0, m_Undef()))
   1081     return Constant::getNullValue(Op0->getType());
   1082 
   1083   // 0 / X -> 0, we don't need to preserve faults!
   1084   if (match(Op0, m_Zero()))
   1085     return Op0;
   1086 
   1087   // X / 1 -> X
   1088   if (match(Op1, m_One()))
   1089     return Op0;
   1090 
   1091   if (Op0->getType()->isIntegerTy(1))
   1092     // It can't be division by zero, hence it must be division by one.
   1093     return Op0;
   1094 
   1095   // X / X -> 1
   1096   if (Op0 == Op1)
   1097     return ConstantInt::get(Op0->getType(), 1);
   1098 
   1099   // (X * Y) / Y -> X if the multiplication does not overflow.
   1100   Value *X = 0, *Y = 0;
   1101   if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) {
   1102     if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1
   1103     OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0);
   1104     // If the Mul knows it does not overflow, then we are good to go.
   1105     if ((isSigned && Mul->hasNoSignedWrap()) ||
   1106         (!isSigned && Mul->hasNoUnsignedWrap()))
   1107       return X;
   1108     // If X has the form X = A / Y then X * Y cannot overflow.
   1109     if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X))
   1110       if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y)
   1111         return X;
   1112   }
   1113 
   1114   // (X rem Y) / Y -> 0
   1115   if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
   1116       (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
   1117     return Constant::getNullValue(Op0->getType());
   1118 
   1119   // If the operation is with the result of a select instruction, check whether
   1120   // operating on either branch of the select always yields the same value.
   1121   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
   1122     if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
   1123       return V;
   1124 
   1125   // If the operation is with the result of a phi instruction, check whether
   1126   // operating on all incoming values of the phi always yields the same value.
   1127   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
   1128     if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
   1129       return V;
   1130 
   1131   return 0;
   1132 }
   1133 
   1134 /// SimplifySDivInst - Given operands for an SDiv, see if we can
   1135 /// fold the result.  If not, this returns null.
   1136 static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q,
   1137                                unsigned MaxRecurse) {
   1138   if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse))
   1139     return V;
   1140 
   1141   return 0;
   1142 }
   1143 
   1144 Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1145                               const TargetLibraryInfo *TLI,
   1146                               const DominatorTree *DT) {
   1147   return ::SimplifySDivInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1148 }
   1149 
   1150 /// SimplifyUDivInst - Given operands for a UDiv, see if we can
   1151 /// fold the result.  If not, this returns null.
   1152 static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q,
   1153                                unsigned MaxRecurse) {
   1154   if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse))
   1155     return V;
   1156 
   1157   return 0;
   1158 }
   1159 
   1160 Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1161                               const TargetLibraryInfo *TLI,
   1162                               const DominatorTree *DT) {
   1163   return ::SimplifyUDivInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1164 }
   1165 
   1166 static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const Query &Q,
   1167                                unsigned) {
   1168   // undef / X -> undef    (the undef could be a snan).
   1169   if (match(Op0, m_Undef()))
   1170     return Op0;
   1171 
   1172   // X / undef -> undef
   1173   if (match(Op1, m_Undef()))
   1174     return Op1;
   1175 
   1176   return 0;
   1177 }
   1178 
   1179 Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1180                               const TargetLibraryInfo *TLI,
   1181                               const DominatorTree *DT) {
   1182   return ::SimplifyFDivInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1183 }
   1184 
   1185 /// SimplifyRem - Given operands for an SRem or URem, see if we can
   1186 /// fold the result.  If not, this returns null.
   1187 static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
   1188                           const Query &Q, unsigned MaxRecurse) {
   1189   if (Constant *C0 = dyn_cast<Constant>(Op0)) {
   1190     if (Constant *C1 = dyn_cast<Constant>(Op1)) {
   1191       Constant *Ops[] = { C0, C1 };
   1192       return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.TD, Q.TLI);
   1193     }
   1194   }
   1195 
   1196   // X % undef -> undef
   1197   if (match(Op1, m_Undef()))
   1198     return Op1;
   1199 
   1200   // undef % X -> 0
   1201   if (match(Op0, m_Undef()))
   1202     return Constant::getNullValue(Op0->getType());
   1203 
   1204   // 0 % X -> 0, we don't need to preserve faults!
   1205   if (match(Op0, m_Zero()))
   1206     return Op0;
   1207 
   1208   // X % 0 -> undef, we don't need to preserve faults!
   1209   if (match(Op1, m_Zero()))
   1210     return UndefValue::get(Op0->getType());
   1211 
   1212   // X % 1 -> 0
   1213   if (match(Op1, m_One()))
   1214     return Constant::getNullValue(Op0->getType());
   1215 
   1216   if (Op0->getType()->isIntegerTy(1))
   1217     // It can't be remainder by zero, hence it must be remainder by one.
   1218     return Constant::getNullValue(Op0->getType());
   1219 
   1220   // X % X -> 0
   1221   if (Op0 == Op1)
   1222     return Constant::getNullValue(Op0->getType());
   1223 
   1224   // If the operation is with the result of a select instruction, check whether
   1225   // operating on either branch of the select always yields the same value.
   1226   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
   1227     if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
   1228       return V;
   1229 
   1230   // If the operation is with the result of a phi instruction, check whether
   1231   // operating on all incoming values of the phi always yields the same value.
   1232   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
   1233     if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
   1234       return V;
   1235 
   1236   return 0;
   1237 }
   1238 
   1239 /// SimplifySRemInst - Given operands for an SRem, see if we can
   1240 /// fold the result.  If not, this returns null.
   1241 static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q,
   1242                                unsigned MaxRecurse) {
   1243   if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse))
   1244     return V;
   1245 
   1246   return 0;
   1247 }
   1248 
   1249 Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1250                               const TargetLibraryInfo *TLI,
   1251                               const DominatorTree *DT) {
   1252   return ::SimplifySRemInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1253 }
   1254 
   1255 /// SimplifyURemInst - Given operands for a URem, see if we can
   1256 /// fold the result.  If not, this returns null.
   1257 static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q,
   1258                                unsigned MaxRecurse) {
   1259   if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse))
   1260     return V;
   1261 
   1262   return 0;
   1263 }
   1264 
   1265 Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1266                               const TargetLibraryInfo *TLI,
   1267                               const DominatorTree *DT) {
   1268   return ::SimplifyURemInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1269 }
   1270 
   1271 static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const Query &,
   1272                                unsigned) {
   1273   // undef % X -> undef    (the undef could be a snan).
   1274   if (match(Op0, m_Undef()))
   1275     return Op0;
   1276 
   1277   // X % undef -> undef
   1278   if (match(Op1, m_Undef()))
   1279     return Op1;
   1280 
   1281   return 0;
   1282 }
   1283 
   1284 Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1285                               const TargetLibraryInfo *TLI,
   1286                               const DominatorTree *DT) {
   1287   return ::SimplifyFRemInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1288 }
   1289 
   1290 /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can
   1291 /// fold the result.  If not, this returns null.
   1292 static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1,
   1293                             const Query &Q, unsigned MaxRecurse) {
   1294   if (Constant *C0 = dyn_cast<Constant>(Op0)) {
   1295     if (Constant *C1 = dyn_cast<Constant>(Op1)) {
   1296       Constant *Ops[] = { C0, C1 };
   1297       return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.TD, Q.TLI);
   1298     }
   1299   }
   1300 
   1301   // 0 shift by X -> 0
   1302   if (match(Op0, m_Zero()))
   1303     return Op0;
   1304 
   1305   // X shift by 0 -> X
   1306   if (match(Op1, m_Zero()))
   1307     return Op0;
   1308 
   1309   // X shift by undef -> undef because it may shift by the bitwidth.
   1310   if (match(Op1, m_Undef()))
   1311     return Op1;
   1312 
   1313   // Shifting by the bitwidth or more is undefined.
   1314   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1))
   1315     if (CI->getValue().getLimitedValue() >=
   1316         Op0->getType()->getScalarSizeInBits())
   1317       return UndefValue::get(Op0->getType());
   1318 
   1319   // If the operation is with the result of a select instruction, check whether
   1320   // operating on either branch of the select always yields the same value.
   1321   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
   1322     if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
   1323       return V;
   1324 
   1325   // If the operation is with the result of a phi instruction, check whether
   1326   // operating on all incoming values of the phi always yields the same value.
   1327   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
   1328     if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
   1329       return V;
   1330 
   1331   return 0;
   1332 }
   1333 
   1334 /// SimplifyShlInst - Given operands for an Shl, see if we can
   1335 /// fold the result.  If not, this returns null.
   1336 static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
   1337                               const Query &Q, unsigned MaxRecurse) {
   1338   if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse))
   1339     return V;
   1340 
   1341   // undef << X -> 0
   1342   if (match(Op0, m_Undef()))
   1343     return Constant::getNullValue(Op0->getType());
   1344 
   1345   // (X >> A) << A -> X
   1346   Value *X;
   1347   if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
   1348     return X;
   1349   return 0;
   1350 }
   1351 
   1352 Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
   1353                              const DataLayout *TD, const TargetLibraryInfo *TLI,
   1354                              const DominatorTree *DT) {
   1355   return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query (TD, TLI, DT),
   1356                            RecursionLimit);
   1357 }
   1358 
   1359 /// SimplifyLShrInst - Given operands for an LShr, see if we can
   1360 /// fold the result.  If not, this returns null.
   1361 static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
   1362                                const Query &Q, unsigned MaxRecurse) {
   1363   if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, Q, MaxRecurse))
   1364     return V;
   1365 
   1366   // undef >>l X -> 0
   1367   if (match(Op0, m_Undef()))
   1368     return Constant::getNullValue(Op0->getType());
   1369 
   1370   // (X << A) >> A -> X
   1371   Value *X;
   1372   if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
   1373       cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap())
   1374     return X;
   1375 
   1376   return 0;
   1377 }
   1378 
   1379 Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
   1380                               const DataLayout *TD,
   1381                               const TargetLibraryInfo *TLI,
   1382                               const DominatorTree *DT) {
   1383   return ::SimplifyLShrInst(Op0, Op1, isExact, Query (TD, TLI, DT),
   1384                             RecursionLimit);
   1385 }
   1386 
   1387 /// SimplifyAShrInst - Given operands for an AShr, see if we can
   1388 /// fold the result.  If not, this returns null.
   1389 static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
   1390                                const Query &Q, unsigned MaxRecurse) {
   1391   if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, Q, MaxRecurse))
   1392     return V;
   1393 
   1394   // all ones >>a X -> all ones
   1395   if (match(Op0, m_AllOnes()))
   1396     return Op0;
   1397 
   1398   // undef >>a X -> all ones
   1399   if (match(Op0, m_Undef()))
   1400     return Constant::getAllOnesValue(Op0->getType());
   1401 
   1402   // (X << A) >> A -> X
   1403   Value *X;
   1404   if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
   1405       cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap())
   1406     return X;
   1407 
   1408   return 0;
   1409 }
   1410 
   1411 Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
   1412                               const DataLayout *TD,
   1413                               const TargetLibraryInfo *TLI,
   1414                               const DominatorTree *DT) {
   1415   return ::SimplifyAShrInst(Op0, Op1, isExact, Query (TD, TLI, DT),
   1416                             RecursionLimit);
   1417 }
   1418 
   1419 /// SimplifyAndInst - Given operands for an And, see if we can
   1420 /// fold the result.  If not, this returns null.
   1421 static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q,
   1422                               unsigned MaxRecurse) {
   1423   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
   1424     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
   1425       Constant *Ops[] = { CLHS, CRHS };
   1426       return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
   1427                                       Ops, Q.TD, Q.TLI);
   1428     }
   1429 
   1430     // Canonicalize the constant to the RHS.
   1431     std::swap(Op0, Op1);
   1432   }
   1433 
   1434   // X & undef -> 0
   1435   if (match(Op1, m_Undef()))
   1436     return Constant::getNullValue(Op0->getType());
   1437 
   1438   // X & X = X
   1439   if (Op0 == Op1)
   1440     return Op0;
   1441 
   1442   // X & 0 = 0
   1443   if (match(Op1, m_Zero()))
   1444     return Op1;
   1445 
   1446   // X & -1 = X
   1447   if (match(Op1, m_AllOnes()))
   1448     return Op0;
   1449 
   1450   // A & ~A  =  ~A & A  =  0
   1451   if (match(Op0, m_Not(m_Specific(Op1))) ||
   1452       match(Op1, m_Not(m_Specific(Op0))))
   1453     return Constant::getNullValue(Op0->getType());
   1454 
   1455   // (A | ?) & A = A
   1456   Value *A = 0, *B = 0;
   1457   if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
   1458       (A == Op1 || B == Op1))
   1459     return Op1;
   1460 
   1461   // A & (A | ?) = A
   1462   if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
   1463       (A == Op0 || B == Op0))
   1464     return Op0;
   1465 
   1466   // A & (-A) = A if A is a power of two or zero.
   1467   if (match(Op0, m_Neg(m_Specific(Op1))) ||
   1468       match(Op1, m_Neg(m_Specific(Op0)))) {
   1469     if (isKnownToBeAPowerOfTwo(Op0, /*OrZero*/true))
   1470       return Op0;
   1471     if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true))
   1472       return Op1;
   1473   }
   1474 
   1475   // Try some generic simplifications for associative operations.
   1476   if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q,
   1477                                           MaxRecurse))
   1478     return V;
   1479 
   1480   // And distributes over Or.  Try some generic simplifications based on this.
   1481   if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
   1482                              Q, MaxRecurse))
   1483     return V;
   1484 
   1485   // And distributes over Xor.  Try some generic simplifications based on this.
   1486   if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
   1487                              Q, MaxRecurse))
   1488     return V;
   1489 
   1490   // Or distributes over And.  Try some generic simplifications based on this.
   1491   if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or,
   1492                                 Q, MaxRecurse))
   1493     return V;
   1494 
   1495   // If the operation is with the result of a select instruction, check whether
   1496   // operating on either branch of the select always yields the same value.
   1497   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
   1498     if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q,
   1499                                          MaxRecurse))
   1500       return V;
   1501 
   1502   // If the operation is with the result of a phi instruction, check whether
   1503   // operating on all incoming values of the phi always yields the same value.
   1504   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
   1505     if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q,
   1506                                       MaxRecurse))
   1507       return V;
   1508 
   1509   return 0;
   1510 }
   1511 
   1512 Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1513                              const TargetLibraryInfo *TLI,
   1514                              const DominatorTree *DT) {
   1515   return ::SimplifyAndInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1516 }
   1517 
   1518 /// SimplifyOrInst - Given operands for an Or, see if we can
   1519 /// fold the result.  If not, this returns null.
   1520 static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q,
   1521                              unsigned MaxRecurse) {
   1522   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
   1523     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
   1524       Constant *Ops[] = { CLHS, CRHS };
   1525       return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
   1526                                       Ops, Q.TD, Q.TLI);
   1527     }
   1528 
   1529     // Canonicalize the constant to the RHS.
   1530     std::swap(Op0, Op1);
   1531   }
   1532 
   1533   // X | undef -> -1
   1534   if (match(Op1, m_Undef()))
   1535     return Constant::getAllOnesValue(Op0->getType());
   1536 
   1537   // X | X = X
   1538   if (Op0 == Op1)
   1539     return Op0;
   1540 
   1541   // X | 0 = X
   1542   if (match(Op1, m_Zero()))
   1543     return Op0;
   1544 
   1545   // X | -1 = -1
   1546   if (match(Op1, m_AllOnes()))
   1547     return Op1;
   1548 
   1549   // A | ~A  =  ~A | A  =  -1
   1550   if (match(Op0, m_Not(m_Specific(Op1))) ||
   1551       match(Op1, m_Not(m_Specific(Op0))))
   1552     return Constant::getAllOnesValue(Op0->getType());
   1553 
   1554   // (A & ?) | A = A
   1555   Value *A = 0, *B = 0;
   1556   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
   1557       (A == Op1 || B == Op1))
   1558     return Op1;
   1559 
   1560   // A | (A & ?) = A
   1561   if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
   1562       (A == Op0 || B == Op0))
   1563     return Op0;
   1564 
   1565   // ~(A & ?) | A = -1
   1566   if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) &&
   1567       (A == Op1 || B == Op1))
   1568     return Constant::getAllOnesValue(Op1->getType());
   1569 
   1570   // A | ~(A & ?) = -1
   1571   if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) &&
   1572       (A == Op0 || B == Op0))
   1573     return Constant::getAllOnesValue(Op0->getType());
   1574 
   1575   // Try some generic simplifications for associative operations.
   1576   if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q,
   1577                                           MaxRecurse))
   1578     return V;
   1579 
   1580   // Or distributes over And.  Try some generic simplifications based on this.
   1581   if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q,
   1582                              MaxRecurse))
   1583     return V;
   1584 
   1585   // And distributes over Or.  Try some generic simplifications based on this.
   1586   if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And,
   1587                                 Q, MaxRecurse))
   1588     return V;
   1589 
   1590   // If the operation is with the result of a select instruction, check whether
   1591   // operating on either branch of the select always yields the same value.
   1592   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
   1593     if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q,
   1594                                          MaxRecurse))
   1595       return V;
   1596 
   1597   // If the operation is with the result of a phi instruction, check whether
   1598   // operating on all incoming values of the phi always yields the same value.
   1599   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
   1600     if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
   1601       return V;
   1602 
   1603   return 0;
   1604 }
   1605 
   1606 Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1607                             const TargetLibraryInfo *TLI,
   1608                             const DominatorTree *DT) {
   1609   return ::SimplifyOrInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1610 }
   1611 
   1612 /// SimplifyXorInst - Given operands for a Xor, see if we can
   1613 /// fold the result.  If not, this returns null.
   1614 static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q,
   1615                               unsigned MaxRecurse) {
   1616   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
   1617     if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
   1618       Constant *Ops[] = { CLHS, CRHS };
   1619       return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(),
   1620                                       Ops, Q.TD, Q.TLI);
   1621     }
   1622 
   1623     // Canonicalize the constant to the RHS.
   1624     std::swap(Op0, Op1);
   1625   }
   1626 
   1627   // A ^ undef -> undef
   1628   if (match(Op1, m_Undef()))
   1629     return Op1;
   1630 
   1631   // A ^ 0 = A
   1632   if (match(Op1, m_Zero()))
   1633     return Op0;
   1634 
   1635   // A ^ A = 0
   1636   if (Op0 == Op1)
   1637     return Constant::getNullValue(Op0->getType());
   1638 
   1639   // A ^ ~A  =  ~A ^ A  =  -1
   1640   if (match(Op0, m_Not(m_Specific(Op1))) ||
   1641       match(Op1, m_Not(m_Specific(Op0))))
   1642     return Constant::getAllOnesValue(Op0->getType());
   1643 
   1644   // Try some generic simplifications for associative operations.
   1645   if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q,
   1646                                           MaxRecurse))
   1647     return V;
   1648 
   1649   // And distributes over Xor.  Try some generic simplifications based on this.
   1650   if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And,
   1651                                 Q, MaxRecurse))
   1652     return V;
   1653 
   1654   // Threading Xor over selects and phi nodes is pointless, so don't bother.
   1655   // Threading over the select in "A ^ select(cond, B, C)" means evaluating
   1656   // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
   1657   // only if B and C are equal.  If B and C are equal then (since we assume
   1658   // that operands have already been simplified) "select(cond, B, C)" should
   1659   // have been simplified to the common value of B and C already.  Analysing
   1660   // "A^B" and "A^C" thus gains nothing, but costs compile time.  Similarly
   1661   // for threading over phi nodes.
   1662 
   1663   return 0;
   1664 }
   1665 
   1666 Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout *TD,
   1667                              const TargetLibraryInfo *TLI,
   1668                              const DominatorTree *DT) {
   1669   return ::SimplifyXorInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit);
   1670 }
   1671 
   1672 static Type *GetCompareTy(Value *Op) {
   1673   return CmpInst::makeCmpResultType(Op->getType());
   1674 }
   1675 
   1676 /// ExtractEquivalentCondition - Rummage around inside V looking for something
   1677 /// equivalent to the comparison "LHS Pred RHS".  Return such a value if found,
   1678 /// otherwise return null.  Helper function for analyzing max/min idioms.
   1679 static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred,
   1680                                          Value *LHS, Value *RHS) {
   1681   SelectInst *SI = dyn_cast<SelectInst>(V);
   1682   if (!SI)
   1683     return 0;
   1684   CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
   1685   if (!Cmp)
   1686     return 0;
   1687   Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
   1688   if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
   1689     return Cmp;
   1690   if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
   1691       LHS == CmpRHS && RHS == CmpLHS)
   1692     return Cmp;
   1693   return 0;
   1694 }
   1695 
   1696 // A significant optimization not implemented here is assuming that alloca
   1697 // addresses are not equal to incoming argument values. They don't *alias*,
   1698 // as we say, but that doesn't mean they aren't equal, so we take a
   1699 // conservative approach.
   1700 //
   1701 // This is inspired in part by C++11 5.10p1:
   1702 //   "Two pointers of the same type compare equal if and only if they are both
   1703 //    null, both point to the same function, or both represent the same
   1704 //    address."
   1705 //
   1706 // This is pretty permissive.
   1707 //
   1708 // It's also partly due to C11 6.5.9p6:
   1709 //   "Two pointers compare equal if and only if both are null pointers, both are
   1710 //    pointers to the same object (including a pointer to an object and a
   1711 //    subobject at its beginning) or function, both are pointers to one past the
   1712 //    last element of the same array object, or one is a pointer to one past the
   1713 //    end of one array object and the other is a pointer to the start of a
   1714 //    different array object that happens to immediately follow the rst array
   1715 //    object in the address space.)
   1716 //
   1717 // C11's version is more restrictive, however there's no reason why an argument
   1718 // couldn't be a one-past-the-end value for a stack object in the caller and be
   1719 // equal to the beginning of a stack object in the callee.
   1720 //
   1721 // If the C and C++ standards are ever made sufficiently restrictive in this
   1722 // area, it may be possible to update LLVM's semantics accordingly and reinstate
   1723 // this optimization.
   1724 static Constant *computePointerICmp(const DataLayout *TD,
   1725                                     const TargetLibraryInfo *TLI,
   1726                                     CmpInst::Predicate Pred,
   1727                                     Value *LHS, Value *RHS) {
   1728   // First, skip past any trivial no-ops.
   1729   LHS = LHS->stripPointerCasts();
   1730   RHS = RHS->stripPointerCasts();
   1731 
   1732   // A non-null pointer is not equal to a null pointer.
   1733   if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) &&
   1734       (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE))
   1735     return ConstantInt::get(GetCompareTy(LHS),
   1736                             !CmpInst::isTrueWhenEqual(Pred));
   1737 
   1738   // We can only fold certain predicates on pointer comparisons.
   1739   switch (Pred) {
   1740   default:
   1741     return 0;
   1742 
   1743     // Equality comaprisons are easy to fold.
   1744   case CmpInst::ICMP_EQ:
   1745   case CmpInst::ICMP_NE:
   1746     break;
   1747 
   1748     // We can only handle unsigned relational comparisons because 'inbounds' on
   1749     // a GEP only protects against unsigned wrapping.
   1750   case CmpInst::ICMP_UGT:
   1751   case CmpInst::ICMP_UGE:
   1752   case CmpInst::ICMP_ULT:
   1753   case CmpInst::ICMP_ULE:
   1754     // However, we have to switch them to their signed variants to handle
   1755     // negative indices from the base pointer.
   1756     Pred = ICmpInst::getSignedPredicate(Pred);
   1757     break;
   1758   }
   1759 
   1760   // Strip off any constant offsets so that we can reason about them.
   1761   // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
   1762   // here and compare base addresses like AliasAnalysis does, however there are
   1763   // numerous hazards. AliasAnalysis and its utilities rely on special rules
   1764   // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
   1765   // doesn't need to guarantee pointer inequality when it says NoAlias.
   1766   Constant *LHSOffset = stripAndComputeConstantOffsets(TD, LHS);
   1767   Constant *RHSOffset = stripAndComputeConstantOffsets(TD, RHS);
   1768 
   1769   // If LHS and RHS are related via constant offsets to the same base
   1770   // value, we can replace it with an icmp which just compares the offsets.
   1771   if (LHS == RHS)
   1772     return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset);
   1773 
   1774   // Various optimizations for (in)equality comparisons.
   1775   if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
   1776     // Different non-empty allocations that exist at the same time have
   1777     // different addresses (if the program can tell). Global variables always
   1778     // exist, so they always exist during the lifetime of each other and all
   1779     // allocas. Two different allocas usually have different addresses...
   1780     //
   1781     // However, if there's an @llvm.stackrestore dynamically in between two
   1782     // allocas, they may have the same address. It's tempting to reduce the
   1783     // scope of the problem by only looking at *static* allocas here. That would
   1784     // cover the majority of allocas while significantly reducing the likelihood
   1785     // of having an @llvm.stackrestore pop up in the middle. However, it's not
   1786     // actually impossible for an @llvm.stackrestore to pop up in the middle of
   1787     // an entry block. Also, if we have a block that's not attached to a
   1788     // function, we can't tell if it's "static" under the current definition.
   1789     // Theoretically, this problem could be fixed by creating a new kind of
   1790     // instruction kind specifically for static allocas. Such a new instruction
   1791     // could be required to be at the top of the entry block, thus preventing it
   1792     // from being subject to a @llvm.stackrestore. Instcombine could even
   1793     // convert regular allocas into these special allocas. It'd be nifty.
   1794     // However, until then, this problem remains open.
   1795     //
   1796     // So, we'll assume that two non-empty allocas have different addresses
   1797     // for now.
   1798     //
   1799     // With all that, if the offsets are within the bounds of their allocations
   1800     // (and not one-past-the-end! so we can't use inbounds!), and their
   1801     // allocations aren't the same, the pointers are not equal.
   1802     //
   1803     // Note that it's not necessary to check for LHS being a global variable
   1804     // address, due to canonicalization and constant folding.
   1805     if (isa<AllocaInst>(LHS) &&
   1806         (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
   1807       ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset);
   1808       ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset);
   1809       uint64_t LHSSize, RHSSize;
   1810       if (LHSOffsetCI && RHSOffsetCI &&
   1811           getObjectSize(LHS, LHSSize, TD, TLI) &&
   1812           getObjectSize(RHS, RHSSize, TD, TLI)) {
   1813         const APInt &LHSOffsetValue = LHSOffsetCI->getValue();
   1814         const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
   1815         if (!LHSOffsetValue.isNegative() &&
   1816             !RHSOffsetValue.isNegative() &&
   1817             LHSOffsetValue.ult(LHSSize) &&
   1818             RHSOffsetValue.ult(RHSSize)) {
   1819           return ConstantInt::get(GetCompareTy(LHS),
   1820                                   !CmpInst::isTrueWhenEqual(Pred));
   1821         }
   1822       }
   1823 
   1824       // Repeat the above check but this time without depending on DataLayout
   1825       // or being able to compute a precise size.
   1826       if (!cast<PointerType>(LHS->getType())->isEmptyTy() &&
   1827           !cast<PointerType>(RHS->getType())->isEmptyTy() &&
   1828           LHSOffset->isNullValue() &&
   1829           RHSOffset->isNullValue())
   1830         return ConstantInt::get(GetCompareTy(LHS),
   1831                                 !CmpInst::isTrueWhenEqual(Pred));
   1832     }
   1833   }
   1834 
   1835   // Otherwise, fail.
   1836   return 0;
   1837 }
   1838 
   1839 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
   1840 /// fold the result.  If not, this returns null.
   1841 static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   1842                                const Query &Q, unsigned MaxRecurse) {
   1843   CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
   1844   assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
   1845 
   1846   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
   1847     if (Constant *CRHS = dyn_cast<Constant>(RHS))
   1848       return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.TD, Q.TLI);
   1849 
   1850     // If we have a constant, make sure it is on the RHS.
   1851     std::swap(LHS, RHS);
   1852     Pred = CmpInst::getSwappedPredicate(Pred);
   1853   }
   1854 
   1855   Type *ITy = GetCompareTy(LHS); // The return type.
   1856   Type *OpTy = LHS->getType();   // The operand type.
   1857 
   1858   // icmp X, X -> true/false
   1859   // X icmp undef -> true/false.  For example, icmp ugt %X, undef -> false
   1860   // because X could be 0.
   1861   if (LHS == RHS || isa<UndefValue>(RHS))
   1862     return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
   1863 
   1864   // Special case logic when the operands have i1 type.
   1865   if (OpTy->getScalarType()->isIntegerTy(1)) {
   1866     switch (Pred) {
   1867     default: break;
   1868     case ICmpInst::ICMP_EQ:
   1869       // X == 1 -> X
   1870       if (match(RHS, m_One()))
   1871         return LHS;
   1872       break;
   1873     case ICmpInst::ICMP_NE:
   1874       // X != 0 -> X
   1875       if (match(RHS, m_Zero()))
   1876         return LHS;
   1877       break;
   1878     case ICmpInst::ICMP_UGT:
   1879       // X >u 0 -> X
   1880       if (match(RHS, m_Zero()))
   1881         return LHS;
   1882       break;
   1883     case ICmpInst::ICMP_UGE:
   1884       // X >=u 1 -> X
   1885       if (match(RHS, m_One()))
   1886         return LHS;
   1887       break;
   1888     case ICmpInst::ICMP_SLT:
   1889       // X <s 0 -> X
   1890       if (match(RHS, m_Zero()))
   1891         return LHS;
   1892       break;
   1893     case ICmpInst::ICMP_SLE:
   1894       // X <=s -1 -> X
   1895       if (match(RHS, m_One()))
   1896         return LHS;
   1897       break;
   1898     }
   1899   }
   1900 
   1901   // If we are comparing with zero then try hard since this is a common case.
   1902   if (match(RHS, m_Zero())) {
   1903     bool LHSKnownNonNegative, LHSKnownNegative;
   1904     switch (Pred) {
   1905     default: llvm_unreachable("Unknown ICmp predicate!");
   1906     case ICmpInst::ICMP_ULT:
   1907       return getFalse(ITy);
   1908     case ICmpInst::ICMP_UGE:
   1909       return getTrue(ITy);
   1910     case ICmpInst::ICMP_EQ:
   1911     case ICmpInst::ICMP_ULE:
   1912       if (isKnownNonZero(LHS, Q.TD))
   1913         return getFalse(ITy);
   1914       break;
   1915     case ICmpInst::ICMP_NE:
   1916     case ICmpInst::ICMP_UGT:
   1917       if (isKnownNonZero(LHS, Q.TD))
   1918         return getTrue(ITy);
   1919       break;
   1920     case ICmpInst::ICMP_SLT:
   1921       ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD);
   1922       if (LHSKnownNegative)
   1923         return getTrue(ITy);
   1924       if (LHSKnownNonNegative)
   1925         return getFalse(ITy);
   1926       break;
   1927     case ICmpInst::ICMP_SLE:
   1928       ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD);
   1929       if (LHSKnownNegative)
   1930         return getTrue(ITy);
   1931       if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.TD))
   1932         return getFalse(ITy);
   1933       break;
   1934     case ICmpInst::ICMP_SGE:
   1935       ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD);
   1936       if (LHSKnownNegative)
   1937         return getFalse(ITy);
   1938       if (LHSKnownNonNegative)
   1939         return getTrue(ITy);
   1940       break;
   1941     case ICmpInst::ICMP_SGT:
   1942       ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD);
   1943       if (LHSKnownNegative)
   1944         return getFalse(ITy);
   1945       if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.TD))
   1946         return getTrue(ITy);
   1947       break;
   1948     }
   1949   }
   1950 
   1951   // See if we are doing a comparison with a constant integer.
   1952   if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
   1953     // Rule out tautological comparisons (eg., ult 0 or uge 0).
   1954     ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue());
   1955     if (RHS_CR.isEmptySet())
   1956       return ConstantInt::getFalse(CI->getContext());
   1957     if (RHS_CR.isFullSet())
   1958       return ConstantInt::getTrue(CI->getContext());
   1959 
   1960     // Many binary operators with constant RHS have easy to compute constant
   1961     // range.  Use them to check whether the comparison is a tautology.
   1962     uint32_t Width = CI->getBitWidth();
   1963     APInt Lower = APInt(Width, 0);
   1964     APInt Upper = APInt(Width, 0);
   1965     ConstantInt *CI2;
   1966     if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) {
   1967       // 'urem x, CI2' produces [0, CI2).
   1968       Upper = CI2->getValue();
   1969     } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) {
   1970       // 'srem x, CI2' produces (-|CI2|, |CI2|).
   1971       Upper = CI2->getValue().abs();
   1972       Lower = (-Upper) + 1;
   1973     } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) {
   1974       // 'udiv CI2, x' produces [0, CI2].
   1975       Upper = CI2->getValue() + 1;
   1976     } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) {
   1977       // 'udiv x, CI2' produces [0, UINT_MAX / CI2].
   1978       APInt NegOne = APInt::getAllOnesValue(Width);
   1979       if (!CI2->isZero())
   1980         Upper = NegOne.udiv(CI2->getValue()) + 1;
   1981     } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) {
   1982       // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2].
   1983       APInt IntMin = APInt::getSignedMinValue(Width);
   1984       APInt IntMax = APInt::getSignedMaxValue(Width);
   1985       APInt Val = CI2->getValue().abs();
   1986       if (!Val.isMinValue()) {
   1987         Lower = IntMin.sdiv(Val);
   1988         Upper = IntMax.sdiv(Val) + 1;
   1989       }
   1990     } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) {
   1991       // 'lshr x, CI2' produces [0, UINT_MAX >> CI2].
   1992       APInt NegOne = APInt::getAllOnesValue(Width);
   1993       if (CI2->getValue().ult(Width))
   1994         Upper = NegOne.lshr(CI2->getValue()) + 1;
   1995     } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) {
   1996       // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2].
   1997       APInt IntMin = APInt::getSignedMinValue(Width);
   1998       APInt IntMax = APInt::getSignedMaxValue(Width);
   1999       if (CI2->getValue().ult(Width)) {
   2000         Lower = IntMin.ashr(CI2->getValue());
   2001         Upper = IntMax.ashr(CI2->getValue()) + 1;
   2002       }
   2003     } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) {
   2004       // 'or x, CI2' produces [CI2, UINT_MAX].
   2005       Lower = CI2->getValue();
   2006     } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) {
   2007       // 'and x, CI2' produces [0, CI2].
   2008       Upper = CI2->getValue() + 1;
   2009     }
   2010     if (Lower != Upper) {
   2011       ConstantRange LHS_CR = ConstantRange(Lower, Upper);
   2012       if (RHS_CR.contains(LHS_CR))
   2013         return ConstantInt::getTrue(RHS->getContext());
   2014       if (RHS_CR.inverse().contains(LHS_CR))
   2015         return ConstantInt::getFalse(RHS->getContext());
   2016     }
   2017   }
   2018 
   2019   // Compare of cast, for example (zext X) != 0 -> X != 0
   2020   if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
   2021     Instruction *LI = cast<CastInst>(LHS);
   2022     Value *SrcOp = LI->getOperand(0);
   2023     Type *SrcTy = SrcOp->getType();
   2024     Type *DstTy = LI->getType();
   2025 
   2026     // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
   2027     // if the integer type is the same size as the pointer type.
   2028     if (MaxRecurse && Q.TD && isa<PtrToIntInst>(LI) &&
   2029         Q.TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) {
   2030       if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
   2031         // Transfer the cast to the constant.
   2032         if (Value *V = SimplifyICmpInst(Pred, SrcOp,
   2033                                         ConstantExpr::getIntToPtr(RHSC, SrcTy),
   2034                                         Q, MaxRecurse-1))
   2035           return V;
   2036       } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
   2037         if (RI->getOperand(0)->getType() == SrcTy)
   2038           // Compare without the cast.
   2039           if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
   2040                                           Q, MaxRecurse-1))
   2041             return V;
   2042       }
   2043     }
   2044 
   2045     if (isa<ZExtInst>(LHS)) {
   2046       // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
   2047       // same type.
   2048       if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
   2049         if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
   2050           // Compare X and Y.  Note that signed predicates become unsigned.
   2051           if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
   2052                                           SrcOp, RI->getOperand(0), Q,
   2053                                           MaxRecurse-1))
   2054             return V;
   2055       }
   2056       // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
   2057       // too.  If not, then try to deduce the result of the comparison.
   2058       else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
   2059         // Compute the constant that would happen if we truncated to SrcTy then
   2060         // reextended to DstTy.
   2061         Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
   2062         Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
   2063 
   2064         // If the re-extended constant didn't change then this is effectively
   2065         // also a case of comparing two zero-extended values.
   2066         if (RExt == CI && MaxRecurse)
   2067           if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
   2068                                         SrcOp, Trunc, Q, MaxRecurse-1))
   2069             return V;
   2070 
   2071         // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
   2072         // there.  Use this to work out the result of the comparison.
   2073         if (RExt != CI) {
   2074           switch (Pred) {
   2075           default: llvm_unreachable("Unknown ICmp predicate!");
   2076           // LHS <u RHS.
   2077           case ICmpInst::ICMP_EQ:
   2078           case ICmpInst::ICMP_UGT:
   2079           case ICmpInst::ICMP_UGE:
   2080             return ConstantInt::getFalse(CI->getContext());
   2081 
   2082           case ICmpInst::ICMP_NE:
   2083           case ICmpInst::ICMP_ULT:
   2084           case ICmpInst::ICMP_ULE:
   2085             return ConstantInt::getTrue(CI->getContext());
   2086 
   2087           // LHS is non-negative.  If RHS is negative then LHS >s LHS.  If RHS
   2088           // is non-negative then LHS <s RHS.
   2089           case ICmpInst::ICMP_SGT:
   2090           case ICmpInst::ICMP_SGE:
   2091             return CI->getValue().isNegative() ?
   2092               ConstantInt::getTrue(CI->getContext()) :
   2093               ConstantInt::getFalse(CI->getContext());
   2094 
   2095           case ICmpInst::ICMP_SLT:
   2096           case ICmpInst::ICMP_SLE:
   2097             return CI->getValue().isNegative() ?
   2098               ConstantInt::getFalse(CI->getContext()) :
   2099               ConstantInt::getTrue(CI->getContext());
   2100           }
   2101         }
   2102       }
   2103     }
   2104 
   2105     if (isa<SExtInst>(LHS)) {
   2106       // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
   2107       // same type.
   2108       if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
   2109         if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
   2110           // Compare X and Y.  Note that the predicate does not change.
   2111           if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
   2112                                           Q, MaxRecurse-1))
   2113             return V;
   2114       }
   2115       // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
   2116       // too.  If not, then try to deduce the result of the comparison.
   2117       else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
   2118         // Compute the constant that would happen if we truncated to SrcTy then
   2119         // reextended to DstTy.
   2120         Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
   2121         Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
   2122 
   2123         // If the re-extended constant didn't change then this is effectively
   2124         // also a case of comparing two sign-extended values.
   2125         if (RExt == CI && MaxRecurse)
   2126           if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1))
   2127             return V;
   2128 
   2129         // Otherwise the upper bits of LHS are all equal, while RHS has varying
   2130         // bits there.  Use this to work out the result of the comparison.
   2131         if (RExt != CI) {
   2132           switch (Pred) {
   2133           default: llvm_unreachable("Unknown ICmp predicate!");
   2134           case ICmpInst::ICMP_EQ:
   2135             return ConstantInt::getFalse(CI->getContext());
   2136           case ICmpInst::ICMP_NE:
   2137             return ConstantInt::getTrue(CI->getContext());
   2138 
   2139           // If RHS is non-negative then LHS <s RHS.  If RHS is negative then
   2140           // LHS >s RHS.
   2141           case ICmpInst::ICMP_SGT:
   2142           case ICmpInst::ICMP_SGE:
   2143             return CI->getValue().isNegative() ?
   2144               ConstantInt::getTrue(CI->getContext()) :
   2145               ConstantInt::getFalse(CI->getContext());
   2146           case ICmpInst::ICMP_SLT:
   2147           case ICmpInst::ICMP_SLE:
   2148             return CI->getValue().isNegative() ?
   2149               ConstantInt::getFalse(CI->getContext()) :
   2150               ConstantInt::getTrue(CI->getContext());
   2151 
   2152           // If LHS is non-negative then LHS <u RHS.  If LHS is negative then
   2153           // LHS >u RHS.
   2154           case ICmpInst::ICMP_UGT:
   2155           case ICmpInst::ICMP_UGE:
   2156             // Comparison is true iff the LHS <s 0.
   2157             if (MaxRecurse)
   2158               if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
   2159                                               Constant::getNullValue(SrcTy),
   2160                                               Q, MaxRecurse-1))
   2161                 return V;
   2162             break;
   2163           case ICmpInst::ICMP_ULT:
   2164           case ICmpInst::ICMP_ULE:
   2165             // Comparison is true iff the LHS >=s 0.
   2166             if (MaxRecurse)
   2167               if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
   2168                                               Constant::getNullValue(SrcTy),
   2169                                               Q, MaxRecurse-1))
   2170                 return V;
   2171             break;
   2172           }
   2173         }
   2174       }
   2175     }
   2176   }
   2177 
   2178   // Special logic for binary operators.
   2179   BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
   2180   BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
   2181   if (MaxRecurse && (LBO || RBO)) {
   2182     // Analyze the case when either LHS or RHS is an add instruction.
   2183     Value *A = 0, *B = 0, *C = 0, *D = 0;
   2184     // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
   2185     bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
   2186     if (LBO && LBO->getOpcode() == Instruction::Add) {
   2187       A = LBO->getOperand(0); B = LBO->getOperand(1);
   2188       NoLHSWrapProblem = ICmpInst::isEquality(Pred) ||
   2189         (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) ||
   2190         (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap());
   2191     }
   2192     if (RBO && RBO->getOpcode() == Instruction::Add) {
   2193       C = RBO->getOperand(0); D = RBO->getOperand(1);
   2194       NoRHSWrapProblem = ICmpInst::isEquality(Pred) ||
   2195         (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) ||
   2196         (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap());
   2197     }
   2198 
   2199     // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
   2200     if ((A == RHS || B == RHS) && NoLHSWrapProblem)
   2201       if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A,
   2202                                       Constant::getNullValue(RHS->getType()),
   2203                                       Q, MaxRecurse-1))
   2204         return V;
   2205 
   2206     // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
   2207     if ((C == LHS || D == LHS) && NoRHSWrapProblem)
   2208       if (Value *V = SimplifyICmpInst(Pred,
   2209                                       Constant::getNullValue(LHS->getType()),
   2210                                       C == LHS ? D : C, Q, MaxRecurse-1))
   2211         return V;
   2212 
   2213     // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
   2214     if (A && C && (A == C || A == D || B == C || B == D) &&
   2215         NoLHSWrapProblem && NoRHSWrapProblem) {
   2216       // Determine Y and Z in the form icmp (X+Y), (X+Z).
   2217       Value *Y, *Z;
   2218       if (A == C) {
   2219         // C + B == C + D  ->  B == D
   2220         Y = B;
   2221         Z = D;
   2222       } else if (A == D) {
   2223         // D + B == C + D  ->  B == C
   2224         Y = B;
   2225         Z = C;
   2226       } else if (B == C) {
   2227         // A + C == C + D  ->  A == D
   2228         Y = A;
   2229         Z = D;
   2230       } else {
   2231         assert(B == D);
   2232         // A + D == C + D  ->  A == C
   2233         Y = A;
   2234         Z = C;
   2235       }
   2236       if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse-1))
   2237         return V;
   2238     }
   2239   }
   2240 
   2241   if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
   2242     bool KnownNonNegative, KnownNegative;
   2243     switch (Pred) {
   2244     default:
   2245       break;
   2246     case ICmpInst::ICMP_SGT:
   2247     case ICmpInst::ICMP_SGE:
   2248       ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.TD);
   2249       if (!KnownNonNegative)
   2250         break;
   2251       // fall-through
   2252     case ICmpInst::ICMP_EQ:
   2253     case ICmpInst::ICMP_UGT:
   2254     case ICmpInst::ICMP_UGE:
   2255       return getFalse(ITy);
   2256     case ICmpInst::ICMP_SLT:
   2257     case ICmpInst::ICMP_SLE:
   2258       ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.TD);
   2259       if (!KnownNonNegative)
   2260         break;
   2261       // fall-through
   2262     case ICmpInst::ICMP_NE:
   2263     case ICmpInst::ICMP_ULT:
   2264     case ICmpInst::ICMP_ULE:
   2265       return getTrue(ITy);
   2266     }
   2267   }
   2268   if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) {
   2269     bool KnownNonNegative, KnownNegative;
   2270     switch (Pred) {
   2271     default:
   2272       break;
   2273     case ICmpInst::ICMP_SGT:
   2274     case ICmpInst::ICMP_SGE:
   2275       ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.TD);
   2276       if (!KnownNonNegative)
   2277         break;
   2278       // fall-through
   2279     case ICmpInst::ICMP_NE:
   2280     case ICmpInst::ICMP_UGT:
   2281     case ICmpInst::ICMP_UGE:
   2282       return getTrue(ITy);
   2283     case ICmpInst::ICMP_SLT:
   2284     case ICmpInst::ICMP_SLE:
   2285       ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.TD);
   2286       if (!KnownNonNegative)
   2287         break;
   2288       // fall-through
   2289     case ICmpInst::ICMP_EQ:
   2290     case ICmpInst::ICMP_ULT:
   2291     case ICmpInst::ICMP_ULE:
   2292       return getFalse(ITy);
   2293     }
   2294   }
   2295 
   2296   // x udiv y <=u x.
   2297   if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) {
   2298     // icmp pred (X /u Y), X
   2299     if (Pred == ICmpInst::ICMP_UGT)
   2300       return getFalse(ITy);
   2301     if (Pred == ICmpInst::ICMP_ULE)
   2302       return getTrue(ITy);
   2303   }
   2304 
   2305   if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() &&
   2306       LBO->getOperand(1) == RBO->getOperand(1)) {
   2307     switch (LBO->getOpcode()) {
   2308     default: break;
   2309     case Instruction::UDiv:
   2310     case Instruction::LShr:
   2311       if (ICmpInst::isSigned(Pred))
   2312         break;
   2313       // fall-through
   2314     case Instruction::SDiv:
   2315     case Instruction::AShr:
   2316       if (!LBO->isExact() || !RBO->isExact())
   2317         break;
   2318       if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
   2319                                       RBO->getOperand(0), Q, MaxRecurse-1))
   2320         return V;
   2321       break;
   2322     case Instruction::Shl: {
   2323       bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap();
   2324       bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap();
   2325       if (!NUW && !NSW)
   2326         break;
   2327       if (!NSW && ICmpInst::isSigned(Pred))
   2328         break;
   2329       if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
   2330                                       RBO->getOperand(0), Q, MaxRecurse-1))
   2331         return V;
   2332       break;
   2333     }
   2334     }
   2335   }
   2336 
   2337   // Simplify comparisons involving max/min.
   2338   Value *A, *B;
   2339   CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
   2340   CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
   2341 
   2342   // Signed variants on "max(a,b)>=a -> true".
   2343   if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
   2344     if (A != RHS) std::swap(A, B); // smax(A, B) pred A.
   2345     EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
   2346     // We analyze this as smax(A, B) pred A.
   2347     P = Pred;
   2348   } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
   2349              (A == LHS || B == LHS)) {
   2350     if (A != LHS) std::swap(A, B); // A pred smax(A, B).
   2351     EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
   2352     // We analyze this as smax(A, B) swapped-pred A.
   2353     P = CmpInst::getSwappedPredicate(Pred);
   2354   } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
   2355              (A == RHS || B == RHS)) {
   2356     if (A != RHS) std::swap(A, B); // smin(A, B) pred A.
   2357     EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
   2358     // We analyze this as smax(-A, -B) swapped-pred -A.
   2359     // Note that we do not need to actually form -A or -B thanks to EqP.
   2360     P = CmpInst::getSwappedPredicate(Pred);
   2361   } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
   2362              (A == LHS || B == LHS)) {
   2363     if (A != LHS) std::swap(A, B); // A pred smin(A, B).
   2364     EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
   2365     // We analyze this as smax(-A, -B) pred -A.
   2366     // Note that we do not need to actually form -A or -B thanks to EqP.
   2367     P = Pred;
   2368   }
   2369   if (P != CmpInst::BAD_ICMP_PREDICATE) {
   2370     // Cases correspond to "max(A, B) p A".
   2371     switch (P) {
   2372     default:
   2373       break;
   2374     case CmpInst::ICMP_EQ:
   2375     case CmpInst::ICMP_SLE:
   2376       // Equivalent to "A EqP B".  This may be the same as the condition tested
   2377       // in the max/min; if so, we can just return that.
   2378       if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
   2379         return V;
   2380       if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
   2381         return V;
   2382       // Otherwise, see if "A EqP B" simplifies.
   2383       if (MaxRecurse)
   2384         if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1))
   2385           return V;
   2386       break;
   2387     case CmpInst::ICMP_NE:
   2388     case CmpInst::ICMP_SGT: {
   2389       CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
   2390       // Equivalent to "A InvEqP B".  This may be the same as the condition
   2391       // tested in the max/min; if so, we can just return that.
   2392       if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
   2393         return V;
   2394       if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
   2395         return V;
   2396       // Otherwise, see if "A InvEqP B" simplifies.
   2397       if (MaxRecurse)
   2398         if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1))
   2399           return V;
   2400       break;
   2401     }
   2402     case CmpInst::ICMP_SGE:
   2403       // Always true.
   2404       return getTrue(ITy);
   2405     case CmpInst::ICMP_SLT:
   2406       // Always false.
   2407       return getFalse(ITy);
   2408     }
   2409   }
   2410 
   2411   // Unsigned variants on "max(a,b)>=a -> true".
   2412   P = CmpInst::BAD_ICMP_PREDICATE;
   2413   if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
   2414     if (A != RHS) std::swap(A, B); // umax(A, B) pred A.
   2415     EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
   2416     // We analyze this as umax(A, B) pred A.
   2417     P = Pred;
   2418   } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
   2419              (A == LHS || B == LHS)) {
   2420     if (A != LHS) std::swap(A, B); // A pred umax(A, B).
   2421     EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
   2422     // We analyze this as umax(A, B) swapped-pred A.
   2423     P = CmpInst::getSwappedPredicate(Pred);
   2424   } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
   2425              (A == RHS || B == RHS)) {
   2426     if (A != RHS) std::swap(A, B); // umin(A, B) pred A.
   2427     EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
   2428     // We analyze this as umax(-A, -B) swapped-pred -A.
   2429     // Note that we do not need to actually form -A or -B thanks to EqP.
   2430     P = CmpInst::getSwappedPredicate(Pred);
   2431   } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
   2432              (A == LHS || B == LHS)) {
   2433     if (A != LHS) std::swap(A, B); // A pred umin(A, B).
   2434     EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
   2435     // We analyze this as umax(-A, -B) pred -A.
   2436     // Note that we do not need to actually form -A or -B thanks to EqP.
   2437     P = Pred;
   2438   }
   2439   if (P != CmpInst::BAD_ICMP_PREDICATE) {
   2440     // Cases correspond to "max(A, B) p A".
   2441     switch (P) {
   2442     default:
   2443       break;
   2444     case CmpInst::ICMP_EQ:
   2445     case CmpInst::ICMP_ULE:
   2446       // Equivalent to "A EqP B".  This may be the same as the condition tested
   2447       // in the max/min; if so, we can just return that.
   2448       if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
   2449         return V;
   2450       if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
   2451         return V;
   2452       // Otherwise, see if "A EqP B" simplifies.
   2453       if (MaxRecurse)
   2454         if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1))
   2455           return V;
   2456       break;
   2457     case CmpInst::ICMP_NE:
   2458     case CmpInst::ICMP_UGT: {
   2459       CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
   2460       // Equivalent to "A InvEqP B".  This may be the same as the condition
   2461       // tested in the max/min; if so, we can just return that.
   2462       if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
   2463         return V;
   2464       if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
   2465         return V;
   2466       // Otherwise, see if "A InvEqP B" simplifies.
   2467       if (MaxRecurse)
   2468         if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1))
   2469           return V;
   2470       break;
   2471     }
   2472     case CmpInst::ICMP_UGE:
   2473       // Always true.
   2474       return getTrue(ITy);
   2475     case CmpInst::ICMP_ULT:
   2476       // Always false.
   2477       return getFalse(ITy);
   2478     }
   2479   }
   2480 
   2481   // Variants on "max(x,y) >= min(x,z)".
   2482   Value *C, *D;
   2483   if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
   2484       match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
   2485       (A == C || A == D || B == C || B == D)) {
   2486     // max(x, ?) pred min(x, ?).
   2487     if (Pred == CmpInst::ICMP_SGE)
   2488       // Always true.
   2489       return getTrue(ITy);
   2490     if (Pred == CmpInst::ICMP_SLT)
   2491       // Always false.
   2492       return getFalse(ITy);
   2493   } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
   2494              match(RHS, m_SMax(m_Value(C), m_Value(D))) &&
   2495              (A == C || A == D || B == C || B == D)) {
   2496     // min(x, ?) pred max(x, ?).
   2497     if (Pred == CmpInst::ICMP_SLE)
   2498       // Always true.
   2499       return getTrue(ITy);
   2500     if (Pred == CmpInst::ICMP_SGT)
   2501       // Always false.
   2502       return getFalse(ITy);
   2503   } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
   2504              match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
   2505              (A == C || A == D || B == C || B == D)) {
   2506     // max(x, ?) pred min(x, ?).
   2507     if (Pred == CmpInst::ICMP_UGE)
   2508       // Always true.
   2509       return getTrue(ITy);
   2510     if (Pred == CmpInst::ICMP_ULT)
   2511       // Always false.
   2512       return getFalse(ITy);
   2513   } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
   2514              match(RHS, m_UMax(m_Value(C), m_Value(D))) &&
   2515              (A == C || A == D || B == C || B == D)) {
   2516     // min(x, ?) pred max(x, ?).
   2517     if (Pred == CmpInst::ICMP_ULE)
   2518       // Always true.
   2519       return getTrue(ITy);
   2520     if (Pred == CmpInst::ICMP_UGT)
   2521       // Always false.
   2522       return getFalse(ITy);
   2523   }
   2524 
   2525   // Simplify comparisons of related pointers using a powerful, recursive
   2526   // GEP-walk when we have target data available..
   2527   if (LHS->getType()->isPointerTy())
   2528     if (Constant *C = computePointerICmp(Q.TD, Q.TLI, Pred, LHS, RHS))
   2529       return C;
   2530 
   2531   if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) {
   2532     if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
   2533       if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
   2534           GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() &&
   2535           (ICmpInst::isEquality(Pred) ||
   2536            (GLHS->isInBounds() && GRHS->isInBounds() &&
   2537             Pred == ICmpInst::getSignedPredicate(Pred)))) {
   2538         // The bases are equal and the indices are constant.  Build a constant
   2539         // expression GEP with the same indices and a null base pointer to see
   2540         // what constant folding can make out of it.
   2541         Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType());
   2542         SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end());
   2543         Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS);
   2544 
   2545         SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end());
   2546         Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS);
   2547         return ConstantExpr::getICmp(Pred, NewLHS, NewRHS);
   2548       }
   2549     }
   2550   }
   2551 
   2552   // If the comparison is with the result of a select instruction, check whether
   2553   // comparing with either branch of the select always yields the same value.
   2554   if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
   2555     if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
   2556       return V;
   2557 
   2558   // If the comparison is with the result of a phi instruction, check whether
   2559   // doing the compare with each incoming phi value yields a common result.
   2560   if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
   2561     if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
   2562       return V;
   2563 
   2564   return 0;
   2565 }
   2566 
   2567 Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   2568                               const DataLayout *TD,
   2569                               const TargetLibraryInfo *TLI,
   2570                               const DominatorTree *DT) {
   2571   return ::SimplifyICmpInst(Predicate, LHS, RHS, Query (TD, TLI, DT),
   2572                             RecursionLimit);
   2573 }
   2574 
   2575 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
   2576 /// fold the result.  If not, this returns null.
   2577 static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   2578                                const Query &Q, unsigned MaxRecurse) {
   2579   CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
   2580   assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
   2581 
   2582   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
   2583     if (Constant *CRHS = dyn_cast<Constant>(RHS))
   2584       return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.TD, Q.TLI);
   2585 
   2586     // If we have a constant, make sure it is on the RHS.
   2587     std::swap(LHS, RHS);
   2588     Pred = CmpInst::getSwappedPredicate(Pred);
   2589   }
   2590 
   2591   // Fold trivial predicates.
   2592   if (Pred == FCmpInst::FCMP_FALSE)
   2593     return ConstantInt::get(GetCompareTy(LHS), 0);
   2594   if (Pred == FCmpInst::FCMP_TRUE)
   2595     return ConstantInt::get(GetCompareTy(LHS), 1);
   2596 
   2597   if (isa<UndefValue>(RHS))                  // fcmp pred X, undef -> undef
   2598     return UndefValue::get(GetCompareTy(LHS));
   2599 
   2600   // fcmp x,x -> true/false.  Not all compares are foldable.
   2601   if (LHS == RHS) {
   2602     if (CmpInst::isTrueWhenEqual(Pred))
   2603       return ConstantInt::get(GetCompareTy(LHS), 1);
   2604     if (CmpInst::isFalseWhenEqual(Pred))
   2605       return ConstantInt::get(GetCompareTy(LHS), 0);
   2606   }
   2607 
   2608   // Handle fcmp with constant RHS
   2609   if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
   2610     // If the constant is a nan, see if we can fold the comparison based on it.
   2611     if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
   2612       if (CFP->getValueAPF().isNaN()) {
   2613         if (FCmpInst::isOrdered(Pred))   // True "if ordered and foo"
   2614           return ConstantInt::getFalse(CFP->getContext());
   2615         assert(FCmpInst::isUnordered(Pred) &&
   2616                "Comparison must be either ordered or unordered!");
   2617         // True if unordered.
   2618         return ConstantInt::getTrue(CFP->getContext());
   2619       }
   2620       // Check whether the constant is an infinity.
   2621       if (CFP->getValueAPF().isInfinity()) {
   2622         if (CFP->getValueAPF().isNegative()) {
   2623           switch (Pred) {
   2624           case FCmpInst::FCMP_OLT:
   2625             // No value is ordered and less than negative infinity.
   2626             return ConstantInt::getFalse(CFP->getContext());
   2627           case FCmpInst::FCMP_UGE:
   2628             // All values are unordered with or at least negative infinity.
   2629             return ConstantInt::getTrue(CFP->getContext());
   2630           default:
   2631             break;
   2632           }
   2633         } else {
   2634           switch (Pred) {
   2635           case FCmpInst::FCMP_OGT:
   2636             // No value is ordered and greater than infinity.
   2637             return ConstantInt::getFalse(CFP->getContext());
   2638           case FCmpInst::FCMP_ULE:
   2639             // All values are unordered with and at most infinity.
   2640             return ConstantInt::getTrue(CFP->getContext());
   2641           default:
   2642             break;
   2643           }
   2644         }
   2645       }
   2646     }
   2647   }
   2648 
   2649   // If the comparison is with the result of a select instruction, check whether
   2650   // comparing with either branch of the select always yields the same value.
   2651   if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
   2652     if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
   2653       return V;
   2654 
   2655   // If the comparison is with the result of a phi instruction, check whether
   2656   // doing the compare with each incoming phi value yields a common result.
   2657   if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
   2658     if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
   2659       return V;
   2660 
   2661   return 0;
   2662 }
   2663 
   2664 Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   2665                               const DataLayout *TD,
   2666                               const TargetLibraryInfo *TLI,
   2667                               const DominatorTree *DT) {
   2668   return ::SimplifyFCmpInst(Predicate, LHS, RHS, Query (TD, TLI, DT),
   2669                             RecursionLimit);
   2670 }
   2671 
   2672 /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
   2673 /// the result.  If not, this returns null.
   2674 static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal,
   2675                                  Value *FalseVal, const Query &Q,
   2676                                  unsigned MaxRecurse) {
   2677   // select true, X, Y  -> X
   2678   // select false, X, Y -> Y
   2679   if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
   2680     return CB->getZExtValue() ? TrueVal : FalseVal;
   2681 
   2682   // select C, X, X -> X
   2683   if (TrueVal == FalseVal)
   2684     return TrueVal;
   2685 
   2686   if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
   2687     if (isa<Constant>(TrueVal))
   2688       return TrueVal;
   2689     return FalseVal;
   2690   }
   2691   if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
   2692     return FalseVal;
   2693   if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
   2694     return TrueVal;
   2695 
   2696   return 0;
   2697 }
   2698 
   2699 Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
   2700                                 const DataLayout *TD,
   2701                                 const TargetLibraryInfo *TLI,
   2702                                 const DominatorTree *DT) {
   2703   return ::SimplifySelectInst(Cond, TrueVal, FalseVal, Query (TD, TLI, DT),
   2704                               RecursionLimit);
   2705 }
   2706 
   2707 /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
   2708 /// fold the result.  If not, this returns null.
   2709 static Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const Query &Q, unsigned) {
   2710   // The type of the GEP pointer operand.
   2711   PointerType *PtrTy = dyn_cast<PointerType>(Ops[0]->getType());
   2712   // The GEP pointer operand is not a pointer, it's a vector of pointers.
   2713   if (!PtrTy)
   2714     return 0;
   2715 
   2716   // getelementptr P -> P.
   2717   if (Ops.size() == 1)
   2718     return Ops[0];
   2719 
   2720   if (isa<UndefValue>(Ops[0])) {
   2721     // Compute the (pointer) type returned by the GEP instruction.
   2722     Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1));
   2723     Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace());
   2724     return UndefValue::get(GEPTy);
   2725   }
   2726 
   2727   if (Ops.size() == 2) {
   2728     // getelementptr P, 0 -> P.
   2729     if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
   2730       if (C->isZero())
   2731         return Ops[0];
   2732     // getelementptr P, N -> P if P points to a type of zero size.
   2733     if (Q.TD) {
   2734       Type *Ty = PtrTy->getElementType();
   2735       if (Ty->isSized() && Q.TD->getTypeAllocSize(Ty) == 0)
   2736         return Ops[0];
   2737     }
   2738   }
   2739 
   2740   // Check to see if this is constant foldable.
   2741   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
   2742     if (!isa<Constant>(Ops[i]))
   2743       return 0;
   2744 
   2745   return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1));
   2746 }
   2747 
   2748 Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD,
   2749                              const TargetLibraryInfo *TLI,
   2750                              const DominatorTree *DT) {
   2751   return ::SimplifyGEPInst(Ops, Query (TD, TLI, DT), RecursionLimit);
   2752 }
   2753 
   2754 /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
   2755 /// can fold the result.  If not, this returns null.
   2756 static Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
   2757                                       ArrayRef<unsigned> Idxs, const Query &Q,
   2758                                       unsigned) {
   2759   if (Constant *CAgg = dyn_cast<Constant>(Agg))
   2760     if (Constant *CVal = dyn_cast<Constant>(Val))
   2761       return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
   2762 
   2763   // insertvalue x, undef, n -> x
   2764   if (match(Val, m_Undef()))
   2765     return Agg;
   2766 
   2767   // insertvalue x, (extractvalue y, n), n
   2768   if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
   2769     if (EV->getAggregateOperand()->getType() == Agg->getType() &&
   2770         EV->getIndices() == Idxs) {
   2771       // insertvalue undef, (extractvalue y, n), n -> y
   2772       if (match(Agg, m_Undef()))
   2773         return EV->getAggregateOperand();
   2774 
   2775       // insertvalue y, (extractvalue y, n), n -> y
   2776       if (Agg == EV->getAggregateOperand())
   2777         return Agg;
   2778     }
   2779 
   2780   return 0;
   2781 }
   2782 
   2783 Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
   2784                                      ArrayRef<unsigned> Idxs,
   2785                                      const DataLayout *TD,
   2786                                      const TargetLibraryInfo *TLI,
   2787                                      const DominatorTree *DT) {
   2788   return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query (TD, TLI, DT),
   2789                                    RecursionLimit);
   2790 }
   2791 
   2792 /// SimplifyPHINode - See if we can fold the given phi.  If not, returns null.
   2793 static Value *SimplifyPHINode(PHINode *PN, const Query &Q) {
   2794   // If all of the PHI's incoming values are the same then replace the PHI node
   2795   // with the common value.
   2796   Value *CommonValue = 0;
   2797   bool HasUndefInput = false;
   2798   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
   2799     Value *Incoming = PN->getIncomingValue(i);
   2800     // If the incoming value is the phi node itself, it can safely be skipped.
   2801     if (Incoming == PN) continue;
   2802     if (isa<UndefValue>(Incoming)) {
   2803       // Remember that we saw an undef value, but otherwise ignore them.
   2804       HasUndefInput = true;
   2805       continue;
   2806     }
   2807     if (CommonValue && Incoming != CommonValue)
   2808       return 0;  // Not the same, bail out.
   2809     CommonValue = Incoming;
   2810   }
   2811 
   2812   // If CommonValue is null then all of the incoming values were either undef or
   2813   // equal to the phi node itself.
   2814   if (!CommonValue)
   2815     return UndefValue::get(PN->getType());
   2816 
   2817   // If we have a PHI node like phi(X, undef, X), where X is defined by some
   2818   // instruction, we cannot return X as the result of the PHI node unless it
   2819   // dominates the PHI block.
   2820   if (HasUndefInput)
   2821     return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : 0;
   2822 
   2823   return CommonValue;
   2824 }
   2825 
   2826 static Value *SimplifyTruncInst(Value *Op, Type *Ty, const Query &Q, unsigned) {
   2827   if (Constant *C = dyn_cast<Constant>(Op))
   2828     return ConstantFoldInstOperands(Instruction::Trunc, Ty, C, Q.TD, Q.TLI);
   2829 
   2830   return 0;
   2831 }
   2832 
   2833 Value *llvm::SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD,
   2834                                const TargetLibraryInfo *TLI,
   2835                                const DominatorTree *DT) {
   2836   return ::SimplifyTruncInst(Op, Ty, Query (TD, TLI, DT), RecursionLimit);
   2837 }
   2838 
   2839 //=== Helper functions for higher up the class hierarchy.
   2840 
   2841 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
   2842 /// fold the result.  If not, this returns null.
   2843 static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
   2844                             const Query &Q, unsigned MaxRecurse) {
   2845   switch (Opcode) {
   2846   case Instruction::Add:
   2847     return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
   2848                            Q, MaxRecurse);
   2849   case Instruction::FAdd:
   2850     return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
   2851 
   2852   case Instruction::Sub:
   2853     return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
   2854                            Q, MaxRecurse);
   2855   case Instruction::FSub:
   2856     return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
   2857 
   2858   case Instruction::Mul:  return SimplifyMulInst (LHS, RHS, Q, MaxRecurse);
   2859   case Instruction::FMul:
   2860     return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse);
   2861   case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse);
   2862   case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse);
   2863   case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, Q, MaxRecurse);
   2864   case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse);
   2865   case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse);
   2866   case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, Q, MaxRecurse);
   2867   case Instruction::Shl:
   2868     return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
   2869                            Q, MaxRecurse);
   2870   case Instruction::LShr:
   2871     return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse);
   2872   case Instruction::AShr:
   2873     return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse);
   2874   case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse);
   2875   case Instruction::Or:  return SimplifyOrInst (LHS, RHS, Q, MaxRecurse);
   2876   case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse);
   2877   default:
   2878     if (Constant *CLHS = dyn_cast<Constant>(LHS))
   2879       if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
   2880         Constant *COps[] = {CLHS, CRHS};
   2881         return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, Q.TD,
   2882                                         Q.TLI);
   2883       }
   2884 
   2885     // If the operation is associative, try some generic simplifications.
   2886     if (Instruction::isAssociative(Opcode))
   2887       if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse))
   2888         return V;
   2889 
   2890     // If the operation is with the result of a select instruction check whether
   2891     // operating on either branch of the select always yields the same value.
   2892     if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
   2893       if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse))
   2894         return V;
   2895 
   2896     // If the operation is with the result of a phi instruction, check whether
   2897     // operating on all incoming values of the phi always yields the same value.
   2898     if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
   2899       if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse))
   2900         return V;
   2901 
   2902     return 0;
   2903   }
   2904 }
   2905 
   2906 Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
   2907                            const DataLayout *TD, const TargetLibraryInfo *TLI,
   2908                            const DominatorTree *DT) {
   2909   return ::SimplifyBinOp(Opcode, LHS, RHS, Query (TD, TLI, DT), RecursionLimit);
   2910 }
   2911 
   2912 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
   2913 /// fold the result.
   2914 static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   2915                               const Query &Q, unsigned MaxRecurse) {
   2916   if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
   2917     return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
   2918   return SimplifyFCmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
   2919 }
   2920 
   2921 Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   2922                              const DataLayout *TD, const TargetLibraryInfo *TLI,
   2923                              const DominatorTree *DT) {
   2924   return ::SimplifyCmpInst(Predicate, LHS, RHS, Query (TD, TLI, DT),
   2925                            RecursionLimit);
   2926 }
   2927 
   2928 static bool IsIdempotent(Intrinsic::ID ID) {
   2929   switch (ID) {
   2930   default: return false;
   2931 
   2932   // Unary idempotent: f(f(x)) = f(x)
   2933   case Intrinsic::fabs:
   2934   case Intrinsic::floor:
   2935   case Intrinsic::ceil:
   2936   case Intrinsic::trunc:
   2937   case Intrinsic::rint:
   2938   case Intrinsic::nearbyint:
   2939     return true;
   2940   }
   2941 }
   2942 
   2943 template <typename IterTy>
   2944 static Value *SimplifyIntrinsic(Intrinsic::ID IID, IterTy ArgBegin, IterTy ArgEnd,
   2945                                 const Query &Q, unsigned MaxRecurse) {
   2946   // Perform idempotent optimizations
   2947   if (!IsIdempotent(IID))
   2948     return 0;
   2949 
   2950   // Unary Ops
   2951   if (std::distance(ArgBegin, ArgEnd) == 1)
   2952     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin))
   2953       if (II->getIntrinsicID() == IID)
   2954         return II;
   2955 
   2956   return 0;
   2957 }
   2958 
   2959 template <typename IterTy>
   2960 static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd,
   2961                            const Query &Q, unsigned MaxRecurse) {
   2962   Type *Ty = V->getType();
   2963   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
   2964     Ty = PTy->getElementType();
   2965   FunctionType *FTy = cast<FunctionType>(Ty);
   2966 
   2967   // call undef -> undef
   2968   if (isa<UndefValue>(V))
   2969     return UndefValue::get(FTy->getReturnType());
   2970 
   2971   Function *F = dyn_cast<Function>(V);
   2972   if (!F)
   2973     return 0;
   2974 
   2975   if (unsigned IID = F->getIntrinsicID())
   2976     if (Value *Ret =
   2977         SimplifyIntrinsic((Intrinsic::ID) IID, ArgBegin, ArgEnd, Q, MaxRecurse))
   2978       return Ret;
   2979 
   2980   if (!canConstantFoldCallTo(F))
   2981     return 0;
   2982 
   2983   SmallVector<Constant *, 4> ConstantArgs;
   2984   ConstantArgs.reserve(ArgEnd - ArgBegin);
   2985   for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) {
   2986     Constant *C = dyn_cast<Constant>(*I);
   2987     if (!C)
   2988       return 0;
   2989     ConstantArgs.push_back(C);
   2990   }
   2991 
   2992   return ConstantFoldCall(F, ConstantArgs, Q.TLI);
   2993 }
   2994 
   2995 Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin,
   2996                           User::op_iterator ArgEnd, const DataLayout *TD,
   2997                           const TargetLibraryInfo *TLI,
   2998                           const DominatorTree *DT) {
   2999   return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(TD, TLI, DT),
   3000                         RecursionLimit);
   3001 }
   3002 
   3003 Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args,
   3004                           const DataLayout *TD, const TargetLibraryInfo *TLI,
   3005                           const DominatorTree *DT) {
   3006   return ::SimplifyCall(V, Args.begin(), Args.end(), Query(TD, TLI, DT),
   3007                         RecursionLimit);
   3008 }
   3009 
   3010 /// SimplifyInstruction - See if we can compute a simplified version of this
   3011 /// instruction.  If not, this returns null.
   3012 Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout *TD,
   3013                                  const TargetLibraryInfo *TLI,
   3014                                  const DominatorTree *DT) {
   3015   Value *Result;
   3016 
   3017   switch (I->getOpcode()) {
   3018   default:
   3019     Result = ConstantFoldInstruction(I, TD, TLI);
   3020     break;
   3021   case Instruction::FAdd:
   3022     Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1),
   3023                               I->getFastMathFlags(), TD, TLI, DT);
   3024     break;
   3025   case Instruction::Add:
   3026     Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
   3027                              cast<BinaryOperator>(I)->hasNoSignedWrap(),
   3028                              cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
   3029                              TD, TLI, DT);
   3030     break;
   3031   case Instruction::FSub:
   3032     Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1),
   3033                               I->getFastMathFlags(), TD, TLI, DT);
   3034     break;
   3035   case Instruction::Sub:
   3036     Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
   3037                              cast<BinaryOperator>(I)->hasNoSignedWrap(),
   3038                              cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
   3039                              TD, TLI, DT);
   3040     break;
   3041   case Instruction::FMul:
   3042     Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1),
   3043                               I->getFastMathFlags(), TD, TLI, DT);
   3044     break;
   3045   case Instruction::Mul:
   3046     Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3047     break;
   3048   case Instruction::SDiv:
   3049     Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3050     break;
   3051   case Instruction::UDiv:
   3052     Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3053     break;
   3054   case Instruction::FDiv:
   3055     Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3056     break;
   3057   case Instruction::SRem:
   3058     Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3059     break;
   3060   case Instruction::URem:
   3061     Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3062     break;
   3063   case Instruction::FRem:
   3064     Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3065     break;
   3066   case Instruction::Shl:
   3067     Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1),
   3068                              cast<BinaryOperator>(I)->hasNoSignedWrap(),
   3069                              cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
   3070                              TD, TLI, DT);
   3071     break;
   3072   case Instruction::LShr:
   3073     Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1),
   3074                               cast<BinaryOperator>(I)->isExact(),
   3075                               TD, TLI, DT);
   3076     break;
   3077   case Instruction::AShr:
   3078     Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1),
   3079                               cast<BinaryOperator>(I)->isExact(),
   3080                               TD, TLI, DT);
   3081     break;
   3082   case Instruction::And:
   3083     Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3084     break;
   3085   case Instruction::Or:
   3086     Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3087     break;
   3088   case Instruction::Xor:
   3089     Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3090     break;
   3091   case Instruction::ICmp:
   3092     Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
   3093                               I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3094     break;
   3095   case Instruction::FCmp:
   3096     Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
   3097                               I->getOperand(0), I->getOperand(1), TD, TLI, DT);
   3098     break;
   3099   case Instruction::Select:
   3100     Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
   3101                                 I->getOperand(2), TD, TLI, DT);
   3102     break;
   3103   case Instruction::GetElementPtr: {
   3104     SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
   3105     Result = SimplifyGEPInst(Ops, TD, TLI, DT);
   3106     break;
   3107   }
   3108   case Instruction::InsertValue: {
   3109     InsertValueInst *IV = cast<InsertValueInst>(I);
   3110     Result = SimplifyInsertValueInst(IV->getAggregateOperand(),
   3111                                      IV->getInsertedValueOperand(),
   3112                                      IV->getIndices(), TD, TLI, DT);
   3113     break;
   3114   }
   3115   case Instruction::PHI:
   3116     Result = SimplifyPHINode(cast<PHINode>(I), Query (TD, TLI, DT));
   3117     break;
   3118   case Instruction::Call: {
   3119     CallSite CS(cast<CallInst>(I));
   3120     Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(),
   3121                           TD, TLI, DT);
   3122     break;
   3123   }
   3124   case Instruction::Trunc:
   3125     Result = SimplifyTruncInst(I->getOperand(0), I->getType(), TD, TLI, DT);
   3126     break;
   3127   }
   3128 
   3129   /// If called on unreachable code, the above logic may report that the
   3130   /// instruction simplified to itself.  Make life easier for users by
   3131   /// detecting that case here, returning a safe value instead.
   3132   return Result == I ? UndefValue::get(I->getType()) : Result;
   3133 }
   3134 
   3135 /// \brief Implementation of recursive simplification through an instructions
   3136 /// uses.
   3137 ///
   3138 /// This is the common implementation of the recursive simplification routines.
   3139 /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
   3140 /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
   3141 /// instructions to process and attempt to simplify it using
   3142 /// InstructionSimplify.
   3143 ///
   3144 /// This routine returns 'true' only when *it* simplifies something. The passed
   3145 /// in simplified value does not count toward this.
   3146 static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV,
   3147                                               const DataLayout *TD,
   3148                                               const TargetLibraryInfo *TLI,
   3149                                               const DominatorTree *DT) {
   3150   bool Simplified = false;
   3151   SmallSetVector<Instruction *, 8> Worklist;
   3152 
   3153   // If we have an explicit value to collapse to, do that round of the
   3154   // simplification loop by hand initially.
   3155   if (SimpleV) {
   3156     for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE;
   3157          ++UI)
   3158       if (*UI != I)
   3159         Worklist.insert(cast<Instruction>(*UI));
   3160 
   3161     // Replace the instruction with its simplified value.
   3162     I->replaceAllUsesWith(SimpleV);
   3163 
   3164     // Gracefully handle edge cases where the instruction is not wired into any
   3165     // parent block.
   3166     if (I->getParent())
   3167       I->eraseFromParent();
   3168   } else {
   3169     Worklist.insert(I);
   3170   }
   3171 
   3172   // Note that we must test the size on each iteration, the worklist can grow.
   3173   for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
   3174     I = Worklist[Idx];
   3175 
   3176     // See if this instruction simplifies.
   3177     SimpleV = SimplifyInstruction(I, TD, TLI, DT);
   3178     if (!SimpleV)
   3179       continue;
   3180 
   3181     Simplified = true;
   3182 
   3183     // Stash away all the uses of the old instruction so we can check them for
   3184     // recursive simplifications after a RAUW. This is cheaper than checking all
   3185     // uses of To on the recursive step in most cases.
   3186     for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE;
   3187          ++UI)
   3188       Worklist.insert(cast<Instruction>(*UI));
   3189 
   3190     // Replace the instruction with its simplified value.
   3191     I->replaceAllUsesWith(SimpleV);
   3192 
   3193     // Gracefully handle edge cases where the instruction is not wired into any
   3194     // parent block.
   3195     if (I->getParent())
   3196       I->eraseFromParent();
   3197   }
   3198   return Simplified;
   3199 }
   3200 
   3201 bool llvm::recursivelySimplifyInstruction(Instruction *I,
   3202                                           const DataLayout *TD,
   3203                                           const TargetLibraryInfo *TLI,
   3204                                           const DominatorTree *DT) {
   3205   return replaceAndRecursivelySimplifyImpl(I, 0, TD, TLI, DT);
   3206 }
   3207 
   3208 bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
   3209                                          const DataLayout *TD,
   3210                                          const TargetLibraryInfo *TLI,
   3211                                          const DominatorTree *DT) {
   3212   assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
   3213   assert(SimpleV && "Must provide a simplified value.");
   3214   return replaceAndRecursivelySimplifyImpl(I, SimpleV, TD, TLI, DT);
   3215 }
   3216