[llvm] ff3b085 - [X86] Use bundle for CALL_RVMARKER expansion.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 14 02:53:43 PST 2021


Author: Florian Hahn
Date: 2021-12-14T10:53:22Z
New Revision: ff3b085ab0704ba505c398c3d18bde01a4df8209

URL: https://github.com/llvm/llvm-project/commit/ff3b085ab0704ba505c398c3d18bde01a4df8209
DIFF: https://github.com/llvm/llvm-project/commit/ff3b085ab0704ba505c398c3d18bde01a4df8209.diff

LOG: [X86] Use bundle for CALL_RVMARKER expansion.

This patch updates expandCALL_RVMARKER to wrap the call, marker and
objc runtime call in an instruction bundle. This ensures later passes,
like machine block placement, cannot break them up.

On AArch64, the instruction sequence is already wrapped in a bundle.
Keeping the whole instruction sequence together is highly desirable for
performance and outweighs potential other benefits from breaking the
sequence up.

Reviewed By: ahatanak

Differential Revision: https://reviews.llvm.org/D115230

Added: 
    

Modified: 
    llvm/lib/Target/X86/X86ExpandPseudo.cpp
    llvm/lib/Target/X86/X86TargetMachine.cpp
    llvm/test/CodeGen/X86/call-rv-marker.ll
    llvm/test/CodeGen/X86/expand-call-rvmarker.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86ExpandPseudo.cpp b/llvm/lib/Target/X86/X86ExpandPseudo.cpp
index 93bc23006dc4a..6a047838f0b5e 100644
--- a/llvm/lib/Target/X86/X86ExpandPseudo.cpp
+++ b/llvm/lib/Target/X86/X86ExpandPseudo.cpp
@@ -191,8 +191,6 @@ void X86ExpandPseudo::expandCALL_RVMARKER(MachineBasicBlock &MBB,
                                           MachineBasicBlock::iterator MBBI) {
   // Expand CALL_RVMARKER pseudo to call instruction, followed by the special
   //"movq %rax, %rdi" marker.
-  // TODO: Mark the sequence as bundle, to avoid passes moving other code
-  // in between.
   MachineInstr &MI = *MBBI;
 
   MachineInstr *OriginalCall;
@@ -236,15 +234,23 @@ void X86ExpandPseudo::expandCALL_RVMARKER(MachineBasicBlock &MBB,
   // Emit call to ObjC runtime.
   const uint32_t *RegMask =
       TRI->getCallPreservedMask(*MBB.getParent(), CallingConv::C);
-  BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(X86::CALL64pcrel32))
-      .addGlobalAddress(MI.getOperand(0).getGlobal(), 0, 0)
-      .addRegMask(RegMask)
-      .addReg(X86::RAX,
-              RegState::Implicit |
-                  (RAXImplicitDead ? (RegState::Dead | RegState::Define)
-                                   : RegState::Define))
-      .getInstr();
+  MachineInstr *RtCall =
+      BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(X86::CALL64pcrel32))
+          .addGlobalAddress(MI.getOperand(0).getGlobal(), 0, 0)
+          .addRegMask(RegMask)
+          .addReg(X86::RAX,
+                  RegState::Implicit |
+                      (RAXImplicitDead ? (RegState::Dead | RegState::Define)
+                                       : RegState::Define))
+          .getInstr();
   MI.eraseFromParent();
+
+  auto &TM = MBB.getParent()->getTarget();
+  // On Darwin platforms, wrap the expanded sequence in a bundle to prevent
+  // later optimizations from breaking up the sequence.
+  if (TM.getTargetTriple().isOSDarwin())
+    finalizeBundle(MBB, OriginalCall->getIterator(),
+                   std::next(RtCall->getIterator()));
 }
 
 /// If \p MBBI is a pseudo instruction, this method expands

