[libFuzzer] refactoring around PCMap, NFC
llvm-svn: 278825
diff --git a/llvm/lib/Fuzzer/FuzzerTracePC.cpp b/llvm/lib/Fuzzer/FuzzerTracePC.cpp
index 46c43d0..53341d3 100644
--- a/llvm/lib/Fuzzer/FuzzerTracePC.cpp
+++ b/llvm/lib/Fuzzer/FuzzerTracePC.cpp
@@ -16,43 +16,22 @@
namespace fuzzer {
-void PcCoverageMap::Reset() { memset(Map, 0, sizeof(Map)); }
+static size_t PreviouslyComputedPCHash;
+static ValueBitMap CurrentPCMap;
-void PcCoverageMap::Update(uintptr_t Addr) {
- uintptr_t Idx = Addr % kMapSizeInBits;
- uintptr_t WordIdx = Idx / kBitsInWord;
- uintptr_t BitIdx = Idx % kBitsInWord;
- Map[WordIdx] |= 1UL << BitIdx;
-}
-
-size_t PcCoverageMap::MergeFrom(const PcCoverageMap &Other) {
- uintptr_t Res = 0;
- for (size_t i = 0; i < kMapSizeInWords; i++)
- Res += __builtin_popcountl(Map[i] |= Other.Map[i]);
- return Res;
-}
-
-static PcCoverageMap CurrentMap;
-static thread_local uintptr_t Prev;
-
-void PcMapResetCurrent() {
- if (Prev) {
- Prev = 0;
- CurrentMap.Reset();
- }
-}
-
-size_t PcMapMergeInto(PcCoverageMap *Map) {
- if (!Prev)
+// Merges CurrentPCMap into M, returns the number of new bits.
+size_t PCMapMergeFromCurrent(ValueBitMap &M) {
+ if (!PreviouslyComputedPCHash)
return 0;
- return Map->MergeFrom(CurrentMap);
+ PreviouslyComputedPCHash = 0;
+ return M.MergeFrom(CurrentPCMap);
}
static void HandlePC(uint32_t PC) {
// We take 12 bits of PC and mix it with the previous PCs.
- uintptr_t Next = (Prev << 5) ^ (PC & 4095);
- CurrentMap.Update(Next);
- Prev = Next;
+ uintptr_t Next = (PreviouslyComputedPCHash << 5) ^ (PC & 4095);
+ CurrentPCMap.AddValue(Next);
+ PreviouslyComputedPCHash = Next;
}
} // namespace fuzzer