summaryrefslogtreecommitdiffstats
path: root/llvm/lib/IR/DebugInfoMetadata.cpp
blob: 66dcc8e80558b4312efba5be3008a767a9f75a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the debug info Metadata classes.
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/DebugInfoMetadata.h"
#include "LLVMContextImpl.h"
#include "MetadataImpl.h"

using namespace llvm;

MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
                       unsigned Column, ArrayRef<Metadata *> MDs)
    : MDNode(C, MDLocationKind, Storage, MDs) {
  assert((MDs.size() == 1 || MDs.size() == 2) &&
         "Expected a scope and optional inlined-at");

  // Set line and column.
  assert(Line < (1u << 24) && "Expected 24-bit line");
  assert(Column < (1u << 16) && "Expected 16-bit column");

  SubclassData32 = Line;
  SubclassData16 = Column;
}

static void adjustLine(unsigned &Line) {
  // Set to unknown on overflow.  Still use 24 bits for now.
  if (Line >= (1u << 24))
    Line = 0;
}

static void adjustColumn(unsigned &Column) {
  // Set to unknown on overflow.  We only have 16 bits to play with here.
  if (Column >= (1u << 16))
    Column = 0;
}

MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
                                unsigned Column, Metadata *Scope,
                                Metadata *InlinedAt, StorageType Storage,
                                bool ShouldCreate) {
  // Fixup line/column.
  adjustLine(Line);
  adjustColumn(Column);

  if (Storage == Uniqued) {
    if (auto *N =
            getUniqued(Context.pImpl->MDLocations,
                       MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
      return N;
    if (!ShouldCreate)
      return nullptr;
  } else {
    assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
  }

  SmallVector<Metadata *, 2> Ops;
  Ops.push_back(Scope);
  if (InlinedAt)
    Ops.push_back(InlinedAt);
  return storeImpl(new (Ops.size())
                       MDLocation(Context, Storage, Line, Column, Ops),
                   Storage, Context.pImpl->MDLocations);
}

/// \brief Get the MDString, or nullptr if the string is empty.
static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
  if (S.empty())
    return nullptr;
  return MDString::get(Context, S);
}

GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
                                            StringRef Header,
                                            ArrayRef<Metadata *> DwarfOps,
                                            StorageType Storage,
                                            bool ShouldCreate) {
  unsigned Hash = 0;
  if (Storage == Uniqued) {
    GenericDebugNodeInfo::KeyTy Key(Tag, Header, DwarfOps);
    if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
      return N;
    if (!ShouldCreate)
      return nullptr;
    Hash = Key.getHash();
  } else {
    assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
  }

  // Use a nullptr for empty headers.
  Metadata *PreOps[] = {getCanonicalMDString(Context, Header)};
  return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
                       Context, Storage, Hash, Tag, PreOps, DwarfOps),
                   Storage, Context.pImpl->GenericDebugNodes);
}

void GenericDebugNode::recalculateHash() {
  setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
}
OpenPOWER on IntegriCloud