LCOV - code coverage report
Current view: top level - include/llvm/DebugInfo/CodeView - TypeHashing.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 24 28 85.7 %
Date: 2018-02-20 03:34:22 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- TypeHashing.h ---------------------------------------------*- C++-*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #ifndef LLVM_DEBUGINFO_CODEVIEW_TYPEHASHING_H
      11             : #define LLVM_DEBUGINFO_CODEVIEW_TYPEHASHING_H
      12             : 
      13             : #include "llvm/ADT/DenseMapInfo.h"
      14             : #include "llvm/ADT/Hashing.h"
      15             : 
      16             : #include "llvm/DebugInfo/CodeView/CodeView.h"
      17             : #include "llvm/DebugInfo/CodeView/TypeCollection.h"
      18             : #include "llvm/DebugInfo/CodeView/TypeIndex.h"
      19             : 
      20             : #include "llvm/Support/FormatProviders.h"
      21             : 
      22             : #include <type_traits>
      23             : 
      24             : namespace llvm {
      25             : namespace codeview {
      26             : 
      27             : /// A locally hashed type represents a straightforward hash code of a serialized
      28             : /// record.  The record is simply serialized, and then the bytes are hashed by
      29             : /// a standard algorithm.  This is sufficient for the case of de-duplicating
      30             : /// records within a single sequence of types, because if two records both have
      31             : /// a back-reference to the same type in the same stream, they will both have
      32             : /// the same numeric value for the TypeIndex of the back reference.
      33             : struct LocallyHashedType {
      34             :   hash_code Hash;
      35             :   ArrayRef<uint8_t> RecordData;
      36             : 
      37             :   /// Given a type, compute its local hash.
      38             :   static LocallyHashedType hashType(ArrayRef<uint8_t> RecordData);
      39             : 
      40             :   /// Given a sequence of types, compute all of the local hashes.
      41             :   template <typename Range>
      42             :   static std::vector<LocallyHashedType> hashTypes(Range &&Records) {
      43             :     std::vector<LocallyHashedType> Hashes;
      44             :     Hashes.reserve(std::distance(std::begin(Records), std::end(Records)));
      45             :     for (const auto &R : Records)
      46             :       Hashes.push_back(hashType(R));
      47             : 
      48             :     return Hashes;
      49             :   }
      50             : 
      51             :   static std::vector<LocallyHashedType>
      52             :   hashTypeCollection(TypeCollection &Types) {
      53             :     std::vector<LocallyHashedType> Hashes;
      54          13 :     Types.ForEachRecord([&Hashes](TypeIndex TI, const CVType &Type) {
      55          22 :       Hashes.push_back(hashType(Type.RecordData));
      56          11 :     });
      57             :     return Hashes;
      58             :   }
      59             : };
      60             : 
      61             : enum class GlobalTypeHashAlg : uint16_t { SHA1 = 0 };
      62             : 
      63             : /// A globally hashed type represents a hash value that is sufficient to
      64             : /// uniquely identify a record across multiple type streams or type sequences.
      65             : /// This works by, for any given record A which references B, replacing the
      66             : /// TypeIndex that refers to B with a previously-computed global hash for B.  As
      67             : /// this is a recursive algorithm (e.g. the global hash of B also depends on the
      68             : /// global hashes of the types that B refers to), a global hash can uniquely
      69             : /// identify identify that A occurs in another stream that has a completely
      70             : /// different graph structure.  Although the hash itself is slower to compute,
      71             : /// probing is much faster with a globally hashed type, because the hash itself
      72             : /// is considered "as good as" the original type.  Since type records can be
      73             : /// quite large, this makes the equality comparison of the hash much faster than
      74             : /// equality comparison of a full record.
      75             : struct GloballyHashedType {
      76             :   GloballyHashedType() = default;
      77             :   GloballyHashedType(StringRef H)
      78             :       : GloballyHashedType(ArrayRef<uint8_t>(H.bytes_begin(), H.bytes_end())) {}
      79      195792 :   GloballyHashedType(ArrayRef<uint8_t> H) {
      80             :     assert(H.size() == 20);
      81      195792 :     ::memcpy(Hash.data(), H.data(), 20);
      82             :   }
      83             :   std::array<uint8_t, 20> Hash;
      84             : 
      85             :   /// Given a sequence of bytes representing a record, compute a global hash for
      86             :   /// this record.  Due to the nature of global hashes incorporating the hashes
      87             :   /// of referenced records, this function requires a list of types and ids
      88             :   /// that RecordData might reference, indexable by TypeIndex.
      89             :   static GloballyHashedType hashType(ArrayRef<uint8_t> RecordData,
      90             :                                      ArrayRef<GloballyHashedType> PreviousTypes,
      91             :                                      ArrayRef<GloballyHashedType> PreviousIds);
      92             : 
      93             :   /// Given a sequence of bytes representing a record, compute a global hash for
      94             :   /// this record.  Due to the nature of global hashes incorporating the hashes
      95             :   /// of referenced records, this function requires a list of types and ids
      96             :   /// that RecordData might reference, indexable by TypeIndex.
      97             :   static GloballyHashedType hashType(CVType Type,
      98             :                                      ArrayRef<GloballyHashedType> PreviousTypes,
      99             :                                      ArrayRef<GloballyHashedType> PreviousIds) {
     100          14 :     return hashType(Type.RecordData, PreviousTypes, PreviousIds);
     101             :   }
     102             : 
     103             :   /// Given a sequence of combined type and ID records, compute global hashes
     104             :   /// for each of them, returning the results in a vector of hashed types.
     105             :   template <typename Range>
     106           3 :   static std::vector<GloballyHashedType> hashTypes(Range &&Records) {
     107             :     std::vector<GloballyHashedType> Hashes;
     108          41 :     for (const auto &R : Records)
     109          36 :       Hashes.push_back(hashType(R, Hashes, Hashes));
     110             : 
     111           3 :     return Hashes;
     112             :   }
     113             : 
     114             :   /// Given a sequence of combined type and ID records, compute global hashes
     115             :   /// for each of them, returning the results in a vector of hashed types.
     116             :   template <typename Range>
     117             :   static std::vector<GloballyHashedType>
     118           0 :   hashIds(Range &&Records, ArrayRef<GloballyHashedType> TypeHashes) {
     119             :     std::vector<GloballyHashedType> IdHashes;
     120           0 :     for (const auto &R : Records)
     121           0 :       IdHashes.push_back(hashType(R, TypeHashes, IdHashes));
     122             : 
     123           0 :     return IdHashes;
     124             :   }
     125             : 
     126             :   static std::vector<GloballyHashedType>
     127             :   hashTypeCollection(TypeCollection &Types) {
     128             :     std::vector<GloballyHashedType> Hashes;
     129          13 :     Types.ForEachRecord([&Hashes](TypeIndex TI, const CVType &Type) {
     130          22 :       Hashes.push_back(hashType(Type.RecordData, Hashes, Hashes));
     131          11 :     });
     132             :     return Hashes;
     133             :   }
     134             : };
     135             : #if defined(_MSC_VER)
     136             : // is_trivially_copyable is not available in older versions of libc++, but it is
     137             : // available in all supported versions of MSVC, so at least this gives us some
     138             : // coverage.
     139             : static_assert(std::is_trivially_copyable<GloballyHashedType>::value,
     140             :               "GloballyHashedType must be trivially copyable so that we can "
     141             :               "reinterpret_cast arrays of hash data to arrays of "
     142             :               "GloballyHashedType");
     143             : #endif
     144             : } // namespace codeview
     145             : 
     146             : template <> struct DenseMapInfo<codeview::LocallyHashedType> {
     147             :   static codeview::LocallyHashedType Empty;
     148             :   static codeview::LocallyHashedType Tombstone;
     149             : 
     150        3364 :   static codeview::LocallyHashedType getEmptyKey() { return Empty; }
     151             : 
     152        2099 :   static codeview::LocallyHashedType getTombstoneKey() { return Tombstone; }
     153             : 
     154             :   static unsigned getHashValue(codeview::LocallyHashedType Val) {
     155        2090 :     return Val.Hash;
     156             :   }
     157             : 
     158             :   static bool isEqual(codeview::LocallyHashedType LHS,
     159             :                       codeview::LocallyHashedType RHS) {
     160        9543 :     if (LHS.Hash != RHS.Hash)
     161             :       return false;
     162             :     return LHS.RecordData == RHS.RecordData;
     163             :   }
     164             : };
     165             : 
     166             : template <> struct DenseMapInfo<codeview::GloballyHashedType> {
     167             :   static codeview::GloballyHashedType Empty;
     168             :   static codeview::GloballyHashedType Tombstone;
     169             : 
     170        2330 :   static codeview::GloballyHashedType getEmptyKey() { return Empty; }
     171             : 
     172        1202 :   static codeview::GloballyHashedType getTombstoneKey() { return Tombstone; }
     173             : 
     174             :   static unsigned getHashValue(codeview::GloballyHashedType Val) {
     175             :     return *reinterpret_cast<const unsigned *>(Val.Hash.data());
     176             :   }
     177             : 
     178             :   static bool isEqual(codeview::GloballyHashedType LHS,
     179             :                       codeview::GloballyHashedType RHS) {
     180             :     return LHS.Hash == RHS.Hash;
     181             :   }
     182             : };
     183             : 
     184             : template <> struct format_provider<codeview::LocallyHashedType> {
     185             : public:
     186             :   static void format(const codeview::LocallyHashedType &V,
     187             :                      llvm::raw_ostream &Stream, StringRef Style) {
     188          11 :     write_hex(Stream, V.Hash, HexPrintStyle::Upper, 8);
     189             :   }
     190             : };
     191             : 
     192             : template <> struct format_provider<codeview::GloballyHashedType> {
     193             : public:
     194          27 :   static void format(const codeview::GloballyHashedType &V,
     195             :                      llvm::raw_ostream &Stream, StringRef Style) {
     196        1107 :     for (uint8_t B : V.Hash) {
     197         540 :       write_hex(Stream, B, HexPrintStyle::Upper, 2);
     198             :     }
     199          27 :   }
     200             : };
     201             : 
     202             : } // namespace llvm
     203             : 
     204             : #endif

Generated by: LCOV version 1.13