Skip to content

Commit b90a926

Browse files
committed
MCExpr: Remove unused SectionAddrMap workaround
Mach-O's ARM and X86 writers use MCExpr's `SectionAddrMap *Addrs` argument to compute label differences, which was a bit of a hack. The hack has been cleaned up by commit 1b7759de8e6979dda2d949b1ba1c742922e5c366.
1 parent 976de53 commit b90a926

File tree

3 files changed

+22
-45
lines changed

3 files changed

+22
-45
lines changed

llvm/include/llvm/MC/MCExpr.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class raw_ostream;
2828
class StringRef;
2929
class MCSymbolRefExpr;
3030

31-
using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
32-
3331
/// Base class for the full range of assembler expressions which are
3432
/// needed for parsing.
3533
class MCExpr {
@@ -54,7 +52,7 @@ class MCExpr {
5452
SMLoc Loc;
5553

5654
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
57-
const SectionAddrMap *Addrs, bool InSet) const;
55+
bool InSet) const;
5856

5957
protected:
6058
explicit MCExpr(ExprKind Kind, SMLoc Loc, unsigned SubclassData = 0)
@@ -64,7 +62,7 @@ class MCExpr {
6462
}
6563

6664
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
67-
const SectionAddrMap *Addrs, bool InSet) const;
65+
bool InSet) const;
6866

6967
unsigned getSubclassData() const { return SubclassData; }
7068

@@ -98,8 +96,6 @@ class MCExpr {
9896
///
9997
/// \param Res - The absolute value, if evaluation succeeds.
10098
/// \return - True on success.
101-
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm,
102-
const SectionAddrMap &Addrs) const;
10399
bool evaluateAsAbsolute(int64_t &Res) const;
104100
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
105101
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const;
@@ -132,9 +128,8 @@ class MCExpr {
132128

133129
/// @}
134130

135-
static bool evaluateSymbolicAdd(const MCAssembler *, const SectionAddrMap *,
136-
bool, const MCValue &, const MCValue &,
137-
MCValue &);
131+
static bool evaluateSymbolicAdd(const MCAssembler *, bool, const MCValue &,
132+
const MCValue &, MCValue &);
138133
};
139134

140135
inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class MachObjectWriter final : public MCObjectWriter {
141141

142142
std::vector<DataRegionData> DataRegions;
143143

144-
SectionAddrMap SectionAddress;
144+
DenseMap<const MCSection *, uint64_t> SectionAddress;
145145

146146
// List of sections in layout order. Virtual sections are after non-virtual
147147
// sections.
@@ -203,7 +203,6 @@ class MachObjectWriter final : public MCObjectWriter {
203203
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
204204
return SectionOrder;
205205
}
206-
SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
207206
MCLOHContainer &getLOHContainer() { return LOHContainer; }
208207

209208
uint64_t getSectionAddress(const MCSection *Sec) const {

llvm/lib/MC/MCExpr.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -254,30 +254,23 @@ void MCTargetExpr::anchor() {}
254254
/* *** */
255255

256256
bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
257-
return evaluateAsAbsolute(Res, nullptr, nullptr, false);
258-
}
259-
260-
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm,
261-
const SectionAddrMap &Addrs) const {
262-
// Setting InSet causes us to absolutize differences across sections and that
263-
// is what the MachO writer uses Addrs for.
264-
return evaluateAsAbsolute(Res, &Asm, &Addrs, true);
257+
return evaluateAsAbsolute(Res, nullptr, false);
265258
}
266259

267260
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
268-
return evaluateAsAbsolute(Res, &Asm, nullptr, false);
261+
return evaluateAsAbsolute(Res, &Asm, false);
269262
}
270263

271264
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
272-
return evaluateAsAbsolute(Res, Asm, nullptr, false);
265+
return evaluateAsAbsolute(Res, Asm, false);
273266
}
274267

275268
bool MCExpr::evaluateKnownAbsolute(int64_t &Res, const MCAssembler &Asm) const {
276-
return evaluateAsAbsolute(Res, &Asm, nullptr, true);
269+
return evaluateAsAbsolute(Res, &Asm, true);
277270
}
278271

279272
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
280-
const SectionAddrMap *Addrs, bool InSet) const {
273+
bool InSet) const {
281274
MCValue Value;
282275

283276
// Fast path constants.
@@ -286,7 +279,7 @@ bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
286279
return true;
287280
}
288281

289-
bool IsRelocatable = evaluateAsRelocatableImpl(Value, Asm, Addrs, InSet);
282+
bool IsRelocatable = evaluateAsRelocatableImpl(Value, Asm, InSet);
290283
Res = Value.getConstant();
291284
// Value with RefKind (e.g. %hi(0xdeadbeef) in MIPS) is not considered
292285
// absolute (the value is unknown at parse time), even if it might be resolved
@@ -296,7 +289,6 @@ bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
296289

