Index: include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h =================================================================== --- include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h +++ include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h @@ -195,13 +195,8 @@ 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: include/llvm/IR/Module.h =================================================================== --- include/llvm/IR/Module.h +++ include/llvm/IR/Module.h @@ -604,9 +604,78 @@ } /// @} -/// @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 qModule; + + global_object_iterator_t(qModule &M) + : function_i(M.begin()), function_e(M.end()), + global_i(M.global_begin()) {} + global_object_iterator_t(qModule &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(); Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1173,17 +1173,11 @@ // 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: lib/ExecutionEngine/ExecutionEngine.cpp =================================================================== --- lib/ExecutionEngine/ExecutionEngine.cpp +++ lib/ExecutionEngine/ExecutionEngine.cpp @@ -235,10 +235,8 @@ 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: lib/IR/AsmWriter.cpp =================================================================== --- lib/IR/AsmWriter.cpp +++ lib/IR/AsmWriter.cpp @@ -2126,11 +2126,8 @@ 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: lib/Transforms/IPO/GlobalDCE.cpp =================================================================== --- lib/Transforms/IPO/GlobalDCE.cpp +++ lib/Transforms/IPO/GlobalDCE.cpp @@ -96,21 +96,14 @@ 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: test/CodeGen/XCore/linkage.ll =================================================================== --- test/CodeGen/XCore/linkage.ll +++ test/CodeGen/XCore/linkage.ll @@ -42,9 +42,8 @@ ; 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