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