Skip to content

Commit 912c8e4

Browse files
committed
fixup: fix aligment of lwp/swp
1 parent 567ea42 commit 912c8e4

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

llvm/lib/Target/RISCV/RISCVLoadStoreOptimizer.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,43 @@ bool RISCVLoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) {
143143
}
144144

145145
// Merge two adjacent load/store instructions into a paired instruction
146-
// (LDP/SDP/SWP/LWP) if the effective address is 16-byte aligned. This function
147-
// selects the appropriate paired opcode, verifies that the memory operand is
148-
// 16-byte aligned, and checks that the offset is valid. If all conditions are
149-
// met, it builds and inserts the paired instruction.
146+
// (LDP/SDP/SWP/LWP) if the effective address is 8-byte aligned in case of
147+
// SWP/LWP 16-byte aligned in case of LDP/SDP. This function selects the
148+
// appropriate paired opcode, verifies that the memory operand is properly
149+
// aligned, and checks that the offset is valid. If all conditions are met, it
150+
// builds and inserts the paired instruction.
150151
bool RISCVLoadStoreOpt::tryConvertToLdStPair(
151152
MachineBasicBlock::iterator First, MachineBasicBlock::iterator Second) {
152153
unsigned PairOpc;
153154
// TODO: Handle the rest from RISCVInstrInfo::isPairableLdStInstOpc.
155+
Align RequiredAlignment;
154156
switch (First->getOpcode()) {
155157
default:
156158
return false;
157159
case RISCV::SW:
158160
PairOpc = RISCV::MIPS_SWP;
161+
RequiredAlignment = Align(8);
159162
break;
160163
case RISCV::LW:
161164
PairOpc = RISCV::MIPS_LWP;
165+
RequiredAlignment = Align(8);
162166
break;
163167
case RISCV::SD:
164168
PairOpc = RISCV::MIPS_SDP;
169+
RequiredAlignment = Align(16);
165170
break;
166171
case RISCV::LD:
167172
PairOpc = RISCV::MIPS_LDP;
173+
RequiredAlignment = Align(16);
168174
break;
169175
}
170176

171177
MachineFunction *MF = First->getMF();
172178
const MachineMemOperand *MMO = *First->memoperands_begin();
173179
Align MMOAlign = MMO->getAlign();
174180

175-
// Only pair if alignment is exactly 16 bytes.
176-
if (MMOAlign != 16)
181+
// Only pair if alignment is exactly RequiredAlignment bytes.
182+
if (MMOAlign != RequiredAlignment)
177183
return false;
178184

179185
int64_t Offset = First->getOperand(2).getImm();

llvm/test/CodeGen/RISCV/load-store-pair.ll

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ define dso_local void @testi(i8** nocapture noundef readonly %a) local_unnamed_a
153153
; RV32I_PAIR-NEXT: .cfi_offset s3, -8
154154
; RV32I_PAIR-NEXT: .cfi_offset s4, -12
155155
; RV32I_PAIR-NEXT: .cfi_offset s5, -16
156-
; RV32I_PAIR-NEXT: mips.lwp s3, s2, 0(a0)
156+
; RV32I_PAIR-NEXT: lw s3, 0(a0)
157+
; RV32I_PAIR-NEXT: lw s2, 4(a0)
157158
; RV32I_PAIR-NEXT: mips.lwp s5, s4, 8(a0)
158159
; RV32I_PAIR-NEXT: #APP
159160
; RV32I_PAIR-NEXT: #NO_APP
@@ -181,7 +182,8 @@ define dso_local void @testi(i8** nocapture noundef readonly %a) local_unnamed_a
181182
; RV32D_PAIR-NEXT: .cfi_offset s3, -8
182183
; RV32D_PAIR-NEXT: .cfi_offset s4, -12
183184
; RV32D_PAIR-NEXT: .cfi_offset s5, -16
184-
; RV32D_PAIR-NEXT: mips.lwp s3, s2, 0(a0)
185+
; RV32D_PAIR-NEXT: lw s3, 0(a0)
186+
; RV32D_PAIR-NEXT: lw s2, 4(a0)
185187
; RV32D_PAIR-NEXT: mips.lwp s5, s4, 8(a0)
186188
; RV32D_PAIR-NEXT: #APP
187189
; RV32D_PAIR-NEXT: #NO_APP
@@ -210,7 +212,8 @@ define dso_local void @testi(i8** nocapture noundef readonly %a) local_unnamed_a
210212
; RV64I_PAIR-NEXT: .cfi_offset s4, -24
211213
; RV64I_PAIR-NEXT: .cfi_offset s5, -32
212214
; RV64I_PAIR-NEXT: mips.ldp s3, s2, 0(a0)
213-
; RV64I_PAIR-NEXT: mips.ldp s5, s4, 16(a0)
215+
; RV64I_PAIR-NEXT: ld s5, 16(a0)
216+
; RV64I_PAIR-NEXT: ld s4, 24(a0)
214217
; RV64I_PAIR-NEXT: #APP
215218
; RV64I_PAIR-NEXT: #NO_APP
216219
; RV64I_PAIR-NEXT: ld s3, 16(sp) # 8-byte Folded Reload
@@ -238,7 +241,8 @@ define dso_local void @testi(i8** nocapture noundef readonly %a) local_unnamed_a
238241
; RV64P_8700-NEXT: .cfi_offset s4, -24
239242
; RV64P_8700-NEXT: .cfi_offset s5, -32
240243
; RV64P_8700-NEXT: mips.ldp s3, s2, 0(a0)
241-
; RV64P_8700-NEXT: mips.ldp s5, s4, 16(a0)
244+
; RV64P_8700-NEXT: ld s5, 16(a0)
245+
; RV64P_8700-NEXT: ld s4, 24(a0)
242246
; RV64P_8700-NEXT: #APP
243247
; RV64P_8700-NEXT: #NO_APP
244248
; RV64P_8700-NEXT: ld s3, 16(sp) # 8-byte Folded Reload
@@ -266,7 +270,8 @@ define dso_local void @testi(i8** nocapture noundef readonly %a) local_unnamed_a
266270
; RV64D_PAIR-NEXT: .cfi_offset s4, -24
267271
; RV64D_PAIR-NEXT: .cfi_offset s5, -32
268272
; RV64D_PAIR-NEXT: mips.ldp s3, s2, 0(a0)
269-
; RV64D_PAIR-NEXT: mips.ldp s5, s4, 16(a0)
273+
; RV64D_PAIR-NEXT: ld s5, 16(a0)
274+
; RV64D_PAIR-NEXT: ld s4, 24(a0)
270275
; RV64D_PAIR-NEXT: #APP
271276
; RV64D_PAIR-NEXT: #NO_APP
272277
; RV64D_PAIR-NEXT: ld s3, 16(sp) # 8-byte Folded Reload
@@ -317,7 +322,7 @@ entry:
317322
%arrayidx2 = getelementptr inbounds i8*, i8** %a, i64 3
318323
%2 = load i8*, i8** %arrayidx2, align 16
319324
%arrayidx3 = getelementptr inbounds i8*, i8** %a, i64 2
320-
%3 = load i8*, i8** %arrayidx3, align 16
325+
%3 = load i8*, i8** %arrayidx3, align 8
321326
tail call void asm sideeffect "", "{x18},{x19},{x20},{x21}"(i8* %0, i8* %1, i8* %2, i8* %3)
322327
ret void
323328
}

0 commit comments

Comments
 (0)