]> granicus.if.org Git - llvm/commitdiff
IR: Introduce Module::global_objects().
authorPeter Collingbourne <peter@pcc.me.uk>
Wed, 22 Jun 2016 20:29:42 +0000 (20:29 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Wed, 22 Jun 2016 20:29:42 +0000 (20:29 +0000)
This is a convenience iterator that allows clients to enumerate the
GlobalObjects within a Module.

Also start using it in a few places where it is obviously the right thing
to use.

Differential Revision: http://reviews.llvm.org/D21580

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273470 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h
include/llvm/IR/Module.h
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/ExecutionEngine/ExecutionEngine.cpp
lib/IR/AsmWriter.cpp
lib/Transforms/IPO/GlobalDCE.cpp
test/CodeGen/XCore/linkage.ll

index a5286ff9adde68fc46a502daf94fea5a6c1ccd79..c5fb6b847b3039f373ac2cc1ba156ddb75430ebb 100644 (file)
@@ -195,13 +195,8 @@ private:
       for (const auto &M : Ms) {
         Mangler Mang;
 
-        for (const auto &V : M->globals())
-          if (auto GV = addGlobalValue(*Symbols, V, Mang, SearchName,
-                                       ExportedSymbolsOnly))
-            return GV;
-
-        for (const auto &F : *M)
-          if (auto GV = addGlobalValue(*Symbols, F, Mang, SearchName,
+        for (const auto &GO : M->global_objects())
+          if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
                                        ExportedSymbolsOnly))
             return GV;
       }
index fd5303431ac8aafd919774e7094f3289d5c5ebcb..632b27e2d0dd5ce46813232a6dfe131f109ed5d2 100644 (file)
@@ -604,9 +604,78 @@ public:
   }
 
 /// @}
-/// @name Named Metadata Iteration
+/// @name Convenience iterators
 /// @{
 
