Skip to content

Commit 79d84a8

Browse files
committed
MipsMCExpr: remove unneeded folding and fix a crash for %hi(und-$L3)
After folding the inner expression, we might get something like %hi(0x30124), MipsAsmBackend::applyFixup will apply the relocation operator, so we don't need to duplicate code in MipsMCExpr (introduced by https://reviews.llvm.org/D19716). While we don't know the encoding at parse time (see hilo-addressing.s), it is not an issue. This change also removes an inappropriate use of `Fixup` (introduced in 2014 by 752b91b ; which will go away with my next change). In addition, fix a crash by porting the fix from RISCV. ``` lui $4, %hi(und-$L3) ```
1 parent 9a1e390 commit 79d84a8

File tree

2 files changed

+11
-67
lines changed

2 files changed

+11
-67
lines changed

llvm/lib/Target/Mips/MCTargetDesc/MipsMCExpr.cpp

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void MipsMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
131131

132132
bool MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
133133
const MCFixup *Fixup) const {
134+
if (!Asm)
135+
return false;
134136
// Look for the %hi(%neg(%gp_rel(X))) and %lo(%neg(%gp_rel(X))) special cases.
135137
if (isGpOff()) {
136138
const MCExpr *SubExpr =
@@ -146,73 +148,9 @@ bool MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
146148

147149
if (!getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup))
148150
return false;
149-
150-
if (Res.getRefKind() != MCSymbolRefExpr::VK_None)
151-
return false;
152-
153-
// evaluateAsAbsolute() and evaluateAsValue() require that we evaluate the
154-
// %hi/%lo/etc. here. Fixup is a null pointer when either of these is the
155-
// caller.
156-
if (Res.isAbsolute() && Fixup == nullptr) {
157-
int64_t AbsVal = Res.getConstant();
158-
switch (Kind) {
159-
case MEK_None:
160-
case MEK_Special:
161-
llvm_unreachable("MEK_None and MEK_Special are invalid");
162-
case MEK_DTPREL:
163-
// MEK_DTPREL is used for marking TLS DIEExpr only
164-
// and contains a regular sub-expression.
165-
return getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup);
166-
case MEK_DTPREL_HI:
167-
case MEK_DTPREL_LO:
168-
case MEK_GOT:
169-
case MEK_GOTTPREL:
170-
case MEK_GOT_CALL:
171-
case MEK_GOT_DISP:
172-
case MEK_GOT_HI16:
173-
case MEK_GOT_LO16:
174-
case MEK_GOT_OFST:
175-
case MEK_GOT_PAGE:
176-
case MEK_GPREL:
177-
case MEK_PCREL_HI16:
178-
case MEK_PCREL_LO16:
179-
case MEK_TLSGD:
180-
case MEK_TLSLDM:
181-
case MEK_TPREL_HI:
182-
case MEK_TPREL_LO:
183-
return false;
184-
case MEK_LO:
185-
case MEK_CALL_LO16:
186-
AbsVal = SignExtend64<16>(AbsVal);
187-
break;
188-
case MEK_CALL_HI16:
189-
case MEK_HI:
190-
AbsVal = SignExtend64<16>((AbsVal + 0x8000) >> 16);
191-
break;
192-
case MEK_HIGHER:
193-
AbsVal = SignExtend64<16>((AbsVal + 0x80008000LL) >> 32);
194-
break;
195-
case MEK_HIGHEST:
196-
AbsVal = SignExtend64<16>((AbsVal + 0x800080008000LL) >> 48);
197-
break;
198-
case MEK_NEG:
199-
AbsVal = -AbsVal;
200-
break;
201-
}
202-
Res = MCValue::get(AbsVal);
203-
return true;
204-
}
205-
206-
// We want to defer it for relocatable expressions since the constant is
207-
// applied to the whole symbol value.
208-
//
209-
// The value of getKind() that is given to MCValue is only intended to aid
210-
// debugging when inspecting MCValue objects. It shouldn't be relied upon
211-
// for decision making.
212151
Res =
213152
MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
214-
215-
return true;
153+
return !Res.getSymB();
216154
}
217155

218156
void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {

llvm/test/MC/Mips/hilo-addressing.s

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# RUN: llvm-mc -filetype=obj -triple=mipsel-unknown-linux %s \
88
# RUN: | llvm-readobj -r - | FileCheck %s -check-prefix=CHECK-REL
99

10+
# RUN: not llvm-mc -filetype=obj -triple=mipsel %s --defsym ERR=1 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
1011

1112
# Check that 1 is added to the high 16 bits if bit 15 of the low part is 1.
1213

1314
.equ addr, 0xdeadbeef
1415
lui $4, %hi(addr)
1516
lb $2, %lo(addr)($4)
16-
# CHECK-ENC: # encoding: [0x3c,0x04,0xde,0xae]
17-
# CHECK-ENC: # encoding: [0x80,0x82,0xbe,0xef]
17+
# CHECK-ENC: # encoding: [0x3c,0x04,A,A]
18+
# CHECK-ENC: # encoding: [0x80,0x82,A,A]
1819

1920

2021
# Check that assembler can handle %hi(label1 - label2) and %lo(label1 - label2)
@@ -40,3 +41,8 @@ $L3:
4041
# %lo(label1 - label2) expressions.
4142

4243
# CHECK-REL-NOT: R_MIPS
44+
45+
.ifdef ERR
46+
# ERR: <unknown>:0: error: expected relocatable expression
47+
lui $4, %hi(und-$L3)
48+
.endif

0 commit comments

Comments
 (0)