Skip to content

Commit 9fee2ac

Browse files
Kepontryllongint
authored andcommitted
[BOLT][NFC] Split createRelocation in X86 and share the second part
This commit splits the createRelocation function for the X86 architecture into two parts, retaining the first half and moving the second half to a new function called extractFixupExpr. The purpose of this change is to make extractFixupExpr a shared function between AArch64 and X86 architectures, increasing code reusability and maintainability. Child revision: https://reviews.llvm.org/D156018 Reviewed By: Amir Differential Revision: https://reviews.llvm.org/D157217
1 parent a4dbdf4 commit 9fee2ac

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,48 @@ class MCPlusBuilder {
17371737
return true;
17381738
}
17391739

1740+
/// Extract a symbol and an addend out of the fixup value expression.
1741+
///
1742+
/// Only the following limited expression types are supported:
1743+
/// Symbol + Addend
1744+
/// Symbol + Constant + Addend
1745+
/// Const + Addend
1746+
/// Symbol
1747+
std::pair<MCSymbol *, uint64_t> extractFixupExpr(const MCFixup &Fixup) const {
1748+
uint64_t Addend = 0;
1749+
MCSymbol *Symbol = nullptr;
1750+
const MCExpr *ValueExpr = Fixup.getValue();
1751+
if (ValueExpr->getKind() == MCExpr::Binary) {
1752+
const auto *BinaryExpr = cast<MCBinaryExpr>(ValueExpr);
1753+
assert(BinaryExpr->getOpcode() == MCBinaryExpr::Add &&
1754+
"unexpected binary expression");
1755+
const MCExpr *LHS = BinaryExpr->getLHS();
1756+
if (LHS->getKind() == MCExpr::Constant) {
1757+
Addend = cast<MCConstantExpr>(LHS)->getValue();
1758+
} else if (LHS->getKind() == MCExpr::Binary) {
1759+
const auto *LHSBinaryExpr = cast<MCBinaryExpr>(LHS);
1760+
assert(LHSBinaryExpr->getOpcode() == MCBinaryExpr::Add &&
1761+
"unexpected binary expression");
1762+
const MCExpr *LLHS = LHSBinaryExpr->getLHS();
1763+
assert(LLHS->getKind() == MCExpr::SymbolRef && "unexpected LLHS");
1764+
Symbol = const_cast<MCSymbol *>(this->getTargetSymbol(LLHS));
1765+
const MCExpr *RLHS = LHSBinaryExpr->getRHS();
1766+
assert(RLHS->getKind() == MCExpr::Constant && "unexpected RLHS");
1767+
Addend = cast<MCConstantExpr>(RLHS)->getValue();
1768+
} else {
1769+
assert(LHS->getKind() == MCExpr::SymbolRef && "unexpected LHS");
1770+
Symbol = const_cast<MCSymbol *>(this->getTargetSymbol(LHS));
1771+
}
1772+
const MCExpr *RHS = BinaryExpr->getRHS();
1773+
assert(RHS->getKind() == MCExpr::Constant && "unexpected RHS");
1774+
Addend += cast<MCConstantExpr>(RHS)->getValue();
1775+
} else {
1776+
assert(ValueExpr->getKind() == MCExpr::SymbolRef && "unexpected value");
1777+
Symbol = const_cast<MCSymbol *>(this->getTargetSymbol(ValueExpr));
1778+
}
1779+
return std::make_pair(Symbol, Addend);
1780+
}
1781+
17401782
/// Return annotation index matching the \p Name.
17411783
std::optional<unsigned> getAnnotationIndex(StringRef Name) const {
17421784
auto AI = AnnotationNameIndexMap.find(Name);

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,46 +2458,9 @@ class X86MCPlusBuilder : public MCPlusBuilder {
24582458
}
24592459
}
24602460

2461-
// Extract a symbol and an addend out of the fixup value expression.
2462-
//
2463-
// Only the following limited expression types are supported:
2464-
// Symbol + Addend
2465-
// Symbol + Constant + Addend
2466-
// Const + Addend
2467-
// Symbol
2468-
uint64_t Addend = 0;
2469-
MCSymbol *Symbol = nullptr;
2470-
const MCExpr *ValueExpr = Fixup.getValue();
2471-
if (ValueExpr->getKind() == MCExpr::Binary) {
2472-
const auto *BinaryExpr = cast<MCBinaryExpr>(ValueExpr);
2473-
assert(BinaryExpr->getOpcode() == MCBinaryExpr::Add &&
2474-
"unexpected binary expression");
2475-
const MCExpr *LHS = BinaryExpr->getLHS();
2476-
if (LHS->getKind() == MCExpr::Constant) {
2477-
Addend = cast<MCConstantExpr>(LHS)->getValue();
2478-
} else if (LHS->getKind() == MCExpr::Binary) {
2479-
const auto *LHSBinaryExpr = cast<MCBinaryExpr>(LHS);
2480-
assert(LHSBinaryExpr->getOpcode() == MCBinaryExpr::Add &&
2481-
"unexpected binary expression");
2482-
const MCExpr *LLHS = LHSBinaryExpr->getLHS();
2483-
assert(LLHS->getKind() == MCExpr::SymbolRef && "unexpected LLHS");
2484-
Symbol = const_cast<MCSymbol *>(this->getTargetSymbol(LLHS));
2485-
const MCExpr *RLHS = LHSBinaryExpr->getRHS();
2486-
assert(RLHS->getKind() == MCExpr::Constant && "unexpected RLHS");
2487-
Addend = cast<MCConstantExpr>(RLHS)->getValue();
2488-
} else {
2489-
assert(LHS->getKind() == MCExpr::SymbolRef && "unexpected LHS");
2490-
Symbol = const_cast<MCSymbol *>(this->getTargetSymbol(LHS));
2491-
}
2492-
const MCExpr *RHS = BinaryExpr->getRHS();
2493-
assert(RHS->getKind() == MCExpr::Constant && "unexpected RHS");
2494-
Addend += cast<MCConstantExpr>(RHS)->getValue();
2495-
} else {
2496-
assert(ValueExpr->getKind() == MCExpr::SymbolRef && "unexpected value");
2497-
Symbol = const_cast<MCSymbol *>(this->getTargetSymbol(ValueExpr));
2498-
}
2461+
auto [RelSymbol, RelAddend] = extractFixupExpr(Fixup);
24992462

2500-
return Relocation({RelOffset, Symbol, RelType, Addend, 0});
2463+
return Relocation({RelOffset, RelSymbol, RelType, RelAddend, 0});
25012464
}
25022465

25032466
bool replaceImmWithSymbolRef(MCInst &Inst, const MCSymbol *Symbol,

0 commit comments

Comments
 (0)