[llvm] 4a2e7da - [RISCV] Teach RISCVMergeBaseOffset to handle constant pools.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 20 07:17:07 PST 2022


Author: Craig Topper
Date: 2022-12-20T07:16:43-08:00
New Revision: 4a2e7da3ce3e8b6fa8d7d886be441dda42c18265

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

LOG: [RISCV] Teach RISCVMergeBaseOffset to handle constant pools.

Primarily this allows us to fold the addi from PseudoLLA expansion
into a load.

If the linker is able to GP relax the constant pool access we'll
end up with a GP relative load.

Reviewed By: asb

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

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
    llvm/test/CodeGen/RISCV/codemodel-lowering.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp b/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
index da1ada12975fc..1436d3c681e12 100644
--- a/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
@@ -84,22 +84,23 @@ INITIALIZE_PASS(RISCVMergeBaseOffsetOpt, DEBUG_TYPE,
 // The pattern is only accepted if:
 //    1) The first instruction has only one use, which is the ADDI.
 //    2) The address operands have the appropriate type, reflecting the
-//       lowering of a global address using medlow or medany.
-//    3) The offset value in the Global Address is 0.
+//       lowering of a global address or constant pool using medlow or medany.
+//    3) The offset value in the Global Address or Constant Pool is 0.
 bool RISCVMergeBaseOffsetOpt::detectFoldable(MachineInstr &Hi,
                                              MachineInstr *&Lo) {
   if (Hi.getOpcode() == RISCV::LUI) {
     Register HiDestReg = Hi.getOperand(0).getReg();
     const MachineOperand &HiOp1 = Hi.getOperand(1);
-    if (HiOp1.getTargetFlags() != RISCVII::MO_HI || !HiOp1.isGlobal() ||
-        HiOp1.getOffset() != 0 || !MRI->hasOneUse(HiDestReg))
+    if (HiOp1.getTargetFlags() != RISCVII::MO_HI ||
+        !(HiOp1.isGlobal() || HiOp1.isCPI()) || HiOp1.getOffset() != 0 ||
+        !MRI->hasOneUse(HiDestReg))
       return false;
     Lo = &*MRI->use_instr_begin(HiDestReg);
     if (Lo->getOpcode() != RISCV::ADDI)
       return false;
     const MachineOperand &LoOp2 = Lo->getOperand(2);
-    if (LoOp2.getTargetFlags() != RISCVII::MO_LO || !LoOp2.isGlobal() ||
-        LoOp2.getOffset() != 0)
+    if (LoOp2.getTargetFlags() != RISCVII::MO_LO ||
+        !(LoOp2.isGlobal() || LoOp2.isCPI()) || LoOp2.getOffset() != 0)
       return false;
     return true;
   }
@@ -107,8 +108,9 @@ bool RISCVMergeBaseOffsetOpt::detectFoldable(MachineInstr &Hi,
   if (Hi.getOpcode() == RISCV::AUIPC) {
     Register HiDestReg = Hi.getOperand(0).getReg();
     const MachineOperand &HiOp1 = Hi.getOperand(1);
-    if (HiOp1.getTargetFlags() != RISCVII::MO_PCREL_HI || !HiOp1.isGlobal() ||
-        HiOp1.getOffset() != 0 || !MRI->hasOneUse(HiDestReg))
+    if (HiOp1.getTargetFlags() != RISCVII::MO_PCREL_HI ||
+        !(HiOp1.isGlobal() || HiOp1.isCPI()) || HiOp1.getOffset() != 0 ||
+        !MRI->hasOneUse(HiDestReg))
       return false;
     Lo = &*MRI->use_instr_begin(HiDestReg);
     if (Lo->getOpcode() != RISCV::ADDI)
@@ -432,8 +434,14 @@ bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(MachineFunction &Fn) {
       MachineInstr *Lo = nullptr;
       if (!detectFoldable(Hi, Lo))
         continue;
-      LLVM_DEBUG(dbgs() << "  Found lowered global address: "
-                        << *Hi.getOperand(1).getGlobal() << "\n");
+      if (Hi.getOperand(1).isGlobal()) {
+        LLVM_DEBUG(dbgs() << "  Found lowered global address: "
+                          << *Hi.getOperand(1).getGlobal() << "\n");
+      } else {
+        assert(Hi.getOperand(1).isCPI());
+        LLVM_DEBUG(dbgs() << "  Found lowered constant pool: "
+                          << Hi.getOperand(1).getIndex() << "\n");
+      }
       MadeChange |= detectAndFoldOffset(Hi, *Lo);
       MadeChange |= foldIntoMemoryOps(Hi, *Lo);
     }

diff  --git a/llvm/test/CodeGen/RISCV/codemodel-lowering.ll b/llvm/test/CodeGen/RISCV/codemodel-lowering.ll
index a485660a28c05..af66116571a75 100644
--- a/llvm/test/CodeGen/RISCV/codemodel-lowering.ll
+++ b/llvm/test/CodeGen/RISCV/codemodel-lowering.ll
@@ -133,8 +133,7 @@ define float @lower_constantpool(float %a) nounwind {
 ; RV32I-MEDIUM:       # %bb.0:
 ; RV32I-MEDIUM-NEXT:  .Lpcrel_hi3:
 ; RV32I-MEDIUM-NEXT:    auipc a0, %pcrel_hi(.LCPI3_0)
-; RV32I-MEDIUM-NEXT:    addi a0, a0, %pcrel_lo(.Lpcrel_hi3)
-; RV32I-MEDIUM-NEXT:    flw ft0, 0(a0)
+; RV32I-MEDIUM-NEXT:    flw ft0, %pcrel_lo(.Lpcrel_hi3)(a0)
 ; RV32I-MEDIUM-NEXT:    fadd.s fa0, fa0, ft0
 ; RV32I-MEDIUM-NEXT:    ret
   %1 = fadd float %a, 1.0


        


More information about the llvm-commits mailing list