[llvm-branch-commits] [llvm] 653b976 - [SystemZ] Improve handling of backchain offset.

Jonas Paulsson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Dec 14 10:44:21 PST 2020


Author: Jonas Paulsson
Date: 2020-12-14T12:39:38-06:00
New Revision: 653b97690f0d5c26a93002fb94ddd86397ed6c1c

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

LOG: [SystemZ] Improve handling of backchain offset.

- New function SDValue getBackchainAddress() used by
  lowerDYNAMIC_STACKALLOC() and lowerSTACKRESTORE() to properly handle the
  backchain offset also with packed-stack.

- Make a common function getBackchainOffset() for the computation of the
  backchain offset and use in some places (NFC).

Review: Ulrich Weigand

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

Added: 
    

Modified: 
    llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
    llvm/lib/Target/SystemZ/SystemZFrameLowering.h
    llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
    llvm/lib/Target/SystemZ/SystemZISelLowering.h
    llvm/test/CodeGen/SystemZ/backchain.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp b/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
index d9e030de5af8..994f471b75b1 100644
--- a/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
@@ -511,13 +511,10 @@ void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
           .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
       emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
       buildCFAOffs(MBB, MBBI, DL, SPOffsetFromCFA + Delta, ZII);
-      if (StoreBackchain) {
-        // The back chain is stored topmost with packed-stack.
-        int Offset = usePackedStack(MF) ? SystemZMC::CallFrameSize - 8 : 0;
+      if (StoreBackchain)
         BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
           .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
-          .addImm(Offset).addReg(0);
-      }
+          .addImm(getBackchainOffset(MF)).addReg(0);
     }
     SPOffsetFromCFA += Delta;
   }
@@ -709,13 +706,10 @@ void SystemZFrameLowering::inlineStackProbe(MachineFunction &MF,
   if (Residual)
     allocateAndProbe(*MBB, MBBI, Residual, true/*EmitCFI*/);
 
-  if (StoreBackchain) {
-    // The back chain is stored topmost with packed-stack.
-    int Offset = usePackedStack(MF) ? SystemZMC::CallFrameSize - 8 : 0;
+  if (StoreBackchain)
     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::STG))
       .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
-      .addImm(Offset).addReg(0);
-  }
+      .addImm(getBackchainOffset(MF)).addReg(0);
 
   StackAllocMI->eraseFromParent();
   if (DoneMBB != nullptr) {
@@ -790,8 +784,7 @@ getOrCreateFramePointerSaveIndex(MachineFunction &MF) const {
   int FI = ZFI->getFramePointerSaveIndex();
   if (!FI) {
     MachineFrameInfo &MFFrame = MF.getFrameInfo();
-    // The back chain is stored topmost with packed-stack.
-    int Offset = usePackedStack(MF) ? -8 : -SystemZMC::CallFrameSize;
+    int Offset = getBackchainOffset(MF) - SystemZMC::CallFrameSize;
     FI = MFFrame.CreateFixedObject(8, Offset, false);
     ZFI->setFramePointerSaveIndex(FI);
   }

diff  --git a/llvm/lib/Target/SystemZ/SystemZFrameLowering.h b/llvm/lib/Target/SystemZ/SystemZFrameLowering.h
index 001d26d4d3bb..085c31ca0f18 100644
--- a/llvm/lib/Target/SystemZ/SystemZFrameLowering.h
+++ b/llvm/lib/Target/SystemZ/SystemZFrameLowering.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZFRAMELOWERING_H
 #define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZFRAMELOWERING_H
 
+#include "MCTargetDesc/SystemZMCTargetDesc.h"
 #include "llvm/ADT/IndexedMap.h"
 #include "llvm/CodeGen/TargetFrameLowering.h"
 #include "llvm/Support/TypeSize.h"
@@ -63,6 +64,12 @@ class SystemZFrameLowering : public TargetFrameLowering {
   int getOrCreateFramePointerSaveIndex(MachineFunction &MF) const;
 
   bool usePackedStack(MachineFunction &MF) const;
+
+  // Return the offset of the backchain.
+  unsigned getBackchainOffset(MachineFunction &MF) const {
+    // The back chain is stored topmost with packed-stack.
+    return usePackedStack(MF) ? SystemZMC::CallFrameSize - 8 : 0;
+  }
 };
 } // end namespace llvm
 

diff  --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
index 812fe80864ac..663af1d64943 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
@@ -3435,7 +3435,8 @@ lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
   // If we need a backchain, save it now.
   SDValue Backchain;
   if (StoreBackchain)
-    Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
+    Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG),
+                            MachinePointerInfo());
 
   // Add extra space for alignment if needed.
   if (ExtraAlignSpace)
