LLVM  14.0.0git
TypeHashing.cpp
Go to the documentation of this file.
1 //===- TypeHashing.cpp -------------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
12 #include "llvm/Support/SHA1.h"
13 
14 using namespace llvm;
15 using namespace llvm::codeview;
16 
19 
20 static std::array<uint8_t, 8> EmptyHash = {
21  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
22 static std::array<uint8_t, 8> TombstoneHash = {
23  {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
24 
27 
30 }
31 
34  ArrayRef<GloballyHashedType> PreviousTypes,
35  ArrayRef<GloballyHashedType> PreviousIds) {
37  discoverTypeIndices(RecordData, Refs);
38  SHA1 S;
39  S.init();
40  uint32_t Off = 0;
41  S.update(RecordData.take_front(sizeof(RecordPrefix)));
42  RecordData = RecordData.drop_front(sizeof(RecordPrefix));
43  for (const auto &Ref : Refs) {
44  // Hash any data that comes before this TiRef.
45  uint32_t PreLen = Ref.Offset - Off;
46  ArrayRef<uint8_t> PreData = RecordData.slice(Off, PreLen);
47  S.update(PreData);
48  auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
49 
50  auto RefData = RecordData.slice(Ref.Offset, Ref.Count * sizeof(TypeIndex));
51  // For each type index referenced, add in the previously computed hash
52  // value of that type.
53  ArrayRef<TypeIndex> Indices(
54  reinterpret_cast<const TypeIndex *>(RefData.data()), Ref.Count);
55  for (TypeIndex TI : Indices) {
56  ArrayRef<uint8_t> BytesToHash;
57  if (TI.isSimple() || TI.isNoneType()) {
58  const uint8_t *IndexBytes = reinterpret_cast<const uint8_t *>(&TI);
59  BytesToHash = makeArrayRef(IndexBytes, sizeof(TypeIndex));
60  } else {
61  if (TI.toArrayIndex() >= Prev.size() ||
62  Prev[TI.toArrayIndex()].empty()) {
63  // There are references to yet-unhashed records. Suspend hashing for
64  // this record until all the other records are processed.
65  return {};
66  }
67  BytesToHash = Prev[TI.toArrayIndex()].Hash;
68  }
69  S.update(BytesToHash);
70  }
71 
72  Off = Ref.Offset + Ref.Count * sizeof(TypeIndex);
73  }
74 
75  // Don't forget to add in any trailing bytes.
76  auto TrailingBytes = RecordData.drop_front(Off);
77  S.update(TrailingBytes);
78 
79  return {S.final().take_back(8)};
80 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::hash_value
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4827
llvm::codeview::RecordPrefix
Definition: RecordSerialization.h:33
llvm::codeview::LocallyHashedType
A locally hashed type represents a straightforward hash code of a serialized record.
Definition: TypeHashing.h:32
llvm::codeview::LocallyHashedType::RecordData
ArrayRef< uint8_t > RecordData
Definition: TypeHashing.h:34
llvm::DenseMapInfo
Definition: APInt.h:34
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::SHA1
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:26
EmptyHash
static std::array< uint8_t, 8 > EmptyHash
Definition: TypeHashing.cpp:20
llvm::codeview::GloballyHashedType::hashType
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:33
llvm::ArrayRef::slice
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:195
llvm::codeview::GloballyHashedType
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition: TypeHashing.h:77
llvm::ArrayRef::drop_front
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:204
TombstoneHash
static std::array< uint8_t, 8 > TombstoneHash
Definition: TypeHashing.cpp:22
llvm::ArrayRef< uint8_t >
TypeHashing.h
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::codeview::TiRefKind::IndexRef
@ IndexRef
llvm::ArrayRef::take_front
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:228
llvm::codeview::discoverTypeIndices
void discoverTypeIndices(ArrayRef< uint8_t > RecordData, SmallVectorImpl< TiReference > &Refs)
Definition: TypeIndexDiscovery.cpp:493
llvm::codeview::LocallyHashedType::hashType
static LocallyHashedType hashType(ArrayRef< uint8_t > RecordData)
Given a type, compute its local hash.
Definition: TypeHashing.cpp:28
SHA1.h
llvm::codeview
Definition: AppendingTypeTableBuilder.h:22
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::codeview::TypeIndex
A 32-bit type reference.
Definition: TypeIndex.h:95
TypeIndexDiscovery.h
llvm::hash_code
An opaque object representing a hash code.
Definition: Hashing.h:72