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->stripPointerCasts()); 143 auto BDec = decompose(Op1->stripPointerCasts()); 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 #ifndef NDEBUG 220 static void dumpWithNames(ConstraintTy &C, 221 DenseMap<Value *, unsigned> &Value2Index) { 222 SmallVector<std::string> Names(Value2Index.size(), ""); 223 for (auto &KV : Value2Index) { 224 Names[KV.second - 1] = std::string("%") + KV.first->getName().str(); 225 } 226 ConstraintSystem CS; 227 CS.addVariableRowFill(C.Coefficients); 228 CS.dump(Names); 229 } 230 #endif 231 232 static bool eliminateConstraints(Function &F, DominatorTree &DT) { 233 bool Changed = false; 234 DT.updateDFSNumbers(); 235 ConstraintSystem CS; 236 237 SmallVector<ConstraintOrBlock, 64> WorkList; 238 239 // First, collect conditions implied by branches and blocks with their 240 // Dominator DFS in and out numbers. 241 for (BasicBlock &BB : F) { 242 if (!DT.getNode(&BB)) 243 continue; 244 WorkList.emplace_back(DT.getNode(&BB)); 245 246 auto *Br = dyn_cast<BranchInst>(BB.getTerminator()); 247 if (!Br || !Br->isConditional()) 248 continue; 249 250 // Returns true if we can add a known condition from BB to its successor 251 // block Succ. Each predecessor of Succ can either be BB or be dominated by 252 // Succ (e.g. the case when adding a condition from a pre-header to a loop 253 // header). 254 auto CanAdd = [&BB, &DT](BasicBlock *Succ) { 255 return all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) { 256 return Pred == &BB || DT.dominates(Succ, Pred); 257 }); 258 }; 259 // If the condition is an OR of 2 compares and the false successor only has 260 // the current block as predecessor, queue both negated conditions for the 261 // false successor. 262 Value *Op0, *Op1; 263 if (match(Br->getCondition(), m_LogicalOr(m_Value(Op0), m_Value(Op1))) && 264 match(Op0, m_Cmp()) && match(Op1, m_Cmp())) { 265 BasicBlock *FalseSuccessor = Br->getSuccessor(1); 266 if (CanAdd(FalseSuccessor)) { 267 WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op0), 268 true); 269 WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op1), 270 true); 271 } 272 continue; 273 } 274 275 // If the condition is an AND of 2 compares and the true successor only has 276 // the current block as predecessor, queue both conditions for the true 277 // successor. 278 if (match(Br->getCondition(), m_LogicalAnd(m_Value(Op0), m_Value(Op1))) && 279 match(Op0, m_Cmp()) && match(Op1, m_Cmp())) { 280 BasicBlock *TrueSuccessor = Br->getSuccessor(0); 281 if (CanAdd(TrueSuccessor)) { 282 WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op0), 283 false); 284 WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op1), 285 false); 286 } 287 continue; 288 } 289 290 auto *CmpI = dyn_cast<CmpInst>(Br->getCondition()); 291 if (!CmpI) 292 continue; 293 if (CanAdd(Br->getSuccessor(0))) 294 WorkList.emplace_back(DT.getNode(Br->getSuccessor(0)), CmpI, false); 295 if (CanAdd(Br->getSuccessor(1))) 296 WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true); 297 } 298 299 // Next, sort worklist by dominance, so that dominating blocks and conditions 300 // come before blocks and conditions dominated by them. If a block and a 301 // condition have the same numbers, the condition comes before the block, as 302 // it holds on entry to the block. 303 sort(WorkList, [](const ConstraintOrBlock &A, const ConstraintOrBlock &B) { 304 return std::tie(A.NumIn, A.IsBlock) < std::tie(B.NumIn, B.IsBlock); 305 }); 306 307 // Finally, process ordered worklist and eliminate implied conditions. 308 SmallVector<StackEntry, 16> DFSInStack; 309 DenseMap<Value *, unsigned> Value2Index; 310 for (ConstraintOrBlock &CB : WorkList) { 311 // First, pop entries from the stack that are out-of-scope for CB. Remove 312 // the corresponding entry from the constraint system. 313 while (!DFSInStack.empty()) { 314 auto &E = DFSInStack.back(); 315 LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut 316 << "\n"); 317 LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n"); 318 assert(E.NumIn <= CB.NumIn); 319 if (CB.NumOut <= E.NumOut) 320 break; 321 LLVM_DEBUG(dbgs() << "Removing " << *E.Condition << " " << E.IsNot 322 << "\n"); 323 DFSInStack.pop_back(); 324 CS.popLastConstraint(); 325 } 326 327 LLVM_DEBUG({ 328 dbgs() << "Processing "; 329 if (CB.IsBlock) 330 dbgs() << *CB.BB; 331 else 332 dbgs() << *CB.Condition; 333 dbgs() << "\n"; 334 }); 335 336 // For a block, check if any CmpInsts become known based on the current set 337 // of constraints. 338 if (CB.IsBlock) { 339 for (Instruction &I : *CB.BB) { 340 auto *Cmp = dyn_cast<CmpInst>(&I); 341 if (!Cmp) 342 continue; 343 auto R = getConstraint(Cmp, Value2Index, false); 344 if (R.size() != 1 || R[0].size() == 1) 345 continue; 346 if (CS.isConditionImplied(R[0].Coefficients)) { 347 if (!DebugCounter::shouldExecute(EliminatedCounter)) 348 continue; 349 350 LLVM_DEBUG(dbgs() << "Condition " << *Cmp 351 << " implied by dominating constraints\n"); 352 LLVM_DEBUG({ 353 for (auto &E : reverse(DFSInStack)) 354 dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n"; 355 }); 356 Cmp->replaceAllUsesWith( 357 ConstantInt::getTrue(F.getParent()->getContext())); 358 NumCondsRemoved++; 359 Changed = true; 360 } 361 if (CS.isConditionImplied( 362 ConstraintSystem::negate(R[0].Coefficients))) { 363 if (!DebugCounter::shouldExecute(EliminatedCounter)) 364 continue; 365 366 LLVM_DEBUG(dbgs() << "Condition !" << *Cmp 367 << " implied by dominating constraints\n"); 368 LLVM_DEBUG({ 369 for (auto &E : reverse(DFSInStack)) 370 dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n"; 371 }); 372 Cmp->replaceAllUsesWith( 373 ConstantInt::getFalse(F.getParent()->getContext())); 374 NumCondsRemoved++; 375 Changed = true; 376 } 377 } 378 continue; 379 } 380 381 // Set up a function to restore the predicate at the end of the scope if it 382 // has been negated. Negate the predicate in-place, if required. 383 auto *CI = dyn_cast<CmpInst>(CB.Condition); 384 auto PredicateRestorer = make_scope_exit([CI, &CB]() { 385 if (CB.Not && CI) 386 CI->setPredicate(CI->getInversePredicate()); 387 }); 388 if (CB.Not) { 389 if (CI) { 390 CI->setPredicate(CI->getInversePredicate()); 391 } else { 392 LLVM_DEBUG(dbgs() << "Can only negate compares so far.\n"); 393 continue; 394 } 395 } 396 397 // Otherwise, add the condition to the system and stack, if we can transform 398 // it into a constraint. 399 auto R = getConstraint(CB.Condition, Value2Index, true); 400 if (R.empty()) 401 continue; 402 403 LLVM_DEBUG(dbgs() << "Adding " << *CB.Condition << " " << CB.Not << "\n"); 404 bool Added = false; 405 for (auto &C : R) { 406 auto Coeffs = C.Coefficients; 407 LLVM_DEBUG({ 408 dbgs() << " constraint: "; 409 dumpWithNames(C, Value2Index); 410 }); 411 Added |= CS.addVariableRowFill(Coeffs); 412 // If R has been added to the system, queue it for removal once it goes 413 // out-of-scope. 414 if (Added) 415 DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not); 416 } 417 } 418 419 assert(CS.size() == DFSInStack.size() && 420 "updates to CS and DFSInStack are out of sync"); 421 return Changed; 422 } 423 424 PreservedAnalyses ConstraintEliminationPass::run(Function &F, 425 FunctionAnalysisManager &AM) { 426 auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 427 if (!eliminateConstraints(F, DT)) 428 return PreservedAnalyses::all(); 429 430 PreservedAnalyses PA; 431 PA.preserve<DominatorTreeAnalysis>(); 432 PA.preserve<GlobalsAA>(); 433 PA.preserveSet<CFGAnalyses>(); 434 return PA; 435 } 436 437 namespace { 438 439 class ConstraintElimination : public FunctionPass { 440 public: 441 static char ID; 442 443 ConstraintElimination() : FunctionPass(ID) { 444 initializeConstraintEliminationPass(*PassRegistry::getPassRegistry()); 445 } 446 447 bool runOnFunction(Function &F) override { 448 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 449 return eliminateConstraints(F, DT); 450 } 451 452 void getAnalysisUsage(AnalysisUsage &AU) const override { 453 AU.setPreservesCFG(); 454 AU.addRequired<DominatorTreeWrapperPass>(); 455 AU.addPreserved<GlobalsAAWrapperPass>(); 456 AU.addPreserved<DominatorTreeWrapperPass>(); 457 } 458 }; 459 460 } // end anonymous namespace 461 462 char ConstraintElimination::ID = 0; 463 464 INITIALIZE_PASS_BEGIN(ConstraintElimination, "constraint-elimination", 465 "Constraint Elimination", false, false) 466 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 467 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass) 468 INITIALIZE_PASS_END(ConstraintElimination, "constraint-elimination", 469 "Constraint Elimination", false, false) 470 471 FunctionPass *llvm::createConstraintEliminationPass() { 472 return new ConstraintElimination(); 473 } 474