Reland: [Workflow] Add new code format helper.
[llvm-project.git] / bolt / lib / Passes / ADRRelaxationPass.cpp
blob76924d96fcf9bc5795cef91326a8b904f5a8545f
1 //===- bolt/Passes/ADRRelaxationPass.cpp ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the ADRRelaxationPass class.
11 //===----------------------------------------------------------------------===//
13 #include "bolt/Passes/ADRRelaxationPass.h"
14 #include "bolt/Core/ParallelUtilities.h"
15 #include "bolt/Utils/CommandLineOpts.h"
16 #include <iterator>
18 using namespace llvm;
20 namespace opts {
21 extern cl::OptionCategory BoltCategory;
23 static cl::opt<bool>
24 AdrPassOpt("adr-relaxation",
25 cl::desc("Replace ARM non-local ADR instructions with ADRP"),
26 cl::init(true), cl::cat(BoltCategory), cl::ReallyHidden);
27 } // namespace opts
29 namespace llvm {
30 namespace bolt {
32 void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
33 BinaryContext &BC = BF.getBinaryContext();
34 for (BinaryBasicBlock &BB : BF) {
35 for (auto It = BB.begin(); It != BB.end(); ++It) {
36 MCInst &Inst = *It;
37 if (!BC.MIB->isADR(Inst))
38 continue;
40 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst);
41 if (!Symbol)
42 continue;
44 if (BF.hasIslandsInfo()) {
45 BinaryFunction::IslandInfo &Islands = BF.getIslandInfo();
46 if (Islands.Symbols.count(Symbol) || Islands.ProxySymbols.count(Symbol))
47 continue;
50 BinaryFunction *TargetBF = BC.getFunctionForSymbol(Symbol);
51 if (TargetBF && TargetBF == &BF)
52 continue;
54 MCPhysReg Reg;
55 BC.MIB->getADRReg(Inst, Reg);
56 int64_t Addend = BC.MIB->getTargetAddend(Inst);
57 InstructionListType Addr =
58 BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
60 if (It != BB.begin() && BC.MIB->isNoop(*std::prev(It))) {
61 It = BB.eraseInstruction(std::prev(It));
62 } else if (opts::StrictMode && !BF.isSimple()) {
63 // If the function is not simple, it may contain a jump table undetected
64 // by us. This jump table may use an offset from the branch instruction
65 // to land in the desired place. If we add new instructions, we
66 // invalidate this offset, so we have to rely on linker-inserted NOP to
67 // replace it with ADRP, and abort if it is not present.
68 errs() << formatv("BOLT-ERROR: Cannot relax adr in non-simple function "
69 "{0}. Can't proceed in current mode.\n",
70 BF.getOneName());
71 exit(1);
73 It = BB.replaceInstruction(It, Addr);
78 void ADRRelaxationPass::runOnFunctions(BinaryContext &BC) {
79 if (!opts::AdrPassOpt || !BC.HasRelocations)
80 return;
82 ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
83 runOnFunction(BF);
86 ParallelUtilities::runOnEachFunction(
87 BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
88 "ADRRelaxationPass", /* ForceSequential */ true);
91 } // end namespace bolt
92 } // end namespace llvm