1 | //===- bolt/Passes/FixRISCVCallsPass.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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "bolt/Passes/FixRISCVCallsPass.h" |
10 | #include "bolt/Core/ParallelUtilities.h" |
11 | |
12 | #include <iterator> |
13 | |
14 | using namespace llvm; |
15 | |
16 | namespace llvm { |
17 | namespace bolt { |
18 | |
19 | void FixRISCVCallsPass::runOnFunction(BinaryFunction &BF) { |
20 | auto &BC = BF.getBinaryContext(); |
21 | auto &MIB = BC.MIB; |
22 | auto *Ctx = BC.Ctx.get(); |
23 | |
24 | for (auto &BB : BF) { |
25 | for (auto II = BB.begin(); II != BB.end();) { |
26 | if (MIB->isCall(Inst: *II) && !MIB->isIndirectCall(Inst: *II)) { |
27 | auto *Target = MIB->getTargetSymbol(Inst: *II); |
28 | assert(Target && "Cannot find call target" ); |
29 | |
30 | MCInst OldCall = *II; |
31 | auto L = BC.scopeLock(); |
32 | |
33 | if (MIB->isTailCall(Inst: *II)) |
34 | MIB->createTailCall(Inst&: *II, Target, Ctx); |
35 | else |
36 | MIB->createCall(Inst&: *II, Target, Ctx); |
37 | |
38 | MIB->moveAnnotations(SrcInst: std::move(OldCall), DstInst&: *II); |
39 | ++II; |
40 | continue; |
41 | } |
42 | |
43 | auto NextII = std::next(x: II); |
44 | |
45 | if (NextII == BB.end()) |
46 | break; |
47 | |
48 | if (MIB->isRISCVCall(First: *II, Second: *NextII)) { |
49 | auto *Target = MIB->getTargetSymbol(Inst: *II); |
50 | assert(Target && "Cannot find call target" ); |
51 | |
52 | MCInst OldCall = *NextII; |
53 | auto L = BC.scopeLock(); |
54 | |
55 | if (MIB->isTailCall(Inst: *NextII)) |
56 | MIB->createTailCall(Inst&: *II, Target, Ctx); |
57 | else |
58 | MIB->createCall(Inst&: *II, Target, Ctx); |
59 | |
60 | MIB->moveAnnotations(SrcInst: std::move(OldCall), DstInst&: *II); |
61 | |
62 | // The original offset was set on the jalr of the auipc+jalr pair. Since |
63 | // the whole pair is replaced by a call, adjust the offset by -4 (the |
64 | // size of a auipc). |
65 | if (std::optional<uint32_t> Offset = MIB->getOffset(Inst: *II)) { |
66 | assert(*Offset >= 4 && "Illegal jalr offset" ); |
67 | MIB->setOffset(Inst&: *II, Offset: *Offset - 4); |
68 | } |
69 | |
70 | II = BB.eraseInstruction(II: NextII); |
71 | continue; |
72 | } |
73 | |
74 | ++II; |
75 | } |
76 | } |
77 | } |
78 | |
79 | Error FixRISCVCallsPass::runOnFunctions(BinaryContext &BC) { |
80 | if (!BC.isRISCV() || !BC.HasRelocations) |
81 | return Error::success(); |
82 | |
83 | ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) { |
84 | runOnFunction(BF); |
85 | }; |
86 | |
87 | ParallelUtilities::runOnEachFunction( |
88 | BC, SchedPolicy: ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFunction: WorkFun, SkipPredicate: nullptr, |
89 | LogName: "FixRISCVCalls" ); |
90 | |
91 | return Error::success(); |
92 | } |
93 | |
94 | } // namespace bolt |
95 | } // namespace llvm |
96 | |