From 277258478e522af4c22ba3b873a84ff26833789f Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 22 Jun 2016 20:29:42 +0000 Subject: [PATCH] IR: Introduce Module::global_objects(). 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 --- .../ExecutionEngine/Orc/LazyEmittingLayer.h | 9 +-- include/llvm/IR/Module.h | 71 ++++++++++++++++++- lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 14 ++-- lib/ExecutionEngine/ExecutionEngine.cpp | 6 +- lib/IR/AsmWriter.cpp | 7 +- lib/Transforms/IPO/GlobalDCE.cpp | 19 ++--- test/CodeGen/XCore/linkage.ll | 5 +- 7 files changed, 88 insertions(+), 43 deletions(-) diff --git a/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h b/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h index a5286ff9add..c5fb6b847b3 100644 --- a/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h +++ b/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h @@ -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; } diff --git a/include/llvm/IR/Module.h b/include/llvm/IR/Module.h index fd5303431ac..632b27e2d0d 100644 --- a/include/llvm/IR/Module.h +++ b/include/llvm/IR/Module.h @@ -604,9 +604,78 @@ public: } /// @} -/// @name Named Metadata Iteration +/// @name Convenience iterators /// @{ + template class global_object_iterator_t { + friend Module; + + typename std::conditional::type + function_i, + function_e; + typename std::conditional::type global_i; + + typedef + typename std::conditional::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::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 global_object_iterator; + typedef global_object_iterator_t + 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_objects() { + return make_range(global_object_begin(), global_object_end()); + } + iterator_range 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(); diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index ad2f216f504..3f2ef090b86 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -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); } } diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index de3d647438b..a8e68bf49ab 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -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, diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index 17b622458c4..0aa0b8014f0 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -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); } diff --git a/lib/Transforms/IPO/GlobalDCE.cpp b/lib/Transforms/IPO/GlobalDCE.cpp index f30e69ccbcd..4c74698a1b6 100644 --- a/lib/Transforms/IPO/GlobalDCE.cpp +++ b/lib/Transforms/IPO/GlobalDCE.cpp @@ -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()) { diff --git a/test/CodeGen/XCore/linkage.ll b/test/CodeGen/XCore/linkage.ll index 7384fe7bcf0..ff07a261fc5 100644 --- a/test/CodeGen/XCore/linkage.ll +++ b/test/CodeGen/XCore/linkage.ll @@ -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