1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
//===- bolt/Passes/RegAnalysis.cpp ----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the RegAnalysis class.
//
//===----------------------------------------------------------------------===//
#include "bolt/Passes/RegAnalysis.h"
#include "bolt/Core/BinaryFunction.h"
#include "bolt/Passes/CallGraphWalker.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/Support/CommandLine.h"
#define DEBUG_TYPE "ra"
using namespace llvm;
namespace opts {
extern cl::opt<unsigned> Verbosity;
extern cl::OptionCategory BoltOptCategory;
cl::opt<bool> AssumeABI("assume-abi",
cl::desc("assume the ABI is never violated"),
cl::cat(BoltOptCategory));
}
namespace llvm {
namespace bolt {
RegAnalysis::RegAnalysis(BinaryContext &BC,
std::map<uint64_t, BinaryFunction> *BFs,
BinaryFunctionCallGraph *CG)
: BC(BC), CS(opts::AssumeABI ? ConservativeStrategy::CLOBBERS_ABI
: ConservativeStrategy::CLOBBERS_ALL) {
if (!CG)
return;
CallGraphWalker CGWalker(*CG);
CGWalker.registerVisitor([&](BinaryFunction *Func) -> bool {
BitVector RegsKilled = getFunctionClobberList(Func);
bool Updated = RegsKilledMap.find(Func) == RegsKilledMap.end() ||
RegsKilledMap[Func] != RegsKilled;
if (Updated)
RegsKilledMap[Func] = std::move(RegsKilled);
return Updated;
});
CGWalker.registerVisitor([&](BinaryFunction *Func) -> bool {
BitVector RegsGen = getFunctionUsedRegsList(Func);
bool Updated = RegsGenMap.find(Func) == RegsGenMap.end() ||
RegsGenMap[Func] != RegsGen;
if (Updated)
RegsGenMap[Func] = std::move(RegsGen);
return Updated;
});
CGWalker.walk();
if (opts::Verbosity == 0) {
#ifndef NDEBUG
if (!DebugFlag || !isCurrentDebugType(DEBUG_TYPE))
return;
#else
return;
#endif
}
if (!BFs)
return;
// This loop is for computing statistics only
for (auto &MapEntry : *BFs) {
BinaryFunction *Func = &MapEntry.second;
auto Iter = RegsKilledMap.find(Func);
assert(Iter != RegsKilledMap.end() &&
"Failed to compute all clobbers list");
if (Iter->second.all()) {
uint64_t Count = Func->getExecutionCount();
if (Count != BinaryFunction::COUNT_NO_PROFILE)
CountFunctionsAllClobber += Count;
++NumFunctionsAllClobber;
}
DEBUG_WITH_TYPE("ra",
dbgs() << "Killed regs set for func: " << Func->getPrintName() << "\n";
const BitVector &RegsKilled = Iter->second;
int RegIdx = RegsKilled.find_first();
while (RegIdx != -1) {
dbgs() << "\tREG" << RegIdx;
RegIdx = RegsKilled.find_next(RegIdx);
};
dbgs() << "\nUsed regs set for func: " << Func->getPrintName() << "\n";
const BitVector &RegsUsed = RegsGenMap.find(Func)->second;
RegIdx = RegsUsed.find_first();
while (RegIdx != -1) {
dbgs() << "\tREG" << RegIdx;
RegIdx = RegsUsed.find_next(RegIdx);
};
dbgs() << "\n";
);
}
}
void RegAnalysis::beConservative(BitVector &Result) const {
switch (CS) {
case ConservativeStrategy::CLOBBERS_ALL:
Result.set();
break;
case ConservativeStrategy::CLOBBERS_ABI: {
BitVector BV(BC.MRI->getNumRegs(), false);
BC.MIB->getCalleeSavedRegs(BV);
BV.flip();
Result |= BV;
break;
}
case ConservativeStrategy::CLOBBERS_NONE:
Result.reset();
break;
}
}
bool RegAnalysis::isConservative(BitVector &Vec) const {
switch (CS) {
case ConservativeStrategy::CLOBBERS_ALL:
return Vec.all();
case ConservativeStrategy::CLOBBERS_ABI: {
BitVector BV(BC.MRI->getNumRegs(), false);
BC.MIB->getCalleeSavedRegs(BV);
BV |= Vec;
return BV.all();
}
case ConservativeStrategy::CLOBBERS_NONE:
return Vec.none();
}
return false;
}
void RegAnalysis::getInstUsedRegsList(const MCInst &Inst, BitVector &RegSet,
bool GetClobbers) const {
if (!BC.MIB->isCall(Inst)) {
if (GetClobbers)
BC.MIB->getClobberedRegs(Inst, RegSet);
else
BC.MIB->getUsedRegs(Inst, RegSet);
return;
}
// If no call graph supplied...
if (RegsKilledMap.size() == 0) {
beConservative(RegSet);
return;
}
const MCSymbol *TargetSymbol = BC.MIB->getTargetSymbol(Inst);
// If indirect call, we know nothing
if (TargetSymbol == nullptr) {
beConservative(RegSet);
return;
}
const BinaryFunction *Function = BC.getFunctionForSymbol(TargetSymbol);
if (Function == nullptr) {
// Call to a function without a BinaryFunction object.
// This should be a call to a PLT entry, and since it is a trampoline to
// a DSO, we can't really know the code in advance.
beConservative(RegSet);
return;
}
if (GetClobbers) {
auto BV = RegsKilledMap.find(Function);
if (BV != RegsKilledMap.end()) {
RegSet |= BV->second;
return;
}
// Ignore calls to function whose clobber list wasn't yet calculated. This
// instruction will be evaluated again once we have info for the callee.
return;
}
auto BV = RegsGenMap.find(Function);
if (BV != RegsGenMap.end()) {
RegSet |= BV->second;
return;
}
}
void RegAnalysis::getInstClobberList(const MCInst &Inst,
BitVector &KillSet) const {
return getInstUsedRegsList(Inst, KillSet, /*GetClobbers*/ true);
}
BitVector RegAnalysis::getFunctionUsedRegsList(const BinaryFunction *Func) {
BitVector UsedRegs = BitVector(BC.MRI->getNumRegs(), false);
if (!Func->isSimple() || !Func->hasCFG()) {
beConservative(UsedRegs);
return UsedRegs;
}
for (const BinaryBasicBlock &BB : *Func) {
for (const MCInst &Inst : BB) {
getInstUsedRegsList(Inst, UsedRegs, /*GetClobbers*/ false);
if (UsedRegs.all())
return UsedRegs;
}
}
return UsedRegs;
}
BitVector RegAnalysis::getFunctionClobberList(const BinaryFunction *Func) {
BitVector RegsKilled = BitVector(BC.MRI->getNumRegs(), false);
if (!Func->isSimple() || !Func->hasCFG()) {
beConservative(RegsKilled);
return RegsKilled;
}
for (const BinaryBasicBlock &BB : *Func) {
for (const MCInst &Inst : BB) {
getInstClobberList(Inst, RegsKilled);
if (RegsKilled.all())
return RegsKilled;
}
}
return RegsKilled;
}
void RegAnalysis::printStats() {
outs() << "BOLT-INFO REG ANALYSIS: Number of functions conservatively "
"treated as clobbering all registers: "
<< NumFunctionsAllClobber
<< format(" (%.1lf%% dyn cov)\n",
(100.0 * CountFunctionsAllClobber / CountDenominator));
}
} // namespace bolt
} // namespace llvm
|