ConstraintElimination.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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/GetElementPtrTypeIterator.h"
  25. #include "llvm/IR/IRBuilder.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/PatternMatch.h"
  28. #include "llvm/Pass.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/DebugCounter.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include <cmath>
  34. #include <string>
  35. using namespace llvm;
  36. using namespace PatternMatch;
  37. #define DEBUG_TYPE "constraint-elimination"
  38. STATISTIC(NumCondsRemoved, "Number of instructions removed");
  39. DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
  40. "Controls which conditions are eliminated");
  41. static cl::opt<unsigned>
  42. MaxRows("constraint-elimination-max-rows", cl::init(500), cl::Hidden,
  43. cl::desc("Maximum number of rows to keep in constraint system"));
  44. static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
  45. static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min();
  46. // A helper to multiply 2 signed integers where overflowing is allowed.
  47. static int64_t multiplyWithOverflow(int64_t A, int64_t B) {
  48. int64_t Result;
  49. MulOverflow(A, B, Result);
  50. return Result;
  51. }
  52. // A helper to add 2 signed integers where overflowing is allowed.
  53. static int64_t addWithOverflow(int64_t A, int64_t B) {
  54. int64_t Result;
  55. AddOverflow(A, B, Result);
  56. return Result;
  57. }
  58. namespace {
  59. class ConstraintInfo;
  60. struct StackEntry {
  61. unsigned NumIn;
  62. unsigned NumOut;
  63. bool IsSigned = false;
  64. /// Variables that can be removed from the system once the stack entry gets
  65. /// removed.
  66. SmallVector<Value *, 2> ValuesToRelease;
  67. StackEntry(unsigned NumIn, unsigned NumOut, bool IsSigned,
  68. SmallVector<Value *, 2> ValuesToRelease)
  69. : NumIn(NumIn), NumOut(NumOut), IsSigned(IsSigned),
  70. ValuesToRelease(ValuesToRelease) {}
  71. };
  72. /// Struct to express a pre-condition of the form %Op0 Pred %Op1.
  73. struct PreconditionTy {
  74. CmpInst::Predicate Pred;
  75. Value *Op0;
  76. Value *Op1;
  77. PreconditionTy(CmpInst::Predicate Pred, Value *Op0, Value *Op1)
  78. : Pred(Pred), Op0(Op0), Op1(Op1) {}
  79. };
  80. struct ConstraintTy {
  81. SmallVector<int64_t, 8> Coefficients;
  82. SmallVector<PreconditionTy, 2> Preconditions;
  83. SmallVector<SmallVector<int64_t, 8>> ExtraInfo;
  84. bool IsSigned = false;
  85. bool IsEq = false;
  86. ConstraintTy() = default;
  87. ConstraintTy(SmallVector<int64_t, 8> Coefficients, bool IsSigned)
  88. : Coefficients(Coefficients), IsSigned(IsSigned) {}
  89. unsigned size() const { return Coefficients.size(); }
  90. unsigned empty() const { return Coefficients.empty(); }
  91. /// Returns true if all preconditions for this list of constraints are
  92. /// satisfied given \p CS and the corresponding \p Value2Index mapping.
  93. bool isValid(const ConstraintInfo &Info) const;
  94. };
  95. /// Wrapper encapsulating separate constraint systems and corresponding value
  96. /// mappings for both unsigned and signed information. Facts are added to and
  97. /// conditions are checked against the corresponding system depending on the
  98. /// signed-ness of their predicates. While the information is kept separate
  99. /// based on signed-ness, certain conditions can be transferred between the two
  100. /// systems.
  101. class ConstraintInfo {
  102. DenseMap<Value *, unsigned> UnsignedValue2Index;
  103. DenseMap<Value *, unsigned> SignedValue2Index;
  104. ConstraintSystem UnsignedCS;
  105. ConstraintSystem SignedCS;
  106. const DataLayout &DL;
  107. public:
  108. ConstraintInfo(const DataLayout &DL) : DL(DL) {}
  109. DenseMap<Value *, unsigned> &getValue2Index(bool Signed) {
  110. return Signed ? SignedValue2Index : UnsignedValue2Index;
  111. }
  112. const DenseMap<Value *, unsigned> &getValue2Index(bool Signed) const {
  113. return Signed ? SignedValue2Index : UnsignedValue2Index;
  114. }
  115. ConstraintSystem &getCS(bool Signed) {
  116. return Signed ? SignedCS : UnsignedCS;
  117. }
  118. const ConstraintSystem &getCS(bool Signed) const {
  119. return Signed ? SignedCS : UnsignedCS;
  120. }
  121. void popLastConstraint(bool Signed) { getCS(Signed).popLastConstraint(); }
  122. void popLastNVariables(bool Signed, unsigned N) {
  123. getCS(Signed).popLastNVariables(N);
  124. }
  125. bool doesHold(CmpInst::Predicate Pred, Value *A, Value *B) const;
  126. void addFact(CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
  127. unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack);
  128. /// Turn a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
  129. /// constraints, using indices from the corresponding constraint system.
  130. /// New variables that need to be added to the system are collected in
  131. /// \p NewVariables.
  132. ConstraintTy getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
  133. SmallVectorImpl<Value *> &NewVariables) const;
  134. /// Turns a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
  135. /// constraints using getConstraint. Returns an empty constraint if the result
  136. /// cannot be used to query the existing constraint system, e.g. because it
  137. /// would require adding new variables. Also tries to convert signed
  138. /// predicates to unsigned ones if possible to allow using the unsigned system
  139. /// which increases the effectiveness of the signed <-> unsigned transfer
  140. /// logic.
  141. ConstraintTy getConstraintForSolving(CmpInst::Predicate Pred, Value *Op0,
  142. Value *Op1) const;
  143. /// Try to add information from \p A \p Pred \p B to the unsigned/signed
  144. /// system if \p Pred is signed/unsigned.
  145. void transferToOtherSystem(CmpInst::Predicate Pred, Value *A, Value *B,
  146. unsigned NumIn, unsigned NumOut,
  147. SmallVectorImpl<StackEntry> &DFSInStack);
  148. };
  149. /// Represents a (Coefficient * Variable) entry after IR decomposition.
  150. struct DecompEntry {
  151. int64_t Coefficient;
  152. Value *Variable;
  153. /// True if the variable is known positive in the current constraint.
  154. bool IsKnownNonNegative;
  155. DecompEntry(int64_t Coefficient, Value *Variable,
  156. bool IsKnownNonNegative = false)
  157. : Coefficient(Coefficient), Variable(Variable),
  158. IsKnownNonNegative(IsKnownNonNegative) {}
  159. };
  160. /// Represents an Offset + Coefficient1 * Variable1 + ... decomposition.
  161. struct Decomposition {
  162. int64_t Offset = 0;
  163. SmallVector<DecompEntry, 3> Vars;
  164. Decomposition(int64_t Offset) : Offset(Offset) {}
  165. Decomposition(Value *V, bool IsKnownNonNegative = false) {
  166. Vars.emplace_back(1, V, IsKnownNonNegative);
  167. }
  168. Decomposition(int64_t Offset, ArrayRef<DecompEntry> Vars)
  169. : Offset(Offset), Vars(Vars) {}
  170. void add(int64_t OtherOffset) {
  171. Offset = addWithOverflow(Offset, OtherOffset);
  172. }
  173. void add(const Decomposition &Other) {
  174. add(Other.Offset);
  175. append_range(Vars, Other.Vars);
  176. }
  177. void mul(int64_t Factor) {
  178. Offset = multiplyWithOverflow(Offset, Factor);
  179. for (auto &Var : Vars)
  180. Var.Coefficient = multiplyWithOverflow(Var.Coefficient, Factor);
  181. }
  182. };
  183. } // namespace
  184. static Decomposition decompose(Value *V,
  185. SmallVectorImpl<PreconditionTy> &Preconditions,
  186. bool IsSigned, const DataLayout &DL);
  187. static bool canUseSExt(ConstantInt *CI) {
  188. const APInt &Val = CI->getValue();
  189. return Val.sgt(MinSignedConstraintValue) && Val.slt(MaxConstraintValue);
  190. }
  191. static Decomposition
  192. decomposeGEP(GetElementPtrInst &GEP,
  193. SmallVectorImpl<PreconditionTy> &Preconditions, bool IsSigned,
  194. const DataLayout &DL) {
  195. // Do not reason about pointers where the index size is larger than 64 bits,
  196. // as the coefficients used to encode constraints are 64 bit integers.
  197. if (DL.getIndexTypeSizeInBits(GEP.getPointerOperand()->getType()) > 64)
  198. return &GEP;
  199. if (!GEP.isInBounds())
  200. return &GEP;
  201. assert(!IsSigned && "The logic below only supports decomposition for "
  202. "unsinged predicates at the moment.");
  203. Type *PtrTy = GEP.getType()->getScalarType();
  204. unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
  205. MapVector<Value *, APInt> VariableOffsets;
  206. APInt ConstantOffset(BitWidth, 0);
  207. if (!GEP.collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
  208. return &GEP;
  209. // Handle the (gep (gep ....), C) case by incrementing the constant
  210. // coefficient of the inner GEP, if C is a constant.
  211. auto *InnerGEP = dyn_cast<GetElementPtrInst>(GEP.getPointerOperand());
  212. if (VariableOffsets.empty() && InnerGEP && InnerGEP->getNumOperands() == 2) {
  213. auto Result = decompose(InnerGEP, Preconditions, IsSigned, DL);
  214. Result.add(ConstantOffset.getSExtValue());
  215. if (ConstantOffset.isNegative()) {
  216. unsigned Scale = DL.getTypeAllocSize(InnerGEP->getResultElementType());
  217. int64_t ConstantOffsetI = ConstantOffset.getSExtValue();
  218. if (ConstantOffsetI % Scale != 0)
  219. return &GEP;
  220. // Add pre-condition ensuring the GEP is increasing monotonically and
  221. // can be de-composed.
  222. // Both sides are normalized by being divided by Scale.
  223. Preconditions.emplace_back(
  224. CmpInst::ICMP_SGE, InnerGEP->getOperand(1),
  225. ConstantInt::get(InnerGEP->getOperand(1)->getType(),
  226. -1 * (ConstantOffsetI / Scale)));
  227. }
  228. return Result;
  229. }
  230. Decomposition Result(ConstantOffset.getSExtValue(),
  231. DecompEntry(1, GEP.getPointerOperand()));
  232. for (auto [Index, Scale] : VariableOffsets) {
  233. auto IdxResult = decompose(Index, Preconditions, IsSigned, DL);
  234. IdxResult.mul(Scale.getSExtValue());
  235. Result.add(IdxResult);
  236. // If Op0 is signed non-negative, the GEP is increasing monotonically and
  237. // can be de-composed.
  238. if (!isKnownNonNegative(Index, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
  239. Preconditions.emplace_back(CmpInst::ICMP_SGE, Index,
  240. ConstantInt::get(Index->getType(), 0));
  241. }
  242. return Result;
  243. }
  244. // Decomposes \p V into a constant offset + list of pairs { Coefficient,
  245. // Variable } where Coefficient * Variable. The sum of the constant offset and
  246. // pairs equals \p V.
  247. static Decomposition decompose(Value *V,
  248. SmallVectorImpl<PreconditionTy> &Preconditions,
  249. bool IsSigned, const DataLayout &DL) {
  250. auto MergeResults = [&Preconditions, IsSigned, &DL](Value *A, Value *B,
  251. bool IsSignedB) {
  252. auto ResA = decompose(A, Preconditions, IsSigned, DL);
  253. auto ResB = decompose(B, Preconditions, IsSignedB, DL);
  254. ResA.add(ResB);
  255. return ResA;
  256. };
  257. // Decompose \p V used with a signed predicate.
  258. if (IsSigned) {
  259. if (auto *CI = dyn_cast<ConstantInt>(V)) {
  260. if (canUseSExt(CI))
  261. return CI->getSExtValue();
  262. }
  263. Value *Op0;
  264. Value *Op1;
  265. if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1))))
  266. return MergeResults(Op0, Op1, IsSigned);
  267. return V;
  268. }
  269. if (auto *CI = dyn_cast<ConstantInt>(V)) {
  270. if (CI->uge(MaxConstraintValue))
  271. return V;
  272. return int64_t(CI->getZExtValue());
  273. }
  274. if (auto *GEP = dyn_cast<GetElementPtrInst>(V))
  275. return decomposeGEP(*GEP, Preconditions, IsSigned, DL);
  276. Value *Op0;
  277. bool IsKnownNonNegative = false;
  278. if (match(V, m_ZExt(m_Value(Op0)))) {
  279. IsKnownNonNegative = true;
  280. V = Op0;
  281. }
  282. Value *Op1;
  283. ConstantInt *CI;
  284. if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1)))) {
  285. return MergeResults(Op0, Op1, IsSigned);
  286. }
  287. if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1)))) {
  288. if (!isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
  289. Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
  290. ConstantInt::get(Op0->getType(), 0));
  291. if (!isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
  292. Preconditions.emplace_back(CmpInst::ICMP_SGE, Op1,
  293. ConstantInt::get(Op1->getType(), 0));
  294. return MergeResults(Op0, Op1, IsSigned);
  295. }
  296. if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
  297. canUseSExt(CI)) {
  298. Preconditions.emplace_back(
  299. CmpInst::ICMP_UGE, Op0,
  300. ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
  301. return MergeResults(Op0, CI, true);
  302. }
  303. if (match(V, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI)) {
  304. int64_t Mult = int64_t(std::pow(int64_t(2), CI->getSExtValue()));
  305. auto Result = decompose(Op1, Preconditions, IsSigned, DL);
  306. Result.mul(Mult);
  307. return Result;
  308. }
  309. if (match(V, m_NUWMul(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI) &&
  310. (!CI->isNegative())) {
  311. auto Result = decompose(Op1, Preconditions, IsSigned, DL);
  312. Result.mul(CI->getSExtValue());
  313. return Result;
  314. }
  315. if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))) && canUseSExt(CI))
  316. return {-1 * CI->getSExtValue(), {{1, Op0}}};
  317. if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1))))
  318. return {0, {{1, Op0}, {-1, Op1}}};
  319. return {V, IsKnownNonNegative};
  320. }
  321. ConstraintTy
  322. ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
  323. SmallVectorImpl<Value *> &NewVariables) const {
  324. assert(NewVariables.empty() && "NewVariables must be empty when passed in");
  325. bool IsEq = false;
  326. // Try to convert Pred to one of ULE/SLT/SLE/SLT.
  327. switch (Pred) {
  328. case CmpInst::ICMP_UGT:
  329. case CmpInst::ICMP_UGE:
  330. case CmpInst::ICMP_SGT:
  331. case CmpInst::ICMP_SGE: {
  332. Pred = CmpInst::getSwappedPredicate(Pred);
  333. std::swap(Op0, Op1);
  334. break;
  335. }
  336. case CmpInst::ICMP_EQ:
  337. if (match(Op1, m_Zero())) {
  338. Pred = CmpInst::ICMP_ULE;
  339. } else {
  340. IsEq = true;
  341. Pred = CmpInst::ICMP_ULE;
  342. }
  343. break;
  344. case CmpInst::ICMP_NE:
  345. if (!match(Op1, m_Zero()))
  346. return {};
  347. Pred = CmpInst::getSwappedPredicate(CmpInst::ICMP_UGT);
  348. std::swap(Op0, Op1);
  349. break;
  350. default:
  351. break;
  352. }
  353. if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT &&
  354. Pred != CmpInst::ICMP_SLE && Pred != CmpInst::ICMP_SLT)
  355. return {};
  356. SmallVector<PreconditionTy, 4> Preconditions;
  357. bool IsSigned = CmpInst::isSigned(Pred);
  358. auto &Value2Index = getValue2Index(IsSigned);
  359. auto ADec = decompose(Op0->stripPointerCastsSameRepresentation(),
  360. Preconditions, IsSigned, DL);
  361. auto BDec = decompose(Op1->stripPointerCastsSameRepresentation(),
  362. Preconditions, IsSigned, DL);
  363. int64_t Offset1 = ADec.Offset;
  364. int64_t Offset2 = BDec.Offset;
  365. Offset1 *= -1;
  366. auto &VariablesA = ADec.Vars;
  367. auto &VariablesB = BDec.Vars;
  368. // First try to look up \p V in Value2Index and NewVariables. Otherwise add a
  369. // new entry to NewVariables.
  370. DenseMap<Value *, unsigned> NewIndexMap;
  371. auto GetOrAddIndex = [&Value2Index, &NewVariables,
  372. &NewIndexMap](Value *V) -> unsigned {
  373. auto V2I = Value2Index.find(V);
  374. if (V2I != Value2Index.end())
  375. return V2I->second;
  376. auto Insert =
  377. NewIndexMap.insert({V, Value2Index.size() + NewVariables.size() + 1});
  378. if (Insert.second)
  379. NewVariables.push_back(V);
  380. return Insert.first->second;
  381. };
  382. // Make sure all variables have entries in Value2Index or NewVariables.
  383. for (const auto &KV : concat<DecompEntry>(VariablesA, VariablesB))
  384. GetOrAddIndex(KV.Variable);
  385. // Build result constraint, by first adding all coefficients from A and then
  386. // subtracting all coefficients from B.
  387. ConstraintTy Res(
  388. SmallVector<int64_t, 8>(Value2Index.size() + NewVariables.size() + 1, 0),
  389. IsSigned);
  390. // Collect variables that are known to be positive in all uses in the
  391. // constraint.
  392. DenseMap<Value *, bool> KnownNonNegativeVariables;
  393. Res.IsEq = IsEq;
  394. auto &R = Res.Coefficients;
  395. for (const auto &KV : VariablesA) {
  396. R[GetOrAddIndex(KV.Variable)] += KV.Coefficient;
  397. auto I =
  398. KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
  399. I.first->second &= KV.IsKnownNonNegative;
  400. }
  401. for (const auto &KV : VariablesB) {
  402. R[GetOrAddIndex(KV.Variable)] -= KV.Coefficient;
  403. auto I =
  404. KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
  405. I.first->second &= KV.IsKnownNonNegative;
  406. }
  407. int64_t OffsetSum;
  408. if (AddOverflow(Offset1, Offset2, OffsetSum))
  409. return {};
  410. if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT))
  411. if (AddOverflow(OffsetSum, int64_t(-1), OffsetSum))
  412. return {};
  413. R[0] = OffsetSum;
  414. Res.Preconditions = std::move(Preconditions);
  415. // Remove any (Coefficient, Variable) entry where the Coefficient is 0 for new
  416. // variables.
  417. while (!NewVariables.empty()) {
  418. int64_t Last = R.back();
  419. if (Last != 0)
  420. break;
  421. R.pop_back();
  422. Value *RemovedV = NewVariables.pop_back_val();
  423. NewIndexMap.erase(RemovedV);
  424. }
  425. // Add extra constraints for variables that are known positive.
  426. for (auto &KV : KnownNonNegativeVariables) {
  427. if (!KV.second || (Value2Index.find(KV.first) == Value2Index.end() &&
  428. NewIndexMap.find(KV.first) == NewIndexMap.end()))
  429. continue;
  430. SmallVector<int64_t, 8> C(Value2Index.size() + NewVariables.size() + 1, 0);
  431. C[GetOrAddIndex(KV.first)] = -1;
  432. Res.ExtraInfo.push_back(C);
  433. }
  434. return Res;
  435. }
  436. ConstraintTy ConstraintInfo::getConstraintForSolving(CmpInst::Predicate Pred,
  437. Value *Op0,
  438. Value *Op1) const {
  439. // If both operands are known to be non-negative, change signed predicates to
  440. // unsigned ones. This increases the reasoning effectiveness in combination
  441. // with the signed <-> unsigned transfer logic.
  442. if (CmpInst::isSigned(Pred) &&
  443. isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1) &&
  444. isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
  445. Pred = CmpInst::getUnsignedPredicate(Pred);
  446. SmallVector<Value *> NewVariables;
  447. ConstraintTy R = getConstraint(Pred, Op0, Op1, NewVariables);
  448. if (R.IsEq || !NewVariables.empty())
  449. return {};
  450. return R;
  451. }
  452. bool ConstraintTy::isValid(const ConstraintInfo &Info) const {
  453. return Coefficients.size() > 0 &&
  454. all_of(Preconditions, [&Info](const PreconditionTy &C) {
  455. return Info.doesHold(C.Pred, C.Op0, C.Op1);
  456. });
  457. }
  458. bool ConstraintInfo::doesHold(CmpInst::Predicate Pred, Value *A,
  459. Value *B) const {
  460. auto R = getConstraintForSolving(Pred, A, B);
  461. return R.Preconditions.empty() && !R.empty() &&
  462. getCS(R.IsSigned).isConditionImplied(R.Coefficients);
  463. }
  464. void ConstraintInfo::transferToOtherSystem(
  465. CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
  466. unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack) {
  467. // Check if we can combine facts from the signed and unsigned systems to
  468. // derive additional facts.
  469. if (!A->getType()->isIntegerTy())
  470. return;
  471. // FIXME: This currently depends on the order we add facts. Ideally we
  472. // would first add all known facts and only then try to add additional
  473. // facts.
  474. switch (Pred) {
  475. default:
  476. break;
  477. case CmpInst::ICMP_ULT:
  478. // If B is a signed positive constant, A >=s 0 and A <s B.
  479. if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0))) {
  480. addFact(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0), NumIn,
  481. NumOut, DFSInStack);
  482. addFact(CmpInst::ICMP_SLT, A, B, NumIn, NumOut, DFSInStack);
  483. }
  484. break;
  485. case CmpInst::ICMP_SLT:
  486. if (doesHold(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0)))
  487. addFact(CmpInst::ICMP_ULT, A, B, NumIn, NumOut, DFSInStack);
  488. break;
  489. case CmpInst::ICMP_SGT:
  490. if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), -1)))
  491. addFact(CmpInst::ICMP_UGE, A, ConstantInt::get(B->getType(), 0), NumIn,
  492. NumOut, DFSInStack);
  493. break;
  494. case CmpInst::ICMP_SGE:
  495. if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0))) {
  496. addFact(CmpInst::ICMP_UGE, A, B, NumIn, NumOut, DFSInStack);
  497. }
  498. break;
  499. }
  500. }
  501. namespace {
  502. /// Represents either
  503. /// * a condition that holds on entry to a block (=conditional fact)
  504. /// * an assume (=assume fact)
  505. /// * an instruction to simplify.
  506. /// It also tracks the Dominator DFS in and out numbers for each entry.
  507. struct FactOrCheck {
  508. Instruction *Inst;
  509. unsigned NumIn;
  510. unsigned NumOut;
  511. bool IsCheck;
  512. bool Not;
  513. FactOrCheck(DomTreeNode *DTN, Instruction *Inst, bool IsCheck, bool Not)
  514. : Inst(Inst), NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()),
  515. IsCheck(IsCheck), Not(Not) {}
  516. static FactOrCheck getFact(DomTreeNode *DTN, Instruction *Inst,
  517. bool Not = false) {
  518. return FactOrCheck(DTN, Inst, false, Not);
  519. }
  520. static FactOrCheck getCheck(DomTreeNode *DTN, Instruction *Inst) {
  521. return FactOrCheck(DTN, Inst, true, false);
  522. }
  523. bool isAssumeFact() const {
  524. if (!IsCheck && isa<IntrinsicInst>(Inst)) {
  525. assert(match(Inst, m_Intrinsic<Intrinsic::assume>()));
  526. return true;
  527. }
  528. return false;
  529. }
  530. bool isConditionFact() const { return !IsCheck && isa<CmpInst>(Inst); }
  531. };
  532. /// Keep state required to build worklist.
  533. struct State {
  534. DominatorTree &DT;
  535. SmallVector<FactOrCheck, 64> WorkList;
  536. State(DominatorTree &DT) : DT(DT) {}
  537. /// Process block \p BB and add known facts to work-list.
  538. void addInfoFor(BasicBlock &BB);
  539. /// Returns true if we can add a known condition from BB to its successor
  540. /// block Succ.
  541. bool canAddSuccessor(BasicBlock &BB, BasicBlock *Succ) const {
  542. return DT.dominates(BasicBlockEdge(&BB, Succ), Succ);
  543. }
  544. };
  545. } // namespace
  546. #ifndef NDEBUG
  547. static void dumpWithNames(const ConstraintSystem &CS,
  548. DenseMap<Value *, unsigned> &Value2Index) {
  549. SmallVector<std::string> Names(Value2Index.size(), "");
  550. for (auto &KV : Value2Index) {
  551. Names[KV.second - 1] = std::string("%") + KV.first->getName().str();
  552. }
  553. CS.dump(Names);
  554. }
  555. static void dumpWithNames(ArrayRef<int64_t> C,
  556. DenseMap<Value *, unsigned> &Value2Index) {
  557. ConstraintSystem CS;
  558. CS.addVariableRowFill(C);
  559. dumpWithNames(CS, Value2Index);
  560. }
  561. #endif
  562. void State::addInfoFor(BasicBlock &BB) {
  563. // True as long as long as the current instruction is guaranteed to execute.
  564. bool GuaranteedToExecute = true;
  565. // Queue conditions and assumes.
  566. for (Instruction &I : BB) {
  567. if (auto Cmp = dyn_cast<ICmpInst>(&I)) {
  568. WorkList.push_back(FactOrCheck::getCheck(DT.getNode(&BB), Cmp));
  569. continue;
  570. }
  571. if (match(&I, m_Intrinsic<Intrinsic::ssub_with_overflow>())) {
  572. WorkList.push_back(FactOrCheck::getCheck(DT.getNode(&BB), &I));
  573. continue;
  574. }
  575. Value *Cond;
  576. // For now, just handle assumes with a single compare as condition.
  577. if (match(&I, m_Intrinsic<Intrinsic::assume>(m_Value(Cond))) &&
  578. isa<ICmpInst>(Cond)) {
  579. if (GuaranteedToExecute) {
  580. // The assume is guaranteed to execute when BB is entered, hence Cond
  581. // holds on entry to BB.
  582. WorkList.emplace_back(FactOrCheck::getFact(DT.getNode(I.getParent()),
  583. cast<Instruction>(Cond)));
  584. } else {
  585. WorkList.emplace_back(
  586. FactOrCheck::getFact(DT.getNode(I.getParent()), &I));
  587. }
  588. }
  589. GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
  590. }
  591. auto *Br = dyn_cast<BranchInst>(BB.getTerminator());
  592. if (!Br || !Br->isConditional())
  593. return;
  594. Value *Cond = Br->getCondition();
  595. // If the condition is a chain of ORs/AND and the successor only has the
  596. // current block as predecessor, queue conditions for the successor.
  597. Value *Op0, *Op1;
  598. if (match(Cond, m_LogicalOr(m_Value(Op0), m_Value(Op1))) ||
  599. match(Cond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
  600. bool IsOr = match(Cond, m_LogicalOr());
  601. bool IsAnd = match(Cond, m_LogicalAnd());
  602. // If there's a select that matches both AND and OR, we need to commit to
  603. // one of the options. Arbitrarily pick OR.
  604. if (IsOr && IsAnd)
  605. IsAnd = false;
  606. BasicBlock *Successor = Br->getSuccessor(IsOr ? 1 : 0);
  607. if (canAddSuccessor(BB, Successor)) {
  608. SmallVector<Value *> CondWorkList;
  609. SmallPtrSet<Value *, 8> SeenCond;
  610. auto QueueValue = [&CondWorkList, &SeenCond](Value *V) {
  611. if (SeenCond.insert(V).second)
  612. CondWorkList.push_back(V);
  613. };
  614. QueueValue(Op1);
  615. QueueValue(Op0);
  616. while (!CondWorkList.empty()) {
  617. Value *Cur = CondWorkList.pop_back_val();
  618. if (auto *Cmp = dyn_cast<ICmpInst>(Cur)) {
  619. WorkList.emplace_back(
  620. FactOrCheck::getFact(DT.getNode(Successor), Cmp, IsOr));
  621. continue;
  622. }
  623. if (IsOr && match(Cur, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) {
  624. QueueValue(Op1);
  625. QueueValue(Op0);
  626. continue;
  627. }
  628. if (IsAnd && match(Cur, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
  629. QueueValue(Op1);
  630. QueueValue(Op0);
  631. continue;
  632. }
  633. }
  634. }
  635. return;
  636. }
  637. auto *CmpI = dyn_cast<ICmpInst>(Br->getCondition());
  638. if (!CmpI)
  639. return;
  640. if (canAddSuccessor(BB, Br->getSuccessor(0)))
  641. WorkList.emplace_back(
  642. FactOrCheck::getFact(DT.getNode(Br->getSuccessor(0)), CmpI));
  643. if (canAddSuccessor(BB, Br->getSuccessor(1)))
  644. WorkList.emplace_back(
  645. FactOrCheck::getFact(DT.getNode(Br->getSuccessor(1)), CmpI, true));
  646. }
  647. static bool checkAndReplaceCondition(CmpInst *Cmp, ConstraintInfo &Info) {
  648. LLVM_DEBUG(dbgs() << "Checking " << *Cmp << "\n");
  649. CmpInst::Predicate Pred = Cmp->getPredicate();
  650. Value *A = Cmp->getOperand(0);
  651. Value *B = Cmp->getOperand(1);
  652. auto R = Info.getConstraintForSolving(Pred, A, B);
  653. if (R.empty() || !R.isValid(Info)){
  654. LLVM_DEBUG(dbgs() << " failed to decompose condition\n");
  655. return false;
  656. }
  657. auto &CSToUse = Info.getCS(R.IsSigned);
  658. // If there was extra information collected during decomposition, apply
  659. // it now and remove it immediately once we are done with reasoning
  660. // about the constraint.
  661. for (auto &Row : R.ExtraInfo)
  662. CSToUse.addVariableRow(Row);
  663. auto InfoRestorer = make_scope_exit([&]() {
  664. for (unsigned I = 0; I < R.ExtraInfo.size(); ++I)
  665. CSToUse.popLastConstraint();
  666. });
  667. bool Changed = false;
  668. if (CSToUse.isConditionImplied(R.Coefficients)) {
  669. if (!DebugCounter::shouldExecute(EliminatedCounter))
  670. return false;
  671. LLVM_DEBUG({
  672. dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n";
  673. dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned));
  674. });
  675. Constant *TrueC =
  676. ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType()));
  677. Cmp->replaceUsesWithIf(TrueC, [](Use &U) {
  678. // Conditions in an assume trivially simplify to true. Skip uses
  679. // in assume calls to not destroy the available information.
  680. auto *II = dyn_cast<IntrinsicInst>(U.getUser());
  681. return !II || II->getIntrinsicID() != Intrinsic::assume;
  682. });
  683. NumCondsRemoved++;
  684. Changed = true;
  685. }
  686. if (CSToUse.isConditionImplied(ConstraintSystem::negate(R.Coefficients))) {
  687. if (!DebugCounter::shouldExecute(EliminatedCounter))
  688. return false;
  689. LLVM_DEBUG({
  690. dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n";
  691. dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned));
  692. });
  693. Constant *FalseC =
  694. ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType()));
  695. Cmp->replaceAllUsesWith(FalseC);
  696. NumCondsRemoved++;
  697. Changed = true;
  698. }
  699. return Changed;
  700. }
  701. void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
  702. unsigned NumIn, unsigned NumOut,
  703. SmallVectorImpl<StackEntry> &DFSInStack) {
  704. // If the constraint has a pre-condition, skip the constraint if it does not
  705. // hold.
  706. SmallVector<Value *> NewVariables;
  707. auto R = getConstraint(Pred, A, B, NewVariables);
  708. if (!R.isValid(*this))
  709. return;
  710. LLVM_DEBUG(dbgs() << "Adding '" << CmpInst::getPredicateName(Pred) << " ";
  711. A->printAsOperand(dbgs(), false); dbgs() << ", ";
  712. B->printAsOperand(dbgs(), false); dbgs() << "'\n");
  713. bool Added = false;
  714. auto &CSToUse = getCS(R.IsSigned);
  715. if (R.Coefficients.empty())
  716. return;
  717. Added |= CSToUse.addVariableRowFill(R.Coefficients);
  718. // If R has been added to the system, add the new variables and queue it for
  719. // removal once it goes out-of-scope.
  720. if (Added) {
  721. SmallVector<Value *, 2> ValuesToRelease;
  722. auto &Value2Index = getValue2Index(R.IsSigned);
  723. for (Value *V : NewVariables) {
  724. Value2Index.insert({V, Value2Index.size() + 1});
  725. ValuesToRelease.push_back(V);
  726. }
  727. LLVM_DEBUG({
  728. dbgs() << " constraint: ";
  729. dumpWithNames(R.Coefficients, getValue2Index(R.IsSigned));
  730. dbgs() << "\n";
  731. });
  732. DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
  733. std::move(ValuesToRelease));
  734. if (R.IsEq) {
  735. // Also add the inverted constraint for equality constraints.
  736. for (auto &Coeff : R.Coefficients)
  737. Coeff *= -1;
  738. CSToUse.addVariableRowFill(R.Coefficients);
  739. DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
  740. SmallVector<Value *, 2>());
  741. }
  742. }
  743. }
  744. static bool replaceSubOverflowUses(IntrinsicInst *II, Value *A, Value *B,
  745. SmallVectorImpl<Instruction *> &ToRemove) {
  746. bool Changed = false;
  747. IRBuilder<> Builder(II->getParent(), II->getIterator());
  748. Value *Sub = nullptr;
  749. for (User *U : make_early_inc_range(II->users())) {
  750. if (match(U, m_ExtractValue<0>(m_Value()))) {
  751. if (!Sub)
  752. Sub = Builder.CreateSub(A, B);
  753. U->replaceAllUsesWith(Sub);
  754. Changed = true;
  755. } else if (match(U, m_ExtractValue<1>(m_Value()))) {
  756. U->replaceAllUsesWith(Builder.getFalse());
  757. Changed = true;
  758. } else
  759. continue;
  760. if (U->use_empty()) {
  761. auto *I = cast<Instruction>(U);
  762. ToRemove.push_back(I);
  763. I->setOperand(0, PoisonValue::get(II->getType()));
  764. Changed = true;
  765. }
  766. }
  767. if (II->use_empty()) {
  768. II->eraseFromParent();
  769. Changed = true;
  770. }
  771. return Changed;
  772. }
  773. static bool
  774. tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
  775. SmallVectorImpl<Instruction *> &ToRemove) {
  776. auto DoesConditionHold = [](CmpInst::Predicate Pred, Value *A, Value *B,
  777. ConstraintInfo &Info) {
  778. auto R = Info.getConstraintForSolving(Pred, A, B);
  779. if (R.size() < 2 || !R.isValid(Info))
  780. return false;
  781. auto &CSToUse = Info.getCS(R.IsSigned);
  782. return CSToUse.isConditionImplied(R.Coefficients);
  783. };
  784. bool Changed = false;
  785. if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
  786. // If A s>= B && B s>= 0, ssub.with.overflow(a, b) should not overflow and
  787. // can be simplified to a regular sub.
  788. Value *A = II->getArgOperand(0);
  789. Value *B = II->getArgOperand(1);
  790. if (!DoesConditionHold(CmpInst::ICMP_SGE, A, B, Info) ||
  791. !DoesConditionHold(CmpInst::ICMP_SGE, B,
  792. ConstantInt::get(A->getType(), 0), Info))
  793. return false;
  794. Changed = replaceSubOverflowUses(II, A, B, ToRemove);
  795. }
  796. return Changed;
  797. }
  798. static bool eliminateConstraints(Function &F, DominatorTree &DT) {
  799. bool Changed = false;
  800. DT.updateDFSNumbers();
  801. ConstraintInfo Info(F.getParent()->getDataLayout());
  802. State S(DT);
  803. // First, collect conditions implied by branches and blocks with their
  804. // Dominator DFS in and out numbers.
  805. for (BasicBlock &BB : F) {
  806. if (!DT.getNode(&BB))
  807. continue;
  808. S.addInfoFor(BB);
  809. }
  810. // Next, sort worklist by dominance, so that dominating conditions to check
  811. // and facts come before conditions and facts dominated by them. If a
  812. // condition to check and a fact have the same numbers, conditional facts come
  813. // first. Assume facts and checks are ordered according to their relative
  814. // order in the containing basic block. Also make sure conditions with
  815. // constant operands come before conditions without constant operands. This
  816. // increases the effectiveness of the current signed <-> unsigned fact
  817. // transfer logic.
  818. stable_sort(S.WorkList, [](const FactOrCheck &A, const FactOrCheck &B) {
  819. auto HasNoConstOp = [](const FactOrCheck &B) {
  820. return !isa<ConstantInt>(B.Inst->getOperand(0)) &&
  821. !isa<ConstantInt>(B.Inst->getOperand(1));
  822. };
  823. // If both entries have the same In numbers, conditional facts come first.
  824. // Otherwise use the relative order in the basic block.
  825. if (A.NumIn == B.NumIn) {
  826. if (A.isConditionFact() && B.isConditionFact()) {
  827. bool NoConstOpA = HasNoConstOp(A);
  828. bool NoConstOpB = HasNoConstOp(B);
  829. return NoConstOpA < NoConstOpB;
  830. }
  831. if (A.isConditionFact())
  832. return true;
  833. if (B.isConditionFact())
  834. return false;
  835. return A.Inst->comesBefore(B.Inst);
  836. }
  837. return A.NumIn < B.NumIn;
  838. });
  839. SmallVector<Instruction *> ToRemove;
  840. // Finally, process ordered worklist and eliminate implied conditions.
  841. SmallVector<StackEntry, 16> DFSInStack;
  842. for (FactOrCheck &CB : S.WorkList) {
  843. // First, pop entries from the stack that are out-of-scope for CB. Remove
  844. // the corresponding entry from the constraint system.
  845. while (!DFSInStack.empty()) {
  846. auto &E = DFSInStack.back();
  847. LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut
  848. << "\n");
  849. LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n");
  850. assert(E.NumIn <= CB.NumIn);
  851. if (CB.NumOut <= E.NumOut)
  852. break;
  853. LLVM_DEBUG({
  854. dbgs() << "Removing ";
  855. dumpWithNames(Info.getCS(E.IsSigned).getLastConstraint(),
  856. Info.getValue2Index(E.IsSigned));
  857. dbgs() << "\n";
  858. });
  859. Info.popLastConstraint(E.IsSigned);
  860. // Remove variables in the system that went out of scope.
  861. auto &Mapping = Info.getValue2Index(E.IsSigned);
  862. for (Value *V : E.ValuesToRelease)
  863. Mapping.erase(V);
  864. Info.popLastNVariables(E.IsSigned, E.ValuesToRelease.size());
  865. DFSInStack.pop_back();
  866. }
  867. LLVM_DEBUG({
  868. dbgs() << "Processing ";
  869. if (CB.IsCheck)
  870. dbgs() << "condition to simplify: " << *CB.Inst;
  871. else
  872. dbgs() << "fact to add to the system: " << *CB.Inst;
  873. dbgs() << "\n";
  874. });
  875. // For a block, check if any CmpInsts become known based on the current set
  876. // of constraints.
  877. if (CB.IsCheck) {
  878. if (auto *II = dyn_cast<WithOverflowInst>(CB.Inst)) {
  879. Changed |= tryToSimplifyOverflowMath(II, Info, ToRemove);
  880. } else if (auto *Cmp = dyn_cast<ICmpInst>(CB.Inst)) {
  881. Changed |= checkAndReplaceCondition(Cmp, Info);
  882. }
  883. continue;
  884. }
  885. ICmpInst::Predicate Pred;
  886. Value *A, *B;
  887. Value *Cmp = CB.Inst;
  888. match(Cmp, m_Intrinsic<Intrinsic::assume>(m_Value(Cmp)));
  889. if (match(Cmp, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
  890. if (Info.getCS(CmpInst::isSigned(Pred)).size() > MaxRows) {
  891. LLVM_DEBUG(
  892. dbgs()
  893. << "Skip adding constraint because system has too many rows.\n");
  894. continue;
  895. }
  896. // Use the inverse predicate if required.
  897. if (CB.Not)
  898. Pred = CmpInst::getInversePredicate(Pred);
  899. Info.addFact(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
  900. Info.transferToOtherSystem(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
  901. }
  902. }
  903. #ifndef NDEBUG
  904. unsigned SignedEntries =
  905. count_if(DFSInStack, [](const StackEntry &E) { return E.IsSigned; });
  906. assert(Info.getCS(false).size() == DFSInStack.size() - SignedEntries &&
  907. "updates to CS and DFSInStack are out of sync");
  908. assert(Info.getCS(true).size() == SignedEntries &&
  909. "updates to CS and DFSInStack are out of sync");
  910. #endif
  911. for (Instruction *I : ToRemove)
  912. I->eraseFromParent();
  913. return Changed;
  914. }
  915. PreservedAnalyses ConstraintEliminationPass::run(Function &F,
  916. FunctionAnalysisManager &AM) {
  917. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  918. if (!eliminateConstraints(F, DT))
  919. return PreservedAnalyses::all();
  920. PreservedAnalyses PA;
  921. PA.preserve<DominatorTreeAnalysis>();
  922. PA.preserveSet<CFGAnalyses>();
  923. return PA;
  924. }