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