Skip to content

Commit c62ea47

Browse files
committed
handle illegal offset but fits in si12
1 parent 6422b87 commit c62ea47

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

llvm/lib/Target/LoongArch/LoongArchRegisterInfo.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,36 +140,48 @@ bool LoongArchRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
140140

141141
bool FrameRegIsKill = false;
142142

143-
int fixed_offset = Offset.getFixed();
144-
bool OffsetLegal = false;
143+
int FixedOffset = Offset.getFixed();
144+
bool OffsetLegal = true;
145145

146-
// Handle offsets that exceed the immediate range of the instruction
146+
// Handle offsets that exceed the immediate range of the instruction.
147147
switch (MIOpc) {
148148
case LoongArch::VSTELM_B:
149149
case LoongArch::XVSTELM_B: {
150-
OffsetLegal = isInt<8>(fixed_offset);
150+
OffsetLegal = isInt<8>(FixedOffset);
151151
break;
152152
}
153153
case LoongArch::VSTELM_H:
154154
case LoongArch::XVSTELM_H: {
155-
OffsetLegal = isShiftedInt<8, 1>(fixed_offset);
155+
OffsetLegal = isShiftedInt<8, 1>(FixedOffset);
156156
break;
157157
}
158158
case LoongArch::VSTELM_W:
159159
case LoongArch::XVSTELM_W: {
160-
OffsetLegal = isShiftedInt<8, 2>(fixed_offset);
160+
OffsetLegal = isShiftedInt<8, 2>(FixedOffset);
161161
break;
162162
}
163163
case LoongArch::VSTELM_D:
164164
case LoongArch::XVSTELM_D: {
165-
OffsetLegal = isShiftedInt<8, 3>(fixed_offset);
165+
OffsetLegal = isShiftedInt<8, 3>(FixedOffset);
166166
break;
167167
}
168-
default:
169-
OffsetLegal = isInt<12>(fixed_offset);
170168
}
171169

172-
if (!OffsetLegal) {
170+
if (!OffsetLegal && isInt<12>(FixedOffset)) {
171+
unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
172+
173+
// The offset fits in si12 but is not legal for the instruction,
174+
// so use only one scratch register instead.
175+
Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
176+
BuildMI(MBB, II, DL, TII->get(Addi), ScratchReg)
177+
.addReg(FrameReg)
178+
.addImm(FixedOffset);
179+
Offset = StackOffset::getFixed(0);
180+
FrameReg = ScratchReg;
181+
FrameRegIsKill = true;
182+
}
183+
184+
if (!isInt<12>(FixedOffset)) {
173185
unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
174186
unsigned Add = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;
175187

llvm/test/CodeGen/LoongArch/lasx/ir-instruction/extractelement.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ define void @eliminate_frame_index(<8 x i32> %a) nounwind {
227227
; CHECK-LABEL: eliminate_frame_index:
228228
; CHECK: # %bb.0:
229229
; CHECK-NEXT: addi.d $sp, $sp, -1040
230-
; CHECK-NEXT: ori $a0, $zero, 524
231-
; CHECK-NEXT: add.d $a0, $sp, $a0
230+
; CHECK-NEXT: addi.d $a0, $sp, 524
232231
; CHECK-NEXT: xvstelm.w $xr0, $a0, 0, 1
233232
; CHECK-NEXT: addi.d $sp, $sp, 1040
234233
; CHECK-NEXT: ret

llvm/test/CodeGen/LoongArch/lsx/ir-instruction/extractelement.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ define void @eliminate_frame_index(<4 x i32> %a) nounwind {
167167
; CHECK-LABEL: eliminate_frame_index:
168168
; CHECK: # %bb.0:
169169
; CHECK-NEXT: addi.d $sp, $sp, -1040
170-
; CHECK-NEXT: ori $a0, $zero, 524
171-
; CHECK-NEXT: add.d $a0, $sp, $a0
170+
; CHECK-NEXT: addi.d $a0, $sp, 524
172171
; CHECK-NEXT: vstelm.w $vr0, $a0, 0, 1
173172
; CHECK-NEXT: addi.d $sp, $sp, 1040
174173
; CHECK-NEXT: ret

0 commit comments

Comments
 (0)