[CodeGenPrepare] Drop nsw flags in `optimizeLoadExt` (#118180)
[llvm-project.git] / bolt / lib / Passes / FixRelaxationPass.cpp
blob7c970e464a94e361626dac979546e0564f25a012
1 //===- bolt/Passes/FixRelaxationPass.cpp ------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #include "bolt/Passes/FixRelaxationPass.h"
10 #include "bolt/Core/ParallelUtilities.h"
12 using namespace llvm;
14 namespace llvm {
15 namespace bolt {
17 // This function finds ADRP+ADD instruction sequences that originally before
18 // linker relaxations were ADRP+LDR. We've modified LDR/ADD relocation properly
19 // during relocation reading, so its targeting right symbol. As for ADRP its
20 // target is wrong before this pass since we won't be able to recognize and
21 // properly change R_AARCH64_ADR_GOT_PAGE relocation to
22 // R_AARCH64_ADR_PREL_PG_HI21 during relocation reading. Now we're searching for
23 // ADRP+ADD sequences, checking that ADRP points to the GOT-table symbol and the
24 // target of ADD is another symbol. When found change ADRP symbol reference to
25 // the ADDs one.
26 void FixRelaxations::runOnFunction(BinaryFunction &BF) {
27 BinaryContext &BC = BF.getBinaryContext();
28 for (BinaryBasicBlock &BB : BF) {
29 for (auto II = BB.begin(); II != BB.end(); ++II) {
30 MCInst &Adrp = *II;
31 if (BC.MIB->isPseudo(Adrp) || !BC.MIB->isADRP(Adrp))
32 continue;
34 const MCSymbol *AdrpSymbol = BC.MIB->getTargetSymbol(Adrp);
35 if (!AdrpSymbol || AdrpSymbol->getName() != "__BOLT_got_zero")
36 continue;
38 auto NextII = std::next(II);
39 if (NextII == BB.end())
40 continue;
42 const MCInst &Add = *NextII;
43 if (!BC.MIB->matchAdrpAddPair(Adrp, Add))
44 continue;
46 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Add);
47 if (!Symbol || AdrpSymbol == Symbol)
48 continue;
50 auto L = BC.scopeLock();
51 const int64_t Addend = BC.MIB->getTargetAddend(Add);
52 BC.MIB->setOperandToSymbolRef(Adrp, /*OpNum*/ 1, Symbol, Addend,
53 BC.Ctx.get(), ELF::R_AARCH64_NONE);
58 Error FixRelaxations::runOnFunctions(BinaryContext &BC) {
59 if (!BC.isAArch64() || !BC.HasRelocations)
60 return Error::success();
62 ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
63 runOnFunction(BF);
66 ParallelUtilities::runOnEachFunction(
67 BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, nullptr,
68 "FixRelaxations");
69 return Error::success();
72 } // namespace bolt
73 } // namespace llvm