| 1 | //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===// |
| 2 | // |
| 3 | // 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 |
| 6 | // |
| 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" |
| 16 | #include "llvm/ADT/SetVector.h" |
| 17 | #include "llvm/ADT/StringSwitch.h" |
| 18 | #include "llvm/BinaryFormat/Dwarf.h" |
| 19 | #include "llvm/IR/DebugProgramInstruction.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/Type.h" |
| 23 | #include "llvm/IR/Value.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | |
| 27 | #include <numeric> |
| 28 | #include <optional> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace llvm { |
| 33 | // Use FS-AFDO discriminator. |
| 34 | cl::opt<bool> EnableFSDiscriminator( |
| 35 | "enable-fs-discriminator" , cl::Hidden, |
| 36 | cl::desc("Enable adding flow sensitive discriminators" )); |
| 37 | |
| 38 | // When true, preserves line and column number by picking one of the merged |
| 39 | // location info in a deterministic manner to assist sample based PGO. |
| 40 | LLVM_ABI cl::opt<bool> PickMergedSourceLocations( |
| 41 | "pick-merged-source-locations" , cl::init(Val: false), cl::Hidden, |
| 42 | cl::desc("Preserve line and column number when merging locations." )); |
| 43 | } // namespace llvm |
| 44 | |
| 45 | uint32_t DIType::getAlignInBits() const { |
| 46 | return (getTag() == dwarf::DW_TAG_LLVM_ptrauth_type ? 0 : SubclassData32); |
| 47 | } |
| 48 | |
| 49 | const DIExpression::FragmentInfo DebugVariable::DefaultFragment = { |
| 50 | std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::min()}; |
| 51 | |
| 52 | DebugVariable::DebugVariable(const DbgVariableRecord *DVR) |
| 53 | : Variable(DVR->getVariable()), |
| 54 | Fragment(DVR->getExpression()->getFragmentInfo()), |
| 55 | InlinedAt(DVR->getDebugLoc().getInlinedAt()) {} |
| 56 | |
| 57 | DebugVariableAggregate::DebugVariableAggregate(const DbgVariableRecord *DVR) |
| 58 | : DebugVariable(DVR->getVariable(), std::nullopt, |
| 59 | DVR->getDebugLoc()->getInlinedAt()) {} |
| 60 | |
| 61 | DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line, |
| 62 | unsigned Column, uint64_t AtomGroup, uint8_t AtomRank, |
| 63 | ArrayRef<Metadata *> MDs, bool ImplicitCode) |
| 64 | : MDNode(C, DILocationKind, Storage, MDs), AtomGroup(AtomGroup), |
| 65 | AtomRank(AtomRank) { |
| 66 | assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits" ); |
| 67 | if (AtomGroup) |
| 68 | C.updateDILocationAtomGroupWaterline(G: AtomGroup + 1); |
| 69 | |
| 70 | assert((MDs.size() == 1 || MDs.size() == 2) && |
| 71 | "Expected a scope and optional inlined-at" ); |
| 72 | // Set line and column. |
| 73 | assert(Column < (1u << 16) && "Expected 16-bit column" ); |
| 74 | |
| 75 | SubclassData32 = Line; |
| 76 | SubclassData16 = Column; |
| 77 | |
| 78 | setImplicitCode(ImplicitCode); |
| 79 | } |
| 80 | |
| 81 | static void adjustColumn(unsigned &Column) { |
| 82 | // Set to unknown on overflow. We only have 16 bits to play with here. |
| 83 | if (Column >= (1u << 16)) |
| 84 | Column = 0; |
| 85 | } |
| 86 | |
| 87 | DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line, |
| 88 | unsigned Column, Metadata *Scope, |
| 89 | Metadata *InlinedAt, bool ImplicitCode, |
| 90 | uint64_t AtomGroup, uint8_t AtomRank, |
| 91 | StorageType Storage, bool ShouldCreate) { |
| 92 | // Fixup column. |
| 93 | adjustColumn(Column); |
| 94 | |
| 95 | if (Storage == Uniqued) { |
| 96 | if (auto *N = getUniqued(Store&: Context.pImpl->DILocations, |
| 97 | Key: DILocationInfo::KeyTy(Line, Column, Scope, |
| 98 | InlinedAt, ImplicitCode, |
| 99 | AtomGroup, AtomRank))) |
| 100 | return N; |
| 101 | if (!ShouldCreate) |
| 102 | return nullptr; |
| 103 | } else { |
| 104 | assert(ShouldCreate && "Expected non-uniqued nodes to always be created" ); |
| 105 | } |
| 106 | |
| 107 | SmallVector<Metadata *, 2> Ops; |
| 108 | Ops.push_back(Elt: Scope); |
| 109 | if (InlinedAt) |
| 110 | Ops.push_back(Elt: InlinedAt); |
| 111 | return storeImpl(N: new (Ops.size(), Storage) |
| 112 | DILocation(Context, Storage, Line, Column, AtomGroup, |
| 113 | AtomRank, Ops, ImplicitCode), |
| 114 | Storage, Store&: Context.pImpl->DILocations); |
| 115 | } |
| 116 | |
| 117 | DILocation *DILocation::getMergedLocations(ArrayRef<DILocation *> Locs) { |
| 118 | if (Locs.empty()) |
| 119 | return nullptr; |
| 120 | if (Locs.size() == 1) |
| 121 | return Locs[0]; |
| 122 | auto *Merged = Locs[0]; |
| 123 | for (DILocation *L : llvm::drop_begin(RangeOrContainer&: Locs)) { |
| 124 | Merged = getMergedLocation(LocA: Merged, LocB: L); |
| 125 | if (Merged == nullptr) |
| 126 | break; |
| 127 | } |
| 128 | return Merged; |
| 129 | } |
| 130 | |
| 131 | static DILexicalBlockBase *cloneAndReplaceParentScope(DILexicalBlockBase *LBB, |
| 132 | DIScope *NewParent) { |
| 133 | TempMDNode ClonedScope = LBB->clone(); |
| 134 | cast<DILexicalBlockBase>(Val&: *ClonedScope).replaceScope(Scope: NewParent); |
| 135 | return cast<DILexicalBlockBase>( |
| 136 | Val: MDNode::replaceWithUniqued(N: std::move(ClonedScope))); |
| 137 | } |
| 138 | |
| 139 | using LineColumn = std::pair<unsigned /* Line */, unsigned /* Column */>; |
| 140 | |
| 141 | /// Returns the location of DILocalScope, if present, or a default value. |
| 142 | static LineColumn getLocalScopeLocationOr(DIScope *S, LineColumn Default) { |
| 143 | assert(isa<DILocalScope>(S) && "Expected DILocalScope." ); |
| 144 | |
| 145 | if (isa<DILexicalBlockFile>(Val: S)) |
| 146 | return Default; |
| 147 | if (auto *LB = dyn_cast<DILexicalBlock>(Val: S)) |
| 148 | return {LB->getLine(), LB->getColumn()}; |
| 149 | if (auto *SP = dyn_cast<DISubprogram>(Val: S)) |
| 150 | return {SP->getLine(), 0u}; |
| 151 | |
| 152 | llvm_unreachable("Unhandled type of DILocalScope." ); |
| 153 | } |
| 154 | |
| 155 | // Returns the nearest matching scope inside a subprogram. |
| 156 | template <typename MatcherT> |
| 157 | static std::pair<DIScope *, LineColumn> |
| 158 | getNearestMatchingScope(const DILocation *L1, const DILocation *L2) { |
| 159 | MatcherT Matcher; |
| 160 | |
| 161 | DIScope *S1 = L1->getScope(); |
| 162 | DIScope *S2 = L2->getScope(); |
| 163 | |
| 164 | LineColumn Loc1(L1->getLine(), L1->getColumn()); |
| 165 | for (; S1; S1 = S1->getScope()) { |
| 166 | Loc1 = getLocalScopeLocationOr(S: S1, Default: Loc1); |
| 167 | Matcher.insert(S1, Loc1); |
| 168 | if (isa<DISubprogram>(Val: S1)) |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | LineColumn Loc2(L2->getLine(), L2->getColumn()); |
| 173 | for (; S2; S2 = S2->getScope()) { |
| 174 | Loc2 = getLocalScopeLocationOr(S: S2, Default: Loc2); |
| 175 | |
| 176 | if (DIScope *S = Matcher.match(S2, Loc2)) |
| 177 | return std::make_pair(x&: S, y&: Loc2); |
| 178 | |
| 179 | if (isa<DISubprogram>(Val: S2)) |
| 180 | break; |
| 181 | } |
| 182 | return std::make_pair(x: nullptr, y: LineColumn(L2->getLine(), L2->getColumn())); |
| 183 | } |
| 184 | |
| 185 | // Matches equal scopes. |
| 186 | struct EqualScopesMatcher { |
| 187 | SmallPtrSet<DIScope *, 8> Scopes; |
| 188 | |
| 189 | void insert(DIScope *S, LineColumn Loc) { Scopes.insert(Ptr: S); } |
| 190 | |
| 191 | DIScope *match(DIScope *S, LineColumn Loc) { |
| 192 | return Scopes.contains(Ptr: S) ? S : nullptr; |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | // Matches scopes with the same location. |
| 197 | struct ScopeLocationsMatcher { |
| 198 | SmallMapVector<std::pair<DIFile *, LineColumn>, SmallSetVector<DIScope *, 8>, |
| 199 | 8> |
| 200 | Scopes; |
| 201 | |
| 202 | void insert(DIScope *S, LineColumn Loc) { |
| 203 | Scopes[{S->getFile(), Loc}].insert(X: S); |
| 204 | } |
| 205 | |
| 206 | DIScope *match(DIScope *S, LineColumn Loc) { |
| 207 | auto ScopesAtLoc = Scopes.find(Key: {S->getFile(), Loc}); |
| 208 | // No scope found with the given location. |
| 209 | if (ScopesAtLoc == Scopes.end()) |
| 210 | return nullptr; |
| 211 | |
| 212 | // Prefer S over other scopes with the same location. |
| 213 | if (ScopesAtLoc->second.contains(key: S)) |
| 214 | return S; |
| 215 | |
| 216 | if (!ScopesAtLoc->second.empty()) |
| 217 | return *ScopesAtLoc->second.begin(); |
| 218 | |
| 219 | llvm_unreachable("Scopes must not have empty entries." ); |
| 220 | } |
| 221 | }; |
| 222 | |
| 223 | DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) { |
| 224 | if (LocA == LocB) |
| 225 | return LocA; |
| 226 | |
| 227 | // For some use cases (SamplePGO), it is important to retain distinct source |
| 228 | // locations. When this flag is set, we choose arbitrarily between A and B, |
| 229 | // rather than computing a merged location using line 0, which is typically |
| 230 | // not useful for PGO. If one of them is null, then try to return one which is |
| 231 | // valid. |
| 232 | if (PickMergedSourceLocations) { |
| 233 | if (!LocA || !LocB) |
| 234 | return LocA ? LocA : LocB; |
| 235 | |
| 236 | auto A = std::make_tuple(args: LocA->getLine(), args: LocA->getColumn(), |
| 237 | args: LocA->getDiscriminator(), args: LocA->getFilename(), |
| 238 | args: LocA->getDirectory()); |
| 239 | auto B = std::make_tuple(args: LocB->getLine(), args: LocB->getColumn(), |
| 240 | args: LocB->getDiscriminator(), args: LocB->getFilename(), |
| 241 | args: LocB->getDirectory()); |
| 242 | return A < B ? LocA : LocB; |
| 243 | } |
| 244 | |
| 245 | if (!LocA || !LocB) |
| 246 | return nullptr; |
| 247 | |
| 248 | LLVMContext &C = LocA->getContext(); |
| 249 | |
| 250 | using LocVec = SmallVector<const DILocation *>; |
| 251 | LocVec ALocs; |
| 252 | LocVec BLocs; |
| 253 | SmallDenseMap<std::pair<const DISubprogram *, const DILocation *>, unsigned, |
| 254 | 4> |
| 255 | ALookup; |
| 256 | |
| 257 | // Walk through LocA and its inlined-at locations, populate them in ALocs and |
| 258 | // save the index for the subprogram and inlined-at pair, which we use to find |
| 259 | // a matching starting location in LocB's chain. |
| 260 | for (auto [L, I] = std::make_pair(x&: LocA, y: 0U); L; L = L->getInlinedAt(), I++) { |
| 261 | ALocs.push_back(Elt: L); |
| 262 | auto Res = ALookup.try_emplace( |
| 263 | Key: {L->getScope()->getSubprogram(), L->getInlinedAt()}, Args&: I); |
| 264 | assert(Res.second && "Multiple <SP, InlinedAt> pairs in a location chain?" ); |
| 265 | (void)Res; |
| 266 | } |
| 267 | |
| 268 | LocVec::reverse_iterator ARIt = ALocs.rend(); |
| 269 | LocVec::reverse_iterator BRIt = BLocs.rend(); |
| 270 | |
| 271 | // Populate BLocs and look for a matching starting location, the first |
| 272 | // location with the same subprogram and inlined-at location as in LocA's |
| 273 | // chain. Since the two locations have the same inlined-at location we do |
| 274 | // not need to look at those parts of the chains. |
| 275 | for (auto [L, I] = std::make_pair(x&: LocB, y: 0U); L; L = L->getInlinedAt(), I++) { |
| 276 | BLocs.push_back(Elt: L); |
| 277 | |
| 278 | if (ARIt != ALocs.rend()) |
| 279 | // We have already found a matching starting location. |
| 280 | continue; |
| 281 | |
| 282 | auto IT = ALookup.find(Val: {L->getScope()->getSubprogram(), L->getInlinedAt()}); |
| 283 | if (IT == ALookup.end()) |
| 284 | continue; |
| 285 | |
| 286 | // The + 1 is to account for the &*rev_it = &(it - 1) relationship. |
| 287 | ARIt = LocVec::reverse_iterator(ALocs.begin() + IT->second + 1); |
| 288 | BRIt = LocVec::reverse_iterator(BLocs.begin() + I + 1); |
| 289 | |
| 290 | // If we have found a matching starting location we do not need to add more |
| 291 | // locations to BLocs, since we will only look at location pairs preceding |
| 292 | // the matching starting location, and adding more elements to BLocs could |
| 293 | // invalidate the iterator that we initialized here. |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | // Merge the two locations if possible, using the supplied |
| 298 | // inlined-at location for the created location. |
| 299 | auto *LocAIA = LocA->getInlinedAt(); |
| 300 | auto *LocBIA = LocB->getInlinedAt(); |
| 301 | auto MergeLocPair = [&C, LocAIA, |
| 302 | LocBIA](const DILocation *L1, const DILocation *L2, |
| 303 | DILocation *InlinedAt) -> DILocation * { |
| 304 | if (L1 == L2) |
| 305 | return DILocation::get(Context&: C, Line: L1->getLine(), Column: L1->getColumn(), Scope: L1->getScope(), |
| 306 | InlinedAt, ImplicitCode: L1->isImplicitCode(), |
| 307 | AtomGroup: L1->getAtomGroup(), AtomRank: L1->getAtomRank()); |
| 308 | |
| 309 | // If the locations originate from different subprograms we can't produce |
| 310 | // a common location. |
| 311 | if (L1->getScope()->getSubprogram() != L2->getScope()->getSubprogram()) |
| 312 | return nullptr; |
| 313 | |
| 314 | // Find nearest common scope inside subprogram. |
| 315 | DIScope *Scope = getNearestMatchingScope<EqualScopesMatcher>(L1, L2).first; |
| 316 | assert(Scope && "No common scope in the same subprogram?" ); |
| 317 | |
| 318 | // Try using the nearest scope with common location if files are different. |
| 319 | if (Scope->getFile() != L1->getFile() || L1->getFile() != L2->getFile()) { |
| 320 | auto [CommonLocScope, CommonLoc] = |
| 321 | getNearestMatchingScope<ScopeLocationsMatcher>(L1, L2); |
| 322 | |
| 323 | // If CommonLocScope is a DILexicalBlockBase, clone it and locate |
| 324 | // a new scope inside the nearest common scope to preserve |
| 325 | // lexical blocks structure. |
| 326 | if (auto *LBB = dyn_cast<DILexicalBlockBase>(Val: CommonLocScope); |
| 327 | LBB && LBB != Scope) |
| 328 | CommonLocScope = cloneAndReplaceParentScope(LBB, NewParent: Scope); |
| 329 | |
| 330 | Scope = CommonLocScope; |
| 331 | |
| 332 | // If files are still different, assume that L1 and L2 were "included" |
| 333 | // from CommonLoc. Use it as merged location. |
| 334 | if (Scope->getFile() != L1->getFile() || L1->getFile() != L2->getFile()) |
| 335 | return DILocation::get(Context&: C, Line: CommonLoc.first, Column: CommonLoc.second, |
| 336 | Scope: CommonLocScope, InlinedAt); |
| 337 | } |
| 338 | |
| 339 | bool SameLine = L1->getLine() == L2->getLine(); |
| 340 | bool SameCol = L1->getColumn() == L2->getColumn(); |
| 341 | unsigned Line = SameLine ? L1->getLine() : 0; |
| 342 | unsigned Col = SameLine && SameCol ? L1->getColumn() : 0; |
| 343 | bool IsImplicitCode = L1->isImplicitCode() && L2->isImplicitCode(); |
| 344 | |
| 345 | // Discard source location atom if the line becomes 0. And there's nothing |
| 346 | // further to do if neither location has an atom number. |
| 347 | if (!SameLine || !(L1->getAtomGroup() || L2->getAtomGroup())) |
| 348 | return DILocation::get(Context&: C, Line, Column: Col, Scope, InlinedAt, ImplicitCode: IsImplicitCode, |
| 349 | /*AtomGroup*/ 0, /*AtomRank*/ 0); |
| 350 | |
| 351 | uint64_t Group = 0; |
| 352 | uint64_t Rank = 0; |
| 353 | // If we're preserving the same matching inlined-at field we can |
| 354 | // preserve the atom. |
| 355 | if (LocBIA == LocAIA && InlinedAt == LocBIA) { |
| 356 | // Deterministically keep the lowest non-zero ranking atom group |
| 357 | // number. |
| 358 | // FIXME: It would be nice if we could track that an instruction |
| 359 | // belongs to two source atoms. |
| 360 | bool UseL1Atom = [L1, L2]() { |
| 361 | if (L1->getAtomRank() == L2->getAtomRank()) { |
| 362 | // Arbitrarily choose the lowest non-zero group number. |
| 363 | if (!L1->getAtomGroup() || !L2->getAtomGroup()) |
| 364 | return !L2->getAtomGroup(); |
| 365 | return L1->getAtomGroup() < L2->getAtomGroup(); |
| 366 | } |
| 367 | // Choose the lowest non-zero rank. |
| 368 | if (!L1->getAtomRank() || !L2->getAtomRank()) |
| 369 | return !L2->getAtomRank(); |
| 370 | return L1->getAtomRank() < L2->getAtomRank(); |
| 371 | }(); |
| 372 | Group = UseL1Atom ? L1->getAtomGroup() : L2->getAtomGroup(); |
| 373 | Rank = UseL1Atom ? L1->getAtomRank() : L2->getAtomRank(); |
| 374 | } else { |
| 375 | // If either instruction is part of a source atom, reassign it a new |
| 376 | // atom group. This essentially regresses to non-key-instructions |
| 377 | // behaviour (now that it's the only instruction in its group it'll |
| 378 | // probably get is_stmt applied). |
| 379 | Group = C.incNextDILocationAtomGroup(); |
| 380 | Rank = 1; |
| 381 | } |
| 382 | return DILocation::get(Context&: C, Line, Column: Col, Scope, InlinedAt, ImplicitCode: IsImplicitCode, |
| 383 | AtomGroup: Group, AtomRank: Rank); |
| 384 | }; |
| 385 | |
| 386 | DILocation *Result = ARIt != ALocs.rend() ? (*ARIt)->getInlinedAt() : nullptr; |
| 387 | |
| 388 | // If we have found a common starting location, walk up the inlined-at chains |
| 389 | // and try to produce common locations. |
| 390 | for (; ARIt != ALocs.rend() && BRIt != BLocs.rend(); ++ARIt, ++BRIt) { |
| 391 | DILocation *Tmp = MergeLocPair(*ARIt, *BRIt, Result); |
| 392 | |
| 393 | if (!Tmp) |
| 394 | // We have walked up to a point in the chains where the two locations |
| 395 | // are irreconsilable. At this point Result contains the nearest common |
| 396 | // location in the inlined-at chains of LocA and LocB, so we break here. |
| 397 | break; |
| 398 | |
| 399 | Result = Tmp; |
| 400 | } |
| 401 | |
| 402 | if (Result) |
| 403 | return Result; |
| 404 | |
| 405 | // We ended up with LocA and LocB as irreconsilable locations. Produce a |
| 406 | // location at 0:0 with one of the locations' scope. The function has |
| 407 | // historically picked A's scope, and a nullptr inlined-at location, so that |
| 408 | // behavior is mimicked here but I am not sure if this is always the correct |
| 409 | // way to handle this. |
| 410 | // Key Instructions: it's fine to drop atom group and rank here, as line 0 |
| 411 | // is a nonsensical is_stmt location. |
| 412 | return DILocation::get(Context&: C, Line: 0, Column: 0, Scope: LocA->getScope(), InlinedAt: nullptr, ImplicitCode: false, |
| 413 | /*AtomGroup*/ 0, /*AtomRank*/ 0); |
| 414 | } |
| 415 | |
| 416 | std::optional<unsigned> |
| 417 | DILocation::encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI) { |
| 418 | std::array<unsigned, 3> Components = {BD, DF, CI}; |
| 419 | uint64_t RemainingWork = 0U; |
| 420 | // We use RemainingWork to figure out if we have no remaining components to |
| 421 | // encode. For example: if BD != 0 but DF == 0 && CI == 0, we don't need to |
| 422 | // encode anything for the latter 2. |
| 423 | // Since any of the input components is at most 32 bits, their sum will be |
| 424 | // less than 34 bits, and thus RemainingWork won't overflow. |
| 425 | RemainingWork = |
| 426 | std::accumulate(first: Components.begin(), last: Components.end(), init: RemainingWork); |
| 427 | |
| 428 | int I = 0; |
| 429 | unsigned Ret = 0; |
| 430 | unsigned NextBitInsertionIndex = 0; |
| 431 | while (RemainingWork > 0) { |
| 432 | unsigned C = Components[I++]; |
| 433 | RemainingWork -= C; |
| 434 | unsigned EC = encodeComponent(C); |
| 435 | Ret |= (EC << NextBitInsertionIndex); |
| 436 | NextBitInsertionIndex += encodingBits(C); |
| 437 | } |
| 438 | |
| 439 | // Encoding may be unsuccessful because of overflow. We determine success by |
| 440 | // checking equivalence of components before & after encoding. Alternatively, |
| 441 | // we could determine Success during encoding, but the current alternative is |
| 442 | // simpler. |
| 443 | unsigned TBD, TDF, TCI = 0; |
| 444 | decodeDiscriminator(D: Ret, BD&: TBD, DF&: TDF, CI&: TCI); |
| 445 | if (TBD == BD && TDF == DF && TCI == CI) |
| 446 | return Ret; |
| 447 | return std::nullopt; |
| 448 | } |
| 449 | |
| 450 | void DILocation::decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF, |
| 451 | unsigned &CI) { |
| 452 | BD = getUnsignedFromPrefixEncoding(U: D); |
| 453 | DF = getUnsignedFromPrefixEncoding(U: getNextComponentInDiscriminator(D)); |
| 454 | CI = getUnsignedFromPrefixEncoding( |
| 455 | U: getNextComponentInDiscriminator(D: getNextComponentInDiscriminator(D))); |
| 456 | } |
| 457 | dwarf::Tag DINode::getTag() const { return (dwarf::Tag)SubclassData16; } |
| 458 | |
| 459 | DINode::DIFlags DINode::getFlag(StringRef Flag) { |
| 460 | return StringSwitch<DIFlags>(Flag) |
| 461 | #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) |
| 462 | #include "llvm/IR/DebugInfoFlags.def" |
| 463 | .Default(Value: DINode::FlagZero); |
| 464 | } |
| 465 | |
| 466 | StringRef DINode::getFlagString(DIFlags Flag) { |
| 467 | switch (Flag) { |
| 468 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 469 | case Flag##NAME: \ |
| 470 | return "DIFlag" #NAME; |
| 471 | #include "llvm/IR/DebugInfoFlags.def" |
| 472 | } |
| 473 | return "" ; |
| 474 | } |
| 475 | |
| 476 | DINode::DIFlags DINode::splitFlags(DIFlags Flags, |
| 477 | SmallVectorImpl<DIFlags> &SplitFlags) { |
| 478 | // Flags that are packed together need to be specially handled, so |
| 479 | // that, for example, we emit "DIFlagPublic" and not |
| 480 | // "DIFlagPrivate | DIFlagProtected". |
| 481 | if (DIFlags A = Flags & FlagAccessibility) { |
| 482 | if (A == FlagPrivate) |
| 483 | SplitFlags.push_back(Elt: FlagPrivate); |
| 484 | else if (A == FlagProtected) |
| 485 | SplitFlags.push_back(Elt: FlagProtected); |
| 486 | else |
| 487 | SplitFlags.push_back(Elt: FlagPublic); |
| 488 | Flags &= ~A; |
| 489 | } |
| 490 | if (DIFlags R = Flags & FlagPtrToMemberRep) { |
| 491 | if (R == FlagSingleInheritance) |
| 492 | SplitFlags.push_back(Elt: FlagSingleInheritance); |
| 493 | else if (R == FlagMultipleInheritance) |
| 494 | SplitFlags.push_back(Elt: FlagMultipleInheritance); |
| 495 | else |
| 496 | SplitFlags.push_back(Elt: FlagVirtualInheritance); |
| 497 | Flags &= ~R; |
| 498 | } |
| 499 | if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) { |
| 500 | Flags &= ~FlagIndirectVirtualBase; |
| 501 | SplitFlags.push_back(Elt: FlagIndirectVirtualBase); |
| 502 | } |
| 503 | |
| 504 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 505 | if (DIFlags Bit = Flags & Flag##NAME) { \ |
| 506 | SplitFlags.push_back(Bit); \ |
| 507 | Flags &= ~Bit; \ |
| 508 | } |
| 509 | #include "llvm/IR/DebugInfoFlags.def" |
| 510 | return Flags; |
| 511 | } |
| 512 | |
| 513 | DIScope *DIScope::getScope() const { |
| 514 | if (auto *T = dyn_cast<DIType>(Val: this)) |
| 515 | return T->getScope(); |
| 516 | |
| 517 | if (auto *SP = dyn_cast<DISubprogram>(Val: this)) |
| 518 | return SP->getScope(); |
| 519 | |
| 520 | if (auto *LB = dyn_cast<DILexicalBlockBase>(Val: this)) |
| 521 | return LB->getScope(); |
| 522 | |
| 523 | if (auto *NS = dyn_cast<DINamespace>(Val: this)) |
| 524 | return NS->getScope(); |
| 525 | |
| 526 | if (auto *CB = dyn_cast<DICommonBlock>(Val: this)) |
| 527 | return CB->getScope(); |
| 528 | |
| 529 | if (auto *M = dyn_cast<DIModule>(Val: this)) |
| 530 | return M->getScope(); |
| 531 | |
| 532 | assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) && |
| 533 | "Unhandled type of scope." ); |
| 534 | return nullptr; |
| 535 | } |
| 536 | |
| 537 | StringRef DIScope::getName() const { |
| 538 | if (auto *T = dyn_cast<DIType>(Val: this)) |
| 539 | return T->getName(); |
| 540 | if (auto *SP = dyn_cast<DISubprogram>(Val: this)) |
| 541 | return SP->getName(); |
| 542 | if (auto *NS = dyn_cast<DINamespace>(Val: this)) |
| 543 | return NS->getName(); |
| 544 | if (auto *CB = dyn_cast<DICommonBlock>(Val: this)) |
| 545 | return CB->getName(); |
| 546 | if (auto *M = dyn_cast<DIModule>(Val: this)) |
| 547 | return M->getName(); |
| 548 | assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) || |
| 549 | isa<DICompileUnit>(this)) && |
| 550 | "Unhandled type of scope." ); |
| 551 | return "" ; |
| 552 | } |
| 553 | |
| 554 | #ifndef NDEBUG |
| 555 | static bool isCanonical(const MDString *S) { |
| 556 | return !S || !S->getString().empty(); |
| 557 | } |
| 558 | #endif |
| 559 | |
| 560 | dwarf::Tag GenericDINode::getTag() const { return (dwarf::Tag)SubclassData16; } |
| 561 | GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag, |
| 562 | MDString *, |
| 563 | ArrayRef<Metadata *> DwarfOps, |
| 564 | StorageType Storage, bool ShouldCreate) { |
| 565 | unsigned Hash = 0; |
| 566 | if (Storage == Uniqued) { |
| 567 | GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps); |
| 568 | if (auto *N = getUniqued(Store&: Context.pImpl->GenericDINodes, Key)) |
| 569 | return N; |
| 570 | if (!ShouldCreate) |
| 571 | return nullptr; |
| 572 | Hash = Key.getHash(); |
| 573 | } else { |
| 574 | assert(ShouldCreate && "Expected non-uniqued nodes to always be created" ); |
| 575 | } |
| 576 | |
| 577 | // Use a nullptr for empty headers. |
| 578 | assert(isCanonical(Header) && "Expected canonical MDString" ); |
| 579 | Metadata *PreOps[] = {Header}; |
| 580 | return storeImpl(N: new (DwarfOps.size() + 1, Storage) GenericDINode( |
| 581 | Context, Storage, Hash, Tag, PreOps, DwarfOps), |
| 582 | Storage, Store&: Context.pImpl->GenericDINodes); |
| 583 | } |
| 584 | |
| 585 | void GenericDINode::recalculateHash() { |
| 586 | setHash(GenericDINodeInfo::KeyTy::calculateHash(N: this)); |
| 587 | } |
| 588 | |
| 589 | #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__ |
| 590 | #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS |
| 591 | #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \ |
| 592 | do { \ |
| 593 | if (Storage == Uniqued) { \ |
| 594 | if (auto *N = getUniqued(Context.pImpl->CLASS##s, \ |
| 595 | CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \ |
| 596 | return N; \ |
| 597 | if (!ShouldCreate) \ |
| 598 | return nullptr; \ |
| 599 | } else { \ |
| 600 | assert(ShouldCreate && \ |
| 601 | "Expected non-uniqued nodes to always be created"); \ |
| 602 | } \ |
| 603 | } while (false) |
| 604 | #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \ |
| 605 | return storeImpl(new (std::size(OPS), Storage) \ |
| 606 | CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ |
| 607 | Storage, Context.pImpl->CLASS##s) |
| 608 | #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \ |
| 609 | return storeImpl(new (0u, Storage) \ |
| 610 | CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \ |
| 611 | Storage, Context.pImpl->CLASS##s) |
| 612 | #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \ |
| 613 | return storeImpl(new (std::size(OPS), Storage) CLASS(Context, Storage, OPS), \ |
| 614 | Storage, Context.pImpl->CLASS##s) |
| 615 | #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \ |
| 616 | return storeImpl(new (NUM_OPS, Storage) \ |
| 617 | CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ |
| 618 | Storage, Context.pImpl->CLASS##s) |
| 619 | |
| 620 | DISubrange::DISubrange(LLVMContext &C, StorageType Storage, |
| 621 | ArrayRef<Metadata *> Ops) |
| 622 | : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops) {} |
| 623 | DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo, |
| 624 | StorageType Storage, bool ShouldCreate) { |
| 625 | auto *CountNode = ConstantAsMetadata::get( |
| 626 | C: ConstantInt::getSigned(Ty: Type::getInt64Ty(C&: Context), V: Count)); |
| 627 | auto *LB = ConstantAsMetadata::get( |
| 628 | C: ConstantInt::getSigned(Ty: Type::getInt64Ty(C&: Context), V: Lo)); |
| 629 | return getImpl(Context, CountNode, LowerBound: LB, UpperBound: nullptr, Stride: nullptr, Storage, |
| 630 | ShouldCreate); |
| 631 | } |
| 632 | |
| 633 | DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode, |
| 634 | int64_t Lo, StorageType Storage, |
| 635 | bool ShouldCreate) { |
| 636 | auto *LB = ConstantAsMetadata::get( |
| 637 | C: ConstantInt::getSigned(Ty: Type::getInt64Ty(C&: Context), V: Lo)); |
| 638 | return getImpl(Context, CountNode, LowerBound: LB, UpperBound: nullptr, Stride: nullptr, Storage, |
| 639 | ShouldCreate); |
| 640 | } |
| 641 | |
| 642 | DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode, |
| 643 | Metadata *LB, Metadata *UB, Metadata *Stride, |
| 644 | StorageType Storage, bool ShouldCreate) { |
| 645 | DEFINE_GETIMPL_LOOKUP(DISubrange, (CountNode, LB, UB, Stride)); |
| 646 | Metadata *Ops[] = {CountNode, LB, UB, Stride}; |
| 647 | DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DISubrange, Ops); |
| 648 | } |
| 649 | |
| 650 | DISubrange::BoundType DISubrange::getCount() const { |
| 651 | Metadata *CB = getRawCountNode(); |
| 652 | if (!CB) |
| 653 | return BoundType(); |
| 654 | |
| 655 | assert((isa<ConstantAsMetadata>(CB) || isa<DIVariable>(CB) || |
| 656 | isa<DIExpression>(CB)) && |
| 657 | "Count must be signed constant or DIVariable or DIExpression" ); |
| 658 | |
| 659 | if (auto *MD = dyn_cast<ConstantAsMetadata>(Val: CB)) |
| 660 | return BoundType(cast<ConstantInt>(Val: MD->getValue())); |
| 661 | |
| 662 | if (auto *MD = dyn_cast<DIVariable>(Val: CB)) |
| 663 | return BoundType(MD); |
| 664 | |
| 665 | if (auto *MD = dyn_cast<DIExpression>(Val: CB)) |
| 666 | return BoundType(MD); |
| 667 | |
| 668 | return BoundType(); |
| 669 | } |
| 670 | |
| 671 | DISubrange::BoundType DISubrange::getLowerBound() const { |
| 672 | Metadata *LB = getRawLowerBound(); |
| 673 | if (!LB) |
| 674 | return BoundType(); |
| 675 | |
| 676 | assert((isa<ConstantAsMetadata>(LB) || isa<DIVariable>(LB) || |
| 677 | isa<DIExpression>(LB)) && |
| 678 | "LowerBound must be signed constant or DIVariable or DIExpression" ); |
| 679 | |
| 680 | if (auto *MD = dyn_cast<ConstantAsMetadata>(Val: LB)) |
| 681 | return BoundType(cast<ConstantInt>(Val: MD->getValue())); |
| 682 | |
| 683 | if (auto *MD = dyn_cast<DIVariable>(Val: LB)) |
| 684 | return BoundType(MD); |
| 685 | |
| 686 | if (auto *MD = dyn_cast<DIExpression>(Val: LB)) |
| 687 | return BoundType(MD); |
| 688 | |
| 689 | return BoundType(); |
| 690 | } |
| 691 | |
| 692 | DISubrange::BoundType DISubrange::getUpperBound() const { |
| 693 | Metadata *UB = getRawUpperBound(); |
| 694 | if (!UB) |
| 695 | return BoundType(); |
| 696 | |
| 697 | assert((isa<ConstantAsMetadata>(UB) || isa<DIVariable>(UB) || |
| 698 | isa<DIExpression>(UB)) && |
| 699 | "UpperBound must be signed constant or DIVariable or DIExpression" ); |
| 700 | |
| 701 | if (auto *MD = dyn_cast<ConstantAsMetadata>(Val: UB)) |
| 702 | return BoundType(cast<ConstantInt>(Val: MD->getValue())); |
| 703 | |
| 704 | if (auto *MD = dyn_cast<DIVariable>(Val: UB)) |
| 705 | return BoundType(MD); |
| 706 | |
| 707 | if (auto *MD = dyn_cast<DIExpression>(Val: UB)) |
| 708 | return BoundType(MD); |
| 709 | |
| 710 | return BoundType(); |
| 711 | } |
| 712 | |
| 713 | DISubrange::BoundType DISubrange::getStride() const { |
| 714 | Metadata *ST = getRawStride(); |
| 715 | if (!ST) |
| 716 | return BoundType(); |
| 717 | |
| 718 | assert((isa<ConstantAsMetadata>(ST) || isa<DIVariable>(ST) || |
| 719 | isa<DIExpression>(ST)) && |
| 720 | "Stride must be signed constant or DIVariable or DIExpression" ); |
| 721 | |
| 722 | if (auto *MD = dyn_cast<ConstantAsMetadata>(Val: ST)) |
| 723 | return BoundType(cast<ConstantInt>(Val: MD->getValue())); |
| 724 | |
| 725 | if (auto *MD = dyn_cast<DIVariable>(Val: ST)) |
| 726 | return BoundType(MD); |
| 727 | |
| 728 | if (auto *MD = dyn_cast<DIExpression>(Val: ST)) |
| 729 | return BoundType(MD); |
| 730 | |
| 731 | return BoundType(); |
| 732 | } |
| 733 | DIGenericSubrange::DIGenericSubrange(LLVMContext &C, StorageType Storage, |
| 734 | ArrayRef<Metadata *> Ops) |
| 735 | : DINode(C, DIGenericSubrangeKind, Storage, dwarf::DW_TAG_generic_subrange, |
| 736 | Ops) {} |
| 737 | |
| 738 | DIGenericSubrange *DIGenericSubrange::getImpl(LLVMContext &Context, |
| 739 | Metadata *CountNode, Metadata *LB, |
| 740 | Metadata *UB, Metadata *Stride, |
| 741 | StorageType Storage, |
| 742 | bool ShouldCreate) { |
| 743 | DEFINE_GETIMPL_LOOKUP(DIGenericSubrange, (CountNode, LB, UB, Stride)); |
| 744 | Metadata *Ops[] = {CountNode, LB, UB, Stride}; |
| 745 | DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGenericSubrange, Ops); |
| 746 | } |
| 747 | |
| 748 | DIGenericSubrange::BoundType DIGenericSubrange::getCount() const { |
| 749 | Metadata *CB = getRawCountNode(); |
| 750 | if (!CB) |
| 751 | return BoundType(); |
| 752 | |
| 753 | assert((isa<DIVariable>(CB) || isa<DIExpression>(CB)) && |
| 754 | "Count must be signed constant or DIVariable or DIExpression" ); |
| 755 | |
| 756 | if (auto *MD = dyn_cast<DIVariable>(Val: CB)) |
| 757 | return BoundType(MD); |
| 758 | |
| 759 | if (auto *MD = dyn_cast<DIExpression>(Val: CB)) |
| 760 | return BoundType(MD); |
| 761 | |
| 762 | return BoundType(); |
| 763 | } |
| 764 | |
| 765 | DIGenericSubrange::BoundType DIGenericSubrange::getLowerBound() const { |
| 766 | Metadata *LB = getRawLowerBound(); |
| 767 | if (!LB) |
| 768 | return BoundType(); |
| 769 | |
| 770 | assert((isa<DIVariable>(LB) || isa<DIExpression>(LB)) && |
| 771 | "LowerBound must be signed constant or DIVariable or DIExpression" ); |
| 772 | |
| 773 | if (auto *MD = dyn_cast<DIVariable>(Val: LB)) |
| 774 | return BoundType(MD); |
| 775 | |
| 776 | if (auto *MD = dyn_cast<DIExpression>(Val: LB)) |
| 777 | return BoundType(MD); |
| 778 | |
| 779 | return BoundType(); |
| 780 | } |
| 781 | |
| 782 | DIGenericSubrange::BoundType DIGenericSubrange::getUpperBound() const { |
| 783 | Metadata *UB = getRawUpperBound(); |
| 784 | if (!UB) |
| 785 | return BoundType(); |
| 786 | |
| 787 | assert((isa<DIVariable>(UB) || isa<DIExpression>(UB)) && |
| 788 | "UpperBound must be signed constant or DIVariable or DIExpression" ); |
| 789 | |
| 790 | if (auto *MD = dyn_cast<DIVariable>(Val: UB)) |
| 791 | return BoundType(MD); |
| 792 | |
| 793 | if (auto *MD = dyn_cast<DIExpression>(Val: UB)) |
| 794 | return BoundType(MD); |
| 795 | |
| 796 | return BoundType(); |
| 797 | } |
| 798 | |
| 799 | DIGenericSubrange::BoundType DIGenericSubrange::getStride() const { |
| 800 | Metadata *ST = getRawStride(); |
| 801 | if (!ST) |
| 802 | return BoundType(); |
| 803 | |
| 804 | assert((isa<DIVariable>(ST) || isa<DIExpression>(ST)) && |
| 805 | "Stride must be signed constant or DIVariable or DIExpression" ); |
| 806 | |
| 807 | if (auto *MD = dyn_cast<DIVariable>(Val: ST)) |
| 808 | return BoundType(MD); |
| 809 | |
| 810 | if (auto *MD = dyn_cast<DIExpression>(Val: ST)) |
| 811 | return BoundType(MD); |
| 812 | |
| 813 | return BoundType(); |
| 814 | } |
| 815 | |
| 816 | DISubrangeType::DISubrangeType(LLVMContext &C, StorageType Storage, |
| 817 | unsigned Line, uint32_t AlignInBits, |
| 818 | DIFlags Flags, ArrayRef<Metadata *> Ops) |
| 819 | : DIType(C, DISubrangeTypeKind, Storage, dwarf::DW_TAG_subrange_type, Line, |
| 820 | AlignInBits, 0, Flags, Ops) {} |
| 821 | |
| 822 | DISubrangeType *DISubrangeType::getImpl( |
| 823 | LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, |
| 824 | Metadata *Scope, Metadata *SizeInBits, uint32_t AlignInBits, DIFlags Flags, |
| 825 | Metadata *BaseType, Metadata *LowerBound, Metadata *UpperBound, |
| 826 | Metadata *Stride, Metadata *Bias, StorageType Storage, bool ShouldCreate) { |
| 827 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 828 | DEFINE_GETIMPL_LOOKUP(DISubrangeType, (Name, File, Line, Scope, SizeInBits, |
| 829 | AlignInBits, Flags, BaseType, |
| 830 | LowerBound, UpperBound, Stride, Bias)); |
| 831 | Metadata *Ops[] = {File, Scope, Name, SizeInBits, nullptr, |
| 832 | BaseType, LowerBound, UpperBound, Stride, Bias}; |
| 833 | DEFINE_GETIMPL_STORE(DISubrangeType, (Line, AlignInBits, Flags), Ops); |
| 834 | } |
| 835 | |
| 836 | DISubrangeType::BoundType |
| 837 | DISubrangeType::convertRawToBound(Metadata *IN) const { |
| 838 | if (!IN) |
| 839 | return BoundType(); |
| 840 | |
| 841 | assert(isa<ConstantAsMetadata>(IN) || isa<DIVariable>(IN) || |
| 842 | isa<DIExpression>(IN) || isa<DIDerivedType>(IN)); |
| 843 | |
| 844 | if (auto *MD = dyn_cast<ConstantAsMetadata>(Val: IN)) |
| 845 | return BoundType(cast<ConstantInt>(Val: MD->getValue())); |
| 846 | |
| 847 | if (auto *MD = dyn_cast<DIVariable>(Val: IN)) |
| 848 | return BoundType(MD); |
| 849 | |
| 850 | if (auto *MD = dyn_cast<DIExpression>(Val: IN)) |
| 851 | return BoundType(MD); |
| 852 | |
| 853 | if (auto *DT = dyn_cast<DIDerivedType>(Val: IN)) |
| 854 | return BoundType(DT); |
| 855 | |
| 856 | return BoundType(); |
| 857 | } |
| 858 | |
| 859 | DIEnumerator::DIEnumerator(LLVMContext &C, StorageType Storage, |
| 860 | const APInt &Value, bool IsUnsigned, |
| 861 | ArrayRef<Metadata *> Ops) |
| 862 | : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops), |
| 863 | Value(Value) { |
| 864 | SubclassData32 = IsUnsigned; |
| 865 | } |
| 866 | DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, const APInt &Value, |
| 867 | bool IsUnsigned, MDString *Name, |
| 868 | StorageType Storage, bool ShouldCreate) { |
| 869 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 870 | DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, IsUnsigned, Name)); |
| 871 | Metadata *Ops[] = {Name}; |
| 872 | DEFINE_GETIMPL_STORE(DIEnumerator, (Value, IsUnsigned), Ops); |
| 873 | } |
| 874 | |
| 875 | DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag, |
| 876 | MDString *Name, Metadata *SizeInBits, |
| 877 | uint32_t AlignInBits, unsigned Encoding, |
| 878 | uint32_t , |
| 879 | uint32_t DataSizeInBits, DIFlags Flags, |
| 880 | StorageType Storage, bool ShouldCreate) { |
| 881 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 882 | DEFINE_GETIMPL_LOOKUP(DIBasicType, |
| 883 | (Tag, Name, SizeInBits, AlignInBits, Encoding, |
| 884 | NumExtraInhabitants, DataSizeInBits, Flags)); |
| 885 | Metadata *Ops[] = {nullptr, nullptr, Name, SizeInBits, nullptr}; |
| 886 | DEFINE_GETIMPL_STORE( |
| 887 | DIBasicType, |
| 888 | (Tag, AlignInBits, Encoding, NumExtraInhabitants, DataSizeInBits, Flags), |
| 889 | Ops); |
| 890 | } |
| 891 | |
| 892 | std::optional<DIBasicType::Signedness> DIBasicType::getSignedness() const { |
| 893 | switch (getEncoding()) { |
| 894 | case dwarf::DW_ATE_signed: |
| 895 | case dwarf::DW_ATE_signed_char: |
| 896 | case dwarf::DW_ATE_signed_fixed: |
| 897 | return Signedness::Signed; |
| 898 | case dwarf::DW_ATE_unsigned: |
| 899 | case dwarf::DW_ATE_unsigned_char: |
| 900 | case dwarf::DW_ATE_unsigned_fixed: |
| 901 | return Signedness::Unsigned; |
| 902 | default: |
| 903 | return std::nullopt; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | DIFixedPointType * |
| 908 | DIFixedPointType::getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, |
| 909 | Metadata *SizeInBits, uint32_t AlignInBits, |
| 910 | unsigned Encoding, DIFlags Flags, unsigned Kind, |
| 911 | int Factor, APInt Numerator, APInt Denominator, |
| 912 | StorageType Storage, bool ShouldCreate) { |
| 913 | DEFINE_GETIMPL_LOOKUP(DIFixedPointType, |
| 914 | (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags, |
| 915 | Kind, Factor, Numerator, Denominator)); |
| 916 | Metadata *Ops[] = {nullptr, nullptr, Name, SizeInBits, nullptr}; |
| 917 | DEFINE_GETIMPL_STORE( |
| 918 | DIFixedPointType, |
| 919 | (Tag, AlignInBits, Encoding, Flags, Kind, Factor, Numerator, Denominator), |
| 920 | Ops); |
| 921 | } |
| 922 | |
| 923 | bool DIFixedPointType::isSigned() const { |
| 924 | return getEncoding() == dwarf::DW_ATE_signed_fixed; |
| 925 | } |
| 926 | |
| 927 | std::optional<DIFixedPointType::FixedPointKind> |
| 928 | DIFixedPointType::getFixedPointKind(StringRef Str) { |
| 929 | return StringSwitch<std::optional<FixedPointKind>>(Str) |
| 930 | .Case(S: "Binary" , Value: FixedPointBinary) |
| 931 | .Case(S: "Decimal" , Value: FixedPointDecimal) |
| 932 | .Case(S: "Rational" , Value: FixedPointRational) |
| 933 | .Default(Value: std::nullopt); |
| 934 | } |
| 935 | |
| 936 | const char *DIFixedPointType::fixedPointKindString(FixedPointKind V) { |
| 937 | switch (V) { |
| 938 | case FixedPointBinary: |
| 939 | return "Binary" ; |
| 940 | case FixedPointDecimal: |
| 941 | return "Decimal" ; |
| 942 | case FixedPointRational: |
| 943 | return "Rational" ; |
| 944 | } |
| 945 | return nullptr; |
| 946 | } |
| 947 | |
| 948 | DIStringType *DIStringType::getImpl(LLVMContext &Context, unsigned Tag, |
| 949 | MDString *Name, Metadata *StringLength, |
| 950 | Metadata *StringLengthExp, |
| 951 | Metadata *StringLocationExp, |
| 952 | Metadata *SizeInBits, uint32_t AlignInBits, |
| 953 | unsigned Encoding, StorageType Storage, |
| 954 | bool ShouldCreate) { |
| 955 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 956 | DEFINE_GETIMPL_LOOKUP(DIStringType, |
| 957 | (Tag, Name, StringLength, StringLengthExp, |
| 958 | StringLocationExp, SizeInBits, AlignInBits, Encoding)); |
| 959 | Metadata *Ops[] = {nullptr, nullptr, Name, |
| 960 | SizeInBits, nullptr, StringLength, |
| 961 | StringLengthExp, StringLocationExp}; |
| 962 | DEFINE_GETIMPL_STORE(DIStringType, (Tag, AlignInBits, Encoding), Ops); |
| 963 | } |
| 964 | DIType *DIDerivedType::getClassType() const { |
| 965 | assert(getTag() == dwarf::DW_TAG_ptr_to_member_type); |
| 966 | return cast_or_null<DIType>(Val: getExtraData()); |
| 967 | } |
| 968 | |
| 969 | // Helper function to extract ConstantAsMetadata from ExtraData, |
| 970 | // handling extra data MDTuple unwrapping if needed. |
| 971 | static ConstantAsMetadata *(Metadata *) { |
| 972 | Metadata *ED = ExtraData; |
| 973 | if (auto *Tuple = dyn_cast_or_null<MDTuple>(Val: ED)) { |
| 974 | if (Tuple->getNumOperands() != 1) |
| 975 | return nullptr; |
| 976 | ED = Tuple->getOperand(I: 0); |
| 977 | } |
| 978 | return cast_or_null<ConstantAsMetadata>(Val: ED); |
| 979 | } |
| 980 | |
| 981 | uint32_t DIDerivedType::getVBPtrOffset() const { |
| 982 | assert(getTag() == dwarf::DW_TAG_inheritance); |
| 983 | if (auto *CM = extractConstantMetadata(ExtraData: getExtraData())) |
| 984 | if (auto *CI = dyn_cast_or_null<ConstantInt>(Val: CM->getValue())) |
| 985 | return static_cast<uint32_t>(CI->getZExtValue()); |
| 986 | return 0; |
| 987 | } |
| 988 | Constant *DIDerivedType::getStorageOffsetInBits() const { |
| 989 | assert(getTag() == dwarf::DW_TAG_member && isBitField()); |
| 990 | if (auto *C = extractConstantMetadata(ExtraData: getExtraData())) |
| 991 | return C->getValue(); |
| 992 | return nullptr; |
| 993 | } |
| 994 | |
| 995 | Constant *DIDerivedType::getConstant() const { |
| 996 | assert((getTag() == dwarf::DW_TAG_member || |
| 997 | getTag() == dwarf::DW_TAG_variable) && |
| 998 | isStaticMember()); |
| 999 | if (auto *C = extractConstantMetadata(ExtraData: getExtraData())) |
| 1000 | return C->getValue(); |
| 1001 | return nullptr; |
| 1002 | } |
| 1003 | Constant *DIDerivedType::getDiscriminantValue() const { |
| 1004 | assert(getTag() == dwarf::DW_TAG_member && !isStaticMember()); |
| 1005 | if (auto *C = extractConstantMetadata(ExtraData: getExtraData())) |
| 1006 | return C->getValue(); |
| 1007 | return nullptr; |
| 1008 | } |
| 1009 | |
| 1010 | DIDerivedType *DIDerivedType::getImpl( |
| 1011 | LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, |
| 1012 | unsigned Line, Metadata *Scope, Metadata *BaseType, Metadata *SizeInBits, |
| 1013 | uint32_t AlignInBits, Metadata *OffsetInBits, |
| 1014 | std::optional<unsigned> DWARFAddressSpace, |
| 1015 | std::optional<PtrAuthData> PtrAuthData, DIFlags Flags, Metadata *, |
| 1016 | Metadata *Annotations, StorageType Storage, bool ShouldCreate) { |
| 1017 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1018 | DEFINE_GETIMPL_LOOKUP(DIDerivedType, |
| 1019 | (Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 1020 | AlignInBits, OffsetInBits, DWARFAddressSpace, |
| 1021 | PtrAuthData, Flags, ExtraData, Annotations)); |
| 1022 | Metadata *Ops[] = {File, Scope, Name, SizeInBits, |
| 1023 | OffsetInBits, BaseType, ExtraData, Annotations}; |
| 1024 | DEFINE_GETIMPL_STORE( |
| 1025 | DIDerivedType, |
| 1026 | (Tag, Line, AlignInBits, DWARFAddressSpace, PtrAuthData, Flags), Ops); |
| 1027 | } |
| 1028 | |
| 1029 | std::optional<DIDerivedType::PtrAuthData> |
| 1030 | DIDerivedType::getPtrAuthData() const { |
| 1031 | return getTag() == dwarf::DW_TAG_LLVM_ptrauth_type |
| 1032 | ? std::make_optional<PtrAuthData>(args: SubclassData32) |
| 1033 | : std::nullopt; |
| 1034 | } |
| 1035 | |
| 1036 | DICompositeType *DICompositeType::getImpl( |
| 1037 | LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, |
| 1038 | unsigned Line, Metadata *Scope, Metadata *BaseType, Metadata *SizeInBits, |
| 1039 | uint32_t AlignInBits, Metadata *OffsetInBits, DIFlags Flags, |
| 1040 | Metadata *Elements, unsigned RuntimeLang, std::optional<uint32_t> EnumKind, |
| 1041 | Metadata *VTableHolder, Metadata *TemplateParams, MDString *Identifier, |
| 1042 | Metadata *Discriminator, Metadata *DataLocation, Metadata *Associated, |
| 1043 | Metadata *Allocated, Metadata *Rank, Metadata *Annotations, |
| 1044 | Metadata *Specification, uint32_t , Metadata *BitStride, |
| 1045 | StorageType Storage, bool ShouldCreate) { |
| 1046 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1047 | |
| 1048 | // Keep this in sync with buildODRType. |
| 1049 | DEFINE_GETIMPL_LOOKUP( |
| 1050 | DICompositeType, |
| 1051 | (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, |
| 1052 | OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams, |
| 1053 | Identifier, Discriminator, DataLocation, Associated, Allocated, Rank, |
| 1054 | Annotations, Specification, NumExtraInhabitants, BitStride)); |
| 1055 | Metadata *Ops[] = {File, Scope, Name, SizeInBits, |
| 1056 | OffsetInBits, BaseType, Elements, VTableHolder, |
| 1057 | TemplateParams, Identifier, Discriminator, DataLocation, |
| 1058 | Associated, Allocated, Rank, Annotations, |
| 1059 | Specification, BitStride}; |
| 1060 | DEFINE_GETIMPL_STORE(DICompositeType, |
| 1061 | (Tag, Line, RuntimeLang, AlignInBits, |
| 1062 | NumExtraInhabitants, EnumKind, Flags), |
| 1063 | Ops); |
| 1064 | } |
| 1065 | |
| 1066 | DICompositeType *DICompositeType::buildODRType( |
| 1067 | LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, |
| 1068 | Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, |
| 1069 | Metadata *SizeInBits, uint32_t AlignInBits, Metadata *OffsetInBits, |
| 1070 | Metadata *Specification, uint32_t , DIFlags Flags, |
| 1071 | Metadata *Elements, unsigned RuntimeLang, std::optional<uint32_t> EnumKind, |
| 1072 | Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, |
| 1073 | Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, |
| 1074 | Metadata *Rank, Metadata *Annotations, Metadata *BitStride) { |
| 1075 | assert(!Identifier.getString().empty() && "Expected valid identifier" ); |
| 1076 | if (!Context.isODRUniquingDebugTypes()) |
| 1077 | return nullptr; |
| 1078 | auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; |
| 1079 | if (!CT) |
| 1080 | return CT = DICompositeType::getDistinct( |
| 1081 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 1082 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
| 1083 | EnumKind, VTableHolder, TemplateParams, Identifier: &Identifier, |
| 1084 | Discriminator, DataLocation, Associated, Allocated, Rank, |
| 1085 | Annotations, Specification, NumExtraInhabitants, BitStride); |
| 1086 | if (CT->getTag() != Tag) |
| 1087 | return nullptr; |
| 1088 | |
| 1089 | // Only mutate CT if it's a forward declaration and the new operands aren't. |
| 1090 | assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?" ); |
| 1091 | if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl)) |
| 1092 | return CT; |
| 1093 | |
| 1094 | // Mutate CT in place. Keep this in sync with getImpl. |
| 1095 | CT->mutate(Tag, Line, RuntimeLang, AlignInBits, NumExtraInhabitants, EnumKind, |
| 1096 | Flags); |
| 1097 | Metadata *Ops[] = {File, Scope, Name, SizeInBits, |
| 1098 | OffsetInBits, BaseType, Elements, VTableHolder, |
| 1099 | TemplateParams, &Identifier, Discriminator, DataLocation, |
| 1100 | Associated, Allocated, Rank, Annotations, |
| 1101 | Specification, BitStride}; |
| 1102 | assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() && |
| 1103 | "Mismatched number of operands" ); |
| 1104 | for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I) |
| 1105 | if (Ops[I] != CT->getOperand(I)) |
| 1106 | CT->setOperand(I, New: Ops[I]); |
| 1107 | return CT; |
| 1108 | } |
| 1109 | |
| 1110 | DICompositeType *DICompositeType::getODRType( |
| 1111 | LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, |
| 1112 | Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, |
| 1113 | Metadata *SizeInBits, uint32_t AlignInBits, Metadata *OffsetInBits, |
| 1114 | Metadata *Specification, uint32_t , DIFlags Flags, |
| 1115 | Metadata *Elements, unsigned RuntimeLang, std::optional<uint32_t> EnumKind, |
| 1116 | Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, |
| 1117 | Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, |
| 1118 | Metadata *Rank, Metadata *Annotations, Metadata *BitStride) { |
| 1119 | assert(!Identifier.getString().empty() && "Expected valid identifier" ); |
| 1120 | if (!Context.isODRUniquingDebugTypes()) |
| 1121 | return nullptr; |
| 1122 | auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; |
| 1123 | if (!CT) { |
| 1124 | CT = DICompositeType::getDistinct( |
| 1125 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 1126 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, EnumKind, |
| 1127 | VTableHolder, TemplateParams, Identifier: &Identifier, Discriminator, DataLocation, |
| 1128 | Associated, Allocated, Rank, Annotations, Specification, |
| 1129 | NumExtraInhabitants, BitStride); |
| 1130 | } else { |
| 1131 | if (CT->getTag() != Tag) |
| 1132 | return nullptr; |
| 1133 | } |
| 1134 | return CT; |
| 1135 | } |
| 1136 | |
| 1137 | DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context, |
| 1138 | MDString &Identifier) { |
| 1139 | assert(!Identifier.getString().empty() && "Expected valid identifier" ); |
| 1140 | if (!Context.isODRUniquingDebugTypes()) |
| 1141 | return nullptr; |
| 1142 | return Context.pImpl->DITypeMap->lookup(Val: &Identifier); |
| 1143 | } |
| 1144 | DISubroutineType::DISubroutineType(LLVMContext &C, StorageType Storage, |
| 1145 | DIFlags Flags, uint8_t CC, |
| 1146 | ArrayRef<Metadata *> Ops) |
| 1147 | : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type, 0, |
| 1148 | 0, 0, Flags, Ops), |
| 1149 | CC(CC) {} |
| 1150 | |
| 1151 | DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags, |
| 1152 | uint8_t CC, Metadata *TypeArray, |
| 1153 | StorageType Storage, |
| 1154 | bool ShouldCreate) { |
| 1155 | DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray)); |
| 1156 | Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr, nullptr, TypeArray}; |
| 1157 | DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops); |
| 1158 | } |
| 1159 | |
| 1160 | DIFile::DIFile(LLVMContext &C, StorageType Storage, |
| 1161 | std::optional<ChecksumInfo<MDString *>> CS, MDString *Src, |
| 1162 | ArrayRef<Metadata *> Ops) |
| 1163 | : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops), |
| 1164 | Checksum(CS), Source(Src) {} |
| 1165 | |
| 1166 | // FIXME: Implement this string-enum correspondence with a .def file and macros, |
| 1167 | // so that the association is explicit rather than implied. |
| 1168 | static const char *ChecksumKindName[DIFile::CSK_Last] = { |
| 1169 | "CSK_MD5" , |
| 1170 | "CSK_SHA1" , |
| 1171 | "CSK_SHA256" , |
| 1172 | }; |
| 1173 | |
| 1174 | StringRef DIFile::getChecksumKindAsString(ChecksumKind CSKind) { |
| 1175 | assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind" ); |
| 1176 | // The first space was originally the CSK_None variant, which is now |
| 1177 | // obsolete, but the space is still reserved in ChecksumKind, so we account |
| 1178 | // for it here. |
| 1179 | return ChecksumKindName[CSKind - 1]; |
| 1180 | } |
| 1181 | |
| 1182 | std::optional<DIFile::ChecksumKind> |
| 1183 | DIFile::getChecksumKind(StringRef CSKindStr) { |
| 1184 | return StringSwitch<std::optional<DIFile::ChecksumKind>>(CSKindStr) |
| 1185 | .Case(S: "CSK_MD5" , Value: DIFile::CSK_MD5) |
| 1186 | .Case(S: "CSK_SHA1" , Value: DIFile::CSK_SHA1) |
| 1187 | .Case(S: "CSK_SHA256" , Value: DIFile::CSK_SHA256) |
| 1188 | .Default(Value: std::nullopt); |
| 1189 | } |
| 1190 | |
| 1191 | DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename, |
| 1192 | MDString *Directory, |
| 1193 | std::optional<DIFile::ChecksumInfo<MDString *>> CS, |
| 1194 | MDString *Source, StorageType Storage, |
| 1195 | bool ShouldCreate) { |
| 1196 | assert(isCanonical(Filename) && "Expected canonical MDString" ); |
| 1197 | assert(isCanonical(Directory) && "Expected canonical MDString" ); |
| 1198 | assert((!CS || isCanonical(CS->Value)) && "Expected canonical MDString" ); |
| 1199 | // We do *NOT* expect Source to be a canonical MDString because nullptr |
| 1200 | // means none, so we need something to represent the empty file. |
| 1201 | DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CS, Source)); |
| 1202 | Metadata *Ops[] = {Filename, Directory, CS ? CS->Value : nullptr, Source}; |
| 1203 | DEFINE_GETIMPL_STORE(DIFile, (CS, Source), Ops); |
| 1204 | } |
| 1205 | DICompileUnit::DICompileUnit(LLVMContext &C, StorageType Storage, |
| 1206 | DISourceLanguageName SourceLanguage, |
| 1207 | bool IsOptimized, unsigned RuntimeVersion, |
| 1208 | unsigned EmissionKind, uint64_t DWOId, |
| 1209 | bool SplitDebugInlining, |
| 1210 | bool DebugInfoForProfiling, unsigned NameTableKind, |
| 1211 | bool RangesBaseAddress, ArrayRef<Metadata *> Ops) |
| 1212 | : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops), |
| 1213 | SourceLanguage(SourceLanguage), RuntimeVersion(RuntimeVersion), |
| 1214 | DWOId(DWOId), EmissionKind(EmissionKind), NameTableKind(NameTableKind), |
| 1215 | IsOptimized(IsOptimized), SplitDebugInlining(SplitDebugInlining), |
| 1216 | DebugInfoForProfiling(DebugInfoForProfiling), |
| 1217 | RangesBaseAddress(RangesBaseAddress) { |
| 1218 | assert(Storage != Uniqued); |
| 1219 | } |
| 1220 | |
| 1221 | DICompileUnit *DICompileUnit::getImpl( |
| 1222 | LLVMContext &Context, DISourceLanguageName SourceLanguage, Metadata *File, |
| 1223 | MDString *Producer, bool IsOptimized, MDString *Flags, |
| 1224 | unsigned RuntimeVersion, MDString *SplitDebugFilename, |
| 1225 | unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, |
| 1226 | Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros, |
| 1227 | uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, |
| 1228 | unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot, |
| 1229 | MDString *SDK, StorageType Storage, bool ShouldCreate) { |
| 1230 | assert(Storage != Uniqued && "Cannot unique DICompileUnit" ); |
| 1231 | assert(isCanonical(Producer) && "Expected canonical MDString" ); |
| 1232 | assert(isCanonical(Flags) && "Expected canonical MDString" ); |
| 1233 | assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString" ); |
| 1234 | |
| 1235 | Metadata *Ops[] = {File, |
| 1236 | Producer, |
| 1237 | Flags, |
| 1238 | SplitDebugFilename, |
| 1239 | EnumTypes, |
| 1240 | RetainedTypes, |
| 1241 | GlobalVariables, |
| 1242 | ImportedEntities, |
| 1243 | Macros, |
| 1244 | SysRoot, |
| 1245 | SDK}; |
| 1246 | return storeImpl(N: new (std::size(Ops), Storage) DICompileUnit( |
| 1247 | Context, Storage, SourceLanguage, IsOptimized, |
| 1248 | RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining, |
| 1249 | DebugInfoForProfiling, NameTableKind, RangesBaseAddress, |
| 1250 | Ops), |
| 1251 | Storage); |
| 1252 | } |
| 1253 | |
| 1254 | std::optional<DICompileUnit::DebugEmissionKind> |
| 1255 | DICompileUnit::getEmissionKind(StringRef Str) { |
| 1256 | return StringSwitch<std::optional<DebugEmissionKind>>(Str) |
| 1257 | .Case(S: "NoDebug" , Value: NoDebug) |
| 1258 | .Case(S: "FullDebug" , Value: FullDebug) |
| 1259 | .Case(S: "LineTablesOnly" , Value: LineTablesOnly) |
| 1260 | .Case(S: "DebugDirectivesOnly" , Value: DebugDirectivesOnly) |
| 1261 | .Default(Value: std::nullopt); |
| 1262 | } |
| 1263 | |
| 1264 | std::optional<DICompileUnit::DebugNameTableKind> |
| 1265 | DICompileUnit::getNameTableKind(StringRef Str) { |
| 1266 | return StringSwitch<std::optional<DebugNameTableKind>>(Str) |
| 1267 | .Case(S: "Default" , Value: DebugNameTableKind::Default) |
| 1268 | .Case(S: "GNU" , Value: DebugNameTableKind::GNU) |
| 1269 | .Case(S: "Apple" , Value: DebugNameTableKind::Apple) |
| 1270 | .Case(S: "None" , Value: DebugNameTableKind::None) |
| 1271 | .Default(Value: std::nullopt); |
| 1272 | } |
| 1273 | |
| 1274 | const char *DICompileUnit::emissionKindString(DebugEmissionKind EK) { |
| 1275 | switch (EK) { |
| 1276 | case NoDebug: |
| 1277 | return "NoDebug" ; |
| 1278 | case FullDebug: |
| 1279 | return "FullDebug" ; |
| 1280 | case LineTablesOnly: |
| 1281 | return "LineTablesOnly" ; |
| 1282 | case DebugDirectivesOnly: |
| 1283 | return "DebugDirectivesOnly" ; |
| 1284 | } |
| 1285 | return nullptr; |
| 1286 | } |
| 1287 | |
| 1288 | const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) { |
| 1289 | switch (NTK) { |
| 1290 | case DebugNameTableKind::Default: |
| 1291 | return nullptr; |
| 1292 | case DebugNameTableKind::GNU: |
| 1293 | return "GNU" ; |
| 1294 | case DebugNameTableKind::Apple: |
| 1295 | return "Apple" ; |
| 1296 | case DebugNameTableKind::None: |
| 1297 | return "None" ; |
| 1298 | } |
| 1299 | return nullptr; |
| 1300 | } |
| 1301 | DISubprogram::DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line, |
| 1302 | unsigned ScopeLine, unsigned VirtualIndex, |
| 1303 | int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, |
| 1304 | bool UsesKeyInstructions, ArrayRef<Metadata *> Ops) |
| 1305 | : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram, Ops), |
| 1306 | Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex), |
| 1307 | ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags) { |
| 1308 | static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range" ); |
| 1309 | SubclassData1 = UsesKeyInstructions; |
| 1310 | } |
| 1311 | DISubprogram::DISPFlags |
| 1312 | DISubprogram::toSPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized, |
| 1313 | unsigned Virtuality, bool IsMainSubprogram) { |
| 1314 | // We're assuming virtuality is the low-order field. |
| 1315 | static_assert(int(SPFlagVirtual) == int(dwarf::DW_VIRTUALITY_virtual) && |
| 1316 | int(SPFlagPureVirtual) == |
| 1317 | int(dwarf::DW_VIRTUALITY_pure_virtual), |
| 1318 | "Virtuality constant mismatch" ); |
| 1319 | return static_cast<DISPFlags>( |
| 1320 | (Virtuality & SPFlagVirtuality) | |
| 1321 | (IsLocalToUnit ? SPFlagLocalToUnit : SPFlagZero) | |
| 1322 | (IsDefinition ? SPFlagDefinition : SPFlagZero) | |
| 1323 | (IsOptimized ? SPFlagOptimized : SPFlagZero) | |
| 1324 | (IsMainSubprogram ? SPFlagMainSubprogram : SPFlagZero)); |
| 1325 | } |
| 1326 | |
| 1327 | DISubprogram *DILocalScope::getSubprogram() const { |
| 1328 | if (auto *Block = dyn_cast<DILexicalBlockBase>(Val: this)) |
| 1329 | return Block->getScope()->getSubprogram(); |
| 1330 | return const_cast<DISubprogram *>(cast<DISubprogram>(Val: this)); |
| 1331 | } |
| 1332 | |
| 1333 | DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const { |
| 1334 | if (auto *File = dyn_cast<DILexicalBlockFile>(Val: this)) |
| 1335 | return File->getScope()->getNonLexicalBlockFileScope(); |
| 1336 | return const_cast<DILocalScope *>(this); |
| 1337 | } |
| 1338 | |
| 1339 | DILocalScope *DILocalScope::cloneScopeForSubprogram( |
| 1340 | DILocalScope &RootScope, DISubprogram &NewSP, LLVMContext &Ctx, |
| 1341 | DenseMap<const MDNode *, MDNode *> &Cache) { |
| 1342 | SmallVector<DIScope *> ScopeChain; |
| 1343 | DIScope *CachedResult = nullptr; |
| 1344 | |
| 1345 | for (DIScope *Scope = &RootScope; !isa<DISubprogram>(Val: Scope); |
| 1346 | Scope = Scope->getScope()) { |
| 1347 | if (auto It = Cache.find(Val: Scope); It != Cache.end()) { |
| 1348 | CachedResult = cast<DIScope>(Val: It->second); |
| 1349 | break; |
| 1350 | } |
| 1351 | ScopeChain.push_back(Elt: Scope); |
| 1352 | } |
| 1353 | |
| 1354 | // Recreate the scope chain, bottom-up, starting at the new subprogram (or a |
| 1355 | // cached result). |
| 1356 | DIScope *UpdatedScope = CachedResult ? CachedResult : &NewSP; |
| 1357 | for (DIScope *ScopeToUpdate : reverse(C&: ScopeChain)) { |
| 1358 | UpdatedScope = cloneAndReplaceParentScope( |
| 1359 | LBB: cast<DILexicalBlockBase>(Val: ScopeToUpdate), NewParent: UpdatedScope); |
| 1360 | Cache[ScopeToUpdate] = UpdatedScope; |
| 1361 | } |
| 1362 | |
| 1363 | return cast<DILocalScope>(Val: UpdatedScope); |
| 1364 | } |
| 1365 | |
| 1366 | DISubprogram::DISPFlags DISubprogram::getFlag(StringRef Flag) { |
| 1367 | return StringSwitch<DISPFlags>(Flag) |
| 1368 | #define HANDLE_DISP_FLAG(ID, NAME) .Case("DISPFlag" #NAME, SPFlag##NAME) |
| 1369 | #include "llvm/IR/DebugInfoFlags.def" |
| 1370 | .Default(Value: SPFlagZero); |
| 1371 | } |
| 1372 | |
| 1373 | StringRef DISubprogram::getFlagString(DISPFlags Flag) { |
| 1374 | switch (Flag) { |
| 1375 | // Appease a warning. |
| 1376 | case SPFlagVirtuality: |
| 1377 | return "" ; |
| 1378 | #define HANDLE_DISP_FLAG(ID, NAME) \ |
| 1379 | case SPFlag##NAME: \ |
| 1380 | return "DISPFlag" #NAME; |
| 1381 | #include "llvm/IR/DebugInfoFlags.def" |
| 1382 | } |
| 1383 | return "" ; |
| 1384 | } |
| 1385 | |
| 1386 | DISubprogram::DISPFlags |
| 1387 | DISubprogram::splitFlags(DISPFlags Flags, |
| 1388 | SmallVectorImpl<DISPFlags> &SplitFlags) { |
| 1389 | // Multi-bit fields can require special handling. In our case, however, the |
| 1390 | // only multi-bit field is virtuality, and all its values happen to be |
| 1391 | // single-bit values, so the right behavior just falls out. |
| 1392 | #define HANDLE_DISP_FLAG(ID, NAME) \ |
| 1393 | if (DISPFlags Bit = Flags & SPFlag##NAME) { \ |
| 1394 | SplitFlags.push_back(Bit); \ |
| 1395 | Flags &= ~Bit; \ |
| 1396 | } |
| 1397 | #include "llvm/IR/DebugInfoFlags.def" |
| 1398 | return Flags; |
| 1399 | } |
| 1400 | |
| 1401 | DISubprogram *DISubprogram::getImpl( |
| 1402 | LLVMContext &Context, Metadata *Scope, MDString *Name, |
| 1403 | MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, |
| 1404 | unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex, |
| 1405 | int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, |
| 1406 | Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes, |
| 1407 | Metadata *ThrownTypes, Metadata *Annotations, MDString *TargetFuncName, |
| 1408 | bool UsesKeyInstructions, StorageType Storage, bool ShouldCreate) { |
| 1409 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1410 | assert(isCanonical(LinkageName) && "Expected canonical MDString" ); |
| 1411 | assert(isCanonical(TargetFuncName) && "Expected canonical MDString" ); |
| 1412 | DEFINE_GETIMPL_LOOKUP(DISubprogram, |
| 1413 | (Scope, Name, LinkageName, File, Line, Type, ScopeLine, |
| 1414 | ContainingType, VirtualIndex, ThisAdjustment, Flags, |
| 1415 | SPFlags, Unit, TemplateParams, Declaration, |
| 1416 | RetainedNodes, ThrownTypes, Annotations, |
| 1417 | TargetFuncName, UsesKeyInstructions)); |
| 1418 | SmallVector<Metadata *, 13> Ops = { |
| 1419 | File, Scope, Name, LinkageName, |
| 1420 | Type, Unit, Declaration, RetainedNodes, |
| 1421 | ContainingType, TemplateParams, ThrownTypes, Annotations, |
| 1422 | TargetFuncName}; |
| 1423 | if (!TargetFuncName) { |
| 1424 | Ops.pop_back(); |
| 1425 | if (!Annotations) { |
| 1426 | Ops.pop_back(); |
| 1427 | if (!ThrownTypes) { |
| 1428 | Ops.pop_back(); |
| 1429 | if (!TemplateParams) { |
| 1430 | Ops.pop_back(); |
| 1431 | if (!ContainingType) |
| 1432 | Ops.pop_back(); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | DEFINE_GETIMPL_STORE_N(DISubprogram, |
| 1438 | (Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags, |
| 1439 | SPFlags, UsesKeyInstructions), |
| 1440 | Ops, Ops.size()); |
| 1441 | } |
| 1442 | |
| 1443 | bool DISubprogram::describes(const Function *F) const { |
| 1444 | assert(F && "Invalid function" ); |
| 1445 | return F->getSubprogram() == this; |
| 1446 | } |
| 1447 | |
| 1448 | const DIScope *DISubprogram::getRawRetainedNodeScope(const MDNode *N) { |
| 1449 | return visitRetainedNode<DIScope *>( |
| 1450 | N, FuncLV: [](const DILocalVariable *LV) { return LV->getScope(); }, |
| 1451 | FuncLabel: [](const DILabel *L) { return L->getScope(); }, |
| 1452 | FuncIE: [](const DIImportedEntity *IE) { return IE->getScope(); }, |
| 1453 | FuncUnknown: [](const Metadata *N) { return nullptr; }); |
| 1454 | } |
| 1455 | |
| 1456 | const DILocalScope *DISubprogram::getRetainedNodeScope(const MDNode *N) { |
| 1457 | return cast<DILocalScope>(Val: getRawRetainedNodeScope(N)); |
| 1458 | } |
| 1459 | |
| 1460 | DILexicalBlockBase::DILexicalBlockBase(LLVMContext &C, unsigned ID, |
| 1461 | StorageType Storage, |
| 1462 | ArrayRef<Metadata *> Ops) |
| 1463 | : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {} |
| 1464 | |
| 1465 | DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, |
| 1466 | Metadata *File, unsigned Line, |
| 1467 | unsigned Column, StorageType Storage, |
| 1468 | bool ShouldCreate) { |
| 1469 | // Fixup column. |
| 1470 | adjustColumn(Column); |
| 1471 | |
| 1472 | assert(Scope && "Expected scope" ); |
| 1473 | DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column)); |
| 1474 | Metadata *Ops[] = {File, Scope}; |
| 1475 | DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops); |
| 1476 | } |
| 1477 | |
| 1478 | DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context, |
| 1479 | Metadata *Scope, Metadata *File, |
| 1480 | unsigned Discriminator, |
| 1481 | StorageType Storage, |
| 1482 | bool ShouldCreate) { |
| 1483 | assert(Scope && "Expected scope" ); |
| 1484 | DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator)); |
| 1485 | Metadata *Ops[] = {File, Scope}; |
| 1486 | DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops); |
| 1487 | } |
| 1488 | |
| 1489 | DINamespace::DINamespace(LLVMContext &Context, StorageType Storage, |
| 1490 | bool ExportSymbols, ArrayRef<Metadata *> Ops) |
| 1491 | : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace, Ops) { |
| 1492 | SubclassData1 = ExportSymbols; |
| 1493 | } |
| 1494 | DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope, |
| 1495 | MDString *Name, bool ExportSymbols, |
| 1496 | StorageType Storage, bool ShouldCreate) { |
| 1497 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1498 | DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols)); |
| 1499 | // The nullptr is for DIScope's File operand. This should be refactored. |
| 1500 | Metadata *Ops[] = {nullptr, Scope, Name}; |
| 1501 | DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops); |
| 1502 | } |
| 1503 | |
| 1504 | DICommonBlock::DICommonBlock(LLVMContext &Context, StorageType Storage, |
| 1505 | unsigned LineNo, ArrayRef<Metadata *> Ops) |
| 1506 | : DIScope(Context, DICommonBlockKind, Storage, dwarf::DW_TAG_common_block, |
| 1507 | Ops) { |
| 1508 | SubclassData32 = LineNo; |
| 1509 | } |
| 1510 | DICommonBlock *DICommonBlock::getImpl(LLVMContext &Context, Metadata *Scope, |
| 1511 | Metadata *Decl, MDString *Name, |
| 1512 | Metadata *File, unsigned LineNo, |
| 1513 | StorageType Storage, bool ShouldCreate) { |
| 1514 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1515 | DEFINE_GETIMPL_LOOKUP(DICommonBlock, (Scope, Decl, Name, File, LineNo)); |
| 1516 | // The nullptr is for DIScope's File operand. This should be refactored. |
| 1517 | Metadata *Ops[] = {Scope, Decl, Name, File}; |
| 1518 | DEFINE_GETIMPL_STORE(DICommonBlock, (LineNo), Ops); |
| 1519 | } |
| 1520 | |
| 1521 | DIModule::DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo, |
| 1522 | bool IsDecl, ArrayRef<Metadata *> Ops) |
| 1523 | : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) { |
| 1524 | SubclassData1 = IsDecl; |
| 1525 | SubclassData32 = LineNo; |
| 1526 | } |
| 1527 | DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *File, |
| 1528 | Metadata *Scope, MDString *Name, |
| 1529 | MDString *ConfigurationMacros, |
| 1530 | MDString *IncludePath, MDString *APINotesFile, |
| 1531 | unsigned LineNo, bool IsDecl, StorageType Storage, |
| 1532 | bool ShouldCreate) { |
| 1533 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1534 | DEFINE_GETIMPL_LOOKUP(DIModule, (File, Scope, Name, ConfigurationMacros, |
| 1535 | IncludePath, APINotesFile, LineNo, IsDecl)); |
| 1536 | Metadata *Ops[] = {File, Scope, Name, ConfigurationMacros, |
| 1537 | IncludePath, APINotesFile}; |
| 1538 | DEFINE_GETIMPL_STORE(DIModule, (LineNo, IsDecl), Ops); |
| 1539 | } |
| 1540 | DITemplateTypeParameter::DITemplateTypeParameter(LLVMContext &Context, |
| 1541 | StorageType Storage, |
| 1542 | bool IsDefault, |
| 1543 | ArrayRef<Metadata *> Ops) |
| 1544 | : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage, |
| 1545 | dwarf::DW_TAG_template_type_parameter, IsDefault, |
| 1546 | Ops) {} |
| 1547 | |
| 1548 | DITemplateTypeParameter * |
| 1549 | DITemplateTypeParameter::getImpl(LLVMContext &Context, MDString *Name, |
| 1550 | Metadata *Type, bool isDefault, |
| 1551 | StorageType Storage, bool ShouldCreate) { |
| 1552 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1553 | DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type, isDefault)); |
| 1554 | Metadata *Ops[] = {Name, Type}; |
| 1555 | DEFINE_GETIMPL_STORE(DITemplateTypeParameter, (isDefault), Ops); |
| 1556 | } |
| 1557 | |
| 1558 | DITemplateValueParameter *DITemplateValueParameter::getImpl( |
| 1559 | LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type, |
| 1560 | bool isDefault, Metadata *Value, StorageType Storage, bool ShouldCreate) { |
| 1561 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1562 | DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, |
| 1563 | (Tag, Name, Type, isDefault, Value)); |
| 1564 | Metadata *Ops[] = {Name, Type, Value}; |
| 1565 | DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag, isDefault), Ops); |
| 1566 | } |
| 1567 | |
| 1568 | DIGlobalVariable * |
| 1569 | DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, |
| 1570 | MDString *LinkageName, Metadata *File, unsigned Line, |
| 1571 | Metadata *Type, bool IsLocalToUnit, bool IsDefinition, |
| 1572 | Metadata *StaticDataMemberDeclaration, |
| 1573 | Metadata *TemplateParams, uint32_t AlignInBits, |
| 1574 | Metadata *Annotations, StorageType Storage, |
| 1575 | bool ShouldCreate) { |
| 1576 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1577 | assert(isCanonical(LinkageName) && "Expected canonical MDString" ); |
| 1578 | DEFINE_GETIMPL_LOOKUP( |
| 1579 | DIGlobalVariable, |
| 1580 | (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition, |
| 1581 | StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations)); |
| 1582 | Metadata *Ops[] = {Scope, |
| 1583 | Name, |
| 1584 | File, |
| 1585 | Type, |
| 1586 | Name, |
| 1587 | LinkageName, |
| 1588 | StaticDataMemberDeclaration, |
| 1589 | TemplateParams, |
| 1590 | Annotations}; |
| 1591 | DEFINE_GETIMPL_STORE(DIGlobalVariable, |
| 1592 | (Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops); |
| 1593 | } |
| 1594 | |
| 1595 | DILocalVariable * |
| 1596 | DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, |
| 1597 | Metadata *File, unsigned Line, Metadata *Type, |
| 1598 | unsigned Arg, DIFlags Flags, uint32_t AlignInBits, |
| 1599 | Metadata *Annotations, StorageType Storage, |
| 1600 | bool ShouldCreate) { |
| 1601 | // 64K ought to be enough for any frontend. |
| 1602 | assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits" ); |
| 1603 | |
| 1604 | assert(Scope && "Expected scope" ); |
| 1605 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1606 | DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Scope, Name, File, Line, Type, Arg, |
| 1607 | Flags, AlignInBits, Annotations)); |
| 1608 | Metadata *Ops[] = {Scope, Name, File, Type, Annotations}; |
| 1609 | DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops); |
| 1610 | } |
| 1611 | |
| 1612 | DIVariable::DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, |
| 1613 | signed Line, ArrayRef<Metadata *> Ops, |
| 1614 | uint32_t AlignInBits) |
| 1615 | : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line) { |
| 1616 | SubclassData32 = AlignInBits; |
| 1617 | } |
| 1618 | std::optional<uint64_t> DIVariable::getSizeInBits() const { |
| 1619 | // This is used by the Verifier so be mindful of broken types. |
| 1620 | const Metadata *RawType = getRawType(); |
| 1621 | while (RawType) { |
| 1622 | // Try to get the size directly. |
| 1623 | if (auto *T = dyn_cast<DIType>(Val: RawType)) |
| 1624 | if (uint64_t Size = T->getSizeInBits()) |
| 1625 | return Size; |
| 1626 | |
| 1627 | if (auto *DT = dyn_cast<DIDerivedType>(Val: RawType)) { |
| 1628 | // Look at the base type. |
| 1629 | RawType = DT->getRawBaseType(); |
| 1630 | continue; |
| 1631 | } |
| 1632 | |
| 1633 | // Missing type or size. |
| 1634 | break; |
| 1635 | } |
| 1636 | |
| 1637 | // Fail gracefully. |
| 1638 | return std::nullopt; |
| 1639 | } |
| 1640 | |
| 1641 | DILabel::DILabel(LLVMContext &C, StorageType Storage, unsigned Line, |
| 1642 | unsigned Column, bool IsArtificial, |
| 1643 | std::optional<unsigned> CoroSuspendIdx, |
| 1644 | ArrayRef<Metadata *> Ops) |
| 1645 | : DINode(C, DILabelKind, Storage, dwarf::DW_TAG_label, Ops) { |
| 1646 | this->SubclassData32 = Line; |
| 1647 | this->Column = Column; |
| 1648 | this->IsArtificial = IsArtificial; |
| 1649 | this->CoroSuspendIdx = CoroSuspendIdx; |
| 1650 | } |
| 1651 | DILabel *DILabel::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, |
| 1652 | Metadata *File, unsigned Line, unsigned Column, |
| 1653 | bool IsArtificial, |
| 1654 | std::optional<unsigned> CoroSuspendIdx, |
| 1655 | StorageType Storage, bool ShouldCreate) { |
| 1656 | assert(Scope && "Expected scope" ); |
| 1657 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 1658 | DEFINE_GETIMPL_LOOKUP( |
| 1659 | DILabel, (Scope, Name, File, Line, Column, IsArtificial, CoroSuspendIdx)); |
| 1660 | Metadata *Ops[] = {Scope, Name, File}; |
| 1661 | DEFINE_GETIMPL_STORE(DILabel, (Line, Column, IsArtificial, CoroSuspendIdx), |
| 1662 | Ops); |
| 1663 | } |
| 1664 | |
| 1665 | DIExpression *DIExpression::getImpl(LLVMContext &Context, |
| 1666 | ArrayRef<uint64_t> Elements, |
| 1667 | StorageType Storage, bool ShouldCreate) { |
| 1668 | DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements)); |
| 1669 | DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements)); |
| 1670 | } |
| 1671 | bool DIExpression::isEntryValue() const { |
| 1672 | if (auto singleLocElts = getSingleLocationExpressionElements()) { |
| 1673 | return singleLocElts->size() > 0 && |
| 1674 | (*singleLocElts)[0] == dwarf::DW_OP_LLVM_entry_value; |
| 1675 | } |
| 1676 | return false; |
| 1677 | } |
| 1678 | bool DIExpression::startsWithDeref() const { |
| 1679 | if (auto singleLocElts = getSingleLocationExpressionElements()) |
| 1680 | return singleLocElts->size() > 0 && |
| 1681 | (*singleLocElts)[0] == dwarf::DW_OP_deref; |
| 1682 | return false; |
| 1683 | } |
| 1684 | bool DIExpression::isDeref() const { |
| 1685 | if (auto singleLocElts = getSingleLocationExpressionElements()) |
| 1686 | return singleLocElts->size() == 1 && |
| 1687 | (*singleLocElts)[0] == dwarf::DW_OP_deref; |
| 1688 | return false; |
| 1689 | } |
| 1690 | |
| 1691 | DIAssignID *DIAssignID::getImpl(LLVMContext &Context, StorageType Storage, |
| 1692 | bool ShouldCreate) { |
| 1693 | // Uniqued DIAssignID are not supported as the instance address *is* the ID. |
| 1694 | assert(Storage != StorageType::Uniqued && "uniqued DIAssignID unsupported" ); |
| 1695 | return storeImpl(N: new (0u, Storage) DIAssignID(Context, Storage), Storage); |
| 1696 | } |
| 1697 | |
| 1698 | unsigned DIExpression::ExprOperand::getSize() const { |
| 1699 | uint64_t Op = getOp(); |
| 1700 | |
| 1701 | if (Op >= dwarf::DW_OP_breg0 && Op <= dwarf::DW_OP_breg31) |
| 1702 | return 2; |
| 1703 | |
| 1704 | switch (Op) { |
| 1705 | case dwarf::DW_OP_LLVM_convert: |
| 1706 | case dwarf::DW_OP_LLVM_fragment: |
| 1707 | case dwarf::DW_OP_LLVM_extract_bits_sext: |
| 1708 | case dwarf::DW_OP_LLVM_extract_bits_zext: |
| 1709 | case dwarf::DW_OP_bregx: |
| 1710 | return 3; |
| 1711 | case dwarf::DW_OP_constu: |
| 1712 | case dwarf::DW_OP_consts: |
| 1713 | case dwarf::DW_OP_deref_size: |
| 1714 | case dwarf::DW_OP_plus_uconst: |
| 1715 | case dwarf::DW_OP_LLVM_tag_offset: |
| 1716 | case dwarf::DW_OP_LLVM_entry_value: |
| 1717 | case dwarf::DW_OP_LLVM_arg: |
| 1718 | case dwarf::DW_OP_regx: |
| 1719 | return 2; |
| 1720 | default: |
| 1721 | return 1; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | bool DIExpression::isValid() const { |
| 1726 | for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) { |
| 1727 | // Check that there's space for the operand. |
| 1728 | if (I->get() + I->getSize() > E->get()) |
| 1729 | return false; |
| 1730 | |
| 1731 | uint64_t Op = I->getOp(); |
| 1732 | if ((Op >= dwarf::DW_OP_reg0 && Op <= dwarf::DW_OP_reg31) || |
| 1733 | (Op >= dwarf::DW_OP_breg0 && Op <= dwarf::DW_OP_breg31)) |
| 1734 | return true; |
| 1735 | |
| 1736 | // Check that the operand is valid. |
| 1737 | switch (Op) { |
| 1738 | default: |
| 1739 | return false; |
| 1740 | case dwarf::DW_OP_LLVM_fragment: |
| 1741 | // A fragment operator must appear at the end. |
| 1742 | return I->get() + I->getSize() == E->get(); |
| 1743 | case dwarf::DW_OP_stack_value: { |
| 1744 | // Must be the last one or followed by a DW_OP_LLVM_fragment. |
| 1745 | if (I->get() + I->getSize() == E->get()) |
| 1746 | break; |
| 1747 | auto J = I; |
| 1748 | if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment) |
| 1749 | return false; |
| 1750 | break; |
| 1751 | } |
| 1752 | case dwarf::DW_OP_swap: { |
| 1753 | // Must be more than one implicit element on the stack. |
| 1754 | |
| 1755 | // FIXME: A better way to implement this would be to add a local variable |
| 1756 | // that keeps track of the stack depth and introduce something like a |
| 1757 | // DW_LLVM_OP_implicit_location as a placeholder for the location this |
| 1758 | // DIExpression is attached to, or else pass the number of implicit stack |
| 1759 | // elements into isValid. |
| 1760 | if (getNumElements() == 1) |
| 1761 | return false; |
| 1762 | break; |
| 1763 | } |
| 1764 | case dwarf::DW_OP_LLVM_entry_value: { |
| 1765 | // An entry value operator must appear at the beginning or immediately |
| 1766 | // following `DW_OP_LLVM_arg 0`, and the number of operations it cover can |
| 1767 | // currently only be 1, because we support only entry values of a simple |
| 1768 | // register location. One reason for this is that we currently can't |
| 1769 | // calculate the size of the resulting DWARF block for other expressions. |
| 1770 | auto FirstOp = expr_op_begin(); |
| 1771 | if (FirstOp->getOp() == dwarf::DW_OP_LLVM_arg && FirstOp->getArg(I: 0) == 0) |
| 1772 | ++FirstOp; |
| 1773 | return I->get() == FirstOp->get() && I->getArg(I: 0) == 1; |
| 1774 | } |
| 1775 | case dwarf::DW_OP_LLVM_implicit_pointer: |
| 1776 | case dwarf::DW_OP_LLVM_convert: |
| 1777 | case dwarf::DW_OP_LLVM_arg: |
| 1778 | case dwarf::DW_OP_LLVM_tag_offset: |
| 1779 | case dwarf::DW_OP_LLVM_extract_bits_sext: |
| 1780 | case dwarf::DW_OP_LLVM_extract_bits_zext: |
| 1781 | case dwarf::DW_OP_constu: |
| 1782 | case dwarf::DW_OP_plus_uconst: |
| 1783 | case dwarf::DW_OP_plus: |
| 1784 | case dwarf::DW_OP_minus: |
| 1785 | case dwarf::DW_OP_mul: |
| 1786 | case dwarf::DW_OP_div: |
| 1787 | case dwarf::DW_OP_mod: |
| 1788 | case dwarf::DW_OP_or: |
| 1789 | case dwarf::DW_OP_and: |
| 1790 | case dwarf::DW_OP_xor: |
| 1791 | case dwarf::DW_OP_shl: |
| 1792 | case dwarf::DW_OP_shr: |
| 1793 | case dwarf::DW_OP_shra: |
| 1794 | case dwarf::DW_OP_deref: |
| 1795 | case dwarf::DW_OP_deref_size: |
| 1796 | case dwarf::DW_OP_xderef: |
| 1797 | case dwarf::DW_OP_lit0: |
| 1798 | case dwarf::DW_OP_not: |
| 1799 | case dwarf::DW_OP_dup: |
| 1800 | case dwarf::DW_OP_regx: |
| 1801 | case dwarf::DW_OP_bregx: |
| 1802 | case dwarf::DW_OP_push_object_address: |
| 1803 | case dwarf::DW_OP_over: |
| 1804 | case dwarf::DW_OP_rot: |
| 1805 | case dwarf::DW_OP_consts: |
| 1806 | case dwarf::DW_OP_eq: |
| 1807 | case dwarf::DW_OP_ne: |
| 1808 | case dwarf::DW_OP_gt: |
| 1809 | case dwarf::DW_OP_ge: |
| 1810 | case dwarf::DW_OP_lt: |
| 1811 | case dwarf::DW_OP_le: |
| 1812 | case dwarf::DW_OP_neg: |
| 1813 | case dwarf::DW_OP_abs: |
| 1814 | break; |
| 1815 | } |
| 1816 | } |
| 1817 | return true; |
| 1818 | } |
| 1819 | |
| 1820 | bool DIExpression::isImplicit() const { |
| 1821 | if (!isValid()) |
| 1822 | return false; |
| 1823 | |
| 1824 | if (getNumElements() == 0) |
| 1825 | return false; |
| 1826 | |
| 1827 | for (const auto &It : expr_ops()) { |
| 1828 | switch (It.getOp()) { |
| 1829 | default: |
| 1830 | break; |
| 1831 | case dwarf::DW_OP_stack_value: |
| 1832 | return true; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | return false; |
| 1837 | } |
| 1838 | |
| 1839 | bool DIExpression::isComplex() const { |
| 1840 | if (!isValid()) |
| 1841 | return false; |
| 1842 | |
| 1843 | if (getNumElements() == 0) |
| 1844 | return false; |
| 1845 | |
| 1846 | // If there are any elements other than fragment or tag_offset, then some |
| 1847 | // kind of complex computation occurs. |
| 1848 | for (const auto &It : expr_ops()) { |
| 1849 | switch (It.getOp()) { |
| 1850 | case dwarf::DW_OP_LLVM_tag_offset: |
| 1851 | case dwarf::DW_OP_LLVM_fragment: |
| 1852 | case dwarf::DW_OP_LLVM_arg: |
| 1853 | continue; |
| 1854 | default: |
| 1855 | return true; |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | return false; |
| 1860 | } |
| 1861 | |
| 1862 | bool DIExpression::isSingleLocationExpression() const { |
| 1863 | if (!isValid()) |
| 1864 | return false; |
| 1865 | |
| 1866 | if (getNumElements() == 0) |
| 1867 | return true; |
| 1868 | |
| 1869 | auto ExprOpBegin = expr_ops().begin(); |
| 1870 | auto ExprOpEnd = expr_ops().end(); |
| 1871 | if (ExprOpBegin->getOp() == dwarf::DW_OP_LLVM_arg) { |
| 1872 | if (ExprOpBegin->getArg(I: 0) != 0) |
| 1873 | return false; |
| 1874 | ++ExprOpBegin; |
| 1875 | } |
| 1876 | |
| 1877 | return !std::any_of(first: ExprOpBegin, last: ExprOpEnd, pred: [](auto Op) { |
| 1878 | return Op.getOp() == dwarf::DW_OP_LLVM_arg; |
| 1879 | }); |
| 1880 | } |
| 1881 | |
| 1882 | std::optional<ArrayRef<uint64_t>> |
| 1883 | DIExpression::getSingleLocationExpressionElements() const { |
| 1884 | // Check for `isValid` covered by `isSingleLocationExpression`. |
| 1885 | if (!isSingleLocationExpression()) |
| 1886 | return std::nullopt; |
| 1887 | |
| 1888 | // An empty expression is already non-variadic. |
| 1889 | if (!getNumElements()) |
| 1890 | return ArrayRef<uint64_t>(); |
| 1891 | |
| 1892 | // If Expr does not have a leading DW_OP_LLVM_arg then we don't need to do |
| 1893 | // anything. |
| 1894 | if (getElements()[0] == dwarf::DW_OP_LLVM_arg) |
| 1895 | return getElements().drop_front(N: 2); |
| 1896 | return getElements(); |
| 1897 | } |
| 1898 | |
| 1899 | const DIExpression * |
| 1900 | DIExpression::convertToUndefExpression(const DIExpression *Expr) { |
| 1901 | SmallVector<uint64_t, 3> UndefOps; |
| 1902 | if (auto FragmentInfo = Expr->getFragmentInfo()) { |
| 1903 | UndefOps.append(IL: {dwarf::DW_OP_LLVM_fragment, FragmentInfo->OffsetInBits, |
| 1904 | FragmentInfo->SizeInBits}); |
| 1905 | } |
| 1906 | return DIExpression::get(Context&: Expr->getContext(), Elements: UndefOps); |
| 1907 | } |
| 1908 | |
| 1909 | const DIExpression * |
| 1910 | DIExpression::convertToVariadicExpression(const DIExpression *Expr) { |
| 1911 | if (any_of(Range: Expr->expr_ops(), P: [](auto ExprOp) { |
| 1912 | return ExprOp.getOp() == dwarf::DW_OP_LLVM_arg; |
| 1913 | })) |
| 1914 | return Expr; |
| 1915 | SmallVector<uint64_t> NewOps; |
| 1916 | NewOps.reserve(N: Expr->getNumElements() + 2); |
| 1917 | NewOps.append(IL: {dwarf::DW_OP_LLVM_arg, 0}); |
| 1918 | NewOps.append(in_start: Expr->elements_begin(), in_end: Expr->elements_end()); |
| 1919 | return DIExpression::get(Context&: Expr->getContext(), Elements: NewOps); |
| 1920 | } |
| 1921 | |
| 1922 | std::optional<const DIExpression *> |
| 1923 | DIExpression::convertToNonVariadicExpression(const DIExpression *Expr) { |
| 1924 | if (!Expr) |
| 1925 | return std::nullopt; |
| 1926 | |
| 1927 | if (auto Elts = Expr->getSingleLocationExpressionElements()) |
| 1928 | return DIExpression::get(Context&: Expr->getContext(), Elements: *Elts); |
| 1929 | |
| 1930 | return std::nullopt; |
| 1931 | } |
| 1932 | |
| 1933 | void DIExpression::canonicalizeExpressionOps(SmallVectorImpl<uint64_t> &Ops, |
| 1934 | const DIExpression *Expr, |
| 1935 | bool IsIndirect) { |
| 1936 | // If Expr is not already variadic, insert the implied `DW_OP_LLVM_arg 0` |
| 1937 | // to the existing expression ops. |
| 1938 | if (none_of(Range: Expr->expr_ops(), P: [](auto ExprOp) { |
| 1939 | return ExprOp.getOp() == dwarf::DW_OP_LLVM_arg; |
| 1940 | })) |
| 1941 | Ops.append(IL: {dwarf::DW_OP_LLVM_arg, 0}); |
| 1942 | // If Expr is not indirect, we only need to insert the expression elements and |
| 1943 | // we're done. |
| 1944 | if (!IsIndirect) { |
| 1945 | Ops.append(in_start: Expr->elements_begin(), in_end: Expr->elements_end()); |
| 1946 | return; |
| 1947 | } |
| 1948 | // If Expr is indirect, insert the implied DW_OP_deref at the end of the |
| 1949 | // expression but before DW_OP_{stack_value, LLVM_fragment} if they are |
| 1950 | // present. |
| 1951 | for (auto Op : Expr->expr_ops()) { |
| 1952 | if (Op.getOp() == dwarf::DW_OP_stack_value || |
| 1953 | Op.getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 1954 | Ops.push_back(Elt: dwarf::DW_OP_deref); |
| 1955 | IsIndirect = false; |
| 1956 | } |
| 1957 | Op.appendToVector(V&: Ops); |
| 1958 | } |
| 1959 | if (IsIndirect) |
| 1960 | Ops.push_back(Elt: dwarf::DW_OP_deref); |
| 1961 | } |
| 1962 | |
| 1963 | bool DIExpression::isEqualExpression(const DIExpression *FirstExpr, |
| 1964 | bool FirstIndirect, |
| 1965 | const DIExpression *SecondExpr, |
| 1966 | bool SecondIndirect) { |
| 1967 | SmallVector<uint64_t> FirstOps; |
| 1968 | DIExpression::canonicalizeExpressionOps(Ops&: FirstOps, Expr: FirstExpr, IsIndirect: FirstIndirect); |
| 1969 | SmallVector<uint64_t> SecondOps; |
| 1970 | DIExpression::canonicalizeExpressionOps(Ops&: SecondOps, Expr: SecondExpr, |
| 1971 | IsIndirect: SecondIndirect); |
| 1972 | return FirstOps == SecondOps; |
| 1973 | } |
| 1974 | |
| 1975 | std::optional<DIExpression::FragmentInfo> |
| 1976 | DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) { |
| 1977 | for (auto I = Start; I != End; ++I) |
| 1978 | if (I->getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 1979 | DIExpression::FragmentInfo Info = {I->getArg(I: 1), I->getArg(I: 0)}; |
| 1980 | return Info; |
| 1981 | } |
| 1982 | return std::nullopt; |
| 1983 | } |
| 1984 | |
| 1985 | std::optional<uint64_t> DIExpression::getActiveBits(DIVariable *Var) { |
| 1986 | std::optional<uint64_t> InitialActiveBits = Var->getSizeInBits(); |
| 1987 | std::optional<uint64_t> ActiveBits = InitialActiveBits; |
| 1988 | for (auto Op : expr_ops()) { |
| 1989 | switch (Op.getOp()) { |
| 1990 | default: |
| 1991 | // We assume the worst case for anything we don't currently handle and |
| 1992 | // revert to the initial active bits. |
| 1993 | ActiveBits = InitialActiveBits; |
| 1994 | break; |
| 1995 | case dwarf::DW_OP_LLVM_extract_bits_zext: |
| 1996 | case dwarf::DW_OP_LLVM_extract_bits_sext: { |
| 1997 | // We can't handle an extract whose sign doesn't match that of the |
| 1998 | // variable. |
| 1999 | std::optional<DIBasicType::Signedness> VarSign = Var->getSignedness(); |
| 2000 | bool VarSigned = (VarSign == DIBasicType::Signedness::Signed); |
| 2001 | bool OpSigned = (Op.getOp() == dwarf::DW_OP_LLVM_extract_bits_sext); |
| 2002 | if (!VarSign || VarSigned != OpSigned) { |
| 2003 | ActiveBits = InitialActiveBits; |
| 2004 | break; |
| 2005 | } |
| 2006 | [[fallthrough]]; |
| 2007 | } |
| 2008 | case dwarf::DW_OP_LLVM_fragment: |
| 2009 | // Extract or fragment narrows the active bits |
| 2010 | if (ActiveBits) |
| 2011 | ActiveBits = std::min(a: *ActiveBits, b: Op.getArg(I: 1)); |
| 2012 | else |
| 2013 | ActiveBits = Op.getArg(I: 1); |
| 2014 | break; |
| 2015 | } |
| 2016 | } |
| 2017 | return ActiveBits; |
| 2018 | } |
| 2019 | |
| 2020 | void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops, |
| 2021 | int64_t Offset) { |
| 2022 | if (Offset > 0) { |
| 2023 | Ops.push_back(Elt: dwarf::DW_OP_plus_uconst); |
| 2024 | Ops.push_back(Elt: Offset); |
| 2025 | } else if (Offset < 0) { |
| 2026 | Ops.push_back(Elt: dwarf::DW_OP_constu); |
| 2027 | // Avoid UB when encountering LLONG_MIN, because in 2's complement |
| 2028 | // abs(LLONG_MIN) is LLONG_MAX+1. |
| 2029 | uint64_t AbsMinusOne = -(Offset+1); |
| 2030 | Ops.push_back(Elt: AbsMinusOne + 1); |
| 2031 | Ops.push_back(Elt: dwarf::DW_OP_minus); |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | bool DIExpression::(int64_t &Offset) const { |
| 2036 | auto SingleLocEltsOpt = getSingleLocationExpressionElements(); |
| 2037 | if (!SingleLocEltsOpt) |
| 2038 | return false; |
| 2039 | auto SingleLocElts = *SingleLocEltsOpt; |
| 2040 | |
| 2041 | if (SingleLocElts.size() == 0) { |
| 2042 | Offset = 0; |
| 2043 | return true; |
| 2044 | } |
| 2045 | |
| 2046 | if (SingleLocElts.size() == 2 && |
| 2047 | SingleLocElts[0] == dwarf::DW_OP_plus_uconst) { |
| 2048 | Offset = SingleLocElts[1]; |
| 2049 | return true; |
| 2050 | } |
| 2051 | |
| 2052 | if (SingleLocElts.size() == 3 && SingleLocElts[0] == dwarf::DW_OP_constu) { |
| 2053 | if (SingleLocElts[2] == dwarf::DW_OP_plus) { |
| 2054 | Offset = SingleLocElts[1]; |
| 2055 | return true; |
| 2056 | } |
| 2057 | if (SingleLocElts[2] == dwarf::DW_OP_minus) { |
| 2058 | Offset = -SingleLocElts[1]; |
| 2059 | return true; |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | return false; |
| 2064 | } |
| 2065 | |
| 2066 | bool DIExpression::( |
| 2067 | int64_t &OffsetInBytes, SmallVectorImpl<uint64_t> &RemainingOps) const { |
| 2068 | OffsetInBytes = 0; |
| 2069 | RemainingOps.clear(); |
| 2070 | |
| 2071 | auto SingleLocEltsOpt = getSingleLocationExpressionElements(); |
| 2072 | if (!SingleLocEltsOpt) |
| 2073 | return false; |
| 2074 | |
| 2075 | auto ExprOpEnd = expr_op_iterator(SingleLocEltsOpt->end()); |
| 2076 | auto ExprOpIt = expr_op_iterator(SingleLocEltsOpt->begin()); |
| 2077 | while (ExprOpIt != ExprOpEnd) { |
| 2078 | uint64_t Op = ExprOpIt->getOp(); |
| 2079 | if (Op == dwarf::DW_OP_deref || Op == dwarf::DW_OP_deref_size || |
| 2080 | Op == dwarf::DW_OP_deref_type || Op == dwarf::DW_OP_LLVM_fragment || |
| 2081 | Op == dwarf::DW_OP_LLVM_extract_bits_zext || |
| 2082 | Op == dwarf::DW_OP_LLVM_extract_bits_sext) { |
| 2083 | break; |
| 2084 | } else if (Op == dwarf::DW_OP_plus_uconst) { |
| 2085 | OffsetInBytes += ExprOpIt->getArg(I: 0); |
| 2086 | } else if (Op == dwarf::DW_OP_constu) { |
| 2087 | uint64_t Value = ExprOpIt->getArg(I: 0); |
| 2088 | ++ExprOpIt; |
| 2089 | if (ExprOpIt->getOp() == dwarf::DW_OP_plus) |
| 2090 | OffsetInBytes += Value; |
| 2091 | else if (ExprOpIt->getOp() == dwarf::DW_OP_minus) |
| 2092 | OffsetInBytes -= Value; |
| 2093 | else |
| 2094 | return false; |
| 2095 | } else { |
| 2096 | // Not a const plus/minus operation or deref. |
| 2097 | return false; |
| 2098 | } |
| 2099 | ++ExprOpIt; |
| 2100 | } |
| 2101 | RemainingOps.append(in_start: ExprOpIt.getBase(), in_end: ExprOpEnd.getBase()); |
| 2102 | return true; |
| 2103 | } |
| 2104 | |
| 2105 | bool DIExpression::hasAllLocationOps(unsigned N) const { |
| 2106 | SmallDenseSet<uint64_t, 4> SeenOps; |
| 2107 | for (auto ExprOp : expr_ops()) |
| 2108 | if (ExprOp.getOp() == dwarf::DW_OP_LLVM_arg) |
| 2109 | SeenOps.insert(V: ExprOp.getArg(I: 0)); |
| 2110 | for (uint64_t Idx = 0; Idx < N; ++Idx) |
| 2111 | if (!SeenOps.contains(V: Idx)) |
| 2112 | return false; |
| 2113 | return true; |
| 2114 | } |
| 2115 | |
| 2116 | const DIExpression *DIExpression::(const DIExpression *Expr, |
| 2117 | unsigned &AddrClass) { |
| 2118 | // FIXME: This seems fragile. Nothing that verifies that these elements |
| 2119 | // actually map to ops and not operands. |
| 2120 | auto SingleLocEltsOpt = Expr->getSingleLocationExpressionElements(); |
| 2121 | if (!SingleLocEltsOpt) |
| 2122 | return nullptr; |
| 2123 | auto SingleLocElts = *SingleLocEltsOpt; |
| 2124 | |
| 2125 | const unsigned PatternSize = 4; |
| 2126 | if (SingleLocElts.size() >= PatternSize && |
| 2127 | SingleLocElts[PatternSize - 4] == dwarf::DW_OP_constu && |
| 2128 | SingleLocElts[PatternSize - 2] == dwarf::DW_OP_swap && |
| 2129 | SingleLocElts[PatternSize - 1] == dwarf::DW_OP_xderef) { |
| 2130 | AddrClass = SingleLocElts[PatternSize - 3]; |
| 2131 | |
| 2132 | if (SingleLocElts.size() == PatternSize) |
| 2133 | return nullptr; |
| 2134 | return DIExpression::get( |
| 2135 | Context&: Expr->getContext(), |
| 2136 | Elements: ArrayRef(&*SingleLocElts.begin(), SingleLocElts.size() - PatternSize)); |
| 2137 | } |
| 2138 | return Expr; |
| 2139 | } |
| 2140 | |
| 2141 | DIExpression *DIExpression::prepend(const DIExpression *Expr, uint8_t Flags, |
| 2142 | int64_t Offset) { |
| 2143 | SmallVector<uint64_t, 8> Ops; |
| 2144 | if (Flags & DIExpression::DerefBefore) |
| 2145 | Ops.push_back(Elt: dwarf::DW_OP_deref); |
| 2146 | |
| 2147 | appendOffset(Ops, Offset); |
| 2148 | if (Flags & DIExpression::DerefAfter) |
| 2149 | Ops.push_back(Elt: dwarf::DW_OP_deref); |
| 2150 | |
| 2151 | bool StackValue = Flags & DIExpression::StackValue; |
| 2152 | bool EntryValue = Flags & DIExpression::EntryValue; |
| 2153 | |
| 2154 | return prependOpcodes(Expr, Ops, StackValue, EntryValue); |
| 2155 | } |
| 2156 | |
| 2157 | DIExpression *DIExpression::appendOpsToArg(const DIExpression *Expr, |
| 2158 | ArrayRef<uint64_t> Ops, |
| 2159 | unsigned ArgNo, bool StackValue) { |
| 2160 | assert(Expr && "Can't add ops to this expression" ); |
| 2161 | |
| 2162 | // Handle non-variadic intrinsics by prepending the opcodes. |
| 2163 | if (!any_of(Range: Expr->expr_ops(), |
| 2164 | P: [](auto Op) { return Op.getOp() == dwarf::DW_OP_LLVM_arg; })) { |
| 2165 | assert(ArgNo == 0 && |
| 2166 | "Location Index must be 0 for a non-variadic expression." ); |
| 2167 | SmallVector<uint64_t, 8> NewOps(Ops); |
| 2168 | return DIExpression::prependOpcodes(Expr, Ops&: NewOps, StackValue); |
| 2169 | } |
| 2170 | |
| 2171 | SmallVector<uint64_t, 8> NewOps; |
| 2172 | for (auto Op : Expr->expr_ops()) { |
| 2173 | // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. |
| 2174 | if (StackValue) { |
| 2175 | if (Op.getOp() == dwarf::DW_OP_stack_value) |
| 2176 | StackValue = false; |
| 2177 | else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 2178 | NewOps.push_back(Elt: dwarf::DW_OP_stack_value); |
| 2179 | StackValue = false; |
| 2180 | } |
| 2181 | } |
| 2182 | Op.appendToVector(V&: NewOps); |
| 2183 | if (Op.getOp() == dwarf::DW_OP_LLVM_arg && Op.getArg(I: 0) == ArgNo) |
| 2184 | llvm::append_range(C&: NewOps, R&: Ops); |
| 2185 | } |
| 2186 | if (StackValue) |
| 2187 | NewOps.push_back(Elt: dwarf::DW_OP_stack_value); |
| 2188 | |
| 2189 | return DIExpression::get(Context&: Expr->getContext(), Elements: NewOps); |
| 2190 | } |
| 2191 | |
| 2192 | DIExpression *DIExpression::replaceArg(const DIExpression *Expr, |
| 2193 | uint64_t OldArg, uint64_t NewArg) { |
| 2194 | assert(Expr && "Can't replace args in this expression" ); |
| 2195 | |
| 2196 | SmallVector<uint64_t, 8> NewOps; |
| 2197 | |
| 2198 | for (auto Op : Expr->expr_ops()) { |
| 2199 | if (Op.getOp() != dwarf::DW_OP_LLVM_arg || Op.getArg(I: 0) < OldArg) { |
| 2200 | Op.appendToVector(V&: NewOps); |
| 2201 | continue; |
| 2202 | } |
| 2203 | NewOps.push_back(Elt: dwarf::DW_OP_LLVM_arg); |
| 2204 | uint64_t Arg = Op.getArg(I: 0) == OldArg ? NewArg : Op.getArg(I: 0); |
| 2205 | // OldArg has been deleted from the Op list, so decrement all indices |
| 2206 | // greater than it. |
| 2207 | if (Arg > OldArg) |
| 2208 | --Arg; |
| 2209 | NewOps.push_back(Elt: Arg); |
| 2210 | } |
| 2211 | return DIExpression::get(Context&: Expr->getContext(), Elements: NewOps); |
| 2212 | } |
| 2213 | |
| 2214 | DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, |
| 2215 | SmallVectorImpl<uint64_t> &Ops, |
| 2216 | bool StackValue, bool EntryValue) { |
| 2217 | assert(Expr && "Can't prepend ops to this expression" ); |
| 2218 | |
| 2219 | if (EntryValue) { |
| 2220 | Ops.push_back(Elt: dwarf::DW_OP_LLVM_entry_value); |
| 2221 | // Use a block size of 1 for the target register operand. The |
| 2222 | // DWARF backend currently cannot emit entry values with a block |
| 2223 | // size > 1. |
| 2224 | Ops.push_back(Elt: 1); |
| 2225 | } |
| 2226 | |
| 2227 | // If there are no ops to prepend, do not even add the DW_OP_stack_value. |
| 2228 | if (Ops.empty()) |
| 2229 | StackValue = false; |
| 2230 | for (auto Op : Expr->expr_ops()) { |
| 2231 | // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. |
| 2232 | if (StackValue) { |
| 2233 | if (Op.getOp() == dwarf::DW_OP_stack_value) |
| 2234 | StackValue = false; |
| 2235 | else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 2236 | Ops.push_back(Elt: dwarf::DW_OP_stack_value); |
| 2237 | StackValue = false; |
| 2238 | } |
| 2239 | } |
| 2240 | Op.appendToVector(V&: Ops); |
| 2241 | } |
| 2242 | if (StackValue) |
| 2243 | Ops.push_back(Elt: dwarf::DW_OP_stack_value); |
| 2244 | return DIExpression::get(Context&: Expr->getContext(), Elements: Ops); |
| 2245 | } |
| 2246 | |
| 2247 | DIExpression *DIExpression::append(const DIExpression *Expr, |
| 2248 | ArrayRef<uint64_t> Ops) { |
| 2249 | assert(Expr && !Ops.empty() && "Can't append ops to this expression" ); |
| 2250 | |
| 2251 | // Copy Expr's current op list. |
| 2252 | SmallVector<uint64_t, 16> NewOps; |
| 2253 | for (auto Op : Expr->expr_ops()) { |
| 2254 | // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}. |
| 2255 | if (Op.getOp() == dwarf::DW_OP_stack_value || |
| 2256 | Op.getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 2257 | NewOps.append(in_start: Ops.begin(), in_end: Ops.end()); |
| 2258 | |
| 2259 | // Ensure that the new opcodes are only appended once. |
| 2260 | Ops = {}; |
| 2261 | } |
| 2262 | Op.appendToVector(V&: NewOps); |
| 2263 | } |
| 2264 | NewOps.append(in_start: Ops.begin(), in_end: Ops.end()); |
| 2265 | auto *result = |
| 2266 | DIExpression::get(Context&: Expr->getContext(), Elements: NewOps)->foldConstantMath(); |
| 2267 | assert(result->isValid() && "concatenated expression is not valid" ); |
| 2268 | return result; |
| 2269 | } |
| 2270 | |
| 2271 | DIExpression *DIExpression::appendToStack(const DIExpression *Expr, |
| 2272 | ArrayRef<uint64_t> Ops) { |
| 2273 | assert(Expr && !Ops.empty() && "Can't append ops to this expression" ); |
| 2274 | assert(std::none_of(expr_op_iterator(Ops.begin()), |
| 2275 | expr_op_iterator(Ops.end()), |
| 2276 | [](auto Op) { |
| 2277 | return Op.getOp() == dwarf::DW_OP_stack_value || |
| 2278 | Op.getOp() == dwarf::DW_OP_LLVM_fragment; |
| 2279 | }) && |
| 2280 | "Can't append this op" ); |
| 2281 | |
| 2282 | // Append a DW_OP_deref after Expr's current op list if it's non-empty and |
| 2283 | // has no DW_OP_stack_value. |
| 2284 | // |
| 2285 | // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?. |
| 2286 | std::optional<FragmentInfo> FI = Expr->getFragmentInfo(); |
| 2287 | unsigned DropUntilStackValue = FI ? 3 : 0; |
| 2288 | ArrayRef<uint64_t> ExprOpsBeforeFragment = |
| 2289 | Expr->getElements().drop_back(N: DropUntilStackValue); |
| 2290 | bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) && |
| 2291 | (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value); |
| 2292 | bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty(); |
| 2293 | |
| 2294 | // Append a DW_OP_deref after Expr's current op list if needed, then append |
| 2295 | // the new ops, and finally ensure that a single DW_OP_stack_value is present. |
| 2296 | SmallVector<uint64_t, 16> NewOps; |
| 2297 | if (NeedsDeref) |
| 2298 | NewOps.push_back(Elt: dwarf::DW_OP_deref); |
| 2299 | NewOps.append(in_start: Ops.begin(), in_end: Ops.end()); |
| 2300 | if (NeedsStackValue) |
| 2301 | NewOps.push_back(Elt: dwarf::DW_OP_stack_value); |
| 2302 | return DIExpression::append(Expr, Ops: NewOps); |
| 2303 | } |
| 2304 | |
| 2305 | std::optional<DIExpression *> DIExpression::createFragmentExpression( |
| 2306 | const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) { |
| 2307 | SmallVector<uint64_t, 8> Ops; |
| 2308 | // Track whether it's safe to split the value at the top of the DWARF stack, |
| 2309 | // assuming that it'll be used as an implicit location value. |
| 2310 | bool CanSplitValue = true; |
| 2311 | // Track whether we need to add a fragment expression to the end of Expr. |
| 2312 | bool EmitFragment = true; |
| 2313 | // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment. |
| 2314 | if (Expr) { |
| 2315 | for (auto Op : Expr->expr_ops()) { |
| 2316 | switch (Op.getOp()) { |
| 2317 | default: |
| 2318 | break; |
| 2319 | case dwarf::DW_OP_shr: |
| 2320 | case dwarf::DW_OP_shra: |
| 2321 | case dwarf::DW_OP_shl: |
| 2322 | case dwarf::DW_OP_plus: |
| 2323 | case dwarf::DW_OP_plus_uconst: |
| 2324 | case dwarf::DW_OP_minus: |
| 2325 | // We can't safely split arithmetic or shift operations into multiple |
| 2326 | // fragments because we can't express carry-over between fragments. |
| 2327 | // |
| 2328 | // FIXME: We *could* preserve the lowest fragment of a constant offset |
| 2329 | // operation if the offset fits into SizeInBits. |
| 2330 | CanSplitValue = false; |
| 2331 | break; |
| 2332 | case dwarf::DW_OP_deref: |
| 2333 | case dwarf::DW_OP_deref_size: |
| 2334 | case dwarf::DW_OP_deref_type: |
| 2335 | case dwarf::DW_OP_xderef: |
| 2336 | case dwarf::DW_OP_xderef_size: |
| 2337 | case dwarf::DW_OP_xderef_type: |
| 2338 | // Preceeding arithmetic operations have been applied to compute an |
| 2339 | // address. It's okay to split the value loaded from that address. |
| 2340 | CanSplitValue = true; |
| 2341 | break; |
| 2342 | case dwarf::DW_OP_stack_value: |
| 2343 | // Bail if this expression computes a value that cannot be split. |
| 2344 | if (!CanSplitValue) |
| 2345 | return std::nullopt; |
| 2346 | break; |
| 2347 | case dwarf::DW_OP_LLVM_fragment: { |
| 2348 | // If we've decided we don't need a fragment then give up if we see that |
| 2349 | // there's already a fragment expression. |
| 2350 | // FIXME: We could probably do better here |
| 2351 | if (!EmitFragment) |
| 2352 | return std::nullopt; |
| 2353 | // Make the new offset point into the existing fragment. |
| 2354 | uint64_t FragmentOffsetInBits = Op.getArg(I: 0); |
| 2355 | uint64_t FragmentSizeInBits = Op.getArg(I: 1); |
| 2356 | (void)FragmentSizeInBits; |
| 2357 | assert((OffsetInBits + SizeInBits <= FragmentSizeInBits) && |
| 2358 | "new fragment outside of original fragment" ); |
| 2359 | OffsetInBits += FragmentOffsetInBits; |
| 2360 | continue; |
| 2361 | } |
| 2362 | case dwarf::DW_OP_LLVM_extract_bits_zext: |
| 2363 | case dwarf::DW_OP_LLVM_extract_bits_sext: { |
| 2364 | // If we're extracting bits from inside of the fragment that we're |
| 2365 | // creating then we don't have a fragment after all, and just need to |
| 2366 | // adjust the offset that we're extracting from. |
| 2367 | uint64_t = Op.getArg(I: 0); |
| 2368 | uint64_t = Op.getArg(I: 1); |
| 2369 | if (ExtractOffsetInBits >= OffsetInBits && |
| 2370 | ExtractOffsetInBits + ExtractSizeInBits <= |
| 2371 | OffsetInBits + SizeInBits) { |
| 2372 | Ops.push_back(Elt: Op.getOp()); |
| 2373 | Ops.push_back(Elt: ExtractOffsetInBits - OffsetInBits); |
| 2374 | Ops.push_back(Elt: ExtractSizeInBits); |
| 2375 | EmitFragment = false; |
| 2376 | continue; |
| 2377 | } |
| 2378 | // If the extracted bits aren't fully contained within the fragment then |
| 2379 | // give up. |
| 2380 | // FIXME: We could probably do better here |
| 2381 | return std::nullopt; |
| 2382 | } |
| 2383 | } |
| 2384 | Op.appendToVector(V&: Ops); |
| 2385 | } |
| 2386 | } |
| 2387 | assert((!Expr->isImplicit() || CanSplitValue) && "Expr can't be split" ); |
| 2388 | assert(Expr && "Unknown DIExpression" ); |
| 2389 | if (EmitFragment) { |
| 2390 | Ops.push_back(Elt: dwarf::DW_OP_LLVM_fragment); |
| 2391 | Ops.push_back(Elt: OffsetInBits); |
| 2392 | Ops.push_back(Elt: SizeInBits); |
| 2393 | } |
| 2394 | return DIExpression::get(Context&: Expr->getContext(), Elements: Ops); |
| 2395 | } |
| 2396 | |
| 2397 | /// See declaration for more info. |
| 2398 | bool DIExpression::calculateFragmentIntersect( |
| 2399 | const DataLayout &DL, const Value *SliceStart, uint64_t SliceOffsetInBits, |
| 2400 | uint64_t SliceSizeInBits, const Value *DbgPtr, int64_t DbgPtrOffsetInBits, |
| 2401 | int64_t , DIExpression::FragmentInfo VarFrag, |
| 2402 | std::optional<DIExpression::FragmentInfo> &Result, |
| 2403 | int64_t &OffsetFromLocationInBits) { |
| 2404 | |
| 2405 | if (VarFrag.SizeInBits == 0) |
| 2406 | return false; // Variable size is unknown. |
| 2407 | |
| 2408 | // Difference between mem slice start and the dbg location start. |
| 2409 | // 0 4 8 12 16 ... |
| 2410 | // | | |
| 2411 | // dbg location start |
| 2412 | // | |
| 2413 | // mem slice start |
| 2414 | // Here MemStartRelToDbgStartInBits is 8. Note this can be negative. |
| 2415 | int64_t MemStartRelToDbgStartInBits; |
| 2416 | { |
| 2417 | auto MemOffsetFromDbgInBytes = SliceStart->getPointerOffsetFrom(Other: DbgPtr, DL); |
| 2418 | if (!MemOffsetFromDbgInBytes) |
| 2419 | return false; // Can't calculate difference in addresses. |
| 2420 | // Difference between the pointers. |
| 2421 | MemStartRelToDbgStartInBits = *MemOffsetFromDbgInBytes * 8; |
| 2422 | // Add the difference of the offsets. |
| 2423 | MemStartRelToDbgStartInBits += |
| 2424 | SliceOffsetInBits - (DbgPtrOffsetInBits + DbgExtractOffsetInBits); |
| 2425 | } |
| 2426 | |
| 2427 | // Out-param. Invert offset to get offset from debug location. |
| 2428 | OffsetFromLocationInBits = -MemStartRelToDbgStartInBits; |
| 2429 | |
| 2430 | // Check if the variable fragment sits outside (before) this memory slice. |
| 2431 | int64_t MemEndRelToDbgStart = MemStartRelToDbgStartInBits + SliceSizeInBits; |
| 2432 | if (MemEndRelToDbgStart < 0) { |
| 2433 | Result = {0, 0}; // Out-param. |
| 2434 | return true; |
| 2435 | } |
| 2436 | |
| 2437 | // Work towards creating SliceOfVariable which is the bits of the variable |
| 2438 | // that the memory region covers. |
| 2439 | // 0 4 8 12 16 ... |
| 2440 | // | | |
| 2441 | // dbg location start with VarFrag offset=32 |
| 2442 | // | |
| 2443 | // mem slice start: SliceOfVariable offset=40 |
| 2444 | int64_t MemStartRelToVarInBits = |
| 2445 | MemStartRelToDbgStartInBits + VarFrag.OffsetInBits; |
| 2446 | int64_t MemEndRelToVarInBits = MemStartRelToVarInBits + SliceSizeInBits; |
| 2447 | // If the memory region starts before the debug location the fragment |
| 2448 | // offset would be negative, which we can't encode. Limit those to 0. This |
| 2449 | // is fine because those bits necessarily don't overlap with the existing |
| 2450 | // variable fragment. |
| 2451 | int64_t MemFragStart = std::max<int64_t>(a: 0, b: MemStartRelToVarInBits); |
| 2452 | int64_t MemFragSize = |
| 2453 | std::max<int64_t>(a: 0, b: MemEndRelToVarInBits - MemFragStart); |
| 2454 | DIExpression::FragmentInfo SliceOfVariable(MemFragSize, MemFragStart); |
| 2455 | |
| 2456 | // Intersect the memory region fragment with the variable location fragment. |
| 2457 | DIExpression::FragmentInfo TrimmedSliceOfVariable = |
| 2458 | DIExpression::FragmentInfo::intersect(A: SliceOfVariable, B: VarFrag); |
| 2459 | if (TrimmedSliceOfVariable == VarFrag) |
| 2460 | Result = std::nullopt; // Out-param. |
| 2461 | else |
| 2462 | Result = TrimmedSliceOfVariable; // Out-param. |
| 2463 | return true; |
| 2464 | } |
| 2465 | |
| 2466 | std::pair<DIExpression *, const ConstantInt *> |
| 2467 | DIExpression::constantFold(const ConstantInt *CI) { |
| 2468 | // Copy the APInt so we can modify it. |
| 2469 | APInt NewInt = CI->getValue(); |
| 2470 | SmallVector<uint64_t, 8> Ops; |
| 2471 | |
| 2472 | // Fold operators only at the beginning of the expression. |
| 2473 | bool First = true; |
| 2474 | bool Changed = false; |
| 2475 | for (auto Op : expr_ops()) { |
| 2476 | switch (Op.getOp()) { |
| 2477 | default: |
| 2478 | // We fold only the leading part of the expression; if we get to a part |
| 2479 | // that we're going to copy unchanged, and haven't done any folding, |
| 2480 | // then the entire expression is unchanged and we can return early. |
| 2481 | if (!Changed) |
| 2482 | return {this, CI}; |
| 2483 | First = false; |
| 2484 | break; |
| 2485 | case dwarf::DW_OP_LLVM_convert: |
| 2486 | if (!First) |
| 2487 | break; |
| 2488 | Changed = true; |
| 2489 | if (Op.getArg(I: 1) == dwarf::DW_ATE_signed) |
| 2490 | NewInt = NewInt.sextOrTrunc(width: Op.getArg(I: 0)); |
| 2491 | else { |
| 2492 | assert(Op.getArg(1) == dwarf::DW_ATE_unsigned && "Unexpected operand" ); |
| 2493 | NewInt = NewInt.zextOrTrunc(width: Op.getArg(I: 0)); |
| 2494 | } |
| 2495 | continue; |
| 2496 | } |
| 2497 | Op.appendToVector(V&: Ops); |
| 2498 | } |
| 2499 | if (!Changed) |
| 2500 | return {this, CI}; |
| 2501 | return {DIExpression::get(Context&: getContext(), Elements: Ops), |
| 2502 | ConstantInt::get(Context&: getContext(), V: NewInt)}; |
| 2503 | } |
| 2504 | |
| 2505 | uint64_t DIExpression::getNumLocationOperands() const { |
| 2506 | uint64_t Result = 0; |
| 2507 | for (auto ExprOp : expr_ops()) |
| 2508 | if (ExprOp.getOp() == dwarf::DW_OP_LLVM_arg) |
| 2509 | Result = std::max(a: Result, b: ExprOp.getArg(I: 0) + 1); |
| 2510 | assert(hasAllLocationOps(Result) && |
| 2511 | "Expression is missing one or more location operands." ); |
| 2512 | return Result; |
| 2513 | } |
| 2514 | |
| 2515 | std::optional<DIExpression::SignedOrUnsignedConstant> |
| 2516 | DIExpression::isConstant() const { |
| 2517 | |
| 2518 | // Recognize signed and unsigned constants. |
| 2519 | // An signed constants can be represented as DW_OP_consts C DW_OP_stack_value |
| 2520 | // (DW_OP_LLVM_fragment of Len). |
| 2521 | // An unsigned constant can be represented as |
| 2522 | // DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment of Len). |
| 2523 | |
| 2524 | if ((getNumElements() != 2 && getNumElements() != 3 && |
| 2525 | getNumElements() != 6) || |
| 2526 | (getElement(I: 0) != dwarf::DW_OP_consts && |
| 2527 | getElement(I: 0) != dwarf::DW_OP_constu)) |
| 2528 | return std::nullopt; |
| 2529 | |
| 2530 | if (getNumElements() == 2 && getElement(I: 0) == dwarf::DW_OP_consts) |
| 2531 | return SignedOrUnsignedConstant::SignedConstant; |
| 2532 | |
| 2533 | if ((getNumElements() == 3 && getElement(I: 2) != dwarf::DW_OP_stack_value) || |
| 2534 | (getNumElements() == 6 && (getElement(I: 2) != dwarf::DW_OP_stack_value || |
| 2535 | getElement(I: 3) != dwarf::DW_OP_LLVM_fragment))) |
| 2536 | return std::nullopt; |
| 2537 | return getElement(I: 0) == dwarf::DW_OP_constu |
| 2538 | ? SignedOrUnsignedConstant::UnsignedConstant |
| 2539 | : SignedOrUnsignedConstant::SignedConstant; |
| 2540 | } |
| 2541 | |
| 2542 | DIExpression::ExtOps DIExpression::getExtOps(unsigned FromSize, unsigned ToSize, |
| 2543 | bool Signed) { |
| 2544 | dwarf::TypeKind TK = Signed ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned; |
| 2545 | DIExpression::ExtOps Ops{._M_elems: {dwarf::DW_OP_LLVM_convert, FromSize, TK, |
| 2546 | dwarf::DW_OP_LLVM_convert, ToSize, TK}}; |
| 2547 | return Ops; |
| 2548 | } |
| 2549 | |
| 2550 | DIExpression *DIExpression::appendExt(const DIExpression *Expr, |
| 2551 | unsigned FromSize, unsigned ToSize, |
| 2552 | bool Signed) { |
| 2553 | return appendToStack(Expr, Ops: getExtOps(FromSize, ToSize, Signed)); |
| 2554 | } |
| 2555 | |
| 2556 | DIGlobalVariableExpression * |
| 2557 | DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable, |
| 2558 | Metadata *Expression, StorageType Storage, |
| 2559 | bool ShouldCreate) { |
| 2560 | DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression)); |
| 2561 | Metadata *Ops[] = {Variable, Expression}; |
| 2562 | DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops); |
| 2563 | } |
| 2564 | DIObjCProperty::DIObjCProperty(LLVMContext &C, StorageType Storage, |
| 2565 | unsigned Line, unsigned Attributes, |
| 2566 | ArrayRef<Metadata *> Ops) |
| 2567 | : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property, Ops), |
| 2568 | Line(Line), Attributes(Attributes) {} |
| 2569 | |
| 2570 | DIObjCProperty *DIObjCProperty::getImpl( |
| 2571 | LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, |
| 2572 | MDString *GetterName, MDString *SetterName, unsigned Attributes, |
| 2573 | Metadata *Type, StorageType Storage, bool ShouldCreate) { |
| 2574 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 2575 | assert(isCanonical(GetterName) && "Expected canonical MDString" ); |
| 2576 | assert(isCanonical(SetterName) && "Expected canonical MDString" ); |
| 2577 | DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName, |
| 2578 | SetterName, Attributes, Type)); |
| 2579 | Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; |
| 2580 | DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops); |
| 2581 | } |
| 2582 | |
| 2583 | DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, |
| 2584 | Metadata *Scope, Metadata *Entity, |
| 2585 | Metadata *File, unsigned Line, |
| 2586 | MDString *Name, Metadata *Elements, |
| 2587 | StorageType Storage, |
| 2588 | bool ShouldCreate) { |
| 2589 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 2590 | DEFINE_GETIMPL_LOOKUP(DIImportedEntity, |
| 2591 | (Tag, Scope, Entity, File, Line, Name, Elements)); |
| 2592 | Metadata *Ops[] = {Scope, Entity, Name, File, Elements}; |
| 2593 | DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops); |
| 2594 | } |
| 2595 | |
| 2596 | DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType, unsigned Line, |
| 2597 | MDString *Name, MDString *Value, StorageType Storage, |
| 2598 | bool ShouldCreate) { |
| 2599 | assert(isCanonical(Name) && "Expected canonical MDString" ); |
| 2600 | DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value)); |
| 2601 | Metadata *Ops[] = {Name, Value}; |
| 2602 | DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops); |
| 2603 | } |
| 2604 | |
| 2605 | DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType, |
| 2606 | unsigned Line, Metadata *File, |
| 2607 | Metadata *Elements, StorageType Storage, |
| 2608 | bool ShouldCreate) { |
| 2609 | DEFINE_GETIMPL_LOOKUP(DIMacroFile, (MIType, Line, File, Elements)); |
| 2610 | Metadata *Ops[] = {File, Elements}; |
| 2611 | DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops); |
| 2612 | } |
| 2613 | |
| 2614 | DIArgList *DIArgList::get(LLVMContext &Context, |
| 2615 | ArrayRef<ValueAsMetadata *> Args) { |
| 2616 | auto ExistingIt = Context.pImpl->DIArgLists.find_as(Val: DIArgListKeyInfo(Args)); |
| 2617 | if (ExistingIt != Context.pImpl->DIArgLists.end()) |
| 2618 | return *ExistingIt; |
| 2619 | DIArgList *NewArgList = new DIArgList(Context, Args); |
| 2620 | Context.pImpl->DIArgLists.insert(V: NewArgList); |
| 2621 | return NewArgList; |
| 2622 | } |
| 2623 | |
| 2624 | void DIArgList::handleChangedOperand(void *Ref, Metadata *New) { |
| 2625 | ValueAsMetadata **OldVMPtr = static_cast<ValueAsMetadata **>(Ref); |
| 2626 | assert((!New || isa<ValueAsMetadata>(New)) && |
| 2627 | "DIArgList must be passed a ValueAsMetadata" ); |
| 2628 | untrack(); |
| 2629 | // We need to update the set storage once the Args are updated since they |
| 2630 | // form the key to the DIArgLists store. |
| 2631 | getContext().pImpl->DIArgLists.erase(V: this); |
| 2632 | ValueAsMetadata *NewVM = cast_or_null<ValueAsMetadata>(Val: New); |
| 2633 | for (ValueAsMetadata *&VM : Args) { |
| 2634 | if (&VM == OldVMPtr) { |
| 2635 | if (NewVM) |
| 2636 | VM = NewVM; |
| 2637 | else |
| 2638 | VM = ValueAsMetadata::get(V: PoisonValue::get(T: VM->getValue()->getType())); |
| 2639 | } |
| 2640 | } |
| 2641 | // We've changed the contents of this DIArgList, and the set storage may |
| 2642 | // already contain a DIArgList with our new set of args; if it does, then we |
| 2643 | // must RAUW this with the existing DIArgList, otherwise we simply insert this |
| 2644 | // back into the set storage. |
| 2645 | DIArgList *ExistingArgList = getUniqued(Store&: getContext().pImpl->DIArgLists, Key: this); |
| 2646 | if (ExistingArgList) { |
| 2647 | replaceAllUsesWith(MD: ExistingArgList); |
| 2648 | // Clear this here so we don't try to untrack in the destructor. |
| 2649 | Args.clear(); |
| 2650 | delete this; |
| 2651 | return; |
| 2652 | } |
| 2653 | getContext().pImpl->DIArgLists.insert(V: this); |
| 2654 | track(); |
| 2655 | } |
| 2656 | void DIArgList::track() { |
| 2657 | for (ValueAsMetadata *&VAM : Args) |
| 2658 | if (VAM) |
| 2659 | MetadataTracking::track(Ref: &VAM, MD&: *VAM, Owner&: *this); |
| 2660 | } |
| 2661 | void DIArgList::untrack() { |
| 2662 | for (ValueAsMetadata *&VAM : Args) |
| 2663 | if (VAM) |
| 2664 | MetadataTracking::untrack(Ref: &VAM, MD&: *VAM); |
| 2665 | } |
| 2666 | void DIArgList::dropAllReferences(bool Untrack) { |
| 2667 | if (Untrack) |
| 2668 | untrack(); |
| 2669 | Args.clear(); |
| 2670 | ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false); |
| 2671 | } |
| 2672 | |