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