Skip to content

Commit b1d58f0

Browse files
committed
MCAssembler: Simplify fragment relaxation
* FT_Data: skip relaxFragment * Others: Call relaxFragment, which computes the old size, calls the relevant relaxXXX function, then compares the size.
1 parent eadf0eb commit b1d58f0

File tree

7 files changed

+65
-109
lines changed

7 files changed

+65
-109
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,8 @@ class LLVM_ABI MCAsmBackend {
166166
// Return false to use default handling. Otherwise, set `Size` to the number
167167
// of padding bytes.
168168
virtual bool relaxAlign(MCFragment &F, unsigned &Size) { return false; }
169-
virtual bool relaxDwarfLineAddr(MCFragment &, bool &WasRelaxed) const {
170-
return false;
171-
}
172-
virtual bool relaxDwarfCFA(MCFragment &, bool &WasRelaxed) const {
173-
return false;
174-
}
169+
virtual bool relaxDwarfLineAddr(MCFragment &) const { return false; }
170+
virtual bool relaxDwarfCFA(MCFragment &) const { return false; }
175171

176172
// Defined by linker relaxation targets to possibly emit LEB128 relocations
177173
// and set Value at the relocated location.

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,11 @@ class MCAssembler {
112112

113113
/// Perform relaxation on a single fragment.
114114
bool relaxFragment(MCFragment &F);
115-
bool relaxInstruction(MCFragment &F);
116-
bool relaxLEB(MCFragment &F);
117-
bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
118-
bool relaxDwarfLineAddr(MCFragment &F);
119-
bool relaxDwarfCallFrameFragment(MCFragment &F);
120-
bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
121-
bool relaxCVDefRange(MCCVDefRangeFragment &DF);
122-
bool relaxFill(MCFillFragment &F);
123-
bool relaxOrg(MCOrgFragment &F);
115+
void relaxInstruction(MCFragment &F);
116+
void relaxLEB(MCFragment &F);
117+
void relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
118+
void relaxDwarfLineAddr(MCFragment &F);
119+
void relaxDwarfCallFrameFragment(MCFragment &F);
124120

125121
public:
126122
/// Construct a new assembler instance.

llvm/lib/MC/MCAssembler.cpp

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const {
203203
case MCFragment::FT_CVDefRange:
204204
return F.getSize();
205205
case MCFragment::FT_Fill: {
206-
auto &FF = cast<MCFillFragment>(F);
206+
auto &FF = static_cast<const MCFillFragment &>(F);
207207
int64_t NumValues = 0;
208208
if (!FF.getNumValues().evaluateKnownAbsolute(NumValues, *this)) {
209209
recordError(FF.getLoc(), "expected assembly-time absolute expression");
@@ -748,22 +748,22 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFragment &F,
748748
Resolved);
749749
}
750750

751-
bool MCAssembler::relaxInstruction(MCFragment &F) {
751+
void MCAssembler::relaxInstruction(MCFragment &F) {
752752
assert(getEmitterPtr() &&
753753
"Expected CodeEmitter defined for relaxInstruction");
754754
// If this inst doesn't ever need relaxation, ignore it. This occurs when we
755755
// are intentionally pushing out inst fragments, or because we relaxed a
756756
// previous instruction to one that doesn't need relaxation.
757757
if (!getBackend().mayNeedRelaxation(F.getOpcode(), F.getOperands(),
758758
*F.getSubtargetInfo()))
759-
return false;
759+
return;
760760

761761
bool DoRelax = false;
762762
for (const MCFixup &Fixup : F.getVarFixups())
763763
if ((DoRelax = fixupNeedsRelaxation(F, Fixup)))
764764
break;
765765
if (!DoRelax)
766-
return false;
766+
return;
767767

768768
++stats::RelaxedInstructions;
769769

@@ -779,12 +779,10 @@ bool MCAssembler::relaxInstruction(MCFragment &F) {
779779
getEmitter().encodeInstruction(Relaxed, Data, Fixups, *F.getSubtargetInfo());
780780
F.setVarContents(Data);
781781
F.setVarFixups(Fixups);
782-
return true;
783782
}
784783

785-
bool MCAssembler::relaxLEB(MCFragment &F) {
786-
const unsigned OldSize = F.getVarSize();
787-
unsigned PadTo = OldSize;
784+
void MCAssembler::relaxLEB(MCFragment &F) {
785+
unsigned PadTo = F.getVarSize();
788786
int64_t Value;
789787
F.clearVarFixups();
790788
// Use evaluateKnownAbsolute for Mach-O as a hack: .subsections_via_symbols
@@ -818,7 +816,6 @@ bool MCAssembler::relaxLEB(MCFragment &F) {
818816
else
819817
Size = encodeULEB128(Value, Data, PadTo);
820818
F.setVarContents({reinterpret_cast<char *>(Data), Size});
821-
return OldSize != Size;
822819
}
823820

824821
/// Check if the branch crosses the boundary.
@@ -858,11 +855,11 @@ static bool needPadding(uint64_t StartAddr, uint64_t Size,
858855
isAgainstBoundary(StartAddr, Size, BoundaryAlignment);
859856
}
860857

861-
bool MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) {
858+
void MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) {
862859
// BoundaryAlignFragment that doesn't need to align any fragment should not be
863860
// relaxed.
864861
if (!BF.getLastFragment())
865-
return false;
862+
return;
866863

867864
uint64_t AlignedOffset = getFragmentOffset(BF);
868865
uint64_t AlignedSize = 0;
@@ -877,18 +874,15 @@ bool MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) {
877874
? offsetToAlignment(AlignedOffset, BoundaryAlignment)
878875
: 0U;
879876
if (NewSize == BF.getSize())
880-
return false;
877+
return;
881878
BF.setSize(NewSize);
882-
return true;
883879
}
884880

885-
bool MCAssembler::relaxDwarfLineAddr(MCFragment &F) {
886-
bool WasRelaxed;
887-
if (getBackend().relaxDwarfLineAddr(F, WasRelaxed))
888-
return WasRelaxed;
881+
void MCAssembler::relaxDwarfLineAddr(MCFragment &F) {
882+
if (getBackend().relaxDwarfLineAddr(F))
883+
return;
889884

890885
MCContext &Context = getContext();
891-
auto OldSize = F.getVarSize();
892886
int64_t AddrDelta;
893887
bool Abs = F.getDwarfAddrDelta().evaluateKnownAbsolute(AddrDelta, *this);
894888
assert(Abs && "We created a line delta with an invalid expression");
@@ -898,13 +892,11 @@ bool MCAssembler::relaxDwarfLineAddr(MCFragment &F) {
898892
F.getDwarfLineDelta(), AddrDelta, Data);
899893
F.setVarContents(Data);
900894
F.clearVarFixups();
901-
return OldSize != Data.size();
902895
}
903896

904-
bool MCAssembler::relaxDwarfCallFrameFragment(MCFragment &F) {
905-
bool WasRelaxed;
906-
if (getBackend().relaxDwarfCFA(F, WasRelaxed))
907-
return WasRelaxed;
897+
void MCAssembler::relaxDwarfCallFrameFragment(MCFragment &F) {
898+
if (getBackend().relaxDwarfCFA(F))
899+
return;
908900

909901
MCContext &Context = getContext();
910902
int64_t Value;
@@ -913,69 +905,60 @@ bool MCAssembler::relaxDwarfCallFrameFragment(MCFragment &F) {
913905
reportError(F.getDwarfAddrDelta().getLoc(),
914906
"invalid CFI advance_loc expression");
915907
F.setDwarfAddrDelta(MCConstantExpr::create(0, Context));
916-
return false;
908+
return;
917909
}
918910

919-
auto OldSize = F.getVarContents().size();
920911
SmallVector<char, 8> Data;
921912
MCDwarfFrameEmitter::encodeAdvanceLoc(Context, Value, Data);
922913
F.setVarContents(Data);
923914
F.clearVarFixups();
924-
return OldSize != Data.size();
925-
}
926-
927-
bool MCAssembler::relaxCVInlineLineTable(MCCVInlineLineTableFragment &F) {
928-
unsigned OldSize = F.getVarContents().size();
929-
getContext().getCVContext().encodeInlineLineTable(*this, F);
930-
return OldSize != F.getVarContents().size();
931-
}
932-
933-
bool MCAssembler::relaxCVDefRange(MCCVDefRangeFragment &F) {
934-
unsigned OldSize = F.getVarContents().size();
935-
getContext().getCVContext().encodeDefRange(*this, F);
936-
return OldSize != F.getVarContents().size();
937-
}
938-
939-
bool MCAssembler::relaxFill(MCFillFragment &F) {
940-
uint64_t Size = computeFragmentSize(F);
941-
if (F.getSize() == Size)
942-
return false;
943-
F.setSize(Size);
944-
return true;
945-
}
946-
947-
bool MCAssembler::relaxOrg(MCOrgFragment &F) {
948-
uint64_t Size = computeFragmentSize(F);
949-
if (F.getSize() == Size)
950-
return false;
951-
F.setSize(Size);
952-
return true;
953915
}
954916

955917
bool MCAssembler::relaxFragment(MCFragment &F) {
956-
switch(F.getKind()) {
918+
size_t Size = computeFragmentSize(F);
919+
switch (F.getKind()) {
957920
default:
958921
return false;
959922
case MCFragment::FT_Relaxable:
960923
assert(!getRelaxAll() && "Did not expect a FT_Relaxable in RelaxAll mode");
961-
return relaxInstruction(F);
924+
relaxInstruction(F);
925+
break;
962926
case MCFragment::FT_LEB:
963-
return relaxLEB(F);
927+
relaxLEB(F);
928+
break;
964929
case MCFragment::FT_Dwarf:
965-
return relaxDwarfLineAddr(F);
930+
relaxDwarfLineAddr(F);
931+
break;
966932
case MCFragment::FT_DwarfFrame:
967-
return relaxDwarfCallFrameFragment(F);
933+
relaxDwarfCallFrameFragment(F);
934+
break;
968935
case MCFragment::FT_BoundaryAlign:
969-
return relaxBoundaryAlign(cast<MCBoundaryAlignFragment>(F));
936+
relaxBoundaryAlign(static_cast<MCBoundaryAlignFragment &>(F));
937+
break;
970938
case MCFragment::FT_CVInlineLines:
971-
return relaxCVInlineLineTable(cast<MCCVInlineLineTableFragment>(F));
939+
getContext().getCVContext().encodeInlineLineTable(
940+
*this, static_cast<MCCVInlineLineTableFragment &>(F));
941+
break;
972942
case MCFragment::FT_CVDefRange:
973-
return relaxCVDefRange(cast<MCCVDefRangeFragment>(F));
974-
case MCFragment::FT_Fill:
975-
return relaxFill(cast<MCFillFragment>(F));
976-
case MCFragment::FT_Org:
977-
return relaxOrg(static_cast<MCOrgFragment &>(F));
943+
getContext().getCVContext().encodeDefRange(
944+
*this, static_cast<MCCVDefRangeFragment &>(F));
945+
break;
946+
case MCFragment::FT_Fill: {
947+
auto &FF = static_cast<MCFillFragment &>(F);
948+
if (FF.getSize() == Size)
949+
return false;
950+
FF.setSize(Size);
951+
return true;
952+
}
953+
case MCFragment::FT_Org: {
954+
auto &FF = static_cast<MCOrgFragment &>(F);
955+
if (FF.getSize() == Size)
956+
return false;
957+
FF.setSize(Size);
958+
return true;
959+
}
978960
}
961+
return computeFragmentSize(F) != Size;
979962
}
980963

981964
void MCAssembler::layoutSection(MCSection &Sec) {
@@ -1024,7 +1007,7 @@ unsigned MCAssembler::relaxOnce(unsigned FirstStable) {
10241007
for (;;) {
10251008
bool Changed = false;
10261009
for (MCFragment &F : Sec)
1027-
if (relaxFragment(F))
1010+
if (F.getKind() != MCFragment::FT_Data && relaxFragment(F))
10281011
Changed = true;
10291012

10301013
if (!Changed)

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,10 @@ std::pair<bool, bool> LoongArchAsmBackend::relaxLEB128(MCFragment &F,
259259
return std::make_pair(true, true);
260260
}
261261

262-
bool LoongArchAsmBackend::relaxDwarfLineAddr(MCFragment &F,
263-
bool &WasRelaxed) const {
262+
bool LoongArchAsmBackend::relaxDwarfLineAddr(MCFragment &F) const {
264263
MCContext &C = getContext();
265-
266264
int64_t LineDelta = F.getDwarfLineDelta();
267265
const MCExpr &AddrDelta = F.getDwarfAddrDelta();
268-
size_t OldSize = F.getVarSize();
269-
270266
int64_t Value;
271267
if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
272268
return false;
@@ -312,15 +308,12 @@ bool LoongArchAsmBackend::relaxDwarfLineAddr(MCFragment &F,
312308
F.setVarContents(Data);
313309
F.setVarFixups({MCFixup::create(Offset, &AddrDelta,
314310
MCFixup::getDataKindForSize(PCBytes))});
315-
WasRelaxed = OldSize != Data.size();
316311
return true;
317312
}
318313

319-
bool LoongArchAsmBackend::relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const {
314+
bool LoongArchAsmBackend::relaxDwarfCFA(MCFragment &F) const {
320315
const MCExpr &AddrDelta = F.getDwarfAddrDelta();
321316
SmallVector<MCFixup, 2> Fixups;
322-
size_t OldSize = F.getVarContents().size();
323-
324317
int64_t Value;
325318
if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
326319
return false;
@@ -333,7 +326,6 @@ bool LoongArchAsmBackend::relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const {
333326
if (Value == 0) {
334327
F.clearVarContents();
335328
F.clearVarFixups();
336-
WasRelaxed = OldSize != 0;
337329
return true;
338330
}
339331

@@ -367,8 +359,6 @@ bool LoongArchAsmBackend::relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const {
367359
}
368360
F.setVarContents(Data);
369361
F.setVarFixups(Fixups);
370-
371-
WasRelaxed = OldSize != Data.size();
372362
return true;
373363
}
374364

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class LoongArchAsmBackend : public MCAsmBackend {
4949
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
5050

5151
bool relaxAlign(MCFragment &F, unsigned &Size) override;
52-
bool relaxDwarfLineAddr(MCFragment &F, bool &WasRelaxed) const override;
53-
bool relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const override;
52+
bool relaxDwarfLineAddr(MCFragment &) const override;
53+
bool relaxDwarfCFA(MCFragment &) const override;
5454
std::pair<bool, bool> relaxLEB128(MCFragment &F,
5555
int64_t &Value) const override;
5656

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,9 @@ bool RISCVAsmBackend::relaxAlign(MCFragment &F, unsigned &Size) {
340340
return true;
341341
}
342342

343-
bool RISCVAsmBackend::relaxDwarfLineAddr(MCFragment &F,
344-
bool &WasRelaxed) const {
343+
bool RISCVAsmBackend::relaxDwarfLineAddr(MCFragment &F) const {
345344
int64_t LineDelta = F.getDwarfLineDelta();
346345
const MCExpr &AddrDelta = F.getDwarfAddrDelta();
347-
size_t OldSize = F.getVarSize();
348-
349346
int64_t Value;
350347
// If the label difference can be resolved, use the default handling, which
351348
// utilizes a shorter special opcode.
@@ -391,15 +388,12 @@ bool RISCVAsmBackend::relaxDwarfLineAddr(MCFragment &F,
391388
F.setVarContents(Data);
392389
F.setVarFixups({MCFixup::create(Offset, &AddrDelta,
393390
MCFixup::getDataKindForSize(PCBytes))});
394-
WasRelaxed = OldSize != Data.size();
395391
return true;
396392
}
397393

398-
bool RISCVAsmBackend::relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const {
394+
bool RISCVAsmBackend::relaxDwarfCFA(MCFragment &F) const {
399395
const MCExpr &AddrDelta = F.getDwarfAddrDelta();
400396
SmallVector<MCFixup, 2> Fixups;
401-
size_t OldSize = F.getVarSize();
402-
403397
int64_t Value;
404398
if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
405399
return false;
@@ -412,7 +406,6 @@ bool RISCVAsmBackend::relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const {
412406
if (Value == 0) {
413407
F.clearVarContents();
414408
F.clearVarFixups();
415-
WasRelaxed = OldSize != 0;
416409
return true;
417410
}
418411

@@ -445,8 +438,6 @@ bool RISCVAsmBackend::relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const {
445438
}
446439
F.setVarContents(Data);
447440
F.setVarFixups(Fixups);
448-
449-
WasRelaxed = OldSize != Data.size();
450441
return true;
451442
}
452443

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class RISCVAsmBackend : public MCAsmBackend {
6565
const MCSubtargetInfo &STI) const override;
6666

6767
bool relaxAlign(MCFragment &F, unsigned &Size) override;
68-
bool relaxDwarfLineAddr(MCFragment &F, bool &WasRelaxed) const override;
69-
bool relaxDwarfCFA(MCFragment &F, bool &WasRelaxed) const override;
68+
bool relaxDwarfLineAddr(MCFragment &) const override;
69+
bool relaxDwarfCFA(MCFragment &) const override;
7070
std::pair<bool, bool> relaxLEB128(MCFragment &LF,
7171
int64_t &Value) const override;
7272

0 commit comments

Comments
 (0)