Skip to content

Commit 9234d07

Browse files
committed
MCAssembler: Optimize PCRel fixups
* MCAssembler::evaluateFixup sets MCFixup::PCRel. * ELFObjectWriter retrieves the bit from the MCFixup argument.
1 parent 6504c96 commit 9234d07

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class MCAssembler {
101101
/// out.
102102
/// \param RecordReloc Record relocation if needed.
103103
/// relocation.
104-
bool evaluateFixup(const MCFragment &F, const MCFixup &Fixup, MCValue &Target,
104+
bool evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target,
105105
uint64_t &Value, bool RecordReloc,
106106
MutableArrayRef<char> Contents) const;
107107

llvm/include/llvm/MC/MCFixup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class MCFixup {
7373
/// determine how the operand value should be encoded into the instruction.
7474
MCFixupKind Kind = FK_NONE;
7575

76+
bool PCRel = false;
77+
7678
/// Used by RISC-V style linker relaxation. Whether the fixup is
7779
/// linker-relaxable.
7880
bool LinkerRelaxable = false;
@@ -105,6 +107,8 @@ class MCFixup {
105107

106108
const MCExpr *getValue() const { return Value; }
107109

110+
bool isPCRel() const { return PCRel; }
111+
void setPCRel() { PCRel = true; }
108112
bool isLinkerRelaxable() const { return LinkerRelaxable; }
109113
void setLinkerRelaxable() { LinkerRelaxable = true; }
110114

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,6 @@ bool ELFObjectWriter::checkRelocation(SMLoc Loc, const MCSectionELF *From,
13261326
void ELFObjectWriter::recordRelocation(const MCFragment &F,
13271327
const MCFixup &Fixup, MCValue Target,
13281328
uint64_t &FixedValue) {
1329-
MCAsmBackend &Backend = Asm->getBackend();
13301329
const MCSectionELF &Section = cast<MCSectionELF>(*F.getParent());
13311330
MCContext &Ctx = getContext();
13321331

@@ -1337,8 +1336,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
13371336
if (DwoOS && !checkRelocation(Fixup.getLoc(), &Section, SecA))
13381337
return;
13391338

1340-
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1341-
MCFixupKindInfo::FKF_IsPCRel;
1339+
bool IsPCRel = Fixup.isPCRel();
13421340
uint64_t FixupOffset = Asm->getFragmentOffset(F) + Fixup.getOffset();
13431341
uint64_t Addend = Target.getConstant();
13441342
if (auto *RefB = Target.getSubSym()) {

llvm/lib/MC/MCAssembler.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
142142
return true;
143143
}
144144

145-
bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
145+
bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup,
146146
MCValue &Target, uint64_t &Value,
147147
bool RecordReloc,
148148
MutableArrayRef<char> Contents) const {
@@ -163,6 +163,7 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
163163

164164
bool IsResolved = false;
165165
unsigned FixupFlags = getBackend().getFixupKindInfo(Fixup.getKind()).Flags;
166+
bool IsPCRel = FixupFlags & MCFixupKindInfo::FKF_IsPCRel;
166167
if (FixupFlags & MCFixupKindInfo::FKF_IsTarget) {
167168
IsResolved = getBackend().evaluateTargetFixup(Fixup, Target, Value);
168169
} else {
@@ -174,7 +175,6 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
174175
if (Sub && Sub->isDefined())
175176
Value -= getSymbolOffset(*Sub);
176177

177-
bool IsPCRel = FixupFlags & MCFixupKindInfo::FKF_IsPCRel;
178178
bool ShouldAlignPC =
179179
FixupFlags & MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
180180
if (IsPCRel) {
@@ -202,6 +202,8 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
202202

203203
if (IsResolved && mc::isRelocRelocation(Fixup.getKind()))
204204
IsResolved = false;
205+
if (IsPCRel)
206+
Fixup.setPCRel();
205207
getBackend().applyFixup(F, Fixup, Target, Contents, Value, IsResolved);
206208
return true;
207209
}
@@ -875,7 +877,7 @@ void MCAssembler::layout() {
875877
// Process fragments with fixups here.
876878
if (auto *F = dyn_cast<MCEncodedFragment>(&Frag)) {
877879
auto Contents = F->getContents();
878-
for (const MCFixup &Fixup : F->getFixups()) {
880+
for (MCFixup &Fixup : F->getFixups()) {
879881
uint64_t FixedValue;
880882
MCValue Target;
881883
evaluateFixup(Frag, Fixup, Target, FixedValue,

llvm/lib/MC/XCOFFObjectWriter.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -686,15 +686,10 @@ void XCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
686686
};
687687

688688
const MCSymbol *const SymA = Target.getAddSym();
689-
690-
MCAsmBackend &Backend = Asm->getBackend();
691-
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
692-
MCFixupKindInfo::FKF_IsPCRel;
693-
694689
uint8_t Type;
695690
uint8_t SignAndSize;
696-
std::tie(Type, SignAndSize) =
697-
TargetObjectWriter->getRelocTypeAndSignSize(Target, Fixup, IsPCRel);
691+
std::tie(Type, SignAndSize) = TargetObjectWriter->getRelocTypeAndSignSize(
692+
Target, Fixup, Fixup.isPCRel());
698693

699694
const MCSectionXCOFF *SymASec = getContainingCsect(cast<MCSymbolXCOFF>(SymA));
700695
assert(SectionMap.contains(SymASec) &&

0 commit comments

Comments
 (0)