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