@@ -3472,7 +3473,8 @@ lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
   }
 
   if (StoreBackchain)
-    Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
+    Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG),
+                         MachinePointerInfo());
 
   SDValue Ops[2] = { Result, Chain };
   return DAG.getMergeValues(Ops, DL);
@@ -4095,13 +4097,15 @@ SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
 
   if (StoreBackchain) {
     SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
-    Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
+    Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG),
+                            MachinePointerInfo());
   }
 
   Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
 
   if (StoreBackchain)
-    Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
+    Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG),
+                         MachinePointerInfo());
 
   return Chain;
 }
@@ -8144,6 +8148,16 @@ MachineBasicBlock *SystemZTargetLowering::emitProbedAlloca(
   return DoneMBB;
 }
 
+SDValue SystemZTargetLowering::
+getBackchainAddress(SDValue SP, SelectionDAG &DAG) const {
+  MachineFunction &MF = DAG.getMachineFunction();
+  auto *TFL =
+      static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
+  SDLoc DL(SP);
+  return DAG.getNode(ISD::ADD, DL, MVT::i64, SP,
+                     DAG.getIntPtrConstant(TFL->getBackchainOffset(MF), DL));
+}
+
 MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
     MachineInstr &MI, MachineBasicBlock *MBB) const {
   switch (MI.getOpcode()) {

diff  --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.h b/llvm/lib/Target/SystemZ/SystemZISelLowering.h
index 1ab6366ec016..955587da626f 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.h
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.h
@@ -698,6 +698,8 @@ class SystemZTargetLowering : public TargetLowering {
   MachineBasicBlock *emitProbedAlloca(MachineInstr &MI,
                                       MachineBasicBlock *MBB) const;
 
+  SDValue getBackchainAddress(SDValue SP, SelectionDAG &DAG) const;
+
   MachineMemOperand::Flags
   getTargetMMOFlags(const Instruction &I) const override;
   const TargetRegisterClass *getRepRegClassFor(MVT VT) const override;

diff  --git a/llvm/test/CodeGen/SystemZ/backchain.ll b/llvm/test/CodeGen/SystemZ/backchain.ll
index c7c9d03b838a..b295c75c1072 100644
--- a/llvm/test/CodeGen/SystemZ/backchain.ll
+++ b/llvm/test/CodeGen/SystemZ/backchain.ll
@@ -82,3 +82,36 @@ define void @f5(i32 %count1, i32 %count2) "backchain" {
   store volatile i8 0, i8 *%array2
   ret void
 }
+
+; same, but with the kernel backchain
+define void @f6(i32 %count1, i32 %count2) #0 {
+; CHECK-LABEL: f6:
+; CHECK: stmg %r11, %r15, 112(%r15)
+; CHECK: lgr %r1, %r15
+; CHECK: aghi %r15, -48
+; CHECK: stg %r1, 152(%r15)
+; CHECK: lgr %r11, %r15
+; CHECK-DAG: lgr [[SAVESP:%r[0-9]+]], %r15
+; CHECK-DAG: lg [[BC:%r[0-9]+]], 152(%r15)
+; CHECK-DAG: lgr [[NEWSP:%r[0-9]+]], %r15
+; CHECK-DAG: lgr %r15, [[NEWSP]]
+; CHECK-DAG: stg [[BC]], 152([[NEWSP]])
+; CHECK-DAG: lg [[BC2:%r[0-9]+]], 152(%r15)
+; CHECK-DAG: lgr %r15, [[SAVESP]]
+; CHECK-DAG: stg [[BC2]], 152([[SAVESP]])
+; CHECK-DAG: lg [[BC3:%r[0-9]+]], 152(%r15)
+; CHECK-DAG: lgr [[NEWSP2:%r[0-9]+]], %r15
+; CHECK-DAG: lgr %r15, [[NEWSP2]]
+; CHECK-DAG: stg [[BC3]], 152([[NEWSP2]])
+; CHECK: lmg %r11, %r15, 160(%r11)
+; CHECK: br %r14
+  %src = call i8 *@llvm.stacksave()
+  %array1 = alloca i8, i32 %count1
+  store volatile i8 0, i8 *%array1
+  call void @llvm.stackrestore(i8 *%src)
+  %array2 = alloca i8, i32 %count2
+  store volatile i8 0, i8 *%array2
+  ret void
+}
+
+attributes #0 = { "backchain" "packed-stack" "use-soft-float"="true" }


        


More information about the llvm-branch-commits mailing list