blob: 3211a5bb66ddd59c3e9224a6f976858efc5495c2 [file] [log] [blame]
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +00001//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the debug info Metadata classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/DebugInfoMetadata.h"
14#include "LLVMContextImpl.h"
15#include "MetadataImpl.h"
David Blaikie2a813ef2018-08-23 22:35:58 +000016#include "llvm/ADT/SmallSet.h"
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000017#include "llvm/ADT/StringSwitch.h"
Andrew Ng03e35b62017-04-28 08:44:30 +000018#include "llvm/IR/DIBuilder.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000019#include "llvm/IR/Function.h"
Vedant Kumar2b881f52017-11-06 23:15:21 +000020#include "llvm/IR/Instructions.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000021
Mircea Trofinb53eeb62018-12-21 22:48:50 +000022#include <numeric>
23
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000024using namespace llvm;
25
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000026DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
Calixte Denizeteb7f6022018-09-20 08:53:06 +000027 unsigned Column, ArrayRef<Metadata *> MDs,
28 bool ImplicitCode)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000029 : MDNode(C, DILocationKind, Storage, MDs) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000030 assert((MDs.size() == 1 || MDs.size() == 2) &&
31 "Expected a scope and optional inlined-at");
32
33 // Set line and column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000034 assert(Column < (1u << 16) && "Expected 16-bit column");
35
36 SubclassData32 = Line;
37 SubclassData16 = Column;
Calixte Denizeteb7f6022018-09-20 08:53:06 +000038
39 setImplicitCode(ImplicitCode);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000040}
41
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000042static void adjustColumn(unsigned &Column) {
43 // Set to unknown on overflow. We only have 16 bits to play with here.
44 if (Column >= (1u << 16))
45 Column = 0;
46}
47
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000048DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000049 unsigned Column, Metadata *Scope,
Calixte Denizeteb7f6022018-09-20 08:53:06 +000050 Metadata *InlinedAt, bool ImplicitCode,
51 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +000052 // Fixup column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000053 adjustColumn(Column);
54
55 if (Storage == Uniqued) {
Calixte Denizeteb7f6022018-09-20 08:53:06 +000056 if (auto *N = getUniqued(Context.pImpl->DILocations,
57 DILocationInfo::KeyTy(Line, Column, Scope,
58 InlinedAt, ImplicitCode)))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000059 return N;
60 if (!ShouldCreate)
61 return nullptr;
62 } else {
63 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
64 }
65
66 SmallVector<Metadata *, 2> Ops;
67 Ops.push_back(Scope);
68 if (InlinedAt)
69 Ops.push_back(InlinedAt);
Calixte Denizeteb7f6022018-09-20 08:53:06 +000070 return storeImpl(new (Ops.size()) DILocation(Context, Storage, Line, Column,
71 Ops, ImplicitCode),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000072 Storage, Context.pImpl->DILocations);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000073}
74
Vedant Kumar65b0d4d2018-04-12 20:58:24 +000075const DILocation *DILocation::getMergedLocation(const DILocation *LocA,
David Blaikie2a813ef2018-08-23 22:35:58 +000076 const DILocation *LocB) {
Vedant Kumar2b881f52017-11-06 23:15:21 +000077 if (!LocA || !LocB)
78 return nullptr;
79
David Blaikie2a813ef2018-08-23 22:35:58 +000080 if (LocA == LocB)
Vedant Kumar2b881f52017-11-06 23:15:21 +000081 return LocA;
82
Vedant Kumar2b881f52017-11-06 23:15:21 +000083 SmallPtrSet<DILocation *, 5> InlinedLocationsA;
84 for (DILocation *L = LocA->getInlinedAt(); L; L = L->getInlinedAt())
85 InlinedLocationsA.insert(L);
David Blaikie2a813ef2018-08-23 22:35:58 +000086 SmallSet<std::pair<DIScope *, DILocation *>, 5> Locations;
87 DIScope *S = LocA->getScope();
88 DILocation *L = LocA->getInlinedAt();
89 while (S) {
90 Locations.insert(std::make_pair(S, L));
91 S = S->getScope().resolve();
92 if (!S && L) {
93 S = L->getScope();
94 L = L->getInlinedAt();
95 }
Vedant Kumar2b881f52017-11-06 23:15:21 +000096 }
David Blaikie2a813ef2018-08-23 22:35:58 +000097 const DILocation *Result = LocB;
98 S = LocB->getScope();
99 L = LocB->getInlinedAt();
100 while (S) {
101 if (Locations.count(std::make_pair(S, L)))
102 break;
103 S = S->getScope().resolve();
104 if (!S && L) {
105 S = L->getScope();
106 L = L->getInlinedAt();
107 }
108 }
Adrian Prantl4ddd0592018-08-24 23:30:57 +0000109
110 // If the two locations are irreconsilable, just pick one. This is misleading,
111 // but on the other hand, it's a "line 0" location.
112 if (!S || !isa<DILocalScope>(S))
113 S = LocA->getScope();
David Blaikie2a813ef2018-08-23 22:35:58 +0000114 return DILocation::get(Result->getContext(), 0, 0, S, L);
Vedant Kumar2b881f52017-11-06 23:15:21 +0000115}
116
Mircea Trofinb53eeb62018-12-21 22:48:50 +0000117Optional<unsigned> DILocation::encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI) {
118 SmallVector<unsigned, 3> Components = {BD, DF, CI};
119 uint64_t RemainingWork = 0U;
120 // We use RemainingWork to figure out if we have no remaining components to
121 // encode. For example: if BD != 0 but DF == 0 && CI == 0, we don't need to
122 // encode anything for the latter 2.
123 // Since any of the input components is at most 32 bits, their sum will be
124 // less than 34 bits, and thus RemainingWork won't overflow.
125 RemainingWork = std::accumulate(Components.begin(), Components.end(), RemainingWork);
126
127 int I = 0;
128 unsigned Ret = 0;
129 unsigned NextBitInsertionIndex = 0;
130 while (RemainingWork > 0) {
131 unsigned C = Components[I++];
132 RemainingWork -= C;
133 unsigned EC = encodeComponent(C);
134 Ret |= (EC << NextBitInsertionIndex);
135 NextBitInsertionIndex += encodingBits(C);
136 }
137
138 // Encoding may be unsuccessful because of overflow. We determine success by
139 // checking equivalence of components before & after encoding. Alternatively,
140 // we could determine Success during encoding, but the current alternative is
141 // simpler.
142 unsigned TBD, TDF, TCI = 0;
143 decodeDiscriminator(Ret, TBD, TDF, TCI);
144 if (TBD == BD && TDF == DF && TCI == CI)
145 return Ret;
146 return None;
147}
148
149void DILocation::decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
150 unsigned &CI) {
151 BD = getUnsignedFromPrefixEncoding(D);
152 DF = getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(D));
153 CI = getUnsignedFromPrefixEncoding(
154 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D)));
155}
156
157
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000158DINode::DIFlags DINode::getFlag(StringRef Flag) {
159 return StringSwitch<DIFlags>(Flag)
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000160#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
161#include "llvm/IR/DebugInfoFlags.def"
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000162 .Default(DINode::FlagZero);
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000163}
164
Mehdi Aminif42ec792016-10-01 05:57:50 +0000165StringRef DINode::getFlagString(DIFlags Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000166 switch (Flag) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000167#define HANDLE_DI_FLAG(ID, NAME) \
168 case Flag##NAME: \
169 return "DIFlag" #NAME;
170#include "llvm/IR/DebugInfoFlags.def"
171 }
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000172 return "";
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000173}
174
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000175DINode::DIFlags DINode::splitFlags(DIFlags Flags,
Leny Kholodov40c62352016-09-06 17:03:02 +0000176 SmallVectorImpl<DIFlags> &SplitFlags) {
Bob Haarman26a87bd2016-10-25 22:11:52 +0000177 // Flags that are packed together need to be specially handled, so
178 // that, for example, we emit "DIFlagPublic" and not
179 // "DIFlagPrivate | DIFlagProtected".
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000180 if (DIFlags A = Flags & FlagAccessibility) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000181 if (A == FlagPrivate)
182 SplitFlags.push_back(FlagPrivate);
183 else if (A == FlagProtected)
184 SplitFlags.push_back(FlagProtected);
185 else
186 SplitFlags.push_back(FlagPublic);
187 Flags &= ~A;
188 }
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000189 if (DIFlags R = Flags & FlagPtrToMemberRep) {
Reid Kleckner604105b2016-06-17 21:31:33 +0000190 if (R == FlagSingleInheritance)
191 SplitFlags.push_back(FlagSingleInheritance);
192 else if (R == FlagMultipleInheritance)
193 SplitFlags.push_back(FlagMultipleInheritance);
194 else
195 SplitFlags.push_back(FlagVirtualInheritance);
196 Flags &= ~R;
197 }
Bob Haarman26a87bd2016-10-25 22:11:52 +0000198 if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) {
199 Flags &= ~FlagIndirectVirtualBase;
200 SplitFlags.push_back(FlagIndirectVirtualBase);
201 }
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000202
203#define HANDLE_DI_FLAG(ID, NAME) \
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000204 if (DIFlags Bit = Flags & Flag##NAME) { \
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000205 SplitFlags.push_back(Bit); \
206 Flags &= ~Bit; \
207 }
208#include "llvm/IR/DebugInfoFlags.def"
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000209 return Flags;
210}
211
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000212DIScopeRef DIScope::getScope() const {
213 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000214 return T->getScope();
215
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000216 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000217 return SP->getScope();
218
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219 if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000220 return LB->getScope();
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000221
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000222 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000223 return NS->getScope();
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000224
Adrian Prantlab1243f2015-06-29 23:03:47 +0000225 if (auto *M = dyn_cast<DIModule>(this))
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000226 return M->getScope();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000227
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000228 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000229 "Unhandled type of scope.");
230 return nullptr;
231}
232
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000233StringRef DIScope::getName() const {
234 if (auto *T = dyn_cast<DIType>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000235 return T->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236 if (auto *SP = dyn_cast<DISubprogram>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000237 return SP->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238 if (auto *NS = dyn_cast<DINamespace>(this))
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000239 return NS->getName();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000240 if (auto *M = dyn_cast<DIModule>(this))
241 return M->getName();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000242 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
243 isa<DICompileUnit>(this)) &&
Duncan P. N. Exon Smithf0d81a52015-04-11 17:37:23 +0000244 "Unhandled type of scope.");
245 return "";
246}
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +0000247
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000248#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000249static bool isCanonical(const MDString *S) {
250 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000251}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +0000252#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +0000253
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000254GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
255 MDString *Header,
256 ArrayRef<Metadata *> DwarfOps,
257 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000258 unsigned Hash = 0;
259 if (Storage == Uniqued) {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000260 GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000261 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000262 return N;
263 if (!ShouldCreate)
264 return nullptr;
265 Hash = Key.getHash();
266 } else {
267 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
268 }
269
270 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +0000271 assert(isCanonical(Header) && "Expected canonical MDString");
272 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000273 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000274 Context, Storage, Hash, Tag, PreOps, DwarfOps),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000275 Storage, Context.pImpl->GenericDINodes);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000276}
277
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000278void GenericDINode::recalculateHash() {
279 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +0000280}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000281
282#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
283#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
284#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
285 do { \
286 if (Storage == Uniqued) { \
287 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
288 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
289 return N; \
290 if (!ShouldCreate) \
291 return nullptr; \
292 } else { \
293 assert(ShouldCreate && \
294 "Expected non-uniqued nodes to always be created"); \
295 } \
296 } while (false)
297#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
David Blaikie6662d6a2016-04-13 17:42:56 +0000298 return storeImpl(new (array_lengthof(OPS)) \
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000299 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
300 Storage, Context.pImpl->CLASS##s)
301#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
302 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
303 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000304#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
David Blaikie6662d6a2016-04-13 17:42:56 +0000305 return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000306 Storage, Context.pImpl->CLASS##s)
Adrian Prantl9d2f0192017-04-26 23:59:52 +0000307#define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \
308 return storeImpl(new (NUM_OPS) \
309 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
310 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000311
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000312DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000313 StorageType Storage, bool ShouldCreate) {
Sander de Smalenfdf40912018-01-24 09:56:07 +0000314 auto *CountNode = ConstantAsMetadata::get(
315 ConstantInt::getSigned(Type::getInt64Ty(Context), Count));
316 return getImpl(Context, CountNode, Lo, Storage, ShouldCreate);
317}
318
319DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode,
320 int64_t Lo, StorageType Storage,
321 bool ShouldCreate) {
322 DEFINE_GETIMPL_LOOKUP(DISubrange, (CountNode, Lo));
323 Metadata *Ops[] = { CountNode };
324 DEFINE_GETIMPL_STORE(DISubrange, (CountNode, Lo), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000325}
326
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000327DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000328 bool IsUnsigned, MDString *Name,
329 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000330 assert(isCanonical(Name) && "Expected canonical MDString");
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000331 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, IsUnsigned, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000332 Metadata *Ops[] = {Name};
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000333 DEFINE_GETIMPL_STORE(DIEnumerator, (Value, IsUnsigned), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000334}
335
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000336DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000337 MDString *Name, uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000338 uint32_t AlignInBits, unsigned Encoding,
Adrian Prantl55f42622018-08-14 19:35:34 +0000339 DIFlags Flags, StorageType Storage,
340 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000341 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000342 DEFINE_GETIMPL_LOOKUP(DIBasicType,
Adrian Prantl55f42622018-08-14 19:35:34 +0000343 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000344 Metadata *Ops[] = {nullptr, nullptr, Name};
Adrian Prantl55f42622018-08-14 19:35:34 +0000345 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding,
346 Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000347}
348
Vedant Kumar6379a622018-07-06 17:32:39 +0000349Optional<DIBasicType::Signedness> DIBasicType::getSignedness() const {
350 switch (getEncoding()) {
351 case dwarf::DW_ATE_signed:
352 case dwarf::DW_ATE_signed_char:
353 return Signedness::Signed;
354 case dwarf::DW_ATE_unsigned:
355 case dwarf::DW_ATE_unsigned_char:
356 return Signedness::Unsigned;
357 default:
358 return None;
359 }
360}
361
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000362DIDerivedType *DIDerivedType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000363 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000364 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000365 uint32_t AlignInBits, uint64_t OffsetInBits,
366 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData,
367 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000368 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000369 DEFINE_GETIMPL_LOOKUP(DIDerivedType,
370 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000371 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
372 ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000373 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
374 DEFINE_GETIMPL_STORE(
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000375 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
376 DWARFAddressSpace, Flags), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000377}
378
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000379DICompositeType *DICompositeType::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000380 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000381 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000382 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000383 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
Adrian Prantl8c599212018-02-06 23:45:59 +0000384 Metadata *TemplateParams, MDString *Identifier, Metadata *Discriminator,
385 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000386 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000387
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000388 // Keep this in sync with buildODRType.
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000389 DEFINE_GETIMPL_LOOKUP(
390 DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
391 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
Adrian Prantl8c599212018-02-06 23:45:59 +0000392 VTableHolder, TemplateParams, Identifier, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000393 Metadata *Ops[] = {File, Scope, Name, BaseType,
Adrian Prantl8c599212018-02-06 23:45:59 +0000394 Elements, VTableHolder, TemplateParams, Identifier,
395 Discriminator};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000396 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000397 AlignInBits, OffsetInBits, Flags),
398 Ops);
399}
400
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000401DICompositeType *DICompositeType::buildODRType(
402 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
403 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
Victor Leschuk197aa312016-10-18 14:31:22 +0000404 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000405 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
Adrian Prantl8c599212018-02-06 23:45:59 +0000406 Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator) {
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000407 assert(!Identifier.getString().empty() && "Expected valid identifier");
408 if (!Context.isODRUniquingDebugTypes())
409 return nullptr;
410 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
411 if (!CT)
412 return CT = DICompositeType::getDistinct(
413 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
414 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
Adrian Prantl8c599212018-02-06 23:45:59 +0000415 VTableHolder, TemplateParams, &Identifier, Discriminator);
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000416
417 // Only mutate CT if it's a forward declaration and the new operands aren't.
418 assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
419 if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
420 return CT;
421
422 // Mutate CT in place. Keep this in sync with getImpl.
423 CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
424 Flags);
425 Metadata *Ops[] = {File, Scope, Name, BaseType,
Adrian Prantl8c599212018-02-06 23:45:59 +0000426 Elements, VTableHolder, TemplateParams, &Identifier,
427 Discriminator};
Simon Pilgrim1ec7dc72016-05-02 16:45:02 +0000428 assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +0000429 "Mismatched number of operands");
430 for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
431 if (Ops[I] != CT->getOperand(I))
432 CT->setOperand(I, Ops[I]);
433 return CT;
434}
435
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +0000436DICompositeType *DICompositeType::getODRType(
437 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
438 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
Victor Leschuk197aa312016-10-18 14:31:22 +0000439 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000440 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
Adrian Prantl8c599212018-02-06 23:45:59 +0000441 Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator) {
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +0000442 assert(!Identifier.getString().empty() && "Expected valid identifier");
443 if (!Context.isODRUniquingDebugTypes())
444 return nullptr;
445 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
446 if (!CT)
447 CT = DICompositeType::getDistinct(
448 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
449 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
Adrian Prantl8c599212018-02-06 23:45:59 +0000450 TemplateParams, &Identifier, Discriminator);
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +0000451 return CT;
452}
453
454DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
455 MDString &Identifier) {
456 assert(!Identifier.getString().empty() && "Expected valid identifier");
457 if (!Context.isODRUniquingDebugTypes())
458 return nullptr;
459 return Context.pImpl->DITypeMap->lookup(&Identifier);
460}
461
Leny Kholodov40c62352016-09-06 17:03:02 +0000462DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
463 uint8_t CC, Metadata *TypeArray,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000464 StorageType Storage,
465 bool ShouldCreate) {
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000466 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
Duncan P. N. Exon Smithb9e045a2015-07-24 20:56:36 +0000467 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000468 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000469}
470
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000471// FIXME: Implement this string-enum correspondence with a .def file and macros,
472// so that the association is explicit rather than implied.
Scott Linder71603842018-02-12 19:45:54 +0000473static const char *ChecksumKindName[DIFile::CSK_Last] = {
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000474 "CSK_MD5",
475 "CSK_SHA1"
476};
477
Scott Linder71603842018-02-12 19:45:54 +0000478StringRef DIFile::getChecksumKindAsString(ChecksumKind CSKind) {
479 assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
480 // The first space was originally the CSK_None variant, which is now
481 // obsolete, but the space is still reserved in ChecksumKind, so we account
482 // for it here.
483 return ChecksumKindName[CSKind - 1];
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000484}
485
Scott Linder71603842018-02-12 19:45:54 +0000486Optional<DIFile::ChecksumKind> DIFile::getChecksumKind(StringRef CSKindStr) {
487 return StringSwitch<Optional<DIFile::ChecksumKind>>(CSKindStr)
488 .Case("CSK_MD5", DIFile::CSK_MD5)
489 .Case("CSK_SHA1", DIFile::CSK_SHA1)
490 .Default(None);
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000491}
492
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000493DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
Scott Linder71603842018-02-12 19:45:54 +0000494 MDString *Directory,
495 Optional<DIFile::ChecksumInfo<MDString *>> CS,
Scott Linder16c7bda2018-02-23 23:01:06 +0000496 Optional<MDString *> Source, StorageType Storage,
497 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000498 assert(isCanonical(Filename) && "Expected canonical MDString");
499 assert(isCanonical(Directory) && "Expected canonical MDString");
Scott Linder71603842018-02-12 19:45:54 +0000500 assert((!CS || isCanonical(CS->Value)) && "Expected canonical MDString");
Scott Linder16c7bda2018-02-23 23:01:06 +0000501 assert((!Source || isCanonical(*Source)) && "Expected canonical MDString");
502 DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CS, Source));
503 Metadata *Ops[] = {Filename, Directory, CS ? CS->Value : nullptr,
504 Source.getValueOr(nullptr)};
505 DEFINE_GETIMPL_STORE(DIFile, (CS, Source), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000506}
507
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000508DICompileUnit *DICompileUnit::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000509 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
510 MDString *Producer, bool IsOptimized, MDString *Flags,
511 unsigned RuntimeVersion, MDString *SplitDebugFilename,
512 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +0000513 Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
Dehao Chen0944a8c2017-02-01 22:45:09 +0000514 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
David Blaikiebb279112018-11-13 20:08:10 +0000515 unsigned NameTableKind, bool RangesBaseAddress, StorageType Storage,
516 bool ShouldCreate) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000517 assert(Storage != Uniqued && "Cannot unique DICompileUnit");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000518 assert(isCanonical(Producer) && "Expected canonical MDString");
519 assert(isCanonical(Flags) && "Expected canonical MDString");
520 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000521
Adrian Prantl75819ae2016-04-15 15:57:41 +0000522 Metadata *Ops[] = {
523 File, Producer, Flags, SplitDebugFilename,
524 EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
525 Macros};
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000526 return storeImpl(new (array_lengthof(Ops)) DICompileUnit(
527 Context, Storage, SourceLanguage, IsOptimized,
528 RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining,
David Blaikiebb279112018-11-13 20:08:10 +0000529 DebugInfoForProfiling, NameTableKind, RangesBaseAddress,
530 Ops),
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +0000531 Storage);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000532}
533
Adrian Prantlb939a252016-03-31 23:56:58 +0000534Optional<DICompileUnit::DebugEmissionKind>
535DICompileUnit::getEmissionKind(StringRef Str) {
536 return StringSwitch<Optional<DebugEmissionKind>>(Str)
537 .Case("NoDebug", NoDebug)
538 .Case("FullDebug", FullDebug)
539 .Case("LineTablesOnly", LineTablesOnly)
Alexey Bataevd4dd7212018-08-01 19:38:20 +0000540 .Case("DebugDirectivesOnly", DebugDirectivesOnly)
Adrian Prantlb939a252016-03-31 23:56:58 +0000541 .Default(None);
542}
543
David Blaikie66cf14d2018-08-16 21:29:55 +0000544Optional<DICompileUnit::DebugNameTableKind>
545DICompileUnit::getNameTableKind(StringRef Str) {
546 return StringSwitch<Optional<DebugNameTableKind>>(Str)
547 .Case("Default", DebugNameTableKind::Default)
548 .Case("GNU", DebugNameTableKind::GNU)
549 .Case("None", DebugNameTableKind::None)
550 .Default(None);
551}
552
Fangrui Song3c1b5db2018-07-06 19:26:00 +0000553const char *DICompileUnit::emissionKindString(DebugEmissionKind EK) {
Adrian Prantlb939a252016-03-31 23:56:58 +0000554 switch (EK) {
555 case NoDebug: return "NoDebug";
556 case FullDebug: return "FullDebug";
557 case LineTablesOnly: return "LineTablesOnly";
Alexey Bataev075412d2018-08-23 17:43:40 +0000558 case DebugDirectivesOnly: return "DebugDirectivesOnly";
Adrian Prantlb939a252016-03-31 23:56:58 +0000559 }
560 return nullptr;
561}
562
David Blaikie66cf14d2018-08-16 21:29:55 +0000563const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) {
564 switch (NTK) {
565 case DebugNameTableKind::Default:
566 return nullptr;
567 case DebugNameTableKind::GNU:
568 return "GNU";
569 case DebugNameTableKind::None:
570 return "None";
571 }
572 return nullptr;
573}
574
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000575DISubprogram *DILocalScope::getSubprogram() const {
576 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000577 return Block->getScope()->getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000578 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +0000579}
580
Amjad Abouda5ba9912016-04-21 16:58:49 +0000581DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const {
582 if (auto *File = dyn_cast<DILexicalBlockFile>(this))
583 return File->getScope()->getNonLexicalBlockFileScope();
584 return const_cast<DILocalScope *>(this);
585}
586
Paul Robinsonadcdc1b2018-11-28 21:14:32 +0000587DISubprogram::DISPFlags DISubprogram::getFlag(StringRef Flag) {
588 return StringSwitch<DISPFlags>(Flag)
589#define HANDLE_DISP_FLAG(ID, NAME) .Case("DISPFlag" #NAME, SPFlag##NAME)
590#include "llvm/IR/DebugInfoFlags.def"
591 .Default(SPFlagZero);
592}
593
594StringRef DISubprogram::getFlagString(DISPFlags Flag) {
595 switch (Flag) {
Paul Robinson49f51bc2018-11-29 21:13:51 +0000596 // Appease a warning.
597 case SPFlagVirtuality:
Paul Robinsonadcdc1b2018-11-28 21:14:32 +0000598 return "";
599#define HANDLE_DISP_FLAG(ID, NAME) \
600 case SPFlag##NAME: \
601 return "DISPFlag" #NAME;
602#include "llvm/IR/DebugInfoFlags.def"
603 }
604 return "";
605}
606
607DISubprogram::DISPFlags
608DISubprogram::splitFlags(DISPFlags Flags,
609 SmallVectorImpl<DISPFlags> &SplitFlags) {
610 // Multi-bit fields can require special handling. In our case, however, the
611 // only multi-bit field is virtuality, and all its values happen to be
612 // single-bit values, so the right behavior just falls out.
613#define HANDLE_DISP_FLAG(ID, NAME) \
614 if (DISPFlags Bit = Flags & SPFlag##NAME) { \
615 SplitFlags.push_back(Bit); \
616 Flags &= ~Bit; \
617 }
618#include "llvm/IR/DebugInfoFlags.def"
619 return Flags;
620}
621
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000622DISubprogram *DISubprogram::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000623 LLVMContext &Context, Metadata *Scope, MDString *Name,
624 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
Paul Robinsoncda54212018-11-19 18:29:28 +0000625 unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex,
626 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
Shiva Chen2c864552018-05-09 02:40:45 +0000627 Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000628 Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000629 assert(isCanonical(Name) && "Expected canonical MDString");
630 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Paul Robinsoncda54212018-11-19 18:29:28 +0000631 DEFINE_GETIMPL_LOOKUP(DISubprogram,
632 (Scope, Name, LinkageName, File, Line, Type, ScopeLine,
633 ContainingType, VirtualIndex, ThisAdjustment, Flags,
634 SPFlags, Unit, TemplateParams, Declaration,
635 RetainedNodes, ThrownTypes));
Adrian Prantl9d2f0192017-04-26 23:59:52 +0000636 SmallVector<Metadata *, 11> Ops = {
Shiva Chen2c864552018-05-09 02:40:45 +0000637 File, Scope, Name, LinkageName, Type, Unit,
638 Declaration, RetainedNodes, ContainingType, TemplateParams, ThrownTypes};
Adrian Prantl9d2f0192017-04-26 23:59:52 +0000639 if (!ThrownTypes) {
640 Ops.pop_back();
641 if (!TemplateParams) {
642 Ops.pop_back();
643 if (!ContainingType)
644 Ops.pop_back();
645 }
646 }
Paul Robinsoncda54212018-11-19 18:29:28 +0000647 DEFINE_GETIMPL_STORE_N(
648 DISubprogram,
649 (Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags, SPFlags), Ops,
650 Ops.size());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000651}
652
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000653bool DISubprogram::describes(const Function *F) const {
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000654 assert(F && "Invalid function");
Peter Collingbourned4bff302015-11-05 22:03:56 +0000655 if (F->getSubprogram() == this)
Duncan P. N. Exon Smith3c2d7042015-04-13 19:07:27 +0000656 return true;
657 StringRef Name = getLinkageName();
658 if (Name.empty())
659 Name = getName();
660 return F->getName() == Name;
661}
662
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000663DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000664 Metadata *File, unsigned Line,
665 unsigned Column, StorageType Storage,
666 bool ShouldCreate) {
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +0000667 // Fixup column.
668 adjustColumn(Column);
669
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000670 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000671 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000672 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000673 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000674}
675
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000676DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000677 Metadata *Scope, Metadata *File,
678 unsigned Discriminator,
679 StorageType Storage,
680 bool ShouldCreate) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000681 assert(Scope && "Expected scope");
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000682 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000683 Metadata *Ops[] = {File, Scope};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000684 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000685}
686
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000687DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
Adrian Prantlfed4f392017-04-28 22:25:46 +0000688 MDString *Name, bool ExportSymbols,
689 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000690 assert(isCanonical(Name) && "Expected canonical MDString");
Adrian Prantlfed4f392017-04-28 22:25:46 +0000691 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols));
692 // The nullptr is for DIScope's File operand. This should be refactored.
693 Metadata *Ops[] = {nullptr, Scope, Name};
694 DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000695}
696
Adrian Prantlab1243f2015-06-29 23:03:47 +0000697DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
698 MDString *Name, MDString *ConfigurationMacros,
699 MDString *IncludePath, MDString *ISysRoot,
700 StorageType Storage, bool ShouldCreate) {
701 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000702 DEFINE_GETIMPL_LOOKUP(
703 DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
Adrian Prantlab1243f2015-06-29 23:03:47 +0000704 Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
705 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
706}
707
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000708DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000709 MDString *Name,
710 Metadata *Type,
711 StorageType Storage,
712 bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000713 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000714 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000715 Metadata *Ops[] = {Name, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000716 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000717}
718
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000719DITemplateValueParameter *DITemplateValueParameter::getImpl(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000720 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
721 Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000722 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000723 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000724 Metadata *Ops[] = {Name, Type, Value};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000725 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000726}
727
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000728DIGlobalVariable *
729DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000730 MDString *LinkageName, Metadata *File, unsigned Line,
731 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000732 Metadata *StaticDataMemberDeclaration,
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000733 Metadata *TemplateParams, uint32_t AlignInBits,
734 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000735 assert(isCanonical(Name) && "Expected canonical MDString");
736 assert(isCanonical(LinkageName) && "Expected canonical MDString");
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000737 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable, (Scope, Name, LinkageName, File, Line,
738 Type, IsLocalToUnit, IsDefinition,
739 StaticDataMemberDeclaration,
740 TemplateParams, AlignInBits));
741 Metadata *Ops[] = {Scope,
742 Name,
743 File,
744 Type,
745 Name,
746 LinkageName,
747 StaticDataMemberDeclaration,
748 TemplateParams};
Victor Leschuk2ede1262016-10-20 00:13:12 +0000749 DEFINE_GETIMPL_STORE(DIGlobalVariable,
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000750 (Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000751}
752
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000753DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
754 MDString *Name, Metadata *File,
755 unsigned Line, Metadata *Type,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000756 unsigned Arg, DIFlags Flags,
Victor Leschuka37660c2016-10-26 21:32:29 +0000757 uint32_t AlignInBits,
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000758 StorageType Storage,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000759 bool ShouldCreate) {
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +0000760 // 64K ought to be enough for any frontend.
761 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000762
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +0000763 assert(Scope && "Expected scope");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000764 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000765 DEFINE_GETIMPL_LOOKUP(DILocalVariable,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000766 (Scope, Name, File, Line, Type, Arg, Flags,
767 AlignInBits));
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000768 Metadata *Ops[] = {Scope, Name, File, Type};
Victor Leschuk2ede1262016-10-20 00:13:12 +0000769 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000770}
771
Adrian Prantl3e0e1d02017-11-28 00:57:51 +0000772Optional<uint64_t> DIVariable::getSizeInBits() const {
773 // This is used by the Verifier so be mindful of broken types.
774 const Metadata *RawType = getRawType();
775 while (RawType) {
776 // Try to get the size directly.
777 if (auto *T = dyn_cast<DIType>(RawType))
778 if (uint64_t Size = T->getSizeInBits())
779 return Size;
780
781 if (auto *DT = dyn_cast<DIDerivedType>(RawType)) {
782 // Look at the base type.
783 RawType = DT->getRawBaseType();
784 continue;
785 }
786
787 // Missing type or size.
788 break;
789 }
790
791 // Fail gracefully.
792 return None;
793}
794
Shiva Chen2c864552018-05-09 02:40:45 +0000795DILabel *DILabel::getImpl(LLVMContext &Context, Metadata *Scope,
796 MDString *Name, Metadata *File, unsigned Line,
797 StorageType Storage,
798 bool ShouldCreate) {
799 assert(Scope && "Expected scope");
800 assert(isCanonical(Name) && "Expected canonical MDString");
801 DEFINE_GETIMPL_LOOKUP(DILabel,
802 (Scope, Name, File, Line));
803 Metadata *Ops[] = {Scope, Name, File};
804 DEFINE_GETIMPL_STORE(DILabel, (Line), Ops);
805}
806
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000807DIExpression *DIExpression::getImpl(LLVMContext &Context,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000808 ArrayRef<uint64_t> Elements,
809 StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000810 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
811 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000812}
813
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000814unsigned DIExpression::ExprOperand::getSize() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000815 switch (getOp()) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000816 case dwarf::DW_OP_LLVM_fragment:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000817 return 3;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000818 case dwarf::DW_OP_constu:
Florian Hahnc9c403c2017-06-13 16:54:44 +0000819 case dwarf::DW_OP_plus_uconst:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000820 return 2;
821 default:
822 return 1;
823 }
824}
825
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000826bool DIExpression::isValid() const {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000827 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
828 // Check that there's space for the operand.
829 if (I->get() + I->getSize() > E->get())
830 return false;
831
832 // Check that the operand is valid.
833 switch (I->getOp()) {
834 default:
835 return false;
Adrian Prantl941fa752016-12-05 18:04:47 +0000836 case dwarf::DW_OP_LLVM_fragment:
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000837 // A fragment operator must appear at the end.
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000838 return I->get() + I->getSize() == E->get();
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000839 case dwarf::DW_OP_stack_value: {
840 // Must be the last one or followed by a DW_OP_LLVM_fragment.
841 if (I->get() + I->getSize() == E->get())
842 break;
843 auto J = I;
844 if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment)
845 return false;
846 break;
847 }
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000848 case dwarf::DW_OP_swap: {
849 // Must be more than one implicit element on the stack.
850
851 // FIXME: A better way to implement this would be to add a local variable
852 // that keeps track of the stack depth and introduce something like a
853 // DW_LLVM_OP_implicit_location as a placeholder for the location this
854 // DIExpression is attached to, or else pass the number of implicit stack
855 // elements into isValid.
856 if (getNumElements() == 1)
857 return false;
858 break;
859 }
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000860 case dwarf::DW_OP_constu:
Florian Hahnc9c403c2017-06-13 16:54:44 +0000861 case dwarf::DW_OP_plus_uconst:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000862 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000863 case dwarf::DW_OP_minus:
Strahinja Petrovic29202f62017-09-21 10:04:02 +0000864 case dwarf::DW_OP_mul:
Vedant Kumar4011c262018-02-13 01:09:52 +0000865 case dwarf::DW_OP_div:
866 case dwarf::DW_OP_mod:
Vedant Kumar04386d82018-02-09 19:19:55 +0000867 case dwarf::DW_OP_or:
Petar Jovanovic17689572018-02-14 13:10:35 +0000868 case dwarf::DW_OP_and:
Vedant Kumar96b7dc02018-02-13 01:09:46 +0000869 case dwarf::DW_OP_xor:
Vedant Kumar31ec3562018-02-13 01:09:49 +0000870 case dwarf::DW_OP_shl:
871 case dwarf::DW_OP_shr:
872 case dwarf::DW_OP_shra:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000873 case dwarf::DW_OP_deref:
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000874 case dwarf::DW_OP_xderef:
Vedant Kumar6379a622018-07-06 17:32:39 +0000875 case dwarf::DW_OP_lit0:
876 case dwarf::DW_OP_not:
877 case dwarf::DW_OP_dup:
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000878 break;
879 }
880 }
881 return true;
882}
883
Adrian Prantl49797ca2016-12-22 05:27:12 +0000884Optional<DIExpression::FragmentInfo>
885DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
886 for (auto I = Start; I != End; ++I)
887 if (I->getOp() == dwarf::DW_OP_LLVM_fragment) {
888 DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
889 return Info;
890 }
891 return None;
Duncan P. N. Exon Smith86cc3322015-04-07 03:49:59 +0000892}
893
Andrew Ng03e35b62017-04-28 08:44:30 +0000894void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops,
895 int64_t Offset) {
896 if (Offset > 0) {
Florian Hahnffc498d2017-06-14 13:14:38 +0000897 Ops.push_back(dwarf::DW_OP_plus_uconst);
Andrew Ng03e35b62017-04-28 08:44:30 +0000898 Ops.push_back(Offset);
899 } else if (Offset < 0) {
Florian Hahnffc498d2017-06-14 13:14:38 +0000900 Ops.push_back(dwarf::DW_OP_constu);
Andrew Ng03e35b62017-04-28 08:44:30 +0000901 Ops.push_back(-Offset);
Florian Hahnffc498d2017-06-14 13:14:38 +0000902 Ops.push_back(dwarf::DW_OP_minus);
Andrew Ng03e35b62017-04-28 08:44:30 +0000903 }
904}
905
Reid Klecknerb5fced72017-05-09 19:59:29 +0000906bool DIExpression::extractIfOffset(int64_t &Offset) const {
907 if (getNumElements() == 0) {
908 Offset = 0;
909 return true;
910 }
Florian Hahnffc498d2017-06-14 13:14:38 +0000911
912 if (getNumElements() == 2 && Elements[0] == dwarf::DW_OP_plus_uconst) {
Reid Klecknerb5fced72017-05-09 19:59:29 +0000913 Offset = Elements[1];
914 return true;
915 }
Florian Hahnffc498d2017-06-14 13:14:38 +0000916
917 if (getNumElements() == 3 && Elements[0] == dwarf::DW_OP_constu) {
918 if (Elements[2] == dwarf::DW_OP_plus) {
919 Offset = Elements[1];
920 return true;
921 }
922 if (Elements[2] == dwarf::DW_OP_minus) {
923 Offset = -Elements[1];
924 return true;
925 }
Reid Klecknerb5fced72017-05-09 19:59:29 +0000926 }
Florian Hahnffc498d2017-06-14 13:14:38 +0000927
Reid Klecknerb5fced72017-05-09 19:59:29 +0000928 return false;
929}
930
Adrian Prantld1317012017-12-08 21:58:18 +0000931DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore,
932 int64_t Offset, bool DerefAfter,
933 bool StackValue) {
Andrew Ng03e35b62017-04-28 08:44:30 +0000934 SmallVector<uint64_t, 8> Ops;
Adrian Prantld1317012017-12-08 21:58:18 +0000935 if (DerefBefore)
Andrew Ng03e35b62017-04-28 08:44:30 +0000936 Ops.push_back(dwarf::DW_OP_deref);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000937
Adrian Prantld1317012017-12-08 21:58:18 +0000938 appendOffset(Ops, Offset);
939 if (DerefAfter)
940 Ops.push_back(dwarf::DW_OP_deref);
941
Adrian Prantl210a29d2018-04-27 21:41:36 +0000942 return prependOpcodes(Expr, Ops, StackValue);
Vedant Kumar04386d82018-02-09 19:19:55 +0000943}
944
Adrian Prantl210a29d2018-04-27 21:41:36 +0000945DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr,
946 SmallVectorImpl<uint64_t> &Ops,
947 bool StackValue) {
Vedant Kumar8a368082018-07-06 21:06:20 +0000948 assert(Expr && "Can't prepend ops to this expression");
949
Bjorn Pettersson8dd6cf72018-07-03 11:29:00 +0000950 // If there are no ops to prepend, do not even add the DW_OP_stack_value.
951 if (Ops.empty())
952 StackValue = false;
Vedant Kumar8a368082018-07-06 21:06:20 +0000953 for (auto Op : Expr->expr_ops()) {
954 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
955 if (StackValue) {
956 if (Op.getOp() == dwarf::DW_OP_stack_value)
957 StackValue = false;
958 else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
959 Ops.push_back(dwarf::DW_OP_stack_value);
960 StackValue = false;
Andrew Ng03e35b62017-04-28 08:44:30 +0000961 }
Andrew Ng03e35b62017-04-28 08:44:30 +0000962 }
Vedant Kumar71c7c432018-07-06 21:06:21 +0000963 Op.appendToVector(Ops);
Vedant Kumar8a368082018-07-06 21:06:20 +0000964 }
Andrew Ng03e35b62017-04-28 08:44:30 +0000965 if (StackValue)
966 Ops.push_back(dwarf::DW_OP_stack_value);
Adrian Prantl109b2362017-04-28 17:51:05 +0000967 return DIExpression::get(Expr->getContext(), Ops);
Andrew Ng03e35b62017-04-28 08:44:30 +0000968}
969
Vedant Kumarb572f642018-07-26 20:56:53 +0000970DIExpression *DIExpression::append(const DIExpression *Expr,
971 ArrayRef<uint64_t> Ops) {
972 assert(Expr && !Ops.empty() && "Can't append ops to this expression");
973
974 // Copy Expr's current op list.
975 SmallVector<uint64_t, 16> NewOps;
976 for (auto Op : Expr->expr_ops()) {
977 // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}.
978 if (Op.getOp() == dwarf::DW_OP_stack_value ||
979 Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
980 NewOps.append(Ops.begin(), Ops.end());
981
982 // Ensure that the new opcodes are only appended once.
983 Ops = None;
984 }
985 Op.appendToVector(NewOps);
986 }
987
988 NewOps.append(Ops.begin(), Ops.end());
989 return DIExpression::get(Expr->getContext(), NewOps);
990}
991
Vedant Kumar6379a622018-07-06 17:32:39 +0000992DIExpression *DIExpression::appendToStack(const DIExpression *Expr,
993 ArrayRef<uint64_t> Ops) {
994 assert(Expr && !Ops.empty() && "Can't append ops to this expression");
Vedant Kumarb572f642018-07-26 20:56:53 +0000995 assert(none_of(Ops,
996 [](uint64_t Op) {
997 return Op == dwarf::DW_OP_stack_value ||
998 Op == dwarf::DW_OP_LLVM_fragment;
999 }) &&
1000 "Can't append this op");
Vedant Kumar6379a622018-07-06 17:32:39 +00001001
1002 // Append a DW_OP_deref after Expr's current op list if it's non-empty and
1003 // has no DW_OP_stack_value.
1004 //
1005 // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?.
1006 Optional<FragmentInfo> FI = Expr->getFragmentInfo();
1007 unsigned DropUntilStackValue = FI.hasValue() ? 3 : 0;
Vedant Kumarb572f642018-07-26 20:56:53 +00001008 ArrayRef<uint64_t> ExprOpsBeforeFragment =
1009 Expr->getElements().drop_back(DropUntilStackValue);
1010 bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) &&
1011 (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value);
1012 bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty();
Vedant Kumar6379a622018-07-06 17:32:39 +00001013
Vedant Kumarb572f642018-07-26 20:56:53 +00001014 // Append a DW_OP_deref after Expr's current op list if needed, then append
1015 // the new ops, and finally ensure that a single DW_OP_stack_value is present.
Vedant Kumar6379a622018-07-06 17:32:39 +00001016 SmallVector<uint64_t, 16> NewOps;
Vedant Kumar6379a622018-07-06 17:32:39 +00001017 if (NeedsDeref)
1018 NewOps.push_back(dwarf::DW_OP_deref);
1019 NewOps.append(Ops.begin(), Ops.end());
Vedant Kumarb572f642018-07-26 20:56:53 +00001020 if (NeedsStackValue)
1021 NewOps.push_back(dwarf::DW_OP_stack_value);
1022 return DIExpression::append(Expr, NewOps);
Vedant Kumar6379a622018-07-06 17:32:39 +00001023}
1024
Adrian Prantl25a09dd2017-11-07 00:45:34 +00001025Optional<DIExpression *> DIExpression::createFragmentExpression(
1026 const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) {
Adrian Prantlb192b542017-08-30 20:04:17 +00001027 SmallVector<uint64_t, 8> Ops;
1028 // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment.
1029 if (Expr) {
1030 for (auto Op : Expr->expr_ops()) {
Adrian Prantl25a09dd2017-11-07 00:45:34 +00001031 switch (Op.getOp()) {
1032 default: break;
1033 case dwarf::DW_OP_plus:
1034 case dwarf::DW_OP_minus:
1035 // We can't safely split arithmetic into multiple fragments because we
1036 // can't express carry-over between fragments.
1037 //
1038 // FIXME: We *could* preserve the lowest fragment of a constant offset
1039 // operation if the offset fits into SizeInBits.
1040 return None;
1041 case dwarf::DW_OP_LLVM_fragment: {
Adrian Prantlb192b542017-08-30 20:04:17 +00001042 // Make the new offset point into the existing fragment.
1043 uint64_t FragmentOffsetInBits = Op.getArg(0);
Bjorn Pettersson5479ad22018-05-03 17:04:21 +00001044 uint64_t FragmentSizeInBits = Op.getArg(1);
1045 (void)FragmentSizeInBits;
1046 assert((OffsetInBits + SizeInBits <= FragmentSizeInBits) &&
Adrian Prantlb192b542017-08-30 20:04:17 +00001047 "new fragment outside of original fragment");
1048 OffsetInBits += FragmentOffsetInBits;
Adrian Prantl25a09dd2017-11-07 00:45:34 +00001049 continue;
1050 }
Adrian Prantlb192b542017-08-30 20:04:17 +00001051 }
Vedant Kumar71c7c432018-07-06 21:06:21 +00001052 Op.appendToVector(Ops);
Adrian Prantlb192b542017-08-30 20:04:17 +00001053 }
1054 }
1055 Ops.push_back(dwarf::DW_OP_LLVM_fragment);
1056 Ops.push_back(OffsetInBits);
1057 Ops.push_back(SizeInBits);
1058 return DIExpression::get(Expr->getContext(), Ops);
1059}
1060
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001061bool DIExpression::isConstant() const {
1062 // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?.
1063 if (getNumElements() != 3 && getNumElements() != 6)
1064 return false;
1065 if (getElement(0) != dwarf::DW_OP_constu ||
1066 getElement(2) != dwarf::DW_OP_stack_value)
1067 return false;
1068 if (getNumElements() == 6 && getElement(3) != dwarf::DW_OP_LLVM_fragment)
1069 return false;
1070 return true;
1071}
1072
1073DIGlobalVariableExpression *
1074DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable,
1075 Metadata *Expression, StorageType Storage,
1076 bool ShouldCreate) {
1077 DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression));
1078 Metadata *Ops[] = {Variable, Expression};
1079 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops);
1080}
1081
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001082DIObjCProperty *DIObjCProperty::getImpl(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001083 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
1084 MDString *GetterName, MDString *SetterName, unsigned Attributes,
1085 Metadata *Type, StorageType Storage, bool ShouldCreate) {
1086 assert(isCanonical(Name) && "Expected canonical MDString");
1087 assert(isCanonical(GetterName) && "Expected canonical MDString");
1088 assert(isCanonical(SetterName) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001089 DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
1090 SetterName, Attributes, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001091 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001092 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001093}
1094
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001095DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001096 Metadata *Scope, Metadata *Entity,
Adrian Prantld63bfd22017-07-19 00:09:54 +00001097 Metadata *File, unsigned Line,
1098 MDString *Name, StorageType Storage,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001099 bool ShouldCreate) {
1100 assert(isCanonical(Name) && "Expected canonical MDString");
Adrian Prantld63bfd22017-07-19 00:09:54 +00001101 DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
1102 (Tag, Scope, Entity, File, Line, Name));
1103 Metadata *Ops[] = {Scope, Entity, Name, File};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001104 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001105}
Amjad Abouda9bcf162015-12-10 12:56:35 +00001106
1107DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
1108 unsigned Line, MDString *Name, MDString *Value,
1109 StorageType Storage, bool ShouldCreate) {
1110 assert(isCanonical(Name) && "Expected canonical MDString");
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001111 DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
Amjad Abouda9bcf162015-12-10 12:56:35 +00001112 Metadata *Ops[] = { Name, Value };
1113 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
1114}
1115
1116DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
1117 unsigned Line, Metadata *File,
1118 Metadata *Elements, StorageType Storage,
1119 bool ShouldCreate) {
1120 DEFINE_GETIMPL_LOOKUP(DIMacroFile,
1121 (MIType, Line, File, Elements));
1122 Metadata *Ops[] = { File, Elements };
1123 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
1124}