+  template <bool IsConst> class global_object_iterator_t {
+    friend Module;
+
+    typename std::conditional<IsConst, const_iterator, iterator>::type
+        function_i,
+        function_e;
+    typename std::conditional<IsConst, const_global_iterator,
+                              global_iterator>::type global_i;
+
+    typedef
+        typename std::conditional<IsConst, const Module, Module>::type ModuleTy;
+
+    global_object_iterator_t(ModuleTy &M)
+        : function_i(M.begin()), function_e(M.end()),
+          global_i(M.global_begin()) {}
+    global_object_iterator_t(ModuleTy &M, int)
+        : function_i(M.end()), function_e(M.end()), global_i(M.global_end()) {}
+
+  public:
+    global_object_iterator_t &operator++() {
+      if (function_i != function_e)
+        ++function_i;
+      else
+        ++global_i;
+      return *this;
+    }
+
+    typename std::conditional<IsConst, const GlobalObject, GlobalObject>::type &
+    operator*() const {
+      if (function_i != function_e)
+        return *function_i;
+      else
+        return *global_i;
+    }
+
+    bool operator!=(const global_object_iterator_t &other) const {
+      return function_i != other.function_i || global_i != other.global_i;
+    }
+  };
+
+  typedef global_object_iterator_t</*IsConst=*/false> global_object_iterator;
+  typedef global_object_iterator_t</*IsConst=*/true>
+      const_global_object_iterator;
+
+  global_object_iterator global_object_begin() {
+    return global_object_iterator(*this);
+  }
+  global_object_iterator global_object_end() {
+    return global_object_iterator(*this, 0);
+  }
+
+  const_global_object_iterator global_object_begin() const {
+    return const_global_object_iterator(*this);
+  }
+  const_global_object_iterator global_object_end() const {
+    return const_global_object_iterator(*this, 0);
+  }
+
+  iterator_range<global_object_iterator> global_objects() {
+    return make_range(global_object_begin(), global_object_end());
+  }
+  iterator_range<const_global_object_iterator> global_objects() const {
+    return make_range(global_object_begin(), global_object_end());
+  }
+
+  /// @}
+  /// @name Named Metadata Iteration
+  /// @{
+
   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
   const_named_metadata_iterator named_metadata_begin() const {
     return NamedMDList.begin();
index ad2f216f5047c8cb622142e95ae6a711fd1b8d9b..3f2ef090b86e7d2617bc9cdb69c16908553d9df1 100644 (file)
@@ -1173,17 +1173,11 @@ bool AsmPrinter::doFinalization(Module &M) {
     // to notice uses in operands (due to constant exprs etc).  This should
     // happen with the MC stuff eventually.
 
-    // Print out module-level global variables here.
-    for (const auto &G : M.globals()) {
-      if (!G.hasExternalWeakLinkage())
+    // Print out module-level global objects here.
+    for (const auto &GO : M.global_objects()) {
+      if (!GO.hasExternalWeakLinkage())
         continue;
-      OutStreamer->EmitSymbolAttribute(getSymbol(&G), MCSA_WeakReference);
-    }
-
-    for (const auto &F : M) {
-      if (!F.hasExternalWeakLinkage())
-        continue;
-      OutStreamer->EmitSymbolAttribute(getSymbol(&F), MCSA_WeakReference);
+      OutStreamer->EmitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
     }
   }
 
index de3d647438b4c0fadf128606d30141c4e435af32..a8e68bf49abc931e82907a1f707e3f5e4e366cf3 100644 (file)
@@ -235,10 +235,8 @@ void ExecutionEngine::clearAllGlobalMappings() {
 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
   MutexGuard locked(lock);
 
-  for (Function &FI : *M)
-    EEState.RemoveMapping(getMangledName(&FI));
-  for (GlobalVariable &GI : M->globals())
-    EEState.RemoveMapping(getMangledName(&GI));
+  for (GlobalObject &GO : M->global_objects())
+    EEState.RemoveMapping(getMangledName(&GO));
 }
 
 uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
index 17b622458c42b06808fafc3fe00de9e8e8d9ff15..0aa0b8014f037f17f6c413c244341e3fc7ef882f 100644 (file)
@@ -2126,11 +2126,8 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
   if (!TheModule)
     return;
   TypePrinter.incorporateTypes(*TheModule);
-  for (const Function &F : *TheModule)
-    if (const Comdat *C = F.getComdat())
-      Comdats.insert(C);
-  for (const GlobalVariable &GV : TheModule->globals())
-    if (const Comdat *C = GV.getComdat())
+  for (const GlobalObject &GO : TheModule->global_objects())
+    if (const Comdat *C = GO.getComdat())
       Comdats.insert(C);
 }
 
index f30e69ccbcde73d9dd6e562dbad2dc1f6af81673..4c74698a1b619b8377ce3b03218c248b1e958337 100644 (file)
@@ -96,21 +96,14 @@ PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &) {
       ComdatMembers.insert(std::make_pair(C, &GA));
 
   // Loop over the module, adding globals which are obviously necessary.
-  for (Function &F : M) {
-    Changed |= RemoveUnusedGlobalValue(F);
-    // Functions with external linkage are needed if they have a body
-    if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage())
-      if (!F.isDiscardableIfUnused())
-        GlobalIsNeeded(&F);
-  }
-
-  for (GlobalVariable &GV : M.globals()) {
-    Changed |= RemoveUnusedGlobalValue(GV);
+  for (GlobalObject &GO : M.global_objects()) {
+    Changed |= RemoveUnusedGlobalValue(GO);
+    // Functions with external linkage are needed if they have a body.
     // Externally visible & appending globals are needed, if they have an
     // initializer.
-    if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage())
-      if (!GV.isDiscardableIfUnused())
-        GlobalIsNeeded(&GV);
+    if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage())
+      if (!GO.isDiscardableIfUnused())
+        GlobalIsNeeded(&GO);
   }
 
   for (GlobalAlias &GA : M.aliases()) {
index 7384fe7bcf09243fd05bffe755af9f2c0d2e599e..ff07a261fc50c793505f20de9e03b39196cbea53 100644 (file)
@@ -42,9 +42,8 @@ define protected void @test_protected() {
 
 ; CHECK-NOT: .hidden test_hidden_declaration
 
-; CHECK: .weak gr
-@gr = extern_weak global i32
-
 ; CHECK: .weak fr
 declare extern_weak void @fr(i32*, i32*)
 
+; CHECK: .weak gr
+@gr = extern_weak global i32