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