[AsmPrinter] Print aliases in topological order

Print aliases in topological order, that is, for any alias a = b,
b must be printed before a. This is because on some targets (e.g. PowerPC)
linker expects aliases in such an order to generate correct TOC information.

GCC also prints aliases in topological order.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265064 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Tim Shen 2016-03-31 22:08:19 +00:00
parent e6ec1e3b8f
commit 47094ef9e3
2 changed files with 33 additions and 1 deletions

View File

@ -1148,7 +1148,7 @@ bool AsmPrinter::doFinalization(Module &M) {
}
OutStreamer->AddBlankLine();
for (const auto &Alias : M.aliases()) {
const auto printAlias = [this, &M](const GlobalAlias &Alias) {
MCSymbol *Name = getSymbol(&Alias);
if (Alias.hasExternalLinkage() || !MAI->getWeakRefDirective())
@ -1186,6 +1186,23 @@ bool AsmPrinter::doFinalization(Module &M) {
OutStreamer->emitELFSize(cast<MCSymbolELF>(Name),
MCConstantExpr::create(Size, OutContext));
}
};
// Print aliases in topological order, that is, for each alias a = b,
// b must be printed before a.
// This is because on some targets (e.g. PowerPC) linker expects aliases in
// such an order to generate correct TOC information.
SmallVector<const GlobalAlias *, 16> AliasStack;
SmallPtrSet<const GlobalAlias *, 16> AliasVisited;
for (const auto &Alias : M.aliases()) {
for (const GlobalAlias *Cur = &Alias; Cur;
Cur = dyn_cast<GlobalAlias>(Cur->getAliasee())) {
if (!AliasVisited.insert(Cur).second)
break;
AliasStack.push_back(Cur);
}
for (const GlobalAlias *AncestorAlias : reverse(AliasStack))
printAlias(*AncestorAlias);
AliasStack.clear();
}
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();

View File

@ -0,0 +1,15 @@
; RUN: llc < %s | FileCheck %s
@TestA = alias void (), void ()* @TestC
@TestB = alias void (), void ()* @TestC
@TestC = alias void (), void ()* @TestD
define void @TestD() {
entry:
ret void
}
; CHECK-LABEL: TestD:
; CHECK: TestC = TestD
; CHECK-DAG: TestB = TestC
; CHECK-DAG: TestA = TestC