[PATCH] D135092: [RISCV] Restructure eliminateFrameIndex to share more code. NFC

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 3 11:43:59 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: reames, frasercrmck, kito-cheng, asb, luismarques.
Herald added subscribers: sunshaoce, VincentWu, StephenFan, vkmr, evandro, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, arphaman, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: alextsao1999, pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

The old code took two different paths based on whether there is
a scalable offset, but these two paths had some code in common.

The main difference between the two code paths was whether we needed
to create a GPR or not for the ADDI that gets created for RVVSpill.
If we had a scalable offset, the same GPR was used as the destination
for adding the scalable offset and the ADDI. To manage this, we now
cache the scratch register and reuse it if it has already been created.

This is a pre-patch for D135009 <https://reviews.llvm.org/D135009>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135092

Files:
  llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp


Index: llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
+++ llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
@@ -221,29 +221,15 @@
     FrameRegIsKill = true;
   }
 
-  if (!Offset.getScalable()) {
-    // Offset = (fixed offset, 0)
-    MI.getOperand(FIOperandNum)
-        .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
-    if (!IsRVVSpill)
-      MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
-    else {
-      if (Offset.getFixed()) {
-        Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
-        BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), ScratchReg)
-          .addReg(FrameReg, getKillRegState(FrameRegIsKill))
-          .addImm(Offset.getFixed());
-        MI.getOperand(FIOperandNum)
-          .ChangeToRegister(ScratchReg, false, false, true);
-      }
-    }
-  } else {
-    // Offset = (fixed offset, scalable offset)
-    // Step 1, the scalable offset, has already been computed.
+  Register ScratchReg;
+
+  // Add in the scalable offset which has already been computed in
+  // ScalableFactorRegister.
+  if (Offset.getScalable()) {
     assert(ScalableFactorRegister &&
            "Expected pre-computation of scalable factor in earlier step");
 
-    // 2. Calculate address: FrameReg + result of multiply
+    // Calculate address: FrameReg + ScalableFactorRegister.
     if (MI.getOpcode() == RISCV::ADDI && !Offset.getFixed()) {
       BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), MI.getOperand(0).getReg())
           .addReg(FrameReg, getKillRegState(FrameRegIsKill))
@@ -251,25 +237,39 @@
       MI.eraseFromParent();
       return;
     }
-    Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);
-    BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), VL)
+
+    assert(!ScratchReg && "Already populated ScratchReg?");
+    ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
+    BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), ScratchReg)
         .addReg(FrameReg, getKillRegState(FrameRegIsKill))
         .addReg(ScalableFactorRegister, RegState::Kill);
+    FrameReg = ScratchReg;
+    FrameRegIsKill = true;
+  }
 
-    if (IsRVVSpill && Offset.getFixed()) {
-      // Scalable load/store has no immediate argument. We need to add the
-      // fixed part into the load/store base address.
-      BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), VL)
-          .addReg(VL)
-          .addImm(Offset.getFixed());
+  // Handle the fixed offset which might be zero.
+  if (IsRVVSpill) {
+    // RVVSpills don't have an immediate. Add an ADDI if the fixed offset is
+    // needed.
+    if (Offset.getFixed()) {
+      // Reuse ScratchReg if it exists, otherwise create a new register.
+      if (!ScratchReg)
+        ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
+      BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), ScratchReg)
+        .addReg(FrameReg, getKillRegState(FrameRegIsKill))
+        .addImm(Offset.getFixed());
+      FrameReg = ScratchReg;
+      FrameRegIsKill = true;
     }
-
-    // 3. Replace address register with calculated address register
-    MI.getOperand(FIOperandNum).ChangeToRegister(VL, false, false, true);
-    if (!IsRVVSpill)
-      MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
+  } else {
+    // Otherwise we can replace the original immediate.
+    MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
   }
 
+  // Finally, replace the frame index operand.
+  MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false,
+                                               FrameRegIsKill);
+
   auto ZvlssegInfo = RISCV::isRVVSpillForZvlsseg(MI.getOpcode());
   if (ZvlssegInfo) {
     Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135092.464752.patch
Type: text/x-patch
Size: 3860 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221003/6e64e64e/attachment.bin>


More information about the llvm-commits mailing list