Index: lib/Target/X86/X86ISelLowering.cpp =================================================================== --- lib/Target/X86/X86ISelLowering.cpp 2014-04-11 23:04:44.000000000 +0200 +++ lib/Target/X86/X86ISelLowering.cpp (working copy) @@ -15493,12 +15493,36 @@ // non-trivial part is impdef of ESP. if (Subtarget->isTargetWin64()) { + const char *StackProbeSymbol = + Subtarget->isTargetCygMing() ? "___chkstk" : "__chkstk"; + + MachineInstrBuilder MIB; + + if (getTargetMachine().getCodeModel() == CodeModel::Large) { + // For large code model we need to do indirect call to __chkstk. + + // R11 will be used to contain the address of __chkstk. + // R11 is a volotiale register and assumed to be destoyed by the callee, + // so there is no need to save and restore it. + BuildMI(*BB, MI, DL, TII->get(X86::MOV64ri), X86::R11) + .addExternalSymbol(StackProbeSymbol); + // Create a call to __chkstk function which address contained in R11. + MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64r)) + .addReg(X86::R11, RegState::Kill); + + } else { + + // For non-large code model we can do direct call to __chkstk. + + MIB = BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA)) + .addExternalSymbol(StackProbeSymbol); + } + if (Subtarget->isTargetCygMing()) { // ___chkstk(Mingw64): // Clobbers R10, R11, RAX and EFLAGS. // Updates RSP. - BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA)) - .addExternalSymbol("___chkstk") + MIB .addReg(X86::RAX, RegState::Implicit) .addReg(X86::RSP, RegState::Implicit) .addReg(X86::RAX, RegState::Define | RegState::Implicit) @@ -15507,8 +15531,7 @@ } else { // __chkstk(MSVCRT): does not update stack pointer. // Clobbers R10, R11 and EFLAGS. - BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA)) - .addExternalSymbol("__chkstk") + MIB .addReg(X86::RAX, RegState::Implicit) .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); // RAX has the offset to be subtracted from RSP. Index: lib/Target/X86/X86FrameLowering.cpp =================================================================== --- lib/Target/X86/X86FrameLowering.cpp 2013-10-24 01:37:01.000000000 +0200 +++ lib/Target/X86/X86FrameLowering.cpp (working copy) @@ -635,25 +635,49 @@ .addReg(X86::EAX, RegState::Kill) .setMIFlag(MachineInstr::FrameSetup); } + + MachineInstrBuilder MIB; if (Is64Bit) { + // Handle the 64-bit Windows ABI case where we need to call __chkstk. // Function prologue is responsible for adjusting the stack pointer. BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::RAX) .addImm(NumBytes) .setMIFlag(MachineInstr::FrameSetup); + + if (TM.getCodeModel() == CodeModel::Large) { + // For large code model we need to do indirect call to __chkstk. + + + // R11 will be used to contain the address of __chkstk. + // R11 is a volotiale register and assumed to be destoyed by the callee, + // so there is no need to save and restore it. + BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::R11) + .addExternalSymbol(StackProbeSymbol); + // Create a call to __chkstk function which address contained in R11. + MIB = BuildMI(MBB, MBBI, DL, TII.get(X86::CALL64r)) + .addReg(X86::R11, RegState::Kill); + } else { + + // For non-large code model we can do direct call to __chkstk. + + MIB = BuildMI(MBB, MBBI, DL, TII.get(X86::W64ALLOCA)) + .addExternalSymbol(StackProbeSymbol); + } } else { // Allocate NumBytes-4 bytes on stack in case of isEAXAlive. // We'll also use 4 already allocated bytes for EAX. BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) .addImm(isEAXAlive ? NumBytes - 4 : NumBytes) .setMIFlag(MachineInstr::FrameSetup); + + MIB = BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32)) + .addExternalSymbol(StackProbeSymbol); } - BuildMI(MBB, MBBI, DL, - TII.get(Is64Bit ? X86::W64ALLOCA : X86::CALLpcrel32)) - .addExternalSymbol(StackProbeSymbol) - .addReg(StackPtr, RegState::Define | RegState::Implicit) + + MIB.addReg(StackPtr, RegState::Define | RegState::Implicit) .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit) .setMIFlag(MachineInstr::FrameSetup);