llvm.org GIT mirror llvm / d8ae3e1
x86_64: Fix calls to __morestack under the large code model. Under the large code model, we cannot assume that __morestack lives within 2^31 bytes of the call site, so we cannot use pc-relative addressing. We cannot perform the call via a temporary register, as the rax register may be used to store the static chain, and all other suitable registers may be either callee-save or used for parameter passing. We cannot use the stack at this point either because __morestack manipulates the stack directly. To avoid these issues, perform an indirect call via a read-only memory location containing the address. This solution is not perfect, as it assumes that the .rodata section is laid out within 2^31 bytes of each function body, but this seems to be sufficient for JIT. Differential Revision: http://reviews.llvm.org/D6787 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225003 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 5 years ago
5 changed file(s) with 78 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
160160 /// to _fltused on Windows targets.
161161 bool UsesVAFloatArgument;
162162
163 /// UsesMorestackAddr - True if the module calls the __morestack function
164 /// indirectly, as is required under the large code model on x86. This is used
165 /// to emit a definition of a symbol, __morestack_addr, containing the
166 /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more
167 /// details.
168 bool UsesMorestackAddr;
169
163170 public:
164171 static char ID; // Pass identification, replacement for typeid
165172
231238
232239 void setUsesVAFloatArgument(bool b) {
233240 UsesVAFloatArgument = b;
241 }
242
243 bool usesMorestackAddr() const {
244 return UsesMorestackAddr;
245 }
246
247 void setUsesMorestackAddr(bool b) {
248 UsesMorestackAddr = b;
234249 }
235250
236251 /// \brief Returns a reference to a list of cfi instructions in the current
10101010 // Emit llvm.ident metadata in an '.ident' directive.
10111011 EmitModuleIdents(M);
10121012
1013 // Emit __morestack address if needed for indirect calls.
1014 if (MMI->usesMorestackAddr()) {
1015 const MCSection *ReadOnlySection =
1016 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly(),
1017 /*C=*/nullptr);
1018 OutStreamer.SwitchSection(ReadOnlySection);
1019
1020 MCSymbol *AddrSymbol =
1021 OutContext.GetOrCreateSymbol(StringRef("__morestack_addr"));
1022 OutStreamer.EmitLabel(AddrSymbol);
1023
1024 const DataLayout &DL = *TM.getSubtargetImpl()->getDataLayout();
1025 unsigned PtrSize = DL.getPointerSize(0);
1026 OutStreamer.EmitSymbolValue(GetExternalSymbolSymbol("__morestack"),
1027 PtrSize);
1028 }
1029
10131030 // If we don't have any trampolines, then we don't require stack memory
10141031 // to be executable. Some targets have a directive to declare this.
10151032 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
272272 CurCallSite = 0;
273273 CallsEHReturn = 0;
274274 CallsUnwindInit = 0;
275 DbgInfoAvailable = UsesVAFloatArgument = false;
275 DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
276276 // Always emit some info, by default "no personality" info.
277277 Personalities.push_back(nullptr);
278278 AddrLabelSymbols = nullptr;
16471647 }
16481648
16491649 // __morestack is in libgcc
1650 if (Is64Bit)
1651 BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32))
1652 .addExternalSymbol("__morestack");
1653 else
1654 BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32))
1655 .addExternalSymbol("__morestack");
1650 if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) {
1651 // Under the large code model, we cannot assume that __morestack lives
1652 // within 2^31 bytes of the call site, so we cannot use pc-relative
1653 // addressing. We cannot perform the call via a temporary register,
1654 // as the rax register may be used to store the static chain, and all
1655 // other suitable registers may be either callee-save or used for
1656 // parameter passing. We cannot use the stack at this point either
1657 // because __morestack manipulates the stack directly.
1658 //
1659 // To avoid these issues, perform an indirect call via a read-only memory
1660 // location containing the address.
1661 //
1662 // This solution is not perfect, as it assumes that the .rodata section
1663 // is laid out within 2^31 bytes of each function body, but this seems
1664 // to be sufficient for JIT.
1665 BuildMI(allocMBB, DL, TII.get(X86::CALL64m))
1666 .addReg(X86::RIP)
1667 .addImm(0)
1668 .addReg(0)
1669 .addExternalSymbol("__morestack_addr")
1670 .addReg(0);
1671 MF.getMMI().setUsesMorestackAddr(true);
1672 } else {
1673 if (Is64Bit)
1674 BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32))
1675 .addExternalSymbol("__morestack");
1676 else
1677 BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32))
1678 .addExternalSymbol("__morestack");
1679 }
16561680
16571681 if (IsNested)
16581682 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10));
0 ; RUN: llc < %s -mcpu=generic -mtriple=i686-linux -verify-machineinstrs | FileCheck %s -check-prefix=X32-Linux
11 ; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux -verify-machineinstrs | FileCheck %s -check-prefix=X64-Linux
2 ; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux -code-model=large -verify-machineinstrs | FileCheck %s -check-prefix=X64-Linux-Large
23 ; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux-gnux32 -verify-machineinstrs | FileCheck %s -check-prefix=X32ABI
34 ; RUN: llc < %s -mcpu=generic -mtriple=i686-darwin -verify-machineinstrs | FileCheck %s -check-prefix=X32-Darwin
45 ; RUN: llc < %s -mcpu=generic -mtriple=x86_64-darwin -verify-machineinstrs | FileCheck %s -check-prefix=X64-Darwin
5657 ; X64-Linux-NEXT: callq __morestack
5758 ; X64-Linux-NEXT: ret
5859
60 ; X64-Linux-Large-LABEL: test_basic:
61
62 ; X64-Linux-Large: cmpq %fs:112, %rsp
63 ; X64-Linux-Large-NEXT: ja .LBB0_2
64
65 ; X64-Linux-Large: movabsq $40, %r10
66 ; X64-Linux-Large-NEXT: movabsq $0, %r11
67 ; X64-Linux-Large-NEXT: callq *__morestack_addr(%rip)
68 ; X64-Linux-Large-NEXT: ret
69
5970 ; X32ABI-LABEL: test_basic:
6071
6172 ; X32ABI: cmpl %fs:64, %esp
625636 }
626637
627638 attributes #0 = { "split-stack" }
639
640 ; X64-Linux-Large: .rodata
641 ; X64-Linux-Large-NEXT: __morestack_addr:
642 ; X64-Linux-Large-NEXT: .quad __morestack