IR: Separate helpers for string operands, NFC
[oota-llvm.git] / lib / IR / DebugInfoMetadata.cpp
1 //===- 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
18 using namespace llvm;
19
20 MDLocation::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
34 static 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
40 static 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
46 MDLocation *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
74 /// \brief Get the MDString, or nullptr if the string is empty.
75 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
76   if (S.empty())
77     return nullptr;
78   return MDString::get(Context, S);
79 }
80
81 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
82                                             StringRef Header,
83                                             ArrayRef<Metadata *> DwarfOps,
84                                             StorageType Storage,
85                                             bool ShouldCreate) {
86   unsigned Hash = 0;
87   if (Storage == Uniqued) {
88     GenericDebugNodeInfo::KeyTy Key(Tag, Header, DwarfOps);
89     if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
90       return N;
91     if (!ShouldCreate)
92       return nullptr;
93     Hash = Key.getHash();
94   } else {
95     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
96   }
97
98   // Use a nullptr for empty headers.
99   Metadata *PreOps[] = {getCanonicalMDString(Context, Header)};
100   return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
101                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
102                    Storage, Context.pImpl->GenericDebugNodes);
103 }
104
105 void GenericDebugNode::recalculateHash() {
106   setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
107 }