AssumeBundleBuilder.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. //===- AssumeBundleBuilder.cpp - tools to preserve informations -*- C++ -*-===//
  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. #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
  9. #include "llvm/ADT/DepthFirstIterator.h"
  10. #include "llvm/ADT/MapVector.h"
  11. #include "llvm/ADT/Statistic.h"
  12. #include "llvm/Analysis/AssumeBundleQueries.h"
  13. #include "llvm/Analysis/AssumptionCache.h"
  14. #include "llvm/Analysis/ValueTracking.h"
  15. #include "llvm/IR/Dominators.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/InstIterator.h"
  18. #include "llvm/IR/IntrinsicInst.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/Operator.h"
  21. #include "llvm/InitializePasses.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/DebugCounter.h"
  24. #include "llvm/Transforms/Utils/Local.h"
  25. using namespace llvm;
  26. namespace llvm {
  27. cl::opt<bool> ShouldPreserveAllAttributes(
  28. "assume-preserve-all", cl::init(false), cl::Hidden,
  29. cl::desc("enable preservation of all attrbitues. even those that are "
  30. "unlikely to be usefull"));
  31. cl::opt<bool> EnableKnowledgeRetention(
  32. "enable-knowledge-retention", cl::init(false), cl::Hidden,
  33. cl::desc(
  34. "enable preservation of attributes throughout code transformation"));
  35. } // namespace llvm
  36. #define DEBUG_TYPE "assume-builder"
  37. STATISTIC(NumAssumeBuilt, "Number of assume built by the assume builder");
  38. STATISTIC(NumBundlesInAssumes, "Total number of Bundles in the assume built");
  39. STATISTIC(NumAssumesMerged,
  40. "Number of assume merged by the assume simplify pass");
  41. STATISTIC(NumAssumesRemoved,
  42. "Number of assume removed by the assume simplify pass");
  43. DEBUG_COUNTER(BuildAssumeCounter, "assume-builder-counter",
  44. "Controls which assumes gets created");
  45. namespace {
  46. bool isUsefullToPreserve(Attribute::AttrKind Kind) {
  47. switch (Kind) {
  48. case Attribute::NonNull:
  49. case Attribute::NoUndef:
  50. case Attribute::Alignment:
  51. case Attribute::Dereferenceable:
  52. case Attribute::DereferenceableOrNull:
  53. case Attribute::Cold:
  54. return true;
  55. default:
  56. return false;
  57. }
  58. }
  59. /// This function will try to transform the given knowledge into a more
  60. /// canonical one. the canonical knowledge maybe the given one.
  61. RetainedKnowledge canonicalizedKnowledge(RetainedKnowledge RK,
  62. const DataLayout &DL) {
  63. switch (RK.AttrKind) {
  64. default:
  65. return RK;
  66. case Attribute::NonNull:
  67. RK.WasOn = getUnderlyingObject(RK.WasOn);
  68. return RK;
  69. case Attribute::Alignment: {
  70. Value *V = RK.WasOn->stripInBoundsOffsets([&](const Value *Strip) {
  71. if (auto *GEP = dyn_cast<GEPOperator>(Strip))
  72. RK.ArgValue =
  73. MinAlign(RK.ArgValue, GEP->getMaxPreservedAlignment(DL).value());
  74. });
  75. RK.WasOn = V;
  76. return RK;
  77. }
  78. case Attribute::Dereferenceable:
  79. case Attribute::DereferenceableOrNull: {
  80. int64_t Offset = 0;
  81. Value *V = GetPointerBaseWithConstantOffset(RK.WasOn, Offset, DL,
  82. /*AllowNonInBounds*/ false);
  83. if (Offset < 0)
  84. return RK;
  85. RK.ArgValue = RK.ArgValue + Offset;
  86. RK.WasOn = V;
  87. }
  88. }
  89. return RK;
  90. }
  91. /// This class contain all knowledge that have been gather while building an
  92. /// llvm.assume and the function to manipulate it.
  93. struct AssumeBuilderState {
  94. Module *M;
  95. using MapKey = std::pair<Value *, Attribute::AttrKind>;
  96. SmallMapVector<MapKey, uint64_t, 8> AssumedKnowledgeMap;
  97. Instruction *InstBeingModified = nullptr;
  98. AssumptionCache* AC = nullptr;
  99. DominatorTree* DT = nullptr;
  100. AssumeBuilderState(Module *M, Instruction *I = nullptr,
  101. AssumptionCache *AC = nullptr, DominatorTree *DT = nullptr)
  102. : M(M), InstBeingModified(I), AC(AC), DT(DT) {}
  103. bool tryToPreserveWithoutAddingAssume(RetainedKnowledge RK) {
  104. if (!InstBeingModified || !RK.WasOn)
  105. return false;
  106. bool HasBeenPreserved = false;
  107. Use* ToUpdate = nullptr;
  108. getKnowledgeForValue(
  109. RK.WasOn, {RK.AttrKind}, AC,
  110. [&](RetainedKnowledge RKOther, Instruction *Assume,
  111. const CallInst::BundleOpInfo *Bundle) {
  112. if (!isValidAssumeForContext(Assume, InstBeingModified, DT))
  113. return false;
  114. if (RKOther.ArgValue >= RK.ArgValue) {
  115. HasBeenPreserved = true;
  116. return true;
  117. } else if (isValidAssumeForContext(InstBeingModified, Assume, DT)) {
  118. HasBeenPreserved = true;
  119. IntrinsicInst *Intr = cast<IntrinsicInst>(Assume);
  120. ToUpdate = &Intr->op_begin()[Bundle->Begin + ABA_Argument];
  121. return true;
  122. }
  123. return false;
  124. });
  125. if (ToUpdate)
  126. ToUpdate->set(
  127. ConstantInt::get(Type::getInt64Ty(M->getContext()), RK.ArgValue));
  128. return HasBeenPreserved;
  129. }
  130. bool isKnowledgeWorthPreserving(RetainedKnowledge RK) {
  131. if (!RK)
  132. return false;
  133. if (!RK.WasOn)
  134. return true;
  135. if (RK.WasOn->getType()->isPointerTy()) {
  136. Value *UnderlyingPtr = getUnderlyingObject(RK.WasOn);
  137. if (isa<AllocaInst>(UnderlyingPtr) || isa<GlobalValue>(UnderlyingPtr))
  138. return false;
  139. }
  140. if (auto *Arg = dyn_cast<Argument>(RK.WasOn)) {
  141. if (Arg->hasAttribute(RK.AttrKind) &&
  142. (!Attribute::isIntAttrKind(RK.AttrKind) ||
  143. Arg->getAttribute(RK.AttrKind).getValueAsInt() >= RK.ArgValue))
  144. return false;
  145. return true;
  146. }
  147. if (auto *Inst = dyn_cast<Instruction>(RK.WasOn))
  148. if (wouldInstructionBeTriviallyDead(Inst)) {
  149. if (RK.WasOn->use_empty())
  150. return false;
  151. Use *SingleUse = RK.WasOn->getSingleUndroppableUse();
  152. if (SingleUse && SingleUse->getUser() == InstBeingModified)
  153. return false;
  154. }
  155. return true;
  156. }
  157. void addKnowledge(RetainedKnowledge RK) {
  158. RK = canonicalizedKnowledge(RK, M->getDataLayout());
  159. if (!isKnowledgeWorthPreserving(RK))
  160. return;
  161. if (tryToPreserveWithoutAddingAssume(RK))
  162. return;
  163. MapKey Key{RK.WasOn, RK.AttrKind};
  164. auto Lookup = AssumedKnowledgeMap.find(Key);
  165. if (Lookup == AssumedKnowledgeMap.end()) {
  166. AssumedKnowledgeMap[Key] = RK.ArgValue;
  167. return;
  168. }
  169. assert(((Lookup->second == 0 && RK.ArgValue == 0) ||
  170. (Lookup->second != 0 && RK.ArgValue != 0)) &&
  171. "inconsistent argument value");
  172. /// This is only desirable because for all attributes taking an argument
  173. /// higher is better.
  174. Lookup->second = std::max(Lookup->second, RK.ArgValue);
  175. }
  176. void addAttribute(Attribute Attr, Value *WasOn) {
  177. if (Attr.isTypeAttribute() || Attr.isStringAttribute() ||
  178. (!ShouldPreserveAllAttributes &&
  179. !isUsefullToPreserve(Attr.getKindAsEnum())))
  180. return;
  181. uint64_t AttrArg = 0;
  182. if (Attr.isIntAttribute())
  183. AttrArg = Attr.getValueAsInt();
  184. addKnowledge({Attr.getKindAsEnum(), AttrArg, WasOn});
  185. }
  186. void addCall(const CallBase *Call) {
  187. auto addAttrList = [&](AttributeList AttrList, unsigned NumArgs) {
  188. for (unsigned Idx = 0; Idx < NumArgs; Idx++)
  189. for (Attribute Attr : AttrList.getParamAttrs(Idx)) {
  190. bool IsPoisonAttr = Attr.hasAttribute(Attribute::NonNull) ||
  191. Attr.hasAttribute(Attribute::Alignment);
  192. if (!IsPoisonAttr || Call->isPassingUndefUB(Idx))
  193. addAttribute(Attr, Call->getArgOperand(Idx));
  194. }
  195. for (Attribute Attr : AttrList.getFnAttrs())
  196. addAttribute(Attr, nullptr);
  197. };
  198. addAttrList(Call->getAttributes(), Call->arg_size());
  199. if (Function *Fn = Call->getCalledFunction())
  200. addAttrList(Fn->getAttributes(), Fn->arg_size());
  201. }
  202. AssumeInst *build() {
  203. if (AssumedKnowledgeMap.empty())
  204. return nullptr;
  205. if (!DebugCounter::shouldExecute(BuildAssumeCounter))
  206. return nullptr;
  207. Function *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
  208. LLVMContext &C = M->getContext();
  209. SmallVector<OperandBundleDef, 8> OpBundle;
  210. for (auto &MapElem : AssumedKnowledgeMap) {
  211. SmallVector<Value *, 2> Args;
  212. if (MapElem.first.first)
  213. Args.push_back(MapElem.first.first);
  214. /// This is only valid because for all attribute that currently exist a
  215. /// value of 0 is useless. and should not be preserved.
  216. if (MapElem.second)
  217. Args.push_back(ConstantInt::get(Type::getInt64Ty(M->getContext()),
  218. MapElem.second));
  219. OpBundle.push_back(OperandBundleDefT<Value *>(
  220. std::string(Attribute::getNameFromAttrKind(MapElem.first.second)),
  221. Args));
  222. NumBundlesInAssumes++;
  223. }
  224. NumAssumeBuilt++;
  225. return cast<AssumeInst>(CallInst::Create(
  226. FnAssume, ArrayRef<Value *>({ConstantInt::getTrue(C)}), OpBundle));
  227. }
  228. void addAccessedPtr(Instruction *MemInst, Value *Pointer, Type *AccType,
  229. MaybeAlign MA) {
  230. unsigned DerefSize = MemInst->getModule()
  231. ->getDataLayout()
  232. .getTypeStoreSize(AccType)
  233. .getKnownMinValue();
  234. if (DerefSize != 0) {
  235. addKnowledge({Attribute::Dereferenceable, DerefSize, Pointer});
  236. if (!NullPointerIsDefined(MemInst->getFunction(),
  237. Pointer->getType()->getPointerAddressSpace()))
  238. addKnowledge({Attribute::NonNull, 0u, Pointer});
  239. }
  240. if (MA.valueOrOne() > 1)
  241. addKnowledge({Attribute::Alignment, MA.valueOrOne().value(), Pointer});
  242. }
  243. void addInstruction(Instruction *I) {
  244. if (auto *Call = dyn_cast<CallBase>(I))
  245. return addCall(Call);
  246. if (auto *Load = dyn_cast<LoadInst>(I))
  247. return addAccessedPtr(I, Load->getPointerOperand(), Load->getType(),
  248. Load->getAlign());
  249. if (auto *Store = dyn_cast<StoreInst>(I))
  250. return addAccessedPtr(I, Store->getPointerOperand(),
  251. Store->getValueOperand()->getType(),
  252. Store->getAlign());
  253. // TODO: Add support for the other Instructions.
  254. // TODO: Maybe we should look around and merge with other llvm.assume.
  255. }
  256. };
  257. } // namespace
  258. AssumeInst *llvm::buildAssumeFromInst(Instruction *I) {
  259. if (!EnableKnowledgeRetention)
  260. return nullptr;
  261. AssumeBuilderState Builder(I->getModule());
  262. Builder.addInstruction(I);
  263. return Builder.build();
  264. }
  265. void llvm::salvageKnowledge(Instruction *I, AssumptionCache *AC,
  266. DominatorTree *DT) {
  267. if (!EnableKnowledgeRetention || I->isTerminator())
  268. return;
  269. AssumeBuilderState Builder(I->getModule(), I, AC, DT);
  270. Builder.addInstruction(I);
  271. if (auto *Intr = Builder.build()) {
  272. Intr->insertBefore(I);
  273. if (AC)
  274. AC->registerAssumption(Intr);
  275. }
  276. }
  277. AssumeInst *
  278. llvm::buildAssumeFromKnowledge(ArrayRef<RetainedKnowledge> Knowledge,
  279. Instruction *CtxI, AssumptionCache *AC,
  280. DominatorTree *DT) {
  281. AssumeBuilderState Builder(CtxI->getModule(), CtxI, AC, DT);
  282. for (const RetainedKnowledge &RK : Knowledge)
  283. Builder.addKnowledge(RK);
  284. return Builder.build();
  285. }
  286. RetainedKnowledge llvm::simplifyRetainedKnowledge(AssumeInst *Assume,
  287. RetainedKnowledge RK,
  288. AssumptionCache *AC,
  289. DominatorTree *DT) {
  290. AssumeBuilderState Builder(Assume->getModule(), Assume, AC, DT);
  291. RK = canonicalizedKnowledge(RK, Assume->getModule()->getDataLayout());
  292. if (!Builder.isKnowledgeWorthPreserving(RK))
  293. return RetainedKnowledge::none();
  294. if (Builder.tryToPreserveWithoutAddingAssume(RK))
  295. return RetainedKnowledge::none();
  296. return RK;
  297. }
  298. namespace {
  299. struct AssumeSimplify {
  300. Function &F;
  301. AssumptionCache &AC;
  302. DominatorTree *DT;
  303. LLVMContext &C;
  304. SmallDenseSet<IntrinsicInst *> CleanupToDo;
  305. StringMapEntry<uint32_t> *IgnoreTag;
  306. SmallDenseMap<BasicBlock *, SmallVector<IntrinsicInst *, 4>, 8> BBToAssume;
  307. bool MadeChange = false;
  308. AssumeSimplify(Function &F, AssumptionCache &AC, DominatorTree *DT,
  309. LLVMContext &C)
  310. : F(F), AC(AC), DT(DT), C(C),
  311. IgnoreTag(C.getOrInsertBundleTag(IgnoreBundleTag)) {}
  312. void buildMapping(bool FilterBooleanArgument) {
  313. BBToAssume.clear();
  314. for (Value *V : AC.assumptions()) {
  315. if (!V)
  316. continue;
  317. IntrinsicInst *Assume = cast<IntrinsicInst>(V);
  318. if (FilterBooleanArgument) {
  319. auto *Arg = dyn_cast<ConstantInt>(Assume->getOperand(0));
  320. if (!Arg || Arg->isZero())
  321. continue;
  322. }
  323. BBToAssume[Assume->getParent()].push_back(Assume);
  324. }
  325. for (auto &Elem : BBToAssume) {
  326. llvm::sort(Elem.second,
  327. [](const IntrinsicInst *LHS, const IntrinsicInst *RHS) {
  328. return LHS->comesBefore(RHS);
  329. });
  330. }
  331. }
  332. /// Remove all asumes in CleanupToDo if there boolean argument is true and
  333. /// ForceCleanup is set or the assume doesn't hold valuable knowledge.
  334. void RunCleanup(bool ForceCleanup) {
  335. for (IntrinsicInst *Assume : CleanupToDo) {
  336. auto *Arg = dyn_cast<ConstantInt>(Assume->getOperand(0));
  337. if (!Arg || Arg->isZero() ||
  338. (!ForceCleanup &&
  339. !isAssumeWithEmptyBundle(cast<AssumeInst>(*Assume))))
  340. continue;
  341. MadeChange = true;
  342. if (ForceCleanup)
  343. NumAssumesMerged++;
  344. else
  345. NumAssumesRemoved++;
  346. Assume->eraseFromParent();
  347. }
  348. CleanupToDo.clear();
  349. }
  350. /// Remove knowledge stored in assume when it is already know by an attribute
  351. /// or an other assume. This can when valid update an existing knowledge in an
  352. /// attribute or an other assume.
  353. void dropRedundantKnowledge() {
  354. struct MapValue {
  355. IntrinsicInst *Assume;
  356. uint64_t ArgValue;
  357. CallInst::BundleOpInfo *BOI;
  358. };
  359. buildMapping(false);
  360. SmallDenseMap<std::pair<Value *, Attribute::AttrKind>,
  361. SmallVector<MapValue, 2>, 16>
  362. Knowledge;
  363. for (BasicBlock *BB : depth_first(&F))
  364. for (Value *V : BBToAssume[BB]) {
  365. if (!V)
  366. continue;
  367. IntrinsicInst *Assume = cast<IntrinsicInst>(V);
  368. for (CallInst::BundleOpInfo &BOI : Assume->bundle_op_infos()) {
  369. auto RemoveFromAssume = [&]() {
  370. CleanupToDo.insert(Assume);
  371. if (BOI.Begin != BOI.End) {
  372. Use *U = &Assume->op_begin()[BOI.Begin + ABA_WasOn];
  373. U->set(UndefValue::get(U->get()->getType()));
  374. }
  375. BOI.Tag = IgnoreTag;
  376. };
  377. if (BOI.Tag == IgnoreTag) {
  378. CleanupToDo.insert(Assume);
  379. continue;
  380. }
  381. RetainedKnowledge RK =
  382. getKnowledgeFromBundle(cast<AssumeInst>(*Assume), BOI);
  383. if (auto *Arg = dyn_cast_or_null<Argument>(RK.WasOn)) {
  384. bool HasSameKindAttr = Arg->hasAttribute(RK.AttrKind);
  385. if (HasSameKindAttr)
  386. if (!Attribute::isIntAttrKind(RK.AttrKind) ||
  387. Arg->getAttribute(RK.AttrKind).getValueAsInt() >=
  388. RK.ArgValue) {
  389. RemoveFromAssume();
  390. continue;
  391. }
  392. if (isValidAssumeForContext(
  393. Assume, &*F.getEntryBlock().getFirstInsertionPt()) ||
  394. Assume == &*F.getEntryBlock().getFirstInsertionPt()) {
  395. if (HasSameKindAttr)
  396. Arg->removeAttr(RK.AttrKind);
  397. Arg->addAttr(Attribute::get(C, RK.AttrKind, RK.ArgValue));
  398. MadeChange = true;
  399. RemoveFromAssume();
  400. continue;
  401. }
  402. }
  403. auto &Lookup = Knowledge[{RK.WasOn, RK.AttrKind}];
  404. for (MapValue &Elem : Lookup) {
  405. if (!isValidAssumeForContext(Elem.Assume, Assume, DT))
  406. continue;
  407. if (Elem.ArgValue >= RK.ArgValue) {
  408. RemoveFromAssume();
  409. continue;
  410. } else if (isValidAssumeForContext(Assume, Elem.Assume, DT)) {
  411. Elem.Assume->op_begin()[Elem.BOI->Begin + ABA_Argument].set(
  412. ConstantInt::get(Type::getInt64Ty(C), RK.ArgValue));
  413. MadeChange = true;
  414. RemoveFromAssume();
  415. continue;
  416. }
  417. }
  418. Lookup.push_back({Assume, RK.ArgValue, &BOI});
  419. }
  420. }
  421. }
  422. using MergeIterator = SmallVectorImpl<IntrinsicInst *>::iterator;
  423. /// Merge all Assumes from Begin to End in and insert the resulting assume as
  424. /// high as possible in the basicblock.
  425. void mergeRange(BasicBlock *BB, MergeIterator Begin, MergeIterator End) {
  426. if (Begin == End || std::next(Begin) == End)
  427. return;
  428. /// Provide no additional information so that AssumeBuilderState doesn't
  429. /// try to do any punning since it already has been done better.
  430. AssumeBuilderState Builder(F.getParent());
  431. /// For now it is initialized to the best value it could have
  432. Instruction *InsertPt = BB->getFirstNonPHI();
  433. if (isa<LandingPadInst>(InsertPt))
  434. InsertPt = InsertPt->getNextNode();
  435. for (IntrinsicInst *I : make_range(Begin, End)) {
  436. CleanupToDo.insert(I);
  437. for (CallInst::BundleOpInfo &BOI : I->bundle_op_infos()) {
  438. RetainedKnowledge RK =
  439. getKnowledgeFromBundle(cast<AssumeInst>(*I), BOI);
  440. if (!RK)
  441. continue;
  442. Builder.addKnowledge(RK);
  443. if (auto *I = dyn_cast_or_null<Instruction>(RK.WasOn))
  444. if (I->getParent() == InsertPt->getParent() &&
  445. (InsertPt->comesBefore(I) || InsertPt == I))
  446. InsertPt = I->getNextNode();
  447. }
  448. }
  449. /// Adjust InsertPt if it is before Begin, since mergeAssumes only
  450. /// guarantees we can place the resulting assume between Begin and End.
  451. if (InsertPt->comesBefore(*Begin))
  452. for (auto It = (*Begin)->getIterator(), E = InsertPt->getIterator();
  453. It != E; --It)
  454. if (!isGuaranteedToTransferExecutionToSuccessor(&*It)) {
  455. InsertPt = It->getNextNode();
  456. break;
  457. }
  458. auto *MergedAssume = Builder.build();
  459. if (!MergedAssume)
  460. return;
  461. MadeChange = true;
  462. MergedAssume->insertBefore(InsertPt);
  463. AC.registerAssumption(MergedAssume);
  464. }
  465. /// Merge assume when they are in the same BasicBlock and for all instruction
  466. /// between them isGuaranteedToTransferExecutionToSuccessor returns true.
  467. void mergeAssumes() {
  468. buildMapping(true);
  469. SmallVector<MergeIterator, 4> SplitPoints;
  470. for (auto &Elem : BBToAssume) {
  471. SmallVectorImpl<IntrinsicInst *> &AssumesInBB = Elem.second;
  472. if (AssumesInBB.size() < 2)
  473. continue;
  474. /// AssumesInBB is already sorted by order in the block.
  475. BasicBlock::iterator It = AssumesInBB.front()->getIterator();
  476. BasicBlock::iterator E = AssumesInBB.back()->getIterator();
  477. SplitPoints.push_back(AssumesInBB.begin());
  478. MergeIterator LastSplit = AssumesInBB.begin();
  479. for (; It != E; ++It)
  480. if (!isGuaranteedToTransferExecutionToSuccessor(&*It)) {
  481. for (; (*LastSplit)->comesBefore(&*It); ++LastSplit)
  482. ;
  483. if (SplitPoints.back() != LastSplit)
  484. SplitPoints.push_back(LastSplit);
  485. }
  486. SplitPoints.push_back(AssumesInBB.end());
  487. for (auto SplitIt = SplitPoints.begin();
  488. SplitIt != std::prev(SplitPoints.end()); SplitIt++) {
  489. mergeRange(Elem.first, *SplitIt, *(SplitIt + 1));
  490. }
  491. SplitPoints.clear();
  492. }
  493. }
  494. };
  495. bool simplifyAssumes(Function &F, AssumptionCache *AC, DominatorTree *DT) {
  496. AssumeSimplify AS(F, *AC, DT, F.getContext());
  497. /// Remove knowledge that is already known by a dominating other assume or an
  498. /// attribute.
  499. AS.dropRedundantKnowledge();
  500. /// Remove assume that are empty.
  501. AS.RunCleanup(false);
  502. /// Merge assume in the same basicblock when possible.
  503. AS.mergeAssumes();
  504. /// Remove assume that were merged.
  505. AS.RunCleanup(true);
  506. return AS.MadeChange;
  507. }
  508. } // namespace
  509. PreservedAnalyses AssumeSimplifyPass::run(Function &F,
  510. FunctionAnalysisManager &AM) {
  511. if (!EnableKnowledgeRetention)
  512. return PreservedAnalyses::all();
  513. simplifyAssumes(F, &AM.getResult<AssumptionAnalysis>(F),
  514. AM.getCachedResult<DominatorTreeAnalysis>(F));
  515. return PreservedAnalyses::all();
  516. }
  517. namespace {
  518. class AssumeSimplifyPassLegacyPass : public FunctionPass {
  519. public:
  520. static char ID;
  521. AssumeSimplifyPassLegacyPass() : FunctionPass(ID) {
  522. initializeAssumeSimplifyPassLegacyPassPass(
  523. *PassRegistry::getPassRegistry());
  524. }
  525. bool runOnFunction(Function &F) override {
  526. if (skipFunction(F) || !EnableKnowledgeRetention)
  527. return false;
  528. AssumptionCache &AC =
  529. getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  530. DominatorTreeWrapperPass *DTWP =
  531. getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  532. return simplifyAssumes(F, &AC, DTWP ? &DTWP->getDomTree() : nullptr);
  533. }
  534. void getAnalysisUsage(AnalysisUsage &AU) const override {
  535. AU.addRequired<AssumptionCacheTracker>();
  536. AU.setPreservesAll();
  537. }
  538. };
  539. } // namespace
  540. char AssumeSimplifyPassLegacyPass::ID = 0;
  541. INITIALIZE_PASS_BEGIN(AssumeSimplifyPassLegacyPass, "assume-simplify",
  542. "Assume Simplify", false, false)
  543. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  544. INITIALIZE_PASS_END(AssumeSimplifyPassLegacyPass, "assume-simplify",
  545. "Assume Simplify", false, false)
  546. FunctionPass *llvm::createAssumeSimplifyPass() {
  547. return new AssumeSimplifyPassLegacyPass();
  548. }
  549. PreservedAnalyses AssumeBuilderPass::run(Function &F,
  550. FunctionAnalysisManager &AM) {
  551. AssumptionCache *AC = &AM.getResult<AssumptionAnalysis>(F);
  552. DominatorTree* DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
  553. for (Instruction &I : instructions(F))
  554. salvageKnowledge(&I, AC, DT);
  555. return PreservedAnalyses::all();
  556. }
  557. namespace {
  558. class AssumeBuilderPassLegacyPass : public FunctionPass {
  559. public:
  560. static char ID;
  561. AssumeBuilderPassLegacyPass() : FunctionPass(ID) {
  562. initializeAssumeBuilderPassLegacyPassPass(*PassRegistry::getPassRegistry());
  563. }
  564. bool runOnFunction(Function &F) override {
  565. AssumptionCache &AC =
  566. getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  567. DominatorTreeWrapperPass *DTWP =
  568. getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  569. for (Instruction &I : instructions(F))
  570. salvageKnowledge(&I, &AC, DTWP ? &DTWP->getDomTree() : nullptr);
  571. return true;
  572. }
  573. void getAnalysisUsage(AnalysisUsage &AU) const override {
  574. AU.addRequired<AssumptionCacheTracker>();
  575. AU.setPreservesAll();
  576. }
  577. };
  578. } // namespace
  579. char AssumeBuilderPassLegacyPass::ID = 0;
  580. INITIALIZE_PASS_BEGIN(AssumeBuilderPassLegacyPass, "assume-builder",
  581. "Assume Builder", false, false)
  582. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  583. INITIALIZE_PASS_END(AssumeBuilderPassLegacyPass, "assume-builder",
  584. "Assume Builder", false, false)