Skip to content

Commit dd28915

Browse files
authored
MCAsmBackend: Merge addReloc into applyFixup (#146820)
Follow-up to #141333. Relocation generation called both addReloc and applyFixup, with the default addReloc invoking shouldForceRelocation, resulting in three virtual calls. This approach was also inflexible, as targets needing additional data required extending `shouldForceRelocation` (see #73721, resolved by #141311). This change integrates relocation handling into applyFixup, eliminating two virtual calls. The prior default addReloc is renamed to maybeAddReloc. Targets overriding addReloc now call their customized addReloc implementation.
1 parent 119705e commit dd28915

File tree

29 files changed

+115
-115
lines changed

29 files changed

+115
-115
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ class LLVM_ABI MCAsmBackend {
9191
/// Get information on a fixup kind.
9292
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;
9393

94-
// Hook used by the default `addReloc` to check if a relocation is needed.
95-
virtual bool shouldForceRelocation(const MCFixup &, const MCValue &) {
96-
return false;
97-
}
98-
9994
/// Hook to check if extra nop bytes must be inserted for alignment directive.
10095
/// For some targets this may be necessary in order to support linker
10196
/// relaxation. The number of bytes to insert are returned in Size.
@@ -116,9 +111,10 @@ class LLVM_ABI MCAsmBackend {
116111
llvm_unreachable("Need to implement hook if target has custom fixups");
117112
}
118113

119-
virtual bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
120-
uint64_t &FixedValue, bool IsResolved);
114+
void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,
115+
uint64_t &Value, bool IsResolved);
121116

117+
/// Determine if a relocation is required. In addition,
122118
/// Apply the \p Value for given \p Fixup into the provided data fragment, at
123119
/// the offset specified by the fixup and following the fixup kind as
124120
/// appropriate. Errors (such as an out of range fixup value) should be

llvm/lib/MC/MCAsmBackend.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ bool MCAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
116116
return fixupNeedsRelaxation(Fixup, Value);
117117
}
118118

119-
bool MCAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
120-
const MCValue &Target, uint64_t &FixedValue,
121-
bool IsResolved) {
122-
if (IsResolved && shouldForceRelocation(Fixup, Target))
123-
IsResolved = false;
119+
void MCAsmBackend::maybeAddReloc(const MCFragment &F, const MCFixup &Fixup,
120+
const MCValue &Target, uint64_t &Value,
121+
bool IsResolved) {
124122
if (!IsResolved)
125-
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
126-
return IsResolved;
123+
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
127124
}
128125

129126
bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {

llvm/lib/MC/MCAssembler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
202202

203203
if (IsResolved && mc::isRelocRelocation(Fixup.getKind()))
204204
IsResolved = false;
205-
IsResolved = getBackend().addReloc(F, Fixup, Target, Value, IsResolved);
206205
getBackend().applyFixup(F, Fixup, Target, Contents, Value, IsResolved);
207206
return true;
208207
}

llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ class AArch64AsmBackend : public MCAsmBackend {
9494

9595
unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;
9696

97-
bool shouldForceRelocation(const MCFixup &Fixup,
98-
const MCValue &Target) override;
97+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
9998
};
10099

101100
} // end anonymous namespace
@@ -412,10 +411,13 @@ unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) con
412411
}
413412
}
414413

415-
void AArch64AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
414+
void AArch64AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
416415
const MCValue &Target,
417416
MutableArrayRef<char> Data, uint64_t Value,
418417
bool IsResolved) {
418+
if (IsResolved && shouldForceRelocation(Fixup, Target))
419+
IsResolved = false;
420+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
419421
MCFixupKind Kind = Fixup.getKind();
420422
if (mc::isRelocation(Kind))
421423
return;

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AMDGPUAsmBackend : public MCAsmBackend {
5050

5151
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
5252
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
53-
bool shouldForceRelocation(const MCFixup &, const MCValue &) override;
53+
bool shouldForceRelocation(const MCFixup &, const MCValue &);
5454
};
5555

5656
} //End anonymous namespace
@@ -130,10 +130,13 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
130130
}
131131
}
132132

133-
void AMDGPUAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
133+
void AMDGPUAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
134134
const MCValue &Target,
135135
MutableArrayRef<char> Data, uint64_t Value,
136136
bool IsResolved) {
137+
if (IsResolved && shouldForceRelocation(Fixup, Target))
138+
IsResolved = false;
139+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
137140
if (mc::isRelocation(Fixup.getKind()))
138141
return;
139142

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ void ARMAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
11281128
const MCValue &Target,
11291129
MutableArrayRef<char> Data, uint64_t Value,
11301130
bool IsResolved) {
1131+
if (IsResolved && shouldForceRelocation(Fixup, Target))
1132+
IsResolved = false;
1133+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
11311134
auto Kind = Fixup.getKind();
11321135
if (mc::isRelocation(Kind))
11331136
return;

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class ARMAsmBackend : public MCAsmBackend {
3030

3131
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
3232

33-
bool shouldForceRelocation(const MCFixup &Fixup,
34-
const MCValue &Target) override;
33+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
3534

3635
unsigned adjustFixupValue(const MCAssembler &Asm, const MCFixup &Fixup,
3736
const MCValue &Target, uint64_t Value,

llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,21 @@ AVRAsmBackend::createObjectTargetWriter() const {
368368
return createAVRELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));
369369
}
370370

371-
bool AVRAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
372-
const MCValue &Target, uint64_t &FixedValue,
373-
bool IsResolved) {
371+
void AVRAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
372+
const MCValue &Target,
373+
MutableArrayRef<char> Data, uint64_t Value,
374+
bool IsResolved) {
374375
// AVR sets the fixup value to bypass the assembly time overflow with a
375376
// relocation.
376377
if (IsResolved) {
377-
auto TargetVal = MCValue::get(Target.getAddSym(), Target.getSubSym(),
378-
FixedValue, Target.getSpecifier());
378+
auto TargetVal = MCValue::get(Target.getAddSym(), Target.getSubSym(), Value,
379+
Target.getSpecifier());
379380
if (forceRelocation(F, Fixup, TargetVal))
380381
IsResolved = false;
381382
}
382383
if (!IsResolved)
383-
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
384-
return IsResolved;
385-
}
384+
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
386385

387-
void AVRAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
388-
const MCValue &Target,
389-
MutableArrayRef<char> Data, uint64_t Value,
390-
bool IsResolved) {
391386
if (mc::isRelocation(Fixup.getKind()))
392387
return;
393388
adjustFixupValue(Fixup, Target, Value, &getContext());

llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class AVRAsmBackend : public MCAsmBackend {
3737
std::unique_ptr<MCObjectTargetWriter>
3838
createObjectTargetWriter() const override;
3939

40-
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
41-
uint64_t &FixedValue, bool IsResolved) override;
42-
4340
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
4441
MutableArrayRef<char> Data, uint64_t Value,
4542
bool IsResolved) override;

llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
6666
return true;
6767
}
6868

69-
void BPFAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
69+
void BPFAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
7070
const MCValue &Target,
7171
MutableArrayRef<char> Data, uint64_t Value,
7272
bool IsResolved) {
73+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
7374
if (Fixup.getKind() == FK_SecRel_8) {
7475
// The Value is 0 for global variables, and the in-section offset
7576
// for static variables. Write to the immediate field of the inst.

0 commit comments

Comments
 (0)