[llvm] Resolve fixme: create cld only when needed (PR #82415)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 20 12:33:31 PST 2024
https://github.com/AtariDreams created https://github.com/llvm/llvm-project/pull/82415
None
>From 56d20b5687c9db6866b51d83ee14a62007923b1b Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Tue, 20 Feb 2024 15:22:01 -0500
Subject: [PATCH] Resolve fixme: create cld only when needed
---
llvm/lib/Target/X86/X86FrameLowering.cpp | 44 +++++++++++++++++++++---
1 file changed, 40 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index be416fb0db0695..569cd3330de2f2 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -2194,13 +2194,49 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
// flag (DF in EFLAGS register). Clear this flag by creating "cld" instruction
// in each prologue of interrupt handler function.
//
- // FIXME: Create "cld" instruction only in these cases:
+ // Create "cld" instruction only in these cases:
// 1. The interrupt handling function uses any of the "rep" instructions.
// 2. Interrupt handling function calls another function.
//
- if (Fn.getCallingConv() == CallingConv::X86_INTR)
- BuildMI(MBB, MBBI, DL, TII.get(X86::CLD))
- .setMIFlag(MachineInstr::FrameSetup);
+ if (Fn.getCallingConv() == CallingConv::X86_INTR) {
+ bool NeedsCLD = false;
+
+ // Check if the function calls another function.
+ for (const BasicBlock &BB : Fn) {
+ for (const Instruction &I : BB) {
+ if (isa<CallInst>(I)) {
+ NeedsCLD = true;
+ break;
+ }
+
+ // Check for rep opcode.
+ if (auto *MI = dyn_cast<MachineInstr>(&I)) {
+ unsigned Opcode = MI->getOpcode();
+ if (Opcode == X86::REP_PREFIX || Opcode == X86::REPNE_PREFIX) {
+ NeedsCLD = true;
+ break;
+ }
+ }
+ }
+ }
+
+ // Check if the function uses any "rep" instructions.
+ if (!NeedsCLD) {
+ for (const BasicBlock &BB : Fn) {
+ for (const Instruction &I : BB) {
+ if (/* check if I is a "rep" instruction */) {
+ NeedsCLD = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (NeedsCLD) {
+ BuildMI(MBB, MBBI, DL, TII.get(X86::CLD))
+ .setMIFlag(MachineInstr::FrameSetup);
+ }
+ }
// At this point we know if the function has WinCFI or not.
MF.setHasWinCFI(HasWinCFI);
More information about the llvm-commits
mailing list