[BOLT][AArch64] Refactor ADR to ADRP+ADD conversion pass. NFCI (#129399)

In preparation of using the new interface in more places, refactor the
ADR conversion pass.
This commit is contained in:
Maksim Panchenko
2025-03-01 14:10:59 -08:00
committed by GitHub
parent 872e4a3388
commit 8910e41c86
3 changed files with 22 additions and 13 deletions

View File

@@ -637,10 +637,6 @@ public:
return false;
}
virtual void getADRReg(const MCInst &Inst, MCPhysReg &RegName) const {
llvm_unreachable("not implemented");
}
virtual bool isMoveMem2Reg(const MCInst &Inst) const { return false; }
virtual bool mayLoad(const MCInst &Inst) const {
@@ -1538,6 +1534,13 @@ public:
llvm_unreachable("not implemented");
}
/// Undo the linker's ADRP+ADD to ADR relaxation. Take \p ADRInst and return
/// ADRP+ADD instruction sequence.
virtual InstructionListType undoAdrpAddRelaxation(const MCInst &ADRInst,
MCContext *Ctx) const {
llvm_unreachable("not implemented");
}
/// Return not 0 if the instruction CurInst, in combination with the recent
/// history of disassembled instructions supplied by [Begin, End), is a linker
/// generated veneer/stub that needs patching. This happens in AArch64 when

View File

@@ -71,14 +71,10 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
continue;
}
MCPhysReg Reg;
BC.MIB->getADRReg(Inst, Reg);
int64_t Addend = BC.MIB->getTargetAddend(Inst);
InstructionListType Addr;
InstructionListType AdrpAdd;
{
auto L = BC.scopeLock();
Addr = BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
AdrpAdd = BC.MIB->undoAdrpAddRelaxation(Inst, BC.Ctx.get());
}
if (It != BB.begin() && BC.MIB->isNoop(*std::prev(It))) {
@@ -99,7 +95,7 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
PassFailed = true;
return;
}
It = BB.replaceInstruction(It, Addr);
It = BB.replaceInstruction(It, AdrpAdd);
}
}
}

View File

@@ -278,13 +278,23 @@ public:
return Inst.getOpcode() == AArch64::ADDXri;
}
void getADRReg(const MCInst &Inst, MCPhysReg &RegName) const override {
MCPhysReg getADRReg(const MCInst &Inst) const {
assert((isADR(Inst) || isADRP(Inst)) && "Not an ADR instruction");
assert(MCPlus::getNumPrimeOperands(Inst) != 0 &&
"No operands for ADR instruction");
assert(Inst.getOperand(0).isReg() &&
"Unexpected operand in ADR instruction");
RegName = Inst.getOperand(0).getReg();
return Inst.getOperand(0).getReg();
}
InstructionListType undoAdrpAddRelaxation(const MCInst &ADRInst,
MCContext *Ctx) const override {
assert(isADR(ADRInst) && "ADR instruction expected");
const MCPhysReg Reg = getADRReg(ADRInst);
const MCSymbol *Target = getTargetSymbol(ADRInst);
const uint64_t Addend = getTargetAddend(ADRInst);
return materializeAddress(Target, Ctx, Reg, Addend);
}
bool isTB(const MCInst &Inst) const {