Skip to content

Commit f673245

Browse files
committed
[LoongArch] Reduce number of reserved relocations when relax enabled
1 parent 3cd34e6 commit f673245

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
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) {
@@ -484,9 +468,16 @@ bool LoongArchAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
484468
return false;
485469
}
486470

487-
IsResolved = Fallback();
488471
// If linker relaxation is enabled and supported by the current relocation,
489-
// append a RELAX relocation.
472+
// generate a relocation and then append a RELAX.
473+
if (Fixup.isLinkerRelaxable())
474+
IsResolved = false;
475+
if (IsResolved && Fixup.isPCRel())
476+
IsResolved = isPCRelFixupResolved(Target.getAddSym(), F);
477+
478+
if (!IsResolved)
479+
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
480+
490481
if (Fixup.isLinkerRelaxable()) {
491482
auto FA = MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_LARCH_RELAX);
492483
Asm->getWriter().recordRelocation(F, FA, MCValue::get(nullptr),

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)