1*06f32e7eSjoerg //===- TypeHashing.cpp -------------------------------------------*- C++-*-===//
2*06f32e7eSjoerg //
3*06f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*06f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
5*06f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*06f32e7eSjoerg //
7*06f32e7eSjoerg //===----------------------------------------------------------------------===//
8*06f32e7eSjoerg 
9*06f32e7eSjoerg #include "llvm/DebugInfo/CodeView/TypeHashing.h"
10*06f32e7eSjoerg 
11*06f32e7eSjoerg #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
12*06f32e7eSjoerg #include "llvm/Support/SHA1.h"
13*06f32e7eSjoerg 
14*06f32e7eSjoerg using namespace llvm;
15*06f32e7eSjoerg using namespace llvm::codeview;
16*06f32e7eSjoerg 
17*06f32e7eSjoerg LocallyHashedType DenseMapInfo<LocallyHashedType>::Empty{0, {}};
18*06f32e7eSjoerg LocallyHashedType DenseMapInfo<LocallyHashedType>::Tombstone{hash_code(-1), {}};
19*06f32e7eSjoerg 
20*06f32e7eSjoerg static std::array<uint8_t, 8> EmptyHash = {
21*06f32e7eSjoerg     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
22*06f32e7eSjoerg static std::array<uint8_t, 8> TombstoneHash = {
23*06f32e7eSjoerg     {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
24*06f32e7eSjoerg 
25*06f32e7eSjoerg GloballyHashedType DenseMapInfo<GloballyHashedType>::Empty{EmptyHash};
26*06f32e7eSjoerg GloballyHashedType DenseMapInfo<GloballyHashedType>::Tombstone{TombstoneHash};
27*06f32e7eSjoerg 
hashType(ArrayRef<uint8_t> RecordData)28*06f32e7eSjoerg LocallyHashedType LocallyHashedType::hashType(ArrayRef<uint8_t> RecordData) {
29*06f32e7eSjoerg   return {llvm::hash_value(RecordData), RecordData};
30*06f32e7eSjoerg }
31*06f32e7eSjoerg 
32*06f32e7eSjoerg GloballyHashedType
hashType(ArrayRef<uint8_t> RecordData,ArrayRef<GloballyHashedType> PreviousTypes,ArrayRef<GloballyHashedType> PreviousIds)33*06f32e7eSjoerg GloballyHashedType::hashType(ArrayRef<uint8_t> RecordData,
34*06f32e7eSjoerg                              ArrayRef<GloballyHashedType> PreviousTypes,
35*06f32e7eSjoerg                              ArrayRef<GloballyHashedType> PreviousIds) {
36*06f32e7eSjoerg   SmallVector<TiReference, 4> Refs;
37*06f32e7eSjoerg   discoverTypeIndices(RecordData, Refs);
38*06f32e7eSjoerg   SHA1 S;
39*06f32e7eSjoerg   S.init();
40*06f32e7eSjoerg   uint32_t Off = 0;
41*06f32e7eSjoerg   S.update(RecordData.take_front(sizeof(RecordPrefix)));
42*06f32e7eSjoerg   RecordData = RecordData.drop_front(sizeof(RecordPrefix));
43*06f32e7eSjoerg   for (const auto &Ref : Refs) {
44*06f32e7eSjoerg     // Hash any data that comes before this TiRef.
45*06f32e7eSjoerg     uint32_t PreLen = Ref.Offset - Off;
46*06f32e7eSjoerg     ArrayRef<uint8_t> PreData = RecordData.slice(Off, PreLen);
47*06f32e7eSjoerg     S.update(PreData);
48*06f32e7eSjoerg     auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
49*06f32e7eSjoerg 
50*06f32e7eSjoerg     auto RefData = RecordData.slice(Ref.Offset, Ref.Count * sizeof(TypeIndex));
51*06f32e7eSjoerg     // For each type index referenced, add in the previously computed hash
52*06f32e7eSjoerg     // value of that type.
53*06f32e7eSjoerg     ArrayRef<TypeIndex> Indices(
54*06f32e7eSjoerg         reinterpret_cast<const TypeIndex *>(RefData.data()), Ref.Count);
55*06f32e7eSjoerg     for (TypeIndex TI : Indices) {
56*06f32e7eSjoerg       ArrayRef<uint8_t> BytesToHash;
57*06f32e7eSjoerg       if (TI.isSimple() || TI.isNoneType()) {
58*06f32e7eSjoerg         const uint8_t *IndexBytes = reinterpret_cast<const uint8_t *>(&TI);
59*06f32e7eSjoerg         BytesToHash = makeArrayRef(IndexBytes, sizeof(TypeIndex));
60*06f32e7eSjoerg       } else {
61*06f32e7eSjoerg         if (TI.toArrayIndex() >= Prev.size() ||
62*06f32e7eSjoerg             Prev[TI.toArrayIndex()].empty()) {
63*06f32e7eSjoerg           // There are references to yet-unhashed records. Suspend hashing for
64*06f32e7eSjoerg           // this record until all the other records are processed.
65*06f32e7eSjoerg           return {};
66*06f32e7eSjoerg         }
67*06f32e7eSjoerg         BytesToHash = Prev[TI.toArrayIndex()].Hash;
68*06f32e7eSjoerg       }
69*06f32e7eSjoerg       S.update(BytesToHash);
70*06f32e7eSjoerg     }
71*06f32e7eSjoerg 
72*06f32e7eSjoerg     Off = Ref.Offset + Ref.Count * sizeof(TypeIndex);
73*06f32e7eSjoerg   }
74*06f32e7eSjoerg 
75*06f32e7eSjoerg   // Don't forget to add in any trailing bytes.
76*06f32e7eSjoerg   auto TrailingBytes = RecordData.drop_front(Off);
77*06f32e7eSjoerg   S.update(TrailingBytes);
78*06f32e7eSjoerg 
79*06f32e7eSjoerg   return {S.final().take_back(8)};
80*06f32e7eSjoerg }
81