[llvm] r266167 - Refactor the InternalizePass into a helper class, and expose it through a public free function (NFC)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 12 22:25:08 PDT 2016


Author: mehdi_amini
Date: Wed Apr 13 00:25:08 2016
New Revision: 266167

URL: http://llvm.org/viewvc/llvm-project?rev=266167&view=rev
Log:
Refactor the InternalizePass into a helper class, and expose it through a public free function (NFC)

There is really no reason to require to instanciate a pass manager to
internalize.

From: Mehdi Amini <mehdi.amini at apple.com>

Added:
    llvm/trunk/include/llvm/Transforms/IPO/Internalize.h
Modified:
    llvm/trunk/lib/LTO/LTOInternalize.cpp
    llvm/trunk/lib/Transforms/IPO/Internalize.cpp
    llvm/trunk/test/LTO/X86/disable-verify.ll

Added: llvm/trunk/include/llvm/Transforms/IPO/Internalize.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO/Internalize.h?rev=266167&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Transforms/IPO/Internalize.h (added)
+++ llvm/trunk/include/llvm/Transforms/IPO/Internalize.h Wed Apr 13 00:25:08 2016
@@ -0,0 +1,27 @@
+//====- llvm/Transforms/IPO/Internalize.h - Internalization API -*- C++ -*-===//
+//
+//                      The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_INTERNALIZE_H
+#define LLVM_INTERNALIZE_H
+
+#include "llvm/IR/GlobalValue.h"
+
+#include <functional>
+
+namespace llvm {
+class Module;
+class CallGraph;
+
+bool internalizeModule(
+    Module &TheModule,
+    const std::function<bool(const GlobalValue &)> &MustPreserveGV,
+    CallGraph *CG = nullptr);
+}
+
+#endif // LLVM_INTERNALIZE_H

Modified: llvm/trunk/lib/LTO/LTOInternalize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOInternalize.cpp?rev=266167&r1=266166&r2=266167&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOInternalize.cpp (original)
+++ llvm/trunk/lib/LTO/LTOInternalize.cpp Wed Apr 13 00:25:08 2016
@@ -18,7 +18,7 @@
 #include "llvm/IR/Mangler.h"
 #include "llvm/Target/TargetLowering.h"
 #include "llvm/Target/TargetSubtargetInfo.h"
-#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/IPO/Internalize.h"
 
 using namespace llvm;
 
@@ -170,9 +170,5 @@ void llvm::LTOInternalize(
     LLVMCompilerUsed->setSection("llvm.metadata");
   }
 
-  legacy::PassManager passes;
-  passes.add(createInternalizePass(MustPreserveSymbols));
-
-  // apply scope restrictions
-  passes.run(TheModule);
+  internalizeModule(TheModule, MustPreserveSymbols);
 }

Modified: llvm/trunk/lib/Transforms/IPO/Internalize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/Internalize.cpp?rev=266167&r1=266166&r2=266167&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/Internalize.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/Internalize.cpp Wed Apr 13 00:25:08 2016
@@ -19,6 +19,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Transforms/IPO/Internalize.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/Statistic.h"
@@ -37,25 +38,25 @@ using namespace llvm;
 
 #define DEBUG_TYPE "internalize"
 
-STATISTIC(NumAliases  , "Number of aliases internalized");
+STATISTIC(NumAliases, "Number of aliases internalized");
 STATISTIC(NumFunctions, "Number of functions internalized");
-STATISTIC(NumGlobals  , "Number of global vars internalized");
+STATISTIC(NumGlobals, "Number of global vars internalized");
 
 // APIFile - A file which contains a list of symbols that should not be marked
 // external.
 static cl::opt<std::string>
-APIFile("internalize-public-api-file", cl::value_desc("filename"),
-        cl::desc("A file containing list of symbol names to preserve"));
+    APIFile("internalize-public-api-file", cl::value_desc("filename"),
+            cl::desc("A file containing list of symbol names to preserve"));
 
 // APIList - A list of symbols that should not be marked internal.
 static cl::list<std::string>
