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/Analysis/ValueTracking.h" 22 #include "llvm/IR/DataLayout.h" 23 #include "llvm/IR/Dominators.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/IR/Instructions.h" 26 #include "llvm/IR/PatternMatch.h" 27 #include "llvm/InitializePasses.h" 28 #include "llvm/Pass.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/DebugCounter.h" 31 #include "llvm/Transforms/Scalar.h" 32 33 #include <string> 34 35 using namespace llvm; 36 using namespace PatternMatch; 37 38 #define DEBUG_TYPE "constraint-elimination" 39 40 STATISTIC(NumCondsRemoved, "Number of instructions removed"); 41 DEBUG_COUNTER(EliminatedCounter, "conds-eliminated", 42 "Controls which conditions are eliminated"); 43 44 static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max(); 45 46 namespace { 47 48 /// Struct to express a pre-condition of the form %Op0 Pred %Op1. 49 struct PreconditionTy { 50 CmpInst::Predicate Pred; 51 Value *Op0; 52 Value *Op1; 53 54 PreconditionTy(CmpInst::Predicate Pred, Value *Op0, Value *Op1) 55 : Pred(Pred), Op0(Op0), Op1(Op1) {} 56 }; 57 58 struct ConstraintTy { 59 SmallVector<int64_t, 8> Coefficients; 60 61 ConstraintTy(SmallVector<int64_t, 8> Coefficients) 62 : Coefficients(Coefficients) {} 63 64 unsigned size() const { return Coefficients.size(); } 65 }; 66 67 /// Struct to manage a list of constraints with pre-conditions that must be 68 /// satisfied before using the constraints. 69 struct ConstraintListTy { 70 SmallVector<ConstraintTy, 4> Constraints; 71 SmallVector<PreconditionTy, 4> Preconditions; 72 73 ConstraintListTy() {} 74 75 ConstraintListTy(ArrayRef<ConstraintTy> Constraints, 76 ArrayRef<PreconditionTy> Preconditions) 77 : Constraints(Constraints.begin(), Constraints.end()), 78 Preconditions(Preconditions.begin(), Preconditions.end()) {} 79 80 void mergeIn(const ConstraintListTy &Other) { 81 append_range(Constraints, Other.Constraints); 82 // TODO: Do smarter merges here, e.g. exclude duplicates. 83 append_range(Preconditions, Other.Preconditions); 84 } 85 86 unsigned size() const { return Constraints.size(); } 87 88 unsigned empty() const { return Constraints.empty(); } 89 90 /// Returns true if any constraint has a non-zero coefficient for any of the 91 /// newly added indices. Zero coefficients for new indices are removed. If it 92 /// returns true, no new variable need to be added to the system. 93 bool needsNewIndices(const DenseMap<Value *, unsigned> &NewIndices) { 94 assert(size() == 1); 95 for (unsigned I = 0; I < NewIndices.size(); ++I) { 96 int64_t Last = get(0).Coefficients.pop_back_val(); 97 if (Last != 0) 98 return true; 99 } 100 return false; 101 } 102 103 ConstraintTy &get(unsigned I) { return Constraints[I]; } 104 105 /// Returns true if all preconditions for this list of constraints are 106 /// satisfied given \p CS and the corresponding \p Value2Index mapping. 107 bool isValid(const ConstraintSystem &CS, 108 DenseMap<Value *, unsigned> &Value2Index) const; 109 /// Returns true if there is exactly one constraint in the list and isValid is 110 /// also true. 111 bool isValidSingle(const ConstraintSystem &CS, 112 DenseMap<Value *, unsigned> &Value2Index) const { 113 return size() == 1 && isValid(CS, Value2Index); 114 } 115 }; 116 117 } // namespace 118 119 // Decomposes \p V into a vector of pairs of the form { c, X } where c * X. The 120 // sum of the pairs equals \p V. The first pair is the constant-factor and X 121 // must be nullptr. If the expression cannot be decomposed, returns an empty 122 // vector. 123 static SmallVector<std::pair<int64_t, Value *>, 4> 124 decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions) { 125 if (auto *CI = dyn_cast<ConstantInt>(V)) { 126 if (CI->isNegative() || CI->uge(MaxConstraintValue)) 127 return {}; 128 return {{CI->getSExtValue(), nullptr}}; 129 } 130 auto *GEP = dyn_cast<GetElementPtrInst>(V); 131 if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) { 132 Value *Op0, *Op1; 133 ConstantInt *CI; 134 135 // If the index is zero-extended, it is guaranteed to be positive. 136 if (match(GEP->getOperand(GEP->getNumOperands() - 1), 137 m_ZExt(m_Value(Op0)))) { 138 if (match(Op0, m_NUWShl(m_Value(Op1), m_ConstantInt(CI)))) 139 return {{0, nullptr}, 140 {1, GEP->getPointerOperand()}, 141 {std::pow(int64_t(2), CI->getSExtValue()), Op1}}; 142 if (match(Op0, m_NSWAdd(m_Value(Op1), m_ConstantInt(CI)))) 143 return {{CI->getSExtValue(), nullptr}, 144 {1, GEP->getPointerOperand()}, 145 {1, Op1}}; 146 return {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}}; 147 } 148 149 if (match(GEP->getOperand(GEP->getNumOperands() - 1), m_ConstantInt(CI)) && 150 !CI->isNegative()) 151 return {{CI->getSExtValue(), nullptr}, {1, GEP->getPointerOperand()}}; 152 153 SmallVector<std::pair<int64_t, Value *>, 4> Result; 154 if (match(GEP->getOperand(GEP->getNumOperands() - 1), 155 m_NUWShl(m_Value(Op0), m_ConstantInt(CI)))) 156 Result = {{0, nullptr}, 157 {1, GEP->getPointerOperand()}, 158 {std::pow(int64_t(2), CI->getSExtValue()), Op0}}; 159 else if (match(GEP->getOperand(GEP->getNumOperands() - 1), 160 m_NSWAdd(m_Value(Op0), m_ConstantInt(CI)))) 161 Result = {{CI->getSExtValue(), nullptr}, 162 {1, GEP->getPointerOperand()}, 163 {1, Op0}}; 164 else { 165 Op0 = GEP->getOperand(GEP->getNumOperands() - 1); 166 Result = {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}}; 167 } 168 // If Op0 is signed non-negative, the GEP is increasing monotonically and 169 // can be de-composed. 170 Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0, 171 ConstantInt::get(Op0->getType(), 0)); 172 return Result; 173 } 174 175 Value *Op0; 176 if (match(V, m_ZExt(m_Value(Op0)))) 177 V = Op0; 178 179 Value *Op1; 180 ConstantInt *CI; 181 if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI)))) 182 return {{CI->getSExtValue(), nullptr}, {1, Op0}}; 183 if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1)))) 184 return {{0, nullptr}, {1, Op0}, {1, Op1}}; 185 186 if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI)))) 187 return {{-1 * CI->getSExtValue(), nullptr}, {1, Op0}}; 188 if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1)))) 189 return {{0, nullptr}, {1, Op0}, {-1, Op1}}; 190 191 return {{0, nullptr}, {1, V}}; 192 } 193 194 /// Turn a condition \p CmpI into a vector of constraints, using indices from \p 195 /// Value2Index. Additional indices for newly discovered values are added to \p 196 /// NewIndices. 197 static ConstraintListTy 198 getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1, 199 const DenseMap<Value *, unsigned> &Value2Index, 200 DenseMap<Value *, unsigned> &NewIndices) { 201 int64_t Offset1 = 0; 202 int64_t Offset2 = 0; 203 204 SmallVector<PreconditionTy, 4> Preconditions; 205 // First try to look up \p V in Value2Index and NewIndices. Otherwise add a 206 // new entry to NewIndices. 207 auto GetOrAddIndex = [&Value2Index, &NewIndices](Value *V) -> unsigned { 208 auto V2I = Value2Index.find(V); 209 if (V2I != Value2Index.end()) 210 return V2I->second; 211 auto NewI = NewIndices.find(V); 212 if (NewI != NewIndices.end()) 213 return NewI->second; 214 auto Insert = 215 NewIndices.insert({V, Value2Index.size() + NewIndices.size() + 1}); 216 return Insert.first->second; 217 }; 218 219 if (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE) 220 return getConstraint(CmpInst::getSwappedPredicate(Pred), Op1, Op0, 221 Value2Index, NewIndices); 222 223 if (Pred == CmpInst::ICMP_EQ) { 224 if (match(Op1, m_Zero())) 225 return getConstraint(CmpInst::ICMP_ULE, Op0, Op1, Value2Index, 226 NewIndices); 227 228 auto A = 229 getConstraint(CmpInst::ICMP_UGE, Op0, Op1, Value2Index, NewIndices); 230 auto B = 231 getConstraint(CmpInst::ICMP_ULE, Op0, Op1, Value2Index, NewIndices); 232 A.mergeIn(B); 233 return A; 234 } 235 236 if (Pred == CmpInst::ICMP_NE && match(Op1, m_Zero())) { 237 return getConstraint(CmpInst::ICMP_UGT, Op0, Op1, Value2Index, NewIndices); 238 } 239 240 // Only ULE and ULT predicates are supported at the moment. 241 if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT) 242 return {}; 243 244 auto ADec = 245 decompose(Op0->stripPointerCastsSameRepresentation(), Preconditions); 246 auto BDec = 247 decompose(Op1->stripPointerCastsSameRepresentation(), Preconditions); 248 // Skip if decomposing either of the values failed. 249 if (ADec.empty() || BDec.empty()) 250 return {}; 251 252 // Skip trivial constraints without any variables. 253 if (ADec.size() == 1 && BDec.size() == 1) 254 return {}; 255 256 Offset1 = ADec[0].first; 257 Offset2 = BDec[0].first; 258 Offset1 *= -1; 259 260 // Create iterator ranges that skip the constant-factor. 261 auto VariablesA = llvm::drop_begin(ADec); 262 auto VariablesB = llvm::drop_begin(BDec); 263 264 // Make sure all variables have entries in Value2Index or NewIndices. 265 for (const auto &KV : 266 concat<std::pair<int64_t, Value *>>(VariablesA, VariablesB)) 267 GetOrAddIndex(KV.second); 268 269 // Build result constraint, by first adding all coefficients from A and then 270 // subtracting all coefficients from B. 271 SmallVector<int64_t, 8> R(Value2Index.size() + NewIndices.size() + 1, 0); 272 for (const auto &KV : VariablesA) 273 R[GetOrAddIndex(KV.second)] += KV.first; 274 275 for (const auto &KV : VariablesB) 276 R[GetOrAddIndex(KV.second)] -= KV.first; 277 278 R[0] = Offset1 + Offset2 + (Pred == CmpInst::ICMP_ULT ? -1 : 0); 279 return {{R}, Preconditions}; 280 } 281 282 static ConstraintListTy getConstraint(CmpInst *Cmp, 283 DenseMap<Value *, unsigned> &Value2Index, 284 DenseMap<Value *, unsigned> &NewIndices) { 285 return getConstraint(Cmp->getPredicate(), Cmp->getOperand(0), 286 Cmp->getOperand(1), Value2Index, NewIndices); 287 } 288 289 bool ConstraintListTy::isValid(const ConstraintSystem &CS, 290 DenseMap<Value *, unsigned> &Value2Index) const { 291 return all_of(Preconditions, [&CS, &Value2Index](const PreconditionTy &C) { 292 DenseMap<Value *, unsigned> NewIndices; 293 auto R = getConstraint(C.Pred, C.Op0, C.Op1, Value2Index, NewIndices); 294 // TODO: properly check NewIndices. 295 return NewIndices.empty() && R.Preconditions.empty() && R.size() == 1 && 296 CS.isConditionImplied(R.get(0).Coefficients); 297 }); 298 } 299 300 namespace { 301 /// Represents either a condition that holds on entry to a block or a basic 302 /// block, with their respective Dominator DFS in and out numbers. 303 struct ConstraintOrBlock { 304 unsigned NumIn; 305 unsigned NumOut; 306 bool IsBlock; 307 bool Not; 308 union { 309 BasicBlock *BB; 310 CmpInst *Condition; 311 }; 312 313 ConstraintOrBlock(DomTreeNode *DTN) 314 : NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(true), 315 BB(DTN->getBlock()) {} 316 ConstraintOrBlock(DomTreeNode *DTN, CmpInst *Condition, bool Not) 317 : NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(false), 318 Not(Not), Condition(Condition) {} 319 }; 320 321 struct StackEntry { 322 unsigned NumIn; 323 unsigned NumOut; 324 CmpInst *Condition; 325 bool IsNot; 326 327 StackEntry(unsigned NumIn, unsigned NumOut, CmpInst *Condition, bool IsNot) 328 : NumIn(NumIn), NumOut(NumOut), Condition(Condition), IsNot(IsNot) {} 329 }; 330 } // namespace 331 332 #ifndef NDEBUG 333 static void dumpWithNames(ConstraintTy &C, 334 DenseMap<Value *, unsigned> &Value2Index) { 335 SmallVector<std::string> Names(Value2Index.size(), ""); 336 for (auto &KV : Value2Index) { 337 Names[KV.second - 1] = std::string("%") + KV.first->getName().str(); 338 } 339 ConstraintSystem CS; 340 CS.addVariableRowFill(C.Coefficients); 341 CS.dump(Names); 342 } 343 #endif 344 345 static bool eliminateConstraints(Function &F, DominatorTree &DT) { 346 bool Changed = false; 347 DT.updateDFSNumbers(); 348 ConstraintSystem CS; 349 350 SmallVector<ConstraintOrBlock, 64> WorkList; 351 352 // First, collect conditions implied by branches and blocks with their 353 // Dominator DFS in and out numbers. 354 for (BasicBlock &BB : F) { 355 if (!DT.getNode(&BB)) 356 continue; 357 WorkList.emplace_back(DT.getNode(&BB)); 358 359 // True as long as long as the current instruction is guaranteed to execute. 360 bool GuaranteedToExecute = true; 361 // Scan BB for assume calls. 362 // TODO: also use this scan to queue conditions to simplify, so we can 363 // interleave facts from assumes and conditions to simplify in a single 364 // basic block. And to skip another traversal of each basic block when 365 // simplifying. 366 for (Instruction &I : BB) { 367 Value *Cond; 368 // For now, just handle assumes with a single compare as condition. 369 if (match(&I, m_Intrinsic<Intrinsic::assume>(m_Value(Cond))) && 370 isa<CmpInst>(Cond)) { 371 if (GuaranteedToExecute) { 372 // The assume is guaranteed to execute when BB is entered, hence Cond 373 // holds on entry to BB. 374 WorkList.emplace_back(DT.getNode(&BB), cast<CmpInst>(Cond), false); 375 } else { 376 // Otherwise the condition only holds in the successors. 377 for (BasicBlock *Succ : successors(&BB)) 378 WorkList.emplace_back(DT.getNode(Succ), cast<CmpInst>(Cond), false); 379 } 380 } 381 GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I); 382 } 383 384 auto *Br = dyn_cast<BranchInst>(BB.getTerminator()); 385 if (!Br || !Br->isConditional()) 386 continue; 387 388 // Returns true if we can add a known condition from BB to its successor 389 // block Succ. Each predecessor of Succ can either be BB or be dominated by 390 // Succ (e.g. the case when adding a condition from a pre-header to a loop 391 // header). 392 auto CanAdd = [&BB, &DT](BasicBlock *Succ) { 393 assert(isa<BranchInst>(BB.getTerminator())); 394 return any_of(successors(&BB), 395 [Succ](const BasicBlock *S) { return S != Succ; }) && 396 all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) { 397 return Pred == &BB || DT.dominates(Succ, Pred); 398 }); 399 }; 400 // If the condition is an OR of 2 compares and the false successor only has 401 // the current block as predecessor, queue both negated conditions for the 402 // false successor. 403 Value *Op0, *Op1; 404 if (match(Br->getCondition(), m_LogicalOr(m_Value(Op0), m_Value(Op1))) && 405 match(Op0, m_Cmp()) && match(Op1, m_Cmp())) { 406 BasicBlock *FalseSuccessor = Br->getSuccessor(1); 407 if (CanAdd(FalseSuccessor)) { 408 WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op0), 409 true); 410 WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op1), 411 true); 412 } 413 continue; 414 } 415 416 // If the condition is an AND of 2 compares and the true successor only has 417 // the current block as predecessor, queue both conditions for the true 418 // successor. 419 if (match(Br->getCondition(), m_LogicalAnd(m_Value(Op0), m_Value(Op1))) && 420 match(Op0, m_Cmp()) && match(Op1, m_Cmp())) { 421 BasicBlock *TrueSuccessor = Br->getSuccessor(0); 422 if (CanAdd(TrueSuccessor)) { 423 WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op0), 424 false); 425 WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op1), 426 false); 427 } 428 continue; 429 } 430 431 auto *CmpI = dyn_cast<CmpInst>(Br->getCondition()); 432 if (!CmpI) 433 continue; 434 if (CanAdd(Br->getSuccessor(0))) 435 WorkList.emplace_back(DT.getNode(Br->getSuccessor(0)), CmpI, false); 436 if (CanAdd(Br->getSuccessor(1))) 437 WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true); 438 } 439 440 // Next, sort worklist by dominance, so that dominating blocks and conditions 441 // come before blocks and conditions dominated by them. If a block and a 442 // condition have the same numbers, the condition comes before the block, as 443 // it holds on entry to the block. 444 sort(WorkList, [](const ConstraintOrBlock &A, const ConstraintOrBlock &B) { 445 return std::tie(A.NumIn, A.IsBlock) < std::tie(B.NumIn, B.IsBlock); 446 }); 447 448 // Finally, process ordered worklist and eliminate implied conditions. 449 SmallVector<StackEntry, 16> DFSInStack; 450 DenseMap<Value *, unsigned> Value2Index; 451 for (ConstraintOrBlock &CB : WorkList) { 452 // First, pop entries from the stack that are out-of-scope for CB. Remove 453 // the corresponding entry from the constraint system. 454 while (!DFSInStack.empty()) { 455 auto &E = DFSInStack.back(); 456 LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut 457 << "\n"); 458 LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n"); 459 assert(E.NumIn <= CB.NumIn); 460 if (CB.NumOut <= E.NumOut) 461 break; 462 LLVM_DEBUG(dbgs() << "Removing " << *E.Condition << " " << E.IsNot 463 << "\n"); 464 DFSInStack.pop_back(); 465 CS.popLastConstraint(); 466 } 467 468 LLVM_DEBUG({ 469 dbgs() << "Processing "; 470 if (CB.IsBlock) 471 dbgs() << *CB.BB; 472 else 473 dbgs() << *CB.Condition; 474 dbgs() << "\n"; 475 }); 476 477 // For a block, check if any CmpInsts become known based on the current set 478 // of constraints. 479 if (CB.IsBlock) { 480 for (Instruction &I : *CB.BB) { 481 auto *Cmp = dyn_cast<CmpInst>(&I); 482 if (!Cmp) 483 continue; 484 485 DenseMap<Value *, unsigned> NewIndices; 486 auto R = getConstraint(Cmp, Value2Index, NewIndices); 487 488 if (!R.isValidSingle(CS, Value2Index) || R.needsNewIndices(NewIndices)) 489 continue; 490 491 if (CS.isConditionImplied(R.get(0).Coefficients)) { 492 if (!DebugCounter::shouldExecute(EliminatedCounter)) 493 continue; 494 495 LLVM_DEBUG(dbgs() << "Condition " << *Cmp 496 << " implied by dominating constraints\n"); 497 LLVM_DEBUG({ 498 for (auto &E : reverse(DFSInStack)) 499 dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n"; 500 }); 501 Cmp->replaceUsesWithIf( 502 ConstantInt::getTrue(F.getParent()->getContext()), [](Use &U) { 503 // Conditions in an assume trivially simplify to true. Skip uses 504 // in assume calls to not destroy the available information. 505 auto *II = dyn_cast<IntrinsicInst>(U.getUser()); 506 return !II || II->getIntrinsicID() != Intrinsic::assume; 507 }); 508 NumCondsRemoved++; 509 Changed = true; 510 } 511 if (CS.isConditionImplied( 512 ConstraintSystem::negate(R.get(0).Coefficients))) { 513 if (!DebugCounter::shouldExecute(EliminatedCounter)) 514 continue; 515 516 LLVM_DEBUG(dbgs() << "Condition !" << *Cmp 517 << " implied by dominating constraints\n"); 518 LLVM_DEBUG({ 519 for (auto &E : reverse(DFSInStack)) 520 dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n"; 521 }); 522 Cmp->replaceAllUsesWith( 523 ConstantInt::getFalse(F.getParent()->getContext())); 524 NumCondsRemoved++; 525 Changed = true; 526 } 527 } 528 continue; 529 } 530 531 // Set up a function to restore the predicate at the end of the scope if it 532 // has been negated. Negate the predicate in-place, if required. 533 auto *CI = dyn_cast<CmpInst>(CB.Condition); 534 auto PredicateRestorer = make_scope_exit([CI, &CB]() { 535 if (CB.Not && CI) 536 CI->setPredicate(CI->getInversePredicate()); 537 }); 538 if (CB.Not) { 539 if (CI) { 540 CI->setPredicate(CI->getInversePredicate()); 541 } else { 542 LLVM_DEBUG(dbgs() << "Can only negate compares so far.\n"); 543 continue; 544 } 545 } 546 547 // Otherwise, add the condition to the system and stack, if we can transform 548 // it into a constraint. 549 DenseMap<Value *, unsigned> NewIndices; 550 auto R = getConstraint(CB.Condition, Value2Index, NewIndices); 551 if (!R.isValid(CS, Value2Index)) 552 continue; 553 554 for (auto &KV : NewIndices) 555 Value2Index.insert(KV); 556 557 LLVM_DEBUG(dbgs() << "Adding " << *CB.Condition << " " << CB.Not << "\n"); 558 bool Added = false; 559 for (auto &C : R.Constraints) { 560 auto Coeffs = C.Coefficients; 561 LLVM_DEBUG({ 562 dbgs() << " constraint: "; 563 dumpWithNames(C, Value2Index); 564 }); 565 Added |= CS.addVariableRowFill(Coeffs); 566 // If R has been added to the system, queue it for removal once it goes 567 // out-of-scope. 568 if (Added) 569 DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not); 570 } 571 } 572 573 assert(CS.size() == DFSInStack.size() && 574 "updates to CS and DFSInStack are out of sync"); 575 return Changed; 576 } 577 578 PreservedAnalyses ConstraintEliminationPass::run(Function &F, 579 FunctionAnalysisManager &AM) { 580 auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 581 if (!eliminateConstraints(F, DT)) 582 return PreservedAnalyses::all(); 583 584 PreservedAnalyses PA; 585 PA.preserve<DominatorTreeAnalysis>(); 586 PA.preserveSet<CFGAnalyses>(); 587 return PA; 588 } 589 590 namespace { 591 592 class ConstraintElimination : public FunctionPass { 593 public: 594 static char ID; 595 596 ConstraintElimination() : FunctionPass(ID) { 597 initializeConstraintEliminationPass(*PassRegistry::getPassRegistry()); 598 } 599 600 bool runOnFunction(Function &F) override { 601 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 602 return eliminateConstraints(F, DT); 603 } 604 605 void getAnalysisUsage(AnalysisUsage &AU) const override { 606 AU.setPreservesCFG(); 607 AU.addRequired<DominatorTreeWrapperPass>(); 608 AU.addPreserved<GlobalsAAWrapperPass>(); 609 AU.addPreserved<DominatorTreeWrapperPass>(); 610 } 611 }; 612 613 } // end anonymous namespace 614 615 char ConstraintElimination::ID = 0; 616 617 INITIALIZE_PASS_BEGIN(ConstraintElimination, "constraint-elimination", 618 "Constraint Elimination", false, false) 619 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 620 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass) 621 INITIALIZE_PASS_END(ConstraintElimination, "constraint-elimination", 622 "Constraint Elimination", false, false) 623 624 FunctionPass *llvm::createConstraintEliminationPass() { 625 return new ConstraintElimination(); 626 } 627