[llvm] Port CodeGenPrepare to new pass manager (PR #68530)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 9 07:26:42 PDT 2023


================
@@ -486,72 +432,185 @@ class CodeGenPrepare : public FunctionPass {
   bool combineToUSubWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
   bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
   void verifyBFIUpdates(Function &F);
+
+  void releaseMemory() {
+    // Clear per function information.
+    InsertedInsts.clear();
+    PromotedInsts.clear();
+    FreshBBs.clear();
+    BPI.reset();
+    BFI.reset();
+  }
+  bool run(Function &F, const TargetMachine *TM,
+                              const TargetTransformInfo &TTI,
+                              LoopInfo &LI, ProfileSummaryInfo &PSI, 
+                              const TargetLibraryInfo &TLInfo);
+
+private:
+  template <typename F>
+  void resetIteratorIfInvalidatedWhileCalling(BasicBlock *BB, F f) {
+    // Substituting can cause recursive simplifications, which can invalidate
+    // our iterator.  Use a WeakTrackingVH to hold onto it in case this
+    // happens.
+    Value *CurValue = &*CurInstIterator;
+    WeakTrackingVH IterHandle(CurValue);
+
+    f();
+
+    // If the iterator instruction was recursively deleted, start over at the
+    // start of the block.
+    if (IterHandle != CurValue) {
+      CurInstIterator = BB->begin();
+      SunkAddrs.clear();
+    }
+  }
+
+  // Get the DominatorTree, building if necessary.
+  DominatorTree &getDT(Function &F) {
+    if (!DT)
+      DT = std::make_unique<DominatorTree>(F);
+    return *DT;
+  }
+};
+
+ class CodeGenPrepareLegacy : public FunctionPass {
+ public:
+    static char ID; // Pass identification, replacement for typeid
+    bool runOnFunction(Function &F) override;
+    const TargetMachine *TM = nullptr;
+    const TargetSubtargetInfo *SubtargetInfo = nullptr;
+    const TargetLowering *TLI = nullptr;
+    const TargetRegisterInfo *TRI = nullptr;
+    const TargetTransformInfo *TTI = nullptr;
+    const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+    const TargetLibraryInfo *TLInfo = nullptr;
+    LoopInfo *LI = nullptr;
+    std::unique_ptr<BlockFrequencyInfo> BFI;
+    std::unique_ptr<BranchProbabilityInfo> BPI;
+    ProfileSummaryInfo *PSI = nullptr;
+    StringRef getPassName() const { return "CodeGen Prepare"; }
+
+    void getAnalysisUsage(AnalysisUsage &AU) const {
+      // FIXME: When we can selectively preserve passes, preserve the domtree.
+      AU.addRequired<ProfileSummaryInfoWrapperPass>();
+      AU.addRequired<TargetLibraryInfoWrapperPass>();
+      AU.addRequired<TargetPassConfig>();
+      AU.addRequired<TargetTransformInfoWrapperPass>();
+      AU.addRequired<LoopInfoWrapperPass>();
+      AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>();
+    }
+
+    CodeGenPrepareLegacy() : FunctionPass(ID) {
+      initializeCodeGenPrepareLegacyPass(*PassRegistry::getPassRegistry());
+    }
 };
 
 } // end anonymous namespace
 
-char CodeGenPrepare::ID = 0;
+char CodeGenPrepareLegacy::ID = 0;
 
-INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE,
+INITIALIZE_PASS_BEGIN(CodeGenPrepareLegacy, DEBUG_TYPE,
                       "Optimize for code generation", false, false)
 INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReader)
 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
-INITIALIZE_PASS_END(CodeGenPrepare, DEBUG_TYPE, "Optimize for code generation",
+INITIALIZE_PASS_END(CodeGenPrepareLegacy, DEBUG_TYPE, "Optimize for code generation",
                     false, false)
 
-FunctionPass *llvm::createCodeGenPreparePass() { return new CodeGenPrepare(); }
+FunctionPass *llvm::createCodeGenPreparePass() {
+ return new CodeGenPrepareLegacy();
+}
 
-bool CodeGenPrepare::runOnFunction(Function &F) {
+bool CodeGenPrepareLegacy::runOnFunction(Function &F) {
   if (skipFunction(F))
     return false;
+  auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
+  if (!TPC)
+    return false;
+
+  auto *TM = &TPC->getTM<TargetMachine>();
+  auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
+  auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+  auto &PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+  auto &TLInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
+  // auto &BBSectionsProfileReader =
+  //     getAnalysisIfAvailable<BasicBlockSectionsProfileReader>();
+
+  CodeGenPrepare CGP;
+  return CGP.run(F, TM, TTI, LI, PSI, TLInfo);
+}
+
+PreservedAnalyses CodeGenPreparePass::run(Function &F, FunctionAnalysisManager &AM) {
+ auto &TTI = AM.getResult<TargetIRAnalysis>(F);
+ auto &LI = AM.getResult<LoopAnalysis>(F);
+ auto &PSI = AM.getResult<ProfileSummaryAnalysis>(F);
+ auto &TLInfo = AM.getResult<TargetLibraryAnalysis>(F);
+// auto &BasicBlockSectionsProfileReader = AM.getResult<BasicBlockSectionsProfileReader>(F);
----------------
arsenm wrote:

This is another analysis pass that needs to be ported as a dependency

https://github.com/llvm/llvm-project/pull/68530


More information about the llvm-commits mailing list