forked from OSchip/llvm-project
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 llvm-svn: 273470
This commit is contained in:
parent
4a07bf6372
commit
6d88fde3af
|
@ -195,13 +195,8 @@ private:
|
||||||
for (const auto &M : Ms) {
|
for (const auto &M : Ms) {
|
||||||
Mangler Mang;
|
Mangler Mang;
|
||||||
|
|
||||||
for (const auto &V : M->globals())
|
for (const auto &GO : M->global_objects())
|
||||||
if (auto GV = addGlobalValue(*Symbols, V, Mang, SearchName,
|
if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
|
||||||
ExportedSymbolsOnly))
|
|
||||||
return GV;
|
|
||||||
|
|
||||||
for (const auto &F : *M)
|
|
||||||
if (auto GV = addGlobalValue(*Symbols, F, Mang, SearchName,
|
|
||||||
ExportedSymbolsOnly))
|
ExportedSymbolsOnly))
|
||||||
return GV;
|
return GV;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(); }
|
named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
|
||||||
const_named_metadata_iterator named_metadata_begin() const {
|
const_named_metadata_iterator named_metadata_begin() const {
|
||||||
return NamedMDList.begin();
|
return NamedMDList.begin();
|
||||||
|
|
|
@ -1173,17 +1173,11 @@ bool AsmPrinter::doFinalization(Module &M) {
|
||||||
// to notice uses in operands (due to constant exprs etc). This should
|
// to notice uses in operands (due to constant exprs etc). This should
|
||||||
// happen with the MC stuff eventually.
|
// happen with the MC stuff eventually.
|
||||||
|
|
||||||
// Print out module-level global variables here.
|
// Print out module-level global objects here.
|
||||||
for (const auto &G : M.globals()) {
|
for (const auto &GO : M.global_objects()) {
|
||||||
if (!G.hasExternalWeakLinkage())
|
if (!GO.hasExternalWeakLinkage())
|
||||||
continue;
|
continue;
|
||||||
OutStreamer->EmitSymbolAttribute(getSymbol(&G), MCSA_WeakReference);
|
OutStreamer->EmitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto &F : M) {
|
|
||||||
if (!F.hasExternalWeakLinkage())
|
|
||||||
continue;
|
|
||||||
OutStreamer->EmitSymbolAttribute(getSymbol(&F), MCSA_WeakReference);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -235,10 +235,8 @@ void ExecutionEngine::clearAllGlobalMappings() {
|
||||||
void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
|
void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
|
||||||
MutexGuard locked(lock);
|
MutexGuard locked(lock);
|
||||||
|
|
||||||
for (Function &FI : *M)
|
for (GlobalObject &GO : M->global_objects())
|
||||||
EEState.RemoveMapping(getMangledName(&FI));
|
EEState.RemoveMapping(getMangledName(&GO));
|
||||||
for (GlobalVariable &GI : M->globals())
|
|
||||||
EEState.RemoveMapping(getMangledName(&GI));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
|
uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
|
||||||
|
|
|
@ -2126,11 +2126,8 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
|
||||||
if (!TheModule)
|
if (!TheModule)
|
||||||
return;
|
return;
|
||||||
TypePrinter.incorporateTypes(*TheModule);
|
TypePrinter.incorporateTypes(*TheModule);
|
||||||
for (const Function &F : *TheModule)
|
for (const GlobalObject &GO : TheModule->global_objects())
|
||||||
if (const Comdat *C = F.getComdat())
|
if (const Comdat *C = GO.getComdat())
|
||||||
Comdats.insert(C);
|
|
||||||
for (const GlobalVariable &GV : TheModule->globals())
|
|
||||||
if (const Comdat *C = GV.getComdat())
|
|
||||||
Comdats.insert(C);
|
Comdats.insert(C);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,21 +96,14 @@ PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &) {
|
||||||
ComdatMembers.insert(std::make_pair(C, &GA));
|
ComdatMembers.insert(std::make_pair(C, &GA));
|
||||||
|
|
||||||
// Loop over the module, adding globals which are obviously necessary.
|
// Loop over the module, adding globals which are obviously necessary.
|
||||||
for (Function &F : M) {
|
for (GlobalObject &GO : M.global_objects()) {
|
||||||
Changed |= RemoveUnusedGlobalValue(F);
|
Changed |= RemoveUnusedGlobalValue(GO);
|
||||||
// Functions with external linkage are needed if they have a body
|
// 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);
|
|
||||||
// Externally visible & appending globals are needed, if they have an
|
// Externally visible & appending globals are needed, if they have an
|
||||||
// initializer.
|
// initializer.
|
||||||
if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage())
|
if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage())
|
||||||
if (!GV.isDiscardableIfUnused())
|
if (!GO.isDiscardableIfUnused())
|
||||||
GlobalIsNeeded(&GV);
|
GlobalIsNeeded(&GO);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (GlobalAlias &GA : M.aliases()) {
|
for (GlobalAlias &GA : M.aliases()) {
|
||||||
|
|
|
@ -42,9 +42,8 @@ define protected void @test_protected() {
|
||||||
|
|
||||||
; CHECK-NOT: .hidden test_hidden_declaration
|
; CHECK-NOT: .hidden test_hidden_declaration
|
||||||
|
|
||||||
; CHECK: .weak gr
|
|
||||||
@gr = extern_weak global i32
|
|
||||||
|
|
||||||
; CHECK: .weak fr
|
; CHECK: .weak fr
|
||||||
declare extern_weak void @fr(i32*, i32*)
|
declare extern_weak void @fr(i32*, i32*)
|
||||||
|
|
||||||
|
; CHECK: .weak gr
|
||||||
|
@gr = extern_weak global i32
|
||||||
|
|
Loading…
Reference in New Issue