[gcov] Simplify/speed up CFG hash calculation

This commit is contained in:
Fangrui Song 2020-07-26 21:14:20 -07:00
parent e97aa5609f
commit fae221e7ad
1 changed files with 13 additions and 17 deletions

View File

@ -32,6 +32,7 @@
#include "llvm/IR/Module.h" #include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h" #include "llvm/InitializePasses.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Support/CRC.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
@ -300,15 +301,16 @@ namespace {
assert(OutEdges.empty()); assert(OutEdges.empty());
} }
uint32_t Number;
SmallVector<GCOVBlock *, 4> OutEdges;
private: private:
friend class GCOVFunction; friend class GCOVFunction;
GCOVBlock(GCOVProfiler *P, uint32_t Number) GCOVBlock(GCOVProfiler *P, uint32_t Number)
: GCOVRecord(P), Number(Number) {} : GCOVRecord(P), Number(Number) {}
uint32_t Number;
StringMap<GCOVLines> LinesByFile; StringMap<GCOVLines> LinesByFile;
SmallVector<GCOVBlock *, 4> OutEdges;
}; };
// A function has a unique identifier, a checksum (we leave as zero) and a // A function has a unique identifier, a checksum (we leave as zero) and a
@ -347,18 +349,6 @@ namespace {
return ReturnBlock; return ReturnBlock;
} }
std::string getEdgeDestinations() {
std::string EdgeDestinations;
raw_string_ostream EDOS(EdgeDestinations);
Function *F = Blocks.begin()->first->getParent();
for (BasicBlock &I : *F) {
GCOVBlock &Block = getBlock(&I);
for (int i = 0, e = Block.OutEdges.size(); i != e; ++i)
EDOS << Block.OutEdges[i]->Number;
}
return EdgeDestinations;
}
uint32_t getFuncChecksum() const { uint32_t getFuncChecksum() const {
return FuncChecksum; return FuncChecksum;
} }
@ -729,7 +719,7 @@ void GCOVProfiler::emitProfileNotes() {
continue; continue;
} }
std::string EdgeDestinations; std::vector<uint8_t> EdgeDestinations;
Endian = M->getDataLayout().isLittleEndian() ? support::endianness::little Endian = M->getDataLayout().isLittleEndian() ? support::endianness::little
: support::endianness::big; : support::endianness::big;
@ -774,6 +764,11 @@ void GCOVProfiler::emitProfileNotes() {
} else if (isa<ReturnInst>(TI)) { } else if (isa<ReturnInst>(TI)) {
Block.addEdge(Func.getReturnBlock()); Block.addEdge(Func.getReturnBlock());
} }
for (GCOVBlock *Succ : Block.OutEdges) {
uint32_t Idx = Succ->Number;
do EdgeDestinations.push_back(Idx & 255);
while ((Idx >>= 8) > 0);
}
for (auto &I : BB) { for (auto &I : BB) {
// Debug intrinsic locations correspond to the location of the // Debug intrinsic locations correspond to the location of the
@ -798,12 +793,13 @@ void GCOVProfiler::emitProfileNotes() {
} }
Line = 0; Line = 0;
} }
EdgeDestinations += Func.getEdgeDestinations();
} }
char Tmp[4]; char Tmp[4];
JamCRC JC;
JC.update(EdgeDestinations);
os = &out; os = &out;
auto Stamp = static_cast<uint32_t>(hash_value(EdgeDestinations)); uint32_t Stamp = JC.getCRC();
FileChecksums.push_back(Stamp); FileChecksums.push_back(Stamp);
if (Endian == support::endianness::big) { if (Endian == support::endianness::big) {
out.write("gcno", 4); out.write("gcno", 4);