IR: Add COMDATs to the IR

This new IR facility allows us to represent the object-file semantic of
a COMDAT group.

COMDATs allow us to tie together sections and make the inclusion of one
dependent on another. This is required to implement features like MS
ABI VFTables and optimizing away certain kinds of initialization in C++.

This functionality is only representable in COFF and ELF, Mach-O has no
similar mechanism.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211920 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer
2014-06-27 18:19:56 +00:00
parent 4a295dfbf9
commit c8a1169c93
56 changed files with 1511 additions and 104 deletions

View File

@ -77,13 +77,19 @@ bool GlobalDCE::runOnModule(Module &M) {
// Remove empty functions from the global ctors list.
Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
typedef std::multimap<const Comdat *, GlobalValue *> ComdatGVPairsTy;
ComdatGVPairsTy ComdatGVPairs;
// Loop over the module, adding globals which are obviously necessary.
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Changed |= RemoveUnusedGlobalValue(*I);
// Functions with external linkage are needed if they have a body
if (!I->isDiscardableIfUnused() &&
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
GlobalIsNeeded(I);
if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
if (!I->isDiscardableIfUnused())
GlobalIsNeeded(I);
else if (const Comdat *C = I->getComdat())
ComdatGVPairs.insert(std::make_pair(C, I));
}
}
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
@ -91,17 +97,38 @@ bool GlobalDCE::runOnModule(Module &M) {
Changed |= RemoveUnusedGlobalValue(*I);
// Externally visible & appending globals are needed, if they have an
// initializer.
if (!I->isDiscardableIfUnused() &&
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
GlobalIsNeeded(I);
if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
if (!I->isDiscardableIfUnused())
GlobalIsNeeded(I);
else if (const Comdat *C = I->getComdat())
ComdatGVPairs.insert(std::make_pair(C, I));
}
}
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ++I) {
Changed |= RemoveUnusedGlobalValue(*I);
// Externally visible aliases are needed.
if (!I->isDiscardableIfUnused())
if (!I->isDiscardableIfUnused()) {
GlobalIsNeeded(I);
} else if (const Comdat *C = I->getComdat()) {
ComdatGVPairs.insert(std::make_pair(C, I));
}
}
for (ComdatGVPairsTy::iterator I = ComdatGVPairs.begin(),
E = ComdatGVPairs.end();
I != E;) {
ComdatGVPairsTy::iterator UB = ComdatGVPairs.upper_bound(I->first);
bool CanDiscard = std::all_of(I, UB, [](ComdatGVPairsTy::value_type Pair) {
return Pair.second->isDiscardableIfUnused();
});
if (!CanDiscard) {
std::for_each(I, UB, [this](ComdatGVPairsTy::value_type Pair) {
GlobalIsNeeded(Pair.second);
});
}
I = UB;
}
// Now that all globals which are needed are in the AliveGlobals set, we loop