297290
/// Helper method for \see EvaluateSymbolAdd().
298291
static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
299-
const SectionAddrMap *Addrs,
300292
bool InSet, const MCSymbol *&A,
301293
const MCSymbol *&B,
302294
int64_t &Addend) {
@@ -324,7 +316,7 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
324316
const MCFragment *FB = SB.getFragment();
325317
const MCSection &SecA = *FA->getParent();
326318
const MCSection &SecB = *FB->getParent();
327-
if ((&SecA != &SecB) && !Addrs)
319+
if (&SecA != &SecB)
328320
return;
329321

330322
// When layout is available, we can generally compute the difference using the
@@ -345,9 +337,6 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
345337

346338
// Eagerly evaluate when layout is finalized.
347339
Addend += Asm->getSymbolOffset(SA) - Asm->getSymbolOffset(SB);
348-
if (Addrs && (&SecA != &SecB))
349-
Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
350-
351340
FinalizeFolding();
352341
} else {
353342
// When layout is not finalized, our ability to resolve differences between
@@ -434,8 +423,7 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
434423
// NOTE: This function can be used before layout is done (see the object
435424
// streamer for example) and having the Asm argument lets us avoid relaxations
436425
// early.
437-
bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm,
438-
const SectionAddrMap *Addrs, bool InSet,
426+
bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm, bool InSet,
439427
const MCValue &LHS, const MCValue &RHS,
440428
MCValue &Res) {
441429
const MCSymbol *LHS_A = LHS.getAddSym();
@@ -456,12 +444,10 @@ bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm,
456444
// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
457445
// might bring more opportunities.
458446
if (LHS_A && RHS_B) {
459-
attemptToFoldSymbolOffsetDifference(Asm, Addrs, InSet, LHS_A, RHS_B,
460-
Result_Cst);
447+
attemptToFoldSymbolOffsetDifference(Asm, InSet, LHS_A, RHS_B, Result_Cst);
461448
}
462449
if (RHS_A && LHS_B) {
463-
attemptToFoldSymbolOffsetDifference(Asm, Addrs, InSet, RHS_A, LHS_B,
464-
Result_Cst);
450+
attemptToFoldSymbolOffsetDifference(Asm, InSet, RHS_A, LHS_B, Result_Cst);
465451
}
466452
}
467453

@@ -481,10 +467,10 @@ bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm,
481467
}
482468

483469
bool MCExpr::evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const {
484-
return evaluateAsRelocatableImpl(Res, Asm, nullptr, false);
470+
return evaluateAsRelocatableImpl(Res, Asm, false);
485471
}
486472
bool MCExpr::evaluateAsValue(MCValue &Res, const MCAssembler &Asm) const {
487-
return evaluateAsRelocatableImpl(Res, &Asm, nullptr, true);
473+
return evaluateAsRelocatableImpl(Res, &Asm, true);
488474
}
489475
static bool canExpand(const MCSymbol &Sym, bool InSet) {
490476
if (Sym.isWeakExternal())
@@ -503,7 +489,6 @@ static bool canExpand(const MCSymbol &Sym, bool InSet) {
503489
}
504490

505491
bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
506-
const SectionAddrMap *Addrs,
507492
bool InSet) const {
508493
++stats::MCExprEvaluate;
509494
switch (getKind()) {
@@ -523,7 +508,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
523508
if (Sym.isVariable() && (Kind == MCSymbolRefExpr::VK_None || Layout) &&
524509
canExpand(Sym, InSet)) {
525510
bool IsMachO = SRE->hasSubsectionsViaSymbols();
526-
if (Sym.getVariableValue()->evaluateAsRelocatableImpl(Res, Asm, Addrs,
511+
if (Sym.getVariableValue()->evaluateAsRelocatableImpl(Res, Asm,
527512
InSet || IsMachO)) {
528513
if (Kind != MCSymbolRefExpr::VK_None) {
529514
if (Res.isAbsolute()) {
@@ -566,7 +551,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
566551
const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
567552
MCValue Value;
568553

569-
if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Addrs, InSet))
554+
if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, InSet))
570555
return false;
571556
switch (AUE->getOpcode()) {
572557
case MCUnaryExpr::LNot:
@@ -600,10 +585,8 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
600585
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
601586
MCValue LHSValue, RHSValue;
602587

603-
if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Addrs,
604-
InSet) ||
605-
!ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Addrs,
606-
InSet)) {
588+
if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, InSet) ||
589+
!ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, InSet)) {
607590
// Check if both are Target Expressions, see if we can compare them.
608591
if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS())) {
609592
if (const MCTargetExpr *R = dyn_cast<MCTargetExpr>(ABE->getRHS())) {
@@ -650,7 +633,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
650633
return false;
651634
if (RHSValue.SymB && RHSValue.Specifier)
652635
return false;
653-
return evaluateSymbolicAdd(Asm, Addrs, InSet, LHSValue, RHSValue, Res);
636+
return evaluateSymbolicAdd(Asm, InSet, LHSValue, RHSValue, Res);
654637
}
655638
}
656639

0 commit comments

Comments
 (0)