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.
10//
11//===----------------------------------------------------------------------===//
12
13#include "bolt/Passes/ADRRelaxationPass.h"
14#include "bolt/Core/ParallelUtilities.h"
15#include "bolt/Utils/CommandLineOpts.h"
16#include <iterator>
17
18using namespace llvm;
19
20namespace opts {
21extern cl::OptionCategory BoltCategory;
22
23static cl::opt<bool>
24 AdrPassOpt("adr-relaxation",
25 cl::desc("Replace ARM non-local ADR instructions with ADRP"),
26 cl::init(Val: true), cl::cat(BoltCategory), cl::ReallyHidden);
27} // namespace opts
28
29namespace llvm {
30namespace bolt {
31
32// We don't exit directly from runOnFunction since it would call ThreadPool
33// destructor which might result in internal assert if we're not finished
34// creating async jobs on the moment of exit. So we're finishing all parallel
35// jobs and checking the exit flag after it.
36static bool PassFailed = false;
37
38void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
39 if (PassFailed)
40 return;
41
42 BinaryContext &BC = BF.getBinaryContext();
43 for (BinaryBasicBlock &BB : BF) {
44 for (auto It = BB.begin(); It != BB.end(); ++It) {
45 MCInst &Inst = *It;
46 if (!BC.MIB->isADR(Inst))
47 continue;
48
49 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst);
50 if (!Symbol)
51 continue;
52
53 if (BF.hasIslandsInfo()) {
54 BinaryFunction::IslandInfo &Islands = BF.getIslandInfo();
55 if (Islands.Symbols.count(Ptr: Symbol) || Islands.ProxySymbols.count(Val: Symbol))
56 continue;
57 }
58
59 // Don't relax ADR if it points to the same function and is in the main
60 // fragment and BF initial size is < 1MB.
61 const unsigned OneMB = 0x100000;
62 if (BF.getSize() < OneMB) {
63 BinaryFunction *TargetBF = BC.getFunctionForSymbol(Symbol);
64 if (TargetBF == &BF && !BB.isSplit())
65 continue;
66
67 // No relaxation needed if ADR references a basic block in the same
68 // fragment.
69 if (BinaryBasicBlock *TargetBB = BF.getBasicBlockForLabel(Label: Symbol))
70 if (BB.getFragmentNum() == TargetBB->getFragmentNum())
71 continue;
72 }
73
74 InstructionListType AdrpAdd;
75 {
76 auto L = BC.scopeLock();
77 AdrpAdd = BC.MIB->undoAdrpAddRelaxation(ADRInst: Inst, Ctx: BC.Ctx.get());
78 }
79
80 if (It != BB.begin() && BC.MIB->isNoop(Inst: *std::prev(x: It))) {
81 It = BB.eraseInstruction(II: std::prev(x: It));
82 } else if (std::next(x: It) != BB.end() && BC.MIB->isNoop(Inst: *std::next(x: It))) {
83 BB.eraseInstruction(II: std::next(x: It));
84 } else if (!BF.isSimple()) {
85 // If the function is not simple, it may contain a jump table undetected
86 // by us. This jump table may use an offset from the branch instruction
87 // to land in the desired place. If we add new instructions, we
88 // invalidate this offset, so we have to rely on linker-inserted NOP to
89 // replace it with ADRP, and abort if it is not present.
90 auto L = BC.scopeLock();
91 BC.errs() << "BOLT-ERROR: cannot relax ADR in non-simple function "
92 << BF << '\n';
93 PassFailed = true;
94 return;
95 }
96 It = BB.replaceInstruction(II: It, Replacement: AdrpAdd);
97 }
98 }
99}
100
101Error ADRRelaxationPass::runOnFunctions(BinaryContext &BC) {
102 if (!opts::AdrPassOpt || !BC.HasRelocations)
103 return Error::success();
104
105 ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
106 runOnFunction(BF);
107 };
108
109 ParallelUtilities::runOnEachFunction(
110 BC, SchedPolicy: ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFunction: WorkFun, SkipPredicate: nullptr,
111 LogName: "ADRRelaxationPass");
112
113 if (PassFailed)
114 return createFatalBOLTError(S: "");
115 return Error::success();
116}
117
118} // end namespace bolt
119} // end namespace llvm
120

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of bolt/lib/Passes/ADRRelaxationPass.cpp