xref: /llvm-project/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (revision 3af59cfe0b5a319c165e3c74300aacdd42827c2d)
1 //===-- ConstraintElimination.cpp - Eliminate conds using constraints. ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Eliminate conditions based on constraints collected from dominating
10 // conditions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Scalar/ConstraintElimination.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/ScopeExit.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/ConstraintSystem.h"
20 #include "llvm/Analysis/GlobalsModRef.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
23 #include "llvm/Analysis/ScalarEvolution.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
25 #include "llvm/Analysis/ValueTracking.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/IRBuilder.h"
30 #include "llvm/IR/InstrTypes.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/PatternMatch.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/DebugCounter.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Transforms/Utils/Cloning.h"
40 #include "llvm/Transforms/Utils/ValueMapper.h"
41 
42 #include <cmath>
43 #include <optional>
44 #include <string>
45 
46 using namespace llvm;
47 using namespace PatternMatch;
48 
49 #define DEBUG_TYPE "constraint-elimination"
50 
51 STATISTIC(NumCondsRemoved, "Number of instructions removed");
52 DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
53               "Controls which conditions are eliminated");
54 
55 static cl::opt<unsigned>
56     MaxRows("constraint-elimination-max-rows", cl::init(500), cl::Hidden,
57             cl::desc("Maximum number of rows to keep in constraint system"));
58 
59 static cl::opt<bool> DumpReproducers(
60     "constraint-elimination-dump-reproducers", cl::init(false), cl::Hidden,
61     cl::desc("Dump IR to reproduce successful transformations."));
62 
63 static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
64 static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min();
65 
66 // A helper to multiply 2 signed integers where overflowing is allowed.
67 static int64_t multiplyWithOverflow(int64_t A, int64_t B) {
68   int64_t Result;
69   MulOverflow(A, B, Result);
70   return Result;
71 }
72 
73 // A helper to add 2 signed integers where overflowing is allowed.
74 static int64_t addWithOverflow(int64_t A, int64_t B) {
75   int64_t Result;
76   AddOverflow(A, B, Result);
77   return Result;
78 }
79 
80 static Instruction *getContextInstForUse(Use &U) {
81   Instruction *UserI = cast<Instruction>(U.getUser());
82   if (auto *Phi = dyn_cast<PHINode>(UserI))
83     UserI = Phi->getIncomingBlock(U)->getTerminator();
84   return UserI;
85 }
86 
87 namespace {
88 /// Struct to express a condition of the form %Op0 Pred %Op1.
89 struct ConditionTy {
90   CmpInst::Predicate Pred;
91   Value *Op0;
92   Value *Op1;
93 
94   ConditionTy()
95       : Pred(CmpInst::BAD_ICMP_PREDICATE), Op0(nullptr), Op1(nullptr) {}
96   ConditionTy(CmpInst::Predicate Pred, Value *Op0, Value *Op1)
97       : Pred(Pred), Op0(Op0), Op1(Op1) {}
98 };
99 
100 /// Represents either
101 ///  * a condition that holds on entry to a block (=condition fact)
102 ///  * an assume (=assume fact)
103 ///  * a use of a compare instruction to simplify.
104 /// It also tracks the Dominator DFS in and out numbers for each entry.
105 struct FactOrCheck {
106   enum class EntryTy {
107     ConditionFact, /// A condition that holds on entry to a block.
108     InstFact,      /// A fact that holds after Inst executed (e.g. an assume or
109                    /// min/mix intrinsic.
110     InstCheck,     /// An instruction to simplify (e.g. an overflow math
111                    /// intrinsics).
112     UseCheck       /// An use of a compare instruction to simplify.
113   };
114 
115   union {
116     Instruction *Inst;
117     Use *U;
118     ConditionTy Cond;
119   };
120 
121   /// A pre-condition that must hold for the current fact to be added to the
122   /// system.
123   ConditionTy DoesHold;
124 
125   unsigned NumIn;
126   unsigned NumOut;
127   EntryTy Ty;
128 
129   FactOrCheck(EntryTy Ty, DomTreeNode *DTN, Instruction *Inst)
130       : Inst(Inst), NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()),
131         Ty(Ty) {}
132 
133   FactOrCheck(DomTreeNode *DTN, Use *U)
134       : U(U), DoesHold(CmpInst::BAD_ICMP_PREDICATE, nullptr, nullptr),
135         NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()),
136         Ty(EntryTy::UseCheck) {}
137 
138   FactOrCheck(DomTreeNode *DTN, CmpInst::Predicate Pred, Value *Op0, Value *Op1,
139               ConditionTy Precond = ConditionTy())
140       : Cond(Pred, Op0, Op1), DoesHold(Precond), NumIn(DTN->getDFSNumIn()),
141         NumOut(DTN->getDFSNumOut()), Ty(EntryTy::ConditionFact) {}
142 
143   static FactOrCheck getConditionFact(DomTreeNode *DTN, CmpInst::Predicate Pred,
144                                       Value *Op0, Value *Op1,
145                                       ConditionTy Precond = ConditionTy()) {
146     return FactOrCheck(DTN, Pred, Op0, Op1, Precond);
147   }
148 
149   static FactOrCheck getInstFact(DomTreeNode *DTN, Instruction *Inst) {
150     return FactOrCheck(EntryTy::InstFact, DTN, Inst);
151   }
152 
153   static FactOrCheck getCheck(DomTreeNode *DTN, Use *U) {
154     return FactOrCheck(DTN, U);
155   }
156 
157   static FactOrCheck getCheck(DomTreeNode *DTN, CallInst *CI) {
158     return FactOrCheck(EntryTy::InstCheck, DTN, CI);
159   }
160 
161   bool isCheck() const {
162     return Ty == EntryTy::InstCheck || Ty == EntryTy::UseCheck;
163   }
164 
165   Instruction *getContextInst() const {
166     if (Ty == EntryTy::UseCheck)
167       return getContextInstForUse(*U);
168     return Inst;
169   }
170 
171   Instruction *getInstructionToSimplify() const {
172     assert(isCheck());
173     if (Ty == EntryTy::InstCheck)
174       return Inst;
175     // The use may have been simplified to a constant already.
176     return dyn_cast<Instruction>(*U);
177   }
178 
179   bool isConditionFact() const { return Ty == EntryTy::ConditionFact; }
180 };
181 
182 /// Keep state required to build worklist.
183 struct State {
184   DominatorTree &DT;
185   LoopInfo &LI;
186   ScalarEvolution &SE;
187   SmallVector<FactOrCheck, 64> WorkList;
188 
189   State(DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE)
190       : DT(DT), LI(LI), SE(SE) {}
191 
192   /// Process block \p BB and add known facts to work-list.
193   void addInfoFor(BasicBlock &BB);
194 
195   /// Try to add facts for loop inductions (AddRecs) in EQ/NE compares
196   /// controlling the loop header.
197   void addInfoForInductions(BasicBlock &BB);
198 
199   /// Returns true if we can add a known condition from BB to its successor
200   /// block Succ.
201   bool canAddSuccessor(BasicBlock &BB, BasicBlock *Succ) const {
202     return DT.dominates(BasicBlockEdge(&BB, Succ), Succ);
203   }
204 };
205 
206 class ConstraintInfo;
207 
208 struct StackEntry {
209   unsigned NumIn;
210   unsigned NumOut;
211   bool IsSigned = false;
212   /// Variables that can be removed from the system once the stack entry gets
213   /// removed.
214   SmallVector<Value *, 2> ValuesToRelease;
215 
216   StackEntry(unsigned NumIn, unsigned NumOut, bool IsSigned,
217              SmallVector<Value *, 2> ValuesToRelease)
218       : NumIn(NumIn), NumOut(NumOut), IsSigned(IsSigned),
219         ValuesToRelease(ValuesToRelease) {}
220 };
221 
222 struct ConstraintTy {
223   SmallVector<int64_t, 8> Coefficients;
224   SmallVector<ConditionTy, 2> Preconditions;
225 
226   SmallVector<SmallVector<int64_t, 8>> ExtraInfo;
227 
228   bool IsSigned = false;
229 
230   ConstraintTy() = default;
231 
232   ConstraintTy(SmallVector<int64_t, 8> Coefficients, bool IsSigned, bool IsEq,
233                bool IsNe)
234       : Coefficients(Coefficients), IsSigned(IsSigned), IsEq(IsEq), IsNe(IsNe) {
235   }
236 
237   unsigned size() const { return Coefficients.size(); }
238 
239   unsigned empty() const { return Coefficients.empty(); }
240 
241   /// Returns true if all preconditions for this list of constraints are
242   /// satisfied given \p CS and the corresponding \p Value2Index mapping.
243   bool isValid(const ConstraintInfo &Info) const;
244 
245   bool isEq() const { return IsEq; }
246 
247   bool isNe() const { return IsNe; }
248 
249   /// Check if the current constraint is implied by the given ConstraintSystem.
250   ///
251   /// \return true or false if the constraint is proven to be respectively true,
252   /// or false. When the constraint cannot be proven to be either true or false,
253   /// std::nullopt is returned.
254   std::optional<bool> isImpliedBy(const ConstraintSystem &CS) const;
255 
256 private:
257   bool IsEq = false;
258   bool IsNe = false;
259 };
260 
261 /// Wrapper encapsulating separate constraint systems and corresponding value
262 /// mappings for both unsigned and signed information. Facts are added to and
263 /// conditions are checked against the corresponding system depending on the
264 /// signed-ness of their predicates. While the information is kept separate
265 /// based on signed-ness, certain conditions can be transferred between the two
266 /// systems.
267 class ConstraintInfo {
268 
269   ConstraintSystem UnsignedCS;
270   ConstraintSystem SignedCS;
271 
272   const DataLayout &DL;
273 
274 public:
275   ConstraintInfo(const DataLayout &DL, ArrayRef<Value *> FunctionArgs)
276       : UnsignedCS(FunctionArgs), SignedCS(FunctionArgs), DL(DL) {
277     auto &Value2Index = getValue2Index(false);
278     // Add Arg > -1 constraints to unsigned system for all function arguments.
279     for (Value *Arg : FunctionArgs) {
280       ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
281                           false, false, false);
282       VarPos.Coefficients[Value2Index[Arg]] = -1;
283       UnsignedCS.addVariableRow(VarPos.Coefficients);
284     }
285   }
286 
287   DenseMap<Value *, unsigned> &getValue2Index(bool Signed) {
288     return Signed ? SignedCS.getValue2Index() : UnsignedCS.getValue2Index();
289   }
290   const DenseMap<Value *, unsigned> &getValue2Index(bool Signed) const {
291     return Signed ? SignedCS.getValue2Index() : UnsignedCS.getValue2Index();
292   }
293 
294   ConstraintSystem &getCS(bool Signed) {
295     return Signed ? SignedCS : UnsignedCS;
296   }
297   const ConstraintSystem &getCS(bool Signed) const {
298     return Signed ? SignedCS : UnsignedCS;
299   }
300 
301   void popLastConstraint(bool Signed) { getCS(Signed).popLastConstraint(); }
302   void popLastNVariables(bool Signed, unsigned N) {
303     getCS(Signed).popLastNVariables(N);
304   }
305 
306   bool doesHold(CmpInst::Predicate Pred, Value *A, Value *B) const;
307 
308   void addFact(CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
309                unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack);
310 
311   /// Turn a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
312   /// constraints, using indices from the corresponding constraint system.
313   /// New variables that need to be added to the system are collected in
314   /// \p NewVariables.
315   ConstraintTy getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
316                              SmallVectorImpl<Value *> &NewVariables) const;
317 
318   /// Turns a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
319   /// constraints using getConstraint. Returns an empty constraint if the result
320   /// cannot be used to query the existing constraint system, e.g. because it
321   /// would require adding new variables. Also tries to convert signed
322   /// predicates to unsigned ones if possible to allow using the unsigned system
323   /// which increases the effectiveness of the signed <-> unsigned transfer
324   /// logic.
325   ConstraintTy getConstraintForSolving(CmpInst::Predicate Pred, Value *Op0,
326                                        Value *Op1) const;
327 
328   /// Try to add information from \p A \p Pred \p B to the unsigned/signed
329   /// system if \p Pred is signed/unsigned.
330   void transferToOtherSystem(CmpInst::Predicate Pred, Value *A, Value *B,
331                              unsigned NumIn, unsigned NumOut,
332                              SmallVectorImpl<StackEntry> &DFSInStack);
333 };
334 
335 /// Represents a (Coefficient * Variable) entry after IR decomposition.
336 struct DecompEntry {
337   int64_t Coefficient;
338   Value *Variable;
339   /// True if the variable is known positive in the current constraint.
340   bool IsKnownNonNegative;
341 
342   DecompEntry(int64_t Coefficient, Value *Variable,
343               bool IsKnownNonNegative = false)
344       : Coefficient(Coefficient), Variable(Variable),
345         IsKnownNonNegative(IsKnownNonNegative) {}
346 };
347 
348 /// Represents an Offset + Coefficient1 * Variable1 + ... decomposition.
349 struct Decomposition {
350   int64_t Offset = 0;
351   SmallVector<DecompEntry, 3> Vars;
352 
353   Decomposition(int64_t Offset) : Offset(Offset) {}
354   Decomposition(Value *V, bool IsKnownNonNegative = false) {
355     Vars.emplace_back(1, V, IsKnownNonNegative);
356   }
357   Decomposition(int64_t Offset, ArrayRef<DecompEntry> Vars)
358       : Offset(Offset), Vars(Vars) {}
359 
360   void add(int64_t OtherOffset) {
361     Offset = addWithOverflow(Offset, OtherOffset);
362   }
363 
364   void add(const Decomposition &Other) {
365     add(Other.Offset);
366     append_range(Vars, Other.Vars);
367   }
368 
369   void mul(int64_t Factor) {
370     Offset = multiplyWithOverflow(Offset, Factor);
371     for (auto &Var : Vars)
372       Var.Coefficient = multiplyWithOverflow(Var.Coefficient, Factor);
373   }
374 };
375 
376 // Variable and constant offsets for a chain of GEPs, with base pointer BasePtr.
377 struct OffsetResult {
378   Value *BasePtr;
379   APInt ConstantOffset;
380   MapVector<Value *, APInt> VariableOffsets;
381   bool AllInbounds;
382 
383   OffsetResult() : BasePtr(nullptr), ConstantOffset(0, uint64_t(0)) {}
384 
385   OffsetResult(GEPOperator &GEP, const DataLayout &DL)
386       : BasePtr(GEP.getPointerOperand()), AllInbounds(GEP.isInBounds()) {
387     ConstantOffset = APInt(DL.getIndexTypeSizeInBits(BasePtr->getType()), 0);
388   }
389 };
390 } // namespace
391 
392 // Try to collect variable and constant offsets for \p GEP, partly traversing
393 // nested GEPs. Returns an OffsetResult with nullptr as BasePtr of collecting
394 // the offset fails.
395 static OffsetResult collectOffsets(GEPOperator &GEP, const DataLayout &DL) {
396   OffsetResult Result(GEP, DL);
397   unsigned BitWidth = Result.ConstantOffset.getBitWidth();
398   if (!GEP.collectOffset(DL, BitWidth, Result.VariableOffsets,
399                          Result.ConstantOffset))
400     return {};
401 
402   // If we have a nested GEP, check if we can combine the constant offset of the
403   // inner GEP with the outer GEP.
404   if (auto *InnerGEP = dyn_cast<GetElementPtrInst>(Result.BasePtr)) {
405     MapVector<Value *, APInt> VariableOffsets2;
406     APInt ConstantOffset2(BitWidth, 0);
407     bool CanCollectInner = InnerGEP->collectOffset(
408         DL, BitWidth, VariableOffsets2, ConstantOffset2);
409     // TODO: Support cases with more than 1 variable offset.
410     if (!CanCollectInner || Result.VariableOffsets.size() > 1 ||
411         VariableOffsets2.size() > 1 ||
412         (Result.VariableOffsets.size() >= 1 && VariableOffsets2.size() >= 1)) {
413       // More than 1 variable index, use outer result.
414       return Result;
415     }
416     Result.BasePtr = InnerGEP->getPointerOperand();
417     Result.ConstantOffset += ConstantOffset2;
418     if (Result.VariableOffsets.size() == 0 && VariableOffsets2.size() == 1)
419       Result.VariableOffsets = VariableOffsets2;
420     Result.AllInbounds &= InnerGEP->isInBounds();
421   }
422   return Result;
423 }
424 
425 static Decomposition decompose(Value *V,
426                                SmallVectorImpl<ConditionTy> &Preconditions,
427                                bool IsSigned, const DataLayout &DL);
428 
429 static bool canUseSExt(ConstantInt *CI) {
430   const APInt &Val = CI->getValue();
431   return Val.sgt(MinSignedConstraintValue) && Val.slt(MaxConstraintValue);
432 }
433 
434 static Decomposition decomposeGEP(GEPOperator &GEP,
435                                   SmallVectorImpl<ConditionTy> &Preconditions,
436                                   bool IsSigned, const DataLayout &DL) {
437   // Do not reason about pointers where the index size is larger than 64 bits,
438   // as the coefficients used to encode constraints are 64 bit integers.
439   if (DL.getIndexTypeSizeInBits(GEP.getPointerOperand()->getType()) > 64)
440     return &GEP;
441 
442   assert(!IsSigned && "The logic below only supports decomposition for "
443                       "unsigned predicates at the moment.");
444   const auto &[BasePtr, ConstantOffset, VariableOffsets, AllInbounds] =
445       collectOffsets(GEP, DL);
446   if (!BasePtr || !AllInbounds)
447     return &GEP;
448 
449   Decomposition Result(ConstantOffset.getSExtValue(), DecompEntry(1, BasePtr));
450   for (auto [Index, Scale] : VariableOffsets) {
451     auto IdxResult = decompose(Index, Preconditions, IsSigned, DL);
452     IdxResult.mul(Scale.getSExtValue());
453     Result.add(IdxResult);
454 
455     // If Op0 is signed non-negative, the GEP is increasing monotonically and
456     // can be de-composed.
457     if (!isKnownNonNegative(Index, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
458       Preconditions.emplace_back(CmpInst::ICMP_SGE, Index,
459                                  ConstantInt::get(Index->getType(), 0));
460   }
461   return Result;
462 }
463 
464 // Decomposes \p V into a constant offset + list of pairs { Coefficient,
465 // Variable } where Coefficient * Variable. The sum of the constant offset and
466 // pairs equals \p V.
467 static Decomposition decompose(Value *V,
468                                SmallVectorImpl<ConditionTy> &Preconditions,
469                                bool IsSigned, const DataLayout &DL) {
470 
471   auto MergeResults = [&Preconditions, IsSigned, &DL](Value *A, Value *B,
472                                                       bool IsSignedB) {
473     auto ResA = decompose(A, Preconditions, IsSigned, DL);
474     auto ResB = decompose(B, Preconditions, IsSignedB, DL);
475     ResA.add(ResB);
476     return ResA;
477   };
478 
479   Type *Ty = V->getType()->getScalarType();
480   if (Ty->isPointerTy() && !IsSigned) {
481     if (auto *GEP = dyn_cast<GEPOperator>(V))
482       return decomposeGEP(*GEP, Preconditions, IsSigned, DL);
483     if (isa<ConstantPointerNull>(V))
484       return int64_t(0);
485 
486     return V;
487   }
488 
489   // Don't handle integers > 64 bit. Our coefficients are 64-bit large, so
490   // coefficient add/mul may wrap, while the operation in the full bit width
491   // would not.
492   if (!Ty->isIntegerTy() || Ty->getIntegerBitWidth() > 64)
493     return V;
494 
495   // Decompose \p V used with a signed predicate.
496   if (IsSigned) {
497     if (auto *CI = dyn_cast<ConstantInt>(V)) {
498       if (canUseSExt(CI))
499         return CI->getSExtValue();
500     }
501     Value *Op0;
502     Value *Op1;
503     if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1))))
504       return MergeResults(Op0, Op1, IsSigned);
505 
506     ConstantInt *CI;
507     if (match(V, m_NSWMul(m_Value(Op0), m_ConstantInt(CI))) && canUseSExt(CI)) {
508       auto Result = decompose(Op0, Preconditions, IsSigned, DL);
509       Result.mul(CI->getSExtValue());
510       return Result;
511     }
512 
513     return V;
514   }
515 
516   if (auto *CI = dyn_cast<ConstantInt>(V)) {
517     if (CI->uge(MaxConstraintValue))
518       return V;
519     return int64_t(CI->getZExtValue());
520   }
521 
522   Value *Op0;
523   bool IsKnownNonNegative = false;
524   if (match(V, m_ZExt(m_Value(Op0)))) {
525     IsKnownNonNegative = true;
526     V = Op0;
527   }
528 
529   Value *Op1;
530   ConstantInt *CI;
531   if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1)))) {
532     return MergeResults(Op0, Op1, IsSigned);
533   }
534   if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1)))) {
535     if (!isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
536       Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
537                                  ConstantInt::get(Op0->getType(), 0));
538     if (!isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
539       Preconditions.emplace_back(CmpInst::ICMP_SGE, Op1,
540                                  ConstantInt::get(Op1->getType(), 0));
541 
542     return MergeResults(Op0, Op1, IsSigned);
543   }
544 
545   if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
546       canUseSExt(CI)) {
547     Preconditions.emplace_back(
548         CmpInst::ICMP_UGE, Op0,
549         ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
550     return MergeResults(Op0, CI, true);
551   }
552 
553   // Decompose or as an add if there are no common bits between the operands.
554   if (match(V, m_DisjointOr(m_Value(Op0), m_ConstantInt(CI))))
555     return MergeResults(Op0, CI, IsSigned);
556 
557   if (match(V, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI)) {
558     if (CI->getSExtValue() < 0 || CI->getSExtValue() >= 64)
559       return {V, IsKnownNonNegative};
560     auto Result = decompose(Op1, Preconditions, IsSigned, DL);
561     Result.mul(int64_t{1} << CI->getSExtValue());
562     return Result;
563   }
564 
565   if (match(V, m_NUWMul(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI) &&
566       (!CI->isNegative())) {
567     auto Result = decompose(Op1, Preconditions, IsSigned, DL);
568     Result.mul(CI->getSExtValue());
569     return Result;
570   }
571 
572   if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))) && canUseSExt(CI))
573     return {-1 * CI->getSExtValue(), {{1, Op0}}};
574   if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1))))
575     return {0, {{1, Op0}, {-1, Op1}}};
576 
577   return {V, IsKnownNonNegative};
578 }
579 
580 ConstraintTy
581 ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
582                               SmallVectorImpl<Value *> &NewVariables) const {
583   assert(NewVariables.empty() && "NewVariables must be empty when passed in");
584   bool IsEq = false;
585   bool IsNe = false;
586 
587   // Try to convert Pred to one of ULE/SLT/SLE/SLT.
588   switch (Pred) {
589   case CmpInst::ICMP_UGT:
590   case CmpInst::ICMP_UGE:
591   case CmpInst::ICMP_SGT:
592   case CmpInst::ICMP_SGE: {
593     Pred = CmpInst::getSwappedPredicate(Pred);
594     std::swap(Op0, Op1);
595     break;
596   }
597   case CmpInst::ICMP_EQ:
598     if (match(Op1, m_Zero())) {
599       Pred = CmpInst::ICMP_ULE;
600     } else {
601       IsEq = true;
602       Pred = CmpInst::ICMP_ULE;
603     }
604     break;
605   case CmpInst::ICMP_NE:
606     if (match(Op1, m_Zero())) {
607       Pred = CmpInst::getSwappedPredicate(CmpInst::ICMP_UGT);
608       std::swap(Op0, Op1);
609     } else {
610       IsNe = true;
611       Pred = CmpInst::ICMP_ULE;
612     }
613     break;
614   default:
615     break;
616   }
617 
618   if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT &&
619       Pred != CmpInst::ICMP_SLE && Pred != CmpInst::ICMP_SLT)
620     return {};
621 
622   SmallVector<ConditionTy, 4> Preconditions;
623   bool IsSigned = CmpInst::isSigned(Pred);
624   auto &Value2Index = getValue2Index(IsSigned);
625   auto ADec = decompose(Op0->stripPointerCastsSameRepresentation(),
626                         Preconditions, IsSigned, DL);
627   auto BDec = decompose(Op1->stripPointerCastsSameRepresentation(),
628                         Preconditions, IsSigned, DL);
629   int64_t Offset1 = ADec.Offset;
630   int64_t Offset2 = BDec.Offset;
631   Offset1 *= -1;
632 
633   auto &VariablesA = ADec.Vars;
634   auto &VariablesB = BDec.Vars;
635 
636   // First try to look up \p V in Value2Index and NewVariables. Otherwise add a
637   // new entry to NewVariables.
638   DenseMap<Value *, unsigned> NewIndexMap;
639   auto GetOrAddIndex = [&Value2Index, &NewVariables,
640                         &NewIndexMap](Value *V) -> unsigned {
641     auto V2I = Value2Index.find(V);
642     if (V2I != Value2Index.end())
643       return V2I->second;
644     auto Insert =
645         NewIndexMap.insert({V, Value2Index.size() + NewVariables.size() + 1});
646     if (Insert.second)
647       NewVariables.push_back(V);
648     return Insert.first->second;
649   };
650 
651   // Make sure all variables have entries in Value2Index or NewVariables.
652   for (const auto &KV : concat<DecompEntry>(VariablesA, VariablesB))
653     GetOrAddIndex(KV.Variable);
654 
655   // Build result constraint, by first adding all coefficients from A and then
656   // subtracting all coefficients from B.
657   ConstraintTy Res(
658       SmallVector<int64_t, 8>(Value2Index.size() + NewVariables.size() + 1, 0),
659       IsSigned, IsEq, IsNe);
660   // Collect variables that are known to be positive in all uses in the
661   // constraint.
662   DenseMap<Value *, bool> KnownNonNegativeVariables;
663   auto &R = Res.Coefficients;
664   for (const auto &KV : VariablesA) {
665     R[GetOrAddIndex(KV.Variable)] += KV.Coefficient;
666     auto I =
667         KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
668     I.first->second &= KV.IsKnownNonNegative;
669   }
670 
671   for (const auto &KV : VariablesB) {
672     if (SubOverflow(R[GetOrAddIndex(KV.Variable)], KV.Coefficient,
673                     R[GetOrAddIndex(KV.Variable)]))
674       return {};
675     auto I =
676         KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
677     I.first->second &= KV.IsKnownNonNegative;
678   }
679 
680   int64_t OffsetSum;
681   if (AddOverflow(Offset1, Offset2, OffsetSum))
682     return {};
683   if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT))
684     if (AddOverflow(OffsetSum, int64_t(-1), OffsetSum))
685       return {};
686   R[0] = OffsetSum;
687   Res.Preconditions = std::move(Preconditions);
688 
689   // Remove any (Coefficient, Variable) entry where the Coefficient is 0 for new
690   // variables.
691   while (!NewVariables.empty()) {
692     int64_t Last = R.back();
693     if (Last != 0)
694       break;
695     R.pop_back();
696     Value *RemovedV = NewVariables.pop_back_val();
697     NewIndexMap.erase(RemovedV);
698   }
699 
700   // Add extra constraints for variables that are known positive.
701   for (auto &KV : KnownNonNegativeVariables) {
702     if (!KV.second ||
703         (!Value2Index.contains(KV.first) && !NewIndexMap.contains(KV.first)))
704       continue;
705     SmallVector<int64_t, 8> C(Value2Index.size() + NewVariables.size() + 1, 0);
706     C[GetOrAddIndex(KV.first)] = -1;
707     Res.ExtraInfo.push_back(C);
708   }
709   return Res;
710 }
711 
712 ConstraintTy ConstraintInfo::getConstraintForSolving(CmpInst::Predicate Pred,
713                                                      Value *Op0,
714                                                      Value *Op1) const {
715   Constant *NullC = Constant::getNullValue(Op0->getType());
716   // Handle trivially true compares directly to avoid adding V UGE 0 constraints
717   // for all variables in the unsigned system.
718   if ((Pred == CmpInst::ICMP_ULE && Op0 == NullC) ||
719       (Pred == CmpInst::ICMP_UGE && Op1 == NullC)) {
720     auto &Value2Index = getValue2Index(false);
721     // Return constraint that's trivially true.
722     return ConstraintTy(SmallVector<int64_t, 8>(Value2Index.size(), 0), false,
723                         false, false);
724   }
725 
726   // If both operands are known to be non-negative, change signed predicates to
727   // unsigned ones. This increases the reasoning effectiveness in combination
728   // with the signed <-> unsigned transfer logic.
729   if (CmpInst::isSigned(Pred) &&
730       isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1) &&
731       isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
732     Pred = CmpInst::getUnsignedPredicate(Pred);
733 
734   SmallVector<Value *> NewVariables;
735   ConstraintTy R = getConstraint(Pred, Op0, Op1, NewVariables);
736   if (!NewVariables.empty())
737     return {};
738   return R;
739 }
740 
741 bool ConstraintTy::isValid(const ConstraintInfo &Info) const {
742   return Coefficients.size() > 0 &&
743          all_of(Preconditions, [&Info](const ConditionTy &C) {
744            return Info.doesHold(C.Pred, C.Op0, C.Op1);
745          });
746 }
747 
748 std::optional<bool>
749 ConstraintTy::isImpliedBy(const ConstraintSystem &CS) const {
750   bool IsConditionImplied = CS.isConditionImplied(Coefficients);
751 
752   if (IsEq || IsNe) {
753     auto NegatedOrEqual = ConstraintSystem::negateOrEqual(Coefficients);
754     bool IsNegatedOrEqualImplied =
755         !NegatedOrEqual.empty() && CS.isConditionImplied(NegatedOrEqual);
756 
757     // In order to check that `%a == %b` is true (equality), both conditions `%a
758     // >= %b` and `%a <= %b` must hold true. When checking for equality (`IsEq`
759     // is true), we return true if they both hold, false in the other cases.
760     if (IsConditionImplied && IsNegatedOrEqualImplied)
761       return IsEq;
762 
763     auto Negated = ConstraintSystem::negate(Coefficients);
764     bool IsNegatedImplied = !Negated.empty() && CS.isConditionImplied(Negated);
765 
766     auto StrictLessThan = ConstraintSystem::toStrictLessThan(Coefficients);
767     bool IsStrictLessThanImplied =
768         !StrictLessThan.empty() && CS.isConditionImplied(StrictLessThan);
769 
770     // In order to check that `%a != %b` is true (non-equality), either
771     // condition `%a > %b` or `%a < %b` must hold true. When checking for
772     // non-equality (`IsNe` is true), we return true if one of the two holds,
773     // false in the other cases.
774     if (IsNegatedImplied || IsStrictLessThanImplied)
775       return IsNe;
776 
777     return std::nullopt;
778   }
779 
780   if (IsConditionImplied)
781     return true;
782 
783   auto Negated = ConstraintSystem::negate(Coefficients);
784   auto IsNegatedImplied = !Negated.empty() && CS.isConditionImplied(Negated);
785   if (IsNegatedImplied)
786     return false;
787 
788   // Neither the condition nor its negated holds, did not prove anything.
789   return std::nullopt;
790 }
791 
792 bool ConstraintInfo::doesHold(CmpInst::Predicate Pred, Value *A,
793                               Value *B) const {
794   auto R = getConstraintForSolving(Pred, A, B);
795   return R.isValid(*this) &&
796          getCS(R.IsSigned).isConditionImplied(R.Coefficients);
797 }
798 
799 void ConstraintInfo::transferToOtherSystem(
800     CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
801     unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack) {
802   auto IsKnownNonNegative = [this](Value *V) {
803     return doesHold(CmpInst::ICMP_SGE, V, ConstantInt::get(V->getType(), 0)) ||
804            isKnownNonNegative(V, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1);
805   };
806   // Check if we can combine facts from the signed and unsigned systems to
807   // derive additional facts.
808   if (!A->getType()->isIntegerTy())
809     return;
810   // FIXME: This currently depends on the order we add facts. Ideally we
811   // would first add all known facts and only then try to add additional
812   // facts.
813   switch (Pred) {
814   default:
815     break;
816   case CmpInst::ICMP_ULT:
817   case CmpInst::ICMP_ULE:
818     //  If B is a signed positive constant, then A >=s 0 and A <s (or <=s) B.
819     if (IsKnownNonNegative(B)) {
820       addFact(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0), NumIn,
821               NumOut, DFSInStack);
822       addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
823               DFSInStack);
824     }
825     break;
826   case CmpInst::ICMP_UGE:
827   case CmpInst::ICMP_UGT:
828     //  If A is a signed positive constant, then B >=s 0 and A >s (or >=s) B.
829     if (IsKnownNonNegative(A)) {
830       addFact(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0), NumIn,
831               NumOut, DFSInStack);
832       addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
833               DFSInStack);
834     }
835     break;
836   case CmpInst::ICMP_SLT:
837     if (IsKnownNonNegative(A))
838       addFact(CmpInst::ICMP_ULT, A, B, NumIn, NumOut, DFSInStack);
839     break;
840   case CmpInst::ICMP_SGT: {
841     if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), -1)))
842       addFact(CmpInst::ICMP_UGE, A, ConstantInt::get(B->getType(), 0), NumIn,
843               NumOut, DFSInStack);
844     if (IsKnownNonNegative(B))
845       addFact(CmpInst::ICMP_UGT, A, B, NumIn, NumOut, DFSInStack);
846 
847     break;
848   }
849   case CmpInst::ICMP_SGE:
850     if (IsKnownNonNegative(B))
851       addFact(CmpInst::ICMP_UGE, A, B, NumIn, NumOut, DFSInStack);
852     break;
853   }
854 }
855 
856 #ifndef NDEBUG
857 
858 static void dumpConstraint(ArrayRef<int64_t> C,
859                            const DenseMap<Value *, unsigned> &Value2Index) {
860   ConstraintSystem CS(Value2Index);
861   CS.addVariableRowFill(C);
862   CS.dump();
863 }
864 #endif
865 
866 void State::addInfoForInductions(BasicBlock &BB) {
867   auto *L = LI.getLoopFor(&BB);
868   if (!L || L->getHeader() != &BB)
869     return;
870 
871   Value *A;
872   Value *B;
873   CmpInst::Predicate Pred;
874 
875   if (!match(BB.getTerminator(),
876              m_Br(m_ICmp(Pred, m_Value(A), m_Value(B)), m_Value(), m_Value())))
877     return;
878   PHINode *PN = dyn_cast<PHINode>(A);
879   if (!PN) {
880     Pred = CmpInst::getSwappedPredicate(Pred);
881     std::swap(A, B);
882     PN = dyn_cast<PHINode>(A);
883   }
884 
885   if (!PN || PN->getParent() != &BB || PN->getNumIncomingValues() != 2 ||
886       !SE.isSCEVable(PN->getType()))
887     return;
888 
889   BasicBlock *InLoopSucc = nullptr;
890   if (Pred == CmpInst::ICMP_NE)
891     InLoopSucc = cast<BranchInst>(BB.getTerminator())->getSuccessor(0);
892   else if (Pred == CmpInst::ICMP_EQ)
893     InLoopSucc = cast<BranchInst>(BB.getTerminator())->getSuccessor(1);
894   else
895     return;
896 
897   if (!L->contains(InLoopSucc) || !L->isLoopExiting(&BB) || InLoopSucc == &BB)
898     return;
899 
900   auto *AR = dyn_cast_or_null<SCEVAddRecExpr>(SE.getSCEV(PN));
901   BasicBlock *LoopPred = L->getLoopPredecessor();
902   if (!AR || AR->getLoop() != L || !LoopPred)
903     return;
904 
905   const SCEV *StartSCEV = AR->getStart();
906   Value *StartValue = nullptr;
907   if (auto *C = dyn_cast<SCEVConstant>(StartSCEV)) {
908     StartValue = C->getValue();
909   } else {
910     StartValue = PN->getIncomingValueForBlock(LoopPred);
911     assert(SE.getSCEV(StartValue) == StartSCEV && "inconsistent start value");
912   }
913 
914   DomTreeNode *DTN = DT.getNode(InLoopSucc);
915   auto Inc = SE.getMonotonicPredicateType(AR, CmpInst::ICMP_UGT);
916   bool MonotonicallyIncreasing =
917       Inc && *Inc == ScalarEvolution::MonotonicallyIncreasing;
918   if (MonotonicallyIncreasing) {
919     // SCEV guarantees that AR does not wrap, so PN >= StartValue can be added
920     // unconditionally.
921     WorkList.push_back(
922         FactOrCheck::getConditionFact(DTN, CmpInst::ICMP_UGE, PN, StartValue));
923   }
924 
925   APInt StepOffset;
926   if (auto *C = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE)))
927     StepOffset = C->getAPInt();
928   else
929     return;
930 
931   // Make sure the bound B is loop-invariant.
932   if (!L->isLoopInvariant(B))
933     return;
934 
935   // Handle negative steps.
936   if (StepOffset.isNegative()) {
937     // TODO: Extend to allow steps > -1.
938     if (!(-StepOffset).isOne())
939       return;
940 
941     // AR may wrap.
942     // Add StartValue >= PN conditional on B <= StartValue which guarantees that
943     // the loop exits before wrapping with a step of -1.
944     WorkList.push_back(FactOrCheck::getConditionFact(
945         DTN, CmpInst::ICMP_UGE, StartValue, PN,
946         ConditionTy(CmpInst::ICMP_ULE, B, StartValue)));
947     // Add PN > B conditional on B <= StartValue which guarantees that the loop
948     // exits when reaching B with a step of -1.
949     WorkList.push_back(FactOrCheck::getConditionFact(
950         DTN, CmpInst::ICMP_UGT, PN, B,
951         ConditionTy(CmpInst::ICMP_ULE, B, StartValue)));
952     return;
953   }
954 
955   // Make sure AR either steps by 1 or that the value we compare against is a
956   // GEP based on the same start value and all offsets are a multiple of the
957   // step size, to guarantee that the induction will reach the value.
958   if (StepOffset.isZero() || StepOffset.isNegative())
959     return;
960 
961   if (!StepOffset.isOne()) {
962     auto *UpperGEP = dyn_cast<GetElementPtrInst>(B);
963     if (!UpperGEP || UpperGEP->getPointerOperand() != StartValue ||
964         !UpperGEP->isInBounds())
965       return;
966 
967     MapVector<Value *, APInt> UpperVariableOffsets;
968     APInt UpperConstantOffset(StepOffset.getBitWidth(), 0);
969     const DataLayout &DL = BB.getModule()->getDataLayout();
970     if (!UpperGEP->collectOffset(DL, StepOffset.getBitWidth(),
971                                  UpperVariableOffsets, UpperConstantOffset))
972       return;
973     // All variable offsets and the constant offset have to be a multiple of the
974     // step.
975     if (!UpperConstantOffset.urem(StepOffset).isZero() ||
976         any_of(UpperVariableOffsets, [&StepOffset](const auto &P) {
977           return !P.second.urem(StepOffset).isZero();
978         }))
979       return;
980   }
981 
982   // AR may wrap. Add PN >= StartValue conditional on StartValue <= B which
983   // guarantees that the loop exits before wrapping in combination with the
984   // restrictions on B and the step above.
985   if (!MonotonicallyIncreasing) {
986     WorkList.push_back(FactOrCheck::getConditionFact(
987         DTN, CmpInst::ICMP_UGE, PN, StartValue,
988         ConditionTy(CmpInst::ICMP_ULE, StartValue, B)));
989   }
990   WorkList.push_back(FactOrCheck::getConditionFact(
991       DTN, CmpInst::ICMP_ULT, PN, B,
992       ConditionTy(CmpInst::ICMP_ULE, StartValue, B)));
993 }
994 
995 void State::addInfoFor(BasicBlock &BB) {
996   addInfoForInductions(BB);
997 
998   // True as long as long as the current instruction is guaranteed to execute.
999   bool GuaranteedToExecute = true;
1000   // Queue conditions and assumes.
1001   for (Instruction &I : BB) {
1002     if (auto Cmp = dyn_cast<ICmpInst>(&I)) {
1003       for (Use &U : Cmp->uses()) {
1004         auto *UserI = getContextInstForUse(U);
1005         auto *DTN = DT.getNode(UserI->getParent());
1006         if (!DTN)
1007           continue;
1008         WorkList.push_back(FactOrCheck::getCheck(DTN, &U));
1009       }
1010       continue;
1011     }
1012 
1013     auto *II = dyn_cast<IntrinsicInst>(&I);
1014     Intrinsic::ID ID = II ? II->getIntrinsicID() : Intrinsic::not_intrinsic;
1015     switch (ID) {
1016     case Intrinsic::assume: {
1017       Value *A, *B;
1018       CmpInst::Predicate Pred;
1019       if (!match(I.getOperand(0), m_ICmp(Pred, m_Value(A), m_Value(B))))
1020         break;
1021       if (GuaranteedToExecute) {
1022         // The assume is guaranteed to execute when BB is entered, hence Cond
1023         // holds on entry to BB.
1024         WorkList.emplace_back(FactOrCheck::getConditionFact(
1025             DT.getNode(I.getParent()), Pred, A, B));
1026       } else {
1027         WorkList.emplace_back(
1028             FactOrCheck::getInstFact(DT.getNode(I.getParent()), &I));
1029       }
1030       break;
1031     }
1032     // Enqueue ssub_with_overflow for simplification.
1033     case Intrinsic::ssub_with_overflow:
1034       WorkList.push_back(
1035           FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
1036       break;
1037     // Enqueue the intrinsics to add extra info.
1038     case Intrinsic::abs:
1039     case Intrinsic::umin:
1040     case Intrinsic::umax:
1041     case Intrinsic::smin:
1042     case Intrinsic::smax:
1043       WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
1044       break;
1045     }
1046 
1047     GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
1048   }
1049 
1050   if (auto *Switch = dyn_cast<SwitchInst>(BB.getTerminator())) {
1051     for (auto &Case : Switch->cases()) {
1052       BasicBlock *Succ = Case.getCaseSuccessor();
1053       Value *V = Case.getCaseValue();
1054       if (!canAddSuccessor(BB, Succ))
1055         continue;
1056       WorkList.emplace_back(FactOrCheck::getConditionFact(
1057           DT.getNode(Succ), CmpInst::ICMP_EQ, Switch->getCondition(), V));
1058     }
1059     return;
1060   }
1061 
1062   auto *Br = dyn_cast<BranchInst>(BB.getTerminator());
1063   if (!Br || !Br->isConditional())
1064     return;
1065 
1066   Value *Cond = Br->getCondition();
1067 
1068   // If the condition is a chain of ORs/AND and the successor only has the
1069   // current block as predecessor, queue conditions for the successor.
1070   Value *Op0, *Op1;
1071   if (match(Cond, m_LogicalOr(m_Value(Op0), m_Value(Op1))) ||
1072       match(Cond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
1073     bool IsOr = match(Cond, m_LogicalOr());
1074     bool IsAnd = match(Cond, m_LogicalAnd());
1075     // If there's a select that matches both AND and OR, we need to commit to
1076     // one of the options. Arbitrarily pick OR.
1077     if (IsOr && IsAnd)
1078       IsAnd = false;
1079 
1080     BasicBlock *Successor = Br->getSuccessor(IsOr ? 1 : 0);
1081     if (canAddSuccessor(BB, Successor)) {
1082       SmallVector<Value *> CondWorkList;
1083       SmallPtrSet<Value *, 8> SeenCond;
1084       auto QueueValue = [&CondWorkList, &SeenCond](Value *V) {
1085         if (SeenCond.insert(V).second)
1086           CondWorkList.push_back(V);
1087       };
1088       QueueValue(Op1);
1089       QueueValue(Op0);
1090       while (!CondWorkList.empty()) {
1091         Value *Cur = CondWorkList.pop_back_val();
1092         if (auto *Cmp = dyn_cast<ICmpInst>(Cur)) {
1093           WorkList.emplace_back(FactOrCheck::getConditionFact(
1094               DT.getNode(Successor),
1095               IsOr ? CmpInst::getInversePredicate(Cmp->getPredicate())
1096                    : Cmp->getPredicate(),
1097               Cmp->getOperand(0), Cmp->getOperand(1)));
1098           continue;
1099         }
1100         if (IsOr && match(Cur, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) {
1101           QueueValue(Op1);
1102           QueueValue(Op0);
1103           continue;
1104         }
1105         if (IsAnd && match(Cur, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
1106           QueueValue(Op1);
1107           QueueValue(Op0);
1108           continue;
1109         }
1110       }
1111     }
1112     return;
1113   }
1114 
1115   auto *CmpI = dyn_cast<ICmpInst>(Br->getCondition());
1116   if (!CmpI)
1117     return;
1118   if (canAddSuccessor(BB, Br->getSuccessor(0)))
1119     WorkList.emplace_back(FactOrCheck::getConditionFact(
1120         DT.getNode(Br->getSuccessor(0)), CmpI->getPredicate(),
1121         CmpI->getOperand(0), CmpI->getOperand(1)));
1122   if (canAddSuccessor(BB, Br->getSuccessor(1)))
1123     WorkList.emplace_back(FactOrCheck::getConditionFact(
1124         DT.getNode(Br->getSuccessor(1)),
1125         CmpInst::getInversePredicate(CmpI->getPredicate()), CmpI->getOperand(0),
1126         CmpI->getOperand(1)));
1127 }
1128 
1129 #ifndef NDEBUG
1130 static void dumpUnpackedICmp(raw_ostream &OS, ICmpInst::Predicate Pred,
1131                              Value *LHS, Value *RHS) {
1132   OS << "icmp " << Pred << ' ';
1133   LHS->printAsOperand(OS, /*PrintType=*/true);
1134   OS << ", ";
1135   RHS->printAsOperand(OS, /*PrintType=*/false);
1136 }
1137 #endif
1138 
1139 namespace {
1140 /// Helper to keep track of a condition and if it should be treated as negated
1141 /// for reproducer construction.
1142 /// Pred == Predicate::BAD_ICMP_PREDICATE indicates that this entry is a
1143 /// placeholder to keep the ReproducerCondStack in sync with DFSInStack.
1144 struct ReproducerEntry {
1145   ICmpInst::Predicate Pred;
1146   Value *LHS;
1147   Value *RHS;
1148 
1149   ReproducerEntry(ICmpInst::Predicate Pred, Value *LHS, Value *RHS)
1150       : Pred(Pred), LHS(LHS), RHS(RHS) {}
1151 };
1152 } // namespace
1153 
1154 /// Helper function to generate a reproducer function for simplifying \p Cond.
1155 /// The reproducer function contains a series of @llvm.assume calls, one for
1156 /// each condition in \p Stack. For each condition, the operand instruction are
1157 /// cloned until we reach operands that have an entry in \p Value2Index. Those
1158 /// will then be added as function arguments. \p DT is used to order cloned
1159 /// instructions. The reproducer function will get added to \p M, if it is
1160 /// non-null. Otherwise no reproducer function is generated.
1161 static void generateReproducer(CmpInst *Cond, Module *M,
1162                                ArrayRef<ReproducerEntry> Stack,
1163                                ConstraintInfo &Info, DominatorTree &DT) {
1164   if (!M)
1165     return;
1166 
1167   LLVMContext &Ctx = Cond->getContext();
1168 
1169   LLVM_DEBUG(dbgs() << "Creating reproducer for " << *Cond << "\n");
1170 
1171   ValueToValueMapTy Old2New;
1172   SmallVector<Value *> Args;
1173   SmallPtrSet<Value *, 8> Seen;
1174   // Traverse Cond and its operands recursively until we reach a value that's in
1175   // Value2Index or not an instruction, or not a operation that
1176   // ConstraintElimination can decompose. Such values will be considered as
1177   // external inputs to the reproducer, they are collected and added as function
1178   // arguments later.
1179   auto CollectArguments = [&](ArrayRef<Value *> Ops, bool IsSigned) {
1180     auto &Value2Index = Info.getValue2Index(IsSigned);
1181     SmallVector<Value *, 4> WorkList(Ops);
1182     while (!WorkList.empty()) {
1183       Value *V = WorkList.pop_back_val();
1184       if (!Seen.insert(V).second)
1185         continue;
1186       if (Old2New.find(V) != Old2New.end())
1187         continue;
1188       if (isa<Constant>(V))
1189         continue;
1190 
1191       auto *I = dyn_cast<Instruction>(V);
1192       if (Value2Index.contains(V) || !I ||
1193           !isa<CmpInst, BinaryOperator, GEPOperator, CastInst>(V)) {
1194         Old2New[V] = V;
1195         Args.push_back(V);
1196         LLVM_DEBUG(dbgs() << "  found external input " << *V << "\n");
1197       } else {
1198         append_range(WorkList, I->operands());
1199       }
1200     }
1201   };
1202 
1203   for (auto &Entry : Stack)
1204     if (Entry.Pred != ICmpInst::BAD_ICMP_PREDICATE)
1205       CollectArguments({Entry.LHS, Entry.RHS}, ICmpInst::isSigned(Entry.Pred));
1206   CollectArguments(Cond, ICmpInst::isSigned(Cond->getPredicate()));
1207 
1208   SmallVector<Type *> ParamTys;
1209   for (auto *P : Args)
1210     ParamTys.push_back(P->getType());
1211 
1212   FunctionType *FTy = FunctionType::get(Cond->getType(), ParamTys,
1213                                         /*isVarArg=*/false);
1214   Function *F = Function::Create(FTy, Function::ExternalLinkage,
1215                                  Cond->getModule()->getName() +
1216                                      Cond->getFunction()->getName() + "repro",
1217                                  M);
1218   // Add arguments to the reproducer function for each external value collected.
1219   for (unsigned I = 0; I < Args.size(); ++I) {
1220     F->getArg(I)->setName(Args[I]->getName());
1221     Old2New[Args[I]] = F->getArg(I);
1222   }
1223 
1224   BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);
1225   IRBuilder<> Builder(Entry);
1226   Builder.CreateRet(Builder.getTrue());
1227   Builder.SetInsertPoint(Entry->getTerminator());
1228 
1229   // Clone instructions in \p Ops and their operands recursively until reaching
1230   // an value in Value2Index (external input to the reproducer). Update Old2New
1231   // mapping for the original and cloned instructions. Sort instructions to
1232   // clone by dominance, then insert the cloned instructions in the function.
1233   auto CloneInstructions = [&](ArrayRef<Value *> Ops, bool IsSigned) {
1234     SmallVector<Value *, 4> WorkList(Ops);
1235     SmallVector<Instruction *> ToClone;
1236     auto &Value2Index = Info.getValue2Index(IsSigned);
1237     while (!WorkList.empty()) {
1238       Value *V = WorkList.pop_back_val();
1239       if (Old2New.find(V) != Old2New.end())
1240         continue;
1241 
1242       auto *I = dyn_cast<Instruction>(V);
1243       if (!Value2Index.contains(V) && I) {
1244         Old2New[V] = nullptr;
1245         ToClone.push_back(I);
1246         append_range(WorkList, I->operands());
1247       }
1248     }
1249 
1250     sort(ToClone,
1251          [&DT](Instruction *A, Instruction *B) { return DT.dominates(A, B); });
1252     for (Instruction *I : ToClone) {
1253       Instruction *Cloned = I->clone();
1254       Old2New[I] = Cloned;
1255       Old2New[I]->setName(I->getName());
1256       Cloned->insertBefore(&*Builder.GetInsertPoint());
1257       Cloned->dropUnknownNonDebugMetadata();
1258       Cloned->setDebugLoc({});
1259     }
1260   };
1261 
1262   // Materialize the assumptions for the reproducer using the entries in Stack.
1263   // That is, first clone the operands of the condition recursively until we
1264   // reach an external input to the reproducer and add them to the reproducer
1265   // function. Then add an ICmp for the condition (with the inverse predicate if
1266   // the entry is negated) and an assert using the ICmp.
1267   for (auto &Entry : Stack) {
1268     if (Entry.Pred == ICmpInst::BAD_ICMP_PREDICATE)
1269       continue;
1270 
1271     LLVM_DEBUG(dbgs() << "  Materializing assumption ";
1272                dumpUnpackedICmp(dbgs(), Entry.Pred, Entry.LHS, Entry.RHS);
1273                dbgs() << "\n");
1274     CloneInstructions({Entry.LHS, Entry.RHS}, CmpInst::isSigned(Entry.Pred));
1275 
1276     auto *Cmp = Builder.CreateICmp(Entry.Pred, Entry.LHS, Entry.RHS);
1277     Builder.CreateAssumption(Cmp);
1278   }
1279 
1280   // Finally, clone the condition to reproduce and remap instruction operands in
1281   // the reproducer using Old2New.
1282   CloneInstructions(Cond, CmpInst::isSigned(Cond->getPredicate()));
1283   Entry->getTerminator()->setOperand(0, Cond);
1284   remapInstructionsInBlocks({Entry}, Old2New);
1285 
1286   assert(!verifyFunction(*F, &dbgs()));
1287 }
1288 
1289 static std::optional<bool> checkCondition(CmpInst::Predicate Pred, Value *A,
1290                                           Value *B, Instruction *CheckInst,
1291                                           ConstraintInfo &Info, unsigned NumIn,
1292                                           unsigned NumOut,
1293                                           Instruction *ContextInst) {
1294   LLVM_DEBUG(dbgs() << "Checking " << *CheckInst << "\n");
1295 
1296   auto R = Info.getConstraintForSolving(Pred, A, B);
1297   if (R.empty() || !R.isValid(Info)){
1298     LLVM_DEBUG(dbgs() << "   failed to decompose condition\n");
1299     return std::nullopt;
1300   }
1301 
1302   auto &CSToUse = Info.getCS(R.IsSigned);
1303 
1304   // If there was extra information collected during decomposition, apply
1305   // it now and remove it immediately once we are done with reasoning
1306   // about the constraint.
1307   for (auto &Row : R.ExtraInfo)
1308     CSToUse.addVariableRow(Row);
1309   auto InfoRestorer = make_scope_exit([&]() {
1310     for (unsigned I = 0; I < R.ExtraInfo.size(); ++I)
1311       CSToUse.popLastConstraint();
1312   });
1313 
1314   if (auto ImpliedCondition = R.isImpliedBy(CSToUse)) {
1315     if (!DebugCounter::shouldExecute(EliminatedCounter))
1316       return std::nullopt;
1317 
1318     LLVM_DEBUG({
1319       dbgs() << "Condition ";
1320       dumpUnpackedICmp(
1321           dbgs(), *ImpliedCondition ? Pred : CmpInst::getInversePredicate(Pred),
1322           A, B);
1323       dbgs() << " implied by dominating constraints\n";
1324       CSToUse.dump();
1325     });
1326     return ImpliedCondition;
1327   }
1328 
1329   return std::nullopt;
1330 }
1331 
1332 static bool checkAndReplaceCondition(
1333     CmpInst *Cmp, ConstraintInfo &Info, unsigned NumIn, unsigned NumOut,
1334     Instruction *ContextInst, Module *ReproducerModule,
1335     ArrayRef<ReproducerEntry> ReproducerCondStack, DominatorTree &DT,
1336     SmallVectorImpl<Instruction *> &ToRemove) {
1337   auto ReplaceCmpWithConstant = [&](CmpInst *Cmp, bool IsTrue) {
1338     generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT);
1339     Constant *ConstantC = ConstantInt::getBool(
1340         CmpInst::makeCmpResultType(Cmp->getType()), IsTrue);
1341     Cmp->replaceUsesWithIf(ConstantC, [&DT, NumIn, NumOut,
1342                                        ContextInst](Use &U) {
1343       auto *UserI = getContextInstForUse(U);
1344       auto *DTN = DT.getNode(UserI->getParent());
1345       if (!DTN || DTN->getDFSNumIn() < NumIn || DTN->getDFSNumOut() > NumOut)
1346         return false;
1347       if (UserI->getParent() == ContextInst->getParent() &&
1348           UserI->comesBefore(ContextInst))
1349         return false;
1350 
1351       // Conditions in an assume trivially simplify to true. Skip uses
1352       // in assume calls to not destroy the available information.
1353       auto *II = dyn_cast<IntrinsicInst>(U.getUser());
1354       return !II || II->getIntrinsicID() != Intrinsic::assume;
1355     });
1356     NumCondsRemoved++;
1357     if (Cmp->use_empty())
1358       ToRemove.push_back(Cmp);
1359     return true;
1360   };
1361 
1362   if (auto ImpliedCondition = checkCondition(
1363           Cmp->getPredicate(), Cmp->getOperand(0), Cmp->getOperand(1), Cmp,
1364           Info, NumIn, NumOut, ContextInst))
1365     return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
1366   return false;
1367 }
1368 
1369 static void
1370 removeEntryFromStack(const StackEntry &E, ConstraintInfo &Info,
1371                      Module *ReproducerModule,
1372                      SmallVectorImpl<ReproducerEntry> &ReproducerCondStack,
1373                      SmallVectorImpl<StackEntry> &DFSInStack) {
1374   Info.popLastConstraint(E.IsSigned);
1375   // Remove variables in the system that went out of scope.
1376   auto &Mapping = Info.getValue2Index(E.IsSigned);
1377   for (Value *V : E.ValuesToRelease)
1378     Mapping.erase(V);
1379   Info.popLastNVariables(E.IsSigned, E.ValuesToRelease.size());
1380   DFSInStack.pop_back();
1381   if (ReproducerModule)
1382     ReproducerCondStack.pop_back();
1383 }
1384 
1385 /// Check if either the first condition of an AND or OR is implied by the
1386 /// (negated in case of OR) second condition or vice versa.
1387 static bool checkOrAndOpImpliedByOther(
1388     FactOrCheck &CB, ConstraintInfo &Info, Module *ReproducerModule,
1389     SmallVectorImpl<ReproducerEntry> &ReproducerCondStack,
1390     SmallVectorImpl<StackEntry> &DFSInStack) {
1391 
1392   CmpInst::Predicate Pred;
1393   Value *A, *B;
1394   Instruction *JoinOp = CB.getContextInst();
1395   CmpInst *CmpToCheck = cast<CmpInst>(CB.getInstructionToSimplify());
1396   unsigned OtherOpIdx = JoinOp->getOperand(0) == CmpToCheck ? 1 : 0;
1397 
1398   // Don't try to simplify the first condition of a select by the second, as
1399   // this may make the select more poisonous than the original one.
1400   // TODO: check if the first operand may be poison.
1401   if (OtherOpIdx != 0 && isa<SelectInst>(JoinOp))
1402     return false;
1403 
1404   if (!match(JoinOp->getOperand(OtherOpIdx),
1405              m_ICmp(Pred, m_Value(A), m_Value(B))))
1406     return false;
1407 
1408   // For OR, check if the negated condition implies CmpToCheck.
1409   bool IsOr = match(JoinOp, m_LogicalOr());
1410   if (IsOr)
1411     Pred = CmpInst::getInversePredicate(Pred);
1412 
1413   // Optimistically add fact from first condition.
1414   unsigned OldSize = DFSInStack.size();
1415   Info.addFact(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
1416   if (OldSize == DFSInStack.size())
1417     return false;
1418 
1419   bool Changed = false;
1420   // Check if the second condition can be simplified now.
1421   if (auto ImpliedCondition =
1422           checkCondition(CmpToCheck->getPredicate(), CmpToCheck->getOperand(0),
1423                          CmpToCheck->getOperand(1), CmpToCheck, Info, CB.NumIn,
1424                          CB.NumOut, CB.getContextInst())) {
1425     if (IsOr && isa<SelectInst>(JoinOp)) {
1426       JoinOp->setOperand(
1427           OtherOpIdx == 0 ? 2 : 0,
1428           ConstantInt::getBool(JoinOp->getType(), *ImpliedCondition));
1429     } else
1430       JoinOp->setOperand(
1431           1 - OtherOpIdx,
1432           ConstantInt::getBool(JoinOp->getType(), *ImpliedCondition));
1433 
1434     Changed = true;
1435   }
1436 
1437   // Remove entries again.
1438   while (OldSize < DFSInStack.size()) {
1439     StackEntry E = DFSInStack.back();
1440     removeEntryFromStack(E, Info, ReproducerModule, ReproducerCondStack,
1441                          DFSInStack);
1442   }
1443   return Changed;
1444 }
1445 
1446 void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
1447                              unsigned NumIn, unsigned NumOut,
1448                              SmallVectorImpl<StackEntry> &DFSInStack) {
1449   // If the constraint has a pre-condition, skip the constraint if it does not
1450   // hold.
1451   SmallVector<Value *> NewVariables;
1452   auto R = getConstraint(Pred, A, B, NewVariables);
1453 
1454   // TODO: Support non-equality for facts as well.
1455   if (!R.isValid(*this) || R.isNe())
1456     return;
1457 
1458   LLVM_DEBUG(dbgs() << "Adding '"; dumpUnpackedICmp(dbgs(), Pred, A, B);
1459              dbgs() << "'\n");
1460   bool Added = false;
1461   auto &CSToUse = getCS(R.IsSigned);
1462   if (R.Coefficients.empty())
1463     return;
1464 
1465   Added |= CSToUse.addVariableRowFill(R.Coefficients);
1466 
1467   // If R has been added to the system, add the new variables and queue it for
1468   // removal once it goes out-of-scope.
1469   if (Added) {
1470     SmallVector<Value *, 2> ValuesToRelease;
1471     auto &Value2Index = getValue2Index(R.IsSigned);
1472     for (Value *V : NewVariables) {
1473       Value2Index.insert({V, Value2Index.size() + 1});
1474       ValuesToRelease.push_back(V);
1475     }
1476 
1477     LLVM_DEBUG({
1478       dbgs() << "  constraint: ";
1479       dumpConstraint(R.Coefficients, getValue2Index(R.IsSigned));
1480       dbgs() << "\n";
1481     });
1482 
1483     DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1484                             std::move(ValuesToRelease));
1485 
1486     if (!R.IsSigned) {
1487       for (Value *V : NewVariables) {
1488         ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
1489                             false, false, false);
1490         VarPos.Coefficients[Value2Index[V]] = -1;
1491         CSToUse.addVariableRow(VarPos.Coefficients);
1492         DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1493                                 SmallVector<Value *, 2>());
1494       }
1495     }
1496 
1497     if (R.isEq()) {
1498       // Also add the inverted constraint for equality constraints.
1499       for (auto &Coeff : R.Coefficients)
1500         Coeff *= -1;
1501       CSToUse.addVariableRowFill(R.Coefficients);
1502 
1503       DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1504                               SmallVector<Value *, 2>());
1505     }
1506   }
1507 }
1508 
1509 static bool replaceSubOverflowUses(IntrinsicInst *II, Value *A, Value *B,
1510                                    SmallVectorImpl<Instruction *> &ToRemove) {
1511   bool Changed = false;
1512   IRBuilder<> Builder(II->getParent(), II->getIterator());
1513   Value *Sub = nullptr;
1514   for (User *U : make_early_inc_range(II->users())) {
1515     if (match(U, m_ExtractValue<0>(m_Value()))) {
1516       if (!Sub)
1517         Sub = Builder.CreateSub(A, B);
1518       U->replaceAllUsesWith(Sub);
1519       Changed = true;
1520     } else if (match(U, m_ExtractValue<1>(m_Value()))) {
1521       U->replaceAllUsesWith(Builder.getFalse());
1522       Changed = true;
1523     } else
1524       continue;
1525 
1526     if (U->use_empty()) {
1527       auto *I = cast<Instruction>(U);
1528       ToRemove.push_back(I);
1529       I->setOperand(0, PoisonValue::get(II->getType()));
1530       Changed = true;
1531     }
1532   }
1533 
1534   if (II->use_empty()) {
1535     II->eraseFromParent();
1536     Changed = true;
1537   }
1538   return Changed;
1539 }
1540 
1541 static bool
1542 tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
1543                           SmallVectorImpl<Instruction *> &ToRemove) {
1544   auto DoesConditionHold = [](CmpInst::Predicate Pred, Value *A, Value *B,
1545                               ConstraintInfo &Info) {
1546     auto R = Info.getConstraintForSolving(Pred, A, B);
1547     if (R.size() < 2 || !R.isValid(Info))
1548       return false;
1549 
1550     auto &CSToUse = Info.getCS(R.IsSigned);
1551     return CSToUse.isConditionImplied(R.Coefficients);
1552   };
1553 
1554   bool Changed = false;
1555   if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
1556     // If A s>= B && B s>= 0, ssub.with.overflow(a, b) should not overflow and
1557     // can be simplified to a regular sub.
1558     Value *A = II->getArgOperand(0);
1559     Value *B = II->getArgOperand(1);
1560     if (!DoesConditionHold(CmpInst::ICMP_SGE, A, B, Info) ||
1561         !DoesConditionHold(CmpInst::ICMP_SGE, B,
1562                            ConstantInt::get(A->getType(), 0), Info))
1563       return false;
1564     Changed = replaceSubOverflowUses(II, A, B, ToRemove);
1565   }
1566   return Changed;
1567 }
1568 
1569 static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
1570                                  ScalarEvolution &SE,
1571                                  OptimizationRemarkEmitter &ORE) {
1572   bool Changed = false;
1573   DT.updateDFSNumbers();
1574   SmallVector<Value *> FunctionArgs;
1575   for (Value &Arg : F.args())
1576     FunctionArgs.push_back(&Arg);
1577   ConstraintInfo Info(F.getParent()->getDataLayout(), FunctionArgs);
1578   State S(DT, LI, SE);
1579   std::unique_ptr<Module> ReproducerModule(
1580       DumpReproducers ? new Module(F.getName(), F.getContext()) : nullptr);
1581 
1582   // First, collect conditions implied by branches and blocks with their
1583   // Dominator DFS in and out numbers.
1584   for (BasicBlock &BB : F) {
1585     if (!DT.getNode(&BB))
1586       continue;
1587     S.addInfoFor(BB);
1588   }
1589 
1590   // Next, sort worklist by dominance, so that dominating conditions to check
1591   // and facts come before conditions and facts dominated by them. If a
1592   // condition to check and a fact have the same numbers, conditional facts come
1593   // first. Assume facts and checks are ordered according to their relative
1594   // order in the containing basic block. Also make sure conditions with
1595   // constant operands come before conditions without constant operands. This
1596   // increases the effectiveness of the current signed <-> unsigned fact
1597   // transfer logic.
1598   stable_sort(S.WorkList, [](const FactOrCheck &A, const FactOrCheck &B) {
1599     auto HasNoConstOp = [](const FactOrCheck &B) {
1600       Value *V0 = B.isConditionFact() ? B.Cond.Op0 : B.Inst->getOperand(0);
1601       Value *V1 = B.isConditionFact() ? B.Cond.Op1 : B.Inst->getOperand(1);
1602       return !isa<ConstantInt>(V0) && !isa<ConstantInt>(V1);
1603     };
1604     // If both entries have the same In numbers, conditional facts come first.
1605     // Otherwise use the relative order in the basic block.
1606     if (A.NumIn == B.NumIn) {
1607       if (A.isConditionFact() && B.isConditionFact()) {
1608         bool NoConstOpA = HasNoConstOp(A);
1609         bool NoConstOpB = HasNoConstOp(B);
1610         return NoConstOpA < NoConstOpB;
1611       }
1612       if (A.isConditionFact())
1613         return true;
1614       if (B.isConditionFact())
1615         return false;
1616       auto *InstA = A.getContextInst();
1617       auto *InstB = B.getContextInst();
1618       return InstA->comesBefore(InstB);
1619     }
1620     return A.NumIn < B.NumIn;
1621   });
1622 
1623   SmallVector<Instruction *> ToRemove;
1624 
1625   // Finally, process ordered worklist and eliminate implied conditions.
1626   SmallVector<StackEntry, 16> DFSInStack;
1627   SmallVector<ReproducerEntry> ReproducerCondStack;
1628   for (FactOrCheck &CB : S.WorkList) {
1629     // First, pop entries from the stack that are out-of-scope for CB. Remove
1630     // the corresponding entry from the constraint system.
1631     while (!DFSInStack.empty()) {
1632       auto &E = DFSInStack.back();
1633       LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut
1634                         << "\n");
1635       LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n");
1636       assert(E.NumIn <= CB.NumIn);
1637       if (CB.NumOut <= E.NumOut)
1638         break;
1639       LLVM_DEBUG({
1640         dbgs() << "Removing ";
1641         dumpConstraint(Info.getCS(E.IsSigned).getLastConstraint(),
1642                        Info.getValue2Index(E.IsSigned));
1643         dbgs() << "\n";
1644       });
1645       removeEntryFromStack(E, Info, ReproducerModule.get(), ReproducerCondStack,
1646                            DFSInStack);
1647     }
1648 
1649     LLVM_DEBUG(dbgs() << "Processing ");
1650 
1651     // For a block, check if any CmpInsts become known based on the current set
1652     // of constraints.
1653     if (CB.isCheck()) {
1654       Instruction *Inst = CB.getInstructionToSimplify();
1655       if (!Inst)
1656         continue;
1657       LLVM_DEBUG(dbgs() << "condition to simplify: " << *Inst << "\n");
1658       if (auto *II = dyn_cast<WithOverflowInst>(Inst)) {
1659         Changed |= tryToSimplifyOverflowMath(II, Info, ToRemove);
1660       } else if (auto *Cmp = dyn_cast<ICmpInst>(Inst)) {
1661         bool Simplified = checkAndReplaceCondition(
1662             Cmp, Info, CB.NumIn, CB.NumOut, CB.getContextInst(),
1663             ReproducerModule.get(), ReproducerCondStack, S.DT, ToRemove);
1664         if (!Simplified &&
1665             match(CB.getContextInst(), m_LogicalOp(m_Value(), m_Value()))) {
1666           Simplified =
1667               checkOrAndOpImpliedByOther(CB, Info, ReproducerModule.get(),
1668                                          ReproducerCondStack, DFSInStack);
1669         }
1670         Changed |= Simplified;
1671       }
1672       continue;
1673     }
1674 
1675     auto AddFact = [&](CmpInst::Predicate Pred, Value *A, Value *B) {
1676       LLVM_DEBUG(dbgs() << "fact to add to the system: ";
1677                  dumpUnpackedICmp(dbgs(), Pred, A, B); dbgs() << "\n");
1678       if (Info.getCS(CmpInst::isSigned(Pred)).size() > MaxRows) {
1679         LLVM_DEBUG(
1680             dbgs()
1681             << "Skip adding constraint because system has too many rows.\n");
1682         return;
1683       }
1684 
1685       Info.addFact(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
1686       if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size())
1687         ReproducerCondStack.emplace_back(Pred, A, B);
1688 
1689       Info.transferToOtherSystem(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
1690       if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size()) {
1691         // Add dummy entries to ReproducerCondStack to keep it in sync with
1692         // DFSInStack.
1693         for (unsigned I = 0,
1694                       E = (DFSInStack.size() - ReproducerCondStack.size());
1695              I < E; ++I) {
1696           ReproducerCondStack.emplace_back(ICmpInst::BAD_ICMP_PREDICATE,
1697                                            nullptr, nullptr);
1698         }
1699       }
1700     };
1701 
1702     ICmpInst::Predicate Pred;
1703     if (!CB.isConditionFact()) {
1704       Value *X;
1705       if (match(CB.Inst, m_Intrinsic<Intrinsic::abs>(m_Value(X)))) {
1706         // TODO: Add CB.Inst >= 0 fact.
1707         AddFact(CmpInst::ICMP_SGE, CB.Inst, X);
1708         continue;
1709       }
1710 
1711       if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(CB.Inst)) {
1712         Pred = ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
1713         AddFact(Pred, MinMax, MinMax->getLHS());
1714         AddFact(Pred, MinMax, MinMax->getRHS());
1715         continue;
1716       }
1717     }
1718 
1719     Value *A = nullptr, *B = nullptr;
1720     if (CB.isConditionFact()) {
1721       Pred = CB.Cond.Pred;
1722       A = CB.Cond.Op0;
1723       B = CB.Cond.Op1;
1724       if (CB.DoesHold.Pred != CmpInst::BAD_ICMP_PREDICATE &&
1725           !Info.doesHold(CB.DoesHold.Pred, CB.DoesHold.Op0, CB.DoesHold.Op1))
1726         continue;
1727     } else {
1728       bool Matched = match(CB.Inst, m_Intrinsic<Intrinsic::assume>(
1729                                         m_ICmp(Pred, m_Value(A), m_Value(B))));
1730       (void)Matched;
1731       assert(Matched && "Must have an assume intrinsic with a icmp operand");
1732     }
1733     AddFact(Pred, A, B);
1734   }
1735 
1736   if (ReproducerModule && !ReproducerModule->functions().empty()) {
1737     std::string S;
1738     raw_string_ostream StringS(S);
1739     ReproducerModule->print(StringS, nullptr);
1740     StringS.flush();
1741     OptimizationRemark Rem(DEBUG_TYPE, "Reproducer", &F);
1742     Rem << ore::NV("module") << S;
1743     ORE.emit(Rem);
1744   }
1745 
1746 #ifndef NDEBUG
1747   unsigned SignedEntries =
1748       count_if(DFSInStack, [](const StackEntry &E) { return E.IsSigned; });
1749   assert(Info.getCS(false).size() - FunctionArgs.size() ==
1750              DFSInStack.size() - SignedEntries &&
1751          "updates to CS and DFSInStack are out of sync");
1752   assert(Info.getCS(true).size() == SignedEntries &&
1753          "updates to CS and DFSInStack are out of sync");
1754 #endif
1755 
1756   for (Instruction *I : ToRemove)
1757     I->eraseFromParent();
1758   return Changed;
1759 }
1760 
1761 PreservedAnalyses ConstraintEliminationPass::run(Function &F,
1762                                                  FunctionAnalysisManager &AM) {
1763   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1764   auto &LI = AM.getResult<LoopAnalysis>(F);
1765   auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
1766   auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
1767   if (!eliminateConstraints(F, DT, LI, SE, ORE))
1768     return PreservedAnalyses::all();
1769 
1770   PreservedAnalyses PA;
1771   PA.preserve<DominatorTreeAnalysis>();
1772   PA.preserve<LoopAnalysis>();
1773   PA.preserve<ScalarEvolutionAnalysis>();
1774   PA.preserveSet<CFGAnalyses>();
1775   return PA;
1776 }
1777