Skip to content

Commit 8910e41

Browse files
authored
[BOLT][AArch64] Refactor ADR to ADRP+ADD conversion pass. NFCI (llvm#129399)
In preparation of using the new interface in more places, refactor the ADR conversion pass.
1 parent 872e4a3 commit 8910e41

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,6 @@ class MCPlusBuilder {
637637
return false;
638638
}
639639

640-
virtual void getADRReg(const MCInst &Inst, MCPhysReg &RegName) const {
641-
llvm_unreachable("not implemented");
642-
}
643-
644640
virtual bool isMoveMem2Reg(const MCInst &Inst) const { return false; }
645641

646642
virtual bool mayLoad(const MCInst &Inst) const {
@@ -1538,6 +1534,13 @@ class MCPlusBuilder {
15381534
llvm_unreachable("not implemented");
15391535
}
15401536

1537+
/// Undo the linker's ADRP+ADD to ADR relaxation. Take \p ADRInst and return
1538+
/// ADRP+ADD instruction sequence.
1539+
virtual InstructionListType undoAdrpAddRelaxation(const MCInst &ADRInst,
1540+
MCContext *Ctx) const {
1541+
llvm_unreachable("not implemented");
1542+
}
1543+
15411544
/// Return not 0 if the instruction CurInst, in combination with the recent
15421545
/// history of disassembled instructions supplied by [Begin, End), is a linker
15431546
/// generated veneer/stub that needs patching. This happens in AArch64 when

bolt/lib/Passes/ADRRelaxationPass.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
7171
continue;
7272
}
7373

74-
MCPhysReg Reg;
75-
BC.MIB->getADRReg(Inst, Reg);
76-
int64_t Addend = BC.MIB->getTargetAddend(Inst);
77-
InstructionListType Addr;
78-
74+
InstructionListType AdrpAdd;
7975
{
8076
auto L = BC.scopeLock();
81-
Addr = BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
77+
AdrpAdd = BC.MIB->undoAdrpAddRelaxation(Inst, BC.Ctx.get());
8278
}
8379

8480
if (It != BB.begin() && BC.MIB->isNoop(*std::prev(It))) {
@@ -99,7 +95,7 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
9995
PassFailed = true;
10096
return;
10197
}
102-
It = BB.replaceInstruction(It, Addr);
98+
It = BB.replaceInstruction(It, AdrpAdd);
10399
}
104100
}
105101
}

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,23 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
278278
return Inst.getOpcode() == AArch64::ADDXri;
279279
}
280280

281-
void getADRReg(const MCInst &Inst, MCPhysReg &RegName) const override {
281+
MCPhysReg getADRReg(const MCInst &Inst) const {
282282
assert((isADR(Inst) || isADRP(Inst)) && "Not an ADR instruction");
283283
assert(MCPlus::getNumPrimeOperands(Inst) != 0 &&
284284
"No operands for ADR instruction");
285285
assert(Inst.getOperand(0).isReg() &&
286286
"Unexpected operand in ADR instruction");
287-
RegName = Inst.getOperand(0).getReg();
287+
return Inst.getOperand(0).getReg();
288+
}
289+
290+
InstructionListType undoAdrpAddRelaxation(const MCInst &ADRInst,
291+
MCContext *Ctx) const override {
292+
assert(isADR(ADRInst) && "ADR instruction expected");
293+
294+
const MCPhysReg Reg = getADRReg(ADRInst);
295+
const MCSymbol *Target = getTargetSymbol(ADRInst);
296+
const uint64_t Addend = getTargetAddend(ADRInst);
297+
return materializeAddress(Target, Ctx, Reg, Addend);
288298
}
289299

290300
bool isTB(const MCInst &Inst) const {

0 commit comments

Comments
 (0)