1 //===- bolt/Passes/PLTCall.h - PLT call optimization ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the PLTCall class, which replaces calls to PLT entries
10 // with indirect calls against GOT.
12 //===----------------------------------------------------------------------===//
14 #include "bolt/Passes/PLTCall.h"
15 #include "llvm/Support/CommandLine.h"
17 #define DEBUG_TYPE "bolt-plt"
23 extern cl::OptionCategory BoltOptCategory
;
25 cl::opt
<bolt::PLTCall::OptType
>
27 cl::desc("optimize PLT calls (requires linking with -znow)"),
28 cl::init(bolt::PLTCall::OT_NONE
),
29 cl::values(clEnumValN(bolt::PLTCall::OT_NONE
,
31 "do not optimize PLT calls"),
32 clEnumValN(bolt::PLTCall::OT_HOT
,
34 "optimize executed (hot) PLT calls"),
35 clEnumValN(bolt::PLTCall::OT_ALL
,
37 "optimize all PLT calls")),
39 cl::cat(BoltOptCategory
));
46 void PLTCall::runOnFunctions(BinaryContext
&BC
) {
47 if (opts::PLT
== OT_NONE
)
50 uint64_t NumCallsOptimized
= 0;
51 for (auto &It
: BC
.getBinaryFunctions()) {
52 BinaryFunction
&Function
= It
.second
;
53 if (!shouldOptimize(Function
))
56 if (opts::PLT
== OT_HOT
&&
57 Function
.getExecutionCount() == BinaryFunction::COUNT_NO_PROFILE
)
60 for (BinaryBasicBlock
&BB
: Function
) {
61 if (opts::PLT
== OT_HOT
&& !BB
.getKnownExecutionCount())
64 for (MCInst
&Instr
: BB
) {
65 if (!BC
.MIB
->isCall(Instr
))
67 const MCSymbol
*CallSymbol
= BC
.MIB
->getTargetSymbol(Instr
);
70 const BinaryFunction
*CalleeBF
= BC
.getFunctionForSymbol(CallSymbol
);
71 if (!CalleeBF
|| !CalleeBF
->isPLTFunction())
73 BC
.MIB
->convertCallToIndirectCall(Instr
, CalleeBF
->getPLTSymbol(),
75 BC
.MIB
->addAnnotation(Instr
, "PLTCall", true);
81 if (NumCallsOptimized
) {
82 BC
.RequiresZNow
= true;
83 outs() << "BOLT-INFO: " << NumCallsOptimized
84 << " PLT calls in the binary were optimized.\n";