xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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/DataLayout.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/Function.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/Transforms/Scalar.h"
32 
33 #include <string>
34 
35 using namespace llvm;
36 using namespace PatternMatch;
37 
38 #define DEBUG_TYPE "constraint-elimination"
39 
40 STATISTIC(NumCondsRemoved, "Number of instructions removed");
41 DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
42               "Controls which conditions are eliminated");
43 
44 static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
45 
46 // Decomposes \p V into a vector of pairs of the form { c, X } where c * X. The
47 // sum of the pairs equals \p V.  The first pair is the constant-factor and X
48 // must be nullptr. If the expression cannot be decomposed, returns an empty
49 // vector.
50 static SmallVector<std::pair<int64_t, Value *>, 4> decompose(Value *V) {
51   if (auto *CI = dyn_cast<ConstantInt>(V)) {
52     if (CI->isNegative() || CI->uge(MaxConstraintValue))
53       return {};
54     return {{CI->getSExtValue(), nullptr}};
55   }
56   auto *GEP = dyn_cast<GetElementPtrInst>(V);
57   if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
58     Value *Op0, *Op1;
59     ConstantInt *CI;
60 
61     // If the index is zero-extended, it is guaranteed to be positive.
62     if (match(GEP->getOperand(GEP->getNumOperands() - 1),
63               m_ZExt(m_Value(Op0)))) {
64       if (match(Op0, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))))
65         return {{0, nullptr},
66                 {1, GEP->getPointerOperand()},
67                 {std::pow(int64_t(2), CI->getSExtValue()), Op1}};
68       if (match(Op0, m_NSWAdd(m_Value(Op1), m_ConstantInt(CI))))
69         return {{CI->getSExtValue(), nullptr},
70                 {1, GEP->getPointerOperand()},
71                 {1, Op1}};
72       return {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}};
73     }
74 
75     if (match(GEP->getOperand(GEP->getNumOperands() - 1), m_ConstantInt(CI)) &&
76         !CI->isNegative())
77       return {{CI->getSExtValue(), nullptr}, {1, GEP->getPointerOperand()}};
78 
79     SmallVector<std::pair<int64_t, Value *>, 4> Result;
80     if (match(GEP->getOperand(GEP->getNumOperands() - 1),
81               m_NUWShl(m_Value(Op0), m_ConstantInt(CI))))
82       Result = {{0, nullptr},
83                 {1, GEP->getPointerOperand()},
84                 {std::pow(int64_t(2), CI->getSExtValue()), Op0}};
85     else if (match(GEP->getOperand(GEP->getNumOperands() - 1),
86                    m_NSWAdd(m_Value(Op0), m_ConstantInt(CI))))
87       Result = {{CI->getSExtValue(), nullptr},
88                 {1, GEP->getPointerOperand()},
89                 {1, Op0}};
90     else {
91       Op0 = GEP->getOperand(GEP->getNumOperands() - 1);
92       Result = {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}};
93     }
94     return Result;
95   }
96 
97   Value *Op0;
98   if (match(V, m_ZExt(m_Value(Op0))))
99     V = Op0;
100 
101   Value *Op1;
102   ConstantInt *CI;
103   if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))))
104     return {{CI->getSExtValue(), nullptr}, {1, Op0}};
105   if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1))))
106     return {{0, nullptr}, {1, Op0}, {1, Op1}};
107 
108   if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))))
109     return {{-1 * CI->getSExtValue(), nullptr}, {1, Op0}};
110   if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1))))
111     return {{0, nullptr}, {1, Op0}, {1, Op1}};
112 
113   return {{0, nullptr}, {1, V}};
114 }
115 
116 struct ConstraintTy {
117   SmallVector<int64_t, 8> Coefficients;
118 
119   ConstraintTy(SmallVector<int64_t, 8> Coefficients)
120       : Coefficients(Coefficients) {}
121 
122   unsigned size() const { return Coefficients.size(); }
123 };
124 
125 /// Turn a condition \p CmpI into a vector of constraints, using indices from \p
126 /// Value2Index. Additional indices for newly discovered values are added to \p
127 /// NewIndices.
128 static SmallVector<ConstraintTy, 4>
129 getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
130               const DenseMap<Value *, unsigned> &Value2Index,
131               DenseMap<Value *, unsigned> &NewIndices) {
132   int64_t Offset1 = 0;
133   int64_t Offset2 = 0;
134 
135   // First try to look up \p V in Value2Index and NewIndices. Otherwise add a
136   // new entry to NewIndices.
137   auto GetOrAddIndex = [&Value2Index, &NewIndices](Value *V) -> unsigned {
138     auto V2I = Value2Index.find(V);
139     if (V2I != Value2Index.end())
140       return V2I->second;
141     auto NewI = NewIndices.find(V);
142     if (NewI != NewIndices.end())
143       return NewI->second;
144     auto Insert =
145         NewIndices.insert({V, Value2Index.size() + NewIndices.size() + 1});
146     return Insert.first->second;
147   };
148 
149   if (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE)
150     return getConstraint(CmpInst::getSwappedPredicate(Pred), Op1, Op0,
151                          Value2Index, NewIndices);
152 
153   if (Pred == CmpInst::ICMP_EQ) {
154     auto A =
155         getConstraint(CmpInst::ICMP_UGE, Op0, Op1, Value2Index, NewIndices);
156     auto B =
157         getConstraint(CmpInst::ICMP_ULE, Op0, Op1, Value2Index, NewIndices);
158     append_range(A, B);
159     return A;
160   }
161 
162   if (Pred == CmpInst::ICMP_NE && match(Op1, m_Zero())) {
163     return getConstraint(CmpInst::ICMP_UGT, Op0, Op1, Value2Index, NewIndices);
164   }
165 
166   // Only ULE and ULT predicates are supported at the moment.
167   if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT)
168     return {};
169 
170   auto ADec = decompose(Op0->stripPointerCastsSameRepresentation());
171   auto BDec = decompose(Op1->stripPointerCastsSameRepresentation());
172   // Skip if decomposing either of the values failed.
173   if (ADec.empty() || BDec.empty())
174     return {};
175 
176   // Skip trivial constraints without any variables.
177   if (ADec.size() == 1 && BDec.size() == 1)
178     return {};
179 
180   Offset1 = ADec[0].first;
181   Offset2 = BDec[0].first;
182   Offset1 *= -1;
183 
184   // Create iterator ranges that skip the constant-factor.
185   auto VariablesA = llvm::drop_begin(ADec);
186   auto VariablesB = llvm::drop_begin(BDec);
187 
188   // Make sure all variables have entries in Value2Index or NewIndices.
189   for (const auto &KV :
190        concat<std::pair<int64_t, Value *>>(VariablesA, VariablesB))
191     GetOrAddIndex(KV.second);
192 
193   // Build result constraint, by first adding all coefficients from A and then
194   // subtracting all coefficients from B.
195   SmallVector<int64_t, 8> R(Value2Index.size() + NewIndices.size() + 1, 0);
196   for (const auto &KV : VariablesA)
197     R[GetOrAddIndex(KV.second)] += KV.first;
198 
199   for (const auto &KV : VariablesB)
200     R[GetOrAddIndex(KV.second)] -= KV.first;
201 
202   R[0] = Offset1 + Offset2 + (Pred == CmpInst::ICMP_ULT ? -1 : 0);
203   return {R};
204 }
205 
206 static SmallVector<ConstraintTy, 4>
207 getConstraint(CmpInst *Cmp, const DenseMap<Value *, unsigned> &Value2Index,
208               DenseMap<Value *, unsigned> &NewIndices) {
209   return getConstraint(Cmp->getPredicate(), Cmp->getOperand(0),
210                        Cmp->getOperand(1), Value2Index, NewIndices);
211 }
212 
213 namespace {
214 /// Represents either a condition that holds on entry to a block or a basic
215 /// block, with their respective Dominator DFS in and out numbers.
216 struct ConstraintOrBlock {
217   unsigned NumIn;
218   unsigned NumOut;
219   bool IsBlock;
220   bool Not;
221   union {
222     BasicBlock *BB;
223     CmpInst *Condition;
224   };
225 
226   ConstraintOrBlock(DomTreeNode *DTN)
227       : NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(true),
228         BB(DTN->getBlock()) {}
229   ConstraintOrBlock(DomTreeNode *DTN, CmpInst *Condition, bool Not)
230       : NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(false),
231         Not(Not), Condition(Condition) {}
232 };
233 
234 struct StackEntry {
235   unsigned NumIn;
236   unsigned NumOut;
237   CmpInst *Condition;
238   bool IsNot;
239 
240   StackEntry(unsigned NumIn, unsigned NumOut, CmpInst *Condition, bool IsNot)
241       : NumIn(NumIn), NumOut(NumOut), Condition(Condition), IsNot(IsNot) {}
242 };
243 } // namespace
244 
245 #ifndef NDEBUG
246 static void dumpWithNames(ConstraintTy &C,
247                           DenseMap<Value *, unsigned> &Value2Index) {
248   SmallVector<std::string> Names(Value2Index.size(), "");
249   for (auto &KV : Value2Index) {
250     Names[KV.second - 1] = std::string("%") + KV.first->getName().str();
251   }
252   ConstraintSystem CS;
253   CS.addVariableRowFill(C.Coefficients);
254   CS.dump(Names);
255 }
256 #endif
257 
258 static bool eliminateConstraints(Function &F, DominatorTree &DT) {
259   bool Changed = false;
260   DT.updateDFSNumbers();
261   ConstraintSystem CS;
262 
263   SmallVector<ConstraintOrBlock, 64> WorkList;
264 
265   // First, collect conditions implied by branches and blocks with their
266   // Dominator DFS in and out numbers.
267   for (BasicBlock &BB : F) {
268     if (!DT.getNode(&BB))
269       continue;
270     WorkList.emplace_back(DT.getNode(&BB));
271 
272     // True as long as long as the current instruction is guaranteed to execute.
273     bool GuaranteedToExecute = true;
274     // Scan BB for assume calls.
275     // TODO: also use this scan to queue conditions to simplify, so we can
276     // interleave facts from assumes and conditions to simplify in a single
277     // basic block. And to skip another traversal of each basic block when
278     // simplifying.
279     for (Instruction &I : BB) {
280       Value *Cond;
281       // For now, just handle assumes with a single compare as condition.
282       if (match(&I, m_Intrinsic<Intrinsic::assume>(m_Value(Cond))) &&
283           isa<CmpInst>(Cond)) {
284         if (GuaranteedToExecute) {
285           // The assume is guaranteed to execute when BB is entered, hence Cond
286           // holds on entry to BB.
287           WorkList.emplace_back(DT.getNode(&BB), cast<CmpInst>(Cond), false);
288         } else {
289           // Otherwise the condition only holds in the successors.
290           for (BasicBlock *Succ : successors(&BB))
291             WorkList.emplace_back(DT.getNode(Succ), cast<CmpInst>(Cond), false);
292         }
293       }
294       GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
295     }
296 
297     auto *Br = dyn_cast<BranchInst>(BB.getTerminator());
298     if (!Br || !Br->isConditional())
299       continue;
300 
301     // Returns true if we can add a known condition from BB to its successor
302     // block Succ. Each predecessor of Succ can either be BB or be dominated by
303     // Succ (e.g. the case when adding a condition from a pre-header to a loop
304     // header).
305     auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
306       return all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
307         return Pred == &BB || DT.dominates(Succ, Pred);
308       });
309     };
310     // If the condition is an OR of 2 compares and the false successor only has
311     // the current block as predecessor, queue both negated conditions for the
312     // false successor.
313     Value *Op0, *Op1;
314     if (match(Br->getCondition(), m_LogicalOr(m_Value(Op0), m_Value(Op1))) &&
315         match(Op0, m_Cmp()) && match(Op1, m_Cmp())) {
316       BasicBlock *FalseSuccessor = Br->getSuccessor(1);
317       if (CanAdd(FalseSuccessor)) {
318         WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op0),
319                               true);
320         WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op1),
321                               true);
322       }
323       continue;
324     }
325 
326     // If the condition is an AND of 2 compares and the true successor only has
327     // the current block as predecessor, queue both conditions for the true
328     // successor.
329     if (match(Br->getCondition(), m_LogicalAnd(m_Value(Op0), m_Value(Op1))) &&
330         match(Op0, m_Cmp()) && match(Op1, m_Cmp())) {
331       BasicBlock *TrueSuccessor = Br->getSuccessor(0);
332       if (CanAdd(TrueSuccessor)) {
333         WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op0),
334                               false);
335         WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op1),
336                               false);
337       }
338       continue;
339     }
340 
341     auto *CmpI = dyn_cast<CmpInst>(Br->getCondition());
342     if (!CmpI)
343       continue;
344     if (CanAdd(Br->getSuccessor(0)))
345       WorkList.emplace_back(DT.getNode(Br->getSuccessor(0)), CmpI, false);
346     if (CanAdd(Br->getSuccessor(1)))
347       WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true);
348   }
349 
350   // Next, sort worklist by dominance, so that dominating blocks and conditions
351   // come before blocks and conditions dominated by them. If a block and a
352   // condition have the same numbers, the condition comes before the block, as
353   // it holds on entry to the block.
354   sort(WorkList, [](const ConstraintOrBlock &A, const ConstraintOrBlock &B) {
355     return std::tie(A.NumIn, A.IsBlock) < std::tie(B.NumIn, B.IsBlock);
356   });
357 
358   // Finally, process ordered worklist and eliminate implied conditions.
359   SmallVector<StackEntry, 16> DFSInStack;
360   DenseMap<Value *, unsigned> Value2Index;
361   for (ConstraintOrBlock &CB : WorkList) {
362     // First, pop entries from the stack that are out-of-scope for CB. Remove
363     // the corresponding entry from the constraint system.
364     while (!DFSInStack.empty()) {
365       auto &E = DFSInStack.back();
366       LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut
367                         << "\n");
368       LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n");
369       assert(E.NumIn <= CB.NumIn);
370       if (CB.NumOut <= E.NumOut)
371         break;
372       LLVM_DEBUG(dbgs() << "Removing " << *E.Condition << " " << E.IsNot
373                         << "\n");
374       DFSInStack.pop_back();
375       CS.popLastConstraint();
376     }
377 
378     LLVM_DEBUG({
379       dbgs() << "Processing ";
380       if (CB.IsBlock)
381         dbgs() << *CB.BB;
382       else
383         dbgs() << *CB.Condition;
384       dbgs() << "\n";
385     });
386 
387     // For a block, check if any CmpInsts become known based on the current set
388     // of constraints.
389     if (CB.IsBlock) {
390       for (Instruction &I : *CB.BB) {
391         auto *Cmp = dyn_cast<CmpInst>(&I);
392         if (!Cmp)
393           continue;
394 
395         DenseMap<Value *, unsigned> NewIndices;
396         auto R = getConstraint(Cmp, Value2Index, NewIndices);
397         if (R.size() != 1)
398           continue;
399 
400         // Check if all coefficients of new indices are 0 after building the
401         // constraint. Skip if any of the new indices has a non-null
402         // coefficient.
403         bool HasNewIndex = false;
404         for (unsigned I = 0; I < NewIndices.size(); ++I) {
405           int64_t Last = R[0].Coefficients.pop_back_val();
406           if (Last != 0) {
407             HasNewIndex = true;
408             break;
409           }
410         }
411         if (HasNewIndex || R[0].size() == 1)
412           continue;
413 
414         if (CS.isConditionImplied(R[0].Coefficients)) {
415           if (!DebugCounter::shouldExecute(EliminatedCounter))
416             continue;
417 
418           LLVM_DEBUG(dbgs() << "Condition " << *Cmp
419                             << " implied by dominating constraints\n");
420           LLVM_DEBUG({
421             for (auto &E : reverse(DFSInStack))
422               dbgs() << "   C " << *E.Condition << " " << E.IsNot << "\n";
423           });
424           Cmp->replaceUsesWithIf(
425               ConstantInt::getTrue(F.getParent()->getContext()), [](Use &U) {
426                 // Conditions in an assume trivially simplify to true. Skip uses
427                 // in assume calls to not destroy the available information.
428                 auto *II = dyn_cast<IntrinsicInst>(U.getUser());
429                 return !II || II->getIntrinsicID() != Intrinsic::assume;
430               });
431           NumCondsRemoved++;
432           Changed = true;
433         }
434         if (CS.isConditionImplied(
435                 ConstraintSystem::negate(R[0].Coefficients))) {
436           if (!DebugCounter::shouldExecute(EliminatedCounter))
437             continue;
438 
439           LLVM_DEBUG(dbgs() << "Condition !" << *Cmp
440                             << " implied by dominating constraints\n");
441           LLVM_DEBUG({
442             for (auto &E : reverse(DFSInStack))
443               dbgs() << "   C " << *E.Condition << " " << E.IsNot << "\n";
444           });
445           Cmp->replaceAllUsesWith(
446               ConstantInt::getFalse(F.getParent()->getContext()));
447           NumCondsRemoved++;
448           Changed = true;
449         }
450       }
451       continue;
452     }
453 
454     // Set up a function to restore the predicate at the end of the scope if it
455     // has been negated. Negate the predicate in-place, if required.
456     auto *CI = dyn_cast<CmpInst>(CB.Condition);
457     auto PredicateRestorer = make_scope_exit([CI, &CB]() {
458       if (CB.Not && CI)
459         CI->setPredicate(CI->getInversePredicate());
460     });
461     if (CB.Not) {
462       if (CI) {
463         CI->setPredicate(CI->getInversePredicate());
464       } else {
465         LLVM_DEBUG(dbgs() << "Can only negate compares so far.\n");
466         continue;
467       }
468     }
469 
470     // Otherwise, add the condition to the system and stack, if we can transform
471     // it into a constraint.
472     DenseMap<Value *, unsigned> NewIndices;
473     auto R = getConstraint(CB.Condition, Value2Index, NewIndices);
474     if (R.empty())
475       continue;
476 
477     for (auto &KV : NewIndices)
478       Value2Index.insert(KV);
479 
480     LLVM_DEBUG(dbgs() << "Adding " << *CB.Condition << " " << CB.Not << "\n");
481     bool Added = false;
482     for (auto &C : R) {
483       auto Coeffs = C.Coefficients;
484       LLVM_DEBUG({
485         dbgs() << "  constraint: ";
486         dumpWithNames(C, Value2Index);
487       });
488       Added |= CS.addVariableRowFill(Coeffs);
489       // If R has been added to the system, queue it for removal once it goes
490       // out-of-scope.
491       if (Added)
492         DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not);
493     }
494   }
495 
496   assert(CS.size() == DFSInStack.size() &&
497          "updates to CS and DFSInStack are out of sync");
498   return Changed;
499 }
500 
501 PreservedAnalyses ConstraintEliminationPass::run(Function &F,
502                                                  FunctionAnalysisManager &AM) {
503   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
504   if (!eliminateConstraints(F, DT))
505     return PreservedAnalyses::all();
506 
507   PreservedAnalyses PA;
508   PA.preserve<DominatorTreeAnalysis>();
509   PA.preserveSet<CFGAnalyses>();
510   return PA;
511 }
512 
513 namespace {
514 
515 class ConstraintElimination : public FunctionPass {
516 public:
517   static char ID;
518 
519   ConstraintElimination() : FunctionPass(ID) {
520     initializeConstraintEliminationPass(*PassRegistry::getPassRegistry());
521   }
522 
523   bool runOnFunction(Function &F) override {
524     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
525     return eliminateConstraints(F, DT);
526   }
527 
528   void getAnalysisUsage(AnalysisUsage &AU) const override {
529     AU.setPreservesCFG();
530     AU.addRequired<DominatorTreeWrapperPass>();
531     AU.addPreserved<GlobalsAAWrapperPass>();
532     AU.addPreserved<DominatorTreeWrapperPass>();
533   }
534 };
535 
536 } // end anonymous namespace
537 
538 char ConstraintElimination::ID = 0;
539 
540 INITIALIZE_PASS_BEGIN(ConstraintElimination, "constraint-elimination",
541                       "Constraint Elimination", false, false)
542 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
543 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
544 INITIALIZE_PASS_END(ConstraintElimination, "constraint-elimination",
545                     "Constraint Elimination", false, false)
546 
547 FunctionPass *llvm::createConstraintEliminationPass() {
548   return new ConstraintElimination();
549 }
550