OSDN Git Service

[NewPM] Run ObjC ARC passes
authorArthur Eubanks <aeubanks@google.com>
Wed, 23 Dec 2020 05:41:25 +0000 (21:41 -0800)
committerArthur Eubanks <aeubanks@google.com>
Fri, 8 Jan 2021 23:47:11 +0000 (15:47 -0800)
Match the legacy PM in running various ObjC ARC passes.

This requires making some module passes into function passes. These were
initially ported as module passes since they add function declarations
(e.g. https://reviews.llvm.org/D86178), but that's still up for debate
and other passes do so.

Reviewed By: ahatanak

Differential Revision: https://reviews.llvm.org/D93743

clang/lib/CodeGen/BackendUtil.cpp
llvm/include/llvm/Transforms/ObjCARC.h
llvm/lib/Passes/PassRegistry.def
llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp
llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp

index 90cf5fc..2257be8 100644 (file)
@@ -1199,6 +1199,25 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
     bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
     bool IsLTO = CodeGenOpts.PrepareForLTO;
 
+    if (LangOpts.ObjCAutoRefCount) {
+      PB.registerPipelineStartEPCallback(
+          [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
+            if (Level != PassBuilder::OptimizationLevel::O0)
+              MPM.addPass(
+                  createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
+          });
+      PB.registerPipelineEarlySimplificationEPCallback(
+          [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
+            if (Level != PassBuilder::OptimizationLevel::O0)
+              MPM.addPass(ObjCARCAPElimPass());
+          });
+      PB.registerScalarOptimizerLateEPCallback(
+          [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
+            if (Level != PassBuilder::OptimizationLevel::O0)
+              FPM.addPass(ObjCARCOptPass());
+          });
+    }
+
     // If we reached here with a non-empty index file name, then the index
     // file was empty and we are not performing ThinLTO backend compilation
     // (used in testing in a distributed build environment). Drop any the type
index 18df989..a89df95 100644 (file)
@@ -45,11 +45,11 @@ Pass *createObjCARCContractPass();
 Pass *createObjCARCOptPass();
 
 struct ObjCARCOptPass : public PassInfoMixin<ObjCARCOptPass> {
-  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 };
 
 struct ObjCARCContractPass : public PassInfoMixin<ObjCARCContractPass> {
-  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 };
 
 struct ObjCARCAPElimPass : public PassInfoMixin<ObjCARCAPElimPass> {
index 3db0cff..1937d5a 100644 (file)
@@ -80,9 +80,7 @@ MODULE_PASS("metarenamer", MetaRenamerPass())
 MODULE_PASS("mergefunc", MergeFunctionsPass())
 MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
 MODULE_PASS("no-op-module", NoOpModulePass())
-MODULE_PASS("objc-arc", ObjCARCOptPass())
 MODULE_PASS("objc-arc-apelim", ObjCARCAPElimPass())
-MODULE_PASS("objc-arc-contract", ObjCARCContractPass())
 MODULE_PASS("partial-inliner", PartialInlinerPass())
 MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
 MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
@@ -268,6 +266,8 @@ FUNCTION_PASS("loop-load-elim", LoopLoadEliminationPass())
 FUNCTION_PASS("loop-fusion", LoopFusePass())
 FUNCTION_PASS("loop-distribute", LoopDistributePass())
 FUNCTION_PASS("loop-versioning", LoopVersioningPass())
+FUNCTION_PASS("objc-arc", ObjCARCOptPass())
+FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass())
 FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass())
 FUNCTION_PASS("pgo-memop-opt", PGOMemOPSizeOpt())
 FUNCTION_PASS("print", PrintFunctionPass(dbgs()))
index c2499fc..86d1611 100644 (file)
@@ -750,19 +750,13 @@ bool ObjCARCContractLegacyPass::runOnFunction(Function &F) {
   return OCARCC.run(F, AA, DT);
 }
 
-PreservedAnalyses ObjCARCContractPass::run(Module &M,
-                                           ModuleAnalysisManager &AM) {
+PreservedAnalyses ObjCARCContractPass::run(Function &F,
+                                           FunctionAnalysisManager &AM) {
   ObjCARCContract OCAC;
-  OCAC.init(M);
+  OCAC.init(*F.getParent());
 
-  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
-  bool Changed = false;
-  for (Function &F : M) {
-    if (F.isDeclaration())
-      continue;
-    Changed |= OCAC.run(F, &FAM.getResult<AAManager>(F),
-                        &FAM.getResult<DominatorTreeAnalysis>(F));
-  }
+  bool Changed = OCAC.run(F, &AM.getResult<AAManager>(F),
+                          &AM.getResult<DominatorTreeAnalysis>(F));
   if (Changed) {
     PreservedAnalyses PA;
     PA.preserveSet<CFGAnalyses>();
index 443ff5b..1c44749 100644 (file)
@@ -2462,17 +2462,12 @@ void ObjCARCOpt::releaseMemory() {
 /// @}
 ///
 
-PreservedAnalyses ObjCARCOptPass::run(Module &M, ModuleAnalysisManager &AM) {
+PreservedAnalyses ObjCARCOptPass::run(Function &F,
+                                      FunctionAnalysisManager &AM) {
   ObjCARCOpt OCAO;
-  OCAO.init(M);
+  OCAO.init(*F.getParent());
 
-  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
-  bool Changed = false;
-  for (Function &F : M) {
-    if (F.isDeclaration())
-      continue;
-    Changed |= OCAO.run(F, FAM.getResult<AAManager>(F));
-  }
+  bool Changed = OCAO.run(F, AM.getResult<AAManager>(F));
   if (Changed) {
     PreservedAnalyses PA;
     PA.preserveSet<CFGAnalyses>();