ConstraintElimination.cpp 20 KB

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