[PATCH] D128263: [SPARC] Don't do leaf optimization on procedures with inline assembly

Koakuma via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 21 05:24:40 PDT 2022


koakuma created this revision.
koakuma added reviewers: brad, LemonBoy.
Herald added subscribers: jrtc27, fedor.sergeev, hiraditya, jyknight.
Herald added a project: All.
koakuma requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

On SPARC, leaf function optimization omits the register window sliding (and the associated register name changes). This might result in miscompilation of procedures containing inline assembly, as some of the register constraints used may interfere with the register usage of optimized functions, so we disable leaf procedure optimization on those procedures to prevent it from happening.

This is a continuation of patch D102342 <https://reviews.llvm.org/D102342> by @LemonBoy, the original comment is reproduced below:

> Leaf functions allow the compiler to omit the setup and teardown of a frame pointer, therefore avoiding the exchange of the in/out register. According to the SPARC architecture manual every reference to %i0-%i5 should be replaced with %o0-o5, if the target register is already in use a further remapping step to %g1-%g7 is required to free the output register.
>
> Add a simple check to make sure not to stomp on any output register that's already in use.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128263

Files:
  llvm/lib/Target/Sparc/SparcFrameLowering.cpp
  llvm/test/CodeGen/SPARC/leafproc.ll


Index: llvm/test/CodeGen/SPARC/leafproc.ll
===================================================================
--- llvm/test/CodeGen/SPARC/leafproc.ll
+++ llvm/test/CodeGen/SPARC/leafproc.ll
@@ -78,3 +78,25 @@
   %4 = load i32, i32* %3, align 4
   ret i32 %4
 }
+
+; Here we have a leaf function where it contains inline assembly, which means
+; that register renumbering might interfere with the register constraints.
+; As a result the function is not marked as being a leaf one.
+
+; CHECK-LABEL: leaf_proc_give_up
+; CHECK: save %sp, -96, %sp
+; CHECK: ld [%fp+92], %o5
+; CHECK: mov %i0, %g1
+; CHECK: mov %i1, %o0
+; CHECK: mov %i2, %o1
+; CHECK: mov %i3, %o2
+; CHECK: mov %i4, %o3
+; CHECK: mov %i5, %o4
+; CHECK: ret
+; CHECK-NEXT: restore %g0, %o0, %o0
+
+define i32 @leaf_proc_give_up(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6) {
+Entry:
+  %g = call i32 asm sideeffect "", "={o0},{g1},{o0},{o1},{o2},{o3},{o4},{o5}"(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6)
+  ret i32 %g
+}
Index: llvm/lib/Target/Sparc/SparcFrameLowering.cpp
===================================================================
--- llvm/lib/Target/Sparc/SparcFrameLowering.cpp
+++ llvm/lib/Target/Sparc/SparcFrameLowering.cpp
@@ -326,10 +326,11 @@
   MachineRegisterInfo &MRI = MF.getRegInfo();
   MachineFrameInfo    &MFI = MF.getFrameInfo();
 
-  return !(MFI.hasCalls()                  // has calls
-           || MRI.isPhysRegUsed(SP::L0)    // Too many registers needed
-           || MRI.isPhysRegUsed(SP::O6)    // %sp is used
-           || hasFP(MF));                  // need %fp
+  return !(MFI.hasCalls()               // has calls
+           || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
+           || MRI.isPhysRegUsed(SP::O6) // %sp is used
+           || hasFP(MF)                 // need %fp
+           || MF.hasInlineAsm());       // has inline assembly
 }
 
 void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128263.438647.patch
Type: text/x-patch
Size: 1975 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220621/2974d49e/attachment.bin>


More information about the llvm-commits mailing list