[llvm] [X86] Resolve FIXME: Create cld only when needed (PR #82415)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 21 09:28:25 PST 2024


================
@@ -2194,13 +2194,35 @@ 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.
+        unsigned Opcode = I.getOpcode();
+        if (X86::REP_MOVSB_32 <= Opcode && Opcode <= X86::REP_STOSW_64) {
+          NeedsCLD = true;
+          break;
+        }
----------------
AtariDreams wrote:

Okay, but the main question is that if someone who writes asm knows what they are doing with using rep manually, should we assume they need to have the cld emitted for them? We cannot assume the intention for them like that. For all we know, they could not be expecting cld to happen at all, or they write it themselves.

https://github.com/llvm/llvm-project/pull/82415


More information about the llvm-commits mailing list