Skip to content

Commit 267b136

Browse files
committed
MCFragment: Refactor code for MCFragment
To prepare for moving content and fixup member variables from MCEncodedFragment to MCFragment and removing MCDataFragment/MCRelaxableFragment classes, replace dyn_cast with getKind() tests.
1 parent 6f92313 commit 267b136

File tree

3 files changed

+105
-102
lines changed

3 files changed

+105
-102
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -31,101 +31,21 @@ class MCAsmInfo;
3131
class MCAssembler;
3232
class MCContext;
3333
class MCExpr;
34+
class MCFragment;
3435
class MCObjectStreamer;
3536
class MCSymbol;
3637
class MCSection;
3738
class MCSubtargetInfo;
3839
class raw_ostream;
3940
class Triple;
4041

41-
// Represents a contiguous piece of code or data within a section. Its size is
42-
// determined by MCAssembler::layout. All subclasses must have trivial
43-
// destructors.
44-
//
45-
// Declaration order: MCFragment, MCSection, then MCFragment's derived classes.
46-
// This allows MCSection's inline functions to access MCFragment members and
47-
// allows MCFragment's derived classes to access MCSection.
48-
class MCFragment {
49-
friend class MCAssembler;
50-
friend class MCObjectStreamer;
51-
friend class MCSection;
52-
53-
public:
54-
enum FragmentType : uint8_t {
55-
FT_Data,
56-
FT_Relaxable,
57-
FT_Align,
58-
FT_Fill,
59-
FT_LEB,
60-
FT_Nops,
61-
FT_Org,
62-
FT_Dwarf,
63-
FT_DwarfFrame,
64-
FT_BoundaryAlign,
65-
FT_SymbolId,
66-
FT_CVInlineLines,
67-
FT_CVDefRange,
68-
FT_PseudoProbe,
69-
};
70-
71-
private:
72-
// The next fragment within the section.
73-
MCFragment *Next = nullptr;
74-
75-
/// The data for the section this fragment is in.
76-
MCSection *Parent = nullptr;
77-
78-
/// The offset of this fragment in its section.
79-
uint64_t Offset = 0;
80-
81-
/// The layout order of this fragment.
82-
unsigned LayoutOrder = 0;
83-
84-
FragmentType Kind;
85-
86-
protected:
87-
/// Used by subclasses for better packing.
88-
///
89-
/// MCEncodedFragment
90-
bool HasInstructions : 1;
91-
bool AlignToBundleEnd : 1;
92-
/// MCDataFragment
93-
bool LinkerRelaxable : 1;
94-
/// MCRelaxableFragment: x86-specific
95-
bool AllowAutoPadding : 1;
96-
97-
LLVM_ABI MCFragment(FragmentType Kind, bool HasInstructions);
98-
99-
public:
100-
MCFragment() = delete;
101-
MCFragment(const MCFragment &) = delete;
102-
MCFragment &operator=(const MCFragment &) = delete;
103-
104-
MCFragment *getNext() const { return Next; }
105-
106-
FragmentType getKind() const { return Kind; }
107-
108-
MCSection *getParent() const { return Parent; }
109-
void setParent(MCSection *Value) { Parent = Value; }
110-
111-
LLVM_ABI const MCSymbol *getAtom() const;
112-
113-
unsigned getLayoutOrder() const { return LayoutOrder; }
114-
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
115-
116-
/// Does this fragment have instructions emitted into it? By default
117-
/// this is false, but specific fragment types may set it to true.
118-
bool hasInstructions() const { return HasInstructions; }
119-
120-
LLVM_ABI void dump() const;
121-
};
122-
12342
/// Instances of this class represent a uniqued identifier for a section in the
12443
/// current translation unit. The MCContext class uniques and creates these.
12544
class LLVM_ABI MCSection {
12645
public:
12746
friend MCAssembler;
12847
friend MCObjectStreamer;
48+
friend class MCFragment;
12949
friend class MCEncodedFragment;
13050
friend class MCRelaxableFragment;
13151
static constexpr unsigned NonUniqueID = ~0U;
@@ -155,10 +75,7 @@ class LLVM_ABI MCSection {
15575
MCFragment &operator*() const { return *F; }
15676
bool operator==(const iterator &O) const { return F == O.F; }
15777
bool operator!=(const iterator &O) const { return F != O.F; }
158-
iterator &operator++() {
159-
F = F->Next;
160-
return *this;
161-
}
78+
iterator &operator++();
16279
};
16380

16481
struct FragList {
@@ -296,6 +213,87 @@ class LLVM_ABI MCSection {
296213
virtual StringRef getVirtualSectionKind() const;
297214
};
298215

216+
// Represents a contiguous piece of code or data within a section. Its size is
217+
// determined by MCAssembler::layout. All subclasses must have trivial
218+
// destructors.
219+
class MCFragment {
220+
friend class MCAssembler;
221+
friend class MCObjectStreamer;
222+
friend class MCSection;
223+
224+
public:
225+
enum FragmentType : uint8_t {
226+
FT_Data,
227+
FT_Relaxable,
228+
FT_Align,
229+
FT_Fill,
230+
FT_LEB,
231+
FT_Nops,
232+
FT_Org,
233+
FT_Dwarf,
234+
FT_DwarfFrame,
235+
FT_BoundaryAlign,
236+
FT_SymbolId,
237+
FT_CVInlineLines,
238+
FT_CVDefRange,
239+
FT_PseudoProbe,
240+
};
241+
242+
private:
243+
// The next fragment within the section.
244+
MCFragment *Next = nullptr;
245+
246+
/// The data for the section this fragment is in.
247+
MCSection *Parent = nullptr;
248+
249+
/// The offset of this fragment in its section.
250+
uint64_t Offset = 0;
251+
252+
/// The layout order of this fragment.
253+
unsigned LayoutOrder = 0;
254+
255+
FragmentType Kind;
256+
257+
protected:
258+
/// Used by subclasses for better packing.
259+
///
260+
/// MCEncodedFragment
261+
bool HasInstructions : 1;
262+
bool AlignToBundleEnd : 1;
263+
/// MCDataFragment
264+
bool LinkerRelaxable : 1;
265+
/// MCRelaxableFragment: x86-specific
266+
bool AllowAutoPadding : 1;
267+
268+
LLVM_ABI MCFragment(FragmentType Kind, bool HasInstructions);
269+
270+
public:
271+
MCFragment() = delete;
272+
MCFragment(const MCFragment &) = delete;
273+
MCFragment &operator=(const MCFragment &) = delete;
274+
275+
MCFragment *getNext() const { return Next; }
276+
277+
FragmentType getKind() const { return Kind; }
278+
279+
MCSection *getParent() const { return Parent; }
280+
void setParent(MCSection *Value) { Parent = Value; }
281+
282+
LLVM_ABI const MCSymbol *getAtom() const;
283+
284+
unsigned getLayoutOrder() const { return LayoutOrder; }
285+
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
286+
287+
/// Does this fragment have instructions emitted into it? By default
288+
/// this is false, but specific fragment types may set it to true.
289+
bool hasInstructions() const { return HasInstructions; }
290+
291+
bool isLinkerRelaxable() const { return LinkerRelaxable; }
292+
void setLinkerRelaxable() { LinkerRelaxable = true; }
293+
294+
LLVM_ABI void dump() const;
295+
};
296+
299297
/// Interface implemented by fragments that contain encoded instructions and/or
300298
/// data.
301299
class MCEncodedFragment : public MCFragment {
@@ -357,6 +355,9 @@ class MCEncodedFragment : public MCFragment {
357355
this->STI = &STI;
358356
}
359357

358+
bool getAllowAutoPadding() const { return AllowAutoPadding; }
359+
void setAllowAutoPadding(bool V) { AllowAutoPadding = V; }
360+
360361
// Content-related functions manage parent's storage using ContentStart and
361362
// ContentSize.
362363
void clearContents() { ContentEnd = ContentStart; }
@@ -407,6 +408,8 @@ class MCEncodedFragment : public MCFragment {
407408
return ArrayRef(getParent()->FixupStorage)
408409
.slice(FixupStart, FixupEnd - FixupStart);
409410
}
411+
412+
size_t getSize() const { return ContentEnd - ContentStart; }
410413
};
411414

412415
/// Fragment for data and encoded instructions.
@@ -418,9 +421,6 @@ class MCDataFragment : public MCEncodedFragment {
418421
static bool classof(const MCFragment *F) {
419422
return F->getKind() == MCFragment::FT_Data;
420423
}
421-
422-
bool isLinkerRelaxable() const { return LinkerRelaxable; }
423-
void setLinkerRelaxable() { LinkerRelaxable = true; }
424424
};
425425

426426
/// A relaxable fragment holds on to its MCInst, since it may need to be
@@ -463,9 +463,6 @@ class MCRelaxableFragment : public MCEncodedFragment {
463463
llvm::copy(Inst, S.begin() + OperandStart);
464464
}
465465

466-
bool getAllowAutoPadding() const { return AllowAutoPadding; }
467-
void setAllowAutoPadding(bool V) { AllowAutoPadding = V; }
468-
469466
static bool classof(const MCFragment *F) {
470467
return F->getKind() == MCFragment::FT_Relaxable;
471468
}
@@ -794,6 +791,11 @@ class MCPseudoProbeAddrFragment : public MCEncodedFragment {
794791
}
795792
};
796793

794+
inline MCSection::iterator &MCSection::iterator::operator++() {
795+
F = F->Next;
796+
return *this;
797+
}
798+
797799
} // end namespace llvm
798800

