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