Skip to content

Commit 76fb161

Browse files
authored
[LoongArch] Reduce number of reserved relocations when relax enabled (#153769)
1 parent 51b5a3e commit 76fb161

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ static void fixupLeb128(MCContext &Ctx, const MCFixup &Fixup, uint8_t *Data,
143143
void LoongArchAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
144144
const MCValue &Target, uint8_t *Data,
145145
uint64_t Value, bool IsResolved) {
146-
if (IsResolved && shouldForceRelocation(Fixup, Target))
147-
IsResolved = false;
148146
IsResolved = addReloc(F, Fixup, Target, Value, IsResolved);
149147
if (!Value)
150148
return; // Doesn't change encoding.
@@ -176,20 +174,6 @@ void LoongArchAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
176174
}
177175
}
178176

179-
bool LoongArchAsmBackend::shouldForceRelocation(const MCFixup &Fixup,
180-
const MCValue &Target) {
181-
switch (Fixup.getKind()) {
182-
default:
183-
return STI.hasFeature(LoongArch::FeatureRelax);
184-
case FK_Data_1:
185-
case FK_Data_2:
186-
case FK_Data_4:
187-
case FK_Data_8:
188-
case FK_Data_leb128:
189-
return !Target.isAbsolute();
190-
}
191-
}
192-
193177
static inline std::pair<MCFixupKind, MCFixupKind>
194178
getRelocPairForSize(unsigned Size) {
195179
switch (Size) {
@@ -491,9 +475,16 @@ bool LoongArchAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
491475
return false;
492476
}
493477

494-
IsResolved = Fallback();
495478
// If linker relaxation is enabled and supported by the current relocation,
496-
// append a RELAX relocation.
479+
// generate a relocation and then append a RELAX.
480+
if (Fixup.isLinkerRelaxable())
481+
IsResolved = false;
482+
if (IsResolved && Fixup.isPCRel())
483+
IsResolved = isPCRelFixupResolved(Target.getAddSym(), F);
484+
485+
if (!IsResolved)
486+
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
487+
497488
if (Fixup.isLinkerRelaxable()) {
498489
auto FA = MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_LARCH_RELAX);
499490
Asm->getWriter().recordRelocation(F, FA, MCValue::get(nullptr),

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ class LoongArchAsmBackend : public MCAsmBackend {
4444
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
4545
uint8_t *Data, uint64_t Value, bool IsResolved) override;
4646

47-
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
48-
4947
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
5048

5149
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;

llvm/test/MC/LoongArch/Relocations/relax-attr.s

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
# CHECKR-NEXT: 0x4 R_LARCH_CALL36 foo 0x0
1818
# CHECKR-NEXT: 0x4 R_LARCH_RELAX - 0x0
1919
# CHECKR-NEXT: 0x10 R_LARCH_B21 .L0 0x0
20-
# CHECKR-NEXT: 0x14 R_LARCH_B21 .L1 0x0
2120
# CHECKR-NEXT: 0x18 R_LARCH_B16 .L0 0x0
22-
# CHECKR-NEXT: 0x1C R_LARCH_B16 .L1 0x0
2321
# CHECKR-NEXT: 0x20 R_LARCH_B26 .L0 0x0
24-
# CHECKR-NEXT: 0x24 R_LARCH_B26 .L1 0x0
2522
# CHECKR-NEXT: }
2623
# CHECKR-NEXT: Section ({{.*}}) .rela.data {
2724
# CHECKR-NEXT: 0x0 R_LARCH_64 .L1 0x0
@@ -36,6 +33,10 @@
3633

3734
.L1:
3835
nop
36+
## Relocations for branches to .L0 must be reserved and be fixed up by linker
37+
## when linker relaxation enabled, because of the relaxable call36 instruction.
38+
## Branches to .L1 can be resolved correctly at compile time, so their
39+
## relocations can simply be removed.
3940
bnez $a0, .L0
4041
beqz $a0, .L1
4142
beq $a0, $a1, .L0

0 commit comments

Comments
 (0)