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/ValueTracking.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/InitializePasses.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/DebugCounter.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Transforms/Scalar.h"
33 
34 #include <string>
35 
36 using namespace llvm;
37 using namespace PatternMatch;
38 
39 #define DEBUG_TYPE "constraint-elimination"
40 
41 STATISTIC(NumCondsRemoved, "Number of instructions removed");
42 DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
43               "Controls which conditions are eliminated");
44 
45 static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
46 static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min();
47 
48 namespace {
49 
50 class ConstraintInfo;
51 
52 /// Struct to express a pre-condition of the form %Op0 Pred %Op1.
53 struct PreconditionTy {
54   CmpInst::Predicate Pred;
55   Value *Op0;
56   Value *Op1;
57 
58   PreconditionTy(CmpInst::Predicate Pred, Value *Op0, Value *Op1)
59       : Pred(Pred), Op0(Op0), Op1(Op1) {}
60 };
61 
62 struct ConstraintTy {
63   SmallVector<int64_t, 8> Coefficients;
64   SmallVector<PreconditionTy, 2> Preconditions;
65 
66   bool IsSigned = false;
67   bool IsEq = false;
68 
69   ConstraintTy() = default;
70 
71   ConstraintTy(SmallVector<int64_t, 8> Coefficients, bool IsSigned)
72       : Coefficients(Coefficients), IsSigned(IsSigned) {}
73 
74   unsigned size() const { return Coefficients.size(); }
75 
76   unsigned empty() const { return Coefficients.empty(); }
77 
78   /// Returns true if any constraint has a non-zero coefficient for any of the
79   /// newly added indices. Zero coefficients for new indices are removed. If it
80   /// returns true, no new variable need to be added to the system.
81   bool needsNewIndices(const DenseMap<Value *, unsigned> &NewIndices) {
82     for (unsigned I = 0; I < NewIndices.size(); ++I) {
83       int64_t Last = Coefficients.pop_back_val();
84       if (Last != 0)
85         return true;
86     }
87     return false;
88   }
89 
90   /// Returns true if all preconditions for this list of constraints are
91   /// satisfied given \p CS and the corresponding \p Value2Index mapping.
92   bool isValid(const ConstraintInfo &Info) const;
93 
94   /// Returns true if there is exactly one constraint in the list and isValid is
95   /// also true.
96   bool isValidSingle(const ConstraintInfo &Info) const {
97     if (size() != 1)
98       return false;
99     return isValid(Info);
100   }
101 };
102 
103 /// Wrapper encapsulating separate constraint systems and corresponding value
104 /// mappings for both unsigned and signed information. Facts are added to and
105 /// conditions are checked against the corresponding system depending on the
106 /// signed-ness of their predicates. While the information is kept separate
107 /// based on signed-ness, certain conditions can be transferred between the two
108 /// systems.
109 class ConstraintInfo {
110   DenseMap<Value *, unsigned> UnsignedValue2Index;
111   DenseMap<Value *, unsigned> SignedValue2Index;
112 
113   ConstraintSystem UnsignedCS;
114   ConstraintSystem SignedCS;
115 
116 public:
117   DenseMap<Value *, unsigned> &getValue2Index(bool Signed) {
118     return Signed ? SignedValue2Index : UnsignedValue2Index;
119   }
120   const DenseMap<Value *, unsigned> &getValue2Index(bool Signed) const {
121     return Signed ? SignedValue2Index : UnsignedValue2Index;
122   }
123 
124   ConstraintSystem &getCS(bool Signed) {
125     return Signed ? SignedCS : UnsignedCS;
126   }
127   const ConstraintSystem &getCS(bool Signed) const {
128     return Signed ? SignedCS : UnsignedCS;
129   }
130 
131   void popLastConstraint(bool Signed) { getCS(Signed).popLastConstraint(); }
132   void popLastNVariables(bool Signed, unsigned N) {
133     getCS(Signed).popLastNVariables(N);
134   }
135 };
136 
137 } // namespace
138 
139 // Decomposes \p V into a vector of pairs of the form { c, X } where c * X. The
140 // sum of the pairs equals \p V.  The first pair is the constant-factor and X
141 // must be nullptr. If the expression cannot be decomposed, returns an empty
142 // vector.
143 static SmallVector<std::pair<int64_t, Value *>, 4>
144 decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
145           bool IsSigned) {
146 
147   auto CanUseSExt = [](ConstantInt *CI) {
148     const APInt &Val = CI->getValue();
149     return Val.sgt(MinSignedConstraintValue) && Val.slt(MaxConstraintValue);
150   };
151   // Decompose \p V used with a signed predicate.
152   if (IsSigned) {
153     if (auto *CI = dyn_cast<ConstantInt>(V)) {
154       if (CanUseSExt(CI))
155         return {{CI->getSExtValue(), nullptr}};
156     }
157 
158     return {{0, nullptr}, {1, V}};
159   }
160 
161   if (auto *CI = dyn_cast<ConstantInt>(V)) {
162     if (CI->uge(MaxConstraintValue))
163       return {};
164     return {{CI->getZExtValue(), nullptr}};
165   }
166   auto *GEP = dyn_cast<GetElementPtrInst>(V);
167   if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
168     Value *Op0, *Op1;
169     ConstantInt *CI;
170 
171     // If the index is zero-extended, it is guaranteed to be positive.
172     if (match(GEP->getOperand(GEP->getNumOperands() - 1),
173               m_ZExt(m_Value(Op0)))) {
174       if (match(Op0, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))) &&
175           CanUseSExt(CI))
176         return {{0, nullptr},
177                 {1, GEP->getPointerOperand()},
178                 {std::pow(int64_t(2), CI->getSExtValue()), Op1}};
179       if (match(Op0, m_NSWAdd(m_Value(Op1), m_ConstantInt(CI))) &&
180           CanUseSExt(CI))
181         return {{CI->getSExtValue(), nullptr},
182                 {1, GEP->getPointerOperand()},
183                 {1, Op1}};
184       return {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}};
185     }
186 
187     if (match(GEP->getOperand(GEP->getNumOperands() - 1), m_ConstantInt(CI)) &&
188         !CI->isNegative() && CanUseSExt(CI))
189       return {{CI->getSExtValue(), nullptr}, {1, GEP->getPointerOperand()}};
190 
191     SmallVector<std::pair<int64_t, Value *>, 4> Result;
192     if (match(GEP->getOperand(GEP->getNumOperands() - 1),
193               m_NUWShl(m_Value(Op0), m_ConstantInt(CI))) &&
194         CanUseSExt(CI))
195       Result = {{0, nullptr},
196                 {1, GEP->getPointerOperand()},
197                 {std::pow(int64_t(2), CI->getSExtValue()), Op0}};
198     else if (match(GEP->getOperand(GEP->getNumOperands() - 1),
199                    m_NSWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
200              CanUseSExt(CI))
201       Result = {{CI->getSExtValue(), nullptr},
202                 {1, GEP->getPointerOperand()},
203                 {1, Op0}};
204     else {
205       Op0 = GEP->getOperand(GEP->getNumOperands() - 1);
206       Result = {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}};
207     }
208     // If Op0 is signed non-negative, the GEP is increasing monotonically and
209     // can be de-composed.
210     Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
211                                ConstantInt::get(Op0->getType(), 0));
212     return Result;
213   }
214 
215   Value *Op0;
216   if (match(V, m_ZExt(m_Value(Op0))))
217     V = Op0;
218 
219   Value *Op1;
220   ConstantInt *CI;
221   if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
222       !CI->uge(MaxConstraintValue))
223     return {{CI->getZExtValue(), nullptr}, {1, Op0}};
224   if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
225       CanUseSExt(CI)) {
226     Preconditions.emplace_back(
227         CmpInst::ICMP_UGE, Op0,
228         ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
229     return {{CI->getSExtValue(), nullptr}, {1, Op0}};
230   }
231   if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1))))
232     return {{0, nullptr}, {1, Op0}, {1, Op1}};
233 
234   if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))) && CanUseSExt(CI))
235     return {{-1 * CI->getSExtValue(), nullptr}, {1, Op0}};
236   if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1))))
237     return {{0, nullptr}, {1, Op0}, {-1, Op1}};
238 
239   return {{0, nullptr}, {1, V}};
240 }
241 
242 /// Turn a condition \p CmpI into a vector of constraints, using indices from \p
243 /// Value2Index. Additional indices for newly discovered values are added to \p
244 /// NewIndices.
245 static ConstraintTy
246 getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
247               const DenseMap<Value *, unsigned> &Value2Index,
248               DenseMap<Value *, unsigned> &NewIndices) {
249   bool IsEq = false;
250   // Try to convert Pred to one of ULE/SLT/SLE/SLT.
251   switch (Pred) {
252   case CmpInst::ICMP_UGT:
253   case CmpInst::ICMP_UGE:
254   case CmpInst::ICMP_SGT:
255   case CmpInst::ICMP_SGE: {
256     Pred = CmpInst::getSwappedPredicate(Pred);
257     std::swap(Op0, Op1);
258     break;
259   }
260   case CmpInst::ICMP_EQ:
261     if (match(Op1, m_Zero())) {
262       Pred = CmpInst::ICMP_ULE;
263     } else {
264       IsEq = true;
265       Pred = CmpInst::ICMP_ULE;
266     }
267     break;
268   case CmpInst::ICMP_NE:
269     if (!match(Op1, m_Zero()))
270       return {};
271     Pred = CmpInst::getSwappedPredicate(CmpInst::ICMP_UGT);
272     std::swap(Op0, Op1);
273     break;
274   default:
275     break;
276   }
277 
278   // Only ULE and ULT predicates are supported at the moment.
279   if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT &&
280       Pred != CmpInst::ICMP_SLE && Pred != CmpInst::ICMP_SLT)
281     return {};
282 
283   SmallVector<PreconditionTy, 4> Preconditions;
284   bool IsSigned = CmpInst::isSigned(Pred);
285   auto ADec = decompose(Op0->stripPointerCastsSameRepresentation(),
286                         Preconditions, IsSigned);
287   auto BDec = decompose(Op1->stripPointerCastsSameRepresentation(),
288                         Preconditions, IsSigned);
289   // Skip if decomposing either of the values failed.
290   if (ADec.empty() || BDec.empty())
291     return {};
292 
293   // Skip trivial constraints without any variables.
294   if (ADec.size() == 1 && BDec.size() == 1)
295     return {};
296 
297   int64_t Offset1 = ADec[0].first;
298   int64_t Offset2 = BDec[0].first;
299   Offset1 *= -1;
300 
301   // Create iterator ranges that skip the constant-factor.
302   auto VariablesA = llvm::drop_begin(ADec);
303   auto VariablesB = llvm::drop_begin(BDec);
304 
305   // First try to look up \p V in Value2Index and NewIndices. Otherwise add a
306   // new entry to NewIndices.
307   auto GetOrAddIndex = [&Value2Index, &NewIndices](Value *V) -> unsigned {
308     auto V2I = Value2Index.find(V);
309     if (V2I != Value2Index.end())
310       return V2I->second;
311     auto Insert =
312         NewIndices.insert({V, Value2Index.size() + NewIndices.size() + 1});
313     return Insert.first->second;
314   };
315 
316   // Make sure all variables have entries in Value2Index or NewIndices.
317   for (const auto &KV :
318        concat<std::pair<int64_t, Value *>>(VariablesA, VariablesB))
319     GetOrAddIndex(KV.second);
320 
321   // Build result constraint, by first adding all coefficients from A and then
322   // subtracting all coefficients from B.
323   ConstraintTy Res(
324       SmallVector<int64_t, 8>(Value2Index.size() + NewIndices.size() + 1, 0),
325       IsSigned);
326   Res.IsEq = IsEq;
327   auto &R = Res.Coefficients;
328   for (const auto &KV : VariablesA)
329     R[GetOrAddIndex(KV.second)] += KV.first;
330 
331   for (const auto &KV : VariablesB)
332     R[GetOrAddIndex(KV.second)] -= KV.first;
333 
334   int64_t OffsetSum;
335   if (AddOverflow(Offset1, Offset2, OffsetSum))
336     return {};
337   if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT))
338     if (AddOverflow(OffsetSum, int64_t(-1), OffsetSum))
339       return {};
340   R[0] = OffsetSum;
341   Res.Preconditions = std::move(Preconditions);
342   return Res;
343 }
344 
345 static ConstraintTy getConstraint(CmpInst *Cmp, ConstraintInfo &Info,
346                                   DenseMap<Value *, unsigned> &NewIndices) {
347   return getConstraint(
348       Cmp->getPredicate(), Cmp->getOperand(0), Cmp->getOperand(1),
349       Info.getValue2Index(CmpInst::isSigned(Cmp->getPredicate())), NewIndices);
350 }
351 
352 bool ConstraintTy::isValid(const ConstraintInfo &Info) const {
353   return Coefficients.size() > 0 &&
354          all_of(Preconditions, [&Info](const PreconditionTy &C) {
355            DenseMap<Value *, unsigned> NewIndices;
356            auto R = getConstraint(
357                C.Pred, C.Op0, C.Op1,
358                Info.getValue2Index(CmpInst::isSigned(C.Pred)), NewIndices);
359            // TODO: properly check NewIndices.
360            return NewIndices.empty() && R.Preconditions.empty() && !R.IsEq &&
361                   R.size() >= 2 &&
362                   Info.getCS(CmpInst::isSigned(C.Pred))
363                       .isConditionImplied(R.Coefficients);
364          });
365 }
366 
367 namespace {
368 /// Represents either a condition that holds on entry to a block or a basic
369 /// block, with their respective Dominator DFS in and out numbers.
370 struct ConstraintOrBlock {
371   unsigned NumIn;
372   unsigned NumOut;
373   bool IsBlock;
374   bool Not;
375   union {
376     BasicBlock *BB;
377     CmpInst *Condition;
378   };
379 
380   ConstraintOrBlock(DomTreeNode *DTN)
381       : NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(true),
382         BB(DTN->getBlock()) {}
383   ConstraintOrBlock(DomTreeNode *DTN, CmpInst *Condition, bool Not)
384       : NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(false),
385         Not(Not), Condition(Condition) {}
386 };
387 
388 struct StackEntry {
389   unsigned NumIn;
390   unsigned NumOut;
391   Instruction *Condition;
392   bool IsNot;
393   bool IsSigned = false;
394   /// Variables that can be removed from the system once the stack entry gets
395   /// removed.
396   SmallVector<Value *, 2> ValuesToRelease;
397 
398   StackEntry(unsigned NumIn, unsigned NumOut, CmpInst *Condition, bool IsNot,
399              bool IsSigned, SmallVector<Value *, 2> ValuesToRelease)
400       : NumIn(NumIn), NumOut(NumOut), Condition(Condition), IsNot(IsNot),
401         IsSigned(IsSigned), ValuesToRelease(ValuesToRelease) {}
402 };
403 
404 /// Keep state required to build worklist.
405 struct State {
406   DominatorTree &DT;
407   SmallVector<ConstraintOrBlock, 64> WorkList;
408 
409   State(DominatorTree &DT) : DT(DT) {}
410 
411   /// Process block \p BB and add known facts to work-list.
412   void addInfoFor(BasicBlock &BB);
413 
414   /// Returns true if we can add a known condition from BB to its successor
415   /// block Succ. Each predecessor of Succ can either be BB or be dominated
416   /// by Succ (e.g. the case when adding a condition from a pre-header to a
417   /// loop header).
418   bool canAddSuccessor(BasicBlock &BB, BasicBlock *Succ) const {
419     if (BB.getSingleSuccessor()) {
420       assert(BB.getSingleSuccessor() == Succ);
421       return DT.properlyDominates(&BB, Succ);
422     }
423     return any_of(successors(&BB),
424                   [Succ](const BasicBlock *S) { return S != Succ; }) &&
425            all_of(predecessors(Succ), [&BB, Succ, this](BasicBlock *Pred) {
426              return Pred == &BB || DT.dominates(Succ, Pred);
427            });
428   }
429 };
430 
431 } // namespace
432 
433 #ifndef NDEBUG
434 static void dumpWithNames(ConstraintTy &C,
435                           DenseMap<Value *, unsigned> &Value2Index) {
436   SmallVector<std::string> Names(Value2Index.size(), "");
437   for (auto &KV : Value2Index) {
438     Names[KV.second - 1] = std::string("%") + KV.first->getName().str();
439   }
440   ConstraintSystem CS;
441   CS.addVariableRowFill(C.Coefficients);
442   CS.dump(Names);
443 }
444 #endif
445 
446 void State::addInfoFor(BasicBlock &BB) {
447   WorkList.emplace_back(DT.getNode(&BB));
448 
449   // True as long as long as the current instruction is guaranteed to execute.
450   bool GuaranteedToExecute = true;
451   // Scan BB for assume calls.
452   // TODO: also use this scan to queue conditions to simplify, so we can
453   // interleave facts from assumes and conditions to simplify in a single
454   // basic block. And to skip another traversal of each basic block when
455   // simplifying.
456   for (Instruction &I : BB) {
457     Value *Cond;
458     // For now, just handle assumes with a single compare as condition.
459     if (match(&I, m_Intrinsic<Intrinsic::assume>(m_Value(Cond))) &&
460         isa<ICmpInst>(Cond)) {
461       if (GuaranteedToExecute) {
462         // The assume is guaranteed to execute when BB is entered, hence Cond
463         // holds on entry to BB.
464         WorkList.emplace_back(DT.getNode(&BB), cast<ICmpInst>(Cond), false);
465       } else {
466         // Otherwise the condition only holds in the successors.
467         for (BasicBlock *Succ : successors(&BB)) {
468           if (!canAddSuccessor(BB, Succ))
469             continue;
470           WorkList.emplace_back(DT.getNode(Succ), cast<ICmpInst>(Cond), false);
471         }
472       }
473     }
474     GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
475   }
476 
477   auto *Br = dyn_cast<BranchInst>(BB.getTerminator());
478   if (!Br || !Br->isConditional())
479     return;
480 
481   // If the condition is an OR of 2 compares and the false successor only has
482   // the current block as predecessor, queue both negated conditions for the
483   // false successor.
484   Value *Op0, *Op1;
485   if (match(Br->getCondition(), m_LogicalOr(m_Value(Op0), m_Value(Op1))) &&
486       isa<ICmpInst>(Op0) && isa<ICmpInst>(Op1)) {
487     BasicBlock *FalseSuccessor = Br->getSuccessor(1);
488     if (canAddSuccessor(BB, FalseSuccessor)) {
489       WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<ICmpInst>(Op0),
490                             true);
491       WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<ICmpInst>(Op1),
492                             true);
493     }
494     return;
495   }
496 
497   // If the condition is an AND of 2 compares and the true successor only has
498   // the current block as predecessor, queue both conditions for the true
499   // successor.
500   if (match(Br->getCondition(), m_LogicalAnd(m_Value(Op0), m_Value(Op1))) &&
501       isa<ICmpInst>(Op0) && isa<ICmpInst>(Op1)) {
502     BasicBlock *TrueSuccessor = Br->getSuccessor(0);
503     if (canAddSuccessor(BB, TrueSuccessor)) {
504       WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<ICmpInst>(Op0),
505                             false);
506       WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<ICmpInst>(Op1),
507                             false);
508     }
509     return;
510   }
511 
512   auto *CmpI = dyn_cast<ICmpInst>(Br->getCondition());
513   if (!CmpI)
514     return;
515   if (canAddSuccessor(BB, Br->getSuccessor(0)))
516     WorkList.emplace_back(DT.getNode(Br->getSuccessor(0)), CmpI, false);
517   if (canAddSuccessor(BB, Br->getSuccessor(1)))
518     WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true);
519 }
520 
521 static void
522 tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
523                           SmallVectorImpl<Instruction *> &ToRemove) {
524   auto DoesConditionHold = [](CmpInst::Predicate Pred, Value *A, Value *B,
525                               ConstraintInfo &Info) {
526     DenseMap<Value *, unsigned> NewIndices;
527     auto R = getConstraint(
528         Pred, A, B, Info.getValue2Index(CmpInst::isSigned(Pred)), NewIndices);
529     if (R.size() < 2 || R.needsNewIndices(NewIndices) || !R.isValid(Info))
530       return false;
531 
532     auto &CSToUse = Info.getCS(CmpInst::isSigned(Pred));
533     return CSToUse.isConditionImplied(R.Coefficients);
534   };
535 
536   if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
537     // If A s>= B && B s>= 0, ssub.with.overflow(a, b) should not overflow and
538     // can be simplified to a regular sub.
539     Value *A = II->getArgOperand(0);
540     Value *B = II->getArgOperand(1);
541     if (!DoesConditionHold(CmpInst::ICMP_SGE, A, B, Info) ||
542         !DoesConditionHold(CmpInst::ICMP_SGE, B,
543                            ConstantInt::get(A->getType(), 0), Info))
544       return;
545 
546     IRBuilder<> Builder(II->getParent(), II->getIterator());
547     Value *Sub = nullptr;
548     for (User *U : make_early_inc_range(II->users())) {
549       if (match(U, m_ExtractValue<0>(m_Value()))) {
550         if (!Sub)
551           Sub = Builder.CreateSub(A, B);
552         U->replaceAllUsesWith(Sub);
553       } else if (match(U, m_ExtractValue<1>(m_Value())))
554         U->replaceAllUsesWith(Builder.getFalse());
555       else
556         continue;
557 
558       if (U->use_empty()) {
559         auto *I = cast<Instruction>(U);
560         ToRemove.push_back(I);
561         I->setOperand(0, PoisonValue::get(II->getType()));
562       }
563     }
564 
565     if (II->use_empty())
566       II->eraseFromParent();
567   }
568 }
569 
570 static bool eliminateConstraints(Function &F, DominatorTree &DT) {
571   bool Changed = false;
572   DT.updateDFSNumbers();
573 
574   ConstraintInfo Info;
575   State S(DT);
576 
577   // First, collect conditions implied by branches and blocks with their
578   // Dominator DFS in and out numbers.
579   for (BasicBlock &BB : F) {
580     if (!DT.getNode(&BB))
581       continue;
582     S.addInfoFor(BB);
583   }
584 
585   // Next, sort worklist by dominance, so that dominating blocks and conditions
586   // come before blocks and conditions dominated by them. If a block and a
587   // condition have the same numbers, the condition comes before the block, as
588   // it holds on entry to the block.
589   sort(S.WorkList, [](const ConstraintOrBlock &A, const ConstraintOrBlock &B) {
590     return std::tie(A.NumIn, A.IsBlock) < std::tie(B.NumIn, B.IsBlock);
591   });
592 
593   SmallVector<Instruction *> ToRemove;
594 
595   // Finally, process ordered worklist and eliminate implied conditions.
596   SmallVector<StackEntry, 16> DFSInStack;
597   for (ConstraintOrBlock &CB : S.WorkList) {
598     // First, pop entries from the stack that are out-of-scope for CB. Remove
599     // the corresponding entry from the constraint system.
600     while (!DFSInStack.empty()) {
601       auto &E = DFSInStack.back();
602       LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut
603                         << "\n");
604       LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n");
605       assert(E.NumIn <= CB.NumIn);
606       if (CB.NumOut <= E.NumOut)
607         break;
608       LLVM_DEBUG(dbgs() << "Removing " << *E.Condition << " " << E.IsNot
609                         << "\n");
610       Info.popLastConstraint(E.IsSigned);
611       // Remove variables in the system that went out of scope.
612       auto &Mapping = Info.getValue2Index(E.IsSigned);
613       for (Value *V : E.ValuesToRelease)
614         Mapping.erase(V);
615       Info.popLastNVariables(E.IsSigned, E.ValuesToRelease.size());
616       DFSInStack.pop_back();
617     }
618 
619     LLVM_DEBUG({
620       dbgs() << "Processing ";
621       if (CB.IsBlock)
622         dbgs() << *CB.BB;
623       else
624         dbgs() << *CB.Condition;
625       dbgs() << "\n";
626     });
627 
628     // For a block, check if any CmpInsts become known based on the current set
629     // of constraints.
630     if (CB.IsBlock) {
631       for (Instruction &I : make_early_inc_range(*CB.BB)) {
632         if (auto *II = dyn_cast<WithOverflowInst>(&I)) {
633           tryToSimplifyOverflowMath(II, Info, ToRemove);
634           continue;
635         }
636         auto *Cmp = dyn_cast<ICmpInst>(&I);
637         if (!Cmp)
638           continue;
639 
640         DenseMap<Value *, unsigned> NewIndices;
641         auto R = getConstraint(Cmp, Info, NewIndices);
642         if (R.IsEq || R.size() < 2 || R.needsNewIndices(NewIndices) ||
643             !R.isValid(Info))
644           continue;
645 
646         auto &CSToUse = Info.getCS(R.IsSigned);
647         if (CSToUse.isConditionImplied(R.Coefficients)) {
648           if (!DebugCounter::shouldExecute(EliminatedCounter))
649             continue;
650 
651           LLVM_DEBUG(dbgs() << "Condition " << *Cmp
652                             << " implied by dominating constraints\n");
653           LLVM_DEBUG({
654             for (auto &E : reverse(DFSInStack))
655               dbgs() << "   C " << *E.Condition << " " << E.IsNot << "\n";
656           });
657           Cmp->replaceUsesWithIf(
658               ConstantInt::getTrue(F.getParent()->getContext()), [](Use &U) {
659                 // Conditions in an assume trivially simplify to true. Skip uses
660                 // in assume calls to not destroy the available information.
661                 auto *II = dyn_cast<IntrinsicInst>(U.getUser());
662                 return !II || II->getIntrinsicID() != Intrinsic::assume;
663               });
664           NumCondsRemoved++;
665           Changed = true;
666         }
667         if (CSToUse.isConditionImplied(
668                 ConstraintSystem::negate(R.Coefficients))) {
669           if (!DebugCounter::shouldExecute(EliminatedCounter))
670             continue;
671 
672           LLVM_DEBUG(dbgs() << "Condition !" << *Cmp
673                             << " implied by dominating constraints\n");
674           LLVM_DEBUG({
675             for (auto &E : reverse(DFSInStack))
676               dbgs() << "   C " << *E.Condition << " " << E.IsNot << "\n";
677           });
678           Cmp->replaceAllUsesWith(
679               ConstantInt::getFalse(F.getParent()->getContext()));
680           NumCondsRemoved++;
681           Changed = true;
682         }
683       }
684       continue;
685     }
686 
687     // Set up a function to restore the predicate at the end of the scope if it
688     // has been negated. Negate the predicate in-place, if required.
689     auto *CI = dyn_cast<ICmpInst>(CB.Condition);
690     auto PredicateRestorer = make_scope_exit([CI, &CB]() {
691       if (CB.Not && CI)
692         CI->setPredicate(CI->getInversePredicate());
693     });
694     if (CB.Not) {
695       if (CI) {
696         CI->setPredicate(CI->getInversePredicate());
697       } else {
698         LLVM_DEBUG(dbgs() << "Can only negate compares so far.\n");
699         continue;
700       }
701     }
702 
703     // Otherwise, add the condition to the system and stack, if we can transform
704     // it into a constraint.
705     DenseMap<Value *, unsigned> NewIndices;
706     auto R = getConstraint(CB.Condition, Info, NewIndices);
707     if (!R.isValid(Info))
708       continue;
709 
710     LLVM_DEBUG(dbgs() << "Adding " << *CB.Condition << " " << CB.Not << "\n");
711     bool Added = false;
712     assert(CmpInst::isSigned(CB.Condition->getPredicate()) == R.IsSigned &&
713            "condition and constraint signs must match");
714     auto &CSToUse = Info.getCS(R.IsSigned);
715     if (R.Coefficients.empty())
716       continue;
717 
718     Added |= CSToUse.addVariableRowFill(R.Coefficients);
719 
720     // If R has been added to the system, queue it for removal once it goes
721     // out-of-scope.
722     if (Added) {
723       SmallVector<Value *, 2> ValuesToRelease;
724       for (auto &KV : NewIndices) {
725         Info.getValue2Index(R.IsSigned).insert(KV);
726         ValuesToRelease.push_back(KV.first);
727       }
728 
729       LLVM_DEBUG({
730         dbgs() << "  constraint: ";
731         dumpWithNames(R, Info.getValue2Index(R.IsSigned));
732       });
733 
734       DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not,
735                               R.IsSigned, ValuesToRelease);
736 
737       if (R.IsEq) {
738         // Also add the inverted constraint for equality constraints.
739         for (auto &Coeff : R.Coefficients)
740           Coeff *= -1;
741         CSToUse.addVariableRowFill(R.Coefficients);
742 
743         DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not,
744                                 R.IsSigned, SmallVector<Value *, 2>());
745       }
746     }
747   }
748 
749 #ifndef NDEBUG
750   unsigned SignedEntries =
751       count_if(DFSInStack, [](const StackEntry &E) { return E.IsSigned; });
752   assert(Info.getCS(false).size() == DFSInStack.size() - SignedEntries &&
753          "updates to CS and DFSInStack are out of sync");
754   assert(Info.getCS(true).size() == SignedEntries &&
755          "updates to CS and DFSInStack are out of sync");
756 #endif
757 
758   for (Instruction *I : ToRemove)
759     I->eraseFromParent();
760   return Changed;
761 }
762 
763 PreservedAnalyses ConstraintEliminationPass::run(Function &F,
764                                                  FunctionAnalysisManager &AM) {
765   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
766   if (!eliminateConstraints(F, DT))
767     return PreservedAnalyses::all();
768 
769   PreservedAnalyses PA;
770   PA.preserve<DominatorTreeAnalysis>();
771   PA.preserveSet<CFGAnalyses>();
772   return PA;
773 }
774 
775 namespace {
776 
777 class ConstraintElimination : public FunctionPass {
778 public:
779   static char ID;
780 
781   ConstraintElimination() : FunctionPass(ID) {
782     initializeConstraintEliminationPass(*PassRegistry::getPassRegistry());
783   }
784 
785   bool runOnFunction(Function &F) override {
786     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
787     return eliminateConstraints(F, DT);
788   }
789 
790   void getAnalysisUsage(AnalysisUsage &AU) const override {
791     AU.setPreservesCFG();
792     AU.addRequired<DominatorTreeWrapperPass>();
793     AU.addPreserved<GlobalsAAWrapperPass>();
794     AU.addPreserved<DominatorTreeWrapperPass>();
795   }
796 };
797 
798 } // end anonymous namespace
799 
800 char ConstraintElimination::ID = 0;
801 
802 INITIALIZE_PASS_BEGIN(ConstraintElimination, "constraint-elimination",
803                       "Constraint Elimination", false, false)
804 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
805 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
806 INITIALIZE_PASS_END(ConstraintElimination, "constraint-elimination",
807                     "Constraint Elimination", false, false)
808 
809 FunctionPass *llvm::createConstraintEliminationPass() {
810   return new ConstraintElimination();
811 }
812