diff  --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index 336985f3bf9dd..78bc5519c23ff 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -588,6 +588,18 @@ void X86PassConfig::addPreEmitPass2() {
 
   // Insert pseudo probe annotation for callsite profiling
   addPass(createPseudoProbeInserter());
+
+  // On Darwin platforms, BLR_RVMARKER pseudo instructions are lowered to
+  // bundles.
+  if (TT.isOSDarwin())
+    addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
+      // Only run bundle expansion if there are relevant ObjC runtime functions
+      // present in the module.
+      const Function &F = MF.getFunction();
+      const Module *M = F.getParent();
+      return M->getFunction("objc_retainAutoreleasedReturnValue") ||
+             M->getFunction("objc_unsafeClaimAutoreleasedReturnValue");
+    }));
 }
 
 bool X86PassConfig::addPostFastRegAllocRewrite() {

diff  --git a/llvm/test/CodeGen/X86/call-rv-marker.ll b/llvm/test/CodeGen/X86/call-rv-marker.ll
index 609e53a89f33a..0848bec01723e 100644
--- a/llvm/test/CodeGen/X86/call-rv-marker.ll
+++ b/llvm/test/CodeGen/X86/call-rv-marker.ll
@@ -232,14 +232,16 @@ define i8* @rv_marker_block_placement(i1 %c.0) {
 
 ; CHECK-NEXT: ## %bb.1:
 ; CHECK-NEXT:   callq   _fn1
+; CHECK-NEXT:   movq    %rax, %rdi
+; CHECK-NEXT:   callq   _objc_retainAutoreleasedReturnValue
 ; CHECK-NEXT:   jmp LBB8_3
 
 ; CHECK-NEXT: LBB8_2:
 ; CHECK-NEXT:   callq   _fn2
-
-; CHECK-NEXT: LBB8_3:
 ; CHECK-NEXT:   movq    %rax, %rdi
 ; CHECK-NEXT:   callq   _objc_retainAutoreleasedReturnValue
+
+; CHECK-NEXT: LBB8_3:
 ; CHECK-NEXT:   xorl    %eax, %eax
 ; CHECK-NEXT:   popq    %rcx
 ; CHECK-NEXT:   retq

diff  --git a/llvm/test/CodeGen/X86/expand-call-rvmarker.mir b/llvm/test/CodeGen/X86/expand-call-rvmarker.mir
index 7124d9934d9ca..4f16265e1ea95 100644
--- a/llvm/test/CodeGen/X86/expand-call-rvmarker.mir
+++ b/llvm/test/CodeGen/X86/expand-call-rvmarker.mir
@@ -30,9 +30,11 @@
 # CHECK:      bb.0
 # CHECK-NEXT:   frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   CFI_INSTRUCTION def_cfa_offset 16
-# CHECK-NEXT:   CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
-# CHECK-NEXT:   $rdi = MOV64rr $rax
-# CHECK-NEXT:   CALL64pcrel32 @objc_retainAutoreleasedReturnValue, csr_64, implicit $rsp, implicit $ssp, implicit-def $rax
+# CHECK-NEXT:   BUNDLE
+# CHECK-NEXT:     CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
+# CHECK-NEXT:     $rdi = MOV64rr internal $rax
+# CHECK-NEXT:     CALL64pcrel32 @objc_retainAutoreleasedReturnValue, csr_64, implicit internal $rsp, implicit internal $ssp, implicit-def $rax
+# CHECK-NEXT:   }
 # CHECK-NEXT:   $rcx = frame-destroy POP64r implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   RET64
 #
@@ -62,9 +64,11 @@ body:             |
 # CHECK:      bb.0
 # CHECK-NEXT:   frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   CFI_INSTRUCTION def_cfa_offset 16
-# CHECK-NEXT:   CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
-# CHECK-NEXT:   $rdi = MOV64rr $rax
-# CHECK-NEXT:   CALL64pcrel32 @objc_unsafeClaimAutoreleasedReturnValue, csr_64, implicit $rsp, implicit $ssp, implicit-def $rax
+# CHECK-NEXT:   BUNDLE
+# CHECK-NEXT:     CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
+# CHECK-NEXT:     $rdi = MOV64rr internal $rax
+# CHECK-NEXT:     CALL64pcrel32 @objc_unsafeClaimAutoreleasedReturnValue, csr_64, implicit internal $rsp, implicit internal $ssp, implicit-def $rax
+# CHECK-NEXT:   }
 # CHECK-NEXT:   $rcx = frame-destroy POP64r implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   RET64
 #
@@ -95,9 +99,11 @@ body:             |
 # CHECK-NEXT:   $rax = MOV64rr $rdi
 # CHECK-NEXT:   $rdi = MOV64rr killed $rdx
 # CHECK-NEXT:   $rdx = MOV64rr killed $rax
-# CHECK-NEXT:   CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit $rdi, implicit $rsi, implicit $rdx, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
-# CHECK-NEXT:   $rdi = MOV64rr $rax
-# CHECK-NEXT:   CALL64pcrel32 @objc_retainAutoreleasedReturnValue, csr_64, implicit $rsp, implicit $ssp, implicit-def dead $rax
+# CHECK-NEXT:   BUNDLE
+# CHECK-NEXT:     CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit $rdi, implicit $rsi, implicit $rdx, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
+# CHECK-NEXT:     $rdi = MOV64rr internal $rax
+# CHECK-NEXT:     CALL64pcrel32 @objc_retainAutoreleasedReturnValue, csr_64, implicit internal $rsp, implicit internal $ssp, implicit-def dead $rax
+# CHECK-NEXT:  }
 # CHECK-NEXT:   $rax = frame-destroy POP64r implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   RET64
 #
@@ -129,9 +135,11 @@ body:             |
 # CHECK:      bb.0
 # CHECK-NEXT:   frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   CFI_INSTRUCTION def_cfa_offset 16
-# CHECK-NEXT:   CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
-# CHECK-NEXT:   $rdi = MOV64rr $rax
-# CHECK-NEXT:   CALL64pcrel32 @objc_retainAutoreleasedReturnValue, csr_64, implicit $rsp, implicit $ssp, implicit-def dead $rax
+# CHECK-NEXT:   BUNDLE
+# CHECK-NEXT:     CALL64pcrel32 @fn, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
+# CHECK-NEXT:     $rdi = MOV64rr internal $rax
+# CHECK-NEXT:     CALL64pcrel32 @objc_retainAutoreleasedReturnValue, csr_64, implicit internal $rsp, implicit internal $ssp, implicit-def dead $rax
+# CHECK-NEXT:  }
 # CHECK-NEXT:   $rax = frame-destroy POP64r implicit-def $rsp, implicit $rsp
 # CHECK-NEXT:   RET64
 #


        


More information about the llvm-commits mailing list