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