blob: 21a95f9f9fc6ba997633c9e6a12b2971b8e7d53b [file] [log] [blame]
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +00001//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
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// This file implements the debug info Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/DebugInfoMetadata.h"
15#include "LLVMContextImpl.h"
16#include "MetadataImpl.h"
17
18using namespace llvm;
19
20MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
21 unsigned Column, ArrayRef<Metadata *> MDs)
22 : MDNode(C, MDLocationKind, Storage, MDs) {
23 assert((MDs.size() == 1 || MDs.size() == 2) &&
24 "Expected a scope and optional inlined-at");
25
26 // Set line and column.
27 assert(Line < (1u << 24) && "Expected 24-bit line");
28 assert(Column < (1u << 16) && "Expected 16-bit column");
29
30 SubclassData32 = Line;
31 SubclassData16 = Column;
32}
33
34static void adjustLine(unsigned &Line) {
35 // Set to unknown on overflow. Still use 24 bits for now.
36 if (Line >= (1u << 24))
37 Line = 0;
38}
39
40static void adjustColumn(unsigned &Column) {
41 // Set to unknown on overflow. We only have 16 bits to play with here.
42 if (Column >= (1u << 16))
43 Column = 0;
44}
45
46MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
47 unsigned Column, Metadata *Scope,
48 Metadata *InlinedAt, StorageType Storage,
49 bool ShouldCreate) {
50 // Fixup line/column.
51 adjustLine(Line);
52 adjustColumn(Column);
53
54 if (Storage == Uniqued) {
55 if (auto *N =
56 getUniqued(Context.pImpl->MDLocations,
57 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
58 return N;
59 if (!ShouldCreate)
60 return nullptr;
61 } else {
62 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
63 }
64
65 SmallVector<Metadata *, 2> Ops;
66 Ops.push_back(Scope);
67 if (InlinedAt)
68 Ops.push_back(InlinedAt);
69 return storeImpl(new (Ops.size())
70 MDLocation(Context, Storage, Line, Column, Ops),
71 Storage, Context.pImpl->MDLocations);
72}
73
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000074static StringRef getString(const MDString *S) {
75 if (S)
76 return S->getString();
77 return StringRef();
78}
79
80static bool isCanonical(const MDString *S) {
81 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000082}
83
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000084GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000085 MDString *Header,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000086 ArrayRef<Metadata *> DwarfOps,
87 StorageType Storage,
88 bool ShouldCreate) {
89 unsigned Hash = 0;
90 if (Storage == Uniqued) {
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000091 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000092 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
93 return N;
94 if (!ShouldCreate)
95 return nullptr;
96 Hash = Key.getHash();
97 } else {
98 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
99 }
100
101 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000102 assert(isCanonical(Header) && "Expected canonical MDString");
103 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000104 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
105 Context, Storage, Hash, Tag, PreOps, DwarfOps),
106 Storage, Context.pImpl->GenericDebugNodes);
107}
108
109void GenericDebugNode::recalculateHash() {
110 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
111}