xref: /llvm-project/bolt/lib/Passes/ADRRelaxationPass.cpp (revision 0e86e5214c7ba4fbf99b632b080db82716fd9db0)
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 
18 using namespace llvm;
19 
20 namespace opts {
21 extern cl::OptionCategory BoltCategory;
22 
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
28 
29 namespace llvm {
30 namespace 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.
36 static bool PassFailed = false;
37 
38 void 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(Symbol) || Islands.ProxySymbols.count(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(Symbol))
70           if (BB.getFragmentNum() == TargetBB->getFragmentNum())
71             continue;
72       }
73 
74       MCPhysReg Reg;
75       BC.MIB->getADRReg(Inst, Reg);
76       int64_t Addend = BC.MIB->getTargetAddend(Inst);
77       InstructionListType Addr;
78 
79       {
80         auto L = BC.scopeLock();
81         Addr = BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
82       }
83 
84       if (It != BB.begin() && BC.MIB->isNoop(*std::prev(It))) {
85         It = BB.eraseInstruction(std::prev(It));
86       } else if (std::next(It) != BB.end() && BC.MIB->isNoop(*std::next(It))) {
87         BB.eraseInstruction(std::next(It));
88       } else if (!opts::StrictMode && !BF.isSimple()) {
89         // If the function is not simple, it may contain a jump table undetected
90         // by us. This jump table may use an offset from the branch instruction
91         // to land in the desired place. If we add new instructions, we
92         // invalidate this offset, so we have to rely on linker-inserted NOP to
93         // replace it with ADRP, and abort if it is not present.
94         auto L = BC.scopeLock();
95         BC.errs() << formatv(
96             "BOLT-ERROR: Cannot relax adr in non-simple function "
97             "{0}. Use --strict option to override\n",
98             BF.getOneName());
99         PassFailed = true;
100         return;
101       }
102       It = BB.replaceInstruction(It, Addr);
103     }
104   }
105 }
106 
107 Error ADRRelaxationPass::runOnFunctions(BinaryContext &BC) {
108   if (!opts::AdrPassOpt || !BC.HasRelocations)
109     return Error::success();
110 
111   ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
112     runOnFunction(BF);
113   };
114 
115   ParallelUtilities::runOnEachFunction(
116       BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
117       "ADRRelaxationPass");
118 
119   if (PassFailed)
120     return createFatalBOLTError("");
121   return Error::success();
122 }
123 
124 } // end namespace bolt
125 } // end namespace llvm
126