llvm.org GIT mirror llvm / ac226bb
Target/X86: Add explicit Win64 and System V/x86-64 calling conventions. Summary: This patch adds explicit calling convention types for the Win64 and System V/x86-64 ABIs. This allows code to override the default, and use the Win64 convention on a target that wants to use SysV (and vice-versa). This is needed to implement the `ms_abi` and `sysv_abi` GNU attributes. Reviewers: CC: git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186144 91177308-0d34-0410-b5e6-96231b3b80d8 Charles Davis 6 years ago
12 changed file(s) with 112 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
118118 SPIR_KERNEL = 76,
119119
120120 /// Intel_OCL_BI - Calling conventions for Intel OpenCL built-ins
121 Intel_OCL_BI = 77
121 Intel_OCL_BI = 77,
122122
123 /// \brief The C convention as specified in the x86-64 supplement to the
124 /// System V ABI, used on most non-Windows systems.
125 X86_64_SysV = 78,
126
127 /// \brief The C convention as implemented on Windows/x86-64. This
128 /// convention differs from the more common \c X86_64_SysV convention
129 /// in a number of ways, most notably in that XMM registers used to pass
130 /// arguments are shadowed by GPRs, and vice versa.
131 X86_64_Win64 = 79
123132 };
124133 } // End CallingConv namespace
125134
555555 KEYWORD(spir_kernel);
556556 KEYWORD(spir_func);
557557 KEYWORD(intel_ocl_bicc);
558 KEYWORD(x86_64_sysvcc);
559 KEYWORD(x86_64_win64cc);
558560
559561 KEYWORD(cc);
560562 KEYWORD(c);
13431343 /// ::= 'ptx_device'
13441344 /// ::= 'spir_func'
13451345 /// ::= 'spir_kernel'
1346 /// ::= 'x86_64_sysvcc'
1347 /// ::= 'x86_64_win64cc'
13461348 /// ::= 'cc' UINT
13471349 ///
13481350 bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
13631365 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
13641366 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
13651367 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
1368 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1369 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
13661370 case lltok::kw_cc: {
13671371 unsigned ArbitraryCC;
13681372 Lex.Lex();
8383 kw_c,
8484
8585 kw_cc, kw_ccc, kw_fastcc, kw_coldcc,
86 kw_intel_ocl_bicc,
86 kw_intel_ocl_bicc,
8787 kw_x86_stdcallcc, kw_x86_fastcallcc, kw_x86_thiscallcc,
8888 kw_arm_apcscc, kw_arm_aapcscc, kw_arm_aapcs_vfpcc,
8989 kw_msp430_intrcc,
9090 kw_ptx_kernel, kw_ptx_device,
9191 kw_spir_kernel, kw_spir_func,
92 kw_x86_64_sysvcc, kw_x86_64_win64cc,
9293
9394 // Attributes:
9495 kw_attributes,
8383 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
8484 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
8585 case CallingConv::PTX_Device: Out << "ptx_device"; break;
86 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
87 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
8688 }
8789 }
8890
155155 def RetCC_X86_64 : CallingConv<[
156156 // HiPE uses RetCC_X86_64_HiPE
157157 CCIfCC<"CallingConv::HiPE", CCDelegateTo>,
158
159 // Handle explicit CC selection
160 CCIfCC<"CallingConv::X86_64_Win64", CCDelegateTo>,
161 CCIfCC<"CallingConv::X86_64_SysV", CCDelegateTo>,
162
158163 // Mingw64 and native Win64 use Win64 CC
159164 CCIfSubtarget<"isTargetWin64()", CCDelegateTo>,
160165
488493 def CC_X86_64 : CallingConv<[
489494 CCIfCC<"CallingConv::GHC", CCDelegateTo>,
490495 CCIfCC<"CallingConv::HiPE", CCDelegateTo>,
496 CCIfCC<"CallingConv::X86_64_Win64", CCDelegateTo>,
497 CCIfCC<"CallingConv::X86_64_SysV", CCDelegateTo>,
491498
492499 // Mingw64 and native Win64 use Win64 CC
493500 CCIfSubtarget<"isTargetWin64()", CCDelegateTo>,
711711 CallingConv::ID CC = F.getCallingConv();
712712 if (CC != CallingConv::C &&
713713 CC != CallingConv::Fast &&
714 CC != CallingConv::X86_FastCall)
715 return false;
716
717 if (Subtarget->isTargetWin64())
714 CC != CallingConv::X86_FastCall &&
715 CC != CallingConv::X86_64_SysV)
716 return false;
717
718 if (Subtarget->isCallingConvWin64(CC))
718719 return false;
719720
720721 // Don't handle popping bytes on return for now.
17041705 if (!FuncInfo.CanLowerReturn)
17051706 return false;
17061707
1707 if (Subtarget->isTargetWin64())
1708 return false;
1709
17101708 const Function *F = FuncInfo.Fn;
17111709 if (F->isVarArg())
17121710 return false;
17141712 CallingConv::ID CC = F->getCallingConv();
17151713 if (CC != CallingConv::C)
17161714 return false;
1717
1715
1716 if (Subtarget->isCallingConvWin64(CC))
1717 return false;
1718
17181719 if (!Subtarget->is64Bit())
17191720 return false;
17201721
18161817 // Handle only C and fastcc calling conventions for now.
18171818 ImmutableCallSite CS(CI);
18181819 CallingConv::ID CC = CS.getCallingConv();
1820 bool isWin64 = Subtarget->isCallingConvWin64(CC);
18191821 if (CC != CallingConv::C && CC != CallingConv::Fast &&
1820 CC != CallingConv::X86_FastCall)
1822 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1823 CC != CallingConv::X86_64_SysV)
18211824 return false;
18221825
18231826 // fastcc with -tailcallopt is intended to provide a guaranteed
18311834
18321835 // Don't know how to handle Win64 varargs yet. Nothing special needed for
18331836 // x86-32. Special handling for x86-64 is implemented.
1834 if (isVarArg && Subtarget->isTargetWin64())
1837 if (isVarArg && isWin64)
18351838 return false;
18361839
18371840 // Fast-isel doesn't know about callee-pop yet.
19611964 I->getParent()->getContext());
19621965
19631966 // Allocate shadow area for Win64
1964 if (Subtarget->isTargetWin64())
1967 if (isWin64)
19651968 CCInfo.AllocateStack(32, 8);
19661969
19671970 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
20772080 X86::EBX).addReg(Base);
20782081 }
20792082
2080 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
2083 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
20812084 // Count the number of XMM registers allocated.
20822085 static const uint16_t XMMArgRegs[] = {
20832086 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
21462149 if (Subtarget->isPICStyleGOT())
21472150 MIB.addReg(X86::EBX, RegState::Implicit);
21482151
2149 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
2152 if (Subtarget->is64Bit() && isVarArg && !isWin64)
21502153 MIB.addReg(X86::AL, RegState::Implicit);
21512154
21522155 // Add implicit physical register uses to the call.
18791879 CC == CallingConv::HiPE);
18801880 }
18811881
1882 /// \brief Return true if the calling convention is a C calling convention.
1883 static bool IsCCallConvention(CallingConv::ID CC) {
1884 return (CC == CallingConv::C || CC == CallingConv::X86_64_Win64 ||
1885 CC == CallingConv::X86_64_SysV);
1886 }
1887
18821888 bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
18831889 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
18841890 return false;
18851891
18861892 CallSite CS(CI);
18871893 CallingConv::ID CalleeCC = CS.getCallingConv();
1888 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1894 if (!IsTailCallConvention(CalleeCC) && !IsCCallConvention(CalleeCC))
18891895 return false;
18901896
18911897 return true;
19601966 MachineFrameInfo *MFI = MF.getFrameInfo();
19611967 bool Is64Bit = Subtarget->is64Bit();
19621968 bool IsWindows = Subtarget->isTargetWindows();
1963 bool IsWin64 = Subtarget->isTargetWin64();
1969 bool IsWin64 = Subtarget->isCallingConvWin64(CallConv);
19641970
19651971 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
19661972 "Var args not supported with calling convention fastcc, ghc or hipe");
19711977 ArgLocs, *DAG.getContext());
19721978
19731979 // Allocate shadow area for Win64
1974 if (IsWin64) {
1980 if (IsWin64)
19751981 CCInfo.AllocateStack(32, 8);
1976 }
19771982
19781983 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
19791984
22862291
22872292 MachineFunction &MF = DAG.getMachineFunction();
22882293 bool Is64Bit = Subtarget->is64Bit();
2289 bool IsWin64 = Subtarget->isTargetWin64();
2294 bool IsWin64 = Subtarget->isCallingConvWin64(CallConv);
22902295 bool IsWindows = Subtarget->isTargetWindows();
22912296 StructReturnType SR = callIsStructReturn(Outs);
22922297 bool IsSibcall = false;
23192324 ArgLocs, *DAG.getContext());
23202325
23212326 // Allocate shadow area for Win64
2322 if (IsWin64) {
2327 if (IsWin64)
23232328 CCInfo.AllocateStack(32, 8);
2324 }
23252329
23262330 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
23272331
28322836 const SmallVectorImpl &OutVals,
28332837 const SmallVectorImpl &Ins,
28342838 SelectionDAG &DAG) const {
2835 if (!IsTailCallConvention(CalleeCC) &&
2836 CalleeCC != CallingConv::C)
2839 if (!IsTailCallConvention(CalleeCC) && !IsCCallConvention(CalleeCC))
28372840 return false;
28382841
28392842 // If -tailcallopt is specified, make fastcc functions tail-callable.
28402843 const MachineFunction &MF = DAG.getMachineFunction();
2841 const Function *CallerF = DAG.getMachineFunction().getFunction();
2844 const Function *CallerF = MF.getFunction();
28422845
28432846 // If the function return type is x86_fp80 and the callee return type is not,
28442847 // then the FP_EXTEND of the call result is not a nop. It's not safe to
28482851
28492852 CallingConv::ID CallerCC = CallerF->getCallingConv();
28502853 bool CCMatch = CallerCC == CalleeCC;
2854 bool IsCalleeWin64 = Subtarget->isCallingConvWin64(CalleeCC);
2855 bool IsCallerWin64 = Subtarget->isCallingConvWin64(CallerCC);
28512856
28522857 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
28532858 if (IsTailCallConvention(CalleeCC) && CCMatch)
28812886
28822887 // Optimizing for varargs on Win64 is unlikely to be safe without
28832888 // additional testing.
2884 if (Subtarget->isTargetWin64())
2889 if (IsCalleeWin64 || IsCallerWin64)
28852890 return false;
28862891
28872892 SmallVector ArgLocs;
29562961 getTargetMachine(), ArgLocs, *DAG.getContext());
29572962
29582963 // Allocate shadow area for Win64
2959 if (Subtarget->isTargetWin64()) {
2964 if (IsCalleeWin64)
29602965 CCInfo.AllocateStack(32, 8);
2961 }
29622966
29632967 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
29642968 if (CCInfo.getNextStackOffset()) {
337337 }
338338 bool isPICStyleStubAny() const {
339339 return PICStyle == PICStyles::StubDynamicNoPIC ||
340 PICStyle == PICStyles::StubPIC; }
340 PICStyle == PICStyles::StubPIC;
341 }
342
343 bool isCallingConvWin64(CallingConv::ID CC) const {
344 return (isTargetWin64() && CC != CallingConv::X86_64_SysV) ||
345 CC == CallingConv::X86_64_Win64;
346 }
341347
342348 /// ClassifyGlobalReference - Classify a global variable reference for the
343349 /// current subtarget according to how we should reference it in a non-pcrel
0 ; RUN: llc < %s -mtriple=x86_64-pc-win32 | FileCheck %s
1 ; RUN: llc < %s -mtriple=x86_64-pc-linux | FileCheck %s -check-prefix=LINUX
12
23 ; Verify that the 5th and 6th parameters are coming from the correct location
34 ; on the stack.
56 entry:
67 ; CHECK: movl 48(%rsp), %eax
78 ; CHECK: addl 40(%rsp), %eax
9 ; LINUX: addl %r9d, %r8d
10 ; LINUX: movl %r8d, %eax
811 %add = add nsw i32 %p6, %p5
912 ret i32 %add
1013 }
14
15 define x86_64_win64cc i32 @f7(i32 %p1, i32 %p2, i32 %p3, i32 %p4, i32 %p5, i32 %p6) nounwind readnone optsize {
16 entry:
17 ; CHECK: movl 48(%rsp), %eax
18 ; CHECK: addl 40(%rsp), %eax
19 ; LINUX: movl 48(%rsp), %eax
20 ; LINUX: addl 40(%rsp), %eax
21 %add = add nsw i32 %p6, %p5
22 ret i32 %add
23 }
24
25 ; Verify that even though we're compiling for Windows, parameters behave as
26 ; on other platforms here (note the x86_64_sysvcc calling convention).
27 define x86_64_sysvcc i32 @f8(i32 %p1, i32 %p2, i32 %p3, i32 %p4, i32 %p5, i32 %p6) nounwind readnone optsize {
28 entry:
29 ; CHECK: addl %r9d, %r8d
30 ; CHECK: movl %r8d, %eax
31 ; LINUX: addl %r9d, %r8d
32 ; LINUX: movl %r8d, %eax
33 %add = add nsw i32 %p6, %p5
34 ret i32 %add
35 }
4444 %array128 = alloca [128 x i8], align 16 ; <[128 x i8]*> [#uses=0]
4545 ret i32 0
4646 }
47
48 ; Make sure we don't call __chkstk or __alloca on non-Windows even if the
49 ; caller has the Win64 calling convention.
50 define x86_64_win64cc i32 @main4k_win64() nounwind {
51 entry:
52 ; WIN_X32: calll __chkstk
53 ; WIN_X64: callq __chkstk
54 ; MINGW_X32: calll __alloca
55 ; MINGW_X64: callq ___chkstk
56 ; LINUX-NOT: call __chkstk
57 %array4096 = alloca [4096 x i8], align 16 ; <[4096 x i8]*> [#uses=0]
58 ret i32 0
59 }
4949 syn keyword llvmKeyword noimplicitfloat noinline nonlazybind noredzone noreturn
5050 syn keyword llvmKeyword nounwind optsize personality private protected
5151 syn keyword llvmKeyword ptx_device ptx_kernel readnone readonly release
52 syn keyword llvmKeyword returns_twice section seq_cst sideeffect signext
53 syn keyword llvmKeyword singlethread spir_func spir_kernel sret ssp sspreq
54 syn keyword llvmKeyword sspstrong tail target thread_local to triple
55 syn keyword llvmKeyword unnamed_addr unordered uwtable volatile weak weak_odr
56 syn keyword llvmKeyword x86_fastcallcc x86_stdcallcc x86_thiscallcc zeroext
57 syn keyword llvmKeyword sanitize_thread sanitize_memory
52 syn keyword llvmKeyword returns_twice sanitize_thread sanitize_memory
53 syn keyword llvmKeyword section seq_cst sideeffect signext singlethread
54 syn keyword llvmKeyword spir_func spir_kernel sret ssp sspreq sspstrong
55 syn keyword llvmKeyword tail target thread_local to triple unnamed_addr
56 syn keyword llvmKeyword unordered uwtable volatile weak weak_odr
57 syn keyword llvmKeyword x86_fastcallcc x86_stdcallcc x86_thiscallcc x86_64_sysvcc
58 syn keyword llvmKeyword x86_64_win64cc zeroext
5859
5960 " Obsolete keywords.
6061 syn keyword llvmError getresult begin end