Skip to content

Commit

Permalink
[AsmPrinter] Print aliases in topological order
Browse files Browse the repository at this point in the history
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
  • Loading branch information
timshen91 committed Mar 31, 2016
1 parent e6ec1e3 commit 47094ef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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>();
Expand Down
15 changes: 15 additions & 0 deletions test/CodeGen/Generic/asm-printer-topological-order.ll
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 47094ef

Please sign in to comment.