LCOV - code coverage report
Current view: top level - lib/DebugInfo/CodeView - MergingTypeTableBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 36 51 70.6 %
Date: 2018-09-23 13:06:45 Functions: 10 15 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MergingTypeTableBuilder.cpp ----------------------------------------===//
       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             : #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
      11             : #include "llvm/ADT/ArrayRef.h"
      12             : #include "llvm/ADT/DenseSet.h"
      13             : #include "llvm/ADT/STLExtras.h"
      14             : #include "llvm/DebugInfo/CodeView/CodeView.h"
      15             : #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
      16             : #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
      17             : #include "llvm/DebugInfo/CodeView/TypeIndex.h"
      18             : #include "llvm/Support/Allocator.h"
      19             : #include "llvm/Support/BinaryByteStream.h"
      20             : #include "llvm/Support/BinaryStreamWriter.h"
      21             : #include "llvm/Support/Endian.h"
      22             : #include "llvm/Support/Error.h"
      23             : #include <algorithm>
      24             : #include <cassert>
      25             : #include <cstdint>
      26             : #include <cstring>
      27             : 
      28             : using namespace llvm;
      29             : using namespace llvm::codeview;
      30             : 
      31        2770 : TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
      32        5540 :   return TypeIndex::fromArrayIndex(SeenRecords.size());
      33             : }
      34             : 
      35        6086 : MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
      36        6086 :     : RecordStorage(Storage) {
      37             :   SeenRecords.reserve(4096);
      38        6086 : }
      39             : 
      40             : MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
      41             : 
      42         160 : Optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
      43         160 :   if (empty())
      44          96 :     return None;
      45             : 
      46          64 :   return TypeIndex(TypeIndex::FirstNonSimpleIndex);
      47             : }
      48             : 
      49        1165 : Optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
      50        1165 :   if (++Prev == nextTypeIndex())
      51          64 :     return None;
      52        1101 :   return Prev;
      53             : }
      54             : 
      55        1225 : CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
      56             :   CVType Type;
      57        2450 :   Type.RecordData = SeenRecords[Index.toArrayIndex()];
      58             :   const RecordPrefix *P =
      59        1225 :       reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
      60        1225 :   Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
      61        1225 :   return Type;
      62             : }
      63             : 
      64           0 : StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
      65           0 :   llvm_unreachable("Method not implemented");
      66             : }
      67             : 
      68           0 : bool MergingTypeTableBuilder::contains(TypeIndex Index) {
      69           0 :   if (Index.isSimple() || Index.isNoneType())
      70           0 :     return false;
      71             : 
      72           0 :   return Index.toArrayIndex() < SeenRecords.size();
      73             : }
      74             : 
      75         160 : uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
      76             : 
      77           0 : uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
      78             : 
      79           8 : ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
      80           8 :   return SeenRecords;
      81             : }
      82             : 
      83           0 : void MergingTypeTableBuilder::reset() {
      84           0 :   HashedRecords.clear();
      85             :   SeenRecords.clear();
      86           0 : }
      87             : 
      88        1234 : static inline ArrayRef<uint8_t> stabilize(BumpPtrAllocator &Alloc,
      89             :                                           ArrayRef<uint8_t> Data) {
      90             :   uint8_t *Stable = Alloc.Allocate<uint8_t>(Data.size());
      91        1234 :   memcpy(Stable, Data.data(), Data.size());
      92        1234 :   return makeArrayRef(Stable, Data.size());
      93             : }
      94             : 
      95        1605 : TypeIndex MergingTypeTableBuilder::insertRecordAs(hash_code Hash,
      96             :                                                   ArrayRef<uint8_t> &Record) {
      97             :   assert(Record.size() < UINT32_MAX && "Record too big");
      98             :   assert(Record.size() % 4 == 0 && "Record is not aligned to 4 bytes!");
      99             : 
     100        1605 :   LocallyHashedType WeakHash{Hash, Record};
     101        1605 :   auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
     102             : 
     103        1605 :   if (Result.second) {
     104        1234 :     ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
     105        1234 :     Result.first->first.RecordData = RecordData;
     106        1234 :     SeenRecords.push_back(RecordData);
     107             :   }
     108             : 
     109             :   // Update the caller's copy of Record to point a stable copy.
     110        1605 :   TypeIndex ActualTI = Result.first->second;
     111        3210 :   Record = SeenRecords[ActualTI.toArrayIndex()];
     112        1605 :   return ActualTI;
     113             : }
     114             : 
     115             : TypeIndex
     116        1605 : MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
     117        1605 :   return insertRecordAs(hash_value(Record), Record);
     118             : }
     119             : 
     120             : TypeIndex
     121           0 : MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
     122             :   TypeIndex TI;
     123           0 :   auto Fragments = Builder.end(nextTypeIndex());
     124             :   assert(!Fragments.empty());
     125           0 :   for (auto C : Fragments)
     126           0 :     TI = insertRecordBytes(C.RecordData);
     127           0 :   return TI;
     128             : }

Generated by: LCOV version 1.13