Skip to content

[RISCV] Restrict when we fold an ADD_LO into a load/store address. #93129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 deletions llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,61 @@ static bool isWorthFoldingAdd(SDValue Add) {
return true;
}

// To prevent SelectAddrRegImm from folding offsets that conflicts with the
// fusion of PseudoLIAddr, check if the offset of every use of a given address
// is within the alignment
static bool areUserOffsetsWithinAlignment(SDValue Addr, Align Alignment) {
for (auto *Use : Addr->uses()) {
if (!Use->isMachineOpcode()) {
// Don't allow stores of the value. It must be used as the address.
if (Use->getOpcode() == ISD::STORE &&
cast<StoreSDNode>(Use)->getValue() == Addr)
return false;
if (Use->getOpcode() == ISD::ATOMIC_STORE &&
cast<AtomicSDNode>(Use)->getVal() == Addr)
return false;
// If the user is direct load/store, there is no offset.
if (Use->getOpcode() == ISD::LOAD || Use->getOpcode() == ISD::STORE ||
Use->getOpcode() == ISD::ATOMIC_LOAD ||
Use->getOpcode() == ISD::ATOMIC_STORE)
continue;
if (Use->getOpcode() == ISD::ADD &&
isa<ConstantSDNode>(Use->getOperand(1)) &&
Alignment > cast<ConstantSDNode>(Use->getOperand(1))->getSExtValue())
continue;

return false;
}

// If user is already selected, get offsets from load/store instructions
unsigned int Opcode = Use->getMachineOpcode();
if (Opcode == RISCV::LB || Opcode == RISCV::LBU || Opcode == RISCV::LH ||
Opcode == RISCV::LHU || Opcode == RISCV::LW || Opcode == RISCV::LWU ||
Opcode == RISCV::LD || Opcode == RISCV::FLH || Opcode == RISCV::FLW ||
Opcode == RISCV::FLD) {
if (auto *Offset = dyn_cast<ConstantSDNode>(Use->getOperand(1))) {
if (Offset->isZero() || Alignment > Offset->getSExtValue())
continue;
}
return false;
}
if (Opcode == RISCV::SB || Opcode == RISCV::SH || Opcode == RISCV::SW ||
Opcode == RISCV::SD || Opcode == RISCV::FSH || Opcode == RISCV::FSW ||
Opcode == RISCV::FSD) {
// Also check if Addr is used as the value of store.
if (Use->getOperand(0) == Addr)
return false;
if (auto *Offset = dyn_cast<ConstantSDNode>(Use->getOperand(2))) {
if (Offset->isZero() || Alignment > Offset->getSExtValue())
continue;
}
return false;
}
return false;
}

return true;
}
bool RISCVDAGToDAGISel::SelectAddrRegRegScale(SDValue Addr,
unsigned MaxShiftAmount,
SDValue &Base, SDValue &Index,
Expand Down Expand Up @@ -2520,9 +2575,21 @@ bool RISCVDAGToDAGISel::SelectAddrRegImm(SDValue Addr, SDValue &Base,
MVT VT = Addr.getSimpleValueType();

if (Addr.getOpcode() == RISCVISD::ADD_LO) {
Base = Addr.getOperand(0);
Offset = Addr.getOperand(1);
return true;
bool CanFold = true;
// Unconditionally fold if operand 1 is not a global address (e.g.
// externsymbol)
if (auto *GA = dyn_cast<GlobalAddressSDNode>(Addr.getOperand(1))) {
const DataLayout &DL = CurDAG->getDataLayout();
Align Alignment = commonAlignment(
GA->getGlobal()->getPointerAlignment(DL), GA->getOffset());
if (!areUserOffsetsWithinAlignment(Addr, Alignment))
CanFold = false;
}
if (CanFold) {
Base = Addr.getOperand(0);
Offset = Addr.getOperand(1);
return true;
}
}

int64_t RV32ZdinxRange = IsINX ? 4 : 0;
Expand All @@ -2541,7 +2608,7 @@ bool RISCVDAGToDAGISel::SelectAddrRegImm(SDValue Addr, SDValue &Base,
const DataLayout &DL = CurDAG->getDataLayout();
Align Alignment = commonAlignment(
GA->getGlobal()->getPointerAlignment(DL), GA->getOffset());
if (CVal == 0 || Alignment > CVal) {
if (areUserOffsetsWithinAlignment(Base, Alignment)) {
int64_t CombinedOffset = CVal + GA->getOffset();
Base = Base.getOperand(0);
Offset = CurDAG->getTargetGlobalAddress(
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/CodeGen/RISCV/bfloat-mem.ll
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ define bfloat @flh_fsh_global(bfloat %a, bfloat %b) nounwind {
; CHECK-NEXT: fadd.s fa5, fa4, fa5
; CHECK-NEXT: fcvt.bf16.s fa0, fa5
; CHECK-NEXT: lui a0, %hi(G)
; CHECK-NEXT: flh fa5, %lo(G)(a0)
; CHECK-NEXT: addi a1, a0, %lo(G)
; CHECK-NEXT: fsh fa0, %lo(G)(a0)
; CHECK-NEXT: flh fa5, 18(a1)
; CHECK-NEXT: fsh fa0, 18(a1)
; CHECK-NEXT: addi a0, a0, %lo(G)
; CHECK-NEXT: flh fa5, 0(a0)
; CHECK-NEXT: fsh fa0, 0(a0)
; CHECK-NEXT: flh fa5, 18(a0)
; CHECK-NEXT: fsh fa0, 18(a0)
; CHECK-NEXT: ret
%1 = fadd bfloat %a, %b
%2 = load volatile bfloat, ptr @G
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/CodeGen/RISCV/byval.ll
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ define void @caller() nounwind {
; RV32I-NEXT: addi sp, sp, -32
; RV32I-NEXT: sw ra, 28(sp) # 4-byte Folded Spill
; RV32I-NEXT: lui a0, %hi(foo)
; RV32I-NEXT: lw a1, %lo(foo)(a0)
; RV32I-NEXT: sw a1, 12(sp)
; RV32I-NEXT: addi a0, a0, %lo(foo)
; RV32I-NEXT: lw a1, 12(a0)
; RV32I-NEXT: sw a1, 24(sp)
; RV32I-NEXT: lw a1, 8(a0)
; RV32I-NEXT: sw a1, 20(sp)
; RV32I-NEXT: lw a0, 4(a0)
; RV32I-NEXT: sw a0, 16(sp)
; RV32I-NEXT: lw a1, 4(a0)
; RV32I-NEXT: sw a1, 16(sp)
; RV32I-NEXT: lw a0, 0(a0)
; RV32I-NEXT: sw a0, 12(sp)
; RV32I-NEXT: addi a0, sp, 12
; RV32I-NEXT: call callee
; RV32I-NEXT: lw ra, 28(sp) # 4-byte Folded Reload
Expand Down
Loading
Loading