LLVM  8.0.1
TypeHashing.cpp
Go to the documentation of this file.
1 //===- TypeHashing.cpp -------------------------------------------*- 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 
11 
13 #include "llvm/Support/SHA1.h"
14 
15 using namespace llvm;
16 using namespace llvm::codeview;
17 
20 
21 static std::array<uint8_t, 8> EmptyHash = {
22  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
23 static std::array<uint8_t, 8> TombstoneHash = {
24  {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
25 
28 
30  return {llvm::hash_value(RecordData), RecordData};
31 }
32 
35  ArrayRef<GloballyHashedType> PreviousTypes,
36  ArrayRef<GloballyHashedType> PreviousIds) {
38  discoverTypeIndices(RecordData, Refs);
39  SHA1 S;
40  S.init();
41  uint32_t Off = 0;
42  S.update(RecordData.take_front(sizeof(RecordPrefix)));
43  RecordData = RecordData.drop_front(sizeof(RecordPrefix));
44  for (const auto &Ref : Refs) {
45  // Hash any data that comes before this TiRef.
46  uint32_t PreLen = Ref.Offset - Off;
47  ArrayRef<uint8_t> PreData = RecordData.slice(Off, PreLen);
48  S.update(PreData);
49  auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
50 
51  auto RefData = RecordData.slice(Ref.Offset, Ref.Count * sizeof(TypeIndex));
52  // For each type index referenced, add in the previously computed hash
53  // value of that type.
54  ArrayRef<TypeIndex> Indices(
55  reinterpret_cast<const TypeIndex *>(RefData.data()), Ref.Count);
56  for (TypeIndex TI : Indices) {
57  ArrayRef<uint8_t> BytesToHash;
58  if (TI.isSimple() || TI.isNoneType() || TI.toArrayIndex() >= Prev.size()) {
59  const uint8_t *IndexBytes = reinterpret_cast<const uint8_t *>(&TI);
60  BytesToHash = makeArrayRef(IndexBytes, sizeof(TypeIndex));
61  } else {
62  BytesToHash = Prev[TI.toArrayIndex()].Hash;
63  }
64  S.update(BytesToHash);
65  }
66 
67  Off = Ref.Offset + Ref.Count * sizeof(TypeIndex);
68  }
69 
70  // Don't forget to add in any trailing bytes.
71  auto TrailingBytes = RecordData.drop_front(Off);
72  S.update(TrailingBytes);
73 
74  return {S.final().take_back(8)};
75 }
void discoverTypeIndices(ArrayRef< uint8_t > RecordData, SmallVectorImpl< TiReference > &Refs)
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:212
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A locally hashed type represents a straightforward hash code of a serialized record.
Definition: TypeHashing.h:33
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:29
void init()
Reinitialize the internal state.
Definition: SHA1.cpp:85
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef take_back(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with only the last N elements remaining.
Definition: StringRef.h:619
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
static LocallyHashedType hashType(ArrayRef< uint8_t > RecordData)
Given a type, compute its local hash.
Definition: TypeHashing.cpp:29
The access may reference the value stored in memory.
StringRef final()
Return a reference to the current raw 160-bits SHA1 for the digested data since the last call to init...
Definition: SHA1.cpp:238
A 32-bit type reference.
Definition: TypeIndex.h:96
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4431
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition: TypeHashing.h:78
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA1.cpp:213
static std::array< uint8_t, 8 > TombstoneHash
Definition: TypeHashing.cpp:23
static std::array< uint8_t, 8 > EmptyHash
Definition: TypeHashing.cpp:21
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
An opaque object representing a hash code.
Definition: Hashing.h:72
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:179
static GloballyHashedType hashType(ArrayRef< uint8_t > RecordData, ArrayRef< GloballyHashedType > PreviousTypes, ArrayRef< GloballyHashedType > PreviousIds)
Given a sequence of bytes representing a record, compute a global hash for this record.
Definition: TypeHashing.cpp:34
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:188
ArrayRef< uint8_t > RecordData
Definition: TypeHashing.h:35