2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/Passes/FrameAnalysis.cpp --------------------------------------===//
|
2017-05-02 07:51:27 +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-05-02 07:51:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2021-12-22 02:21:41 +08:00
|
|
|
// This file implements the FrameAnalysis class.
|
|
|
|
//
|
2017-05-02 07:51:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-12-22 02:21:41 +08:00
|
|
|
|
2021-10-09 02:47:10 +08:00
|
|
|
#include "bolt/Passes/FrameAnalysis.h"
|
|
|
|
#include "bolt/Core/ParallelUtilities.h"
|
|
|
|
#include "bolt/Passes/CallGraphWalker.h"
|
2021-05-01 04:54:02 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2017-05-02 07:51:27 +08:00
|
|
|
#include <fstream>
|
2020-12-02 08:29:39 +08:00
|
|
|
#include <stack>
|
2017-05-02 07:51:27 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "fa"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace opts {
|
2019-06-15 10:56:11 +08:00
|
|
|
extern cl::OptionCategory BoltOptCategory;
|
2017-05-02 07:51:27 +08:00
|
|
|
extern cl::opt<unsigned> Verbosity;
|
2019-06-15 10:56:11 +08:00
|
|
|
|
2017-05-02 07:51:27 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
FrameOptFunctionNames("funcs-fop", cl::CommaSeparated,
|
|
|
|
cl::desc("list of functions to apply frame opts"),
|
|
|
|
cl::value_desc("func1,func2,func3,..."));
|
|
|
|
|
|
|
|
static cl::opt<std::string> FrameOptFunctionNamesFile(
|
|
|
|
"funcs-file-fop",
|
|
|
|
cl::desc("file with list of functions to frame optimize"));
|
|
|
|
|
2019-06-15 10:56:11 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
TimeFA("time-fa",
|
|
|
|
cl::desc("time frame analysis steps"),
|
|
|
|
cl::ReallyHidden,
|
|
|
|
cl::ZeroOrMore,
|
|
|
|
cl::cat(BoltOptCategory));
|
|
|
|
|
2017-05-02 07:51:27 +08:00
|
|
|
bool shouldFrameOptimize(const llvm::bolt::BinaryFunction &Function) {
|
2019-06-29 00:21:27 +08:00
|
|
|
if (Function.hasUnknownControlFlow())
|
|
|
|
return false;
|
|
|
|
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!FrameOptFunctionNamesFile.empty()) {
|
|
|
|
assert(!FrameOptFunctionNamesFile.empty() && "unexpected empty file name");
|
|
|
|
std::ifstream FuncsFile(FrameOptFunctionNamesFile, std::ios::in);
|
|
|
|
std::string FuncName;
|
2021-12-29 08:36:17 +08:00
|
|
|
while (std::getline(FuncsFile, FuncName))
|
2017-05-02 07:51:27 +08:00
|
|
|
FrameOptFunctionNames.push_back(FuncName);
|
|
|
|
FrameOptFunctionNamesFile = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid = true;
|
|
|
|
if (!FrameOptFunctionNames.empty()) {
|
|
|
|
IsValid = false;
|
2021-04-08 15:19:26 +08:00
|
|
|
for (std::string &Name : FrameOptFunctionNames) {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (Function.hasName(Name)) {
|
|
|
|
IsValid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!IsValid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return IsValid;
|
|
|
|
}
|
|
|
|
} // namespace opts
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace bolt {
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const FrameIndexEntry &FIE) {
|
|
|
|
OS << "FrameIndexEntry<IsLoad: " << FIE.IsLoad << ", IsStore: " << FIE.IsStore
|
|
|
|
<< ", IsStoreFromReg: " << FIE.IsStoreFromReg
|
|
|
|
<< ", RegOrImm: " << FIE.RegOrImm << ", StackOffset: ";
|
|
|
|
if (FIE.StackOffset < 0)
|
|
|
|
OS << "-" << Twine::utohexstr(-FIE.StackOffset);
|
|
|
|
else
|
|
|
|
OS << "+" << Twine::utohexstr(FIE.StackOffset);
|
2018-03-13 11:24:01 +08:00
|
|
|
OS << ", Size: " << static_cast<int>(FIE.Size)
|
|
|
|
<< ", IsSimple: " << FIE.IsSimple << ">";
|
2017-05-02 07:51:27 +08:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This class should be used to iterate through basic blocks in layout order
|
|
|
|
/// to analyze instructions for frame accesses. The user should call
|
|
|
|
/// enterNewBB() whenever starting analyzing a new BB and doNext() for each
|
|
|
|
/// instruction. After doNext(), if isValidAccess() returns true, it means the
|
|
|
|
/// current instruction accesses the frame and getFIE() may be used to obtain
|
|
|
|
/// details about this access.
|
|
|
|
class FrameAccessAnalysis {
|
|
|
|
/// We depend on Stack Pointer Tracking to figure out the current SP offset
|
|
|
|
/// value at a given program point
|
2019-06-07 03:58:14 +08:00
|
|
|
StackPointerTracking &SPT;
|
|
|
|
|
2017-05-02 07:51:27 +08:00
|
|
|
/// Context vars
|
|
|
|
const BinaryContext &BC;
|
|
|
|
const BinaryFunction &BF;
|
|
|
|
// Vars used for storing useful CFI info to give us a hint about how the stack
|
|
|
|
// is used in this function
|
|
|
|
int SPOffset{0};
|
|
|
|
int FPOffset{0};
|
|
|
|
int64_t CfaOffset{-8};
|
|
|
|
uint16_t CfaReg{7};
|
|
|
|
std::stack<std::pair<int64_t, uint16_t>> CFIStack;
|
|
|
|
/// Our pointer to access SPT info
|
|
|
|
const MCInst *Prev{nullptr};
|
|
|
|
/// Info about the last frame access
|
|
|
|
bool IsValidAccess{false};
|
|
|
|
FrameIndexEntry FIE;
|
|
|
|
|
|
|
|
bool decodeFrameAccess(const MCInst &Inst) {
|
2021-05-14 01:50:47 +08:00
|
|
|
int32_t SrcImm = 0;
|
|
|
|
MCPhysReg Reg = 0;
|
|
|
|
int64_t StackOffset = 0;
|
|
|
|
bool IsIndexed = false;
|
2020-12-02 08:29:39 +08:00
|
|
|
if (!BC.MIB->isStackAccess(
|
|
|
|
Inst, FIE.IsLoad, FIE.IsStore, FIE.IsStoreFromReg, Reg, SrcImm,
|
|
|
|
FIE.StackPtrReg, StackOffset, FIE.Size, FIE.IsSimple, IsIndexed)) {
|
2017-05-02 07:51:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:34:01 +08:00
|
|
|
if (IsIndexed || FIE.Size == 0) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Giving up on indexed memory access/unknown size\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "Blame insn: ");
|
|
|
|
LLVM_DEBUG(Inst.dump());
|
2017-05-02 07:51:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(FIE.Size != 0);
|
|
|
|
|
|
|
|
FIE.RegOrImm = SrcImm;
|
|
|
|
if (FIE.IsLoad || FIE.IsStoreFromReg)
|
|
|
|
FIE.RegOrImm = Reg;
|
|
|
|
|
2018-03-10 01:45:13 +08:00
|
|
|
if (FIE.StackPtrReg == BC.MIB->getStackPointer() && SPOffset != SPT.EMPTY &&
|
2017-05-02 07:51:27 +08:00
|
|
|
SPOffset != SPT.SUPERPOSITION) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Adding access via SP while CFA reg is another one\n");
|
2017-05-02 07:51:27 +08:00
|
|
|
FIE.StackOffset = SPOffset + StackOffset;
|
2018-03-10 01:45:13 +08:00
|
|
|
} else if (FIE.StackPtrReg == BC.MIB->getFramePointer() &&
|
2017-05-02 07:51:27 +08:00
|
|
|
FPOffset != SPT.EMPTY && FPOffset != SPT.SUPERPOSITION) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Adding access via FP while CFA reg is another one\n");
|
2017-05-02 07:51:27 +08:00
|
|
|
FIE.StackOffset = FPOffset + StackOffset;
|
|
|
|
} else if (FIE.StackPtrReg ==
|
2020-12-02 08:29:39 +08:00
|
|
|
*BC.MRI->getLLVMRegNum(CfaReg, /*isEH=*/false)) {
|
2017-05-02 07:51:27 +08:00
|
|
|
FIE.StackOffset = CfaOffset + StackOffset;
|
|
|
|
} else {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Found stack access with reg different than cfa reg.\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "\tCurrent CFA reg: " << CfaReg
|
|
|
|
<< "\n\tStack access reg: " << FIE.StackPtrReg << "\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "Blame insn: ");
|
|
|
|
LLVM_DEBUG(Inst.dump());
|
2017-05-02 07:51:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IsValidAccess = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-10-26 15:06:34 +08:00
|
|
|
FrameAccessAnalysis(BinaryFunction &BF, StackPointerTracking &SPT)
|
|
|
|
: SPT(SPT), BC(BF.getBinaryContext()), BF(BF) {}
|
2017-05-02 07:51:27 +08:00
|
|
|
|
|
|
|
void enterNewBB() { Prev = nullptr; }
|
|
|
|
const FrameIndexEntry &getFIE() const { return FIE; }
|
|
|
|
int getSPOffset() const { return SPOffset; }
|
|
|
|
bool isValidAccess() const { return IsValidAccess; }
|
|
|
|
|
|
|
|
bool doNext(const BinaryBasicBlock &BB, const MCInst &Inst) {
|
|
|
|
IsValidAccess = false;
|
|
|
|
std::tie(SPOffset, FPOffset) =
|
|
|
|
Prev ? *SPT.getStateAt(*Prev) : *SPT.getStateAt(BB);
|
|
|
|
Prev = &Inst;
|
|
|
|
// Use CFI information to keep track of which register is being used to
|
|
|
|
// access the frame
|
2018-03-10 01:45:13 +08:00
|
|
|
if (BC.MIB->isCFI(Inst)) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const MCCFIInstruction *CFI = BF.getCFIFor(Inst);
|
2017-05-02 07:51:27 +08:00
|
|
|
switch (CFI->getOperation()) {
|
|
|
|
case MCCFIInstruction::OpDefCfa:
|
|
|
|
CfaOffset = CFI->getOffset();
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2017-05-02 07:51:27 +08:00
|
|
|
case MCCFIInstruction::OpDefCfaRegister:
|
|
|
|
CfaReg = CFI->getRegister();
|
|
|
|
break;
|
|
|
|
case MCCFIInstruction::OpDefCfaOffset:
|
|
|
|
CfaOffset = CFI->getOffset();
|
|
|
|
break;
|
|
|
|
case MCCFIInstruction::OpRememberState:
|
|
|
|
CFIStack.push(std::make_pair(CfaOffset, CfaReg));
|
|
|
|
break;
|
|
|
|
case MCCFIInstruction::OpRestoreState: {
|
2021-12-29 08:36:17 +08:00
|
|
|
if (CFIStack.empty())
|
2017-05-02 07:51:27 +08:00
|
|
|
dbgs() << "Assertion is about to fail: " << BF.getPrintName() << "\n";
|
|
|
|
assert(!CFIStack.empty() && "Corrupt CFI stack");
|
2021-04-08 15:19:26 +08:00
|
|
|
std::pair<int64_t, uint16_t> &Elem = CFIStack.top();
|
2017-05-02 07:51:27 +08:00
|
|
|
CFIStack.pop();
|
|
|
|
CfaOffset = Elem.first;
|
|
|
|
CfaReg = Elem.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCCFIInstruction::OpAdjustCfaOffset:
|
|
|
|
llvm_unreachable("Unhandled AdjustCfaOffset");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-10 01:45:13 +08:00
|
|
|
if (BC.MIB->escapesVariable(Inst, SPT.HasFramePointer)) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Leaked stack address, giving up on this function.\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "Blame insn: ");
|
|
|
|
LLVM_DEBUG(Inst.dump());
|
2017-05-02 07:51:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeFrameAccess(Inst);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
void FrameAnalysis::addArgAccessesFor(MCInst &Inst, ArgAccesses &&AA) {
|
2021-04-08 15:19:26 +08:00
|
|
|
if (ErrorOr<ArgAccesses &> OldAA = getArgAccessesFor(Inst)) {
|
2017-05-02 07:52:54 +08:00
|
|
|
if (OldAA->AssumeEverything)
|
|
|
|
return;
|
|
|
|
*OldAA = std::move(AA);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-02 07:51:27 +08:00
|
|
|
if (AA.AssumeEverything) {
|
|
|
|
// Index 0 in ArgAccessesVector represents an "assumeeverything" entry
|
[BOLT][Refactoring] Isolate changes to MC layer
Summary:
Changes that we made to MCInst, MCOperand, MCExpr, etc. are now all
moved into tools/llvm-bolt. That required a change to the way we handle
annotations and any extra operands for MCInst.
Any MCPlus information is now attached via an extra operand of type
MCInst with an opcode ANNOTATION_LABEL. Since this operand is MCInst, we
attach extra info as operands to this instruction. For first-level
annotations use functions to access the information, such as
getConditionalTailCall() or getEHInfo(), etc. For the rest, optional or
second-class annotations, use a general named-annotation interface such
as getAnnotationAs<uint64_t>(Inst, "Count").
I did a test on HHVM binary, and a memory consumption went down a little
bit while the runtime remained the same.
(cherry picked from FBD7405412)
2018-03-20 09:32:12 +08:00
|
|
|
BC.MIB->addAnnotation(Inst, "ArgAccessEntry", 0U);
|
2017-05-02 07:51:27 +08:00
|
|
|
return;
|
|
|
|
}
|
[BOLT][Refactoring] Isolate changes to MC layer
Summary:
Changes that we made to MCInst, MCOperand, MCExpr, etc. are now all
moved into tools/llvm-bolt. That required a change to the way we handle
annotations and any extra operands for MCInst.
Any MCPlus information is now attached via an extra operand of type
MCInst with an opcode ANNOTATION_LABEL. Since this operand is MCInst, we
attach extra info as operands to this instruction. For first-level
annotations use functions to access the information, such as
getConditionalTailCall() or getEHInfo(), etc. For the rest, optional or
second-class annotations, use a general named-annotation interface such
as getAnnotationAs<uint64_t>(Inst, "Count").
I did a test on HHVM binary, and a memory consumption went down a little
bit while the runtime remained the same.
(cherry picked from FBD7405412)
2018-03-20 09:32:12 +08:00
|
|
|
BC.MIB->addAnnotation(Inst, "ArgAccessEntry",
|
2017-05-02 07:51:27 +08:00
|
|
|
(unsigned)ArgAccessesVector.size());
|
2017-05-02 07:52:54 +08:00
|
|
|
ArgAccessesVector.emplace_back(std::move(AA));
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
void FrameAnalysis::addArgInStackAccessFor(MCInst &Inst,
|
2017-05-02 07:51:27 +08:00
|
|
|
const ArgInStackAccess &Arg) {
|
2021-04-08 15:19:26 +08:00
|
|
|
ErrorOr<ArgAccesses &> AA = getArgAccessesFor(Inst);
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!AA) {
|
2017-06-03 07:57:22 +08:00
|
|
|
addArgAccessesFor(Inst, ArgAccesses(false));
|
|
|
|
AA = getArgAccessesFor(Inst);
|
2017-05-02 07:51:27 +08:00
|
|
|
assert(AA && "Object setup failed");
|
|
|
|
}
|
2021-04-08 15:19:26 +08:00
|
|
|
std::set<ArgInStackAccess> &Set = AA->Set;
|
2017-05-02 07:51:27 +08:00
|
|
|
assert(!AA->AssumeEverything && "Adding arg to AssumeEverything set");
|
|
|
|
Set.emplace(Arg);
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
void FrameAnalysis::addFIEFor(MCInst &Inst, const FrameIndexEntry &FIE) {
|
2021-12-15 08:52:51 +08:00
|
|
|
BC.MIB->addAnnotation(Inst, "FrameAccessEntry", (unsigned)FIEVector.size());
|
2017-05-02 07:51:27 +08:00
|
|
|
FIEVector.emplace_back(FIE);
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
ErrorOr<ArgAccesses &> FrameAnalysis::getArgAccessesFor(const MCInst &Inst) {
|
2018-03-10 01:45:13 +08:00
|
|
|
if (auto Idx = BC.MIB->tryGetAnnotationAs<unsigned>(Inst, "ArgAccessEntry")) {
|
2017-05-02 07:51:27 +08:00
|
|
|
assert(ArgAccessesVector.size() > *Idx && "Out of bounds");
|
|
|
|
return ArgAccessesVector[*Idx];
|
|
|
|
}
|
|
|
|
return make_error_code(errc::result_out_of_range);
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<const ArgAccesses &>
|
2017-06-03 07:57:22 +08:00
|
|
|
FrameAnalysis::getArgAccessesFor(const MCInst &Inst) const {
|
2018-03-10 01:45:13 +08:00
|
|
|
if (auto Idx = BC.MIB->tryGetAnnotationAs<unsigned>(Inst, "ArgAccessEntry")) {
|
2017-05-02 07:51:27 +08:00
|
|
|
assert(ArgAccessesVector.size() > *Idx && "Out of bounds");
|
|
|
|
return ArgAccessesVector[*Idx];
|
|
|
|
}
|
|
|
|
return make_error_code(errc::result_out_of_range);
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<const FrameIndexEntry &>
|
2017-06-03 07:57:22 +08:00
|
|
|
FrameAnalysis::getFIEFor(const MCInst &Inst) const {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (auto Idx =
|
2018-03-10 01:45:13 +08:00
|
|
|
BC.MIB->tryGetAnnotationAs<unsigned>(Inst, "FrameAccessEntry")) {
|
2017-05-02 07:51:27 +08:00
|
|
|
assert(FIEVector.size() > *Idx && "Out of bounds");
|
|
|
|
return FIEVector[*Idx];
|
|
|
|
}
|
|
|
|
return make_error_code(errc::result_out_of_range);
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
void FrameAnalysis::traverseCG(BinaryFunctionCallGraph &CG) {
|
2017-06-09 04:46:17 +08:00
|
|
|
CallGraphWalker CGWalker(CG);
|
2017-05-02 07:51:27 +08:00
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
CGWalker.registerVisitor(
|
|
|
|
[&](BinaryFunction *Func) -> bool { return computeArgsAccessed(*Func); });
|
2017-05-02 07:51:27 +08:00
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
CGWalker.walk();
|
2017-06-17 06:02:26 +08:00
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
DEBUG_WITH_TYPE("ra", {
|
2017-06-17 06:02:26 +08:00
|
|
|
for (auto &MapEntry : ArgsTouchedMap) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const BinaryFunction *Func = MapEntry.first;
|
2017-06-17 06:02:26 +08:00
|
|
|
const auto &Set = MapEntry.second;
|
|
|
|
dbgs() << "Args accessed for " << Func->getPrintName() << ": ";
|
2021-12-29 08:36:17 +08:00
|
|
|
if (!Set.empty() && Set.count(std::make_pair(-1, 0)))
|
2017-06-17 06:02:26 +08:00
|
|
|
dbgs() << "assume everything";
|
2021-12-29 08:36:17 +08:00
|
|
|
else
|
|
|
|
for (const std::pair<int64_t, uint8_t> &Entry : Set)
|
2017-06-17 06:02:26 +08:00
|
|
|
dbgs() << "[" << Entry.first << ", " << (int)Entry.second << "] ";
|
|
|
|
dbgs() << "\n";
|
2021-04-08 15:19:26 +08:00
|
|
|
}
|
2017-06-17 06:02:26 +08:00
|
|
|
});
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
bool FrameAnalysis::updateArgsTouchedFor(const BinaryFunction &BF, MCInst &Inst,
|
2017-05-02 07:51:27 +08:00
|
|
|
int CurOffset) {
|
2018-03-10 01:45:13 +08:00
|
|
|
if (!BC.MIB->isCall(Inst))
|
2017-05-02 07:51:27 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
std::set<int64_t> Res;
|
2021-04-08 15:19:26 +08:00
|
|
|
const MCSymbol *TargetSymbol = BC.MIB->getTargetSymbol(Inst);
|
2017-05-02 07:51:27 +08:00
|
|
|
// If indirect call, we conservatively assume it accesses all stack positions
|
|
|
|
if (TargetSymbol == nullptr) {
|
2017-06-03 07:57:22 +08:00
|
|
|
addArgAccessesFor(Inst, ArgAccesses(/*AssumeEverything=*/true));
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!FunctionsRequireAlignment.count(&BF)) {
|
|
|
|
FunctionsRequireAlignment.insert(&BF);
|
2017-12-15 09:26:19 +08:00
|
|
|
return true;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
2017-12-15 09:26:19 +08:00
|
|
|
return false;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
const BinaryFunction *Function = BC.getFunctionForSymbol(TargetSymbol);
|
2017-05-02 07:51:27 +08:00
|
|
|
// Call to a function without a BinaryFunction object. Conservatively assume
|
|
|
|
// it accesses all stack positions
|
|
|
|
if (Function == nullptr) {
|
2017-06-03 07:57:22 +08:00
|
|
|
addArgAccessesFor(Inst, ArgAccesses(/*AssumeEverything=*/true));
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!FunctionsRequireAlignment.count(&BF)) {
|
|
|
|
FunctionsRequireAlignment.insert(&BF);
|
2017-12-15 09:26:19 +08:00
|
|
|
return true;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
2017-12-15 09:26:19 +08:00
|
|
|
return false;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Iter = ArgsTouchedMap.find(Function);
|
|
|
|
|
|
|
|
bool Changed = false;
|
2018-03-10 01:45:13 +08:00
|
|
|
if (BC.MIB->isTailCall(Inst) && Iter != ArgsTouchedMap.end()) {
|
2017-05-02 07:51:27 +08:00
|
|
|
// Ignore checking CurOffset because we can't always reliably determine the
|
|
|
|
// offset specially after an epilogue, where tailcalls happen. It should be
|
|
|
|
// -8.
|
2021-04-08 15:19:26 +08:00
|
|
|
for (std::pair<int64_t, uint8_t> Elem : Iter->second) {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (ArgsTouchedMap[&BF].find(Elem) == ArgsTouchedMap[&BF].end()) {
|
|
|
|
ArgsTouchedMap[&BF].emplace(Elem);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FunctionsRequireAlignment.count(Function) &&
|
|
|
|
!FunctionsRequireAlignment.count(&BF)) {
|
|
|
|
Changed = true;
|
|
|
|
FunctionsRequireAlignment.insert(&BF);
|
|
|
|
}
|
2017-12-15 09:26:19 +08:00
|
|
|
if (Iter == ArgsTouchedMap.end())
|
2020-12-04 12:09:32 +08:00
|
|
|
return Changed;
|
2017-05-02 07:51:27 +08:00
|
|
|
|
|
|
|
if (CurOffset == StackPointerTracking::EMPTY ||
|
|
|
|
CurOffset == StackPointerTracking::SUPERPOSITION) {
|
2017-06-03 07:57:22 +08:00
|
|
|
addArgAccessesFor(Inst, ArgAccesses(/*AssumeEverything=*/true));
|
2017-05-02 07:51:27 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
for (std::pair<int64_t, uint8_t> Elem : Iter->second) {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (Elem.first == -1) {
|
2017-06-03 07:57:22 +08:00
|
|
|
addArgAccessesFor(Inst, ArgAccesses(/*AssumeEverything=*/true));
|
2017-05-02 07:51:27 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Added arg in stack access annotation "
|
|
|
|
<< CurOffset + Elem.first << "\n");
|
2017-05-02 07:51:27 +08:00
|
|
|
addArgInStackAccessFor(
|
2017-06-03 07:57:22 +08:00
|
|
|
Inst, ArgInStackAccess{/*StackOffset=*/CurOffset + Elem.first,
|
|
|
|
/*Size=*/Elem.second});
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
bool FrameAnalysis::computeArgsAccessed(BinaryFunction &BF) {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!BF.isSimple() || !BF.hasCFG()) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Treating " << BF.getPrintName()
|
|
|
|
<< " conservatively.\n");
|
2017-05-02 07:51:27 +08:00
|
|
|
ArgsTouchedMap[&BF].emplace(std::make_pair(-1, 0));
|
|
|
|
if (!FunctionsRequireAlignment.count(&BF)) {
|
|
|
|
FunctionsRequireAlignment.insert(&BF);
|
2017-12-15 09:26:19 +08:00
|
|
|
return true;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
2017-12-15 09:26:19 +08:00
|
|
|
return false;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Now computing args accessed for: " << BF.getPrintName()
|
|
|
|
<< "\n");
|
2017-05-02 07:51:27 +08:00
|
|
|
bool UpdatedArgsTouched = false;
|
2017-12-15 09:26:19 +08:00
|
|
|
bool NoInfo = false;
|
2021-10-26 15:06:34 +08:00
|
|
|
FrameAccessAnalysis FAA(BF, getSPT(BF));
|
2017-05-02 07:51:27 +08:00
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock *BB : BF.layout()) {
|
2017-05-02 07:51:27 +08:00
|
|
|
FAA.enterNewBB();
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
for (MCInst &Inst : *BB) {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!FAA.doNext(*BB, Inst)) {
|
|
|
|
ArgsTouchedMap[&BF].emplace(std::make_pair(-1, 0));
|
2017-12-15 09:26:19 +08:00
|
|
|
NoInfo = true;
|
2017-05-02 07:51:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for calls -- attach stack accessing info to them regarding their
|
|
|
|
// target
|
2017-06-03 07:57:22 +08:00
|
|
|
if (updateArgsTouchedFor(BF, Inst, FAA.getSPOffset()))
|
2017-05-02 07:51:27 +08:00
|
|
|
UpdatedArgsTouched = true;
|
|
|
|
|
|
|
|
// Check for stack accesses that affect callers
|
|
|
|
if (!FAA.isValidAccess())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const FrameIndexEntry &FIE = FAA.getFIE();
|
|
|
|
if (FIE.StackOffset < 0)
|
|
|
|
continue;
|
|
|
|
if (ArgsTouchedMap[&BF].find(std::make_pair(FIE.StackOffset, FIE.Size)) !=
|
|
|
|
ArgsTouchedMap[&BF].end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Record accesses to the previous stack frame
|
|
|
|
ArgsTouchedMap[&BF].emplace(std::make_pair(FIE.StackOffset, FIE.Size));
|
|
|
|
UpdatedArgsTouched = true;
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG({
|
2017-05-02 07:51:27 +08:00
|
|
|
dbgs() << "Arg access offset " << FIE.StackOffset << " added to:\n";
|
|
|
|
BC.printInstruction(dbgs(), Inst, 0, &BF, true);
|
|
|
|
});
|
|
|
|
}
|
2017-12-15 09:26:19 +08:00
|
|
|
if (NoInfo)
|
|
|
|
break;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
if (FunctionsRequireAlignment.count(&BF))
|
|
|
|
return UpdatedArgsTouched;
|
|
|
|
|
2017-12-15 09:26:19 +08:00
|
|
|
if (NoInfo) {
|
|
|
|
FunctionsRequireAlignment.insert(&BF);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock &BB : BF) {
|
|
|
|
for (MCInst &Inst : BB) {
|
2018-03-10 01:45:13 +08:00
|
|
|
if (BC.MIB->requiresAlignedAddress(Inst)) {
|
2017-12-15 09:26:19 +08:00
|
|
|
FunctionsRequireAlignment.insert(&BF);
|
|
|
|
return true;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 09:26:19 +08:00
|
|
|
return UpdatedArgsTouched;
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
bool FrameAnalysis::restoreFrameIndex(BinaryFunction &BF) {
|
2021-10-26 15:06:34 +08:00
|
|
|
FrameAccessAnalysis FAA(BF, getSPT(BF));
|
2017-05-02 07:51:27 +08:00
|
|
|
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Restoring frame indices for \"" << BF.getPrintName()
|
|
|
|
<< "\"\n");
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock *BB : BF.layout()) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tNow at BB " << BB->getName() << "\n");
|
2017-05-02 07:51:27 +08:00
|
|
|
FAA.enterNewBB();
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
for (MCInst &Inst : *BB) {
|
2017-05-02 07:51:27 +08:00
|
|
|
if (!FAA.doNext(*BB, Inst))
|
|
|
|
return false;
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG({
|
2017-05-02 07:51:27 +08:00
|
|
|
dbgs() << "\t\tNow at ";
|
|
|
|
Inst.dump();
|
|
|
|
dbgs() << "\t\t\tSP offset is " << FAA.getSPOffset() << "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!FAA.isValidAccess())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const FrameIndexEntry &FIE = FAA.getFIE();
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
addFIEFor(Inst, FIE);
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG({
|
2017-05-02 07:51:27 +08:00
|
|
|
dbgs() << "Frame index annotation " << FIE << " added to:\n";
|
|
|
|
BC.printInstruction(dbgs(), Inst, 0, &BF, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:57:22 +08:00
|
|
|
void FrameAnalysis::cleanAnnotations() {
|
2019-06-15 10:56:11 +08:00
|
|
|
NamedRegionTimer T("cleanannotations", "clean annotations", "FA",
|
|
|
|
"FA breakdown", opts::TimeFA);
|
2019-07-26 02:57:08 +08:00
|
|
|
|
|
|
|
ParallelUtilities::WorkFuncTy CleanFunction = [&](BinaryFunction &BF) {
|
2021-04-08 15:19:26 +08:00
|
|
|
for (BinaryBasicBlock &BB : BF) {
|
|
|
|
for (MCInst &Inst : BB) {
|
2019-07-26 02:57:08 +08:00
|
|
|
BC.MIB->removeAnnotation(Inst, "ArgAccessEntry");
|
|
|
|
BC.MIB->removeAnnotation(Inst, "FrameAccessEntry");
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 04:42:17 +08:00
|
|
|
};
|
|
|
|
|
2019-07-26 02:57:08 +08:00
|
|
|
ParallelUtilities::runOnEachFunction(
|
|
|
|
BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, CleanFunction,
|
|
|
|
ParallelUtilities::PredicateTy(nullptr), "cleanAnnotations");
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
2019-06-15 10:56:11 +08:00
|
|
|
FrameAnalysis::FrameAnalysis(BinaryContext &BC, BinaryFunctionCallGraph &CG)
|
2019-04-04 06:52:01 +08:00
|
|
|
: BC(BC) {
|
2017-06-03 07:57:22 +08:00
|
|
|
// Position 0 of the vector should be always associated with "assume access
|
|
|
|
// everything".
|
|
|
|
ArgAccessesVector.emplace_back(ArgAccesses(/*AssumeEverything*/ true));
|
[BOLT] Optimize jump tables with hot entries
Summary:
This diff is similar to Bill's diff for optimizing jump tables
(and is built on top of it), but it differs in the strategy used to
optimize the jump table. The previous approach loads the target address
from the jump table and compare it to check if it is a hot target. This
accomplishes branch misprediction reduction by promote the indirect jmp
to a (more predictable) direct jmp.
load %r10, JMPTABLE
cmp %r10, HOTTARGET
je HOTTARGET
ijmp [JMPTABLE + %index * scale]
The idea in this diff is instead to make dcache better by avoiding the
load of the jump table, leaving branch mispredictions as a secondary
target. To do this we compare the index used in the indirect jmp and if
it matches a known hot entry, it performs a direct jump to the target.
cmp %index, HOTINDEX
je CORRESPONDING_TARGET
ijmp [JMPTABLE + %index * scale]
The downside of this approach is that we may have multiple indices
associated with a single target, but we only have profiling to show
which targets are hot and we have no clue about which indices are hot.
INDEX TARGET
0 4004f8
8 4004f8
10 4003d0
18 4004f8
Profiling data:
TARGET COUNT
4004f8 10020
4003d0 17
In this example, we know 4004f8 is hot, but to make a direct call to it
we need to check for indices 0, 8 and 18 -- 3 comparisons instead of 1.
Therefore, once we know a target is hot, we must generate code to
compare against all possible indices associated with this target because
we don't know which index is the hot one (IF there's a hotter index).
cmp %index, 0
je 4004f8
cmp %index, 8
je 4004f8
cmp %index, 18
je 4004f8
(... up to N comparisons as in --indirect-call-promotion-topn=N )
ijmp [JMPTABLE + %index * scale]
(cherry picked from FBD5005620)
2017-05-02 05:04:40 +08:00
|
|
|
|
2019-06-15 10:56:11 +08:00
|
|
|
if (!opts::NoThreads) {
|
|
|
|
NamedRegionTimer T1("precomputespt", "pre-compute spt", "FA",
|
|
|
|
"FA breakdown", opts::TimeFA);
|
|
|
|
preComputeSPT();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
NamedRegionTimer T1("traversecg", "traverse call graph", "FA",
|
|
|
|
"FA breakdown", opts::TimeFA);
|
|
|
|
traverseCG(CG);
|
|
|
|
}
|
[BOLT] Optimize jump tables with hot entries
Summary:
This diff is similar to Bill's diff for optimizing jump tables
(and is built on top of it), but it differs in the strategy used to
optimize the jump table. The previous approach loads the target address
from the jump table and compare it to check if it is a hot target. This
accomplishes branch misprediction reduction by promote the indirect jmp
to a (more predictable) direct jmp.
load %r10, JMPTABLE
cmp %r10, HOTTARGET
je HOTTARGET
ijmp [JMPTABLE + %index * scale]
The idea in this diff is instead to make dcache better by avoiding the
load of the jump table, leaving branch mispredictions as a secondary
target. To do this we compare the index used in the indirect jmp and if
it matches a known hot entry, it performs a direct jump to the target.
cmp %index, HOTINDEX
je CORRESPONDING_TARGET
ijmp [JMPTABLE + %index * scale]
The downside of this approach is that we may have multiple indices
associated with a single target, but we only have profiling to show
which targets are hot and we have no clue about which indices are hot.
INDEX TARGET
0 4004f8
8 4004f8
10 4003d0
18 4004f8
Profiling data:
TARGET COUNT
4004f8 10020
4003d0 17
In this example, we know 4004f8 is hot, but to make a direct call to it
we need to check for indices 0, 8 and 18 -- 3 comparisons instead of 1.
Therefore, once we know a target is hot, we must generate code to
compare against all possible indices associated with this target because
we don't know which index is the hot one (IF there's a hotter index).
cmp %index, 0
je 4004f8
cmp %index, 8
je 4004f8
cmp %index, 18
je 4004f8
(... up to N comparisons as in --indirect-call-promotion-topn=N )
ijmp [JMPTABLE + %index * scale]
(cherry picked from FBD5005620)
2017-05-02 05:04:40 +08:00
|
|
|
|
2019-04-04 06:52:01 +08:00
|
|
|
for (auto &I : BC.getBinaryFunctions()) {
|
2021-04-08 15:19:26 +08:00
|
|
|
uint64_t Count = I.second.getExecutionCount();
|
2017-05-02 07:51:27 +08:00
|
|
|
if (Count != BinaryFunction::COUNT_NO_PROFILE)
|
|
|
|
CountDenominator += Count;
|
|
|
|
|
|
|
|
// "shouldOptimize" for passes that run after finalize
|
2020-05-04 04:54:45 +08:00
|
|
|
if (!(I.second.isSimple() && I.second.hasCFG() && !I.second.isIgnored()) ||
|
2017-05-02 07:51:27 +08:00
|
|
|
!opts::shouldFrameOptimize(I.second)) {
|
|
|
|
++NumFunctionsNotOptimized;
|
|
|
|
if (Count != BinaryFunction::COUNT_NO_PROFILE)
|
|
|
|
CountFunctionsNotOptimized += Count;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-06-15 10:56:11 +08:00
|
|
|
NamedRegionTimer T1("restorefi", "restore frame index", "FA",
|
|
|
|
"FA breakdown", opts::TimeFA);
|
2017-06-03 07:57:22 +08:00
|
|
|
if (!restoreFrameIndex(I.second)) {
|
2017-05-02 07:51:27 +08:00
|
|
|
++NumFunctionsFailedRestoreFI;
|
2021-04-08 15:19:26 +08:00
|
|
|
uint64_t Count = I.second.getExecutionCount();
|
2017-05-02 07:51:27 +08:00
|
|
|
if (Count != BinaryFunction::COUNT_NO_PROFILE)
|
|
|
|
CountFunctionsFailedRestoreFI += Count;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AnalyzedFunctions.insert(&I.second);
|
|
|
|
}
|
2019-06-15 10:56:11 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
NamedRegionTimer T1("clearspt", "clear spt", "FA", "FA breakdown",
|
|
|
|
opts::TimeFA);
|
|
|
|
clearSPTMap();
|
|
|
|
|
|
|
|
// Clean up memory allocated for annotation values
|
2021-12-29 08:36:17 +08:00
|
|
|
if (!opts::NoThreads)
|
2021-04-08 15:19:26 +08:00
|
|
|
for (MCPlusBuilder::AllocatorIdTy Id : SPTAllocatorsId)
|
2019-06-15 10:56:11 +08:00
|
|
|
BC.MIB->freeValuesAllocator(Id);
|
|
|
|
}
|
2017-05-02 07:51:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameAnalysis::printStats() {
|
2017-10-21 03:11:34 +08:00
|
|
|
outs() << "BOLT-INFO: FRAME ANALYSIS: " << NumFunctionsNotOptimized
|
2017-05-02 07:51:27 +08:00
|
|
|
<< " function(s) "
|
|
|
|
<< format("(%.1lf%% dyn cov)",
|
|
|
|
(100.0 * CountFunctionsNotOptimized / CountDenominator))
|
|
|
|
<< " were not optimized.\n"
|
2017-10-21 03:11:34 +08:00
|
|
|
<< "BOLT-INFO: FRAME ANALYSIS: " << NumFunctionsFailedRestoreFI
|
2017-05-02 07:51:27 +08:00
|
|
|
<< " function(s) "
|
|
|
|
<< format("(%.1lf%% dyn cov)",
|
|
|
|
(100.0 * CountFunctionsFailedRestoreFI / CountDenominator))
|
|
|
|
<< " could not have its frame indices restored.\n";
|
|
|
|
}
|
|
|
|
|
2019-06-20 09:01:00 +08:00
|
|
|
void FrameAnalysis::clearSPTMap() {
|
|
|
|
if (opts::NoThreads) {
|
|
|
|
SPTMap.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-26 02:57:08 +08:00
|
|
|
ParallelUtilities::WorkFuncTy ClearFunctionSPT = [&](BinaryFunction &BF) {
|
2021-04-08 15:19:26 +08:00
|
|
|
std::unique_ptr<StackPointerTracking> &SPTPtr = SPTMap.find(&BF)->second;
|
2019-07-26 02:57:08 +08:00
|
|
|
SPTPtr.reset();
|
2019-06-20 09:01:00 +08:00
|
|
|
};
|
|
|
|
|
2019-07-26 02:57:08 +08:00
|
|
|
ParallelUtilities::PredicateTy SkipFunc = [&](const BinaryFunction &BF) {
|
|
|
|
return !BF.isSimple() || !BF.hasCFG();
|
|
|
|
};
|
2019-06-20 09:01:00 +08:00
|
|
|
|
2019-07-26 02:57:08 +08:00
|
|
|
ParallelUtilities::runOnEachFunction(
|
|
|
|
BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, ClearFunctionSPT,
|
|
|
|
SkipFunc, "clearSPTMap");
|
2019-06-20 09:01:00 +08:00
|
|
|
|
|
|
|
SPTMap.clear();
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:56:11 +08:00
|
|
|
void FrameAnalysis::preComputeSPT() {
|
|
|
|
// Make sure that the SPTMap is empty
|
|
|
|
assert(SPTMap.size() == 0);
|
|
|
|
|
|
|
|
// Create map entries to allow lock-free parallel execution
|
|
|
|
for (auto &BFI : BC.getBinaryFunctions()) {
|
2021-04-08 15:19:26 +08:00
|
|
|
BinaryFunction &BF = BFI.second;
|
2019-06-15 10:56:11 +08:00
|
|
|
if (!BF.isSimple() || !BF.hasCFG())
|
|
|
|
continue;
|
|
|
|
SPTMap.emplace(&BF, std::unique_ptr<StackPointerTracking>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an index for the SPT annotation to allow lock-free parallel
|
|
|
|
// execution
|
|
|
|
BC.MIB->getOrCreateAnnotationIndex("StackPointerTracking");
|
|
|
|
|
2019-07-26 02:57:08 +08:00
|
|
|
// Run SPT in parallel
|
|
|
|
ParallelUtilities::WorkFuncWithAllocTy ProcessFunction =
|
|
|
|
[&](BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy AllocId) {
|
2021-04-08 15:19:26 +08:00
|
|
|
std::unique_ptr<StackPointerTracking> &SPTPtr =
|
|
|
|
SPTMap.find(&BF)->second;
|
2021-10-26 15:06:34 +08:00
|
|
|
SPTPtr = std::make_unique<StackPointerTracking>(BF, AllocId);
|
2019-07-26 02:57:08 +08:00
|
|
|
SPTPtr->run();
|
|
|
|
};
|
|
|
|
|
|
|
|
ParallelUtilities::PredicateTy SkipPredicate = [&](const BinaryFunction &BF) {
|
|
|
|
return !BF.isSimple() || !BF.hasCFG();
|
|
|
|
};
|
2019-06-15 10:56:11 +08:00
|
|
|
|
2019-07-26 02:57:08 +08:00
|
|
|
ParallelUtilities::runOnEachFunctionWithUniqueAllocId(
|
|
|
|
BC, ParallelUtilities::SchedulingPolicy::SP_BB_QUADRATIC, ProcessFunction,
|
|
|
|
SkipPredicate, "preComputeSPT");
|
2019-06-15 10:56:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 07:51:27 +08:00
|
|
|
} // namespace bolt
|
|
|
|
} // namespace llvm
|