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