-APIList("internalize-public-api-list", cl::value_desc("list"),
-        cl::desc("A list of symbol names to preserve"),
-        cl::CommaSeparated);
+    APIList("internalize-public-api-list", cl::value_desc("list"),
+            cl::desc("A list of symbol names to preserve"), cl::CommaSeparated);
 
 namespace {
+
 // Helper to load an API list to preserve from file and expose it as a functor
-// to the Internalize Pass
+// for internalization.
 class PreserveAPIList {
 public:
   PreserveAPIList() {
@@ -70,31 +71,63 @@ public:
 
 private:
   // Contains the set of symbols loaded from file
-    StringSet<> ExternalNames;
+  StringSet<> ExternalNames;
 
-    void LoadFile(StringRef Filename) {
-      // Load the APIFile...
-      std::ifstream In(Filename.data());
-      if (!In.good()) {
-        errs() << "WARNING: Internalize couldn't load file '" << Filename
-               << "'! Continuing as if it's empty.\n";
-        return; // Just continue as if the file were empty
-      }
-      while (In) {
-        std::string Symbol;
-        In >> Symbol;
-        if (!Symbol.empty())
-          ExternalNames.insert(Symbol);
-      }
+  void LoadFile(StringRef Filename) {
+    // Load the APIFile...
+    std::ifstream In(Filename.data());
+    if (!In.good()) {
+      errs() << "WARNING: Internalize couldn't load file '" << Filename
+             << "'! Continuing as if it's empty.\n";
+      return; // Just continue as if the file were empty
+    }
+    while (In) {
+      std::string Symbol;
+      In >> Symbol;
+      if (!Symbol.empty())
+        ExternalNames.insert(Symbol);
     }
+  }
 };
-}
 
-namespace {
+// Internalization exposed as a pass
 class InternalizePass : public ModulePass {
-  // Client supply callback to control wheter a symbol must be preserved.
+  // Client supplied callback to control wheter a symbol must be preserved.
   std::function<bool(const GlobalValue &)> MustPreserveGV;
 
+public:
+  static char ID; // Pass identification, replacement for typeid
+
+  InternalizePass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
+
+  InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV)
+      : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
+    initializeInternalizePassPass(*PassRegistry::getPassRegistry());
+  }
+
+  bool runOnModule(Module &M) override {
+    CallGraphWrapperPass *CGPass =
+        getAnalysisIfAvailable<CallGraphWrapperPass>();
+    CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
+    return internalizeModule(M, MustPreserveGV, CG);
+  }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesCFG();
+    AU.addPreserved<CallGraphWrapperPass>();
+  }
+};
+} // end anonymous namespace
+
+char InternalizePass::ID = 0;
+INITIALIZE_PASS(InternalizePass, "internalize", "Internalize Global Symbols",
+                false, false)
+
+// Helper class to perform internalization.
+class Internalizer {
+  // Client supplied callback to control wheter a symbol must be preserved.
+  const std::function<bool(const GlobalValue &)> &MustPreserveGV;
+
   // Set of symbols private to the compiler that this pass should not touch.
   StringSet<> AlwaysPreserved;
 
@@ -123,40 +156,26 @@ class InternalizePass : public ModulePas
     return MustPreserveGV(GV);
   }
 
-    bool maybeInternalize(GlobalValue &GV,
-                          const std::set<const Comdat *> &ExternalComdats);
-    void checkComdatVisibility(GlobalValue &GV,
-                               std::set<const Comdat *> &ExternalComdats);
-
-  public:
-    static char ID; // Pass identification, replacement for typeid
-    explicit InternalizePass();
-    InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV);
-    bool runOnModule(Module &M) override;
-
-    void getAnalysisUsage(AnalysisUsage &AU) const override {
-      AU.setPreservesCFG();
-      AU.addPreserved<CallGraphWrapperPass>();
-    }
-  };
-} // end anonymous namespace
-
-char InternalizePass::ID = 0;
-INITIALIZE_PASS(InternalizePass, "internalize",
-                "Internalize Global Symbols", false, false)
+  bool maybeInternalize(GlobalValue &GV,
+                        const std::set<const Comdat *> &ExternalComdats);
+  void checkComdatVisibility(GlobalValue &GV,
+                             std::set<const Comdat *> &ExternalComdats);
 
