Skip to content

Commit 3659780

Browse files
committed
MachineModuleInfo: Remove UsesMorestackAddr
This is x86 specific, and adds statefulness to MachineModuleInfo. Instead of explicitly tracking this, infer if we need to declare the symbol based on the reference previously inserted. This produces a small change in the output due to the move from AsmPrinter::doFinalization to X86's emitEndOfAsmFile. This will now be moved relative to other end of file fields, which I'm assuming doesn't matter (e.g. the __morestack_addr declaration is now after the .note.GNU-split-stack part) This also produces another small change in code if the module happened to define/declare __morestack_addr, but I assume that's invalid and doesn't really matter.
1 parent fb3b3f7 commit 3659780

File tree

7 files changed

+32
-35
lines changed

7 files changed

+32
-35
lines changed

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,6 @@ class MachineModuleInfo {
123123
/// point. This is used to emit an undefined reference to _fltused.
124124
bool UsesMSVCFloatingPoint;
125125

126-
/// True if the module calls the __morestack function indirectly, as is
127-
/// required under the large code model on x86. This is used to emit
128-
/// a definition of a symbol, __morestack_addr, containing the address. See
129-
/// comments in lib/Target/X86/X86FrameLowering.cpp for more details.
130-
bool UsesMorestackAddr;
131-
132126
/// Maps IR Functions to their corresponding MachineFunctions.
133127
DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
134128
/// Next unique number available for a MachineFunction.
@@ -195,14 +189,6 @@ class MachineModuleInfo {
195189

196190
void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; }
197191

198-
bool usesMorestackAddr() const {
199-
return UsesMorestackAddr;
200-
}
201-
202-
void setUsesMorestackAddr(bool b) {
203-
UsesMorestackAddr = b;
204-
}
205-
206192
/// Return the symbol to be used for the specified basic block when its
207193
/// address is taken. This cannot be its normal LBB label because the block
208194
/// may be accessed outside its containing function.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,23 +1904,6 @@ bool AsmPrinter::doFinalization(Module &M) {
19041904
// Emit bytes for llvm.commandline metadata.
19051905
emitModuleCommandLines(M);
19061906

1907-
// Emit __morestack address if needed for indirect calls.
1908-
if (MMI->usesMorestackAddr()) {
1909-
Align Alignment(1);
1910-
MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant(
1911-
getDataLayout(), SectionKind::getReadOnly(),
1912-
/*C=*/nullptr, Alignment);
1913-
OutStreamer->SwitchSection(ReadOnlySection);
1914-
1915-
MCSymbol *AddrSymbol =
1916-
OutContext.getOrCreateSymbol(StringRef("__morestack_addr"));
1917-
OutStreamer->emitLabel(AddrSymbol);
1918-
1919-
unsigned PtrSize = MAI->getCodePointerSize();
1920-
OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("__morestack"),
1921-
PtrSize);
1922-
}
1923-
19241907
// Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if
19251908
// split-stack is used.
19261909
if (TM.getTargetTriple().isOSBinFormatELF() && HasSplitStack) {

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void MachineModuleInfo::initialize() {
202202
ObjFileMMI = nullptr;
203203
CurCallSite = 0;
204204
NextFnNum = 0;
205-
UsesMSVCFloatingPoint = UsesMorestackAddr = false;
205+
UsesMSVCFloatingPoint = false;
206206
AddrLabelSymbols = nullptr;
207207
DbgInfoAvailable = false;
208208
}
@@ -230,7 +230,6 @@ MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
230230
ObjFileMMI = MMI.ObjFileMMI;
231231
CurCallSite = MMI.CurCallSite;
232232
UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint;
233-
UsesMorestackAddr = MMI.UsesMorestackAddr;
234233
AddrLabelSymbols = MMI.AddrLabelSymbols;
235234
ExternalContext = MMI.ExternalContext;
236235
TheModule = MMI.TheModule;

llvm/lib/Target/X86/X86AsmPrinter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/IR/Mangler.h"
3030
#include "llvm/IR/Module.h"
3131
#include "llvm/IR/Type.h"
32+
#include "llvm/MC/MCAsmInfo.h"
3233
#include "llvm/MC/MCCodeEmitter.h"
3334
#include "llvm/MC/MCContext.h"
3435
#include "llvm/MC/MCExpr.h"
@@ -803,6 +804,22 @@ void X86AsmPrinter::emitEndOfAsmFile(Module &M) {
803804
emitStackMaps(SM);
804805
FM.serializeToFaultMapSection();
805806
}
807+
808+
// Emit __morestack address if needed for indirect calls.
809+
if (TT.getArch() == Triple::x86_64 && TM.getCodeModel() == CodeModel::Large) {
810+
if (MCSymbol *AddrSymbol = OutContext.lookupSymbol("__morestack_addr")) {
811+
Align Alignment(1);
812+
MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant(
813+
getDataLayout(), SectionKind::getReadOnly(),
814+
/*C=*/nullptr, Alignment);
815+
OutStreamer->SwitchSection(ReadOnlySection);
816+
OutStreamer->emitLabel(AddrSymbol);
817+
818+
unsigned PtrSize = MAI->getCodePointerSize();
819+
OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("__morestack"),
820+
PtrSize);
821+
}
822+
}
806823
}
807824

808825
//===----------------------------------------------------------------------===//

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3128,7 +3128,6 @@ void X86FrameLowering::adjustForSegmentedStacks(
31283128
.addReg(0)
31293129
.addExternalSymbol("__morestack_addr")
31303130
.addReg(0);
3131-
MF.getMMI().setUsesMorestackAddr(true);
31323131
} else {
31333132
if (Is64Bit)
31343133
BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; RUN: llc -mcpu=generic -mtriple=x86_64-linux -code-model=large < %s | FileCheck %s
2+
3+
; Check what happens if we have an existing declaration of __morestack_addr
4+
5+
; CHECK: .section ".note.GNU-stack","",@progbits
6+
; CHECK-NEXT: .section .rodata,"a",@progbits
7+
; CHECK-NEXT: __morestack_addr:
8+
; CHECK-NEXT: .quad __morestack
9+
10+
declare void @__morestack_addr()

llvm/test/CodeGen/X86/segmented-stacks.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,10 @@ define i32 @test_nested_unused(i32 * nest %unused) #0 {
21232123

21242124
attributes #0 = { "split-stack" }
21252125

2126-
; X64-Linux-Large: .rodata
2126+
; X64-Linux-Large: .section ".note.GNU-split-stack","",@progbits
2127+
; X64-Linux-Large-NEXT: .section ".note.GNU-no-split-stack","",@progbits
2128+
; X64-Linux-Large-NEXT: .section ".note.GNU-stack","",@progbits
2129+
; X64-Linux-Large-NEXT: .rodata
21272130
; X64-Linux-Large-NEXT: __morestack_addr:
21282131
; X64-Linux-Large-NEXT: .quad __morestack
21292132

0 commit comments

Comments
 (0)