799801
#endif // LLVM_MC_MCSECTION_H

llvm/lib/MC/MCExpr.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,17 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
351351
// instruction, the difference cannot be resolved as it may be changed by
352352
// the linker.
353353
bool BBeforeRelax = false, AAfterRelax = false;
354-
for (auto FI = FB; FI; FI = FI->getNext()) {
355-
auto DF = dyn_cast<MCDataFragment>(FI);
354+
for (auto F = FB; F; F = F->getNext()) {
355+
auto DF = dyn_cast<MCDataFragment>(F);
356356
if (DF && DF->isLinkerRelaxable()) {
357-
if (&*FI != FB || SBOffset != DF->getContents().size())
357+
if (&*F != FB || SBOffset != DF->getContents().size())
358358
BBeforeRelax = true;
359-
if (&*FI != FA || SAOffset == DF->getContents().size())
359+
if (&*F != FA || SAOffset == DF->getContents().size())
360360
AAfterRelax = true;
361361
if (BBeforeRelax && AAfterRelax)
362362
return;
363363
}
364-
if (&*FI == FA) {
364+
if (&*F == FA) {
365365
// If FA and FB belong to the same subsection, the loop will find FA and
366366
// we can resolve the difference.
367367
Addend += Reverse ? -Displacement : Displacement;
@@ -373,18 +373,18 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
373373
unsigned Count;
374374
if (DF) {
375375
Displacement += DF->getContents().size();
376-
} else if (auto *RF = dyn_cast<MCRelaxableFragment>(FI);
376+
} else if (auto *RF = dyn_cast<MCRelaxableFragment>(F);
377377
RF && Asm->hasFinalLayout()) {
378378
// Before finishLayout, a relaxable fragment's size is indeterminate.
379379
// After layout, during relocation generation, it can be treated as a
380380
// data fragment.
381381
Displacement += RF->getContents().size();
382-
} else if (auto *AF = dyn_cast<MCAlignFragment>(FI);
382+
} else if (auto *AF = dyn_cast<MCAlignFragment>(F);
383383
AF && Layout && AF->hasEmitNops() &&
384384
!Asm->getBackend().shouldInsertExtraNopBytesForCodeAlign(
385385
*AF, Count)) {
386386
Displacement += Asm->computeFragmentSize(*AF);
387-
} else if (auto *FF = dyn_cast<MCFillFragment>(FI);
387+
} else if (auto *FF = dyn_cast<MCFillFragment>(F);
388388
FF && FF->getNumValues().evaluateAsAbsolute(Num)) {
389389
Displacement += Num * FF->getValueSize();
390390
} else {

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ static std::optional<uint64_t> absoluteSymbolDiff(const MCSymbol *Hi,
8484
return 0;
8585
if (Hi->isVariable() || Lo->isVariable())
8686
return std::nullopt;
87-
auto *LoF = dyn_cast_or_null<MCDataFragment>(Lo->getFragment());
88-
if (!LoF || Hi->getFragment() != LoF || LoF->isLinkerRelaxable())
87+
auto *LoF = Lo->getFragment();
88+
if (!LoF || LoF->getKind() != MCFragment::FT_Data ||
89+
Hi->getFragment() != LoF || LoF->isLinkerRelaxable())
8990
return std::nullopt;
9091

9192
return Hi->getOffset() - Lo->getOffset();

0 commit comments

Comments
 (0)