llvm.org GIT mirror llvm / 2772584
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 Peter Collingbourne 4 years ago
7 changed file(s) with 89 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
194194 for (const auto &M : Ms) {
195195 Mangler Mang;
196196
197 for (const auto &V : M->globals())
198 if (auto GV = addGlobalValue(*Symbols, V, Mang, SearchName,
199 ExportedSymbolsOnly))
200 return GV;
201
202 for (const auto &F : *M)
203 if (auto GV = addGlobalValue(*Symbols, F, Mang, SearchName,
197 for (const auto &GO : M->global_objects())
198 if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
204199 ExportedSymbolsOnly))
205200 return GV;
206201 }
603603 }
604604
605605 /// @}
606 /// @name Named Metadata Iteration
607 /// @{
606 /// @name Convenience iterators
607 /// @{
608
609 template class global_object_iterator_t {
610 friend Module;
611
612 typename std::conditional::type
613 function_i,
614 function_e;
615 typename std::conditional
616 global_iterator>::type global_i;
617
618 typedef
619 typename std::conditional::type ModuleTy;
620
621 global_object_iterator_t(ModuleTy &M)
622 : function_i(M.begin()), function_e(M.end()),
623 global_i(M.global_begin()) {}
624 global_object_iterator_t(ModuleTy &M, int)
625 : function_i(M.end()), function_e(M.end()), global_i(M.global_end()) {}
626
627 public:
628 global_object_iterator_t &operator++() {
629 if (function_i != function_e)
630 ++function_i;
631 else
632 ++global_i;
633 return *this;
634 }
635
636 typename std::conditional::type &
637 operator*() const {
638 if (function_i != function_e)
639 return *function_i;
640 else
641 return *global_i;
642 }
643
644 bool operator!=(const global_object_iterator_t &other) const {
645 return function_i != other.function_i || global_i != other.global_i;
646 }
647 };
648
649 typedef global_object_iterator_t global_object_iterator;
650 typedef global_object_iterator_t
651 const_global_object_iterator;
652
653 global_object_iterator global_object_begin() {
654 return global_object_iterator(*this);
655 }
656 global_object_iterator global_object_end() {
657 return global_object_iterator(*this, 0);
658 }
659
660 const_global_object_iterator global_object_begin() const {
661 return const_global_object_iterator(*this);
662 }
663 const_global_object_iterator global_object_end() const {
664 return const_global_object_iterator(*this, 0);
665 }
666
667 iterator_range global_objects() {
668 return make_range(global_object_begin(), global_object_end());
669 }
670 iterator_range global_objects() const {
671 return make_range(global_object_begin(), global_object_end());
672 }
673
674 /// @}
675 /// @name Named Metadata Iteration
676 /// @{
608677
609678 named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
610679 const_named_metadata_iterator named_metadata_begin() const {
11721172 // to notice uses in operands (due to constant exprs etc). This should
11731173 // happen with the MC stuff eventually.
11741174
1175 // Print out module-level global variables here.
1176 for (const auto &G : M.globals()) {
1177 if (!G.hasExternalWeakLinkage())
1175 // Print out module-level global objects here.
1176 for (const auto &GO : M.global_objects()) {
1177 if (!GO.hasExternalWeakLinkage())
11781178 continue;
1179 OutStreamer->EmitSymbolAttribute(getSymbol(&G), MCSA_WeakReference);
1180 }
1181
1182 for (const auto &F : M) {
1183 if (!F.hasExternalWeakLinkage())
1184 continue;
1185 OutStreamer->EmitSymbolAttribute(getSymbol(&F), MCSA_WeakReference);
1179 OutStreamer->EmitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
11861180 }
11871181 }
11881182
234234 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
235235 MutexGuard locked(lock);
236236
237 for (Function &FI : *M)
238 EEState.RemoveMapping(getMangledName(&FI));
239 for (GlobalVariable &GI : M->globals())
240 EEState.RemoveMapping(getMangledName(&GI));
237 for (GlobalObject &GO : M->global_objects())
238 EEState.RemoveMapping(getMangledName(&GO));
241239 }
242240
243241 uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
21252125 if (!TheModule)
21262126 return;
21272127 TypePrinter.incorporateTypes(*TheModule);
2128 for (const Function &F : *TheModule)
2129 if (const Comdat *C = F.getComdat())
2130 Comdats.insert(C);
2131 for (const GlobalVariable &GV : TheModule->globals())
2132 if (const Comdat *C = GV.getComdat())
2128 for (const GlobalObject &GO : TheModule->global_objects())
2129 if (const Comdat *C = GO.getComdat())
21332130 Comdats.insert(C);
21342131 }
21352132
9595 ComdatMembers.insert(std::make_pair(C, &GA));
9696
9797 // Loop over the module, adding globals which are obviously necessary.
98 for (Function &F : M) {
99 Changed |= RemoveUnusedGlobalValue(F);
100 // Functions with external linkage are needed if they have a body
101 if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage())
102 if (!F.isDiscardableIfUnused())
103 GlobalIsNeeded(&F);
104 }
105
106 for (GlobalVariable &GV : M.globals()) {
107 Changed |= RemoveUnusedGlobalValue(GV);
98 for (GlobalObject &GO : M.global_objects()) {
99 Changed |= RemoveUnusedGlobalValue(GO);
100 // Functions with external linkage are needed if they have a body.
108101 // Externally visible & appending globals are needed, if they have an
109102 // initializer.
110 if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage())
111 if (!GV.isDiscardableIfUnused())
112 GlobalIsNeeded(&GV);
103 if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage())
104 if (!GO.isDiscardableIfUnused())
105 GlobalIsNeeded(&GO);
113106 }
114107
115108 for (GlobalAlias &GA : M.aliases()) {
4141
4242 ; CHECK-NOT: .hidden test_hidden_declaration
4343
44 ; CHECK: .weak gr
45 @gr = extern_weak global i32
46
4744 ; CHECK: .weak fr
4845 declare extern_weak void @fr(i32*, i32*)
4946
47 ; CHECK: .weak gr
48 @gr = extern_weak global i32