-InternalizePass::InternalizePass()
-    : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
+public:
+  Internalizer(const std::function<bool(const GlobalValue &)> &MustPreserveGV)
+      : MustPreserveGV(MustPreserveGV) {}
 
-InternalizePass::InternalizePass(
-    std::function<bool(const GlobalValue &)> MustPreserveGV)
-    : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
-  initializeInternalizePassPass(*PassRegistry::getPassRegistry());
-}
+  /// Run the internalizer on \p TheModule, returns true if any changes was
+  /// made.
+  ///
+  /// If the CallGraph \p CG is supplied, it will be updated when
+  /// internalizing a function (by removing any edge from the "external node")
+  bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
+};
 
 // Internalize GV if it is possible to do so, i.e. it is not externally visible
 // and is not a member of an externally visible comdat.
-bool InternalizePass::maybeInternalize(
+bool Internalizer::maybeInternalize(
     GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) {
   if (Comdat *C = GV.getComdat()) {
     if (ExternalComdats.count(C))
@@ -183,7 +202,7 @@ bool InternalizePass::maybeInternalize(
 
 // If GV is part of a comdat and is externally visible, keep track of its
 // comdat so that we don't internalize any of its members.
-void InternalizePass::checkComdatVisibility(
+void Internalizer::checkComdatVisibility(
     GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) {
   Comdat *C = GV.getComdat();
   if (!C)
@@ -193,9 +212,7 @@ void InternalizePass::checkComdatVisibil
     ExternalComdats.insert(C);
 }
 
-bool InternalizePass::runOnModule(Module &M) {
-  CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>();
-  CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
+bool Internalizer::internalizeModule(Module &M, CallGraph *CG) {
   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
 
   SmallPtrSet<GlobalValue *, 8> Used;
@@ -258,8 +275,8 @@ bool InternalizePass::runOnModule(Module
 
   // Mark all global variables with initializers that are not in the api as
   // internal as well.
-  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
-       I != E; ++I) {
+  for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;
+       ++I) {
     if (!maybeInternalize(*I, ExternalComdats))
       continue;
 
@@ -268,8 +285,8 @@ bool InternalizePass::runOnModule(Module
   }
 
   // Mark all aliases that are not in the api as internal as well.
-  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
-       I != E; ++I) {
+  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
+       ++I) {
     if (!maybeInternalize(*I, ExternalComdats))
       continue;
 
@@ -286,12 +303,18 @@ bool InternalizePass::runOnModule(Module
   return true;
 }
 
-ModulePass *llvm::createInternalizePass() {
-  return new InternalizePass(PreserveAPIList());
+/// Public API below
+
+bool llvm::internalizeModule(
+    Module &TheModule,
+    const std::function<bool(const GlobalValue &)> &MustPreserveGV,
+    CallGraph *CG) {
+  return Internalizer(MustPreserveGV).internalizeModule(TheModule, CG);
 }
 
+ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
+
 ModulePass *llvm::createInternalizePass(
     std::function<bool(const GlobalValue &)> MustPreserveGV) {
   return new InternalizePass(std::move(MustPreserveGV));
 }
-

Modified: llvm/trunk/test/LTO/X86/disable-verify.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/X86/disable-verify.ll?rev=266167&r1=266166&r2=266167&view=diff
==============================================================================
--- llvm/trunk/test/LTO/X86/disable-verify.ll (original)
+++ llvm/trunk/test/LTO/X86/disable-verify.ll Wed Apr 13 00:25:08 2016
@@ -6,10 +6,9 @@ target datalayout = "e-m:o-i64:64-f80:12
 target triple = "x86_64-apple-macosx10.10.0"
 
 ; -disable-verify should disable verification from the optimization pipeline.
-; CHECK: Pass Arguments: -internalize
+; CHECK: Pass Arguments:
 ; CHECK-NOT: -verify
 
-; VERIFY: Pass Arguments: -internalize
 ; VERIFY: Pass Arguments: {{.*}} -verify {{.*}} -verify
 
 define void @f() {




More information about the llvm-commits mailing list