llvm.org GIT mirror llvm / ace53f2
Properly emit __chkstk call instead of __alloca on non-mingw windows targets. Patch by Cameron Esfahani! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112902 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 10 years ago
3 changed file(s) with 66 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
763763 }
764764 }
765765
766 /// mergeSPUpdatesUp - Merge two stack-manipulating instructions lower iterator.
766 /// mergeSPUpdatesDown - Merge two stack-manipulating instructions lower iterator.
767767 static
768768 void mergeSPUpdatesDown(MachineBasicBlock &MBB,
769769 MachineBasicBlock::iterator &MBBI,
10861086 DL = MBB.findDebugLoc(MBBI);
10871087
10881088 // Adjust stack pointer: ESP -= numbytes.
1089 if (NumBytes >= 4096 && Subtarget->isTargetCygMing()) {
1089
1090 // Windows and cygwin/mingw require a prologue helper routine when allocating
1091 // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw
1092 // uses __alloca. __alloca and the 32-bit version of __chkstk will probe
1093 // the stack and adjust the stack pointer in one go. The 64-bit version
1094 // of __chkstk is only responsible for probing the stack. The 64-bit
1095 // prologue is responsible for adjusting the stack pointer. Touching the
1096 // stack at 4K increments is necessary to ensure that the guard pages used
1097 // by the OS virtual memory manager are allocated in correct sequence.
1098 if (NumBytes >= 4096 &&
1099 (Subtarget->isTargetCygMing() || Subtarget->isTargetWin32())) {
10901100 // Check, whether EAX is livein for this function.
10911101 bool isEAXAlive = false;
10921102 for (MachineRegisterInfo::livein_iterator
10971107 Reg == X86::AH || Reg == X86::AL);
10981108 }
10991109
1100 // Function prologue calls _alloca to probe the stack when allocating more
1101 // than 4k bytes in one go. Touching the stack at 4K increments is necessary
1102 // to ensure that the guard pages used by the OS virtual memory manager are
1103 // allocated in correct sequence.
1110
1111 const char *StackProbeSymbol =
1112 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11041113 if (!isEAXAlive) {
11051114 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
11061115 .addImm(NumBytes);
11071116 BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32))
1108 .addExternalSymbol("_alloca")
1117 .addExternalSymbol(StackProbeSymbol)
11091118 .addReg(StackPtr, RegState::Define | RegState::Implicit)
11101119 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11111120 } else {
11181127 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
11191128 .addImm(NumBytes - 4);
11201129 BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32))
1121 .addExternalSymbol("_alloca")
1130 .addExternalSymbol(StackProbeSymbol)
11221131 .addReg(StackPtr, RegState::Define | RegState::Implicit)
11231132 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11241133
185185 return Is64Bit && (isTargetMingw() || isTargetWindows());
186186 }
187187
188 bool isTargetWin32() const {
189 return !Is64Bit && (isTargetMingw() || isTargetWindows());
190 }
191
188192 std::string getDataLayout() const {
189193 const char *p;
190194 if (is64Bit())
0 ; RUN: llc < %s -mtriple=i686-pc-win32 | FileCheck %s -check-prefix=WIN_X32
1 ; RUN: llc < %s -mtriple=i686-pc-mingw32 | FileCheck %s -check-prefix=MINGW_X32
2 ; RUN: llc < %s -mtriple=x86_64-pc-mingw32 | FileCheck %s -check-prefix=MINGW_X64
3 ; RUN: llc < %s -mtriple=i386-pc-linux | FileCheck %s -check-prefix=LINUX
4
5 ; Windows and mingw require a prologue helper routine if more than 4096 bytes area
6 ; allocated on the stack. Windows uses __chkstk and mingw uses __alloca. __alloca
7 ; and the 32-bit version of __chkstk will probe the stack and adjust the stack pointer.
8 ; The 64-bit version of __chkstk is only responsible for probing the stack. The 64-bit
9 ; prologue is responsible for adjusting the stack pointer.
10
11 ; Stack allocation >= 4096 bytes will require call to __chkstk in the Windows ABI.
12 define i32 @main4k() nounwind {
13 entry:
14 ; WIN_X32: call __chkstk
15 ; WIN_X64: call __chkstk
16 ; MINGW_X32: call __alloca
17 ; MINGW_X64: call _alloca
18 ; LINUX-NOT: call __chkstk
19 %array4096 = alloca [4096 x i8], align 16 ; <[4096 x i8]*> [#uses=0]
20 ret i32 0
21 }
22
23 ; Make sure we don't call __chkstk or __alloca when we have less than a 4096 stack
24 ; allocation.
25 define i32 @main128() nounwind {
26 entry:
27 ; WIN_X32: # BB#0:
28 ; WIN_X32-NOT: call __chkstk
29 ; WIN_X32: ret
30
31 ; WIN_X64: # BB#0:
32 ; WIN_X64-NOT: call __chkstk
33 ; WIN_X64: ret
34
35 ; MINGW_X64: # BB#0:
36 ; MINGW_X64-NOT: call _alloca
37 ; MINGW_X64: ret
38
39 ; LINUX: # BB#0:
40 ; LINUX-NOT: call __chkstk
41 ; LINUX: ret
42 %array128 = alloca [128 x i8], align 16 ; <[128 x i8]*> [#uses=0]
43 ret i32 0
44 }