From ad84c943ffc3e4a8ef7fc534636a5bb548e646e3 Mon Sep 17 00:00:00 2001 From: prabhukr Date: Fri, 2 May 2025 23:26:45 +0000 Subject: [PATCH 1/6] [llvm][X86] REX profile for UEFI Use the appropriate REX profile for UEFI X86_64 target. --- llvm/lib/Target/X86/X86ExpandPseudo.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Target/X86/X86ExpandPseudo.cpp b/llvm/lib/Target/X86/X86ExpandPseudo.cpp index 398b738b85697..5d1f7f7f0fe9d 100644 --- a/llvm/lib/Target/X86/X86ExpandPseudo.cpp +++ b/llvm/lib/Target/X86/X86ExpandPseudo.cpp @@ -302,7 +302,7 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB, } // Jump to label or value in register. - bool IsWin64 = STI->isTargetWin64(); + bool IsWinOrUEFI64 = STI->isTargetWin64() || STI->isTargetUEFI64(); if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) { unsigned Op; @@ -339,16 +339,17 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB, } } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) { - unsigned Op = (Opcode == X86::TCRETURNmi) - ? X86::TAILJMPm - : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64); + unsigned Op = + (Opcode == X86::TCRETURNmi) + ? X86::TAILJMPm + : (IsWinOrUEFI64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64); MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op)); for (unsigned i = 0; i != X86::AddrNumOperands; ++i) MIB.add(MBBI->getOperand(i)); } else if (Opcode == X86::TCRETURNri64) { JumpTarget.setIsKill(); BuildMI(MBB, MBBI, DL, - TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64)) + TII->get(IsWinOrUEFI64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64)) .add(JumpTarget); } else { JumpTarget.setIsKill(); From 4bf4a25d0ac2bafd71e73daa6b386f8483640bf2 Mon Sep 17 00:00:00 2001 From: prabhukr Date: Tue, 6 May 2025 21:40:53 +0000 Subject: [PATCH 2/6] Add test --- llvm/test/CodeGen/X86/rex-profile-test.ll | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 llvm/test/CodeGen/X86/rex-profile-test.ll diff --git a/llvm/test/CodeGen/X86/rex-profile-test.ll b/llvm/test/CodeGen/X86/rex-profile-test.ll new file mode 100644 index 0000000000000..9cb590794e0ab --- /dev/null +++ b/llvm/test/CodeGen/X86/rex-profile-test.ll @@ -0,0 +1,18 @@ +;; Test that the UEFI and Windows targets set the rex64 correctly. +; RUN: llc -mtriple x86_64-uefi %s -o - | FileCheck %s -check-prefix=REX +; RUN: llc -mtriple x86_64-windows-msvc %s -o - | FileCheck %s -check-prefix=REX +; RUN: llc -mtriple x86_64-unknown-linux %s -o - | FileCheck %s -check-prefix=NOREX + +define void @test_tailjmp(ptr %fptr) { +; REX-LABEL: test_tailjmp: # @test_tailjmp +; REX: # %bb.0: # %entry +; REX-NEXT: rex64 jmpq *%rcx # TAILCALL +; +; NOREX-LABEL: test_tailjmp: # @test_tailjmp +; NOREX: .cfi_startproc +; NOREX-NEXT: # %bb.0: # %entry +; NOREX-NEXT: jmpq *%rdi # TAILCALL +entry: + tail call void %fptr() + ret void +} \ No newline at end of file From 4275ba0804ef8531263f6ceabf161c3ac5f11bef Mon Sep 17 00:00:00 2001 From: prabhukr Date: Tue, 6 May 2025 21:42:42 +0000 Subject: [PATCH 3/6] Test formatting fix --- llvm/test/CodeGen/X86/rex-profile-test.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/CodeGen/X86/rex-profile-test.ll b/llvm/test/CodeGen/X86/rex-profile-test.ll index 9cb590794e0ab..2eb1b0e4f67fe 100644 --- a/llvm/test/CodeGen/X86/rex-profile-test.ll +++ b/llvm/test/CodeGen/X86/rex-profile-test.ll @@ -6,7 +6,7 @@ define void @test_tailjmp(ptr %fptr) { ; REX-LABEL: test_tailjmp: # @test_tailjmp ; REX: # %bb.0: # %entry -; REX-NEXT: rex64 jmpq *%rcx # TAILCALL +; REX-NEXT: rex64 jmpq *%rcx # TAILCALL ; ; NOREX-LABEL: test_tailjmp: # @test_tailjmp ; NOREX: .cfi_startproc From ccc1d3c1d437db6d50ab8a7d28a1e635d75342e8 Mon Sep 17 00:00:00 2001 From: prabhukr Date: Wed, 7 May 2025 00:23:50 +0000 Subject: [PATCH 4/6] Add another e2e test. --- llvm/test/CodeGen/X86/musttail-varargs.ll | 1 + llvm/test/CodeGen/X86/rex-profile-test.ll | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/test/CodeGen/X86/musttail-varargs.ll b/llvm/test/CodeGen/X86/musttail-varargs.ll index f9b2ce79a35ca..65cd1edd92e31 100644 --- a/llvm/test/CodeGen/X86/musttail-varargs.ll +++ b/llvm/test/CodeGen/X86/musttail-varargs.ll @@ -2,6 +2,7 @@ ; RUN: llc -verify-machineinstrs < %s -enable-tail-merge=0 -mtriple=x86_64-linux | FileCheck %s --check-prefix=LINUX ; RUN: llc -verify-machineinstrs < %s -enable-tail-merge=0 -mtriple=x86_64-linux-gnux32 | FileCheck %s --check-prefix=LINUX-X32 ; RUN: llc -verify-machineinstrs < %s -enable-tail-merge=0 -mtriple=x86_64-windows | FileCheck %s --check-prefix=WINDOWS +; RUN: llc -verify-machineinstrs < %s -enable-tail-merge=0 -mtriple=x86_64-uefi | FileCheck %s --check-prefix=WINDOWS ; RUN: llc -verify-machineinstrs < %s -enable-tail-merge=0 -mtriple=i686-windows | FileCheck %s --check-prefix=X86 --check-prefix=X86-NOSSE ; RUN: llc -verify-machineinstrs < %s -enable-tail-merge=0 -mtriple=i686-windows -mattr=+sse2 | FileCheck %s --check-prefix=X86 --check-prefix=X86-SSE diff --git a/llvm/test/CodeGen/X86/rex-profile-test.ll b/llvm/test/CodeGen/X86/rex-profile-test.ll index 2eb1b0e4f67fe..379d8faa4fc45 100644 --- a/llvm/test/CodeGen/X86/rex-profile-test.ll +++ b/llvm/test/CodeGen/X86/rex-profile-test.ll @@ -15,4 +15,4 @@ define void @test_tailjmp(ptr %fptr) { entry: tail call void %fptr() ret void -} \ No newline at end of file +} From 1398b0004234acdaad870b680065268b10c3dc08 Mon Sep 17 00:00:00 2001 From: prabhukr Date: Wed, 28 May 2025 22:31:17 +0000 Subject: [PATCH 5/6] Rename local var. --- llvm/lib/Target/X86/X86ExpandPseudo.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Target/X86/X86ExpandPseudo.cpp b/llvm/lib/Target/X86/X86ExpandPseudo.cpp index 5d1f7f7f0fe9d..6ab23e99bda3a 100644 --- a/llvm/lib/Target/X86/X86ExpandPseudo.cpp +++ b/llvm/lib/Target/X86/X86ExpandPseudo.cpp @@ -301,8 +301,9 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB, X86FL->emitSPUpdate(MBB, MBBI, DL, Offset, /*InEpilogue=*/true); } + // Use this predicate to set REX prefix for X86_64 targets. + bool IsX64 = STI->isTargetWin64() || STI->isTargetUEFI64(); // Jump to label or value in register. - bool IsWinOrUEFI64 = STI->isTargetWin64() || STI->isTargetUEFI64(); if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) { unsigned Op; @@ -339,17 +340,16 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB, } } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) { - unsigned Op = - (Opcode == X86::TCRETURNmi) - ? X86::TAILJMPm - : (IsWinOrUEFI64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64); + unsigned Op = (Opcode == X86::TCRETURNmi) + ? X86::TAILJMPm + : (IsX64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64); MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op)); for (unsigned i = 0; i != X86::AddrNumOperands; ++i) MIB.add(MBBI->getOperand(i)); } else if (Opcode == X86::TCRETURNri64) { JumpTarget.setIsKill(); BuildMI(MBB, MBBI, DL, - TII->get(IsWinOrUEFI64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64)) + TII->get(IsX64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64)) .add(JumpTarget); } else { JumpTarget.setIsKill(); From db4070ba678a8e28a715274fbbe6a4af41c011f6 Mon Sep 17 00:00:00 2001 From: prabhukr Date: Thu, 5 Jun 2025 18:13:36 +0000 Subject: [PATCH 6/6] Fix var name on assert --- llvm/lib/Target/X86/X86ExpandPseudo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Target/X86/X86ExpandPseudo.cpp b/llvm/lib/Target/X86/X86ExpandPseudo.cpp index 214c50fd5c084..c7abb367fad28 100644 --- a/llvm/lib/Target/X86/X86ExpandPseudo.cpp +++ b/llvm/lib/Target/X86/X86ExpandPseudo.cpp @@ -353,7 +353,7 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB, TII->get(IsX64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64)) .add(JumpTarget); } else { - assert(!IsWin64 && "Win64 requires REX for indirect jumps."); + assert(!IsX64 && "Win64 and UEFI64 require REX for indirect jumps."); JumpTarget.setIsKill(); BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr)) .add(JumpTarget);