#include "bolt/Passes/AArch64RelaxationPass.h"
#include "bolt/Core/ParallelUtilities.h"
#include "bolt/Utils/CommandLineOpts.h"
#include <iterator>
using namespace llvm;
namespace opts {
extern cl::OptionCategory BoltCategory;
static cl::opt<bool> AArch64PassOpt(
"aarch64-relaxation",
cl::desc("Replace ARM non-local ADR/LDR instructions with ADRP"),
cl::init(true), cl::cat(BoltCategory), cl::ReallyHidden);
}
namespace llvm {
namespace bolt {
static bool PassFailed = false;
void AArch64RelaxationPass::runOnFunction(BinaryFunction &BF) {
if (PassFailed)
return;
BinaryContext &BC = BF.getBinaryContext();
for (BinaryBasicBlock &BB : BF) {
for (auto It = BB.begin(); It != BB.end(); ++It) {
MCInst &Inst = *It;
bool IsADR = BC.MIB->isADR(Inst);
if (!IsADR && !BC.MIB->isLDRXl(Inst) && !BC.MIB->isLDRWl(Inst))
continue;
const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst, IsADR ? 0 : 1);
if (!Symbol)
continue;
if (BF.hasIslandsInfo()) {
BinaryFunction::IslandInfo &Islands = BF.getIslandInfo();
if (Islands.Symbols.count(Symbol) || Islands.ProxySymbols.count(Symbol))
continue;
}
const unsigned OneMB = 0x100000;
if (BF.getSize() < OneMB) {
BinaryFunction *TargetBF = BC.getFunctionForSymbol(Symbol);
if (TargetBF == &BF && !BB.isSplit())
continue;
if (BinaryBasicBlock *TargetBB = BF.getBasicBlockForLabel(Symbol))
if (BB.getFragmentNum() == TargetBB->getFragmentNum())
continue;
}
InstructionListType AdrpMaterialization;
{
auto L = BC.scopeLock();
AdrpMaterialization =
IsADR ? BC.MIB->undoAdrpAddRelaxation(Inst, BC.Ctx.get())
: BC.MIB->createAdrpLdr(Inst, BC.Ctx.get());
}
if (It != BB.begin() && BC.MIB->isNoop(*std::prev(It))) {
It = BB.eraseInstruction(std::prev(It));
} else if (std::next(It) != BB.end() && BC.MIB->isNoop(*std::next(It))) {
BB.eraseInstruction(std::next(It));
} else if (!BF.isSimple()) {
auto L = BC.scopeLock();
BC.errs() << "BOLT-ERROR: cannot relax " << (IsADR ? "ADR" : "LDR")
<< " in non-simple function " << BF << '\n';
PassFailed = true;
return;
}
It = BB.replaceInstruction(It, AdrpMaterialization);
}
}
}
Error AArch64RelaxationPass::runOnFunctions(BinaryContext &BC) {
if (!opts::AArch64PassOpt || !BC.HasRelocations)
return Error::success();
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
runOnFunction(BF);
};
ParallelUtilities::runOnEachFunction(
BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
"AArch64RelaxationPass");
if (PassFailed)
return createFatalBOLTError("");
return Error::success();
}
}
}