Skip to content

Commit 9297af1

Browse files
committed
MCExpr: Simplify and optimize equated symbol evaluation
Sym.isInSection() calls findAssociatedFragment, which traverses the expression tree. This can be replaced by calling evaluateAsRelocatableImpl first and then inspecting the MCValue result.
1 parent 0ba63b2 commit 9297af1

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

llvm/lib/MC/MCExpr.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,6 @@ bool MCExpr::evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const {
475475
bool MCExpr::evaluateAsValue(MCValue &Res, const MCAssembler &Asm) const {
476476
return evaluateAsRelocatableImpl(Res, &Asm, true);
477477
}
478-
static bool canExpand(const MCSymbol &Sym, bool InSet) {
479-
if (Sym.isWeakExternal())
480-
return false;
481-
482-
if (InSet)
483-
return true;
484-
return !Sym.isInSection();
485-
}
486478

487479
bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
488480
bool InSet) const {
@@ -500,7 +492,10 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
500492
const auto Kind = SRE->getKind();
501493
bool Layout = Asm && Asm->hasLayout();
502494

503-
// Evaluate recursively if this is a variable.
495+
// If the symbol is equated, resolve the inner expression.
496+
// However, when two IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols reference
497+
// each other, we retain the equated symbol to avoid a cyclic definition
498+
// error.
504499
if (Sym.isResolving()) {
505500
if (Asm && Asm->hasFinalLayout()) {
506501
Asm->getContext().reportError(
@@ -511,13 +506,20 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
511506
return false;
512507
}
513508
if (Sym.isVariable() && (Kind == MCSymbolRefExpr::VK_None || Layout) &&
514-
canExpand(Sym, InSet)) {
509+
!Sym.isWeakExternal()) {
515510
Sym.setIsResolving(true);
516511
auto _ = make_scope_exit([&] { Sym.setIsResolving(false); });
517512
bool IsMachO =
518513
Asm && Asm->getContext().getAsmInfo()->hasSubsectionsViaSymbols();
519-
if (Sym.getVariableValue()->evaluateAsRelocatableImpl(Res, Asm,
520-
InSet || IsMachO)) {
514+
if (!Sym.getVariableValue()->evaluateAsRelocatableImpl(Res, Asm,
515+
InSet || IsMachO))
516+
return false;
517+
// When generating relocations, if Sym resolves to a symbol relative to a
518+
// section, relocations are generated against Sym. Treat label differences
519+
// as constants.
520+
auto *A = Res.getAddSym();
521+
auto *B = Res.getSubSym();
522+
if (InSet || !(A && !B && A->isInSection())) {
521523
if (Kind != MCSymbolRefExpr::VK_None) {
522524
if (Res.isAbsolute()) {
523525
Res = MCValue::get(&Sym, nullptr, 0, Kind);
@@ -534,8 +536,6 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
534536
if (!IsMachO)
535537
return true;
536538

537-
auto *A = Res.getAddSym();
538-
auto *B = Res.getSubSym();
539539
// FIXME: This is small hack. Given
540540
// a = b + 4
541541
// .long a
@@ -548,8 +548,6 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
548548
// Allows aliases with zero offset.
549549
if (Res.getConstant() == 0 && (!A || !B))
550550
return true;
551-
} else {
552-
return false;
553551
}
554552
}
555553

0 commit comments

Comments
 (0)