2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/Passes/ADRRelaxationPass.cpp ----------------------------------===//
|
2021-08-20 08:07:01 +08:00
|
|
|
//
|
2021-12-24 02:49:37 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2021-08-20 08:07:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2021-12-22 02:21:41 +08:00
|
|
|
// This file implements the ADRRelaxationPass class.
|
|
|
|
//
|
2021-08-20 08:07:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-10-09 02:47:10 +08:00
|
|
|
#include "bolt/Passes/ADRRelaxationPass.h"
|
|
|
|
#include "bolt/Core/ParallelUtilities.h"
|
2021-08-20 08:07:01 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace opts {
|
|
|
|
extern cl::OptionCategory BoltCategory;
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
AdrPassOpt("adr-relaxation",
|
|
|
|
cl::desc("Replace ARM non-local ADR instructions with ADRP"),
|
|
|
|
cl::init(true), cl::cat(BoltCategory), cl::ReallyHidden);
|
|
|
|
} // namespace opts
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace bolt {
|
|
|
|
|
2021-10-26 15:06:34 +08:00
|
|
|
void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
|
|
|
|
BinaryContext &BC = BF.getBinaryContext();
|
2021-08-20 08:07:01 +08:00
|
|
|
for (BinaryBasicBlock *BB : BF.layout()) {
|
|
|
|
for (auto It = BB->begin(); It != BB->end(); ++It) {
|
|
|
|
MCInst &Inst = *It;
|
|
|
|
if (!BC.MIB->isADR(Inst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst);
|
|
|
|
if (!Symbol)
|
|
|
|
continue;
|
2021-10-09 02:31:45 +08:00
|
|
|
|
2021-10-16 19:44:29 +08:00
|
|
|
if (BF.hasIslandsInfo()) {
|
|
|
|
BinaryFunction::IslandInfo &Islands = BF.getIslandInfo();
|
|
|
|
if (Islands.Symbols.count(Symbol) || Islands.ProxySymbols.count(Symbol))
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-20 08:07:01 +08:00
|
|
|
|
|
|
|
BinaryFunction *TargetBF = BC.getFunctionForSymbol(Symbol);
|
|
|
|
if (TargetBF && TargetBF == &BF)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MCPhysReg Reg;
|
|
|
|
BC.MIB->getADRReg(Inst, Reg);
|
|
|
|
int64_t Addend = BC.MIB->getTargetAddend(Inst);
|
2021-12-10 03:53:12 +08:00
|
|
|
InstructionListType Addr =
|
2021-08-20 08:07:01 +08:00
|
|
|
BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
|
|
|
|
It = BB->replaceInstruction(It, Addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ADRRelaxationPass::runOnFunctions(BinaryContext &BC) {
|
|
|
|
if (!opts::AdrPassOpt || !BC.HasRelocations)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
|
2021-10-26 15:06:34 +08:00
|
|
|
runOnFunction(BF);
|
2021-08-20 08:07:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ParallelUtilities::runOnEachFunction(
|
|
|
|
BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
|
|
|
|
"ADRRelaxationPass", /* ForceSequential */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace bolt
|
|
|
|
} // end namespace llvm
|