]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Analysis/InstructionSimplify.cpp
Merge llvm, clang, lld and lldb release_40 branch r292009. Also update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Analysis / InstructionSimplify.cpp
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 #include "llvm/Analysis/InstructionSimplify.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/CaptureTracking.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26 #include "llvm/Analysis/MemoryBuiltins.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/Analysis/VectorUtils.h"
29 #include "llvm/IR/ConstantRange.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/GetElementPtrTypeIterator.h"
33 #include "llvm/IR/GlobalAlias.h"
34 #include "llvm/IR/Operator.h"
35 #include "llvm/IR/PatternMatch.h"
36 #include "llvm/IR/ValueHandle.h"
37 #include <algorithm>
38 using namespace llvm;
39 using namespace llvm::PatternMatch;
40
41 #define DEBUG_TYPE "instsimplify"
42
43 enum { RecursionLimit = 3 };
44
45 STATISTIC(NumExpand,  "Number of expansions");
46 STATISTIC(NumReassoc, "Number of reassociations");
47
48 namespace {
49 struct Query {
50   const DataLayout &DL;
51   const TargetLibraryInfo *TLI;
52   const DominatorTree *DT;
53   AssumptionCache *AC;
54   const Instruction *CxtI;
55
56   Query(const DataLayout &DL, const TargetLibraryInfo *tli,
57         const DominatorTree *dt, AssumptionCache *ac = nullptr,
58         const Instruction *cxti = nullptr)
59       : DL(DL), TLI(tli), DT(dt), AC(ac), CxtI(cxti) {}
60 };
61 } // end anonymous namespace
62
63 static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned);
64 static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &,
65                             unsigned);
66 static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &,
67                               const Query &, unsigned);
68 static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &,
69                               unsigned);
70 static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
71                                const Query &Q, unsigned MaxRecurse);
72 static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned);
73 static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned);
74 static Value *SimplifyCastInst(unsigned, Value *, Type *,
75                                const Query &, unsigned);
76
77 /// For a boolean type, or a vector of boolean type, return false, or
78 /// a vector with every element false, as appropriate for the type.
79 static Constant *getFalse(Type *Ty) {
80   assert(Ty->getScalarType()->isIntegerTy(1) &&
81          "Expected i1 type or a vector of i1!");
82   return Constant::getNullValue(Ty);
83 }
84
85 /// For a boolean type, or a vector of boolean type, return true, or
86 /// a vector with every element true, as appropriate for the type.
87 static Constant *getTrue(Type *Ty) {
88   assert(Ty->getScalarType()->isIntegerTy(1) &&
89          "Expected i1 type or a vector of i1!");
90   return Constant::getAllOnesValue(Ty);
91 }
92
93 /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
94 static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
95                           Value *RHS) {
96   CmpInst *Cmp = dyn_cast<CmpInst>(V);
97   if (!Cmp)
98     return false;
99   CmpInst::Predicate CPred = Cmp->getPredicate();
100   Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
101   if (CPred == Pred && CLHS == LHS && CRHS == RHS)
102     return true;
103   return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
104     CRHS == LHS;
105 }
106
107 /// Does the given value dominate the specified phi node?
108 static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
109   Instruction *I = dyn_cast<Instruction>(V);
110   if (!I)
111     // Arguments and constants dominate all instructions.
112     return true;
113
114   // If we are processing instructions (and/or basic blocks) that have not been
115   // fully added to a function, the parent nodes may still be null. Simply
116   // return the conservative answer in these cases.
117   if (!I->getParent() || !P->getParent() || !I->getParent()->getParent())
118     return false;
119
120   // If we have a DominatorTree then do a precise test.
121   if (DT) {
122     if (!DT->isReachableFromEntry(P->getParent()))
123       return true;
124     if (!DT->isReachableFromEntry(I->getParent()))
125       return false;
126     return DT->dominates(I, P);
127   }
128
129   // Otherwise, if the instruction is in the entry block and is not an invoke,
130   // then it obviously dominates all phi nodes.
131   if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() &&
132       !isa<InvokeInst>(I))
133     return true;
134
135   return false;
136 }
137
138 /// Simplify "A op (B op' C)" by distributing op over op', turning it into
139 /// "(A op B) op' (A op C)".  Here "op" is given by Opcode and "op'" is
140 /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
141 /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
142 /// Returns the simplified value, or null if no simplification was performed.
143 static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS,
144                           unsigned OpcToExpand, const Query &Q,
145                           unsigned MaxRecurse) {
146   Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand;
147   // Recursion is always used, so bail out at once if we already hit the limit.
148   if (!MaxRecurse--)
149     return nullptr;
150
151   // Check whether the expression has the form "(A op' B) op C".
152   if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
153     if (Op0->getOpcode() == OpcodeToExpand) {
154       // It does!  Try turning it into "(A op C) op' (B op C)".
155       Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
156       // Do "A op C" and "B op C" both simplify?
157       if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse))
158         if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
159           // They do! Return "L op' R" if it simplifies or is already available.
160           // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
161           if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand)
162                                      && L == B && R == A)) {
163             ++NumExpand;
164             return LHS;
165           }
166           // Otherwise return "L op' R" if it simplifies.
167           if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) {
168             ++NumExpand;
169             return V;
170           }
171         }
172     }
173
174   // Check whether the expression has the form "A op (B op' C)".
175   if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
176     if (Op1->getOpcode() == OpcodeToExpand) {
177       // It does!  Try turning it into "(A op B) op' (A op C)".
178       Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
179       // Do "A op B" and "A op C" both simplify?
180       if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse))
181         if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) {
182           // They do! Return "L op' R" if it simplifies or is already available.
183           // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
184           if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand)
185                                      && L == C && R == B)) {
186             ++NumExpand;
187             return RHS;
188           }
189           // Otherwise return "L op' R" if it simplifies.
190           if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) {
191             ++NumExpand;
192             return V;
193           }
194         }
195     }
196
197   return nullptr;
198 }
199
200 /// Generic simplifications for associative binary operations.
201 /// Returns the simpler value, or null if none was found.
202 static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS,
203                                        const Query &Q, unsigned MaxRecurse) {
204   Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc;
205   assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
206
207   // Recursion is always used, so bail out at once if we already hit the limit.
208   if (!MaxRecurse--)
209     return nullptr;
210
211   BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
212   BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
213
214   // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
215   if (Op0 && Op0->getOpcode() == Opcode) {
216     Value *A = Op0->getOperand(0);
217     Value *B = Op0->getOperand(1);
218     Value *C = RHS;
219
220     // Does "B op C" simplify?
221     if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
222       // It does!  Return "A op V" if it simplifies or is already available.
223       // If V equals B then "A op V" is just the LHS.
224       if (V == B) return LHS;
225       // Otherwise return "A op V" if it simplifies.
226       if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
227         ++NumReassoc;
228         return W;
229       }
230     }
231   }
232
233   // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
234   if (Op1 && Op1->getOpcode() == Opcode) {
235     Value *A = LHS;
236     Value *B = Op1->getOperand(0);
237     Value *C = Op1->getOperand(1);
238
239     // Does "A op B" simplify?
240     if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
241       // It does!  Return "V op C" if it simplifies or is already available.
242       // If V equals B then "V op C" is just the RHS.
243       if (V == B) return RHS;
244       // Otherwise return "V op C" if it simplifies.
245       if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
246         ++NumReassoc;
247         return W;
248       }
249     }
250   }
251
252   // The remaining transforms require commutativity as well as associativity.
253   if (!Instruction::isCommutative(Opcode))
254     return nullptr;
255
256   // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
257   if (Op0 && Op0->getOpcode() == Opcode) {
258     Value *A = Op0->getOperand(0);
259     Value *B = Op0->getOperand(1);
260     Value *C = RHS;
261
262     // Does "C op A" simplify?
263     if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
264       // It does!  Return "V op B" if it simplifies or is already available.
265       // If V equals A then "V op B" is just the LHS.
266       if (V == A) return LHS;
267       // Otherwise return "V op B" if it simplifies.
268       if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
269         ++NumReassoc;
270         return W;
271       }
272     }
273   }
274
275   // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
276   if (Op1 && Op1->getOpcode() == Opcode) {
277     Value *A = LHS;
278     Value *B = Op1->getOperand(0);
279     Value *C = Op1->getOperand(1);
280
281     // Does "C op A" simplify?
282     if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
283       // It does!  Return "B op V" if it simplifies or is already available.
284       // If V equals C then "B op V" is just the RHS.
285       if (V == C) return RHS;
286       // Otherwise return "B op V" if it simplifies.
287       if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
288         ++NumReassoc;
289         return W;
290       }
291     }
292   }
293
294   return nullptr;
295 }
296
297 /// In the case of a binary operation with a select instruction as an operand,
298 /// try to simplify the binop by seeing whether evaluating it on both branches
299 /// of the select results in the same value. Returns the common value if so,
300 /// otherwise returns null.
301 static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
302                                     const Query &Q, unsigned MaxRecurse) {
303   // Recursion is always used, so bail out at once if we already hit the limit.
304   if (!MaxRecurse--)
305     return nullptr;
306
307   SelectInst *SI;
308   if (isa<SelectInst>(LHS)) {
309     SI = cast<SelectInst>(LHS);
310   } else {
311     assert(isa<SelectInst>(RHS) && "No select instruction operand!");
312     SI = cast<SelectInst>(RHS);
313   }
314
315   // Evaluate the BinOp on the true and false branches of the select.
316   Value *TV;
317   Value *FV;
318   if (SI == LHS) {
319     TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
320     FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
321   } else {
322     TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
323     FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
324   }
325
326   // If they simplified to the same value, then return the common value.
327   // If they both failed to simplify then return null.
328   if (TV == FV)
329     return TV;
330
331   // If one branch simplified to undef, return the other one.
332   if (TV && isa<UndefValue>(TV))
333     return FV;
334   if (FV && isa<UndefValue>(FV))
335     return TV;
336
337   // If applying the operation did not change the true and false select values,
338   // then the result of the binop is the select itself.
339   if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
340     return SI;
341
342   // If one branch simplified and the other did not, and the simplified
343   // value is equal to the unsimplified one, return the simplified value.
344   // For example, select (cond, X, X & Z) & Z -> X & Z.
345   if ((FV && !TV) || (TV && !FV)) {
346     // Check that the simplified value has the form "X op Y" where "op" is the
347     // same as the original operation.
348     Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
349     if (Simplified && Simplified->getOpcode() == Opcode) {
350       // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
351       // We already know that "op" is the same as for the simplified value.  See
352       // if the operands match too.  If so, return the simplified value.
353       Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
354       Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
355       Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
356       if (Simplified->getOperand(0) == UnsimplifiedLHS &&
357           Simplified->getOperand(1) == UnsimplifiedRHS)
358         return Simplified;
359       if (Simplified->isCommutative() &&
360           Simplified->getOperand(1) == UnsimplifiedLHS &&
361           Simplified->getOperand(0) == UnsimplifiedRHS)
362         return Simplified;
363     }
364   }
365
366   return nullptr;
367 }
368
369 /// In the case of a comparison with a select instruction, try to simplify the
370 /// comparison by seeing whether both branches of the select result in the same
371 /// value. Returns the common value if so, otherwise returns null.
372 static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
373                                   Value *RHS, const Query &Q,
374                                   unsigned MaxRecurse) {
375   // Recursion is always used, so bail out at once if we already hit the limit.
376   if (!MaxRecurse--)
377     return nullptr;
378
379   // Make sure the select is on the LHS.
380   if (!isa<SelectInst>(LHS)) {
381     std::swap(LHS, RHS);
382     Pred = CmpInst::getSwappedPredicate(Pred);
383   }
384   assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
385   SelectInst *SI = cast<SelectInst>(LHS);
386   Value *Cond = SI->getCondition();
387   Value *TV = SI->getTrueValue();
388   Value *FV = SI->getFalseValue();
389
390   // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
391   // Does "cmp TV, RHS" simplify?
392   Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse);
393   if (TCmp == Cond) {
394     // It not only simplified, it simplified to the select condition.  Replace
395     // it with 'true'.
396     TCmp = getTrue(Cond->getType());
397   } else if (!TCmp) {
398     // It didn't simplify.  However if "cmp TV, RHS" is equal to the select
399     // condition then we can replace it with 'true'.  Otherwise give up.
400     if (!isSameCompare(Cond, Pred, TV, RHS))
401       return nullptr;
402     TCmp = getTrue(Cond->getType());
403   }
404
405   // Does "cmp FV, RHS" simplify?
406   Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse);
407   if (FCmp == Cond) {
408     // It not only simplified, it simplified to the select condition.  Replace
409     // it with 'false'.
410     FCmp = getFalse(Cond->getType());
411   } else if (!FCmp) {
412     // It didn't simplify.  However if "cmp FV, RHS" is equal to the select
413     // condition then we can replace it with 'false'.  Otherwise give up.
414     if (!isSameCompare(Cond, Pred, FV, RHS))
415       return nullptr;
416     FCmp = getFalse(Cond->getType());
417   }
418
419   // If both sides simplified to the same value, then use it as the result of
420   // the original comparison.
421   if (TCmp == FCmp)
422     return TCmp;
423
424   // The remaining cases only make sense if the select condition has the same
425   // type as the result of the comparison, so bail out if this is not so.
426   if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy())
427     return nullptr;
428   // If the false value simplified to false, then the result of the compare
429   // is equal to "Cond && TCmp".  This also catches the case when the false
430   // value simplified to false and the true value to true, returning "Cond".
431   if (match(FCmp, m_Zero()))
432     if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse))
433       return V;
434   // If the true value simplified to true, then the result of the compare
435   // is equal to "Cond || FCmp".
436   if (match(TCmp, m_One()))
437     if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse))
438       return V;
439   // Finally, if the false value simplified to true and the true value to
440   // false, then the result of the compare is equal to "!Cond".
441   if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
442     if (Value *V =
443         SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
444                         Q, MaxRecurse))
445       return V;
446
447   return nullptr;
448 }
449
450 /// In the case of a binary operation with an operand that is a PHI instruction,
451 /// try to simplify the binop by seeing whether evaluating it on the incoming
452 /// phi values yields the same result for every value. If so returns the common
453 /// value, otherwise returns null.
454 static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
455                                  const Query &Q, unsigned MaxRecurse) {
456   // Recursion is always used, so bail out at once if we already hit the limit.
457   if (!MaxRecurse--)
458     return nullptr;
459
460   PHINode *PI;
461   if (isa<PHINode>(LHS)) {
462     PI = cast<PHINode>(LHS);
463     // Bail out if RHS and the phi may be mutually interdependent due to a loop.
464     if (!ValueDominatesPHI(RHS, PI, Q.DT))
465       return nullptr;
466   } else {
467     assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
468     PI = cast<PHINode>(RHS);
469     // Bail out if LHS and the phi may be mutually interdependent due to a loop.
470     if (!ValueDominatesPHI(LHS, PI, Q.DT))
471       return nullptr;
472   }
473
474   // Evaluate the BinOp on the incoming phi values.
475   Value *CommonValue = nullptr;
476   for (Value *Incoming : PI->incoming_values()) {
477     // If the incoming value is the phi node itself, it can safely be skipped.
478     if (Incoming == PI) continue;
479     Value *V = PI == LHS ?
480       SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) :
481       SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse);
482     // If the operation failed to simplify, or simplified to a different value
483     // to previously, then give up.
484     if (!V || (CommonValue && V != CommonValue))
485       return nullptr;
486     CommonValue = V;
487   }
488
489   return CommonValue;
490 }
491
492 /// In the case of a comparison with a PHI instruction, try to simplify the
493 /// comparison by seeing whether comparing with all of the incoming phi values
494 /// yields the same result every time. If so returns the common result,
495 /// otherwise returns null.
496 static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
497                                const Query &Q, unsigned MaxRecurse) {
498   // Recursion is always used, so bail out at once if we already hit the limit.
499   if (!MaxRecurse--)
500     return nullptr;
501
502   // Make sure the phi is on the LHS.
503   if (!isa<PHINode>(LHS)) {
504     std::swap(LHS, RHS);
505     Pred = CmpInst::getSwappedPredicate(Pred);
506   }
507   assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
508   PHINode *PI = cast<PHINode>(LHS);
509
510   // Bail out if RHS and the phi may be mutually interdependent due to a loop.
511   if (!ValueDominatesPHI(RHS, PI, Q.DT))
512     return nullptr;
513
514   // Evaluate the BinOp on the incoming phi values.
515   Value *CommonValue = nullptr;
516   for (Value *Incoming : PI->incoming_values()) {
517     // If the incoming value is the phi node itself, it can safely be skipped.
518     if (Incoming == PI) continue;
519     Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse);
520     // If the operation failed to simplify, or simplified to a different value
521     // to previously, then give up.
522     if (!V || (CommonValue && V != CommonValue))
523       return nullptr;
524     CommonValue = V;
525   }
526
527   return CommonValue;
528 }
529
530 /// Given operands for an Add, see if we can fold the result.
531 /// If not, this returns null.
532 static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
533                               const Query &Q, unsigned MaxRecurse) {
534   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
535     if (Constant *CRHS = dyn_cast<Constant>(Op1))
536       return ConstantFoldBinaryOpOperands(Instruction::Add, CLHS, CRHS, Q.DL);
537
538     // Canonicalize the constant to the RHS.
539     std::swap(Op0, Op1);
540   }
541
542   // X + undef -> undef
543   if (match(Op1, m_Undef()))
544     return Op1;
545
546   // X + 0 -> X
547   if (match(Op1, m_Zero()))
548     return Op0;
549
550   // X + (Y - X) -> Y
551   // (Y - X) + X -> Y
552   // Eg: X + -X -> 0
553   Value *Y = nullptr;
554   if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
555       match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
556     return Y;
557
558   // X + ~X -> -1   since   ~X = -X-1
559   if (match(Op0, m_Not(m_Specific(Op1))) ||
560       match(Op1, m_Not(m_Specific(Op0))))
561     return Constant::getAllOnesValue(Op0->getType());
562
563   /// i1 add -> xor.
564   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
565     if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
566       return V;
567
568   // Try some generic simplifications for associative operations.
569   if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q,
570                                           MaxRecurse))
571     return V;
572
573   // Threading Add over selects and phi nodes is pointless, so don't bother.
574   // Threading over the select in "A + select(cond, B, C)" means evaluating
575   // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
576   // only if B and C are equal.  If B and C are equal then (since we assume
577   // that operands have already been simplified) "select(cond, B, C)" should
578   // have been simplified to the common value of B and C already.  Analysing
579   // "A+B" and "A+C" thus gains nothing, but costs compile time.  Similarly
580   // for threading over phi nodes.
581
582   return nullptr;
583 }
584
585 Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
586                              const DataLayout &DL, const TargetLibraryInfo *TLI,
587                              const DominatorTree *DT, AssumptionCache *AC,
588                              const Instruction *CxtI) {
589   return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI),
590                            RecursionLimit);
591 }
592
593 /// \brief Compute the base pointer and cumulative constant offsets for V.
594 ///
595 /// This strips all constant offsets off of V, leaving it the base pointer, and
596 /// accumulates the total constant offset applied in the returned constant. It
597 /// returns 0 if V is not a pointer, and returns the constant '0' if there are
598 /// no constant offsets applied.
599 ///
600 /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't
601 /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc.
602 /// folding.
603 static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
604                                                 bool AllowNonInbounds = false) {
605   assert(V->getType()->getScalarType()->isPointerTy());
606
607   Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType();
608   APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth());
609
610   // Even though we don't look through PHI nodes, we could be called on an
611   // instruction in an unreachable block, which may be on a cycle.
612   SmallPtrSet<Value *, 4> Visited;
613   Visited.insert(V);
614   do {
615     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
616       if ((!AllowNonInbounds && !GEP->isInBounds()) ||
617           !GEP->accumulateConstantOffset(DL, Offset))
618         break;
619       V = GEP->getPointerOperand();
620     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
621       V = cast<Operator>(V)->getOperand(0);
622     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
623       if (GA->isInterposable())
624         break;
625       V = GA->getAliasee();
626     } else {
627       if (auto CS = CallSite(V))
628         if (Value *RV = CS.getReturnedArgOperand()) {
629           V = RV;
630           continue;
631         }
632       break;
633     }
634     assert(V->getType()->getScalarType()->isPointerTy() &&
635            "Unexpected operand type!");
636   } while (Visited.insert(V).second);
637
638   Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset);
639   if (V->getType()->isVectorTy())
640     return ConstantVector::getSplat(V->getType()->getVectorNumElements(),
641                                     OffsetIntPtr);
642   return OffsetIntPtr;
643 }
644
645 /// \brief Compute the constant difference between two pointer values.
646 /// If the difference is not a constant, returns zero.
647 static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
648                                           Value *RHS) {
649   Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
650   Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
651
652   // If LHS and RHS are not related via constant offsets to the same base
653   // value, there is nothing we can do here.
654   if (LHS != RHS)
655     return nullptr;
656
657   // Otherwise, the difference of LHS - RHS can be computed as:
658   //    LHS - RHS
659   //  = (LHSOffset + Base) - (RHSOffset + Base)
660   //  = LHSOffset - RHSOffset
661   return ConstantExpr::getSub(LHSOffset, RHSOffset);
662 }
663
664 /// Given operands for a Sub, see if we can fold the result.
665 /// If not, this returns null.
666 static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
667                               const Query &Q, unsigned MaxRecurse) {
668   if (Constant *CLHS = dyn_cast<Constant>(Op0))
669     if (Constant *CRHS = dyn_cast<Constant>(Op1))
670       return ConstantFoldBinaryOpOperands(Instruction::Sub, CLHS, CRHS, Q.DL);
671
672   // X - undef -> undef
673   // undef - X -> undef
674   if (match(Op0, m_Undef()) || match(Op1, m_Undef()))
675     return UndefValue::get(Op0->getType());
676
677   // X - 0 -> X
678   if (match(Op1, m_Zero()))
679     return Op0;
680
681   // X - X -> 0
682   if (Op0 == Op1)
683     return Constant::getNullValue(Op0->getType());
684
685   // Is this a negation?
686   if (match(Op0, m_Zero())) {
687     // 0 - X -> 0 if the sub is NUW.
688     if (isNUW)
689       return Op0;
690
691     unsigned BitWidth = Op1->getType()->getScalarSizeInBits();
692     APInt KnownZero(BitWidth, 0);
693     APInt KnownOne(BitWidth, 0);
694     computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
695     if (KnownZero == ~APInt::getSignBit(BitWidth)) {
696       // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
697       // Op1 must be 0 because negating the minimum signed value is undefined.
698       if (isNSW)
699         return Op0;
700
701       // 0 - X -> X if X is 0 or the minimum signed value.
702       return Op1;
703     }
704   }
705
706   // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
707   // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
708   Value *X = nullptr, *Y = nullptr, *Z = Op1;
709   if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
710     // See if "V === Y - Z" simplifies.
711     if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1))
712       // It does!  Now see if "X + V" simplifies.
713       if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) {
714         // It does, we successfully reassociated!
715         ++NumReassoc;
716         return W;
717       }
718     // See if "V === X - Z" simplifies.
719     if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
720       // It does!  Now see if "Y + V" simplifies.
721       if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) {
722         // It does, we successfully reassociated!
723         ++NumReassoc;
724         return W;
725       }
726   }
727
728   // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
729   // For example, X - (X + 1) -> -1
730   X = Op0;
731   if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
732     // See if "V === X - Y" simplifies.
733     if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
734       // It does!  Now see if "V - Z" simplifies.
735       if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) {
736         // It does, we successfully reassociated!
737         ++NumReassoc;
738         return W;
739       }
740     // See if "V === X - Z" simplifies.
741     if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
742       // It does!  Now see if "V - Y" simplifies.
743       if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) {
744         // It does, we successfully reassociated!
745         ++NumReassoc;
746         return W;
747       }
748   }
749
750   // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
751   // For example, X - (X - Y) -> Y.
752   Z = Op0;
753   if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
754     // See if "V === Z - X" simplifies.
755     if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1))
756       // It does!  Now see if "V + Y" simplifies.
757       if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) {
758         // It does, we successfully reassociated!
759         ++NumReassoc;
760         return W;
761       }
762
763   // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
764   if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
765       match(Op1, m_Trunc(m_Value(Y))))
766     if (X->getType() == Y->getType())
767       // See if "V === X - Y" simplifies.
768       if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
769         // It does!  Now see if "trunc V" simplifies.
770         if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(),
771                                         Q, MaxRecurse - 1))
772           // It does, return the simplified "trunc V".
773           return W;
774
775   // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
776   if (match(Op0, m_PtrToInt(m_Value(X))) &&
777       match(Op1, m_PtrToInt(m_Value(Y))))
778     if (Constant *Result = computePointerDifference(Q.DL, X, Y))
779       return ConstantExpr::getIntegerCast(Result, Op0->getType(), true);
780
781   // i1 sub -> xor.
782   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
783     if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
784       return V;
785
786   // Threading Sub over selects and phi nodes is pointless, so don't bother.
787   // Threading over the select in "A - select(cond, B, C)" means evaluating
788   // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
789   // only if B and C are equal.  If B and C are equal then (since we assume
790   // that operands have already been simplified) "select(cond, B, C)" should
791   // have been simplified to the common value of B and C already.  Analysing
792   // "A-B" and "A-C" thus gains nothing, but costs compile time.  Similarly
793   // for threading over phi nodes.
794
795   return nullptr;
796 }
797
798 Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
799                              const DataLayout &DL, const TargetLibraryInfo *TLI,
800                              const DominatorTree *DT, AssumptionCache *AC,
801                              const Instruction *CxtI) {
802   return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI),
803                            RecursionLimit);
804 }
805
806 /// Given operands for an FAdd, see if we can fold the result.  If not, this
807 /// returns null.
808 static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
809                               const Query &Q, unsigned MaxRecurse) {
810   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
811     if (Constant *CRHS = dyn_cast<Constant>(Op1))
812       return ConstantFoldBinaryOpOperands(Instruction::FAdd, CLHS, CRHS, Q.DL);
813
814     // Canonicalize the constant to the RHS.
815     std::swap(Op0, Op1);
816   }
817
818   // fadd X, -0 ==> X
819   if (match(Op1, m_NegZero()))
820     return Op0;
821
822   // fadd X, 0 ==> X, when we know X is not -0
823   if (match(Op1, m_Zero()) &&
824       (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
825     return Op0;
826
827   // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0
828   //   where nnan and ninf have to occur at least once somewhere in this
829   //   expression
830   Value *SubOp = nullptr;
831   if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0))))
832     SubOp = Op1;
833   else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1))))
834     SubOp = Op0;
835   if (SubOp) {
836     Instruction *FSub = cast<Instruction>(SubOp);
837     if ((FMF.noNaNs() || FSub->hasNoNaNs()) &&
838         (FMF.noInfs() || FSub->hasNoInfs()))
839       return Constant::getNullValue(Op0->getType());
840   }
841
842   return nullptr;
843 }
844
845 /// Given operands for an FSub, see if we can fold the result.  If not, this
846 /// returns null.
847 static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
848                               const Query &Q, unsigned MaxRecurse) {
849   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
850     if (Constant *CRHS = dyn_cast<Constant>(Op1))
851       return ConstantFoldBinaryOpOperands(Instruction::FSub, CLHS, CRHS, Q.DL);
852   }
853
854   // fsub X, 0 ==> X
855   if (match(Op1, m_Zero()))
856     return Op0;
857
858   // fsub X, -0 ==> X, when we know X is not -0
859   if (match(Op1, m_NegZero()) &&
860       (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
861     return Op0;
862
863   // fsub -0.0, (fsub -0.0, X) ==> X
864   Value *X;
865   if (match(Op0, m_NegZero()) && match(Op1, m_FSub(m_NegZero(), m_Value(X))))
866     return X;
867
868   // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
869   if (FMF.noSignedZeros() && match(Op0, m_AnyZero()) &&
870       match(Op1, m_FSub(m_AnyZero(), m_Value(X))))
871     return X;
872
873   // fsub nnan x, x ==> 0.0
874   if (FMF.noNaNs() && Op0 == Op1)
875     return Constant::getNullValue(Op0->getType());
876
877   return nullptr;
878 }
879
880 /// Given the operands for an FMul, see if we can fold the result
881 static Value *SimplifyFMulInst(Value *Op0, Value *Op1,
882                                FastMathFlags FMF,
883                                const Query &Q,
884                                unsigned MaxRecurse) {
885  if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
886     if (Constant *CRHS = dyn_cast<Constant>(Op1))
887       return ConstantFoldBinaryOpOperands(Instruction::FMul, CLHS, CRHS, Q.DL);
888
889     // Canonicalize the constant to the RHS.
890     std::swap(Op0, Op1);
891  }
892
893  // fmul X, 1.0 ==> X
894  if (match(Op1, m_FPOne()))
895    return Op0;
896
897  // fmul nnan nsz X, 0 ==> 0
898  if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero()))
899    return Op1;
900
901  return nullptr;
902 }
903
904 /// Given operands for a Mul, see if we can fold the result.
905 /// If not, this returns null.
906 static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q,
907                               unsigned MaxRecurse) {
908   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
909     if (Constant *CRHS = dyn_cast<Constant>(Op1))
910       return ConstantFoldBinaryOpOperands(Instruction::Mul, CLHS, CRHS, Q.DL);
911
912     // Canonicalize the constant to the RHS.
913     std::swap(Op0, Op1);
914   }
915
916   // X * undef -> 0
917   if (match(Op1, m_Undef()))
918     return Constant::getNullValue(Op0->getType());
919
920   // X * 0 -> 0
921   if (match(Op1, m_Zero()))
922     return Op1;
923
924   // X * 1 -> X
925   if (match(Op1, m_One()))
926     return Op0;
927
928   // (X / Y) * Y -> X if the division is exact.
929   Value *X = nullptr;
930   if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
931       match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))   // Y * (X / Y)
932     return X;
933
934   // i1 mul -> and.
935   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
936     if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1))
937       return V;
938
939   // Try some generic simplifications for associative operations.
940   if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q,
941                                           MaxRecurse))
942     return V;
943
944   // Mul distributes over Add.  Try some generic simplifications based on this.
945   if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
946                              Q, MaxRecurse))
947     return V;
948
949   // If the operation is with the result of a select instruction, check whether
950   // operating on either branch of the select always yields the same value.
951   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
952     if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q,
953                                          MaxRecurse))
954       return V;
955
956   // If the operation is with the result of a phi instruction, check whether
957   // operating on all incoming values of the phi always yields the same value.
958   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
959     if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q,
960                                       MaxRecurse))
961       return V;
962
963   return nullptr;
964 }
965
966 Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
967                               const DataLayout &DL,
968                               const TargetLibraryInfo *TLI,
969                               const DominatorTree *DT, AssumptionCache *AC,
970                               const Instruction *CxtI) {
971   return ::SimplifyFAddInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
972                             RecursionLimit);
973 }
974
975 Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
976                               const DataLayout &DL,
977                               const TargetLibraryInfo *TLI,
978                               const DominatorTree *DT, AssumptionCache *AC,
979                               const Instruction *CxtI) {
980   return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
981                             RecursionLimit);
982 }
983
984 Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
985                               const DataLayout &DL,
986                               const TargetLibraryInfo *TLI,
987                               const DominatorTree *DT, AssumptionCache *AC,
988                               const Instruction *CxtI) {
989   return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
990                             RecursionLimit);
991 }
992
993 Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout &DL,
994                              const TargetLibraryInfo *TLI,
995                              const DominatorTree *DT, AssumptionCache *AC,
996                              const Instruction *CxtI) {
997   return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
998                            RecursionLimit);
999 }
1000
1001 /// Given operands for an SDiv or UDiv, see if we can fold the result.
1002 /// If not, this returns null.
1003 static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1004                           const Query &Q, unsigned MaxRecurse) {
1005   if (Constant *C0 = dyn_cast<Constant>(Op0))
1006     if (Constant *C1 = dyn_cast<Constant>(Op1))
1007       return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL);
1008
1009   bool isSigned = Opcode == Instruction::SDiv;
1010
1011   // X / undef -> undef
1012   if (match(Op1, m_Undef()))
1013     return Op1;
1014
1015   // X / 0 -> undef, we don't need to preserve faults!
1016   if (match(Op1, m_Zero()))
1017     return UndefValue::get(Op1->getType());
1018
1019   // undef / X -> 0
1020   if (match(Op0, m_Undef()))
1021     return Constant::getNullValue(Op0->getType());
1022
1023   // 0 / X -> 0, we don't need to preserve faults!
1024   if (match(Op0, m_Zero()))
1025     return Op0;
1026
1027   // X / 1 -> X
1028   if (match(Op1, m_One()))
1029     return Op0;
1030
1031   if (Op0->getType()->isIntegerTy(1))
1032     // It can't be division by zero, hence it must be division by one.
1033     return Op0;
1034
1035   // X / X -> 1
1036   if (Op0 == Op1)
1037     return ConstantInt::get(Op0->getType(), 1);
1038
1039   // (X * Y) / Y -> X if the multiplication does not overflow.
1040   Value *X = nullptr, *Y = nullptr;
1041   if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) {
1042     if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1
1043     OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0);
1044     // If the Mul knows it does not overflow, then we are good to go.
1045     if ((isSigned && Mul->hasNoSignedWrap()) ||
1046         (!isSigned && Mul->hasNoUnsignedWrap()))
1047       return X;
1048     // If X has the form X = A / Y then X * Y cannot overflow.
1049     if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X))
1050       if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y)
1051         return X;
1052   }
1053
1054   // (X rem Y) / Y -> 0
1055   if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
1056       (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
1057     return Constant::getNullValue(Op0->getType());
1058
1059   // (X /u C1) /u C2 -> 0 if C1 * C2 overflow
1060   ConstantInt *C1, *C2;
1061   if (!isSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) &&
1062       match(Op1, m_ConstantInt(C2))) {
1063     bool Overflow;
1064     C1->getValue().umul_ov(C2->getValue(), Overflow);
1065     if (Overflow)
1066       return Constant::getNullValue(Op0->getType());
1067   }
1068
1069   // If the operation is with the result of a select instruction, check whether
1070   // operating on either branch of the select always yields the same value.
1071   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1072     if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1073       return V;
1074
1075   // If the operation is with the result of a phi instruction, check whether
1076   // operating on all incoming values of the phi always yields the same value.
1077   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1078     if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1079       return V;
1080
1081   return nullptr;
1082 }
1083
1084 /// Given operands for an SDiv, see if we can fold the result.
1085 /// If not, this returns null.
1086 static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q,
1087                                unsigned MaxRecurse) {
1088   if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse))
1089     return V;
1090
1091   return nullptr;
1092 }
1093
1094 Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout &DL,
1095                               const TargetLibraryInfo *TLI,
1096                               const DominatorTree *DT, AssumptionCache *AC,
1097                               const Instruction *CxtI) {
1098   return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1099                             RecursionLimit);
1100 }
1101
1102 /// Given operands for a UDiv, see if we can fold the result.
1103 /// If not, this returns null.
1104 static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q,
1105                                unsigned MaxRecurse) {
1106   if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse))
1107     return V;
1108
1109   // udiv %V, C -> 0 if %V < C
1110   if (MaxRecurse) {
1111     if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst(
1112             ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) {
1113       if (C->isAllOnesValue()) {
1114         return Constant::getNullValue(Op0->getType());
1115       }
1116     }
1117   }
1118
1119   return nullptr;
1120 }
1121
1122 Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout &DL,
1123                               const TargetLibraryInfo *TLI,
1124                               const DominatorTree *DT, AssumptionCache *AC,
1125                               const Instruction *CxtI) {
1126   return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1127                             RecursionLimit);
1128 }
1129
1130 static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1131                                const Query &Q, unsigned) {
1132   // undef / X -> undef    (the undef could be a snan).
1133   if (match(Op0, m_Undef()))
1134     return Op0;
1135
1136   // X / undef -> undef
1137   if (match(Op1, m_Undef()))
1138     return Op1;
1139
1140   // X / 1.0 -> X
1141   if (match(Op1, m_FPOne()))
1142     return Op0;
1143
1144   // 0 / X -> 0
1145   // Requires that NaNs are off (X could be zero) and signed zeroes are
1146   // ignored (X could be positive or negative, so the output sign is unknown).
1147   if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero()))
1148     return Op0;
1149
1150   if (FMF.noNaNs()) {
1151     // X / X -> 1.0 is legal when NaNs are ignored.
1152     if (Op0 == Op1)
1153       return ConstantFP::get(Op0->getType(), 1.0);
1154
1155     // -X /  X -> -1.0 and
1156     //  X / -X -> -1.0 are legal when NaNs are ignored.
1157     // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
1158     if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) &&
1159          BinaryOperator::getFNegArgument(Op0) == Op1) ||
1160         (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) &&
1161          BinaryOperator::getFNegArgument(Op1) == Op0))
1162       return ConstantFP::get(Op0->getType(), -1.0);
1163   }
1164
1165   return nullptr;
1166 }
1167
1168 Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1169                               const DataLayout &DL,
1170                               const TargetLibraryInfo *TLI,
1171                               const DominatorTree *DT, AssumptionCache *AC,
1172                               const Instruction *CxtI) {
1173   return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
1174                             RecursionLimit);
1175 }
1176
1177 /// Given operands for an SRem or URem, see if we can fold the result.
1178 /// If not, this returns null.
1179 static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1180                           const Query &Q, unsigned MaxRecurse) {
1181   if (Constant *C0 = dyn_cast<Constant>(Op0))
1182     if (Constant *C1 = dyn_cast<Constant>(Op1))
1183       return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL);
1184
1185   // X % undef -> undef
1186   if (match(Op1, m_Undef()))
1187     return Op1;
1188
1189   // undef % X -> 0
1190   if (match(Op0, m_Undef()))
1191     return Constant::getNullValue(Op0->getType());
1192
1193   // 0 % X -> 0, we don't need to preserve faults!
1194   if (match(Op0, m_Zero()))
1195     return Op0;
1196
1197   // X % 0 -> undef, we don't need to preserve faults!
1198   if (match(Op1, m_Zero()))
1199     return UndefValue::get(Op0->getType());
1200
1201   // X % 1 -> 0
1202   if (match(Op1, m_One()))
1203     return Constant::getNullValue(Op0->getType());
1204
1205   if (Op0->getType()->isIntegerTy(1))
1206     // It can't be remainder by zero, hence it must be remainder by one.
1207     return Constant::getNullValue(Op0->getType());
1208
1209   // X % X -> 0
1210   if (Op0 == Op1)
1211     return Constant::getNullValue(Op0->getType());
1212
1213   // (X % Y) % Y -> X % Y
1214   if ((Opcode == Instruction::SRem &&
1215        match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
1216       (Opcode == Instruction::URem &&
1217        match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
1218     return Op0;
1219
1220   // If the operation is with the result of a select instruction, check whether
1221   // operating on either branch of the select always yields the same value.
1222   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1223     if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1224       return V;
1225
1226   // If the operation is with the result of a phi instruction, check whether
1227   // operating on all incoming values of the phi always yields the same value.
1228   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1229     if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1230       return V;
1231
1232   return nullptr;
1233 }
1234
1235 /// Given operands for an SRem, see if we can fold the result.
1236 /// If not, this returns null.
1237 static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q,
1238                                unsigned MaxRecurse) {
1239   if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse))
1240     return V;
1241
1242   return nullptr;
1243 }
1244
1245 Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout &DL,
1246                               const TargetLibraryInfo *TLI,
1247                               const DominatorTree *DT, AssumptionCache *AC,
1248                               const Instruction *CxtI) {
1249   return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1250                             RecursionLimit);
1251 }
1252
1253 /// Given operands for a URem, see if we can fold the result.
1254 /// If not, this returns null.
1255 static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q,
1256                                unsigned MaxRecurse) {
1257   if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse))
1258     return V;
1259
1260   // urem %V, C -> %V if %V < C
1261   if (MaxRecurse) {
1262     if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst(
1263             ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) {
1264       if (C->isAllOnesValue()) {
1265         return Op0;
1266       }
1267     }
1268   }
1269
1270   return nullptr;
1271 }
1272
1273 Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout &DL,
1274                               const TargetLibraryInfo *TLI,
1275                               const DominatorTree *DT, AssumptionCache *AC,
1276                               const Instruction *CxtI) {
1277   return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1278                             RecursionLimit);
1279 }
1280
1281 static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1282                                const Query &, unsigned) {
1283   // undef % X -> undef    (the undef could be a snan).
1284   if (match(Op0, m_Undef()))
1285     return Op0;
1286
1287   // X % undef -> undef
1288   if (match(Op1, m_Undef()))
1289     return Op1;
1290
1291   // 0 % X -> 0
1292   // Requires that NaNs are off (X could be zero) and signed zeroes are
1293   // ignored (X could be positive or negative, so the output sign is unknown).
1294   if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero()))
1295     return Op0;
1296
1297   return nullptr;
1298 }
1299
1300 Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
1301                               const DataLayout &DL,
1302                               const TargetLibraryInfo *TLI,
1303                               const DominatorTree *DT, AssumptionCache *AC,
1304                               const Instruction *CxtI) {
1305   return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
1306                             RecursionLimit);
1307 }
1308
1309 /// Returns true if a shift by \c Amount always yields undef.
1310 static bool isUndefShift(Value *Amount) {
1311   Constant *C = dyn_cast<Constant>(Amount);
1312   if (!C)
1313     return false;
1314
1315   // X shift by undef -> undef because it may shift by the bitwidth.
1316   if (isa<UndefValue>(C))
1317     return true;
1318
1319   // Shifting by the bitwidth or more is undefined.
1320   if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1321     if (CI->getValue().getLimitedValue() >=
1322         CI->getType()->getScalarSizeInBits())
1323       return true;
1324
1325   // If all lanes of a vector shift are undefined the whole shift is.
1326   if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) {
1327     for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I)
1328       if (!isUndefShift(C->getAggregateElement(I)))
1329         return false;
1330     return true;
1331   }
1332
1333   return false;
1334 }
1335
1336 /// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1337 /// If not, this returns null.
1338 static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1,
1339                             const Query &Q, unsigned MaxRecurse) {
1340   if (Constant *C0 = dyn_cast<Constant>(Op0))
1341     if (Constant *C1 = dyn_cast<Constant>(Op1))
1342       return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL);
1343
1344   // 0 shift by X -> 0
1345   if (match(Op0, m_Zero()))
1346     return Op0;
1347
1348   // X shift by 0 -> X
1349   if (match(Op1, m_Zero()))
1350     return Op0;
1351
1352   // Fold undefined shifts.
1353   if (isUndefShift(Op1))
1354     return UndefValue::get(Op0->getType());
1355
1356   // If the operation is with the result of a select instruction, check whether
1357   // operating on either branch of the select always yields the same value.
1358   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1359     if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1360       return V;
1361
1362   // If the operation is with the result of a phi instruction, check whether
1363   // operating on all incoming values of the phi always yields the same value.
1364   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1365     if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1366       return V;
1367
1368   // If any bits in the shift amount make that value greater than or equal to
1369   // the number of bits in the type, the shift is undefined.
1370   unsigned BitWidth = Op1->getType()->getScalarSizeInBits();
1371   APInt KnownZero(BitWidth, 0);
1372   APInt KnownOne(BitWidth, 0);
1373   computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1374   if (KnownOne.getLimitedValue() >= BitWidth)
1375     return UndefValue::get(Op0->getType());
1376
1377   // If all valid bits in the shift amount are known zero, the first operand is
1378   // unchanged.
1379   unsigned NumValidShiftBits = Log2_32_Ceil(BitWidth);
1380   APInt ShiftAmountMask = APInt::getLowBitsSet(BitWidth, NumValidShiftBits);
1381   if ((KnownZero & ShiftAmountMask) == ShiftAmountMask)
1382     return Op0;
1383
1384   return nullptr;
1385 }
1386
1387 /// \brief Given operands for an Shl, LShr or AShr, see if we can
1388 /// fold the result.  If not, this returns null.
1389 static Value *SimplifyRightShift(unsigned Opcode, Value *Op0, Value *Op1,
1390                                  bool isExact, const Query &Q,
1391                                  unsigned MaxRecurse) {
1392   if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse))
1393     return V;
1394
1395   // X >> X -> 0
1396   if (Op0 == Op1)
1397     return Constant::getNullValue(Op0->getType());
1398
1399   // undef >> X -> 0
1400   // undef >> X -> undef (if it's exact)
1401   if (match(Op0, m_Undef()))
1402     return isExact ? Op0 : Constant::getNullValue(Op0->getType());
1403
1404   // The low bit cannot be shifted out of an exact shift if it is set.
1405   if (isExact) {
1406     unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
1407     APInt Op0KnownZero(BitWidth, 0);
1408     APInt Op0KnownOne(BitWidth, 0);
1409     computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AC,
1410                      Q.CxtI, Q.DT);
1411     if (Op0KnownOne[0])
1412       return Op0;
1413   }
1414
1415   return nullptr;
1416 }
1417
1418 /// Given operands for an Shl, see if we can fold the result.
1419 /// If not, this returns null.
1420 static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
1421                               const Query &Q, unsigned MaxRecurse) {
1422   if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse))
1423     return V;
1424
1425   // undef << X -> 0
1426   // undef << X -> undef if (if it's NSW/NUW)
1427   if (match(Op0, m_Undef()))
1428     return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType());
1429
1430   // (X >> A) << A -> X
1431   Value *X;
1432   if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
1433     return X;
1434   return nullptr;
1435 }
1436
1437 Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
1438                              const DataLayout &DL, const TargetLibraryInfo *TLI,
1439                              const DominatorTree *DT, AssumptionCache *AC,
1440                              const Instruction *CxtI) {
1441   return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI),
1442                            RecursionLimit);
1443 }
1444
1445 /// Given operands for an LShr, see if we can fold the result.
1446 /// If not, this returns null.
1447 static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
1448                                const Query &Q, unsigned MaxRecurse) {
1449   if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q,
1450                                     MaxRecurse))
1451       return V;
1452
1453   // (X << A) >> A -> X
1454   Value *X;
1455   if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1456     return X;
1457
1458   return nullptr;
1459 }
1460
1461 Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
1462                               const DataLayout &DL,
1463                               const TargetLibraryInfo *TLI,
1464                               const DominatorTree *DT, AssumptionCache *AC,
1465                               const Instruction *CxtI) {
1466   return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI),
1467                             RecursionLimit);
1468 }
1469
1470 /// Given operands for an AShr, see if we can fold the result.
1471 /// If not, this returns null.
1472 static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1473                                const Query &Q, unsigned MaxRecurse) {
1474   if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q,
1475                                     MaxRecurse))
1476     return V;
1477
1478   // all ones >>a X -> all ones
1479   if (match(Op0, m_AllOnes()))
1480     return Op0;
1481
1482   // (X << A) >> A -> X
1483   Value *X;
1484   if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
1485     return X;
1486
1487   // Arithmetic shifting an all-sign-bit value is a no-op.
1488   unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1489   if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1490     return Op0;
1491
1492   return nullptr;
1493 }
1494
1495 Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1496                               const DataLayout &DL,
1497                               const TargetLibraryInfo *TLI,
1498                               const DominatorTree *DT, AssumptionCache *AC,
1499                               const Instruction *CxtI) {
1500   return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI),
1501                             RecursionLimit);
1502 }
1503
1504 static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
1505                                          ICmpInst *UnsignedICmp, bool IsAnd) {
1506   Value *X, *Y;
1507
1508   ICmpInst::Predicate EqPred;
1509   if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1510       !ICmpInst::isEquality(EqPred))
1511     return nullptr;
1512
1513   ICmpInst::Predicate UnsignedPred;
1514   if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1515       ICmpInst::isUnsigned(UnsignedPred))
1516     ;
1517   else if (match(UnsignedICmp,
1518                  m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) &&
1519            ICmpInst::isUnsigned(UnsignedPred))
1520     UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1521   else
1522     return nullptr;
1523
1524   // X < Y && Y != 0  -->  X < Y
1525   // X < Y || Y != 0  -->  Y != 0
1526   if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1527     return IsAnd ? UnsignedICmp : ZeroICmp;
1528
1529   // X >= Y || Y != 0  -->  true
1530   // X >= Y || Y == 0  -->  X >= Y
1531   if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) {
1532     if (EqPred == ICmpInst::ICMP_NE)
1533       return getTrue(UnsignedICmp->getType());
1534     return UnsignedICmp;
1535   }
1536
1537   // X < Y && Y == 0  -->  false
1538   if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1539       IsAnd)
1540     return getFalse(UnsignedICmp->getType());
1541
1542   return nullptr;
1543 }
1544
1545 /// Commuted variants are assumed to be handled by calling this function again
1546 /// with the parameters swapped.
1547 static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) {
1548   ICmpInst::Predicate Pred0, Pred1;
1549   Value *A ,*B;
1550   if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) ||
1551       !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B))))
1552     return nullptr;
1553
1554   // We have (icmp Pred0, A, B) & (icmp Pred1, A, B).
1555   // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we
1556   // can eliminate Op1 from this 'and'.
1557   if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1))
1558     return Op0;
1559
1560   // Check for any combination of predicates that are guaranteed to be disjoint.
1561   if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) ||
1562       (Pred0 == ICmpInst::ICMP_EQ && ICmpInst::isFalseWhenEqual(Pred1)) ||
1563       (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT) ||
1564       (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT))
1565     return getFalse(Op0->getType());
1566
1567   return nullptr;
1568 }
1569
1570 /// Commuted variants are assumed to be handled by calling this function again
1571 /// with the parameters swapped.
1572 static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) {
1573   if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true))
1574     return X;
1575
1576   if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1))
1577     return X;
1578
1579   // Look for this pattern: (icmp V, C0) & (icmp V, C1)).
1580   Type *ITy = Op0->getType();
1581   ICmpInst::Predicate Pred0, Pred1;
1582   const APInt *C0, *C1;
1583   Value *V;
1584   if (match(Op0, m_ICmp(Pred0, m_Value(V), m_APInt(C0))) &&
1585       match(Op1, m_ICmp(Pred1, m_Specific(V), m_APInt(C1)))) {
1586     // Make a constant range that's the intersection of the two icmp ranges.
1587     // If the intersection is empty, we know that the result is false.
1588     auto Range0 = ConstantRange::makeAllowedICmpRegion(Pred0, *C0);
1589     auto Range1 = ConstantRange::makeAllowedICmpRegion(Pred1, *C1);
1590     if (Range0.intersectWith(Range1).isEmptySet())
1591       return getFalse(ITy);
1592   }
1593
1594   // (icmp (add V, C0), C1) & (icmp V, C0)
1595   if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1596     return nullptr;
1597
1598   if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1599     return nullptr;
1600
1601   auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1602   if (AddInst->getOperand(1) != Op1->getOperand(1))
1603     return nullptr;
1604
1605   bool isNSW = AddInst->hasNoSignedWrap();
1606   bool isNUW = AddInst->hasNoUnsignedWrap();
1607
1608   const APInt Delta = *C1 - *C0;
1609   if (C0->isStrictlyPositive()) {
1610     if (Delta == 2) {
1611       if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1612         return getFalse(ITy);
1613       if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW)
1614         return getFalse(ITy);
1615     }
1616     if (Delta == 1) {
1617       if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1618         return getFalse(ITy);
1619       if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW)
1620         return getFalse(ITy);
1621     }
1622   }
1623   if (C0->getBoolValue() && isNUW) {
1624     if (Delta == 2)
1625       if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1626         return getFalse(ITy);
1627     if (Delta == 1)
1628       if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1629         return getFalse(ITy);
1630   }
1631
1632   return nullptr;
1633 }
1634
1635 /// Given operands for an And, see if we can fold the result.
1636 /// If not, this returns null.
1637 static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q,
1638                               unsigned MaxRecurse) {
1639   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1640     if (Constant *CRHS = dyn_cast<Constant>(Op1))
1641       return ConstantFoldBinaryOpOperands(Instruction::And, CLHS, CRHS, Q.DL);
1642
1643     // Canonicalize the constant to the RHS.
1644     std::swap(Op0, Op1);
1645   }
1646
1647   // X & undef -> 0
1648   if (match(Op1, m_Undef()))
1649     return Constant::getNullValue(Op0->getType());
1650
1651   // X & X = X
1652   if (Op0 == Op1)
1653     return Op0;
1654
1655   // X & 0 = 0
1656   if (match(Op1, m_Zero()))
1657     return Op1;
1658
1659   // X & -1 = X
1660   if (match(Op1, m_AllOnes()))
1661     return Op0;
1662
1663   // A & ~A  =  ~A & A  =  0
1664   if (match(Op0, m_Not(m_Specific(Op1))) ||
1665       match(Op1, m_Not(m_Specific(Op0))))
1666     return Constant::getNullValue(Op0->getType());
1667
1668   // (A | ?) & A = A
1669   Value *A = nullptr, *B = nullptr;
1670   if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1671       (A == Op1 || B == Op1))
1672     return Op1;
1673
1674   // A & (A | ?) = A
1675   if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1676       (A == Op0 || B == Op0))
1677     return Op0;
1678
1679   // A & (-A) = A if A is a power of two or zero.
1680   if (match(Op0, m_Neg(m_Specific(Op1))) ||
1681       match(Op1, m_Neg(m_Specific(Op0)))) {
1682     if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
1683                                Q.DT))
1684       return Op0;
1685     if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
1686                                Q.DT))
1687       return Op1;
1688   }
1689
1690   if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) {
1691     if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) {
1692       if (Value *V = SimplifyAndOfICmps(ICILHS, ICIRHS))
1693         return V;
1694       if (Value *V = SimplifyAndOfICmps(ICIRHS, ICILHS))
1695         return V;
1696     }
1697   }
1698
1699   // The compares may be hidden behind casts. Look through those and try the
1700   // same folds as above.
1701   auto *Cast0 = dyn_cast<CastInst>(Op0);
1702   auto *Cast1 = dyn_cast<CastInst>(Op1);
1703   if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1704       Cast0->getSrcTy() == Cast1->getSrcTy()) {
1705     auto *Cmp0 = dyn_cast<ICmpInst>(Cast0->getOperand(0));
1706     auto *Cmp1 = dyn_cast<ICmpInst>(Cast1->getOperand(0));
1707     if (Cmp0 && Cmp1) {
1708       Instruction::CastOps CastOpc = Cast0->getOpcode();
1709       Type *ResultType = Cast0->getType();
1710       if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp0, Cmp1)))
1711         return ConstantExpr::getCast(CastOpc, V, ResultType);
1712       if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp1, Cmp0)))
1713         return ConstantExpr::getCast(CastOpc, V, ResultType);
1714     }
1715   }
1716
1717   // Try some generic simplifications for associative operations.
1718   if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q,
1719                                           MaxRecurse))
1720     return V;
1721
1722   // And distributes over Or.  Try some generic simplifications based on this.
1723   if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
1724                              Q, MaxRecurse))
1725     return V;
1726
1727   // And distributes over Xor.  Try some generic simplifications based on this.
1728   if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
1729                              Q, MaxRecurse))
1730     return V;
1731
1732   // If the operation is with the result of a select instruction, check whether
1733   // operating on either branch of the select always yields the same value.
1734   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1735     if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q,
1736                                          MaxRecurse))
1737       return V;
1738
1739   // If the operation is with the result of a phi instruction, check whether
1740   // operating on all incoming values of the phi always yields the same value.
1741   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1742     if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q,
1743                                       MaxRecurse))
1744       return V;
1745
1746   return nullptr;
1747 }
1748
1749 Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout &DL,
1750                              const TargetLibraryInfo *TLI,
1751                              const DominatorTree *DT, AssumptionCache *AC,
1752                              const Instruction *CxtI) {
1753   return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1754                            RecursionLimit);
1755 }
1756
1757 /// Commuted variants are assumed to be handled by calling this function again
1758 /// with the parameters swapped.
1759 static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) {
1760   ICmpInst::Predicate Pred0, Pred1;
1761   Value *A ,*B;
1762   if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) ||
1763       !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B))))
1764     return nullptr;
1765
1766   // We have (icmp Pred0, A, B) | (icmp Pred1, A, B).
1767   // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we
1768   // can eliminate Op0 from this 'or'.
1769   if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1))
1770     return Op1;
1771
1772   // Check for any combination of predicates that cover the entire range of
1773   // possibilities.
1774   if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) ||
1775       (Pred0 == ICmpInst::ICMP_NE && ICmpInst::isTrueWhenEqual(Pred1)) ||
1776       (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGE) ||
1777       (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGE))
1778     return getTrue(Op0->getType());
1779
1780   return nullptr;
1781 }
1782
1783 /// Commuted variants are assumed to be handled by calling this function again
1784 /// with the parameters swapped.
1785 static Value *SimplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) {
1786   if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false))
1787     return X;
1788
1789   if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1))
1790     return X;
1791
1792   // (icmp (add V, C0), C1) | (icmp V, C0)
1793   ICmpInst::Predicate Pred0, Pred1;
1794   const APInt *C0, *C1;
1795   Value *V;
1796   if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1797     return nullptr;
1798
1799   if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1800     return nullptr;
1801
1802   auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1803   if (AddInst->getOperand(1) != Op1->getOperand(1))
1804     return nullptr;
1805
1806   Type *ITy = Op0->getType();
1807   bool isNSW = AddInst->hasNoSignedWrap();
1808   bool isNUW = AddInst->hasNoUnsignedWrap();
1809
1810   const APInt Delta = *C1 - *C0;
1811   if (C0->isStrictlyPositive()) {
1812     if (Delta == 2) {
1813       if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1814         return getTrue(ITy);
1815       if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW)
1816         return getTrue(ITy);
1817     }
1818     if (Delta == 1) {
1819       if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1820         return getTrue(ITy);
1821       if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW)
1822         return getTrue(ITy);
1823     }
1824   }
1825   if (C0->getBoolValue() && isNUW) {
1826     if (Delta == 2)
1827       if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1828         return getTrue(ITy);
1829     if (Delta == 1)
1830       if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1831         return getTrue(ITy);
1832   }
1833
1834   return nullptr;
1835 }
1836
1837 /// Given operands for an Or, see if we can fold the result.
1838 /// If not, this returns null.
1839 static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q,
1840                              unsigned MaxRecurse) {
1841   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1842     if (Constant *CRHS = dyn_cast<Constant>(Op1))
1843       return ConstantFoldBinaryOpOperands(Instruction::Or, CLHS, CRHS, Q.DL);
1844
1845     // Canonicalize the constant to the RHS.
1846     std::swap(Op0, Op1);
1847   }
1848
1849   // X | undef -> -1
1850   if (match(Op1, m_Undef()))
1851     return Constant::getAllOnesValue(Op0->getType());
1852
1853   // X | X = X
1854   if (Op0 == Op1)
1855     return Op0;
1856
1857   // X | 0 = X
1858   if (match(Op1, m_Zero()))
1859     return Op0;
1860
1861   // X | -1 = -1
1862   if (match(Op1, m_AllOnes()))
1863     return Op1;
1864
1865   // A | ~A  =  ~A | A  =  -1
1866   if (match(Op0, m_Not(m_Specific(Op1))) ||
1867       match(Op1, m_Not(m_Specific(Op0))))
1868     return Constant::getAllOnesValue(Op0->getType());
1869
1870   // (A & ?) | A = A
1871   Value *A = nullptr, *B = nullptr;
1872   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1873       (A == Op1 || B == Op1))
1874     return Op1;
1875
1876   // A | (A & ?) = A
1877   if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
1878       (A == Op0 || B == Op0))
1879     return Op0;
1880
1881   // ~(A & ?) | A = -1
1882   if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) &&
1883       (A == Op1 || B == Op1))
1884     return Constant::getAllOnesValue(Op1->getType());
1885
1886   // A | ~(A & ?) = -1
1887   if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) &&
1888       (A == Op0 || B == Op0))
1889     return Constant::getAllOnesValue(Op0->getType());
1890
1891   if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) {
1892     if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) {
1893       if (Value *V = SimplifyOrOfICmps(ICILHS, ICIRHS))
1894         return V;
1895       if (Value *V = SimplifyOrOfICmps(ICIRHS, ICILHS))
1896         return V;
1897     }
1898   }
1899
1900   // Try some generic simplifications for associative operations.
1901   if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q,
1902                                           MaxRecurse))
1903     return V;
1904
1905   // Or distributes over And.  Try some generic simplifications based on this.
1906   if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q,
1907                              MaxRecurse))
1908     return V;
1909
1910   // If the operation is with the result of a select instruction, check whether
1911   // operating on either branch of the select always yields the same value.
1912   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1913     if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q,
1914                                          MaxRecurse))
1915       return V;
1916
1917   // (A & C)|(B & D)
1918   Value *C = nullptr, *D = nullptr;
1919   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
1920       match(Op1, m_And(m_Value(B), m_Value(D)))) {
1921     ConstantInt *C1 = dyn_cast<ConstantInt>(C);
1922     ConstantInt *C2 = dyn_cast<ConstantInt>(D);
1923     if (C1 && C2 && (C1->getValue() == ~C2->getValue())) {
1924       // (A & C1)|(B & C2)
1925       // If we have: ((V + N) & C1) | (V & C2)
1926       // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
1927       // replace with V+N.
1928       Value *V1, *V2;
1929       if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+
1930           match(A, m_Add(m_Value(V1), m_Value(V2)))) {
1931         // Add commutes, try both ways.
1932         if (V1 == B &&
1933             MaskedValueIsZero(V2, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1934           return A;
1935         if (V2 == B &&
1936             MaskedValueIsZero(V1, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1937           return A;
1938       }
1939       // Or commutes, try both ways.
1940       if ((C1->getValue() & (C1->getValue() + 1)) == 0 &&
1941           match(B, m_Add(m_Value(V1), m_Value(V2)))) {
1942         // Add commutes, try both ways.
1943         if (V1 == A &&
1944             MaskedValueIsZero(V2, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1945           return B;
1946         if (V2 == A &&
1947             MaskedValueIsZero(V1, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1948           return B;
1949       }
1950     }
1951   }
1952
1953   // If the operation is with the result of a phi instruction, check whether
1954   // operating on all incoming values of the phi always yields the same value.
1955   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1956     if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
1957       return V;
1958
1959   return nullptr;
1960 }
1961
1962 Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout &DL,
1963                             const TargetLibraryInfo *TLI,
1964                             const DominatorTree *DT, AssumptionCache *AC,
1965                             const Instruction *CxtI) {
1966   return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1967                           RecursionLimit);
1968 }
1969
1970 /// Given operands for a Xor, see if we can fold the result.
1971 /// If not, this returns null.
1972 static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q,
1973                               unsigned MaxRecurse) {
1974   if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1975     if (Constant *CRHS = dyn_cast<Constant>(Op1))
1976       return ConstantFoldBinaryOpOperands(Instruction::Xor, CLHS, CRHS, Q.DL);
1977
1978     // Canonicalize the constant to the RHS.
1979     std::swap(Op0, Op1);
1980   }
1981
1982   // A ^ undef -> undef
1983   if (match(Op1, m_Undef()))
1984     return Op1;
1985
1986   // A ^ 0 = A
1987   if (match(Op1, m_Zero()))
1988     return Op0;
1989
1990   // A ^ A = 0
1991   if (Op0 == Op1)
1992     return Constant::getNullValue(Op0->getType());
1993
1994   // A ^ ~A  =  ~A ^ A  =  -1
1995   if (match(Op0, m_Not(m_Specific(Op1))) ||
1996       match(Op1, m_Not(m_Specific(Op0))))
1997     return Constant::getAllOnesValue(Op0->getType());
1998
1999   // Try some generic simplifications for associative operations.
2000   if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q,
2001                                           MaxRecurse))
2002     return V;
2003
2004   // Threading Xor over selects and phi nodes is pointless, so don't bother.
2005   // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2006   // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2007   // only if B and C are equal.  If B and C are equal then (since we assume
2008   // that operands have already been simplified) "select(cond, B, C)" should
2009   // have been simplified to the common value of B and C already.  Analysing
2010   // "A^B" and "A^C" thus gains nothing, but costs compile time.  Similarly
2011   // for threading over phi nodes.
2012
2013   return nullptr;
2014 }
2015
2016 Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout &DL,
2017                              const TargetLibraryInfo *TLI,
2018                              const DominatorTree *DT, AssumptionCache *AC,
2019                              const Instruction *CxtI) {
2020   return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
2021                            RecursionLimit);
2022 }
2023
2024 static Type *GetCompareTy(Value *Op) {
2025   return CmpInst::makeCmpResultType(Op->getType());
2026 }
2027
2028 /// Rummage around inside V looking for something equivalent to the comparison
2029 /// "LHS Pred RHS". Return such a value if found, otherwise return null.
2030 /// Helper function for analyzing max/min idioms.
2031 static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred,
2032                                          Value *LHS, Value *RHS) {
2033   SelectInst *SI = dyn_cast<SelectInst>(V);
2034   if (!SI)
2035     return nullptr;
2036   CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2037   if (!Cmp)
2038     return nullptr;
2039   Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2040   if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2041     return Cmp;
2042   if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2043       LHS == CmpRHS && RHS == CmpLHS)
2044     return Cmp;
2045   return nullptr;
2046 }
2047
2048 // A significant optimization not implemented here is assuming that alloca
2049 // addresses are not equal to incoming argument values. They don't *alias*,
2050 // as we say, but that doesn't mean they aren't equal, so we take a
2051 // conservative approach.
2052 //
2053 // This is inspired in part by C++11 5.10p1:
2054 //   "Two pointers of the same type compare equal if and only if they are both
2055 //    null, both point to the same function, or both represent the same
2056 //    address."
2057 //
2058 // This is pretty permissive.
2059 //
2060 // It's also partly due to C11 6.5.9p6:
2061 //   "Two pointers compare equal if and only if both are null pointers, both are
2062 //    pointers to the same object (including a pointer to an object and a
2063 //    subobject at its beginning) or function, both are pointers to one past the
2064 //    last element of the same array object, or one is a pointer to one past the
2065 //    end of one array object and the other is a pointer to the start of a
2066 //    different array object that happens to immediately follow the first array
2067 //    object in the address space.)
2068 //
2069 // C11's version is more restrictive, however there's no reason why an argument
2070 // couldn't be a one-past-the-end value for a stack object in the caller and be
2071 // equal to the beginning of a stack object in the callee.
2072 //
2073 // If the C and C++ standards are ever made sufficiently restrictive in this
2074 // area, it may be possible to update LLVM's semantics accordingly and reinstate
2075 // this optimization.
2076 static Constant *
2077 computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI,
2078                    const DominatorTree *DT, CmpInst::Predicate Pred,
2079                    const Instruction *CxtI, Value *LHS, Value *RHS) {
2080   // First, skip past any trivial no-ops.
2081   LHS = LHS->stripPointerCasts();
2082   RHS = RHS->stripPointerCasts();
2083
2084   // A non-null pointer is not equal to a null pointer.
2085   if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) &&
2086       (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE))
2087     return ConstantInt::get(GetCompareTy(LHS),
2088                             !CmpInst::isTrueWhenEqual(Pred));
2089
2090   // We can only fold certain predicates on pointer comparisons.
2091   switch (Pred) {
2092   default:
2093     return nullptr;
2094
2095     // Equality comaprisons are easy to fold.
2096   case CmpInst::ICMP_EQ:
2097   case CmpInst::ICMP_NE:
2098     break;
2099
2100     // We can only handle unsigned relational comparisons because 'inbounds' on
2101     // a GEP only protects against unsigned wrapping.
2102   case CmpInst::ICMP_UGT:
2103   case CmpInst::ICMP_UGE:
2104   case CmpInst::ICMP_ULT:
2105   case CmpInst::ICMP_ULE:
2106     // However, we have to switch them to their signed variants to handle
2107     // negative indices from the base pointer.
2108     Pred = ICmpInst::getSignedPredicate(Pred);
2109     break;
2110   }
2111
2112   // Strip off any constant offsets so that we can reason about them.
2113   // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2114   // here and compare base addresses like AliasAnalysis does, however there are
2115   // numerous hazards. AliasAnalysis and its utilities rely on special rules
2116   // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2117   // doesn't need to guarantee pointer inequality when it says NoAlias.
2118   Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
2119   Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
2120
2121   // If LHS and RHS are related via constant offsets to the same base
2122   // value, we can replace it with an icmp which just compares the offsets.
2123   if (LHS == RHS)
2124     return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset);
2125
2126   // Various optimizations for (in)equality comparisons.
2127   if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
2128     // Different non-empty allocations that exist at the same time have
2129     // different addresses (if the program can tell). Global variables always
2130     // exist, so they always exist during the lifetime of each other and all
2131     // allocas. Two different allocas usually have different addresses...
2132     //
2133     // However, if there's an @llvm.stackrestore dynamically in between two
2134     // allocas, they may have the same address. It's tempting to reduce the
2135     // scope of the problem by only looking at *static* allocas here. That would
2136     // cover the majority of allocas while significantly reducing the likelihood
2137     // of having an @llvm.stackrestore pop up in the middle. However, it's not
2138     // actually impossible for an @llvm.stackrestore to pop up in the middle of
2139     // an entry block. Also, if we have a block that's not attached to a
2140     // function, we can't tell if it's "static" under the current definition.
2141     // Theoretically, this problem could be fixed by creating a new kind of
2142     // instruction kind specifically for static allocas. Such a new instruction
2143     // could be required to be at the top of the entry block, thus preventing it
2144     // from being subject to a @llvm.stackrestore. Instcombine could even
2145     // convert regular allocas into these special allocas. It'd be nifty.
2146     // However, until then, this problem remains open.
2147     //
2148     // So, we'll assume that two non-empty allocas have different addresses
2149     // for now.
2150     //
2151     // With all that, if the offsets are within the bounds of their allocations
2152     // (and not one-past-the-end! so we can't use inbounds!), and their
2153     // allocations aren't the same, the pointers are not equal.
2154     //
2155     // Note that it's not necessary to check for LHS being a global variable
2156     // address, due to canonicalization and constant folding.
2157     if (isa<AllocaInst>(LHS) &&
2158         (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
2159       ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset);
2160       ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset);
2161       uint64_t LHSSize, RHSSize;
2162       if (LHSOffsetCI && RHSOffsetCI &&
2163           getObjectSize(LHS, LHSSize, DL, TLI) &&
2164           getObjectSize(RHS, RHSSize, DL, TLI)) {
2165         const APInt &LHSOffsetValue = LHSOffsetCI->getValue();
2166         const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
2167         if (!LHSOffsetValue.isNegative() &&
2168             !RHSOffsetValue.isNegative() &&
2169             LHSOffsetValue.ult(LHSSize) &&
2170             RHSOffsetValue.ult(RHSSize)) {
2171           return ConstantInt::get(GetCompareTy(LHS),
2172                                   !CmpInst::isTrueWhenEqual(Pred));
2173         }
2174       }
2175
2176       // Repeat the above check but this time without depending on DataLayout
2177       // or being able to compute a precise size.
2178       if (!cast<PointerType>(LHS->getType())->isEmptyTy() &&
2179           !cast<PointerType>(RHS->getType())->isEmptyTy() &&
2180           LHSOffset->isNullValue() &&
2181           RHSOffset->isNullValue())
2182         return ConstantInt::get(GetCompareTy(LHS),
2183                                 !CmpInst::isTrueWhenEqual(Pred));
2184     }
2185
2186     // Even if an non-inbounds GEP occurs along the path we can still optimize
2187     // equality comparisons concerning the result. We avoid walking the whole
2188     // chain again by starting where the last calls to
2189     // stripAndComputeConstantOffsets left off and accumulate the offsets.
2190     Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true);
2191     Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true);
2192     if (LHS == RHS)
2193       return ConstantExpr::getICmp(Pred,
2194                                    ConstantExpr::getAdd(LHSOffset, LHSNoBound),
2195                                    ConstantExpr::getAdd(RHSOffset, RHSNoBound));
2196
2197     // If one side of the equality comparison must come from a noalias call
2198     // (meaning a system memory allocation function), and the other side must
2199     // come from a pointer that cannot overlap with dynamically-allocated
2200     // memory within the lifetime of the current function (allocas, byval
2201     // arguments, globals), then determine the comparison result here.
2202     SmallVector<Value *, 8> LHSUObjs, RHSUObjs;
2203     GetUnderlyingObjects(LHS, LHSUObjs, DL);
2204     GetUnderlyingObjects(RHS, RHSUObjs, DL);
2205
2206     // Is the set of underlying objects all noalias calls?
2207     auto IsNAC = [](ArrayRef<Value *> Objects) {
2208       return all_of(Objects, isNoAliasCall);
2209     };
2210
2211     // Is the set of underlying objects all things which must be disjoint from
2212     // noalias calls. For allocas, we consider only static ones (dynamic
2213     // allocas might be transformed into calls to malloc not simultaneously
2214     // live with the compared-to allocation). For globals, we exclude symbols
2215     // that might be resolve lazily to symbols in another dynamically-loaded
2216     // library (and, thus, could be malloc'ed by the implementation).
2217     auto IsAllocDisjoint = [](ArrayRef<Value *> Objects) {
2218       return all_of(Objects, [](Value *V) {
2219         if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2220           return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
2221         if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2222           return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2223                   GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2224                  !GV->isThreadLocal();
2225         if (const Argument *A = dyn_cast<Argument>(V))
2226           return A->hasByValAttr();
2227         return false;
2228       });
2229     };
2230
2231     if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2232         (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2233         return ConstantInt::get(GetCompareTy(LHS),
2234                                 !CmpInst::isTrueWhenEqual(Pred));
2235
2236     // Fold comparisons for non-escaping pointer even if the allocation call
2237     // cannot be elided. We cannot fold malloc comparison to null. Also, the
2238     // dynamic allocation call could be either of the operands.
2239     Value *MI = nullptr;
2240     if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonNullAt(RHS, CxtI, DT))
2241       MI = LHS;
2242     else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonNullAt(LHS, CxtI, DT))
2243       MI = RHS;
2244     // FIXME: We should also fold the compare when the pointer escapes, but the
2245     // compare dominates the pointer escape
2246     if (MI && !PointerMayBeCaptured(MI, true, true))
2247       return ConstantInt::get(GetCompareTy(LHS),
2248                               CmpInst::isFalseWhenEqual(Pred));
2249   }
2250
2251   // Otherwise, fail.
2252   return nullptr;
2253 }
2254
2255 /// Fold an icmp when its operands have i1 scalar type.
2256 static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS,
2257                                   Value *RHS, const Query &Q) {
2258   Type *ITy = GetCompareTy(LHS); // The return type.
2259   Type *OpTy = LHS->getType();   // The operand type.
2260   if (!OpTy->getScalarType()->isIntegerTy(1))
2261     return nullptr;
2262
2263   switch (Pred) {
2264   default:
2265     break;
2266   case ICmpInst::ICMP_EQ:
2267     // X == 1 -> X
2268     if (match(RHS, m_One()))
2269       return LHS;
2270     break;
2271   case ICmpInst::ICMP_NE:
2272     // X != 0 -> X
2273     if (match(RHS, m_Zero()))
2274       return LHS;
2275     break;
2276   case ICmpInst::ICMP_UGT:
2277     // X >u 0 -> X
2278     if (match(RHS, m_Zero()))
2279       return LHS;
2280     break;
2281   case ICmpInst::ICMP_UGE:
2282     // X >=u 1 -> X
2283     if (match(RHS, m_One()))
2284       return LHS;
2285     if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false))
2286       return getTrue(ITy);
2287     break;
2288   case ICmpInst::ICMP_SGE:
2289     /// For signed comparison, the values for an i1 are 0 and -1
2290     /// respectively. This maps into a truth table of:
2291     /// LHS | RHS | LHS >=s RHS   | LHS implies RHS
2292     ///  0  |  0  |  1 (0 >= 0)   |  1
2293     ///  0  |  1  |  1 (0 >= -1)  |  1
2294     ///  1  |  0  |  0 (-1 >= 0)  |  0
2295     ///  1  |  1  |  1 (-1 >= -1) |  1
2296     if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false))
2297       return getTrue(ITy);
2298     break;
2299   case ICmpInst::ICMP_SLT:
2300     // X <s 0 -> X
2301     if (match(RHS, m_Zero()))
2302       return LHS;
2303     break;
2304   case ICmpInst::ICMP_SLE:
2305     // X <=s -1 -> X
2306     if (match(RHS, m_One()))
2307       return LHS;
2308     break;
2309   case ICmpInst::ICMP_ULE:
2310     if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false))
2311       return getTrue(ITy);
2312     break;
2313   }
2314
2315   return nullptr;
2316 }
2317
2318 /// Try hard to fold icmp with zero RHS because this is a common case.
2319 static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
2320                                    Value *RHS, const Query &Q) {
2321   if (!match(RHS, m_Zero()))
2322     return nullptr;
2323
2324   Type *ITy = GetCompareTy(LHS); // The return type.
2325   bool LHSKnownNonNegative, LHSKnownNegative;
2326   switch (Pred) {
2327   default:
2328     llvm_unreachable("Unknown ICmp predicate!");
2329   case ICmpInst::ICMP_ULT:
2330     return getFalse(ITy);
2331   case ICmpInst::ICMP_UGE:
2332     return getTrue(ITy);
2333   case ICmpInst::ICMP_EQ:
2334   case ICmpInst::ICMP_ULE:
2335     if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2336       return getFalse(ITy);
2337     break;
2338   case ICmpInst::ICMP_NE:
2339   case ICmpInst::ICMP_UGT:
2340     if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2341       return getTrue(ITy);
2342     break;
2343   case ICmpInst::ICMP_SLT:
2344     ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2345                    Q.CxtI, Q.DT);
2346     if (LHSKnownNegative)
2347       return getTrue(ITy);
2348     if (LHSKnownNonNegative)
2349       return getFalse(ITy);
2350     break;
2351   case ICmpInst::ICMP_SLE:
2352     ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2353                    Q.CxtI, Q.DT);
2354     if (LHSKnownNegative)
2355       return getTrue(ITy);
2356     if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2357       return getFalse(ITy);
2358     break;
2359   case ICmpInst::ICMP_SGE:
2360     ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2361                    Q.CxtI, Q.DT);
2362     if (LHSKnownNegative)
2363       return getFalse(ITy);
2364     if (LHSKnownNonNegative)
2365       return getTrue(ITy);
2366     break;
2367   case ICmpInst::ICMP_SGT:
2368     ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2369                    Q.CxtI, Q.DT);
2370     if (LHSKnownNegative)
2371       return getFalse(ITy);
2372     if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2373       return getTrue(ITy);
2374     break;
2375   }
2376
2377   return nullptr;
2378 }
2379
2380 static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
2381                                        Value *RHS) {
2382   const APInt *C;
2383   if (!match(RHS, m_APInt(C)))
2384     return nullptr;
2385
2386   // Rule out tautological comparisons (eg., ult 0 or uge 0).
2387   ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
2388   if (RHS_CR.isEmptySet())
2389     return ConstantInt::getFalse(GetCompareTy(RHS));
2390   if (RHS_CR.isFullSet())
2391     return ConstantInt::getTrue(GetCompareTy(RHS));
2392
2393   // Many binary operators with constant RHS have easy to compute constant
2394   // range.  Use them to check whether the comparison is a tautology.
2395   unsigned Width = C->getBitWidth();
2396   APInt Lower = APInt(Width, 0);
2397   APInt Upper = APInt(Width, 0);
2398   const APInt *C2;
2399   if (match(LHS, m_URem(m_Value(), m_APInt(C2)))) {
2400     // 'urem x, C2' produces [0, C2).
2401     Upper = *C2;
2402   } else if (match(LHS, m_SRem(m_Value(), m_APInt(C2)))) {
2403     // 'srem x, C2' produces (-|C2|, |C2|).
2404     Upper = C2->abs();
2405     Lower = (-Upper) + 1;
2406   } else if (match(LHS, m_UDiv(m_APInt(C2), m_Value()))) {
2407     // 'udiv C2, x' produces [0, C2].
2408     Upper = *C2 + 1;
2409   } else if (match(LHS, m_UDiv(m_Value(), m_APInt(C2)))) {
2410     // 'udiv x, C2' produces [0, UINT_MAX / C2].
2411     APInt NegOne = APInt::getAllOnesValue(Width);
2412     if (*C2 != 0)
2413       Upper = NegOne.udiv(*C2) + 1;
2414   } else if (match(LHS, m_SDiv(m_APInt(C2), m_Value()))) {
2415     if (C2->isMinSignedValue()) {
2416       // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
2417       Lower = *C2;
2418       Upper = Lower.lshr(1) + 1;
2419     } else {
2420       // 'sdiv C2, x' produces [-|C2|, |C2|].
2421       Upper = C2->abs() + 1;
2422       Lower = (-Upper) + 1;
2423     }
2424   } else if (match(LHS, m_SDiv(m_Value(), m_APInt(C2)))) {
2425     APInt IntMin = APInt::getSignedMinValue(Width);
2426     APInt IntMax = APInt::getSignedMaxValue(Width);
2427     if (C2->isAllOnesValue()) {
2428       // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
2429       //    where C2 != -1 and C2 != 0 and C2 != 1
2430       Lower = IntMin + 1;
2431       Upper = IntMax + 1;
2432     } else if (C2->countLeadingZeros() < Width - 1) {
2433       // 'sdiv x, C2' produces [INT_MIN / C2, INT_MAX / C2]
2434       //    where C2 != -1 and C2 != 0 and C2 != 1
2435       Lower = IntMin.sdiv(*C2);
2436       Upper = IntMax.sdiv(*C2);
2437       if (Lower.sgt(Upper))
2438         std::swap(Lower, Upper);
2439       Upper = Upper + 1;
2440       assert(Upper != Lower && "Upper part of range has wrapped!");
2441     }
2442   } else if (match(LHS, m_NUWShl(m_APInt(C2), m_Value()))) {
2443     // 'shl nuw C2, x' produces [C2, C2 << CLZ(C2)]
2444     Lower = *C2;
2445     Upper = Lower.shl(Lower.countLeadingZeros()) + 1;
2446   } else if (match(LHS, m_NSWShl(m_APInt(C2), m_Value()))) {
2447     if (C2->isNegative()) {
2448       // 'shl nsw C2, x' produces [C2 << CLO(C2)-1, C2]
2449       unsigned ShiftAmount = C2->countLeadingOnes() - 1;
2450       Lower = C2->shl(ShiftAmount);
2451       Upper = *C2 + 1;
2452     } else {
2453       // 'shl nsw C2, x' produces [C2, C2 << CLZ(C2)-1]
2454       unsigned ShiftAmount = C2->countLeadingZeros() - 1;
2455       Lower = *C2;
2456       Upper = C2->shl(ShiftAmount) + 1;
2457     }
2458   } else if (match(LHS, m_LShr(m_Value(), m_APInt(C2)))) {
2459     // 'lshr x, C2' produces [0, UINT_MAX >> C2].
2460     APInt NegOne = APInt::getAllOnesValue(Width);
2461     if (C2->ult(Width))
2462       Upper = NegOne.lshr(*C2) + 1;
2463   } else if (match(LHS, m_LShr(m_APInt(C2), m_Value()))) {
2464     // 'lshr C2, x' produces [C2 >> (Width-1), C2].
2465     unsigned ShiftAmount = Width - 1;
2466     if (*C2 != 0 && cast<BinaryOperator>(LHS)->isExact())
2467       ShiftAmount = C2->countTrailingZeros();
2468     Lower = C2->lshr(ShiftAmount);
2469     Upper = *C2 + 1;
2470   } else if (match(LHS, m_AShr(m_Value(), m_APInt(C2)))) {
2471     // 'ashr x, C2' produces [INT_MIN >> C2, INT_MAX >> C2].
2472     APInt IntMin = APInt::getSignedMinValue(Width);
2473     APInt IntMax = APInt::getSignedMaxValue(Width);
2474     if (C2->ult(Width)) {
2475       Lower = IntMin.ashr(*C2);
2476       Upper = IntMax.ashr(*C2) + 1;
2477     }
2478   } else if (match(LHS, m_AShr(m_APInt(C2), m_Value()))) {
2479     unsigned ShiftAmount = Width - 1;
2480     if (*C2 != 0 && cast<BinaryOperator>(LHS)->isExact())
2481       ShiftAmount = C2->countTrailingZeros();
2482     if (C2->isNegative()) {
2483       // 'ashr C2, x' produces [C2, C2 >> (Width-1)]
2484       Lower = *C2;
2485       Upper = C2->ashr(ShiftAmount) + 1;
2486     } else {
2487       // 'ashr C2, x' produces [C2 >> (Width-1), C2]
2488       Lower = C2->ashr(ShiftAmount);
2489       Upper = *C2 + 1;
2490     }
2491   } else if (match(LHS, m_Or(m_Value(), m_APInt(C2)))) {
2492     // 'or x, C2' produces [C2, UINT_MAX].
2493     Lower = *C2;
2494   } else if (match(LHS, m_And(m_Value(), m_APInt(C2)))) {
2495     // 'and x, C2' produces [0, C2].
2496     Upper = *C2 + 1;
2497   } else if (match(LHS, m_NUWAdd(m_Value(), m_APInt(C2)))) {
2498     // 'add nuw x, C2' produces [C2, UINT_MAX].
2499     Lower = *C2;
2500   }
2501
2502   ConstantRange LHS_CR =
2503       Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true);
2504
2505   if (auto *I = dyn_cast<Instruction>(LHS))
2506     if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
2507       LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges));
2508
2509   if (!LHS_CR.isFullSet()) {
2510     if (RHS_CR.contains(LHS_CR))
2511       return ConstantInt::getTrue(GetCompareTy(RHS));
2512     if (RHS_CR.inverse().contains(LHS_CR))
2513       return ConstantInt::getFalse(GetCompareTy(RHS));
2514   }
2515
2516   return nullptr;
2517 }
2518
2519 static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS,
2520                                     Value *RHS, const Query &Q,
2521                                     unsigned MaxRecurse) {
2522   Type *ITy = GetCompareTy(LHS); // The return type.
2523
2524   BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
2525   BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
2526   if (MaxRecurse && (LBO || RBO)) {
2527     // Analyze the case when either LHS or RHS is an add instruction.
2528     Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
2529     // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
2530     bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
2531     if (LBO && LBO->getOpcode() == Instruction::Add) {
2532       A = LBO->getOperand(0);
2533       B = LBO->getOperand(1);
2534       NoLHSWrapProblem =
2535           ICmpInst::isEquality(Pred) ||
2536           (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) ||
2537           (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap());
2538     }
2539     if (RBO && RBO->getOpcode() == Instruction::Add) {
2540       C = RBO->getOperand(0);
2541       D = RBO->getOperand(1);
2542       NoRHSWrapProblem =
2543           ICmpInst::isEquality(Pred) ||
2544           (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) ||
2545           (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap());
2546     }
2547
2548     // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2549     if ((A == RHS || B == RHS) && NoLHSWrapProblem)
2550       if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A,
2551                                       Constant::getNullValue(RHS->getType()), Q,
2552                                       MaxRecurse - 1))
2553         return V;
2554
2555     // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2556     if ((C == LHS || D == LHS) && NoRHSWrapProblem)
2557       if (Value *V =
2558               SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
2559                                C == LHS ? D : C, Q, MaxRecurse - 1))
2560         return V;
2561
2562     // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
2563     if (A && C && (A == C || A == D || B == C || B == D) && NoLHSWrapProblem &&
2564         NoRHSWrapProblem) {
2565       // Determine Y and Z in the form icmp (X+Y), (X+Z).
2566       Value *Y, *Z;
2567       if (A == C) {
2568         // C + B == C + D  ->  B == D
2569         Y = B;
2570         Z = D;
2571       } else if (A == D) {
2572         // D + B == C + D  ->  B == C
2573         Y = B;
2574         Z = C;
2575       } else if (B == C) {
2576         // A + C == C + D  ->  A == D
2577         Y = A;
2578         Z = D;
2579       } else {
2580         assert(B == D);
2581         // A + D == C + D  ->  A == C
2582         Y = A;
2583         Z = C;
2584       }
2585       if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
2586         return V;
2587     }
2588   }
2589
2590   {
2591     Value *Y = nullptr;
2592     // icmp pred (or X, Y), X
2593     if (LBO && match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
2594       if (Pred == ICmpInst::ICMP_ULT)
2595         return getFalse(ITy);
2596       if (Pred == ICmpInst::ICMP_UGE)
2597         return getTrue(ITy);
2598
2599       if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
2600         bool RHSKnownNonNegative, RHSKnownNegative;
2601         bool YKnownNonNegative, YKnownNegative;
2602         ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, Q.DL, 0,
2603                        Q.AC, Q.CxtI, Q.DT);
2604         ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC,
2605                        Q.CxtI, Q.DT);
2606         if (RHSKnownNonNegative && YKnownNegative)
2607           return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
2608         if (RHSKnownNegative || YKnownNonNegative)
2609           return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
2610       }
2611     }
2612     // icmp pred X, (or X, Y)
2613     if (RBO && match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))) {
2614       if (Pred == ICmpInst::ICMP_ULE)
2615         return getTrue(ITy);
2616       if (Pred == ICmpInst::ICMP_UGT)
2617         return getFalse(ITy);
2618
2619       if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLE) {
2620         bool LHSKnownNonNegative, LHSKnownNegative;
2621         bool YKnownNonNegative, YKnownNegative;
2622         ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0,
2623                        Q.AC, Q.CxtI, Q.DT);
2624         ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC,
2625                        Q.CxtI, Q.DT);
2626         if (LHSKnownNonNegative && YKnownNegative)
2627           return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy);
2628         if (LHSKnownNegative || YKnownNonNegative)
2629           return Pred == ICmpInst::ICMP_SGT ? getFalse(ITy) : getTrue(ITy);
2630       }
2631     }
2632   }
2633
2634   // icmp pred (and X, Y), X
2635   if (LBO && match(LBO, m_CombineOr(m_And(m_Value(), m_Specific(RHS)),
2636                                     m_And(m_Specific(RHS), m_Value())))) {
2637     if (Pred == ICmpInst::ICMP_UGT)
2638       return getFalse(ITy);
2639     if (Pred == ICmpInst::ICMP_ULE)
2640       return getTrue(ITy);
2641   }
2642   // icmp pred X, (and X, Y)
2643   if (RBO && match(RBO, m_CombineOr(m_And(m_Value(), m_Specific(LHS)),
2644                                     m_And(m_Specific(LHS), m_Value())))) {
2645     if (Pred == ICmpInst::ICMP_UGE)
2646       return getTrue(ITy);
2647     if (Pred == ICmpInst::ICMP_ULT)
2648       return getFalse(ITy);
2649   }
2650
2651   // 0 - (zext X) pred C
2652   if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
2653     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
2654       if (RHSC->getValue().isStrictlyPositive()) {
2655         if (Pred == ICmpInst::ICMP_SLT)
2656           return ConstantInt::getTrue(RHSC->getContext());
2657         if (Pred == ICmpInst::ICMP_SGE)
2658           return ConstantInt::getFalse(RHSC->getContext());
2659         if (Pred == ICmpInst::ICMP_EQ)
2660           return ConstantInt::getFalse(RHSC->getContext());
2661         if (Pred == ICmpInst::ICMP_NE)
2662           return ConstantInt::getTrue(RHSC->getContext());
2663       }
2664       if (RHSC->getValue().isNonNegative()) {
2665         if (Pred == ICmpInst::ICMP_SLE)
2666           return ConstantInt::getTrue(RHSC->getContext());
2667         if (Pred == ICmpInst::ICMP_SGT)
2668           return ConstantInt::getFalse(RHSC->getContext());
2669       }
2670     }
2671   }
2672
2673   // icmp pred (urem X, Y), Y
2674   if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
2675     bool KnownNonNegative, KnownNegative;
2676     switch (Pred) {
2677     default:
2678       break;
2679     case ICmpInst::ICMP_SGT:
2680     case ICmpInst::ICMP_SGE:
2681       ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2682                      Q.CxtI, Q.DT);
2683       if (!KnownNonNegative)
2684         break;
2685       LLVM_FALLTHROUGH;
2686     case ICmpInst::ICMP_EQ:
2687     case ICmpInst::ICMP_UGT:
2688     case ICmpInst::ICMP_UGE:
2689       return getFalse(ITy);
2690     case ICmpInst::ICMP_SLT:
2691     case ICmpInst::ICMP_SLE:
2692       ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2693                      Q.CxtI, Q.DT);
2694       if (!KnownNonNegative)
2695         break;
2696       LLVM_FALLTHROUGH;
2697     case ICmpInst::ICMP_NE:
2698     case ICmpInst::ICMP_ULT:
2699     case ICmpInst::ICMP_ULE:
2700       return getTrue(ITy);
2701     }
2702   }
2703
2704   // icmp pred X, (urem Y, X)
2705   if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) {
2706     bool KnownNonNegative, KnownNegative;
2707     switch (Pred) {
2708     default:
2709       break;
2710     case ICmpInst::ICMP_SGT:
2711     case ICmpInst::ICMP_SGE:
2712       ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2713                      Q.CxtI, Q.DT);
2714       if (!KnownNonNegative)
2715         break;
2716       LLVM_FALLTHROUGH;
2717     case ICmpInst::ICMP_NE:
2718     case ICmpInst::ICMP_UGT:
2719     case ICmpInst::ICMP_UGE:
2720       return getTrue(ITy);
2721     case ICmpInst::ICMP_SLT:
2722     case ICmpInst::ICMP_SLE:
2723       ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2724                      Q.CxtI, Q.DT);
2725       if (!KnownNonNegative)
2726         break;
2727       LLVM_FALLTHROUGH;
2728     case ICmpInst::ICMP_EQ:
2729     case ICmpInst::ICMP_ULT:
2730     case ICmpInst::ICMP_ULE:
2731       return getFalse(ITy);
2732     }
2733   }
2734
2735   // x >> y <=u x
2736   // x udiv y <=u x.
2737   if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) ||
2738               match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) {
2739     // icmp pred (X op Y), X
2740     if (Pred == ICmpInst::ICMP_UGT)
2741       return getFalse(ITy);
2742     if (Pred == ICmpInst::ICMP_ULE)
2743       return getTrue(ITy);
2744   }
2745
2746   // x >=u x >> y
2747   // x >=u x udiv y.
2748   if (RBO && (match(RBO, m_LShr(m_Specific(LHS), m_Value())) ||
2749               match(RBO, m_UDiv(m_Specific(LHS), m_Value())))) {
2750     // icmp pred X, (X op Y)
2751     if (Pred == ICmpInst::ICMP_ULT)
2752       return getFalse(ITy);
2753     if (Pred == ICmpInst::ICMP_UGE)
2754       return getTrue(ITy);
2755   }
2756
2757   // handle:
2758   //   CI2 << X == CI
2759   //   CI2 << X != CI
2760   //
2761   //   where CI2 is a power of 2 and CI isn't
2762   if (auto *CI = dyn_cast<ConstantInt>(RHS)) {
2763     const APInt *CI2Val, *CIVal = &CI->getValue();
2764     if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) &&
2765         CI2Val->isPowerOf2()) {
2766       if (!CIVal->isPowerOf2()) {
2767         // CI2 << X can equal zero in some circumstances,
2768         // this simplification is unsafe if CI is zero.
2769         //
2770         // We know it is safe if:
2771         // - The shift is nsw, we can't shift out the one bit.
2772         // - The shift is nuw, we can't shift out the one bit.
2773         // - CI2 is one
2774         // - CI isn't zero
2775         if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() ||
2776             *CI2Val == 1 || !CI->isZero()) {
2777           if (Pred == ICmpInst::ICMP_EQ)
2778             return ConstantInt::getFalse(RHS->getContext());
2779           if (Pred == ICmpInst::ICMP_NE)
2780             return ConstantInt::getTrue(RHS->getContext());
2781         }
2782       }
2783       if (CIVal->isSignBit() && *CI2Val == 1) {
2784         if (Pred == ICmpInst::ICMP_UGT)
2785           return ConstantInt::getFalse(RHS->getContext());
2786         if (Pred == ICmpInst::ICMP_ULE)
2787           return ConstantInt::getTrue(RHS->getContext());
2788       }
2789     }
2790   }
2791
2792   if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() &&
2793       LBO->getOperand(1) == RBO->getOperand(1)) {
2794     switch (LBO->getOpcode()) {
2795     default:
2796       break;
2797     case Instruction::UDiv:
2798     case Instruction::LShr:
2799       if (ICmpInst::isSigned(Pred))
2800         break;
2801       LLVM_FALLTHROUGH;
2802     case Instruction::SDiv:
2803     case Instruction::AShr:
2804       if (!LBO->isExact() || !RBO->isExact())
2805         break;
2806       if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
2807                                       RBO->getOperand(0), Q, MaxRecurse - 1))
2808         return V;
2809       break;
2810     case Instruction::Shl: {
2811       bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap();
2812       bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap();
2813       if (!NUW && !NSW)
2814         break;
2815       if (!NSW && ICmpInst::isSigned(Pred))
2816         break;
2817       if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
2818                                       RBO->getOperand(0), Q, MaxRecurse - 1))
2819         return V;
2820       break;
2821     }
2822     }
2823   }
2824   return nullptr;
2825 }
2826
2827 /// Simplify integer comparisons where at least one operand of the compare
2828 /// matches an integer min/max idiom.
2829 static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS,
2830                                      Value *RHS, const Query &Q,
2831                                      unsigned MaxRecurse) {
2832   Type *ITy = GetCompareTy(LHS); // The return type.
2833   Value *A, *B;
2834   CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
2835   CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
2836
2837   // Signed variants on "max(a,b)>=a -> true".
2838   if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
2839     if (A != RHS)
2840       std::swap(A, B);       // smax(A, B) pred A.
2841     EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
2842     // We analyze this as smax(A, B) pred A.
2843     P = Pred;
2844   } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
2845              (A == LHS || B == LHS)) {
2846     if (A != LHS)
2847       std::swap(A, B);       // A pred smax(A, B).
2848     EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
2849     // We analyze this as smax(A, B) swapped-pred A.
2850     P = CmpInst::getSwappedPredicate(Pred);
2851   } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
2852              (A == RHS || B == RHS)) {
2853     if (A != RHS)
2854       std::swap(A, B);       // smin(A, B) pred A.
2855     EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
2856     // We analyze this as smax(-A, -B) swapped-pred -A.
2857     // Note that we do not need to actually form -A or -B thanks to EqP.
2858     P = CmpInst::getSwappedPredicate(Pred);
2859   } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
2860              (A == LHS || B == LHS)) {
2861     if (A != LHS)
2862       std::swap(A, B);       // A pred smin(A, B).
2863     EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
2864     // We analyze this as smax(-A, -B) pred -A.
2865     // Note that we do not need to actually form -A or -B thanks to EqP.
2866     P = Pred;
2867   }
2868   if (P != CmpInst::BAD_ICMP_PREDICATE) {
2869     // Cases correspond to "max(A, B) p A".
2870     switch (P) {
2871     default:
2872       break;
2873     case CmpInst::ICMP_EQ:
2874     case CmpInst::ICMP_SLE:
2875       // Equivalent to "A EqP B".  This may be the same as the condition tested
2876       // in the max/min; if so, we can just return that.
2877       if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
2878         return V;
2879       if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
2880         return V;
2881       // Otherwise, see if "A EqP B" simplifies.
2882       if (MaxRecurse)
2883         if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
2884           return V;
2885       break;
2886     case CmpInst::ICMP_NE:
2887     case CmpInst::ICMP_SGT: {
2888       CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
2889       // Equivalent to "A InvEqP B".  This may be the same as the condition
2890       // tested in the max/min; if so, we can just return that.
2891       if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
2892         return V;
2893       if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
2894         return V;
2895       // Otherwise, see if "A InvEqP B" simplifies.
2896       if (MaxRecurse)
2897         if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
2898           return V;
2899       break;
2900     }
2901     case CmpInst::ICMP_SGE:
2902       // Always true.
2903       return getTrue(ITy);
2904     case CmpInst::ICMP_SLT:
2905       // Always false.
2906       return getFalse(ITy);
2907     }
2908   }
2909
2910   // Unsigned variants on "max(a,b)>=a -> true".
2911   P = CmpInst::BAD_ICMP_PREDICATE;
2912   if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
2913     if (A != RHS)
2914       std::swap(A, B);       // umax(A, B) pred A.
2915     EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
2916     // We analyze this as umax(A, B) pred A.
2917     P = Pred;
2918   } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
2919              (A == LHS || B == LHS)) {
2920     if (A != LHS)
2921       std::swap(A, B);       // A pred umax(A, B).
2922     EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
2923     // We analyze this as umax(A, B) swapped-pred A.
2924     P = CmpInst::getSwappedPredicate(Pred);
2925   } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
2926              (A == RHS || B == RHS)) {
2927     if (A != RHS)
2928       std::swap(A, B);       // umin(A, B) pred A.
2929     EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
2930     // We analyze this as umax(-A, -B) swapped-pred -A.
2931     // Note that we do not need to actually form -A or -B thanks to EqP.
2932     P = CmpInst::getSwappedPredicate(Pred);
2933   } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
2934              (A == LHS || B == LHS)) {
2935     if (A != LHS)
2936       std::swap(A, B);       // A pred umin(A, B).
2937     EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
2938     // We analyze this as umax(-A, -B) pred -A.
2939     // Note that we do not need to actually form -A or -B thanks to EqP.
2940     P = Pred;
2941   }
2942   if (P != CmpInst::BAD_ICMP_PREDICATE) {
2943     // Cases correspond to "max(A, B) p A".
2944     switch (P) {
2945     default:
2946       break;
2947     case CmpInst::ICMP_EQ:
2948     case CmpInst::ICMP_ULE:
2949       // Equivalent to "A EqP B".  This may be the same as the condition tested
2950       // in the max/min; if so, we can just return that.
2951       if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
2952         return V;
2953       if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
2954         return V;
2955       // Otherwise, see if "A EqP B" simplifies.
2956       if (MaxRecurse)
2957         if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
2958           return V;
2959       break;
2960     case CmpInst::ICMP_NE:
2961     case CmpInst::ICMP_UGT: {
2962       CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
2963       // Equivalent to "A InvEqP B".  This may be the same as the condition
2964       // tested in the max/min; if so, we can just return that.
2965       if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
2966         return V;
2967       if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
2968         return V;
2969       // Otherwise, see if "A InvEqP B" simplifies.
2970       if (MaxRecurse)
2971         if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
2972           return V;
2973       break;
2974     }
2975     case CmpInst::ICMP_UGE:
2976       // Always true.
2977       return getTrue(ITy);
2978     case CmpInst::ICMP_ULT:
2979       // Always false.
2980       return getFalse(ITy);
2981     }
2982   }
2983
2984   // Variants on "max(x,y) >= min(x,z)".
2985   Value *C, *D;
2986   if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
2987       match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
2988       (A == C || A == D || B == C || B == D)) {
2989     // max(x, ?) pred min(x, ?).
2990     if (Pred == CmpInst::ICMP_SGE)
2991       // Always true.
2992       return getTrue(ITy);
2993     if (Pred == CmpInst::ICMP_SLT)
2994       // Always false.
2995       return getFalse(ITy);
2996   } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
2997              match(RHS, m_SMax(m_Value(C), m_Value(D))) &&
2998              (A == C || A == D || B == C || B == D)) {
2999     // min(x, ?) pred max(x, ?).
3000     if (Pred == CmpInst::ICMP_SLE)
3001       // Always true.
3002       return getTrue(ITy);
3003     if (Pred == CmpInst::ICMP_SGT)
3004       // Always false.
3005       return getFalse(ITy);
3006   } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3007              match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3008              (A == C || A == D || B == C || B == D)) {
3009     // max(x, ?) pred min(x, ?).
3010     if (Pred == CmpInst::ICMP_UGE)
3011       // Always true.
3012       return getTrue(ITy);
3013     if (Pred == CmpInst::ICMP_ULT)
3014       // Always false.
3015       return getFalse(ITy);
3016   } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3017              match(RHS, m_UMax(m_Value(C), m_Value(D))) &&
3018              (A == C || A == D || B == C || B == D)) {
3019     // min(x, ?) pred max(x, ?).
3020     if (Pred == CmpInst::ICMP_ULE)
3021       // Always true.
3022       return getTrue(ITy);
3023     if (Pred == CmpInst::ICMP_UGT)
3024       // Always false.
3025       return getFalse(ITy);
3026   }
3027
3028   return nullptr;
3029 }
3030
3031 /// Given operands for an ICmpInst, see if we can fold the result.
3032 /// If not, this returns null.
3033 static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3034                                const Query &Q, unsigned MaxRecurse) {
3035   CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3036   assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3037
3038   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3039     if (Constant *CRHS = dyn_cast<Constant>(RHS))
3040       return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3041
3042     // If we have a constant, make sure it is on the RHS.
3043     std::swap(LHS, RHS);
3044     Pred = CmpInst::getSwappedPredicate(Pred);
3045   }
3046
3047   Type *ITy = GetCompareTy(LHS); // The return type.
3048
3049   // icmp X, X -> true/false
3050   // X icmp undef -> true/false.  For example, icmp ugt %X, undef -> false
3051   // because X could be 0.
3052   if (LHS == RHS || isa<UndefValue>(RHS))
3053     return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3054
3055   if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3056     return V;
3057
3058   if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3059     return V;
3060
3061   if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS))
3062     return V;
3063
3064   // If both operands have range metadata, use the metadata
3065   // to simplify the comparison.
3066   if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) {
3067     auto RHS_Instr = dyn_cast<Instruction>(RHS);
3068     auto LHS_Instr = dyn_cast<Instruction>(LHS);
3069
3070     if (RHS_Instr->getMetadata(LLVMContext::MD_range) &&
3071         LHS_Instr->getMetadata(LLVMContext::MD_range)) {
3072       auto RHS_CR = getConstantRangeFromMetadata(
3073           *RHS_Instr->getMetadata(LLVMContext::MD_range));
3074       auto LHS_CR = getConstantRangeFromMetadata(
3075           *LHS_Instr->getMetadata(LLVMContext::MD_range));
3076
3077       auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR);
3078       if (Satisfied_CR.contains(LHS_CR))
3079         return ConstantInt::getTrue(RHS->getContext());
3080
3081       auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion(
3082                 CmpInst::getInversePredicate(Pred), RHS_CR);
3083       if (InversedSatisfied_CR.contains(LHS_CR))
3084         return ConstantInt::getFalse(RHS->getContext());
3085     }
3086   }
3087
3088   // Compare of cast, for example (zext X) != 0 -> X != 0
3089   if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3090     Instruction *LI = cast<CastInst>(LHS);
3091     Value *SrcOp = LI->getOperand(0);
3092     Type *SrcTy = SrcOp->getType();
3093     Type *DstTy = LI->getType();
3094
3095     // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3096     // if the integer type is the same size as the pointer type.
3097     if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3098         Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3099       if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3100         // Transfer the cast to the constant.
3101         if (Value *V = SimplifyICmpInst(Pred, SrcOp,
3102                                         ConstantExpr::getIntToPtr(RHSC, SrcTy),
3103                                         Q, MaxRecurse-1))
3104           return V;
3105       } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3106         if (RI->getOperand(0)->getType() == SrcTy)
3107           // Compare without the cast.
3108           if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
3109                                           Q, MaxRecurse-1))
3110             return V;
3111       }
3112     }
3113
3114     if (isa<ZExtInst>(LHS)) {
3115       // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3116       // same type.
3117       if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3118         if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3119           // Compare X and Y.  Note that signed predicates become unsigned.
3120           if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3121                                           SrcOp, RI->getOperand(0), Q,
3122                                           MaxRecurse-1))
3123             return V;
3124       }
3125       // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3126       // too.  If not, then try to deduce the result of the comparison.
3127       else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3128         // Compute the constant that would happen if we truncated to SrcTy then
3129         // reextended to DstTy.
3130         Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
3131         Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
3132
3133         // If the re-extended constant didn't change then this is effectively
3134         // also a case of comparing two zero-extended values.
3135         if (RExt == CI && MaxRecurse)
3136           if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3137                                         SrcOp, Trunc, Q, MaxRecurse-1))
3138             return V;
3139
3140         // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3141         // there.  Use this to work out the result of the comparison.
3142         if (RExt != CI) {
3143           switch (Pred) {
3144           default: llvm_unreachable("Unknown ICmp predicate!");
3145           // LHS <u RHS.
3146           case ICmpInst::ICMP_EQ:
3147           case ICmpInst::ICMP_UGT:
3148           case ICmpInst::ICMP_UGE:
3149             return ConstantInt::getFalse(CI->getContext());
3150
3151           case ICmpInst::ICMP_NE:
3152           case ICmpInst::ICMP_ULT:
3153           case ICmpInst::ICMP_ULE:
3154             return ConstantInt::getTrue(CI->getContext());
3155
3156           // LHS is non-negative.  If RHS is negative then LHS >s LHS.  If RHS
3157           // is non-negative then LHS <s RHS.
3158           case ICmpInst::ICMP_SGT:
3159           case ICmpInst::ICMP_SGE:
3160             return CI->getValue().isNegative() ?
3161               ConstantInt::getTrue(CI->getContext()) :
3162               ConstantInt::getFalse(CI->getContext());
3163
3164           case ICmpInst::ICMP_SLT:
3165           case ICmpInst::ICMP_SLE:
3166             return CI->getValue().isNegative() ?
3167               ConstantInt::getFalse(CI->getContext()) :
3168               ConstantInt::getTrue(CI->getContext());
3169           }
3170         }
3171       }
3172     }
3173
3174     if (isa<SExtInst>(LHS)) {
3175       // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3176       // same type.
3177       if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3178         if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3179           // Compare X and Y.  Note that the predicate does not change.
3180           if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
3181                                           Q, MaxRecurse-1))
3182             return V;
3183       }
3184       // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3185       // too.  If not, then try to deduce the result of the comparison.
3186       else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3187         // Compute the constant that would happen if we truncated to SrcTy then
3188         // reextended to DstTy.
3189         Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
3190         Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
3191
3192         // If the re-extended constant didn't change then this is effectively
3193         // also a case of comparing two sign-extended values.
3194         if (RExt == CI && MaxRecurse)
3195           if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1))
3196             return V;
3197
3198         // Otherwise the upper bits of LHS are all equal, while RHS has varying
3199         // bits there.  Use this to work out the result of the comparison.
3200         if (RExt != CI) {
3201           switch (Pred) {
3202           default: llvm_unreachable("Unknown ICmp predicate!");
3203           case ICmpInst::ICMP_EQ:
3204             return ConstantInt::getFalse(CI->getContext());
3205           case ICmpInst::ICMP_NE:
3206             return ConstantInt::getTrue(CI->getContext());
3207
3208           // If RHS is non-negative then LHS <s RHS.  If RHS is negative then
3209           // LHS >s RHS.
3210           case ICmpInst::ICMP_SGT:
3211           case ICmpInst::ICMP_SGE:
3212             return CI->getValue().isNegative() ?
3213               ConstantInt::getTrue(CI->getContext()) :
3214               ConstantInt::getFalse(CI->getContext());
3215           case ICmpInst::ICMP_SLT:
3216           case ICmpInst::ICMP_SLE:
3217             return CI->getValue().isNegative() ?
3218               ConstantInt::getFalse(CI->getContext()) :
3219               ConstantInt::getTrue(CI->getContext());
3220
3221           // If LHS is non-negative then LHS <u RHS.  If LHS is negative then
3222           // LHS >u RHS.
3223           case ICmpInst::ICMP_UGT:
3224           case ICmpInst::ICMP_UGE:
3225             // Comparison is true iff the LHS <s 0.
3226             if (MaxRecurse)
3227               if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3228                                               Constant::getNullValue(SrcTy),
3229                                               Q, MaxRecurse-1))
3230                 return V;
3231             break;
3232           case ICmpInst::ICMP_ULT:
3233           case ICmpInst::ICMP_ULE:
3234             // Comparison is true iff the LHS >=s 0.
3235             if (MaxRecurse)
3236               if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3237                                               Constant::getNullValue(SrcTy),
3238                                               Q, MaxRecurse-1))
3239                 return V;
3240             break;
3241           }
3242         }
3243       }
3244     }
3245   }
3246
3247   // icmp eq|ne X, Y -> false|true if X != Y
3248   if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
3249       isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) {
3250     LLVMContext &Ctx = LHS->getType()->getContext();
3251     return Pred == ICmpInst::ICMP_NE ?
3252       ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx);
3253   }
3254
3255   if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
3256     return V;
3257
3258   if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
3259     return V;
3260
3261   // Simplify comparisons of related pointers using a powerful, recursive
3262   // GEP-walk when we have target data available..
3263   if (LHS->getType()->isPointerTy())
3264     if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, LHS, RHS))
3265       return C;
3266   if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
3267     if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3268       if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
3269               Q.DL.getTypeSizeInBits(CLHS->getType()) &&
3270           Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
3271               Q.DL.getTypeSizeInBits(CRHS->getType()))
3272         if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI,
3273                                          CLHS->getPointerOperand(),
3274                                          CRHS->getPointerOperand()))
3275           return C;
3276
3277   if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) {
3278     if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
3279       if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
3280           GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() &&
3281           (ICmpInst::isEquality(Pred) ||
3282            (GLHS->isInBounds() && GRHS->isInBounds() &&
3283             Pred == ICmpInst::getSignedPredicate(Pred)))) {
3284         // The bases are equal and the indices are constant.  Build a constant
3285         // expression GEP with the same indices and a null base pointer to see
3286         // what constant folding can make out of it.
3287         Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType());
3288         SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end());
3289         Constant *NewLHS = ConstantExpr::getGetElementPtr(
3290             GLHS->getSourceElementType(), Null, IndicesLHS);
3291
3292         SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end());
3293         Constant *NewRHS = ConstantExpr::getGetElementPtr(
3294             GLHS->getSourceElementType(), Null, IndicesRHS);
3295         return ConstantExpr::getICmp(Pred, NewLHS, NewRHS);
3296       }
3297     }
3298   }
3299
3300   // If a bit is known to be zero for A and known to be one for B,
3301   // then A and B cannot be equal.
3302   if (ICmpInst::isEquality(Pred)) {
3303     const APInt *RHSVal;
3304     if (match(RHS, m_APInt(RHSVal))) {
3305       unsigned BitWidth = RHSVal->getBitWidth();
3306       APInt LHSKnownZero(BitWidth, 0);
3307       APInt LHSKnownOne(BitWidth, 0);
3308       computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC,
3309                        Q.CxtI, Q.DT);
3310       if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0))
3311         return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy)
3312                                          : ConstantInt::getTrue(ITy);
3313     }
3314   }
3315
3316   // If the comparison is with the result of a select instruction, check whether
3317   // comparing with either branch of the select always yields the same value.
3318   if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3319     if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
3320       return V;
3321
3322   // If the comparison is with the result of a phi instruction, check whether
3323   // doing the compare with each incoming phi value yields a common result.
3324   if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3325     if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
3326       return V;
3327
3328   return nullptr;
3329 }
3330
3331 Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3332                               const DataLayout &DL,
3333                               const TargetLibraryInfo *TLI,
3334                               const DominatorTree *DT, AssumptionCache *AC,
3335                               const Instruction *CxtI) {
3336   return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI),
3337                             RecursionLimit);
3338 }
3339
3340 /// Given operands for an FCmpInst, see if we can fold the result.
3341 /// If not, this returns null.
3342 static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3343                                FastMathFlags FMF, const Query &Q,
3344                                unsigned MaxRecurse) {
3345   CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3346   assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
3347
3348   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3349     if (Constant *CRHS = dyn_cast<Constant>(RHS))
3350       return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3351
3352     // If we have a constant, make sure it is on the RHS.
3353     std::swap(LHS, RHS);
3354     Pred = CmpInst::getSwappedPredicate(Pred);
3355   }
3356
3357   // Fold trivial predicates.
3358   Type *RetTy = GetCompareTy(LHS);
3359   if (Pred == FCmpInst::FCMP_FALSE)
3360     return getFalse(RetTy);
3361   if (Pred == FCmpInst::FCMP_TRUE)
3362     return getTrue(RetTy);
3363
3364   // UNO/ORD predicates can be trivially folded if NaNs are ignored.
3365   if (FMF.noNaNs()) {
3366     if (Pred == FCmpInst::FCMP_UNO)
3367       return getFalse(RetTy);
3368     if (Pred == FCmpInst::FCMP_ORD)
3369       return getTrue(RetTy);
3370   }
3371
3372   // fcmp pred x, undef  and  fcmp pred undef, x
3373   // fold to true if unordered, false if ordered
3374   if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) {
3375     // Choosing NaN for the undef will always make unordered comparison succeed
3376     // and ordered comparison fail.
3377     return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
3378   }
3379
3380   // fcmp x,x -> true/false.  Not all compares are foldable.
3381   if (LHS == RHS) {
3382     if (CmpInst::isTrueWhenEqual(Pred))
3383       return getTrue(RetTy);
3384     if (CmpInst::isFalseWhenEqual(Pred))
3385       return getFalse(RetTy);
3386   }
3387
3388   // Handle fcmp with constant RHS
3389   const ConstantFP *CFP = nullptr;
3390   if (const auto *RHSC = dyn_cast<Constant>(RHS)) {
3391     if (RHS->getType()->isVectorTy())
3392       CFP = dyn_cast_or_null<ConstantFP>(RHSC->getSplatValue());
3393     else
3394       CFP = dyn_cast<ConstantFP>(RHSC);
3395   }
3396   if (CFP) {
3397     // If the constant is a nan, see if we can fold the comparison based on it.
3398     if (CFP->getValueAPF().isNaN()) {
3399       if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
3400         return getFalse(RetTy);
3401       assert(FCmpInst::isUnordered(Pred) &&
3402              "Comparison must be either ordered or unordered!");
3403       // True if unordered.
3404       return getTrue(RetTy);
3405     }
3406     // Check whether the constant is an infinity.
3407     if (CFP->getValueAPF().isInfinity()) {
3408       if (CFP->getValueAPF().isNegative()) {
3409         switch (Pred) {
3410         case FCmpInst::FCMP_OLT:
3411           // No value is ordered and less than negative infinity.
3412           return getFalse(RetTy);
3413         case FCmpInst::FCMP_UGE:
3414           // All values are unordered with or at least negative infinity.
3415           return getTrue(RetTy);
3416         default:
3417           break;
3418         }
3419       } else {
3420         switch (Pred) {
3421         case FCmpInst::FCMP_OGT:
3422           // No value is ordered and greater than infinity.
3423           return getFalse(RetTy);
3424         case FCmpInst::FCMP_ULE:
3425           // All values are unordered with and at most infinity.
3426           return getTrue(RetTy);
3427         default:
3428           break;
3429         }
3430       }
3431     }
3432     if (CFP->getValueAPF().isZero()) {
3433       switch (Pred) {
3434       case FCmpInst::FCMP_UGE:
3435         if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3436           return getTrue(RetTy);
3437         break;
3438       case FCmpInst::FCMP_OLT:
3439         // X < 0
3440         if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3441           return getFalse(RetTy);
3442         break;
3443       default:
3444         break;
3445       }
3446     }
3447   }
3448
3449   // If the comparison is with the result of a select instruction, check whether
3450   // comparing with either branch of the select always yields the same value.
3451   if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3452     if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
3453       return V;
3454
3455   // If the comparison is with the result of a phi instruction, check whether
3456   // doing the compare with each incoming phi value yields a common result.
3457   if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3458     if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
3459       return V;
3460
3461   return nullptr;
3462 }
3463
3464 Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3465                               FastMathFlags FMF, const DataLayout &DL,
3466                               const TargetLibraryInfo *TLI,
3467                               const DominatorTree *DT, AssumptionCache *AC,
3468                               const Instruction *CxtI) {
3469   return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF,
3470                             Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
3471 }
3472
3473 /// See if V simplifies when its operand Op is replaced with RepOp.
3474 static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
3475                                            const Query &Q,
3476                                            unsigned MaxRecurse) {
3477   // Trivial replacement.
3478   if (V == Op)
3479     return RepOp;
3480
3481   auto *I = dyn_cast<Instruction>(V);
3482   if (!I)
3483     return nullptr;
3484
3485   // If this is a binary operator, try to simplify it with the replaced op.
3486   if (auto *B = dyn_cast<BinaryOperator>(I)) {
3487     // Consider:
3488     //   %cmp = icmp eq i32 %x, 2147483647
3489     //   %add = add nsw i32 %x, 1
3490     //   %sel = select i1 %cmp, i32 -2147483648, i32 %add
3491     //
3492     // We can't replace %sel with %add unless we strip away the flags.
3493     if (isa<OverflowingBinaryOperator>(B))
3494       if (B->hasNoSignedWrap() || B->hasNoUnsignedWrap())
3495         return nullptr;
3496     if (isa<PossiblyExactOperator>(B))
3497       if (B->isExact())
3498         return nullptr;
3499
3500     if (MaxRecurse) {
3501       if (B->getOperand(0) == Op)
3502         return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q,
3503                              MaxRecurse - 1);
3504       if (B->getOperand(1) == Op)
3505         return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q,
3506                              MaxRecurse - 1);
3507     }
3508   }
3509
3510   // Same for CmpInsts.
3511   if (CmpInst *C = dyn_cast<CmpInst>(I)) {
3512     if (MaxRecurse) {
3513       if (C->getOperand(0) == Op)
3514         return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q,
3515                                MaxRecurse - 1);
3516       if (C->getOperand(1) == Op)
3517         return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q,
3518                                MaxRecurse - 1);
3519     }
3520   }
3521
3522   // TODO: We could hand off more cases to instsimplify here.
3523
3524   // If all operands are constant after substituting Op for RepOp then we can
3525   // constant fold the instruction.
3526   if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
3527     // Build a list of all constant operands.
3528     SmallVector<Constant *, 8> ConstOps;
3529     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3530       if (I->getOperand(i) == Op)
3531         ConstOps.push_back(CRepOp);
3532       else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i)))
3533         ConstOps.push_back(COp);
3534       else
3535         break;
3536     }
3537
3538     // All operands were constants, fold it.
3539     if (ConstOps.size() == I->getNumOperands()) {
3540       if (CmpInst *C = dyn_cast<CmpInst>(I))
3541         return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0],
3542                                                ConstOps[1], Q.DL, Q.TLI);
3543
3544       if (LoadInst *LI = dyn_cast<LoadInst>(I))
3545         if (!LI->isVolatile())
3546           return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL);
3547
3548       return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI);
3549     }
3550   }
3551
3552   return nullptr;
3553 }
3554
3555 /// Try to simplify a select instruction when its condition operand is an
3556 /// integer comparison where one operand of the compare is a constant.
3557 static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
3558                                     const APInt *Y, bool TrueWhenUnset) {
3559   const APInt *C;
3560
3561   // (X & Y) == 0 ? X & ~Y : X  --> X
3562   // (X & Y) != 0 ? X & ~Y : X  --> X & ~Y
3563   if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
3564       *Y == ~*C)
3565     return TrueWhenUnset ? FalseVal : TrueVal;
3566
3567   // (X & Y) == 0 ? X : X & ~Y  --> X & ~Y
3568   // (X & Y) != 0 ? X : X & ~Y  --> X
3569   if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
3570       *Y == ~*C)
3571     return TrueWhenUnset ? FalseVal : TrueVal;
3572
3573   if (Y->isPowerOf2()) {
3574     // (X & Y) == 0 ? X | Y : X  --> X | Y
3575     // (X & Y) != 0 ? X | Y : X  --> X
3576     if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
3577         *Y == *C)
3578       return TrueWhenUnset ? TrueVal : FalseVal;
3579
3580     // (X & Y) == 0 ? X : X | Y  --> X
3581     // (X & Y) != 0 ? X : X | Y  --> X | Y
3582     if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
3583         *Y == *C)
3584       return TrueWhenUnset ? TrueVal : FalseVal;
3585   }
3586
3587   return nullptr;
3588 }
3589
3590 /// An alternative way to test if a bit is set or not uses sgt/slt instead of
3591 /// eq/ne.
3592 static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *TrueVal,
3593                                            Value *FalseVal,
3594                                            bool TrueWhenUnset) {
3595   unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits();
3596   if (!BitWidth)
3597     return nullptr;
3598
3599   APInt MinSignedValue;
3600   Value *X;
3601   if (match(CmpLHS, m_Trunc(m_Value(X))) && (X == TrueVal || X == FalseVal)) {
3602     // icmp slt (trunc X), 0  <--> icmp ne (and X, C), 0
3603     // icmp sgt (trunc X), -1 <--> icmp eq (and X, C), 0
3604     unsigned DestSize = CmpLHS->getType()->getScalarSizeInBits();
3605     MinSignedValue = APInt::getSignedMinValue(DestSize).zext(BitWidth);
3606   } else {
3607     // icmp slt X, 0  <--> icmp ne (and X, C), 0
3608     // icmp sgt X, -1 <--> icmp eq (and X, C), 0
3609     X = CmpLHS;
3610     MinSignedValue = APInt::getSignedMinValue(BitWidth);
3611   }
3612
3613   if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, &MinSignedValue,
3614                                        TrueWhenUnset))
3615     return V;
3616
3617   return nullptr;
3618 }
3619
3620 /// Try to simplify a select instruction when its condition operand is an
3621 /// integer comparison.
3622 static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
3623                                          Value *FalseVal, const Query &Q,
3624                                          unsigned MaxRecurse) {
3625   ICmpInst::Predicate Pred;
3626   Value *CmpLHS, *CmpRHS;
3627   if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
3628     return nullptr;
3629
3630   // FIXME: This code is nearly duplicated in InstCombine. Using/refactoring
3631   // decomposeBitTestICmp() might help.
3632   if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero())) {
3633     Value *X;
3634     const APInt *Y;
3635     if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
3636       if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
3637                                            Pred == ICmpInst::ICMP_EQ))
3638         return V;
3639   } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) {
3640     // Comparing signed-less-than 0 checks if the sign bit is set.
3641     if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal,
3642                                                 false))
3643       return V;
3644   } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) {
3645     // Comparing signed-greater-than -1 checks if the sign bit is not set.
3646     if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal,
3647                                                 true))
3648       return V;
3649   }
3650
3651   if (CondVal->hasOneUse()) {
3652     const APInt *C;
3653     if (match(CmpRHS, m_APInt(C))) {
3654       // X < MIN ? T : F  -->  F
3655       if (Pred == ICmpInst::ICMP_SLT && C->isMinSignedValue())
3656         return FalseVal;
3657       // X < MIN ? T : F  -->  F
3658       if (Pred == ICmpInst::ICMP_ULT && C->isMinValue())
3659         return FalseVal;
3660       // X > MAX ? T : F  -->  F
3661       if (Pred == ICmpInst::ICMP_SGT && C->isMaxSignedValue())
3662         return FalseVal;
3663       // X > MAX ? T : F  -->  F
3664       if (Pred == ICmpInst::ICMP_UGT && C->isMaxValue())
3665         return FalseVal;
3666     }
3667   }
3668
3669   // If we have an equality comparison, then we know the value in one of the
3670   // arms of the select. See if substituting this value into the arm and
3671   // simplifying the result yields the same value as the other arm.
3672   if (Pred == ICmpInst::ICMP_EQ) {
3673     if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3674             TrueVal ||
3675         SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3676             TrueVal)
3677       return FalseVal;
3678     if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3679             FalseVal ||
3680         SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3681             FalseVal)
3682       return FalseVal;
3683   } else if (Pred == ICmpInst::ICMP_NE) {
3684     if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3685             FalseVal ||
3686         SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3687             FalseVal)
3688       return TrueVal;
3689     if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3690             TrueVal ||
3691         SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3692             TrueVal)
3693       return TrueVal;
3694   }
3695
3696   return nullptr;
3697 }
3698
3699 /// Given operands for a SelectInst, see if we can fold the result.
3700 /// If not, this returns null.
3701 static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal,
3702                                  Value *FalseVal, const Query &Q,
3703                                  unsigned MaxRecurse) {
3704   // select true, X, Y  -> X
3705   // select false, X, Y -> Y
3706   if (Constant *CB = dyn_cast<Constant>(CondVal)) {
3707     if (CB->isAllOnesValue())
3708       return TrueVal;
3709     if (CB->isNullValue())
3710       return FalseVal;
3711   }
3712
3713   // select C, X, X -> X
3714   if (TrueVal == FalseVal)
3715     return TrueVal;
3716
3717   if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y
3718     if (isa<Constant>(TrueVal))
3719       return TrueVal;
3720     return FalseVal;
3721   }
3722   if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X
3723     return FalseVal;
3724   if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X
3725     return TrueVal;
3726
3727   if (Value *V =
3728           simplifySelectWithICmpCond(CondVal, TrueVal, FalseVal, Q, MaxRecurse))
3729     return V;
3730
3731   return nullptr;
3732 }
3733
3734 Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
3735                                 const DataLayout &DL,
3736                                 const TargetLibraryInfo *TLI,
3737                                 const DominatorTree *DT, AssumptionCache *AC,
3738                                 const Instruction *CxtI) {
3739   return ::SimplifySelectInst(Cond, TrueVal, FalseVal,
3740                               Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
3741 }
3742
3743 /// Given operands for an GetElementPtrInst, see if we can fold the result.
3744 /// If not, this returns null.
3745 static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
3746                               const Query &Q, unsigned) {
3747   // The type of the GEP pointer operand.
3748   unsigned AS =
3749       cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace();
3750
3751   // getelementptr P -> P.
3752   if (Ops.size() == 1)
3753     return Ops[0];
3754
3755   // Compute the (pointer) type returned by the GEP instruction.
3756   Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1));
3757   Type *GEPTy = PointerType::get(LastType, AS);
3758   if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType()))
3759     GEPTy = VectorType::get(GEPTy, VT->getNumElements());
3760
3761   if (isa<UndefValue>(Ops[0]))
3762     return UndefValue::get(GEPTy);
3763
3764   if (Ops.size() == 2) {
3765     // getelementptr P, 0 -> P.
3766     if (match(Ops[1], m_Zero()))
3767       return Ops[0];
3768
3769     Type *Ty = SrcTy;
3770     if (Ty->isSized()) {
3771       Value *P;
3772       uint64_t C;
3773       uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
3774       // getelementptr P, N -> P if P points to a type of zero size.
3775       if (TyAllocSize == 0)
3776         return Ops[0];
3777
3778       // The following transforms are only safe if the ptrtoint cast
3779       // doesn't truncate the pointers.
3780       if (Ops[1]->getType()->getScalarSizeInBits() ==
3781           Q.DL.getPointerSizeInBits(AS)) {
3782         auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * {
3783           if (match(P, m_Zero()))
3784             return Constant::getNullValue(GEPTy);
3785           Value *Temp;
3786           if (match(P, m_PtrToInt(m_Value(Temp))))
3787             if (Temp->getType() == GEPTy)
3788               return Temp;
3789           return nullptr;
3790         };
3791
3792         // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
3793         if (TyAllocSize == 1 &&
3794             match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0])))))
3795           if (Value *R = PtrToIntOrZero(P))
3796             return R;
3797
3798         // getelementptr V, (ashr (sub P, V), C) -> Q
3799         // if P points to a type of size 1 << C.
3800         if (match(Ops[1],
3801                   m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))),
3802                          m_ConstantInt(C))) &&
3803             TyAllocSize == 1ULL << C)
3804           if (Value *R = PtrToIntOrZero(P))
3805             return R;
3806
3807         // getelementptr V, (sdiv (sub P, V), C) -> Q
3808         // if P points to a type of size C.
3809         if (match(Ops[1],
3810                   m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))),
3811                          m_SpecificInt(TyAllocSize))))
3812           if (Value *R = PtrToIntOrZero(P))
3813             return R;
3814       }
3815     }
3816   }
3817
3818   if (Q.DL.getTypeAllocSize(LastType) == 1 &&
3819       all_of(Ops.slice(1).drop_back(1),
3820              [](Value *Idx) { return match(Idx, m_Zero()); })) {
3821     unsigned PtrWidth =
3822         Q.DL.getPointerSizeInBits(Ops[0]->getType()->getPointerAddressSpace());
3823     if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == PtrWidth) {
3824       APInt BasePtrOffset(PtrWidth, 0);
3825       Value *StrippedBasePtr =
3826           Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL,
3827                                                             BasePtrOffset);
3828
3829       // gep (gep V, C), (sub 0, V) -> C
3830       if (match(Ops.back(),
3831                 m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) {
3832         auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
3833         return ConstantExpr::getIntToPtr(CI, GEPTy);
3834       }
3835       // gep (gep V, C), (xor V, -1) -> C-1
3836       if (match(Ops.back(),
3837                 m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) {
3838         auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
3839         return ConstantExpr::getIntToPtr(CI, GEPTy);
3840       }
3841     }
3842   }
3843
3844   // Check to see if this is constant foldable.
3845   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3846     if (!isa<Constant>(Ops[i]))
3847       return nullptr;
3848
3849   return ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]),
3850                                         Ops.slice(1));
3851 }
3852
3853 Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
3854                              const DataLayout &DL,
3855                              const TargetLibraryInfo *TLI,
3856                              const DominatorTree *DT, AssumptionCache *AC,
3857                              const Instruction *CxtI) {
3858   return ::SimplifyGEPInst(SrcTy, Ops,
3859                            Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
3860 }
3861
3862 /// Given operands for an InsertValueInst, see if we can fold the result.
3863 /// If not, this returns null.
3864 static Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
3865                                       ArrayRef<unsigned> Idxs, const Query &Q,
3866                                       unsigned) {
3867   if (Constant *CAgg = dyn_cast<Constant>(Agg))
3868     if (Constant *CVal = dyn_cast<Constant>(Val))
3869       return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
3870
3871   // insertvalue x, undef, n -> x
3872   if (match(Val, m_Undef()))
3873     return Agg;
3874
3875   // insertvalue x, (extractvalue y, n), n
3876   if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
3877     if (EV->getAggregateOperand()->getType() == Agg->getType() &&
3878         EV->getIndices() == Idxs) {
3879       // insertvalue undef, (extractvalue y, n), n -> y
3880       if (match(Agg, m_Undef()))
3881         return EV->getAggregateOperand();
3882
3883       // insertvalue y, (extractvalue y, n), n -> y
3884       if (Agg == EV->getAggregateOperand())
3885         return Agg;
3886     }
3887
3888   return nullptr;
3889 }
3890
3891 Value *llvm::SimplifyInsertValueInst(
3892     Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, const DataLayout &DL,
3893     const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC,
3894     const Instruction *CxtI) {
3895   return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, AC, CxtI),
3896                                    RecursionLimit);
3897 }
3898
3899 /// Given operands for an ExtractValueInst, see if we can fold the result.
3900 /// If not, this returns null.
3901 static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
3902                                        const Query &, unsigned) {
3903   if (auto *CAgg = dyn_cast<Constant>(Agg))
3904     return ConstantFoldExtractValueInstruction(CAgg, Idxs);
3905
3906   // extractvalue x, (insertvalue y, elt, n), n -> elt
3907   unsigned NumIdxs = Idxs.size();
3908   for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
3909        IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
3910     ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
3911     unsigned NumInsertValueIdxs = InsertValueIdxs.size();
3912     unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
3913     if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
3914         Idxs.slice(0, NumCommonIdxs)) {
3915       if (NumIdxs == NumInsertValueIdxs)
3916         return IVI->getInsertedValueOperand();
3917       break;
3918     }
3919   }
3920
3921   return nullptr;
3922 }
3923
3924 Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
3925                                       const DataLayout &DL,
3926                                       const TargetLibraryInfo *TLI,
3927                                       const DominatorTree *DT,
3928                                       AssumptionCache *AC,
3929                                       const Instruction *CxtI) {
3930   return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI),
3931                                     RecursionLimit);
3932 }
3933
3934 /// Given operands for an ExtractElementInst, see if we can fold the result.
3935 /// If not, this returns null.
3936 static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const Query &,
3937                                          unsigned) {
3938   if (auto *CVec = dyn_cast<Constant>(Vec)) {
3939     if (auto *CIdx = dyn_cast<Constant>(Idx))
3940       return ConstantFoldExtractElementInstruction(CVec, CIdx);
3941
3942     // The index is not relevant if our vector is a splat.
3943     if (auto *Splat = CVec->getSplatValue())
3944       return Splat;
3945
3946     if (isa<UndefValue>(Vec))
3947       return UndefValue::get(Vec->getType()->getVectorElementType());
3948   }
3949
3950   // If extracting a specified index from the vector, see if we can recursively
3951   // find a previously computed scalar that was inserted into the vector.
3952   if (auto *IdxC = dyn_cast<ConstantInt>(Idx))
3953     if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
3954       return Elt;
3955
3956   return nullptr;
3957 }
3958
3959 Value *llvm::SimplifyExtractElementInst(
3960     Value *Vec, Value *Idx, const DataLayout &DL, const TargetLibraryInfo *TLI,
3961     const DominatorTree *DT, AssumptionCache *AC, const Instruction *CxtI) {
3962   return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, AC, CxtI),
3963                                       RecursionLimit);
3964 }
3965
3966 /// See if we can fold the given phi. If not, returns null.
3967 static Value *SimplifyPHINode(PHINode *PN, const Query &Q) {
3968   // If all of the PHI's incoming values are the same then replace the PHI node
3969   // with the common value.
3970   Value *CommonValue = nullptr;
3971   bool HasUndefInput = false;
3972   for (Value *Incoming : PN->incoming_values()) {
3973     // If the incoming value is the phi node itself, it can safely be skipped.
3974     if (Incoming == PN) continue;
3975     if (isa<UndefValue>(Incoming)) {
3976       // Remember that we saw an undef value, but otherwise ignore them.
3977       HasUndefInput = true;
3978       continue;
3979     }
3980     if (CommonValue && Incoming != CommonValue)
3981       return nullptr;  // Not the same, bail out.
3982     CommonValue = Incoming;
3983   }
3984
3985   // If CommonValue is null then all of the incoming values were either undef or
3986   // equal to the phi node itself.
3987   if (!CommonValue)
3988     return UndefValue::get(PN->getType());
3989
3990   // If we have a PHI node like phi(X, undef, X), where X is defined by some
3991   // instruction, we cannot return X as the result of the PHI node unless it
3992   // dominates the PHI block.
3993   if (HasUndefInput)
3994     return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr;
3995
3996   return CommonValue;
3997 }
3998
3999 static Value *SimplifyCastInst(unsigned CastOpc, Value *Op,
4000                                Type *Ty, const Query &Q, unsigned MaxRecurse) {
4001   if (auto *C = dyn_cast<Constant>(Op))
4002     return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
4003
4004   if (auto *CI = dyn_cast<CastInst>(Op)) {
4005     auto *Src = CI->getOperand(0);
4006     Type *SrcTy = Src->getType();
4007     Type *MidTy = CI->getType();
4008     Type *DstTy = Ty;
4009     if (Src->getType() == Ty) {
4010       auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
4011       auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
4012       Type *SrcIntPtrTy =
4013           SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
4014       Type *MidIntPtrTy =
4015           MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
4016       Type *DstIntPtrTy =
4017           DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
4018       if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
4019                                          SrcIntPtrTy, MidIntPtrTy,
4020                                          DstIntPtrTy) == Instruction::BitCast)
4021         return Src;
4022     }
4023   }
4024
4025   // bitcast x -> x
4026   if (CastOpc == Instruction::BitCast)
4027     if (Op->getType() == Ty)
4028       return Op;
4029
4030   return nullptr;
4031 }
4032
4033 Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
4034                               const DataLayout &DL,
4035                               const TargetLibraryInfo *TLI,
4036                               const DominatorTree *DT, AssumptionCache *AC,
4037                               const Instruction *CxtI) {
4038   return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, AC, CxtI),
4039                             RecursionLimit);
4040 }
4041
4042 //=== Helper functions for higher up the class hierarchy.
4043
4044 /// Given operands for a BinaryOperator, see if we can fold the result.
4045 /// If not, this returns null.
4046 static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4047                             const Query &Q, unsigned MaxRecurse) {
4048   switch (Opcode) {
4049   case Instruction::Add:
4050     return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
4051                            Q, MaxRecurse);
4052   case Instruction::FAdd:
4053     return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4054
4055   case Instruction::Sub:
4056     return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
4057                            Q, MaxRecurse);
4058   case Instruction::FSub:
4059     return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4060
4061   case Instruction::Mul:  return SimplifyMulInst (LHS, RHS, Q, MaxRecurse);
4062   case Instruction::FMul:
4063     return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4064   case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse);
4065   case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse);
4066   case Instruction::FDiv:
4067       return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4068   case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse);
4069   case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse);
4070   case Instruction::FRem:
4071       return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4072   case Instruction::Shl:
4073     return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
4074                            Q, MaxRecurse);
4075   case Instruction::LShr:
4076     return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse);
4077   case Instruction::AShr:
4078     return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse);
4079   case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse);
4080   case Instruction::Or:  return SimplifyOrInst (LHS, RHS, Q, MaxRecurse);
4081   case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse);
4082   default:
4083     if (Constant *CLHS = dyn_cast<Constant>(LHS))
4084       if (Constant *CRHS = dyn_cast<Constant>(RHS))
4085         return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
4086
4087     // If the operation is associative, try some generic simplifications.
4088     if (Instruction::isAssociative(Opcode))
4089       if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse))
4090         return V;
4091
4092     // If the operation is with the result of a select instruction check whether
4093     // operating on either branch of the select always yields the same value.
4094     if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4095       if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse))
4096         return V;
4097
4098     // If the operation is with the result of a phi instruction, check whether
4099     // operating on all incoming values of the phi always yields the same value.
4100     if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4101       if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse))
4102         return V;
4103
4104     return nullptr;
4105   }
4106 }
4107
4108 /// Given operands for a BinaryOperator, see if we can fold the result.
4109 /// If not, this returns null.
4110 /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
4111 /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
4112 static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4113                               const FastMathFlags &FMF, const Query &Q,
4114                               unsigned MaxRecurse) {
4115   switch (Opcode) {
4116   case Instruction::FAdd:
4117     return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
4118   case Instruction::FSub:
4119     return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
4120   case Instruction::FMul:
4121     return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
4122   case Instruction::FDiv:
4123     return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
4124   default:
4125     return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
4126   }
4127 }
4128
4129 Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4130                            const DataLayout &DL, const TargetLibraryInfo *TLI,
4131                            const DominatorTree *DT, AssumptionCache *AC,
4132                            const Instruction *CxtI) {
4133   return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, AC, CxtI),
4134                          RecursionLimit);
4135 }
4136
4137 Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4138                              const FastMathFlags &FMF, const DataLayout &DL,
4139                              const TargetLibraryInfo *TLI,
4140                              const DominatorTree *DT, AssumptionCache *AC,
4141                              const Instruction *CxtI) {
4142   return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, AC, CxtI),
4143                            RecursionLimit);
4144 }
4145
4146 /// Given operands for a CmpInst, see if we can fold the result.
4147 static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4148                               const Query &Q, unsigned MaxRecurse) {
4149   if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
4150     return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
4151   return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4152 }
4153
4154 Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4155                              const DataLayout &DL, const TargetLibraryInfo *TLI,
4156                              const DominatorTree *DT, AssumptionCache *AC,
4157                              const Instruction *CxtI) {
4158   return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI),
4159                            RecursionLimit);
4160 }
4161
4162 static bool IsIdempotent(Intrinsic::ID ID) {
4163   switch (ID) {
4164   default: return false;
4165
4166   // Unary idempotent: f(f(x)) = f(x)
4167   case Intrinsic::fabs:
4168   case Intrinsic::floor:
4169   case Intrinsic::ceil:
4170   case Intrinsic::trunc:
4171   case Intrinsic::rint:
4172   case Intrinsic::nearbyint:
4173   case Intrinsic::round:
4174     return true;
4175   }
4176 }
4177
4178 static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset,
4179                                    const DataLayout &DL) {
4180   GlobalValue *PtrSym;
4181   APInt PtrOffset;
4182   if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
4183     return nullptr;
4184
4185   Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext());
4186   Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
4187   Type *Int32PtrTy = Int32Ty->getPointerTo();
4188   Type *Int64Ty = Type::getInt64Ty(Ptr->getContext());
4189
4190   auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
4191   if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
4192     return nullptr;
4193
4194   uint64_t OffsetInt = OffsetConstInt->getSExtValue();
4195   if (OffsetInt % 4 != 0)
4196     return nullptr;
4197
4198   Constant *C = ConstantExpr::getGetElementPtr(
4199       Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy),
4200       ConstantInt::get(Int64Ty, OffsetInt / 4));
4201   Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL);
4202   if (!Loaded)
4203     return nullptr;
4204
4205   auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
4206   if (!LoadedCE)
4207     return nullptr;
4208
4209   if (LoadedCE->getOpcode() == Instruction::Trunc) {
4210     LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
4211     if (!LoadedCE)
4212       return nullptr;
4213   }
4214
4215   if (LoadedCE->getOpcode() != Instruction::Sub)
4216     return nullptr;
4217
4218   auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
4219   if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
4220     return nullptr;
4221   auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
4222
4223   Constant *LoadedRHS = LoadedCE->getOperand(1);
4224   GlobalValue *LoadedRHSSym;
4225   APInt LoadedRHSOffset;
4226   if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
4227                                   DL) ||
4228       PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
4229     return nullptr;
4230
4231   return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy);
4232 }
4233
4234 static bool maskIsAllZeroOrUndef(Value *Mask) {
4235   auto *ConstMask = dyn_cast<Constant>(Mask);
4236   if (!ConstMask)
4237     return false;
4238   if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask))
4239     return true;
4240   for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E;
4241        ++I) {
4242     if (auto *MaskElt = ConstMask->getAggregateElement(I))
4243       if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt))
4244         continue;
4245     return false;
4246   }
4247   return true;
4248 }
4249
4250 template <typename IterTy>
4251 static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd,
4252                                 const Query &Q, unsigned MaxRecurse) {
4253   Intrinsic::ID IID = F->getIntrinsicID();
4254   unsigned NumOperands = std::distance(ArgBegin, ArgEnd);
4255
4256   // Unary Ops
4257   if (NumOperands == 1) {
4258     // Perform idempotent optimizations
4259     if (IsIdempotent(IID)) {
4260       if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) {
4261         if (II->getIntrinsicID() == IID)
4262           return II;
4263       }
4264     }
4265
4266     switch (IID) {
4267     case Intrinsic::fabs: {
4268       if (SignBitMustBeZero(*ArgBegin, Q.TLI))
4269         return *ArgBegin;
4270     }
4271     default:
4272       return nullptr;
4273     }
4274   }
4275
4276   // Binary Ops
4277   if (NumOperands == 2) {
4278     Value *LHS = *ArgBegin;
4279     Value *RHS = *(ArgBegin + 1);
4280     Type *ReturnType = F->getReturnType();
4281
4282     switch (IID) {
4283     case Intrinsic::usub_with_overflow:
4284     case Intrinsic::ssub_with_overflow: {
4285       // X - X -> { 0, false }
4286       if (LHS == RHS)
4287         return Constant::getNullValue(ReturnType);
4288
4289       // X - undef -> undef
4290       // undef - X -> undef
4291       if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS))
4292         return UndefValue::get(ReturnType);
4293
4294       return nullptr;
4295     }
4296     case Intrinsic::uadd_with_overflow:
4297     case Intrinsic::sadd_with_overflow: {
4298       // X + undef -> undef
4299       if (isa<UndefValue>(RHS))
4300         return UndefValue::get(ReturnType);
4301
4302       return nullptr;
4303     }
4304     case Intrinsic::umul_with_overflow:
4305     case Intrinsic::smul_with_overflow: {
4306       // X * 0 -> { 0, false }
4307       if (match(RHS, m_Zero()))
4308         return Constant::getNullValue(ReturnType);
4309
4310       // X * undef -> { 0, false }
4311       if (match(RHS, m_Undef()))
4312         return Constant::getNullValue(ReturnType);
4313
4314       return nullptr;
4315     }
4316     case Intrinsic::load_relative: {
4317       Constant *C0 = dyn_cast<Constant>(LHS);
4318       Constant *C1 = dyn_cast<Constant>(RHS);
4319       if (C0 && C1)
4320         return SimplifyRelativeLoad(C0, C1, Q.DL);
4321       return nullptr;
4322     }
4323     default:
4324       return nullptr;
4325     }
4326   }
4327
4328   // Simplify calls to llvm.masked.load.*
4329   switch (IID) {
4330   case Intrinsic::masked_load: {
4331     Value *MaskArg = ArgBegin[2];
4332     Value *PassthruArg = ArgBegin[3];
4333     // If the mask is all zeros or undef, the "passthru" argument is the result.
4334     if (maskIsAllZeroOrUndef(MaskArg))
4335       return PassthruArg;
4336     return nullptr;
4337   }
4338   default:
4339     return nullptr;
4340   }
4341 }
4342
4343 template <typename IterTy>
4344 static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd,
4345                            const Query &Q, unsigned MaxRecurse) {
4346   Type *Ty = V->getType();
4347   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
4348     Ty = PTy->getElementType();
4349   FunctionType *FTy = cast<FunctionType>(Ty);
4350
4351   // call undef -> undef
4352   // call null -> undef
4353   if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V))
4354     return UndefValue::get(FTy->getReturnType());
4355
4356   Function *F = dyn_cast<Function>(V);
4357   if (!F)
4358     return nullptr;
4359
4360   if (F->isIntrinsic())
4361     if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse))
4362       return Ret;
4363
4364   if (!canConstantFoldCallTo(F))
4365     return nullptr;
4366
4367   SmallVector<Constant *, 4> ConstantArgs;
4368   ConstantArgs.reserve(ArgEnd - ArgBegin);
4369   for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) {
4370     Constant *C = dyn_cast<Constant>(*I);
4371     if (!C)
4372       return nullptr;
4373     ConstantArgs.push_back(C);
4374   }
4375
4376   return ConstantFoldCall(F, ConstantArgs, Q.TLI);
4377 }
4378
4379 Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin,
4380                           User::op_iterator ArgEnd, const DataLayout &DL,
4381                           const TargetLibraryInfo *TLI, const DominatorTree *DT,
4382                           AssumptionCache *AC, const Instruction *CxtI) {
4383   return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AC, CxtI),
4384                         RecursionLimit);
4385 }
4386
4387 Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args,
4388                           const DataLayout &DL, const TargetLibraryInfo *TLI,
4389                           const DominatorTree *DT, AssumptionCache *AC,
4390                           const Instruction *CxtI) {
4391   return ::SimplifyCall(V, Args.begin(), Args.end(),
4392                         Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
4393 }
4394
4395 /// See if we can compute a simplified version of this instruction.
4396 /// If not, this returns null.
4397 Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout &DL,
4398                                  const TargetLibraryInfo *TLI,
4399                                  const DominatorTree *DT, AssumptionCache *AC) {
4400   Value *Result;
4401
4402   switch (I->getOpcode()) {
4403   default:
4404     Result = ConstantFoldInstruction(I, DL, TLI);
4405     break;
4406   case Instruction::FAdd:
4407     Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1),
4408                               I->getFastMathFlags(), DL, TLI, DT, AC, I);
4409     break;
4410   case Instruction::Add:
4411     Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
4412                              cast<BinaryOperator>(I)->hasNoSignedWrap(),
4413                              cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL,
4414                              TLI, DT, AC, I);
4415     break;
4416   case Instruction::FSub:
4417     Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1),
4418                               I->getFastMathFlags(), DL, TLI, DT, AC, I);
4419     break;
4420   case Instruction::Sub:
4421     Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
4422                              cast<BinaryOperator>(I)->hasNoSignedWrap(),
4423                              cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL,
4424                              TLI, DT, AC, I);
4425     break;
4426   case Instruction::FMul:
4427     Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1),
4428                               I->getFastMathFlags(), DL, TLI, DT, AC, I);
4429     break;
4430   case Instruction::Mul:
4431     Result =
4432         SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4433     break;
4434   case Instruction::SDiv:
4435     Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4436                               AC, I);
4437     break;
4438   case Instruction::UDiv:
4439     Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4440                               AC, I);
4441     break;
4442   case Instruction::FDiv:
4443     Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1),
4444                               I->getFastMathFlags(), DL, TLI, DT, AC, I);
4445     break;
4446   case Instruction::SRem:
4447     Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4448                               AC, I);
4449     break;
4450   case Instruction::URem:
4451     Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4452                               AC, I);
4453     break;
4454   case Instruction::FRem:
4455     Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1),
4456                               I->getFastMathFlags(), DL, TLI, DT, AC, I);
4457     break;
4458   case Instruction::Shl:
4459     Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1),
4460                              cast<BinaryOperator>(I)->hasNoSignedWrap(),
4461                              cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL,
4462                              TLI, DT, AC, I);
4463     break;
4464   case Instruction::LShr:
4465     Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1),
4466                               cast<BinaryOperator>(I)->isExact(), DL, TLI, DT,
4467                               AC, I);
4468     break;
4469   case Instruction::AShr:
4470     Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1),
4471                               cast<BinaryOperator>(I)->isExact(), DL, TLI, DT,
4472                               AC, I);
4473     break;
4474   case Instruction::And:
4475     Result =
4476         SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4477     break;
4478   case Instruction::Or:
4479     Result =
4480         SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4481     break;
4482   case Instruction::Xor:
4483     Result =
4484         SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4485     break;
4486   case Instruction::ICmp:
4487     Result =
4488         SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), I->getOperand(0),
4489                          I->getOperand(1), DL, TLI, DT, AC, I);
4490     break;
4491   case Instruction::FCmp:
4492     Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
4493                               I->getOperand(0), I->getOperand(1),
4494                               I->getFastMathFlags(), DL, TLI, DT, AC, I);
4495     break;
4496   case Instruction::Select:
4497     Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
4498                                 I->getOperand(2), DL, TLI, DT, AC, I);
4499     break;
4500   case Instruction::GetElementPtr: {
4501     SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
4502     Result = SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(),
4503                              Ops, DL, TLI, DT, AC, I);
4504     break;
4505   }
4506   case Instruction::InsertValue: {
4507     InsertValueInst *IV = cast<InsertValueInst>(I);
4508     Result = SimplifyInsertValueInst(IV->getAggregateOperand(),
4509                                      IV->getInsertedValueOperand(),
4510                                      IV->getIndices(), DL, TLI, DT, AC, I);
4511     break;
4512   }
4513   case Instruction::ExtractValue: {
4514     auto *EVI = cast<ExtractValueInst>(I);
4515     Result = SimplifyExtractValueInst(EVI->getAggregateOperand(),
4516                                       EVI->getIndices(), DL, TLI, DT, AC, I);
4517     break;
4518   }
4519   case Instruction::ExtractElement: {
4520     auto *EEI = cast<ExtractElementInst>(I);
4521     Result = SimplifyExtractElementInst(
4522         EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, AC, I);
4523     break;
4524   }
4525   case Instruction::PHI:
4526     Result = SimplifyPHINode(cast<PHINode>(I), Query(DL, TLI, DT, AC, I));
4527     break;
4528   case Instruction::Call: {
4529     CallSite CS(cast<CallInst>(I));
4530     Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), DL,
4531                           TLI, DT, AC, I);
4532     break;
4533   }
4534 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
4535 #include "llvm/IR/Instruction.def"
4536 #undef HANDLE_CAST_INST
4537     Result = SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(),
4538                               DL, TLI, DT, AC, I);
4539     break;
4540   }
4541
4542   // In general, it is possible for computeKnownBits to determine all bits in a
4543   // value even when the operands are not all constants.
4544   if (!Result && I->getType()->isIntOrIntVectorTy()) {
4545     unsigned BitWidth = I->getType()->getScalarSizeInBits();
4546     APInt KnownZero(BitWidth, 0);
4547     APInt KnownOne(BitWidth, 0);
4548     computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, AC, I, DT);
4549     if ((KnownZero | KnownOne).isAllOnesValue())
4550       Result = ConstantInt::get(I->getType(), KnownOne);
4551   }
4552
4553   /// If called on unreachable code, the above logic may report that the
4554   /// instruction simplified to itself.  Make life easier for users by
4555   /// detecting that case here, returning a safe value instead.
4556   return Result == I ? UndefValue::get(I->getType()) : Result;
4557 }
4558
4559 /// \brief Implementation of recursive simplification through an instruction's
4560 /// uses.
4561 ///
4562 /// This is the common implementation of the recursive simplification routines.
4563 /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
4564 /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
4565 /// instructions to process and attempt to simplify it using
4566 /// InstructionSimplify.
4567 ///
4568 /// This routine returns 'true' only when *it* simplifies something. The passed
4569 /// in simplified value does not count toward this.
4570 static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV,
4571                                               const TargetLibraryInfo *TLI,
4572                                               const DominatorTree *DT,
4573                                               AssumptionCache *AC) {
4574   bool Simplified = false;
4575   SmallSetVector<Instruction *, 8> Worklist;
4576   const DataLayout &DL = I->getModule()->getDataLayout();
4577
4578   // If we have an explicit value to collapse to, do that round of the
4579   // simplification loop by hand initially.
4580   if (SimpleV) {
4581     for (User *U : I->users())
4582       if (U != I)
4583         Worklist.insert(cast<Instruction>(U));
4584
4585     // Replace the instruction with its simplified value.
4586     I->replaceAllUsesWith(SimpleV);
4587
4588     // Gracefully handle edge cases where the instruction is not wired into any
4589     // parent block.
4590     if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) &&
4591         !I->mayHaveSideEffects())
4592       I->eraseFromParent();
4593   } else {
4594     Worklist.insert(I);
4595   }
4596
4597   // Note that we must test the size on each iteration, the worklist can grow.
4598   for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
4599     I = Worklist[Idx];
4600
4601     // See if this instruction simplifies.
4602     SimpleV = SimplifyInstruction(I, DL, TLI, DT, AC);
4603     if (!SimpleV)
4604       continue;
4605
4606     Simplified = true;
4607
4608     // Stash away all the uses of the old instruction so we can check them for
4609     // recursive simplifications after a RAUW. This is cheaper than checking all
4610     // uses of To on the recursive step in most cases.
4611     for (User *U : I->users())
4612       Worklist.insert(cast<Instruction>(U));
4613
4614     // Replace the instruction with its simplified value.
4615     I->replaceAllUsesWith(SimpleV);
4616
4617     // Gracefully handle edge cases where the instruction is not wired into any
4618     // parent block.
4619     if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) &&
4620         !I->mayHaveSideEffects())
4621       I->eraseFromParent();
4622   }
4623   return Simplified;
4624 }
4625
4626 bool llvm::recursivelySimplifyInstruction(Instruction *I,
4627                                           const TargetLibraryInfo *TLI,
4628                                           const DominatorTree *DT,
4629                                           AssumptionCache *AC) {
4630   return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC);
4631 }
4632
4633 bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
4634                                          const TargetLibraryInfo *TLI,
4635                                          const DominatorTree *DT,
4636                                          AssumptionCache *AC) {
4637   assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
4638   assert(SimpleV && "Must provide a simplified value.");
4639   return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC);
4640 }