Skip to content

Commit 7ca448e

Browse files
authored
[LoongArch] Fix MergeBaseOffset for constant pool index operand (#159336)
Fixes #159200
1 parent 91c72e8 commit 7ca448e

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

llvm/include/llvm/CodeGen/MachineOperand.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ class MachineOperand {
788788
LLVM_ABI void ChangeToBA(const BlockAddress *BA, int64_t Offset,
789789
unsigned TargetFlags = 0);
790790

791+
/// ChangeToCPI - Replace this operand with a new constant pool index operand.
792+
LLVM_ABI void ChangeToCPI(unsigned Idx, int Offset, unsigned TargetFlags = 0);
793+
791794
/// ChangeToMCSymbol - Replace this operand with a new MC symbol operand.
792795
LLVM_ABI void ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags = 0);
793796

llvm/lib/CodeGen/MachineOperand.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ void MachineOperand::ChangeToBA(const BlockAddress *BA, int64_t Offset,
218218
setTargetFlags(TargetFlags);
219219
}
220220

221+
void MachineOperand::ChangeToCPI(unsigned Idx, int Offset,
222+
unsigned TargetFlags) {
223+
assert((!isReg() || !isTied()) &&
224+
"Cannot change a tied operand into a constant pool index");
225+
226+
removeRegFromUses();
227+
228+
OpKind = MO_ConstantPoolIndex;
229+
setIndex(Idx);
230+
setOffset(Offset);
231+
setTargetFlags(TargetFlags);
232+
}
233+
221234
void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags) {
222235
assert((!isReg() || !isTied()) &&
223236
"Cannot change a tied operand into an MCSymbol");

llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ bool LoongArchAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
162162
else if (OffsetMO.isImm())
163163
OS << ", " << OffsetMO.getImm();
164164
else if (OffsetMO.isGlobal() || OffsetMO.isBlockAddress() ||
165-
OffsetMO.isMCSymbol()) {
165+
OffsetMO.isMCSymbol() || OffsetMO.isCPI()) {
166166
OS << ", ";
167167
MAI->printExpr(OS, *MCO.getExpr());
168168
} else

llvm/lib/Target/LoongArch/LoongArchMergeBaseOffset.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,10 @@ bool LoongArchMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &Hi20,
759759
MO.ChangeToBA(ImmOp.getBlockAddress(), ImmOp.getOffset(),
760760
LoongArchII::getDirectFlags(ImmOp));
761761
break;
762+
case MachineOperand::MO_ConstantPoolIndex:
763+
MO.ChangeToCPI(ImmOp.getIndex(), ImmOp.getOffset(),
764+
LoongArchII::getDirectFlags(ImmOp));
765+
break;
762766
default:
763767
report_fatal_error("unsupported machine operand type");
764768
break;

llvm/test/CodeGen/LoongArch/inline-asm-constraint-m.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,44 @@ define i32 @m_offset_2048(ptr %p) nounwind {
142142
ret i32 %2
143143
}
144144

145+
define i32 @m_constant_0() nounwind {
146+
; LA32-LABEL: m_constant_0:
147+
; LA32: # %bb.0:
148+
; LA32-NEXT: pcalau12i $a0, %pc_hi20(.LCPI7_0)
149+
; LA32-NEXT: #APP
150+
; LA32-NEXT: #NO_APP
151+
; LA32-NEXT: ret
152+
;
153+
; LA64-LABEL: m_constant_0:
154+
; LA64: # %bb.0:
155+
; LA64-NEXT: pcalau12i $a0, %pc_hi20(.LCPI7_0)
156+
; LA64-NEXT: #APP
157+
; LA64-NEXT: #NO_APP
158+
; LA64-NEXT: ret
159+
%1 = call i32 asm sideeffect "", "=r,m"(i64 0)
160+
ret i32 %1
161+
}
162+
163+
define i32 @m_constant_1() nounwind {
164+
; LA32-LABEL: m_constant_1:
165+
; LA32: # %bb.0:
166+
; LA32-NEXT: pcalau12i $a0, %pc_hi20(.LCPI8_0)
167+
; LA32-NEXT: #APP
168+
; LA32-NEXT: ld.w $a0, $a0, %pc_lo12(.LCPI8_0)
169+
; LA32-NEXT: #NO_APP
170+
; LA32-NEXT: ret
171+
;
172+
; LA64-LABEL: m_constant_1:
173+
; LA64: # %bb.0:
174+
; LA64-NEXT: pcalau12i $a0, %pc_hi20(.LCPI8_0)
175+
; LA64-NEXT: #APP
176+
; LA64-NEXT: ld.w $a0, $a0, %pc_lo12(.LCPI8_0)
177+
; LA64-NEXT: #NO_APP
178+
; LA64-NEXT: ret
179+
%1 = call i32 asm sideeffect "ld.w $0, $1", "=r,m"(i64 1)
180+
ret i32 %1
181+
}
182+
145183
@g_i32 = dso_local global i32 0
146184

147185
define i32 @m_addr_pcrel() nounwind {

0 commit comments

Comments
 (0)