diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td --- a/llvm/lib/Target/X86/X86InstrCompiler.td +++ b/llvm/lib/Target/X86/X86InstrCompiler.td @@ -1235,6 +1235,22 @@ return true; }]>; +def X86tcret_1reg : PatFrag<(ops node:$ptr, node:$off), + (X86tcret node:$ptr, node:$off), [{ + // X86tcret args: (*chain, ptr, imm, regs..., glue) + unsigned NumRegs = 1; + LoadSDNode* ld = dyn_cast(N->getOperand(1)); + const SDValue& BasePtr = ld->getBasePtr(); + if (isa(BasePtr)) + NumRegs = 3; + else if (BasePtr->getNumOperands() && isa(BasePtr->getOperand(0))) + NumRegs = 3; + for (unsigned i = 3, e = N->getNumOperands(); i != e; ++i) + if (isa(N->getOperand(i)) && ( NumRegs-- == 0)) + return false; + return true; +}]>; + def : Pat<(X86tcret ptr_rc_tailcall:$dst, timm:$off), (TCRETURNri ptr_rc_tailcall:$dst, timm:$off)>, Requires<[Not64BitMode, NotUseIndirectThunkCalls]>; @@ -1242,7 +1258,8 @@ // FIXME: This is disabled for 32-bit PIC mode because the global base // register which is part of the address mode may be assigned a // callee-saved register. -def : Pat<(X86tcret (load addr:$dst), timm:$off), +// Similar to X86tcret_6regs, here we only have 1 register left +def : Pat<(X86tcret_1reg (load addr:$dst), timm:$off), (TCRETURNmi addr:$dst, timm:$off)>, Requires<[Not64BitMode, IsNotPIC, NotUseIndirectThunkCalls]>; diff --git a/llvm/test/CodeGen/X86/tailcall-3regparm.ll b/llvm/test/CodeGen/X86/tailcall-3regparm.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/tailcall-3regparm.ll @@ -0,0 +1,30 @@ +; RUN: llc < %s -mtriple=i686-linux-gnu -mcpu=pentium | FileCheck %s + +; Tail call should not make register allocation fail (x86-32) + +%struct.anon = type { i32 (%struct.BIG_PARM*, i8*)*, i32 ()*, i32 ()*, i32 ()*, i32 ()*, i32 ()*, i32 ()* } +%struct.BIG_PARM = type { i32 } + +@vtable = internal unnamed_addr constant [1 x %struct.anon] [%struct.anon { i32 (%struct.BIG_PARM*, i8*)* inttoptr (i32 -559038737 to i32 (%struct.BIG_PARM*, i8*)*), i32 ()* null, i32 ()* null, i32 ()* null, i32 ()* null, i32 ()* null, i32 ()* null }], align 4 + +; Function Attrs: nounwind uwtable +define dso_local i32 @something(%struct.BIG_PARM* inreg noundef %a, i8* inreg noundef %b) local_unnamed_addr #0 { +entry: + ; CHECK: movl (%eax), %ecx + ; CHECK-NEXT: leal (%ecx,%ecx,8), %esi + ; CHECK-NEXT: leal (%esi,%esi,2), %esi + ; CHECK-NEXT: movl vtable(%ecx,%esi), %ecx + ; CHECK-NEXT: popl %esi + ; CHECK: jmpl *%ecx + %ver = getelementptr inbounds %struct.BIG_PARM, %struct.BIG_PARM* %a, i32 0, i32 0 + %0 = load i32, i32* %ver, align 4 + %foo = getelementptr [1 x %struct.anon], [1 x %struct.anon]* @vtable, i32 0, i32 %0, i32 0 + %1 = load i32 (%struct.BIG_PARM*, i8*)*, i32 (%struct.BIG_PARM*, i8*)** %foo, align 4 + %call = tail call i32 %1(%struct.BIG_PARM* inreg noundef %a, i8* inreg noundef %b) #1 + ret i32 %call +} + +!llvm.module.flags = !{!0} + +!0 = !{i32 1, !"NumRegisterParameters", i32 3} +