2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/Passes/Inliner.cpp - Inlining pass for low-level binary IR ----===//
|
2017-02-17 06:57:57 +08:00
|
|
|
//
|
2021-03-16 09:04:18 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-17 06:57:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2021-12-22 02:21:41 +08:00
|
|
|
// This file implements the Inliner class used for inlining binary functions.
|
|
|
|
//
|
2019-02-01 03:23:02 +08:00
|
|
|
// The current inliner has a limited callee support
|
|
|
|
// (see Inliner::getInliningInfo() for the most up-to-date details):
|
|
|
|
//
|
|
|
|
// * No exception handling
|
|
|
|
// * No jump tables
|
|
|
|
// * Single entry point
|
|
|
|
// * CFI update not supported - breaks unwinding
|
|
|
|
// * Regular Call Sites:
|
|
|
|
// - only leaf functions (or callees with only tail calls)
|
|
|
|
// * no invokes (they can't be tail calls)
|
|
|
|
// - no direct use of %rsp
|
|
|
|
// * Tail Call Sites:
|
|
|
|
// - since the stack is unmodified, the regular call limitations are lifted
|
|
|
|
//
|
2017-02-17 06:57:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-10-09 02:47:10 +08:00
|
|
|
#include "bolt/Passes/Inliner.h"
|
|
|
|
#include "bolt/Core/MCPlus.h"
|
2020-12-02 08:29:39 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-02-01 03:23:02 +08:00
|
|
|
#include <map>
|
2017-02-17 06:57:57 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "bolt-inliner"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace opts {
|
2017-03-29 05:40:20 +08:00
|
|
|
|
|
|
|
extern cl::OptionCategory BoltOptCategory;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2019-02-01 03:23:02 +08:00
|
|
|
AdjustProfile("inline-ap",
|
|
|
|
cl::desc("adjust function profile after inlining"),
|
2017-03-29 05:40:20 +08:00
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
ForceInlineFunctions("force-inline",
|
|
|
|
cl::CommaSeparated,
|
|
|
|
cl::desc("list of functions to always consider for inlining"),
|
|
|
|
cl::value_desc("func1,func2,func3,..."),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::cat(BoltOptCategory));
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
InlineAll("inline-all",
|
|
|
|
cl::desc("inline all functions"),
|
|
|
|
cl::init(false),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
InlineIgnoreLeafCFI("inline-ignore-leaf-cfi",
|
|
|
|
cl::desc("inline leaf functions with CFI programs (can break unwinding)"),
|
|
|
|
cl::init(true),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::ReallyHidden,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
InlineIgnoreCFI("inline-ignore-cfi",
|
|
|
|
cl::desc("inline functions with CFI programs (can break exception handling)"),
|
|
|
|
cl::init(false),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::ReallyHidden,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
InlineLimit("inline-limit",
|
|
|
|
cl::desc("maximum number of call sites to inline"),
|
|
|
|
cl::init(0),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::Hidden,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
InlineMaxIters("inline-max-iters",
|
|
|
|
cl::desc("maximum number of inline iterations"),
|
|
|
|
cl::init(3),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::Hidden,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
InlineSmallFunctions("inline-small-functions",
|
|
|
|
cl::desc("inline functions if increase in size is less than defined by "
|
|
|
|
"-inline-small-functions-bytes"),
|
|
|
|
cl::init(false),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
InlineSmallFunctionsBytes("inline-small-functions-bytes",
|
|
|
|
cl::desc("max number of bytes for the function to be considered small for "
|
|
|
|
"inlining purposes"),
|
|
|
|
cl::init(4),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::Hidden,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
NoInline("no-inline",
|
|
|
|
cl::desc("disable all inlining (overrides other inlining options)"),
|
|
|
|
cl::init(false),
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
|
|
|
/// This function returns true if any of inlining options are specified and the
|
|
|
|
/// inlining pass should be executed. Whenever a new inlining option is added,
|
|
|
|
/// this function should reflect the change.
|
|
|
|
bool inliningEnabled() {
|
|
|
|
return !NoInline &&
|
2021-12-15 08:52:51 +08:00
|
|
|
(InlineAll || InlineSmallFunctions || !ForceInlineFunctions.empty());
|
2019-02-01 03:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mustConsider(const llvm::bolt::BinaryFunction &Function) {
|
2021-12-29 08:36:17 +08:00
|
|
|
for (std::string &Name : opts::ForceInlineFunctions)
|
2019-02-01 03:23:02 +08:00
|
|
|
if (Function.hasName(Name))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncOptions() {
|
|
|
|
if (opts::InlineIgnoreCFI)
|
|
|
|
opts::InlineIgnoreLeafCFI = true;
|
|
|
|
|
|
|
|
if (opts::InlineAll)
|
|
|
|
opts::InlineSmallFunctions = true;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
} // namespace opts
|
|
|
|
|
2017-02-17 06:57:57 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace bolt {
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
uint64_t Inliner::SizeOfCallInst;
|
|
|
|
uint64_t Inliner::SizeOfTailCallInst;
|
|
|
|
|
|
|
|
uint64_t Inliner::getSizeOfCallInst(const BinaryContext &BC) {
|
|
|
|
if (SizeOfCallInst)
|
|
|
|
return SizeOfCallInst;
|
|
|
|
|
|
|
|
MCInst Inst;
|
2020-12-02 08:29:39 +08:00
|
|
|
BC.MIB->createCall(Inst, BC.Ctx->createNamedTempSymbol(), BC.Ctx.get());
|
2019-02-01 03:23:02 +08:00
|
|
|
SizeOfCallInst = BC.computeInstructionSize(Inst);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
return SizeOfCallInst;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
uint64_t Inliner::getSizeOfTailCallInst(const BinaryContext &BC) {
|
|
|
|
if (SizeOfTailCallInst)
|
|
|
|
return SizeOfTailCallInst;
|
|
|
|
|
|
|
|
MCInst Inst;
|
2020-12-02 08:29:39 +08:00
|
|
|
BC.MIB->createTailCall(Inst, BC.Ctx->createNamedTempSymbol(), BC.Ctx.get());
|
2019-02-01 03:23:02 +08:00
|
|
|
SizeOfTailCallInst = BC.computeInstructionSize(Inst);
|
|
|
|
|
|
|
|
return SizeOfTailCallInst;
|
|
|
|
}
|
|
|
|
|
|
|
|
Inliner::InliningInfo Inliner::getInliningInfo(const BinaryFunction &BF) const {
|
|
|
|
if (!shouldOptimize(BF))
|
|
|
|
return INL_NONE;
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const BinaryContext &BC = BF.getBinaryContext();
|
2019-02-01 03:23:02 +08:00
|
|
|
bool DirectSP = false;
|
|
|
|
bool HasCFI = false;
|
|
|
|
bool IsLeaf = true;
|
|
|
|
|
|
|
|
// Perform necessary checks unless the option overrides it.
|
|
|
|
if (!opts::mustConsider(BF)) {
|
2019-05-17 03:46:32 +08:00
|
|
|
if (BF.hasSDTMarker())
|
|
|
|
return INL_NONE;
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
if (BF.hasEHRanges())
|
|
|
|
return INL_NONE;
|
|
|
|
|
|
|
|
if (BF.isMultiEntry())
|
|
|
|
return INL_NONE;
|
|
|
|
|
|
|
|
if (BF.hasJumpTables())
|
|
|
|
return INL_NONE;
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const MCPhysReg SPReg = BC.MIB->getStackPointer();
|
|
|
|
for (const BinaryBasicBlock *BB : BF.layout()) {
|
|
|
|
for (const MCInst &Inst : *BB) {
|
2019-02-01 03:23:02 +08:00
|
|
|
// Tail calls are marked as implicitly using the stack pointer and they
|
|
|
|
// could be inlined.
|
|
|
|
if (BC.MIB->isTailCall(Inst))
|
2017-02-17 06:57:57 +08:00
|
|
|
break;
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
if (BC.MIB->isCFI(Inst)) {
|
|
|
|
HasCFI = true;
|
|
|
|
continue;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
if (BC.MIB->isCall(Inst))
|
|
|
|
IsLeaf = false;
|
|
|
|
|
|
|
|
// Push/pop instructions are straightforward to handle.
|
|
|
|
if (BC.MIB->isPush(Inst) || BC.MIB->isPop(Inst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DirectSP |= BC.MIB->hasDefOfPhysReg(Inst, SPReg) ||
|
|
|
|
BC.MIB->hasUseOfPhysReg(Inst, SPReg);
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
if (HasCFI) {
|
|
|
|
if (!opts::InlineIgnoreLeafCFI)
|
|
|
|
return INL_NONE;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
if (!IsLeaf && !opts::InlineIgnoreCFI)
|
|
|
|
return INL_NONE;
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
InliningInfo Info(DirectSP ? INL_TAILCALL : INL_ANY);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
size_t Size = BF.estimateSize();
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
Info.SizeAfterInlining = Size;
|
|
|
|
Info.SizeAfterTailCallInlining = Size;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Handle special case of the known size reduction.
|
|
|
|
if (BF.size() == 1) {
|
|
|
|
// For a regular call the last return instruction could be removed
|
|
|
|
// (or converted to a branch).
|
2021-04-08 15:19:26 +08:00
|
|
|
const MCInst *LastInst = BF.back().getLastNonPseudoInstr();
|
2021-12-15 08:52:51 +08:00
|
|
|
if (LastInst && BC.MIB->isReturn(*LastInst) &&
|
2019-02-01 03:23:02 +08:00
|
|
|
!BC.MIB->isTailCall(*LastInst)) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const uint64_t RetInstSize = BC.computeInstructionSize(*LastInst);
|
2019-02-01 03:23:02 +08:00
|
|
|
assert(Size >= RetInstSize);
|
|
|
|
Info.SizeAfterInlining -= RetInstSize;
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
return Info;
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
void Inliner::findInliningCandidates(BinaryContext &BC) {
|
2019-04-04 06:52:01 +08:00
|
|
|
for (const auto &BFI : BC.getBinaryFunctions()) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const BinaryFunction &Function = BFI.second;
|
|
|
|
const InliningInfo InlInfo = getInliningInfo(Function);
|
2019-02-01 03:23:02 +08:00
|
|
|
if (InlInfo.Type != INL_NONE)
|
|
|
|
InliningCandidates[&Function] = InlInfo;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
std::pair<BinaryBasicBlock *, BinaryBasicBlock::iterator>
|
|
|
|
Inliner::inlineCall(BinaryBasicBlock &CallerBB,
|
|
|
|
BinaryBasicBlock::iterator CallInst,
|
|
|
|
const BinaryFunction &Callee) {
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryFunction &CallerFunction = *CallerBB.getFunction();
|
|
|
|
BinaryContext &BC = CallerFunction.getBinaryContext();
|
2019-02-01 03:23:02 +08:00
|
|
|
auto &MIB = *BC.MIB;
|
|
|
|
|
|
|
|
assert(MIB.isCall(*CallInst) && "can only inline a call or a tail call");
|
|
|
|
assert(!Callee.isMultiEntry() &&
|
|
|
|
"cannot inline function with multiple entries");
|
|
|
|
assert(!Callee.hasJumpTables() &&
|
|
|
|
"cannot inline function with jump table(s)");
|
|
|
|
|
|
|
|
// Get information about the call site.
|
2021-04-08 15:19:26 +08:00
|
|
|
const bool CSIsInvoke = BC.MIB->isInvoke(*CallInst);
|
|
|
|
const bool CSIsTailCall = BC.MIB->isTailCall(*CallInst);
|
|
|
|
const int64_t CSGNUArgsSize = BC.MIB->getGnuArgsSize(*CallInst);
|
|
|
|
const Optional<MCPlus::MCLandingPad> CSEHInfo = BC.MIB->getEHInfo(*CallInst);
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
// Split basic block at the call site if there will be more incoming edges
|
|
|
|
// coming from the callee.
|
|
|
|
BinaryBasicBlock *FirstInlinedBB = &CallerBB;
|
|
|
|
if (Callee.front().pred_size() && CallInst != CallerBB.begin()) {
|
|
|
|
FirstInlinedBB = CallerBB.splitAt(CallInst);
|
|
|
|
CallInst = FirstInlinedBB->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split basic block after the call instruction unless the callee is trivial
|
|
|
|
// (i.e. consists of a single basic block). If necessary, obtain a basic block
|
|
|
|
// for return instructions in the callee to redirect to.
|
|
|
|
BinaryBasicBlock *NextBB = nullptr;
|
|
|
|
if (Callee.size() > 1) {
|
2021-12-29 08:36:17 +08:00
|
|
|
if (std::next(CallInst) != FirstInlinedBB->end())
|
2019-02-01 03:23:02 +08:00
|
|
|
NextBB = FirstInlinedBB->splitAt(std::next(CallInst));
|
2021-12-29 08:36:17 +08:00
|
|
|
else
|
2019-02-01 03:23:02 +08:00
|
|
|
NextBB = FirstInlinedBB->getSuccessor();
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
if (NextBB)
|
|
|
|
FirstInlinedBB->removeSuccessor(NextBB);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Remove the call instruction.
|
|
|
|
auto InsertII = FirstInlinedBB->eraseInstruction(CallInst);
|
|
|
|
|
|
|
|
double ProfileRatio = 0;
|
2021-12-29 08:36:17 +08:00
|
|
|
if (uint64_t CalleeExecCount = Callee.getKnownExecutionCount())
|
2019-02-01 03:23:02 +08:00
|
|
|
ProfileRatio =
|
2021-12-15 08:52:51 +08:00
|
|
|
(double)FirstInlinedBB->getKnownExecutionCount() / CalleeExecCount;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Save execution count of the first block as we don't want it to change
|
|
|
|
// later due to profile adjustment rounding errors.
|
2021-04-08 15:19:26 +08:00
|
|
|
const uint64_t FirstInlinedBBCount = FirstInlinedBB->getKnownExecutionCount();
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Copy basic blocks and maintain a map from their origin.
|
2017-02-17 06:57:57 +08:00
|
|
|
std::unordered_map<const BinaryBasicBlock *, BinaryBasicBlock *> InlinedBBMap;
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedBBMap[&Callee.front()] = FirstInlinedBB;
|
|
|
|
for (auto BBI = std::next(Callee.begin()); BBI != Callee.end(); ++BBI) {
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryBasicBlock *InlinedBB = CallerFunction.addBasicBlock(0);
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedBBMap[&*BBI] = InlinedBB;
|
|
|
|
InlinedBB->setCFIState(FirstInlinedBB->getCFIState());
|
2021-12-29 08:36:17 +08:00
|
|
|
if (Callee.hasValidProfile())
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedBB->setExecutionCount(BBI->getKnownExecutionCount());
|
2021-12-29 08:36:17 +08:00
|
|
|
else
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedBB->setExecutionCount(FirstInlinedBBCount);
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Copy over instructions and edges.
|
2021-04-08 15:19:26 +08:00
|
|
|
for (const BinaryBasicBlock &BB : Callee) {
|
|
|
|
BinaryBasicBlock *InlinedBB = InlinedBBMap[&BB];
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
if (InlinedBB != FirstInlinedBB)
|
|
|
|
InsertII = InlinedBB->begin();
|
|
|
|
|
|
|
|
// Copy over instructions making any necessary mods.
|
2021-04-08 15:19:26 +08:00
|
|
|
for (MCInst Inst : BB) {
|
2019-02-01 03:23:02 +08:00
|
|
|
if (MIB.isPseudo(Inst))
|
2017-02-17 06:57:57 +08:00
|
|
|
continue;
|
2019-02-01 03:23:02 +08:00
|
|
|
|
2020-12-02 08:29:39 +08:00
|
|
|
MIB.stripAnnotations(Inst, /*KeepTC=*/BC.isX86());
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
// Fix branch target. Strictly speaking, we don't have to do this as
|
|
|
|
// targets of direct branches will be fixed later and don't matter
|
|
|
|
// in the CFG state. However, disassembly may look misleading, and
|
|
|
|
// hence we do the fixing.
|
|
|
|
if (MIB.isBranch(Inst)) {
|
|
|
|
assert(!MIB.isIndirectBranch(Inst) &&
|
|
|
|
"unexpected indirect branch in callee");
|
2021-04-08 15:19:26 +08:00
|
|
|
const BinaryBasicBlock *TargetBB =
|
2019-02-01 03:23:02 +08:00
|
|
|
Callee.getBasicBlockForLabel(MIB.getTargetSymbol(Inst));
|
|
|
|
assert(TargetBB && "cannot find target block in callee");
|
|
|
|
MIB.replaceBranchTarget(Inst, InlinedBBMap[TargetBB]->getLabel(),
|
|
|
|
BC.Ctx.get());
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
if (CSIsTailCall || (!MIB.isCall(Inst) && !MIB.isReturn(Inst))) {
|
2021-12-15 08:52:51 +08:00
|
|
|
InsertII =
|
|
|
|
std::next(InlinedBB->insertInstruction(InsertII, std::move(Inst)));
|
2019-02-01 03:23:02 +08:00
|
|
|
continue;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
// Handle special instructions for a non-tail call site.
|
|
|
|
if (!MIB.isCall(Inst)) {
|
|
|
|
// Returns are removed.
|
|
|
|
break;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
MIB.convertTailCallToCall(Inst);
|
|
|
|
|
|
|
|
// Propagate EH-related info to call instructions.
|
|
|
|
if (CSIsInvoke) {
|
|
|
|
MIB.addEHInfo(Inst, *CSEHInfo);
|
|
|
|
if (CSGNUArgsSize >= 0)
|
|
|
|
MIB.addGnuArgsSize(Inst, CSGNUArgsSize);
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
InsertII =
|
|
|
|
std::next(InlinedBB->insertInstruction(InsertII, std::move(Inst)));
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add CFG edges to the basic blocks of the inlined instance.
|
2019-02-01 03:23:02 +08:00
|
|
|
std::vector<BinaryBasicBlock *> Successors(BB.succ_size());
|
2021-12-15 08:52:51 +08:00
|
|
|
std::transform(BB.succ_begin(), BB.succ_end(), Successors.begin(),
|
|
|
|
[&InlinedBBMap](const BinaryBasicBlock *BB) {
|
|
|
|
return InlinedBBMap.at(BB);
|
|
|
|
});
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2021-12-29 08:36:17 +08:00
|
|
|
if (CallerFunction.hasValidProfile() && Callee.hasValidProfile())
|
2021-12-15 08:52:51 +08:00
|
|
|
InlinedBB->addSuccessors(Successors.begin(), Successors.end(),
|
|
|
|
BB.branch_info_begin(), BB.branch_info_end());
|
2021-12-29 08:36:17 +08:00
|
|
|
else
|
2021-12-15 08:52:51 +08:00
|
|
|
InlinedBB->addSuccessors(Successors.begin(), Successors.end());
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
if (!CSIsTailCall && BB.succ_size() == 0 && NextBB) {
|
|
|
|
// Either it's a return block or the last instruction never returns.
|
|
|
|
InlinedBB->addSuccessor(NextBB, InlinedBB->getExecutionCount());
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Scale profiling info for blocks and edges after inlining.
|
|
|
|
if (CallerFunction.hasValidProfile() && Callee.size() > 1) {
|
2021-12-29 08:36:17 +08:00
|
|
|
if (opts::AdjustProfile)
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedBB->adjustExecutionCount(ProfileRatio);
|
2021-12-29 08:36:17 +08:00
|
|
|
else
|
2021-12-15 08:52:51 +08:00
|
|
|
InlinedBB->setExecutionCount(InlinedBB->getKnownExecutionCount() *
|
|
|
|
ProfileRatio);
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Restore the original execution count of the first inlined basic block.
|
|
|
|
FirstInlinedBB->setExecutionCount(FirstInlinedBBCount);
|
|
|
|
|
|
|
|
CallerFunction.recomputeLandingPads();
|
|
|
|
|
|
|
|
if (NextBB)
|
|
|
|
return std::make_pair(NextBB, NextBB->begin());
|
|
|
|
|
|
|
|
if (Callee.size() == 1)
|
|
|
|
return std::make_pair(FirstInlinedBB, InsertII);
|
|
|
|
|
|
|
|
return std::make_pair(FirstInlinedBB, FirstInlinedBB->end());
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
bool Inliner::inlineCallsInFunction(BinaryFunction &Function) {
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryContext &BC = Function.getBinaryContext();
|
2017-02-17 06:57:57 +08:00
|
|
|
std::vector<BinaryBasicBlock *> Blocks(Function.layout().begin(),
|
|
|
|
Function.layout().end());
|
|
|
|
std::sort(Blocks.begin(), Blocks.end(),
|
2021-12-15 08:52:51 +08:00
|
|
|
[](const BinaryBasicBlock *BB1, const BinaryBasicBlock *BB2) {
|
|
|
|
return BB1->getKnownExecutionCount() >
|
|
|
|
BB2->getKnownExecutionCount();
|
|
|
|
});
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
bool DidInlining = false;
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock *BB : Blocks) {
|
2021-12-15 08:52:51 +08:00
|
|
|
for (auto InstIt = BB->begin(); InstIt != BB->end();) {
|
2021-04-08 15:19:26 +08:00
|
|
|
MCInst &Inst = *InstIt;
|
2019-02-01 03:23:02 +08:00
|
|
|
if (!BC.MIB->isCall(Inst) || MCPlus::getNumPrimeOperands(Inst) != 1 ||
|
|
|
|
!Inst.getOperand(0).isExpr()) {
|
|
|
|
++InstIt;
|
|
|
|
continue;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const MCSymbol *TargetSymbol = BC.MIB->getTargetSymbol(Inst);
|
2019-02-01 03:23:02 +08:00
|
|
|
assert(TargetSymbol && "target symbol expected for direct call");
|
2020-01-07 06:57:15 +08:00
|
|
|
|
|
|
|
// Don't inline calls to a secondary entry point in a target function.
|
2021-05-14 01:50:47 +08:00
|
|
|
uint64_t EntryID = 0;
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryFunction *TargetFunction =
|
|
|
|
BC.getFunctionForSymbol(TargetSymbol, &EntryID);
|
2020-01-07 06:57:15 +08:00
|
|
|
if (!TargetFunction || EntryID != 0) {
|
2019-02-01 03:23:02 +08:00
|
|
|
++InstIt;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
// Don't do recursive inlining.
|
|
|
|
if (TargetFunction == &Function) {
|
|
|
|
++InstIt;
|
|
|
|
continue;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
auto IInfo = InliningCandidates.find(TargetFunction);
|
|
|
|
if (IInfo == InliningCandidates.end()) {
|
|
|
|
++InstIt;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const bool IsTailCall = BC.MIB->isTailCall(Inst);
|
2019-02-01 03:23:02 +08:00
|
|
|
if (!IsTailCall && IInfo->second.Type == INL_TAILCALL) {
|
|
|
|
++InstIt;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
int64_t SizeAfterInlining;
|
2021-12-29 08:36:17 +08:00
|
|
|
if (IsTailCall)
|
2021-12-15 08:52:51 +08:00
|
|
|
SizeAfterInlining =
|
|
|
|
IInfo->second.SizeAfterTailCallInlining - getSizeOfTailCallInst(BC);
|
2021-12-29 08:36:17 +08:00
|
|
|
else
|
2021-12-15 08:52:51 +08:00
|
|
|
SizeAfterInlining =
|
|
|
|
IInfo->second.SizeAfterInlining - getSizeOfCallInst(BC);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
if (!opts::InlineAll && !opts::mustConsider(*TargetFunction)) {
|
|
|
|
if (!opts::InlineSmallFunctions ||
|
|
|
|
SizeAfterInlining > opts::InlineSmallFunctionsBytes) {
|
|
|
|
++InstIt;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: inlining call to " << *TargetFunction
|
|
|
|
<< " in " << Function << " : " << BB->getName()
|
|
|
|
<< ". Count: " << BB->getKnownExecutionCount()
|
|
|
|
<< ". Size change: " << SizeAfterInlining
|
|
|
|
<< " bytes.\n");
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
std::tie(BB, InstIt) = inlineCall(*BB, InstIt, *TargetFunction);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
DidInlining = true;
|
|
|
|
TotalInlinedBytes += SizeAfterInlining;
|
|
|
|
|
|
|
|
++NumInlinedCallSites;
|
|
|
|
NumInlinedDynamicCalls += BB->getExecutionCount();
|
|
|
|
|
|
|
|
// Subtract basic block execution count from the callee execution count.
|
2021-12-29 08:36:17 +08:00
|
|
|
if (opts::AdjustProfile)
|
2019-02-01 03:23:02 +08:00
|
|
|
TargetFunction->adjustExecutionCount(BB->getKnownExecutionCount());
|
|
|
|
|
|
|
|
// Check if the caller inlining status has to be adjusted.
|
|
|
|
if (IInfo->second.Type == INL_TAILCALL) {
|
|
|
|
auto CallerIInfo = InliningCandidates.find(&Function);
|
|
|
|
if (CallerIInfo != InliningCandidates.end() &&
|
|
|
|
CallerIInfo->second.Type == INL_ANY) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "adjusting inlining status for function "
|
|
|
|
<< Function << '\n');
|
2019-02-01 03:23:02 +08:00
|
|
|
CallerIInfo->second.Type = INL_TAILCALL;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 08:36:17 +08:00
|
|
|
if (NumInlinedCallSites == opts::InlineLimit)
|
2019-02-01 03:23:02 +08:00
|
|
|
return true;
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DidInlining;
|
|
|
|
}
|
|
|
|
|
2019-04-04 06:52:01 +08:00
|
|
|
void Inliner::runOnFunctions(BinaryContext &BC) {
|
2019-02-01 03:23:02 +08:00
|
|
|
opts::syncOptions();
|
|
|
|
|
|
|
|
if (!opts::inliningEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint64_t TotalSize = 0;
|
2019-04-04 06:52:01 +08:00
|
|
|
for (auto &BFI : BC.getBinaryFunctions())
|
2019-02-01 03:23:02 +08:00
|
|
|
TotalSize += BFI.second.getSize();
|
|
|
|
|
|
|
|
bool InlinedOnce;
|
|
|
|
unsigned NumIters = 0;
|
|
|
|
do {
|
|
|
|
if (opts::InlineLimit && NumInlinedCallSites >= opts::InlineLimit)
|
|
|
|
break;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedOnce = false;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
InliningCandidates.clear();
|
2019-04-04 06:52:01 +08:00
|
|
|
findInliningCandidates(BC);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
std::vector<BinaryFunction *> ConsideredFunctions;
|
2019-04-04 06:52:01 +08:00
|
|
|
for (auto &BFI : BC.getBinaryFunctions()) {
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryFunction &Function = BFI.second;
|
2019-02-01 03:23:02 +08:00
|
|
|
if (!shouldOptimize(Function))
|
|
|
|
continue;
|
|
|
|
ConsideredFunctions.push_back(&Function);
|
2017-02-17 06:57:57 +08:00
|
|
|
}
|
2019-02-01 03:23:02 +08:00
|
|
|
std::sort(ConsideredFunctions.begin(), ConsideredFunctions.end(),
|
2021-12-15 08:52:51 +08:00
|
|
|
[](const BinaryFunction *A, const BinaryFunction *B) {
|
|
|
|
return B->getKnownExecutionCount() <
|
|
|
|
A->getKnownExecutionCount();
|
|
|
|
});
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryFunction *Function : ConsideredFunctions) {
|
2019-02-01 03:23:02 +08:00
|
|
|
if (opts::InlineLimit && NumInlinedCallSites >= opts::InlineLimit)
|
|
|
|
break;
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const bool DidInline = inlineCallsInFunction(*Function);
|
2019-02-01 03:23:02 +08:00
|
|
|
|
|
|
|
if (DidInline)
|
|
|
|
Modified.insert(Function);
|
2017-02-17 06:57:57 +08:00
|
|
|
|
2019-02-01 03:23:02 +08:00
|
|
|
InlinedOnce |= DidInline;
|
|
|
|
}
|
|
|
|
|
|
|
|
++NumIters;
|
|
|
|
} while (InlinedOnce && NumIters < opts::InlineMaxIters);
|
|
|
|
|
2021-12-29 08:36:17 +08:00
|
|
|
if (NumInlinedCallSites)
|
2019-02-01 03:23:02 +08:00
|
|
|
outs() << "BOLT-INFO: inlined " << NumInlinedDynamicCalls << " calls at "
|
|
|
|
<< NumInlinedCallSites << " call sites in " << NumIters
|
|
|
|
<< " iteration(s). Change in binary size: " << TotalInlinedBytes
|
|
|
|
<< " bytes.\n";
|
|
|
|
}
|
2017-02-17 06:57:57 +08:00
|
|
|
|
|
|
|
} // namespace bolt
|
|
|
|
} // namespace llvm
|