| 1 | //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===// |
| 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 helper classes used to build and interpret debug |
| 10 | // information in LLVM IR form. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm-c/DebugInfo.h" |
| 15 | #include "LLVMContextImpl.h" |
| 16 | #include "llvm/ADT/APSInt.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/DenseSet.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/IR/BasicBlock.h" |
| 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DIBuilder.h" |
| 26 | #include "llvm/IR/DebugInfo.h" |
| 27 | #include "llvm/IR/DebugInfoMetadata.h" |
| 28 | #include "llvm/IR/DebugLoc.h" |
| 29 | #include "llvm/IR/DebugProgramInstruction.h" |
| 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/GVMaterializer.h" |
| 32 | #include "llvm/IR/Instruction.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/LLVMContext.h" |
| 35 | #include "llvm/IR/Metadata.h" |
| 36 | #include "llvm/IR/Module.h" |
| 37 | #include "llvm/IR/PassManager.h" |
| 38 | #include "llvm/Support/Casting.h" |
| 39 | #include <algorithm> |
| 40 | #include <cassert> |
| 41 | #include <optional> |
| 42 | #include <utility> |
| 43 | |
| 44 | using namespace llvm; |
| 45 | using namespace llvm::at; |
| 46 | using namespace llvm::dwarf; |
| 47 | |
| 48 | TinyPtrVector<DbgDeclareInst *> llvm::findDbgDeclares(Value *V) { |
| 49 | // This function is hot. Check whether the value has any metadata to avoid a |
| 50 | // DenseMap lookup. This check is a bitfield datamember lookup. |
| 51 | if (!V->isUsedByMetadata()) |
| 52 | return {}; |
| 53 | auto *L = ValueAsMetadata::getIfExists(V); |
| 54 | if (!L) |
| 55 | return {}; |
| 56 | auto *MDV = MetadataAsValue::getIfExists(Context&: V->getContext(), MD: L); |
| 57 | if (!MDV) |
| 58 | return {}; |
| 59 | |
| 60 | TinyPtrVector<DbgDeclareInst *> Declares; |
| 61 | for (User *U : MDV->users()) |
| 62 | if (auto *DDI = dyn_cast<DbgDeclareInst>(Val: U)) |
| 63 | Declares.push_back(NewVal: DDI); |
| 64 | |
| 65 | return Declares; |
| 66 | } |
| 67 | TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclares(Value *V) { |
| 68 | // This function is hot. Check whether the value has any metadata to avoid a |
| 69 | // DenseMap lookup. This check is a bitfield datamember lookup. |
| 70 | if (!V->isUsedByMetadata()) |
| 71 | return {}; |
| 72 | auto *L = ValueAsMetadata::getIfExists(V); |
| 73 | if (!L) |
| 74 | return {}; |
| 75 | |
| 76 | TinyPtrVector<DbgVariableRecord *> Declares; |
| 77 | for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers()) |
| 78 | if (DVR->getType() == DbgVariableRecord::LocationType::Declare) |
| 79 | Declares.push_back(NewVal: DVR); |
| 80 | |
| 81 | return Declares; |
| 82 | } |
| 83 | |
| 84 | TinyPtrVector<DbgVariableRecord *> llvm::findDVRValues(Value *V) { |
| 85 | // This function is hot. Check whether the value has any metadata to avoid a |
| 86 | // DenseMap lookup. This check is a bitfield datamember lookup. |
| 87 | if (!V->isUsedByMetadata()) |
| 88 | return {}; |
| 89 | auto *L = ValueAsMetadata::getIfExists(V); |
| 90 | if (!L) |
| 91 | return {}; |
| 92 | |
| 93 | TinyPtrVector<DbgVariableRecord *> Values; |
| 94 | for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers()) |
| 95 | if (DVR->isValueOfVariable()) |
| 96 | Values.push_back(NewVal: DVR); |
| 97 | |
| 98 | return Values; |
| 99 | } |
| 100 | |
| 101 | template <typename IntrinsicT, bool DbgAssignAndValuesOnly> |
| 102 | static void |
| 103 | findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V, |
| 104 | SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords) { |
| 105 | // This function is hot. Check whether the value has any metadata to avoid a |
| 106 | // DenseMap lookup. |
| 107 | if (!V->isUsedByMetadata()) |
| 108 | return; |
| 109 | |
| 110 | LLVMContext &Ctx = V->getContext(); |
| 111 | // TODO: If this value appears multiple times in a DIArgList, we should still |
| 112 | // only add the owning DbgValueInst once; use this set to track ArgListUsers. |
| 113 | // This behaviour can be removed when we can automatically remove duplicates. |
| 114 | // V will also appear twice in a dbg.assign if its used in the both the value |
| 115 | // and address components. |
| 116 | SmallPtrSet<IntrinsicT *, 4> EncounteredIntrinsics; |
| 117 | SmallPtrSet<DbgVariableRecord *, 4> EncounteredDbgVariableRecords; |
| 118 | |
| 119 | /// Append IntrinsicT users of MetadataAsValue(MD). |
| 120 | auto AppendUsers = [&Ctx, &EncounteredIntrinsics, |
| 121 | &EncounteredDbgVariableRecords, &Result, |
| 122 | DbgVariableRecords](Metadata *MD) { |
| 123 | if (auto *MDV = MetadataAsValue::getIfExists(Context&: Ctx, MD)) { |
| 124 | for (User *U : MDV->users()) |
| 125 | if (IntrinsicT *DVI = dyn_cast<IntrinsicT>(U)) |
| 126 | if (EncounteredIntrinsics.insert(DVI).second) |
| 127 | Result.push_back(DVI); |
| 128 | } |
| 129 | if (!DbgVariableRecords) |
| 130 | return; |
| 131 | // Get DbgVariableRecords that use this as a single value. |
| 132 | if (LocalAsMetadata *L = dyn_cast<LocalAsMetadata>(Val: MD)) { |
| 133 | for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers()) { |
| 134 | if (!DbgAssignAndValuesOnly || DVR->isDbgValue() || DVR->isDbgAssign()) |
| 135 | if (EncounteredDbgVariableRecords.insert(Ptr: DVR).second) |
| 136 | DbgVariableRecords->push_back(Elt: DVR); |
| 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | if (auto *L = LocalAsMetadata::getIfExists(Local: V)) { |
| 142 | AppendUsers(L); |
| 143 | for (Metadata *AL : L->getAllArgListUsers()) { |
| 144 | AppendUsers(AL); |
| 145 | if (!DbgVariableRecords) |
| 146 | continue; |
| 147 | DIArgList *DI = cast<DIArgList>(Val: AL); |
| 148 | for (DbgVariableRecord *DVR : DI->getAllDbgVariableRecordUsers()) |
| 149 | if (!DbgAssignAndValuesOnly || DVR->isDbgValue() || DVR->isDbgAssign()) |
| 150 | if (EncounteredDbgVariableRecords.insert(Ptr: DVR).second) |
| 151 | DbgVariableRecords->push_back(Elt: DVR); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void llvm::findDbgValues( |
| 157 | SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V, |
| 158 | SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords) { |
| 159 | findDbgIntrinsics<DbgValueInst, /*DbgAssignAndValuesOnly=*/true>( |
| 160 | Result&: DbgValues, V, DbgVariableRecords); |
| 161 | } |
| 162 | |
| 163 | void llvm::findDbgUsers( |
| 164 | SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers, Value *V, |
| 165 | SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords) { |
| 166 | findDbgIntrinsics<DbgVariableIntrinsic, /*DbgAssignAndValuesOnly=*/false>( |
| 167 | Result&: DbgUsers, V, DbgVariableRecords); |
| 168 | } |
| 169 | |
| 170 | DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { |
| 171 | if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Val: Scope)) |
| 172 | return LocalScope->getSubprogram(); |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | DebugLoc llvm::getDebugValueLoc(DbgVariableIntrinsic *DII) { |
| 177 | // Original dbg.declare must have a location. |
| 178 | const DebugLoc &DeclareLoc = DII->getDebugLoc(); |
| 179 | MDNode *Scope = DeclareLoc.getScope(); |
| 180 | DILocation *InlinedAt = DeclareLoc.getInlinedAt(); |
| 181 | // Because no machine insts can come from debug intrinsics, only the scope |
| 182 | // and inlinedAt is significant. Zero line numbers are used in case this |
| 183 | // DebugLoc leaks into any adjacent instructions. Produce an unknown location |
| 184 | // with the correct scope / inlinedAt fields. |
| 185 | return DILocation::get(Context&: DII->getContext(), Line: 0, Column: 0, Scope, InlinedAt); |
| 186 | } |
| 187 | |
| 188 | DebugLoc llvm::getDebugValueLoc(DbgVariableRecord *DVR) { |
| 189 | // Original dbg.declare must have a location. |
| 190 | const DebugLoc &DeclareLoc = DVR->getDebugLoc(); |
| 191 | MDNode *Scope = DeclareLoc.getScope(); |
| 192 | DILocation *InlinedAt = DeclareLoc.getInlinedAt(); |
| 193 | // Because no machine insts can come from debug intrinsics, only the scope |
| 194 | // and inlinedAt is significant. Zero line numbers are used in case this |
| 195 | // DebugLoc leaks into any adjacent instructions. Produce an unknown location |
| 196 | // with the correct scope / inlinedAt fields. |
| 197 | return DILocation::get(Context&: DVR->getContext(), Line: 0, Column: 0, Scope, InlinedAt); |
| 198 | } |
| 199 | |
| 200 | //===----------------------------------------------------------------------===// |
| 201 | // DebugInfoFinder implementations. |
| 202 | //===----------------------------------------------------------------------===// |
| 203 | |
| 204 | void DebugInfoFinder::reset() { |
| 205 | CUs.clear(); |
| 206 | SPs.clear(); |
| 207 | GVs.clear(); |
| 208 | TYs.clear(); |
| 209 | Scopes.clear(); |
| 210 | NodesSeen.clear(); |
| 211 | } |
| 212 | |
| 213 | void DebugInfoFinder::processModule(const Module &M) { |
| 214 | for (auto *CU : M.debug_compile_units()) |
| 215 | processCompileUnit(CU); |
| 216 | for (auto &F : M.functions()) { |
| 217 | if (auto *SP = cast_or_null<DISubprogram>(Val: F.getSubprogram())) |
| 218 | processSubprogram(SP); |
| 219 | // There could be subprograms from inlined functions referenced from |
| 220 | // instructions only. Walk the function to find them. |
| 221 | for (const BasicBlock &BB : F) |
| 222 | for (const Instruction &I : BB) |
| 223 | processInstruction(M, I); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) { |
| 228 | if (!addCompileUnit(CU)) |
| 229 | return; |
| 230 | for (auto *DIG : CU->getGlobalVariables()) { |
| 231 | if (!addGlobalVariable(DIG)) |
| 232 | continue; |
| 233 | auto *GV = DIG->getVariable(); |
| 234 | processScope(Scope: GV->getScope()); |
| 235 | processType(DT: GV->getType()); |
| 236 | } |
| 237 | for (auto *ET : CU->getEnumTypes()) |
| 238 | processType(DT: ET); |
| 239 | for (auto *RT : CU->getRetainedTypes()) |
| 240 | if (auto *T = dyn_cast<DIType>(Val: RT)) |
| 241 | processType(DT: T); |
| 242 | else |
| 243 | processSubprogram(SP: cast<DISubprogram>(Val: RT)); |
| 244 | for (auto *Import : CU->getImportedEntities()) |
| 245 | processImportedEntity(Import); |
| 246 | } |
| 247 | |
| 248 | void DebugInfoFinder::processInstruction(const Module &M, |
| 249 | const Instruction &I) { |
| 250 | if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(Val: &I)) |
| 251 | processVariable(DVI: DVI->getVariable()); |
| 252 | |
| 253 | if (auto DbgLoc = I.getDebugLoc()) |
| 254 | processLocation(M, Loc: DbgLoc.get()); |
| 255 | |
| 256 | for (const DbgRecord &DPR : I.getDbgRecordRange()) |
| 257 | processDbgRecord(M, DR: DPR); |
| 258 | } |
| 259 | |
| 260 | void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { |
| 261 | if (!Loc) |
| 262 | return; |
| 263 | processScope(Scope: Loc->getScope()); |
| 264 | processLocation(M, Loc: Loc->getInlinedAt()); |
| 265 | } |
| 266 | |
| 267 | void DebugInfoFinder::processDbgRecord(const Module &M, const DbgRecord &DR) { |
| 268 | if (const DbgVariableRecord *DVR = dyn_cast<const DbgVariableRecord>(Val: &DR)) |
| 269 | processVariable(DVI: DVR->getVariable()); |
| 270 | processLocation(M, Loc: DR.getDebugLoc().get()); |
| 271 | } |
| 272 | |
| 273 | void DebugInfoFinder::processType(DIType *DT) { |
| 274 | if (!addType(DT)) |
| 275 | return; |
| 276 | processScope(Scope: DT->getScope()); |
| 277 | if (auto *ST = dyn_cast<DISubroutineType>(Val: DT)) { |
| 278 | for (DIType *Ref : ST->getTypeArray()) |
| 279 | processType(DT: Ref); |
| 280 | return; |
| 281 | } |
| 282 | if (auto *DCT = dyn_cast<DICompositeType>(Val: DT)) { |
| 283 | processType(DT: DCT->getBaseType()); |
| 284 | for (Metadata *D : DCT->getElements()) { |
| 285 | if (auto *T = dyn_cast<DIType>(Val: D)) |
| 286 | processType(DT: T); |
| 287 | else if (auto *SP = dyn_cast<DISubprogram>(Val: D)) |
| 288 | processSubprogram(SP); |
| 289 | } |
| 290 | return; |
| 291 | } |
| 292 | if (auto *DDT = dyn_cast<DIDerivedType>(Val: DT)) { |
| 293 | processType(DT: DDT->getBaseType()); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void DebugInfoFinder::processImportedEntity(DIImportedEntity *Import) { |
| 298 | auto *Entity = Import->getEntity(); |
| 299 | if (auto *T = dyn_cast<DIType>(Val: Entity)) |
| 300 | processType(DT: T); |
| 301 | else if (auto *SP = dyn_cast<DISubprogram>(Val: Entity)) |
| 302 | processSubprogram(SP); |
| 303 | else if (auto *NS = dyn_cast<DINamespace>(Val: Entity)) |
| 304 | processScope(Scope: NS->getScope()); |
| 305 | else if (auto *M = dyn_cast<DIModule>(Val: Entity)) |
| 306 | processScope(Scope: M->getScope()); |
| 307 | } |
| 308 | |
| 309 | void DebugInfoFinder::processScope(DIScope *Scope) { |
| 310 | if (!Scope) |
| 311 | return; |
| 312 | if (auto *Ty = dyn_cast<DIType>(Val: Scope)) { |
| 313 | processType(DT: Ty); |
| 314 | return; |
| 315 | } |
| 316 | if (auto *CU = dyn_cast<DICompileUnit>(Val: Scope)) { |
| 317 | addCompileUnit(CU); |
| 318 | return; |
| 319 | } |
| 320 | if (auto *SP = dyn_cast<DISubprogram>(Val: Scope)) { |
| 321 | processSubprogram(SP); |
| 322 | return; |
| 323 | } |
| 324 | if (!addScope(Scope)) |
| 325 | return; |
| 326 | if (auto *LB = dyn_cast<DILexicalBlockBase>(Val: Scope)) { |
| 327 | processScope(Scope: LB->getScope()); |
| 328 | } else if (auto *NS = dyn_cast<DINamespace>(Val: Scope)) { |
| 329 | processScope(Scope: NS->getScope()); |
| 330 | } else if (auto *M = dyn_cast<DIModule>(Val: Scope)) { |
| 331 | processScope(Scope: M->getScope()); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void DebugInfoFinder::processSubprogram(DISubprogram *SP) { |
| 336 | if (!addSubprogram(SP)) |
| 337 | return; |
| 338 | processScope(Scope: SP->getScope()); |
| 339 | // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a |
| 340 | // ValueMap containing identity mappings for all of the DICompileUnit's, not |
| 341 | // just DISubprogram's, referenced from anywhere within the Function being |
| 342 | // cloned prior to calling MapMetadata / RemapInstruction to avoid their |
| 343 | // duplication later as DICompileUnit's are also directly referenced by |
| 344 | // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well. |
| 345 | // Also, DICompileUnit's may reference DISubprogram's too and therefore need |
| 346 | // to be at least looked through. |
| 347 | processCompileUnit(CU: SP->getUnit()); |
| 348 | processType(DT: SP->getType()); |
| 349 | for (auto *Element : SP->getTemplateParams()) { |
| 350 | if (auto *TType = dyn_cast<DITemplateTypeParameter>(Val: Element)) { |
| 351 | processType(DT: TType->getType()); |
| 352 | } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Val: Element)) { |
| 353 | processType(DT: TVal->getType()); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | for (auto *N : SP->getRetainedNodes()) { |
| 358 | if (auto *Var = dyn_cast_or_null<DILocalVariable>(Val: N)) |
| 359 | processVariable(DVI: Var); |
| 360 | else if (auto *Import = dyn_cast_or_null<DIImportedEntity>(Val: N)) |
| 361 | processImportedEntity(Import); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void DebugInfoFinder::processVariable(DILocalVariable *DV) { |
| 366 | if (!NodesSeen.insert(Ptr: DV).second) |
| 367 | return; |
| 368 | processScope(Scope: DV->getScope()); |
| 369 | processType(DT: DV->getType()); |
| 370 | } |
| 371 | |
| 372 | bool DebugInfoFinder::addType(DIType *DT) { |
| 373 | if (!DT) |
| 374 | return false; |
| 375 | |
| 376 | if (!NodesSeen.insert(Ptr: DT).second) |
| 377 | return false; |
| 378 | |
| 379 | TYs.push_back(Elt: DT); |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { |
| 384 | if (!CU) |
| 385 | return false; |
| 386 | if (!NodesSeen.insert(Ptr: CU).second) |
| 387 | return false; |
| 388 | |
| 389 | CUs.push_back(Elt: CU); |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { |
| 394 | if (!NodesSeen.insert(Ptr: DIG).second) |
| 395 | return false; |
| 396 | |
| 397 | GVs.push_back(Elt: DIG); |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { |
| 402 | if (!SP) |
| 403 | return false; |
| 404 | |
| 405 | if (!NodesSeen.insert(Ptr: SP).second) |
| 406 | return false; |
| 407 | |
| 408 | SPs.push_back(Elt: SP); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool DebugInfoFinder::addScope(DIScope *Scope) { |
| 413 | if (!Scope) |
| 414 | return false; |
| 415 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 416 | // as null for now. |
| 417 | if (Scope->getNumOperands() == 0) |
| 418 | return false; |
| 419 | if (!NodesSeen.insert(Ptr: Scope).second) |
| 420 | return false; |
| 421 | Scopes.push_back(Elt: Scope); |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | static MDNode *updateLoopMetadataDebugLocationsImpl( |
| 426 | MDNode *OrigLoopID, function_ref<Metadata *(Metadata *)> Updater) { |
| 427 | assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 && |
| 428 | "Loop ID needs at least one operand" ); |
| 429 | assert(OrigLoopID && OrigLoopID->getOperand(0).get() == OrigLoopID && |
| 430 | "Loop ID should refer to itself" ); |
| 431 | |
| 432 | // Save space for the self-referential LoopID. |
| 433 | SmallVector<Metadata *, 4> MDs = {nullptr}; |
| 434 | |
| 435 | for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) { |
| 436 | Metadata *MD = OrigLoopID->getOperand(I: i); |
| 437 | if (!MD) |
| 438 | MDs.push_back(Elt: nullptr); |
| 439 | else if (Metadata *NewMD = Updater(MD)) |
| 440 | MDs.push_back(Elt: NewMD); |
| 441 | } |
| 442 | |
| 443 | MDNode *NewLoopID = MDNode::getDistinct(Context&: OrigLoopID->getContext(), MDs); |
| 444 | // Insert the self-referential LoopID. |
| 445 | NewLoopID->replaceOperandWith(I: 0, New: NewLoopID); |
| 446 | return NewLoopID; |
| 447 | } |
| 448 | |
| 449 | void llvm::updateLoopMetadataDebugLocations( |
| 450 | Instruction &I, function_ref<Metadata *(Metadata *)> Updater) { |
| 451 | MDNode *OrigLoopID = I.getMetadata(KindID: LLVMContext::MD_loop); |
| 452 | if (!OrigLoopID) |
| 453 | return; |
| 454 | MDNode *NewLoopID = updateLoopMetadataDebugLocationsImpl(OrigLoopID, Updater); |
| 455 | I.setMetadata(KindID: LLVMContext::MD_loop, Node: NewLoopID); |
| 456 | } |
| 457 | |
| 458 | /// Return true if a node is a DILocation or if a DILocation is |
| 459 | /// indirectly referenced by one of the node's children. |
| 460 | static bool isDILocationReachable(SmallPtrSetImpl<Metadata *> &Visited, |
| 461 | SmallPtrSetImpl<Metadata *> &Reachable, |
| 462 | Metadata *MD) { |
| 463 | MDNode *N = dyn_cast_or_null<MDNode>(Val: MD); |
| 464 | if (!N) |
| 465 | return false; |
| 466 | if (isa<DILocation>(Val: N) || Reachable.count(Ptr: N)) |
| 467 | return true; |
| 468 | if (!Visited.insert(Ptr: N).second) |
| 469 | return false; |
| 470 | for (auto &OpIt : N->operands()) { |
| 471 | Metadata *Op = OpIt.get(); |
| 472 | if (isDILocationReachable(Visited, Reachable, MD: Op)) { |
| 473 | // Don't return just yet as we want to visit all MD's children to |
| 474 | // initialize DILocationReachable in stripDebugLocFromLoopID |
| 475 | Reachable.insert(Ptr: N); |
| 476 | } |
| 477 | } |
| 478 | return Reachable.count(Ptr: N); |
| 479 | } |
| 480 | |
| 481 | static bool isAllDILocation(SmallPtrSetImpl<Metadata *> &Visited, |
| 482 | SmallPtrSetImpl<Metadata *> &AllDILocation, |
| 483 | const SmallPtrSetImpl<Metadata *> &DIReachable, |
| 484 | Metadata *MD) { |
| 485 | MDNode *N = dyn_cast_or_null<MDNode>(Val: MD); |
| 486 | if (!N) |
| 487 | return false; |
| 488 | if (isa<DILocation>(Val: N) || AllDILocation.count(Ptr: N)) |
| 489 | return true; |
| 490 | if (!DIReachable.count(Ptr: N)) |
| 491 | return false; |
| 492 | if (!Visited.insert(Ptr: N).second) |
| 493 | return false; |
| 494 | for (auto &OpIt : N->operands()) { |
| 495 | Metadata *Op = OpIt.get(); |
| 496 | if (Op == MD) |
| 497 | continue; |
| 498 | if (!isAllDILocation(Visited, AllDILocation, DIReachable, MD: Op)) { |
| 499 | return false; |
| 500 | } |
| 501 | } |
| 502 | AllDILocation.insert(Ptr: N); |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | static Metadata * |
| 507 | stripLoopMDLoc(const SmallPtrSetImpl<Metadata *> &AllDILocation, |
| 508 | const SmallPtrSetImpl<Metadata *> &DIReachable, Metadata *MD) { |
| 509 | if (isa<DILocation>(Val: MD) || AllDILocation.count(Ptr: MD)) |
| 510 | return nullptr; |
| 511 | |
| 512 | if (!DIReachable.count(Ptr: MD)) |
| 513 | return MD; |
| 514 | |
| 515 | MDNode *N = dyn_cast_or_null<MDNode>(Val: MD); |
| 516 | if (!N) |
| 517 | return MD; |
| 518 | |
| 519 | SmallVector<Metadata *, 4> Args; |
| 520 | bool HasSelfRef = false; |
| 521 | for (unsigned i = 0; i < N->getNumOperands(); ++i) { |
| 522 | Metadata *A = N->getOperand(I: i); |
| 523 | if (!A) { |
| 524 | Args.push_back(Elt: nullptr); |
| 525 | } else if (A == MD) { |
| 526 | assert(i == 0 && "expected i==0 for self-reference" ); |
| 527 | HasSelfRef = true; |
| 528 | Args.push_back(Elt: nullptr); |
| 529 | } else if (Metadata *NewArg = |
| 530 | stripLoopMDLoc(AllDILocation, DIReachable, MD: A)) { |
| 531 | Args.push_back(Elt: NewArg); |
| 532 | } |
| 533 | } |
| 534 | if (Args.empty() || (HasSelfRef && Args.size() == 1)) |
| 535 | return nullptr; |
| 536 | |
| 537 | MDNode *NewMD = N->isDistinct() ? MDNode::getDistinct(Context&: N->getContext(), MDs: Args) |
| 538 | : MDNode::get(Context&: N->getContext(), MDs: Args); |
| 539 | if (HasSelfRef) |
| 540 | NewMD->replaceOperandWith(I: 0, New: NewMD); |
| 541 | return NewMD; |
| 542 | } |
| 543 | |
| 544 | static MDNode *stripDebugLocFromLoopID(MDNode *N) { |
| 545 | assert(!N->operands().empty() && "Missing self reference?" ); |
| 546 | SmallPtrSet<Metadata *, 8> Visited, DILocationReachable, AllDILocation; |
| 547 | // If we already visited N, there is nothing to do. |
| 548 | if (!Visited.insert(Ptr: N).second) |
| 549 | return N; |
| 550 | |
| 551 | // If there is no debug location, we do not have to rewrite this |
| 552 | // MDNode. This loop also initializes DILocationReachable, later |
| 553 | // needed by updateLoopMetadataDebugLocationsImpl; the use of |
| 554 | // count_if avoids an early exit. |
| 555 | if (!llvm::count_if(Range: llvm::drop_begin(RangeOrContainer: N->operands()), |
| 556 | P: [&Visited, &DILocationReachable](const MDOperand &Op) { |
| 557 | return isDILocationReachable( |
| 558 | Visited, Reachable&: DILocationReachable, MD: Op.get()); |
| 559 | })) |
| 560 | return N; |
| 561 | |
| 562 | Visited.clear(); |
| 563 | // If there is only the debug location without any actual loop metadata, we |
| 564 | // can remove the metadata. |
| 565 | if (llvm::all_of(Range: llvm::drop_begin(RangeOrContainer: N->operands()), |
| 566 | P: [&Visited, &AllDILocation, |
| 567 | &DILocationReachable](const MDOperand &Op) { |
| 568 | return isAllDILocation(Visited, AllDILocation, |
| 569 | DIReachable: DILocationReachable, MD: Op.get()); |
| 570 | })) |
| 571 | return nullptr; |
| 572 | |
| 573 | return updateLoopMetadataDebugLocationsImpl( |
| 574 | OrigLoopID: N, Updater: [&AllDILocation, &DILocationReachable](Metadata *MD) -> Metadata * { |
| 575 | return stripLoopMDLoc(AllDILocation, DIReachable: DILocationReachable, MD); |
| 576 | }); |
| 577 | } |
| 578 | |
| 579 | bool llvm::stripDebugInfo(Function &F) { |
| 580 | bool Changed = false; |
| 581 | if (F.hasMetadata(KindID: LLVMContext::MD_dbg)) { |
| 582 | Changed = true; |
| 583 | F.setSubprogram(nullptr); |
| 584 | } |
| 585 | |
| 586 | DenseMap<MDNode *, MDNode *> LoopIDsMap; |
| 587 | for (BasicBlock &BB : F) { |
| 588 | for (Instruction &I : llvm::make_early_inc_range(Range&: BB)) { |
| 589 | if (I.getDebugLoc()) { |
| 590 | Changed = true; |
| 591 | I.setDebugLoc(DebugLoc()); |
| 592 | } |
| 593 | if (auto *LoopID = I.getMetadata(KindID: LLVMContext::MD_loop)) { |
| 594 | auto *NewLoopID = LoopIDsMap.lookup(Val: LoopID); |
| 595 | if (!NewLoopID) |
| 596 | NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(N: LoopID); |
| 597 | if (NewLoopID != LoopID) |
| 598 | I.setMetadata(KindID: LLVMContext::MD_loop, Node: NewLoopID); |
| 599 | } |
| 600 | // Strip other attachments that are or use debug info. |
| 601 | if (I.hasMetadataOtherThanDebugLoc()) { |
| 602 | // Heapallocsites point into the DIType system. |
| 603 | I.setMetadata(Kind: "heapallocsite" , Node: nullptr); |
| 604 | // DIAssignID are debug info metadata primitives. |
| 605 | I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: nullptr); |
| 606 | } |
| 607 | I.dropDbgRecords(); |
| 608 | } |
| 609 | } |
| 610 | return Changed; |
| 611 | } |
| 612 | |
| 613 | bool llvm::StripDebugInfo(Module &M) { |
| 614 | bool Changed = false; |
| 615 | |
| 616 | for (NamedMDNode &NMD : llvm::make_early_inc_range(Range: M.named_metadata())) { |
| 617 | // We're stripping debug info, and without them, coverage information |
| 618 | // doesn't quite make sense. |
| 619 | if (NMD.getName().starts_with(Prefix: "llvm.dbg." ) || |
| 620 | NMD.getName() == "llvm.gcov" ) { |
| 621 | NMD.eraseFromParent(); |
| 622 | Changed = true; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | for (Function &F : M) |
| 627 | Changed |= stripDebugInfo(F); |
| 628 | |
| 629 | for (auto &GV : M.globals()) { |
| 630 | Changed |= GV.eraseMetadata(KindID: LLVMContext::MD_dbg); |
| 631 | } |
| 632 | |
| 633 | if (GVMaterializer *Materializer = M.getMaterializer()) |
| 634 | Materializer->setStripDebugInfo(); |
| 635 | |
| 636 | return Changed; |
| 637 | } |
| 638 | |
| 639 | namespace { |
| 640 | |
| 641 | /// Helper class to downgrade -g metadata to -gline-tables-only metadata. |
| 642 | class DebugTypeInfoRemoval { |
| 643 | DenseMap<Metadata *, Metadata *> Replacements; |
| 644 | |
| 645 | public: |
| 646 | /// The (void)() type. |
| 647 | MDNode *EmptySubroutineType; |
| 648 | |
| 649 | private: |
| 650 | /// Remember what linkage name we originally had before stripping. If we end |
| 651 | /// up making two subprograms identical who originally had different linkage |
| 652 | /// names, then we need to make one of them distinct, to avoid them getting |
| 653 | /// uniqued. Maps the new node to the old linkage name. |
| 654 | DenseMap<DISubprogram *, StringRef> NewToLinkageName; |
| 655 | |
| 656 | // TODO: Remember the distinct subprogram we created for a given linkage name, |
| 657 | // so that we can continue to unique whenever possible. Map <newly created |
| 658 | // node, old linkage name> to the first (possibly distinct) mdsubprogram |
| 659 | // created for that combination. This is not strictly needed for correctness, |
| 660 | // but can cut down on the number of MDNodes and let us diff cleanly with the |
| 661 | // output of -gline-tables-only. |
| 662 | |
| 663 | public: |
| 664 | DebugTypeInfoRemoval(LLVMContext &C) |
| 665 | : EmptySubroutineType(DISubroutineType::get(Context&: C, Flags: DINode::FlagZero, CC: 0, |
| 666 | TypeArray: MDNode::get(Context&: C, MDs: {}))) {} |
| 667 | |
| 668 | Metadata *map(Metadata *M) { |
| 669 | if (!M) |
| 670 | return nullptr; |
| 671 | auto Replacement = Replacements.find(Val: M); |
| 672 | if (Replacement != Replacements.end()) |
| 673 | return Replacement->second; |
| 674 | |
| 675 | return M; |
| 676 | } |
| 677 | MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(Val: map(M: N)); } |
| 678 | |
| 679 | /// Recursively remap N and all its referenced children. Does a DF post-order |
| 680 | /// traversal, so as to remap bottoms up. |
| 681 | void traverseAndRemap(MDNode *N) { traverse(N); } |
| 682 | |
| 683 | private: |
| 684 | // Create a new DISubprogram, to replace the one given. |
| 685 | DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { |
| 686 | auto *FileAndScope = cast_or_null<DIFile>(Val: map(M: MDS->getFile())); |
| 687 | StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "" ; |
| 688 | DISubprogram *Declaration = nullptr; |
| 689 | auto *Type = cast_or_null<DISubroutineType>(Val: map(M: MDS->getType())); |
| 690 | DIType *ContainingType = |
| 691 | cast_or_null<DIType>(Val: map(M: MDS->getContainingType())); |
| 692 | auto *Unit = cast_or_null<DICompileUnit>(Val: map(M: MDS->getUnit())); |
| 693 | auto Variables = nullptr; |
| 694 | auto TemplateParams = nullptr; |
| 695 | |
| 696 | // Make a distinct DISubprogram, for situations that warrent it. |
| 697 | auto distinctMDSubprogram = [&]() { |
| 698 | return DISubprogram::getDistinct( |
| 699 | Context&: MDS->getContext(), Scope: FileAndScope, Name: MDS->getName(), LinkageName, |
| 700 | File: FileAndScope, Line: MDS->getLine(), Type, ScopeLine: MDS->getScopeLine(), |
| 701 | ContainingType, VirtualIndex: MDS->getVirtualIndex(), ThisAdjustment: MDS->getThisAdjustment(), |
| 702 | Flags: MDS->getFlags(), SPFlags: MDS->getSPFlags(), Unit, TemplateParams, Declaration, |
| 703 | RetainedNodes: Variables); |
| 704 | }; |
| 705 | |
| 706 | if (MDS->isDistinct()) |
| 707 | return distinctMDSubprogram(); |
| 708 | |
| 709 | auto *NewMDS = DISubprogram::get( |
| 710 | Context&: MDS->getContext(), Scope: FileAndScope, Name: MDS->getName(), LinkageName, |
| 711 | File: FileAndScope, Line: MDS->getLine(), Type, ScopeLine: MDS->getScopeLine(), ContainingType, |
| 712 | VirtualIndex: MDS->getVirtualIndex(), ThisAdjustment: MDS->getThisAdjustment(), Flags: MDS->getFlags(), |
| 713 | SPFlags: MDS->getSPFlags(), Unit, TemplateParams, Declaration, RetainedNodes: Variables); |
| 714 | |
| 715 | StringRef OldLinkageName = MDS->getLinkageName(); |
| 716 | |
| 717 | // See if we need to make a distinct one. |
| 718 | auto OrigLinkage = NewToLinkageName.find(Val: NewMDS); |
| 719 | if (OrigLinkage != NewToLinkageName.end()) { |
| 720 | if (OrigLinkage->second == OldLinkageName) |
| 721 | // We're good. |
| 722 | return NewMDS; |
| 723 | |
| 724 | // Otherwise, need to make a distinct one. |
| 725 | // TODO: Query the map to see if we already have one. |
| 726 | return distinctMDSubprogram(); |
| 727 | } |
| 728 | |
| 729 | NewToLinkageName.insert(KV: {NewMDS, MDS->getLinkageName()}); |
| 730 | return NewMDS; |
| 731 | } |
| 732 | |
| 733 | /// Create a new compile unit, to replace the one given |
| 734 | DICompileUnit *getReplacementCU(DICompileUnit *CU) { |
| 735 | // Drop skeleton CUs. |
| 736 | if (CU->getDWOId()) |
| 737 | return nullptr; |
| 738 | |
| 739 | auto *File = cast_or_null<DIFile>(Val: map(M: CU->getFile())); |
| 740 | MDTuple *EnumTypes = nullptr; |
| 741 | MDTuple *RetainedTypes = nullptr; |
| 742 | MDTuple *GlobalVariables = nullptr; |
| 743 | MDTuple *ImportedEntities = nullptr; |
| 744 | return DICompileUnit::getDistinct( |
| 745 | Context&: CU->getContext(), SourceLanguage: CU->getSourceLanguage(), File, Producer: CU->getProducer(), |
| 746 | IsOptimized: CU->isOptimized(), Flags: CU->getFlags(), RuntimeVersion: CU->getRuntimeVersion(), |
| 747 | SplitDebugFilename: CU->getSplitDebugFilename(), EmissionKind: DICompileUnit::LineTablesOnly, EnumTypes, |
| 748 | RetainedTypes, GlobalVariables, ImportedEntities, Macros: CU->getMacros(), |
| 749 | DWOId: CU->getDWOId(), SplitDebugInlining: CU->getSplitDebugInlining(), |
| 750 | DebugInfoForProfiling: CU->getDebugInfoForProfiling(), NameTableKind: CU->getNameTableKind(), |
| 751 | RangesBaseAddress: CU->getRangesBaseAddress(), SysRoot: CU->getSysRoot(), SDK: CU->getSDK()); |
| 752 | } |
| 753 | |
| 754 | DILocation *getReplacementMDLocation(DILocation *MLD) { |
| 755 | auto *Scope = map(M: MLD->getScope()); |
| 756 | auto *InlinedAt = map(M: MLD->getInlinedAt()); |
| 757 | if (MLD->isDistinct()) |
| 758 | return DILocation::getDistinct(Context&: MLD->getContext(), Line: MLD->getLine(), |
| 759 | Column: MLD->getColumn(), Scope, InlinedAt); |
| 760 | return DILocation::get(Context&: MLD->getContext(), Line: MLD->getLine(), Column: MLD->getColumn(), |
| 761 | Scope, InlinedAt); |
| 762 | } |
| 763 | |
| 764 | /// Create a new generic MDNode, to replace the one given |
| 765 | MDNode *getReplacementMDNode(MDNode *N) { |
| 766 | SmallVector<Metadata *, 8> Ops; |
| 767 | Ops.reserve(N: N->getNumOperands()); |
| 768 | for (auto &I : N->operands()) |
| 769 | if (I) |
| 770 | Ops.push_back(Elt: map(M: I)); |
| 771 | auto *Ret = MDNode::get(Context&: N->getContext(), MDs: Ops); |
| 772 | return Ret; |
| 773 | } |
| 774 | |
| 775 | /// Attempt to re-map N to a newly created node. |
| 776 | void remap(MDNode *N) { |
| 777 | if (Replacements.count(Val: N)) |
| 778 | return; |
| 779 | |
| 780 | auto doRemap = [&](MDNode *N) -> MDNode * { |
| 781 | if (!N) |
| 782 | return nullptr; |
| 783 | if (auto *MDSub = dyn_cast<DISubprogram>(Val: N)) { |
| 784 | remap(N: MDSub->getUnit()); |
| 785 | return getReplacementSubprogram(MDS: MDSub); |
| 786 | } |
| 787 | if (isa<DISubroutineType>(Val: N)) |
| 788 | return EmptySubroutineType; |
| 789 | if (auto *CU = dyn_cast<DICompileUnit>(Val: N)) |
| 790 | return getReplacementCU(CU); |
| 791 | if (isa<DIFile>(Val: N)) |
| 792 | return N; |
| 793 | if (auto *MDLB = dyn_cast<DILexicalBlockBase>(Val: N)) |
| 794 | // Remap to our referenced scope (recursively). |
| 795 | return mapNode(N: MDLB->getScope()); |
| 796 | if (auto *MLD = dyn_cast<DILocation>(Val: N)) |
| 797 | return getReplacementMDLocation(MLD); |
| 798 | |
| 799 | // Otherwise, if we see these, just drop them now. Not strictly necessary, |
| 800 | // but this speeds things up a little. |
| 801 | if (isa<DINode>(Val: N)) |
| 802 | return nullptr; |
| 803 | |
| 804 | return getReplacementMDNode(N); |
| 805 | }; |
| 806 | // Seperate recursive doRemap and operator [] into 2 lines to avoid |
| 807 | // out-of-order evaluations since both of them can access the same memory |
| 808 | // location in map Replacements. |
| 809 | auto Value = doRemap(N); |
| 810 | Replacements[N] = Value; |
| 811 | } |
| 812 | |
| 813 | /// Do the remapping traversal. |
| 814 | void traverse(MDNode *); |
| 815 | }; |
| 816 | |
| 817 | } // end anonymous namespace |
| 818 | |
| 819 | void DebugTypeInfoRemoval::traverse(MDNode *N) { |
| 820 | if (!N || Replacements.count(Val: N)) |
| 821 | return; |
| 822 | |
| 823 | // To avoid cycles, as well as for efficiency sake, we will sometimes prune |
| 824 | // parts of the graph. |
| 825 | auto prune = [](MDNode *Parent, MDNode *Child) { |
| 826 | if (auto *MDS = dyn_cast<DISubprogram>(Val: Parent)) |
| 827 | return Child == MDS->getRetainedNodes().get(); |
| 828 | return false; |
| 829 | }; |
| 830 | |
| 831 | SmallVector<MDNode *, 16> ToVisit; |
| 832 | DenseSet<MDNode *> Opened; |
| 833 | |
| 834 | // Visit each node starting at N in post order, and map them. |
| 835 | ToVisit.push_back(Elt: N); |
| 836 | while (!ToVisit.empty()) { |
| 837 | auto *N = ToVisit.back(); |
| 838 | if (!Opened.insert(V: N).second) { |
| 839 | // Close it. |
| 840 | remap(N); |
| 841 | ToVisit.pop_back(); |
| 842 | continue; |
| 843 | } |
| 844 | for (auto &I : N->operands()) |
| 845 | if (auto *MDN = dyn_cast_or_null<MDNode>(Val: I)) |
| 846 | if (!Opened.count(V: MDN) && !Replacements.count(Val: MDN) && !prune(N, MDN) && |
| 847 | !isa<DICompileUnit>(Val: MDN)) |
| 848 | ToVisit.push_back(Elt: MDN); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | bool llvm::stripNonLineTableDebugInfo(Module &M) { |
| 853 | bool Changed = false; |
| 854 | |
| 855 | // First off, delete the debug intrinsics. |
| 856 | auto RemoveUses = [&](StringRef Name) { |
| 857 | if (auto *DbgVal = M.getFunction(Name)) { |
| 858 | while (!DbgVal->use_empty()) |
| 859 | cast<Instruction>(Val: DbgVal->user_back())->eraseFromParent(); |
| 860 | DbgVal->eraseFromParent(); |
| 861 | Changed = true; |
| 862 | } |
| 863 | }; |
| 864 | RemoveUses("llvm.dbg.declare" ); |
| 865 | RemoveUses("llvm.dbg.label" ); |
| 866 | RemoveUses("llvm.dbg.value" ); |
| 867 | |
| 868 | // Delete non-CU debug info named metadata nodes. |
| 869 | for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); |
| 870 | NMI != NME;) { |
| 871 | NamedMDNode *NMD = &*NMI; |
| 872 | ++NMI; |
| 873 | // Specifically keep dbg.cu around. |
| 874 | if (NMD->getName() == "llvm.dbg.cu" ) |
| 875 | continue; |
| 876 | } |
| 877 | |
| 878 | // Drop all dbg attachments from global variables. |
| 879 | for (auto &GV : M.globals()) |
| 880 | GV.eraseMetadata(KindID: LLVMContext::MD_dbg); |
| 881 | |
| 882 | DebugTypeInfoRemoval Mapper(M.getContext()); |
| 883 | auto remap = [&](MDNode *Node) -> MDNode * { |
| 884 | if (!Node) |
| 885 | return nullptr; |
| 886 | Mapper.traverseAndRemap(N: Node); |
| 887 | auto *NewNode = Mapper.mapNode(N: Node); |
| 888 | Changed |= Node != NewNode; |
| 889 | Node = NewNode; |
| 890 | return NewNode; |
| 891 | }; |
| 892 | |
| 893 | // Rewrite the DebugLocs to be equivalent to what |
| 894 | // -gline-tables-only would have created. |
| 895 | for (auto &F : M) { |
| 896 | if (auto *SP = F.getSubprogram()) { |
| 897 | Mapper.traverseAndRemap(N: SP); |
| 898 | auto *NewSP = cast<DISubprogram>(Val: Mapper.mapNode(N: SP)); |
| 899 | Changed |= SP != NewSP; |
| 900 | F.setSubprogram(NewSP); |
| 901 | } |
| 902 | for (auto &BB : F) { |
| 903 | for (auto &I : BB) { |
| 904 | auto remapDebugLoc = [&](const DebugLoc &DL) -> DebugLoc { |
| 905 | auto *Scope = DL.getScope(); |
| 906 | MDNode *InlinedAt = DL.getInlinedAt(); |
| 907 | Scope = remap(Scope); |
| 908 | InlinedAt = remap(InlinedAt); |
| 909 | return DILocation::get(Context&: M.getContext(), Line: DL.getLine(), Column: DL.getCol(), |
| 910 | Scope, InlinedAt); |
| 911 | }; |
| 912 | |
| 913 | if (I.getDebugLoc() != DebugLoc()) |
| 914 | I.setDebugLoc(remapDebugLoc(I.getDebugLoc())); |
| 915 | |
| 916 | // Remap DILocations in llvm.loop attachments. |
| 917 | updateLoopMetadataDebugLocations(I, Updater: [&](Metadata *MD) -> Metadata * { |
| 918 | if (auto *Loc = dyn_cast_or_null<DILocation>(Val: MD)) |
| 919 | return remapDebugLoc(Loc).get(); |
| 920 | return MD; |
| 921 | }); |
| 922 | |
| 923 | // Strip heapallocsite attachments, they point into the DIType system. |
| 924 | if (I.hasMetadataOtherThanDebugLoc()) |
| 925 | I.setMetadata(Kind: "heapallocsite" , Node: nullptr); |
| 926 | |
| 927 | // Strip any DbgRecords attached. |
| 928 | I.dropDbgRecords(); |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | // Create a new llvm.dbg.cu, which is equivalent to the one |
| 934 | // -gline-tables-only would have created. |
| 935 | for (auto &NMD : M.named_metadata()) { |
| 936 | SmallVector<MDNode *, 8> Ops; |
| 937 | for (MDNode *Op : NMD.operands()) |
| 938 | Ops.push_back(Elt: remap(Op)); |
| 939 | |
| 940 | if (!Changed) |
| 941 | continue; |
| 942 | |
| 943 | NMD.clearOperands(); |
| 944 | for (auto *Op : Ops) |
| 945 | if (Op) |
| 946 | NMD.addOperand(M: Op); |
| 947 | } |
| 948 | return Changed; |
| 949 | } |
| 950 | |
| 951 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
| 952 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
| 953 | MD: M.getModuleFlag(Key: "Debug Info Version" ))) |
| 954 | return Val->getZExtValue(); |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | void Instruction::applyMergedLocation(DebugLoc LocA, DebugLoc LocB) { |
| 959 | setDebugLoc(DebugLoc::getMergedLocation(LocA, LocB)); |
| 960 | } |
| 961 | |
| 962 | void Instruction::mergeDIAssignID( |
| 963 | ArrayRef<const Instruction *> SourceInstructions) { |
| 964 | // Replace all uses (and attachments) of all the DIAssignIDs |
| 965 | // on SourceInstructions with a single merged value. |
| 966 | assert(getFunction() && "Uninserted instruction merged" ); |
| 967 | // Collect up the DIAssignID tags. |
| 968 | SmallVector<DIAssignID *, 4> IDs; |
| 969 | for (const Instruction *I : SourceInstructions) { |
| 970 | if (auto *MD = I->getMetadata(KindID: LLVMContext::MD_DIAssignID)) |
| 971 | IDs.push_back(Elt: cast<DIAssignID>(Val: MD)); |
| 972 | assert(getFunction() == I->getFunction() && |
| 973 | "Merging with instruction from another function not allowed" ); |
| 974 | } |
| 975 | |
| 976 | // Add this instruction's DIAssignID too, if it has one. |
| 977 | if (auto *MD = getMetadata(KindID: LLVMContext::MD_DIAssignID)) |
| 978 | IDs.push_back(Elt: cast<DIAssignID>(Val: MD)); |
| 979 | |
| 980 | if (IDs.empty()) |
| 981 | return; // No DIAssignID tags to process. |
| 982 | |
| 983 | DIAssignID *MergeID = IDs[0]; |
| 984 | for (DIAssignID *AssignID : drop_begin(RangeOrContainer&: IDs)) { |
| 985 | if (AssignID != MergeID) |
| 986 | at::RAUW(Old: AssignID, New: MergeID); |
| 987 | } |
| 988 | setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: MergeID); |
| 989 | } |
| 990 | |
| 991 | void Instruction::updateLocationAfterHoist() { dropLocation(); } |
| 992 | |
| 993 | void Instruction::dropLocation() { |
| 994 | const DebugLoc &DL = getDebugLoc(); |
| 995 | if (!DL) { |
| 996 | setDebugLoc(DebugLoc::getDropped()); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | // If this isn't a call, drop the location to allow a location from a |
| 1001 | // preceding instruction to propagate. |
| 1002 | bool MayLowerToCall = false; |
| 1003 | if (isa<CallBase>(Val: this)) { |
| 1004 | auto *II = dyn_cast<IntrinsicInst>(Val: this); |
| 1005 | MayLowerToCall = |
| 1006 | !II || IntrinsicInst::mayLowerToFunctionCall(IID: II->getIntrinsicID()); |
| 1007 | } |
| 1008 | |
| 1009 | if (!MayLowerToCall) { |
| 1010 | setDebugLoc(DebugLoc::getDropped()); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | // Set a line 0 location for calls to preserve scope information in case |
| 1015 | // inlining occurs. |
| 1016 | DISubprogram *SP = getFunction()->getSubprogram(); |
| 1017 | if (SP) |
| 1018 | // If a function scope is available, set it on the line 0 location. When |
| 1019 | // hoisting a call to a predecessor block, using the function scope avoids |
| 1020 | // making it look like the callee was reached earlier than it should be. |
| 1021 | setDebugLoc(DILocation::get(Context&: getContext(), Line: 0, Column: 0, Scope: SP)); |
| 1022 | else |
| 1023 | // The parent function has no scope. Go ahead and drop the location. If |
| 1024 | // the parent function is inlined, and the callee has a subprogram, the |
| 1025 | // inliner will attach a location to the call. |
| 1026 | // |
| 1027 | // One alternative is to set a line 0 location with the existing scope and |
| 1028 | // inlinedAt info. The location might be sensitive to when inlining occurs. |
| 1029 | setDebugLoc(DebugLoc::getDropped()); |
| 1030 | } |
| 1031 | |
| 1032 | //===----------------------------------------------------------------------===// |
| 1033 | // LLVM C API implementations. |
| 1034 | //===----------------------------------------------------------------------===// |
| 1035 | |
| 1036 | static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) { |
| 1037 | switch (lang) { |
| 1038 | #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \ |
| 1039 | case LLVMDWARFSourceLanguage##NAME: \ |
| 1040 | return ID; |
| 1041 | #include "llvm/BinaryFormat/Dwarf.def" |
| 1042 | #undef HANDLE_DW_LANG |
| 1043 | } |
| 1044 | llvm_unreachable("Unhandled Tag" ); |
| 1045 | } |
| 1046 | |
| 1047 | template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) { |
| 1048 | return (DIT *)(Ref ? unwrap<MDNode>(P: Ref) : nullptr); |
| 1049 | } |
| 1050 | |
| 1051 | static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) { |
| 1052 | return static_cast<DINode::DIFlags>(Flags); |
| 1053 | } |
| 1054 | |
| 1055 | static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) { |
| 1056 | return static_cast<LLVMDIFlags>(Flags); |
| 1057 | } |
| 1058 | |
| 1059 | static DISubprogram::DISPFlags |
| 1060 | pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) { |
| 1061 | return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized); |
| 1062 | } |
| 1063 | |
| 1064 | unsigned LLVMDebugMetadataVersion() { |
| 1065 | return DEBUG_METADATA_VERSION; |
| 1066 | } |
| 1067 | |
| 1068 | LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) { |
| 1069 | return wrap(P: new DIBuilder(*unwrap(P: M), false)); |
| 1070 | } |
| 1071 | |
| 1072 | LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { |
| 1073 | return wrap(P: new DIBuilder(*unwrap(P: M))); |
| 1074 | } |
| 1075 | |
| 1076 | unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) { |
| 1077 | return getDebugMetadataVersionFromModule(M: *unwrap(P: M)); |
| 1078 | } |
| 1079 | |
| 1080 | LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) { |
| 1081 | return StripDebugInfo(M&: *unwrap(P: M)); |
| 1082 | } |
| 1083 | |
| 1084 | void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) { |
| 1085 | delete unwrap(P: Builder); |
| 1086 | } |
| 1087 | |
| 1088 | void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) { |
| 1089 | unwrap(P: Builder)->finalize(); |
| 1090 | } |
| 1091 | |
| 1092 | void LLVMDIBuilderFinalizeSubprogram(LLVMDIBuilderRef Builder, |
| 1093 | LLVMMetadataRef subprogram) { |
| 1094 | unwrap(P: Builder)->finalizeSubprogram(SP: unwrapDI<DISubprogram>(Ref: subprogram)); |
| 1095 | } |
| 1096 | |
| 1097 | LLVMMetadataRef LLVMDIBuilderCreateCompileUnit( |
| 1098 | LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang, |
| 1099 | LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen, |
| 1100 | LLVMBool isOptimized, const char *Flags, size_t FlagsLen, |
| 1101 | unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen, |
| 1102 | LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining, |
| 1103 | LLVMBool DebugInfoForProfiling, const char *SysRoot, size_t SysRootLen, |
| 1104 | const char *SDK, size_t SDKLen) { |
| 1105 | auto File = unwrapDI<DIFile>(Ref: FileRef); |
| 1106 | |
| 1107 | return wrap(P: unwrap(P: Builder)->createCompileUnit( |
| 1108 | Lang: map_from_llvmDWARFsourcelanguage(lang: Lang), File, |
| 1109 | Producer: StringRef(Producer, ProducerLen), isOptimized, Flags: StringRef(Flags, FlagsLen), |
| 1110 | RV: RuntimeVer, SplitName: StringRef(SplitName, SplitNameLen), |
| 1111 | Kind: static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId, |
| 1112 | SplitDebugInlining, DebugInfoForProfiling, |
| 1113 | NameTableKind: DICompileUnit::DebugNameTableKind::Default, RangesBaseAddress: false, |
| 1114 | SysRoot: StringRef(SysRoot, SysRootLen), SDK: StringRef(SDK, SDKLen))); |
| 1115 | } |
| 1116 | |
| 1117 | LLVMMetadataRef |
| 1118 | LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, |
| 1119 | size_t FilenameLen, const char *Directory, |
| 1120 | size_t DirectoryLen) { |
| 1121 | return wrap(P: unwrap(P: Builder)->createFile(Filename: StringRef(Filename, FilenameLen), |
| 1122 | Directory: StringRef(Directory, DirectoryLen))); |
| 1123 | } |
| 1124 | |
| 1125 | LLVMMetadataRef |
| 1126 | LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope, |
| 1127 | const char *Name, size_t NameLen, |
| 1128 | const char *ConfigMacros, size_t ConfigMacrosLen, |
| 1129 | const char *IncludePath, size_t IncludePathLen, |
| 1130 | const char *APINotesFile, size_t APINotesFileLen) { |
| 1131 | return wrap(P: unwrap(P: Builder)->createModule( |
| 1132 | Scope: unwrapDI<DIScope>(Ref: ParentScope), Name: StringRef(Name, NameLen), |
| 1133 | ConfigurationMacros: StringRef(ConfigMacros, ConfigMacrosLen), |
| 1134 | IncludePath: StringRef(IncludePath, IncludePathLen), |
| 1135 | APINotesFile: StringRef(APINotesFile, APINotesFileLen))); |
| 1136 | } |
| 1137 | |
| 1138 | LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder, |
| 1139 | LLVMMetadataRef ParentScope, |
| 1140 | const char *Name, size_t NameLen, |
| 1141 | LLVMBool ExportSymbols) { |
| 1142 | return wrap(P: unwrap(P: Builder)->createNameSpace( |
| 1143 | Scope: unwrapDI<DIScope>(Ref: ParentScope), Name: StringRef(Name, NameLen), ExportSymbols)); |
| 1144 | } |
| 1145 | |
| 1146 | LLVMMetadataRef LLVMDIBuilderCreateFunction( |
| 1147 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1148 | size_t NameLen, const char *LinkageName, size_t LinkageNameLen, |
| 1149 | LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, |
| 1150 | LLVMBool IsLocalToUnit, LLVMBool IsDefinition, |
| 1151 | unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) { |
| 1152 | return wrap(P: unwrap(P: Builder)->createFunction( |
| 1153 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, LinkageName: {LinkageName, LinkageNameLen}, |
| 1154 | File: unwrapDI<DIFile>(Ref: File), LineNo, Ty: unwrapDI<DISubroutineType>(Ref: Ty), ScopeLine, |
| 1155 | Flags: map_from_llvmDIFlags(Flags), |
| 1156 | SPFlags: pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), TParams: nullptr, |
| 1157 | Decl: nullptr, ThrownTypes: nullptr)); |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock( |
| 1162 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, |
| 1163 | LLVMMetadataRef File, unsigned Line, unsigned Col) { |
| 1164 | return wrap(P: unwrap(P: Builder)->createLexicalBlock(Scope: unwrapDI<DIScope>(Ref: Scope), |
| 1165 | File: unwrapDI<DIFile>(Ref: File), |
| 1166 | Line, Col)); |
| 1167 | } |
| 1168 | |
| 1169 | LLVMMetadataRef |
| 1170 | LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder, |
| 1171 | LLVMMetadataRef Scope, |
| 1172 | LLVMMetadataRef File, |
| 1173 | unsigned Discriminator) { |
| 1174 | return wrap(P: unwrap(P: Builder)->createLexicalBlockFile(Scope: unwrapDI<DIScope>(Ref: Scope), |
| 1175 | File: unwrapDI<DIFile>(Ref: File), |
| 1176 | Discriminator)); |
| 1177 | } |
| 1178 | |
| 1179 | LLVMMetadataRef |
| 1180 | LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder, |
| 1181 | LLVMMetadataRef Scope, |
| 1182 | LLVMMetadataRef NS, |
| 1183 | LLVMMetadataRef File, |
| 1184 | unsigned Line) { |
| 1185 | return wrap(P: unwrap(P: Builder)->createImportedModule(Context: unwrapDI<DIScope>(Ref: Scope), |
| 1186 | NS: unwrapDI<DINamespace>(Ref: NS), |
| 1187 | File: unwrapDI<DIFile>(Ref: File), |
| 1188 | Line)); |
| 1189 | } |
| 1190 | |
| 1191 | LLVMMetadataRef LLVMDIBuilderCreateImportedModuleFromAlias( |
| 1192 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, |
| 1193 | LLVMMetadataRef ImportedEntity, LLVMMetadataRef File, unsigned Line, |
| 1194 | LLVMMetadataRef *Elements, unsigned NumElements) { |
| 1195 | auto Elts = |
| 1196 | (NumElements > 0) |
| 1197 | ? unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements}) |
| 1198 | : nullptr; |
| 1199 | return wrap(P: unwrap(P: Builder)->createImportedModule( |
| 1200 | Context: unwrapDI<DIScope>(Ref: Scope), NS: unwrapDI<DIImportedEntity>(Ref: ImportedEntity), |
| 1201 | File: unwrapDI<DIFile>(Ref: File), Line, Elements: Elts)); |
| 1202 | } |
| 1203 | |
| 1204 | LLVMMetadataRef LLVMDIBuilderCreateImportedModuleFromModule( |
| 1205 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, LLVMMetadataRef M, |
| 1206 | LLVMMetadataRef File, unsigned Line, LLVMMetadataRef *Elements, |
| 1207 | unsigned NumElements) { |
| 1208 | auto Elts = |
| 1209 | (NumElements > 0) |
| 1210 | ? unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements}) |
| 1211 | : nullptr; |
| 1212 | return wrap(P: unwrap(P: Builder)->createImportedModule( |
| 1213 | Context: unwrapDI<DIScope>(Ref: Scope), M: unwrapDI<DIModule>(Ref: M), File: unwrapDI<DIFile>(Ref: File), |
| 1214 | Line, Elements: Elts)); |
| 1215 | } |
| 1216 | |
| 1217 | LLVMMetadataRef LLVMDIBuilderCreateImportedDeclaration( |
| 1218 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, LLVMMetadataRef Decl, |
| 1219 | LLVMMetadataRef File, unsigned Line, const char *Name, size_t NameLen, |
| 1220 | LLVMMetadataRef *Elements, unsigned NumElements) { |
| 1221 | auto Elts = |
| 1222 | (NumElements > 0) |
| 1223 | ? unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements}) |
| 1224 | : nullptr; |
| 1225 | return wrap(P: unwrap(P: Builder)->createImportedDeclaration( |
| 1226 | Context: unwrapDI<DIScope>(Ref: Scope), Decl: unwrapDI<DINode>(Ref: Decl), File: unwrapDI<DIFile>(Ref: File), |
| 1227 | Line, Name: {Name, NameLen}, Elements: Elts)); |
| 1228 | } |
| 1229 | |
| 1230 | LLVMMetadataRef |
| 1231 | LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, |
| 1232 | unsigned Column, LLVMMetadataRef Scope, |
| 1233 | LLVMMetadataRef InlinedAt) { |
| 1234 | return wrap(P: DILocation::get(Context&: *unwrap(P: Ctx), Line, Column, Scope: unwrap(P: Scope), |
| 1235 | InlinedAt: unwrap(P: InlinedAt))); |
| 1236 | } |
| 1237 | |
| 1238 | unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) { |
| 1239 | return unwrapDI<DILocation>(Ref: Location)->getLine(); |
| 1240 | } |
| 1241 | |
| 1242 | unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) { |
| 1243 | return unwrapDI<DILocation>(Ref: Location)->getColumn(); |
| 1244 | } |
| 1245 | |
| 1246 | LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) { |
| 1247 | return wrap(P: unwrapDI<DILocation>(Ref: Location)->getScope()); |
| 1248 | } |
| 1249 | |
| 1250 | LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) { |
| 1251 | return wrap(P: unwrapDI<DILocation>(Ref: Location)->getInlinedAt()); |
| 1252 | } |
| 1253 | |
| 1254 | LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) { |
| 1255 | return wrap(P: unwrapDI<DIScope>(Ref: Scope)->getFile()); |
| 1256 | } |
| 1257 | |
| 1258 | const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) { |
| 1259 | auto Dir = unwrapDI<DIFile>(Ref: File)->getDirectory(); |
| 1260 | *Len = Dir.size(); |
| 1261 | return Dir.data(); |
| 1262 | } |
| 1263 | |
| 1264 | const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) { |
| 1265 | auto Name = unwrapDI<DIFile>(Ref: File)->getFilename(); |
| 1266 | *Len = Name.size(); |
| 1267 | return Name.data(); |
| 1268 | } |
| 1269 | |
| 1270 | const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) { |
| 1271 | if (auto Src = unwrapDI<DIFile>(Ref: File)->getSource()) { |
| 1272 | *Len = Src->size(); |
| 1273 | return Src->data(); |
| 1274 | } |
| 1275 | *Len = 0; |
| 1276 | return "" ; |
| 1277 | } |
| 1278 | |
| 1279 | LLVMMetadataRef LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder, |
| 1280 | LLVMMetadataRef ParentMacroFile, |
| 1281 | unsigned Line, |
| 1282 | LLVMDWARFMacinfoRecordType RecordType, |
| 1283 | const char *Name, size_t NameLen, |
| 1284 | const char *Value, size_t ValueLen) { |
| 1285 | return wrap( |
| 1286 | P: unwrap(P: Builder)->createMacro(Parent: unwrapDI<DIMacroFile>(Ref: ParentMacroFile), Line, |
| 1287 | MacroType: static_cast<MacinfoRecordType>(RecordType), |
| 1288 | Name: {Name, NameLen}, Value: {Value, ValueLen})); |
| 1289 | } |
| 1290 | |
| 1291 | LLVMMetadataRef |
| 1292 | LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder, |
| 1293 | LLVMMetadataRef ParentMacroFile, unsigned Line, |
| 1294 | LLVMMetadataRef File) { |
| 1295 | return wrap(P: unwrap(P: Builder)->createTempMacroFile( |
| 1296 | Parent: unwrapDI<DIMacroFile>(Ref: ParentMacroFile), Line, File: unwrapDI<DIFile>(Ref: File))); |
| 1297 | } |
| 1298 | |
| 1299 | LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, |
| 1300 | const char *Name, size_t NameLen, |
| 1301 | int64_t Value, |
| 1302 | LLVMBool IsUnsigned) { |
| 1303 | return wrap(P: unwrap(P: Builder)->createEnumerator(Name: {Name, NameLen}, Val: Value, |
| 1304 | IsUnsigned: IsUnsigned != 0)); |
| 1305 | } |
| 1306 | |
| 1307 | LLVMMetadataRef LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision( |
| 1308 | LLVMDIBuilderRef Builder, const char *Name, size_t NameLen, |
| 1309 | uint64_t SizeInBits, const uint64_t Words[], LLVMBool IsUnsigned) { |
| 1310 | uint64_t NumWords = (SizeInBits + 63) / 64; |
| 1311 | return wrap(P: unwrap(P: Builder)->createEnumerator( |
| 1312 | Name: {Name, NameLen}, |
| 1313 | Value: APSInt(APInt(SizeInBits, ArrayRef(Words, NumWords)), IsUnsigned != 0))); |
| 1314 | } |
| 1315 | |
| 1316 | LLVMMetadataRef LLVMDIBuilderCreateEnumerationType( |
| 1317 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1318 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 1319 | uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements, |
| 1320 | unsigned NumElements, LLVMMetadataRef ClassTy) { |
| 1321 | auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), |
| 1322 | NumElements}); |
| 1323 | return wrap(P: unwrap(P: Builder)->createEnumerationType( |
| 1324 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), |
| 1325 | LineNumber, SizeInBits, AlignInBits, Elements: Elts, UnderlyingType: unwrapDI<DIType>(Ref: ClassTy))); |
| 1326 | } |
| 1327 | |
| 1328 | LLVMMetadataRef LLVMDIBuilderCreateUnionType( |
| 1329 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1330 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 1331 | uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, |
| 1332 | LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang, |
| 1333 | const char *UniqueId, size_t UniqueIdLen) { |
| 1334 | auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), |
| 1335 | NumElements}); |
| 1336 | return wrap(P: unwrap(P: Builder)->createUnionType( |
| 1337 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), |
| 1338 | LineNumber, SizeInBits, AlignInBits, Flags: map_from_llvmDIFlags(Flags), |
| 1339 | Elements: Elts, RunTimeLang, UniqueIdentifier: {UniqueId, UniqueIdLen})); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | LLVMMetadataRef |
| 1344 | LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size, |
| 1345 | uint32_t AlignInBits, LLVMMetadataRef Ty, |
| 1346 | LLVMMetadataRef *Subscripts, |
| 1347 | unsigned NumSubscripts) { |
| 1348 | auto Subs = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Subscripts), |
| 1349 | NumSubscripts}); |
| 1350 | return wrap(P: unwrap(P: Builder)->createArrayType(Size, AlignInBits, |
| 1351 | Ty: unwrapDI<DIType>(Ref: Ty), Subscripts: Subs)); |
| 1352 | } |
| 1353 | |
| 1354 | LLVMMetadataRef |
| 1355 | LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size, |
| 1356 | uint32_t AlignInBits, LLVMMetadataRef Ty, |
| 1357 | LLVMMetadataRef *Subscripts, |
| 1358 | unsigned NumSubscripts) { |
| 1359 | auto Subs = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Subscripts), |
| 1360 | NumSubscripts}); |
| 1361 | return wrap(P: unwrap(P: Builder)->createVectorType(Size, AlignInBits, |
| 1362 | Ty: unwrapDI<DIType>(Ref: Ty), Subscripts: Subs)); |
| 1363 | } |
| 1364 | |
| 1365 | LLVMMetadataRef |
| 1366 | LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name, |
| 1367 | size_t NameLen, uint64_t SizeInBits, |
| 1368 | LLVMDWARFTypeEncoding Encoding, |
| 1369 | LLVMDIFlags Flags) { |
| 1370 | return wrap(P: unwrap(P: Builder)->createBasicType(Name: {Name, NameLen}, |
| 1371 | SizeInBits, Encoding, |
| 1372 | Flags: map_from_llvmDIFlags(Flags))); |
| 1373 | } |
| 1374 | |
| 1375 | LLVMMetadataRef LLVMDIBuilderCreatePointerType( |
| 1376 | LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, |
| 1377 | uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace, |
| 1378 | const char *Name, size_t NameLen) { |
| 1379 | return wrap(P: unwrap(P: Builder)->createPointerType( |
| 1380 | PointeeTy: unwrapDI<DIType>(Ref: PointeeTy), SizeInBits, AlignInBits, DWARFAddressSpace: AddressSpace, |
| 1381 | Name: {Name, NameLen})); |
| 1382 | } |
| 1383 | |
| 1384 | LLVMMetadataRef LLVMDIBuilderCreateStructType( |
| 1385 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1386 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 1387 | uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, |
| 1388 | LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements, |
| 1389 | unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder, |
| 1390 | const char *UniqueId, size_t UniqueIdLen) { |
| 1391 | auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), |
| 1392 | NumElements}); |
| 1393 | return wrap(P: unwrap(P: Builder)->createStructType( |
| 1394 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), |
| 1395 | LineNumber, SizeInBits, AlignInBits, Flags: map_from_llvmDIFlags(Flags), |
| 1396 | DerivedFrom: unwrapDI<DIType>(Ref: DerivedFrom), Elements: Elts, RunTimeLang, |
| 1397 | VTableHolder: unwrapDI<DIType>(Ref: VTableHolder), UniqueIdentifier: {UniqueId, UniqueIdLen})); |
| 1398 | } |
| 1399 | |
| 1400 | LLVMMetadataRef LLVMDIBuilderCreateMemberType( |
| 1401 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1402 | size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits, |
| 1403 | uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, |
| 1404 | LLVMMetadataRef Ty) { |
| 1405 | return wrap(P: unwrap(P: Builder)->createMemberType(Scope: unwrapDI<DIScope>(Ref: Scope), |
| 1406 | Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo, SizeInBits, AlignInBits, |
| 1407 | OffsetInBits, Flags: map_from_llvmDIFlags(Flags), Ty: unwrapDI<DIType>(Ref: Ty))); |
| 1408 | } |
| 1409 | |
| 1410 | LLVMMetadataRef |
| 1411 | LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name, |
| 1412 | size_t NameLen) { |
| 1413 | return wrap(P: unwrap(P: Builder)->createUnspecifiedType(Name: {Name, NameLen})); |
| 1414 | } |
| 1415 | |
| 1416 | LLVMMetadataRef LLVMDIBuilderCreateStaticMemberType( |
| 1417 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1418 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 1419 | LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal, |
| 1420 | uint32_t AlignInBits) { |
| 1421 | return wrap(P: unwrap(P: Builder)->createStaticMemberType( |
| 1422 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), |
| 1423 | LineNo: LineNumber, Ty: unwrapDI<DIType>(Ref: Type), Flags: map_from_llvmDIFlags(Flags), |
| 1424 | Val: unwrap<Constant>(P: ConstantVal), Tag: DW_TAG_member, AlignInBits)); |
| 1425 | } |
| 1426 | |
| 1427 | LLVMMetadataRef |
| 1428 | LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder, |
| 1429 | const char *Name, size_t NameLen, |
| 1430 | LLVMMetadataRef File, unsigned LineNo, |
| 1431 | uint64_t SizeInBits, uint32_t AlignInBits, |
| 1432 | uint64_t OffsetInBits, LLVMDIFlags Flags, |
| 1433 | LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) { |
| 1434 | return wrap(P: unwrap(P: Builder)->createObjCIVar( |
| 1435 | Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo, |
| 1436 | SizeInBits, AlignInBits, OffsetInBits, |
| 1437 | Flags: map_from_llvmDIFlags(Flags), Ty: unwrapDI<DIType>(Ref: Ty), |
| 1438 | PropertyNode: unwrapDI<MDNode>(Ref: PropertyNode))); |
| 1439 | } |
| 1440 | |
| 1441 | LLVMMetadataRef |
| 1442 | LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder, |
| 1443 | const char *Name, size_t NameLen, |
| 1444 | LLVMMetadataRef File, unsigned LineNo, |
| 1445 | const char *GetterName, size_t GetterNameLen, |
| 1446 | const char *SetterName, size_t SetterNameLen, |
| 1447 | unsigned PropertyAttributes, |
| 1448 | LLVMMetadataRef Ty) { |
| 1449 | return wrap(P: unwrap(P: Builder)->createObjCProperty( |
| 1450 | Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNumber: LineNo, |
| 1451 | GetterName: {GetterName, GetterNameLen}, SetterName: {SetterName, SetterNameLen}, |
| 1452 | PropertyAttributes, Ty: unwrapDI<DIType>(Ref: Ty))); |
| 1453 | } |
| 1454 | |
| 1455 | LLVMMetadataRef LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder, |
| 1456 | LLVMMetadataRef Type, |
| 1457 | LLVMBool Implicit) { |
| 1458 | return wrap(P: unwrap(P: Builder)->createObjectPointerType(Ty: unwrapDI<DIType>(Ref: Type), |
| 1459 | Implicit)); |
| 1460 | } |
| 1461 | |
| 1462 | LLVMMetadataRef |
| 1463 | LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type, |
| 1464 | const char *Name, size_t NameLen, |
| 1465 | LLVMMetadataRef File, unsigned LineNo, |
| 1466 | LLVMMetadataRef Scope, uint32_t AlignInBits) { |
| 1467 | return wrap(P: unwrap(P: Builder)->createTypedef( |
| 1468 | Ty: unwrapDI<DIType>(Ref: Type), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo, |
| 1469 | Context: unwrapDI<DIScope>(Ref: Scope), AlignInBits)); |
| 1470 | } |
| 1471 | |
| 1472 | LLVMMetadataRef |
| 1473 | LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder, |
| 1474 | LLVMMetadataRef Ty, LLVMMetadataRef BaseTy, |
| 1475 | uint64_t BaseOffset, uint32_t VBPtrOffset, |
| 1476 | LLVMDIFlags Flags) { |
| 1477 | return wrap(P: unwrap(P: Builder)->createInheritance( |
| 1478 | Ty: unwrapDI<DIType>(Ref: Ty), BaseTy: unwrapDI<DIType>(Ref: BaseTy), |
| 1479 | BaseOffset, VBPtrOffset, Flags: map_from_llvmDIFlags(Flags))); |
| 1480 | } |
| 1481 | |
| 1482 | LLVMMetadataRef |
| 1483 | LLVMDIBuilderCreateForwardDecl( |
| 1484 | LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, |
| 1485 | size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, |
| 1486 | unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, |
| 1487 | const char *UniqueIdentifier, size_t UniqueIdentifierLen) { |
| 1488 | return wrap(P: unwrap(P: Builder)->createForwardDecl( |
| 1489 | Tag, Name: {Name, NameLen}, Scope: unwrapDI<DIScope>(Ref: Scope), |
| 1490 | F: unwrapDI<DIFile>(Ref: File), Line, RuntimeLang, SizeInBits, |
| 1491 | AlignInBits, UniqueIdentifier: {UniqueIdentifier, UniqueIdentifierLen})); |
| 1492 | } |
| 1493 | |
| 1494 | LLVMMetadataRef |
| 1495 | LLVMDIBuilderCreateReplaceableCompositeType( |
| 1496 | LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, |
| 1497 | size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, |
| 1498 | unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, |
| 1499 | LLVMDIFlags Flags, const char *UniqueIdentifier, |
| 1500 | size_t UniqueIdentifierLen) { |
| 1501 | return wrap(P: unwrap(P: Builder)->createReplaceableCompositeType( |
| 1502 | Tag, Name: {Name, NameLen}, Scope: unwrapDI<DIScope>(Ref: Scope), |
| 1503 | F: unwrapDI<DIFile>(Ref: File), Line, RuntimeLang, SizeInBits, |
| 1504 | AlignInBits, Flags: map_from_llvmDIFlags(Flags), |
| 1505 | UniqueIdentifier: {UniqueIdentifier, UniqueIdentifierLen})); |
| 1506 | } |
| 1507 | |
| 1508 | LLVMMetadataRef |
| 1509 | LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag, |
| 1510 | LLVMMetadataRef Type) { |
| 1511 | return wrap(P: unwrap(P: Builder)->createQualifiedType(Tag, |
| 1512 | FromTy: unwrapDI<DIType>(Ref: Type))); |
| 1513 | } |
| 1514 | |
| 1515 | LLVMMetadataRef |
| 1516 | LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag, |
| 1517 | LLVMMetadataRef Type) { |
| 1518 | return wrap(P: unwrap(P: Builder)->createReferenceType(Tag, |
| 1519 | RTy: unwrapDI<DIType>(Ref: Type))); |
| 1520 | } |
| 1521 | |
| 1522 | LLVMMetadataRef |
| 1523 | LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) { |
| 1524 | return wrap(P: unwrap(P: Builder)->createNullPtrType()); |
| 1525 | } |
| 1526 | |
| 1527 | LLVMMetadataRef |
| 1528 | LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder, |
| 1529 | LLVMMetadataRef PointeeType, |
| 1530 | LLVMMetadataRef ClassType, |
| 1531 | uint64_t SizeInBits, |
| 1532 | uint32_t AlignInBits, |
| 1533 | LLVMDIFlags Flags) { |
| 1534 | return wrap(P: unwrap(P: Builder)->createMemberPointerType( |
| 1535 | PointeeTy: unwrapDI<DIType>(Ref: PointeeType), |
| 1536 | Class: unwrapDI<DIType>(Ref: ClassType), SizeInBits: AlignInBits, AlignInBits: SizeInBits, |
| 1537 | Flags: map_from_llvmDIFlags(Flags))); |
| 1538 | } |
| 1539 | |
| 1540 | LLVMMetadataRef |
| 1541 | LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder, |
| 1542 | LLVMMetadataRef Scope, |
| 1543 | const char *Name, size_t NameLen, |
| 1544 | LLVMMetadataRef File, unsigned LineNumber, |
| 1545 | uint64_t SizeInBits, |
| 1546 | uint64_t OffsetInBits, |
| 1547 | uint64_t StorageOffsetInBits, |
| 1548 | LLVMDIFlags Flags, LLVMMetadataRef Type) { |
| 1549 | return wrap(P: unwrap(P: Builder)->createBitFieldMemberType( |
| 1550 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, |
| 1551 | File: unwrapDI<DIFile>(Ref: File), LineNo: LineNumber, |
| 1552 | SizeInBits, OffsetInBits, StorageOffsetInBits, |
| 1553 | Flags: map_from_llvmDIFlags(Flags), Ty: unwrapDI<DIType>(Ref: Type))); |
| 1554 | } |
| 1555 | |
| 1556 | LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder, |
| 1557 | LLVMMetadataRef Scope, const char *Name, size_t NameLen, |
| 1558 | LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits, |
| 1559 | uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, |
| 1560 | LLVMMetadataRef DerivedFrom, |
| 1561 | LLVMMetadataRef *Elements, unsigned NumElements, |
| 1562 | LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode, |
| 1563 | const char *UniqueIdentifier, size_t UniqueIdentifierLen) { |
| 1564 | auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), |
| 1565 | NumElements}); |
| 1566 | return wrap(P: unwrap(P: Builder)->createClassType( |
| 1567 | Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), |
| 1568 | LineNumber, SizeInBits, AlignInBits, OffsetInBits, |
| 1569 | Flags: map_from_llvmDIFlags(Flags), DerivedFrom: unwrapDI<DIType>(Ref: DerivedFrom), Elements: Elts, |
| 1570 | /*RunTimeLang=*/0, VTableHolder: unwrapDI<DIType>(Ref: VTableHolder), |
| 1571 | TemplateParms: unwrapDI<MDNode>(Ref: TemplateParamsNode), |
| 1572 | UniqueIdentifier: {UniqueIdentifier, UniqueIdentifierLen})); |
| 1573 | } |
| 1574 | |
| 1575 | LLVMMetadataRef |
| 1576 | LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder, |
| 1577 | LLVMMetadataRef Type) { |
| 1578 | return wrap(P: unwrap(P: Builder)->createArtificialType(Ty: unwrapDI<DIType>(Ref: Type))); |
| 1579 | } |
| 1580 | |
| 1581 | uint16_t LLVMGetDINodeTag(LLVMMetadataRef MD) { |
| 1582 | return unwrapDI<DINode>(Ref: MD)->getTag(); |
| 1583 | } |
| 1584 | |
| 1585 | const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) { |
| 1586 | StringRef Str = unwrapDI<DIType>(Ref: DType)->getName(); |
| 1587 | *Length = Str.size(); |
| 1588 | return Str.data(); |
| 1589 | } |
| 1590 | |
| 1591 | uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) { |
| 1592 | return unwrapDI<DIType>(Ref: DType)->getSizeInBits(); |
| 1593 | } |
| 1594 | |
| 1595 | uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) { |
| 1596 | return unwrapDI<DIType>(Ref: DType)->getOffsetInBits(); |
| 1597 | } |
| 1598 | |
| 1599 | uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) { |
| 1600 | return unwrapDI<DIType>(Ref: DType)->getAlignInBits(); |
| 1601 | } |
| 1602 | |
| 1603 | unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) { |
| 1604 | return unwrapDI<DIType>(Ref: DType)->getLine(); |
| 1605 | } |
| 1606 | |
| 1607 | LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) { |
| 1608 | return map_to_llvmDIFlags(Flags: unwrapDI<DIType>(Ref: DType)->getFlags()); |
| 1609 | } |
| 1610 | |
| 1611 | LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder, |
| 1612 | LLVMMetadataRef *Types, |
| 1613 | size_t Length) { |
| 1614 | return wrap( |
| 1615 | P: unwrap(P: Builder)->getOrCreateTypeArray(Elements: {unwrap(MDs: Types), Length}).get()); |
| 1616 | } |
| 1617 | |
| 1618 | LLVMMetadataRef |
| 1619 | LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder, |
| 1620 | LLVMMetadataRef File, |
| 1621 | LLVMMetadataRef *ParameterTypes, |
| 1622 | unsigned NumParameterTypes, |
| 1623 | LLVMDIFlags Flags) { |
| 1624 | auto Elts = unwrap(P: Builder)->getOrCreateTypeArray(Elements: {unwrap(MDs: ParameterTypes), |
| 1625 | NumParameterTypes}); |
| 1626 | return wrap(P: unwrap(P: Builder)->createSubroutineType( |
| 1627 | ParameterTypes: Elts, Flags: map_from_llvmDIFlags(Flags))); |
| 1628 | } |
| 1629 | |
| 1630 | LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder, |
| 1631 | uint64_t *Addr, size_t Length) { |
| 1632 | return wrap( |
| 1633 | P: unwrap(P: Builder)->createExpression(Addr: ArrayRef<uint64_t>(Addr, Length))); |
| 1634 | } |
| 1635 | |
| 1636 | LLVMMetadataRef |
| 1637 | LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder, |
| 1638 | uint64_t Value) { |
| 1639 | return wrap(P: unwrap(P: Builder)->createConstantValueExpression(Val: Value)); |
| 1640 | } |
| 1641 | |
| 1642 | LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression( |
| 1643 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1644 | size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File, |
| 1645 | unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, |
| 1646 | LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) { |
| 1647 | return wrap(P: unwrap(P: Builder)->createGlobalVariableExpression( |
| 1648 | Context: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, LinkageName: {Linkage, LinkLen}, |
| 1649 | File: unwrapDI<DIFile>(Ref: File), LineNo, Ty: unwrapDI<DIType>(Ref: Ty), IsLocalToUnit: LocalToUnit, |
| 1650 | isDefined: true, Expr: unwrap<DIExpression>(P: Expr), Decl: unwrapDI<MDNode>(Ref: Decl), |
| 1651 | TemplateParams: nullptr, AlignInBits)); |
| 1652 | } |
| 1653 | |
| 1654 | LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) { |
| 1655 | return wrap(P: unwrapDI<DIGlobalVariableExpression>(Ref: GVE)->getVariable()); |
| 1656 | } |
| 1657 | |
| 1658 | LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression( |
| 1659 | LLVMMetadataRef GVE) { |
| 1660 | return wrap(P: unwrapDI<DIGlobalVariableExpression>(Ref: GVE)->getExpression()); |
| 1661 | } |
| 1662 | |
| 1663 | LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) { |
| 1664 | return wrap(P: unwrapDI<DIVariable>(Ref: Var)->getFile()); |
| 1665 | } |
| 1666 | |
| 1667 | LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) { |
| 1668 | return wrap(P: unwrapDI<DIVariable>(Ref: Var)->getScope()); |
| 1669 | } |
| 1670 | |
| 1671 | unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) { |
| 1672 | return unwrapDI<DIVariable>(Ref: Var)->getLine(); |
| 1673 | } |
| 1674 | |
| 1675 | LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, |
| 1676 | size_t Count) { |
| 1677 | return wrap( |
| 1678 | P: MDTuple::getTemporary(Context&: *unwrap(P: Ctx), MDs: {unwrap(MDs: Data), Count}).release()); |
| 1679 | } |
| 1680 | |
| 1681 | void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) { |
| 1682 | MDNode::deleteTemporary(N: unwrapDI<MDNode>(Ref: TempNode)); |
| 1683 | } |
| 1684 | |
| 1685 | void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata, |
| 1686 | LLVMMetadataRef Replacement) { |
| 1687 | auto *Node = unwrapDI<MDNode>(Ref: TargetMetadata); |
| 1688 | Node->replaceAllUsesWith(MD: unwrap(P: Replacement)); |
| 1689 | MDNode::deleteTemporary(N: Node); |
| 1690 | } |
| 1691 | |
| 1692 | LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl( |
| 1693 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1694 | size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File, |
| 1695 | unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, |
| 1696 | LLVMMetadataRef Decl, uint32_t AlignInBits) { |
| 1697 | return wrap(P: unwrap(P: Builder)->createTempGlobalVariableFwdDecl( |
| 1698 | Context: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, LinkageName: {Linkage, LnkLen}, |
| 1699 | File: unwrapDI<DIFile>(Ref: File), LineNo, Ty: unwrapDI<DIType>(Ref: Ty), IsLocalToUnit: LocalToUnit, |
| 1700 | Decl: unwrapDI<MDNode>(Ref: Decl), TemplateParams: nullptr, AlignInBits)); |
| 1701 | } |
| 1702 | |
| 1703 | LLVMDbgRecordRef LLVMDIBuilderInsertDeclareRecordBefore( |
| 1704 | LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, |
| 1705 | LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) { |
| 1706 | DbgInstPtr DbgInst = unwrap(P: Builder)->insertDeclare( |
| 1707 | Storage: unwrap(P: Storage), VarInfo: unwrap<DILocalVariable>(P: VarInfo), |
| 1708 | Expr: unwrap<DIExpression>(P: Expr), DL: unwrap<DILocation>(P: DL), |
| 1709 | InsertPt: Instr ? InsertPosition(unwrap<Instruction>(P: Instr)->getIterator()) |
| 1710 | : nullptr); |
| 1711 | // This assert will fail if the module is in the old debug info format. |
| 1712 | // This function should only be called if the module is in the new |
| 1713 | // debug info format. |
| 1714 | // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes, |
| 1715 | // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info. |
| 1716 | assert(isa<DbgRecord *>(DbgInst) && |
| 1717 | "Function unexpectedly in old debug info format" ); |
| 1718 | return wrap(P: cast<DbgRecord *>(Val&: DbgInst)); |
| 1719 | } |
| 1720 | |
| 1721 | LLVMDbgRecordRef LLVMDIBuilderInsertDeclareRecordAtEnd( |
| 1722 | LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, |
| 1723 | LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) { |
| 1724 | DbgInstPtr DbgInst = unwrap(P: Builder)->insertDeclare( |
| 1725 | Storage: unwrap(P: Storage), VarInfo: unwrap<DILocalVariable>(P: VarInfo), |
| 1726 | Expr: unwrap<DIExpression>(P: Expr), DL: unwrap<DILocation>(P: DL), InsertAtEnd: unwrap(P: Block)); |
| 1727 | // This assert will fail if the module is in the old debug info format. |
| 1728 | // This function should only be called if the module is in the new |
| 1729 | // debug info format. |
| 1730 | // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes, |
| 1731 | // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info. |
| 1732 | assert(isa<DbgRecord *>(DbgInst) && |
| 1733 | "Function unexpectedly in old debug info format" ); |
| 1734 | return wrap(P: cast<DbgRecord *>(Val&: DbgInst)); |
| 1735 | } |
| 1736 | |
| 1737 | LLVMDbgRecordRef LLVMDIBuilderInsertDbgValueRecordBefore( |
| 1738 | LLVMDIBuilderRef Builder, LLVMValueRef Val, LLVMMetadataRef VarInfo, |
| 1739 | LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMValueRef Instr) { |
| 1740 | DbgInstPtr DbgInst = unwrap(P: Builder)->insertDbgValueIntrinsic( |
| 1741 | Val: unwrap(P: Val), VarInfo: unwrap<DILocalVariable>(P: VarInfo), Expr: unwrap<DIExpression>(P: Expr), |
| 1742 | DL: unwrap<DILocation>(P: DebugLoc), |
| 1743 | InsertPt: Instr ? InsertPosition(unwrap<Instruction>(P: Instr)->getIterator()) |
| 1744 | : nullptr); |
| 1745 | // This assert will fail if the module is in the old debug info format. |
| 1746 | // This function should only be called if the module is in the new |
| 1747 | // debug info format. |
| 1748 | // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes, |
| 1749 | // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info. |
| 1750 | assert(isa<DbgRecord *>(DbgInst) && |
| 1751 | "Function unexpectedly in old debug info format" ); |
| 1752 | return wrap(P: cast<DbgRecord *>(Val&: DbgInst)); |
| 1753 | } |
| 1754 | |
| 1755 | LLVMDbgRecordRef LLVMDIBuilderInsertDbgValueRecordAtEnd( |
| 1756 | LLVMDIBuilderRef Builder, LLVMValueRef Val, LLVMMetadataRef VarInfo, |
| 1757 | LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMBasicBlockRef Block) { |
| 1758 | DbgInstPtr DbgInst = unwrap(P: Builder)->insertDbgValueIntrinsic( |
| 1759 | Val: unwrap(P: Val), VarInfo: unwrap<DILocalVariable>(P: VarInfo), Expr: unwrap<DIExpression>(P: Expr), |
| 1760 | DL: unwrap<DILocation>(P: DebugLoc), |
| 1761 | InsertPt: Block ? InsertPosition(unwrap(P: Block)->end()) : nullptr); |
| 1762 | // This assert will fail if the module is in the old debug info format. |
| 1763 | // This function should only be called if the module is in the new |
| 1764 | // debug info format. |
| 1765 | // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes, |
| 1766 | // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info. |
| 1767 | assert(isa<DbgRecord *>(DbgInst) && |
| 1768 | "Function unexpectedly in old debug info format" ); |
| 1769 | return wrap(P: cast<DbgRecord *>(Val&: DbgInst)); |
| 1770 | } |
| 1771 | |
| 1772 | LLVMMetadataRef LLVMDIBuilderCreateAutoVariable( |
| 1773 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1774 | size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, |
| 1775 | LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) { |
| 1776 | return wrap(P: unwrap(P: Builder)->createAutoVariable( |
| 1777 | Scope: unwrap<DIScope>(P: Scope), Name: {Name, NameLen}, File: unwrap<DIFile>(P: File), |
| 1778 | LineNo, Ty: unwrap<DIType>(P: Ty), AlwaysPreserve, |
| 1779 | Flags: map_from_llvmDIFlags(Flags), AlignInBits)); |
| 1780 | } |
| 1781 | |
| 1782 | LLVMMetadataRef LLVMDIBuilderCreateParameterVariable( |
| 1783 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 1784 | size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo, |
| 1785 | LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) { |
| 1786 | return wrap(P: unwrap(P: Builder)->createParameterVariable( |
| 1787 | Scope: unwrap<DIScope>(P: Scope), Name: {Name, NameLen}, ArgNo, File: unwrap<DIFile>(P: File), |
| 1788 | LineNo, Ty: unwrap<DIType>(P: Ty), AlwaysPreserve, |
| 1789 | Flags: map_from_llvmDIFlags(Flags))); |
| 1790 | } |
| 1791 | |
| 1792 | LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, |
| 1793 | int64_t Lo, int64_t Count) { |
| 1794 | return wrap(P: unwrap(P: Builder)->getOrCreateSubrange(Lo, Count)); |
| 1795 | } |
| 1796 | |
| 1797 | LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder, |
| 1798 | LLVMMetadataRef *Data, |
| 1799 | size_t Length) { |
| 1800 | Metadata **DataValue = unwrap(MDs: Data); |
| 1801 | return wrap(P: unwrap(P: Builder)->getOrCreateArray(Elements: {DataValue, Length}).get()); |
| 1802 | } |
| 1803 | |
| 1804 | LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) { |
| 1805 | return wrap(P: unwrap<Function>(P: Func)->getSubprogram()); |
| 1806 | } |
| 1807 | |
| 1808 | void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) { |
| 1809 | unwrap<Function>(P: Func)->setSubprogram(unwrap<DISubprogram>(P: SP)); |
| 1810 | } |
| 1811 | |
| 1812 | unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) { |
| 1813 | return unwrapDI<DISubprogram>(Ref: Subprogram)->getLine(); |
| 1814 | } |
| 1815 | |
| 1816 | void LLVMDISubprogramReplaceType(LLVMMetadataRef Subprogram, |
| 1817 | LLVMMetadataRef SubroutineType) { |
| 1818 | unwrapDI<DISubprogram>(Ref: Subprogram) |
| 1819 | ->replaceType(Ty: unwrapDI<DISubroutineType>(Ref: SubroutineType)); |
| 1820 | } |
| 1821 | |
| 1822 | LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) { |
| 1823 | return wrap(P: unwrap<Instruction>(P: Inst)->getDebugLoc().getAsMDNode()); |
| 1824 | } |
| 1825 | |
| 1826 | void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) { |
| 1827 | if (Loc) |
| 1828 | unwrap<Instruction>(P: Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(P: Loc))); |
| 1829 | else |
| 1830 | unwrap<Instruction>(P: Inst)->setDebugLoc(DebugLoc()); |
| 1831 | } |
| 1832 | |
| 1833 | LLVMMetadataRef LLVMDIBuilderCreateLabel(LLVMDIBuilderRef Builder, |
| 1834 | LLVMMetadataRef Context, |
| 1835 | const char *Name, size_t NameLen, |
| 1836 | LLVMMetadataRef File, unsigned LineNo, |
| 1837 | LLVMBool AlwaysPreserve) { |
| 1838 | return wrap(P: unwrap(P: Builder)->createLabel( |
| 1839 | Scope: unwrapDI<DIScope>(Ref: Context), Name: StringRef(Name, NameLen), |
| 1840 | File: unwrapDI<DIFile>(Ref: File), LineNo, /*Column*/ 0, /*IsArtificial*/ false, |
| 1841 | /*CoroSuspendIdx*/ std::nullopt, AlwaysPreserve)); |
| 1842 | } |
| 1843 | |
| 1844 | LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore(LLVMDIBuilderRef Builder, |
| 1845 | LLVMMetadataRef LabelInfo, |
| 1846 | LLVMMetadataRef Location, |
| 1847 | LLVMValueRef InsertBefore) { |
| 1848 | DbgInstPtr DbgInst = unwrap(P: Builder)->insertLabel( |
| 1849 | LabelInfo: unwrapDI<DILabel>(Ref: LabelInfo), DL: unwrapDI<DILocation>(Ref: Location), |
| 1850 | InsertPt: InsertBefore |
| 1851 | ? InsertPosition(unwrap<Instruction>(P: InsertBefore)->getIterator()) |
| 1852 | : nullptr); |
| 1853 | // This assert will fail if the module is in the old debug info format. |
| 1854 | // This function should only be called if the module is in the new |
| 1855 | // debug info format. |
| 1856 | // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes, |
| 1857 | // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info. |
| 1858 | assert(isa<DbgRecord *>(DbgInst) && |
| 1859 | "Function unexpectedly in old debug info format" ); |
| 1860 | return wrap(P: cast<DbgRecord *>(Val&: DbgInst)); |
| 1861 | } |
| 1862 | |
| 1863 | LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEnd(LLVMDIBuilderRef Builder, |
| 1864 | LLVMMetadataRef LabelInfo, |
| 1865 | LLVMMetadataRef Location, |
| 1866 | LLVMBasicBlockRef InsertAtEnd) { |
| 1867 | DbgInstPtr DbgInst = unwrap(P: Builder)->insertLabel( |
| 1868 | LabelInfo: unwrapDI<DILabel>(Ref: LabelInfo), DL: unwrapDI<DILocation>(Ref: Location), |
| 1869 | InsertPt: InsertAtEnd ? InsertPosition(unwrap(P: InsertAtEnd)->end()) : nullptr); |
| 1870 | // This assert will fail if the module is in the old debug info format. |
| 1871 | // This function should only be called if the module is in the new |
| 1872 | // debug info format. |
| 1873 | // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes, |
| 1874 | // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info. |
| 1875 | assert(isa<DbgRecord *>(DbgInst) && |
| 1876 | "Function unexpectedly in old debug info format" ); |
| 1877 | return wrap(P: cast<DbgRecord *>(Val&: DbgInst)); |
| 1878 | } |
| 1879 | |
| 1880 | LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) { |
| 1881 | switch(unwrap(P: Metadata)->getMetadataID()) { |
| 1882 | #define HANDLE_METADATA_LEAF(CLASS) \ |
| 1883 | case Metadata::CLASS##Kind: \ |
| 1884 | return (LLVMMetadataKind)LLVM##CLASS##MetadataKind; |
| 1885 | #include "llvm/IR/Metadata.def" |
| 1886 | default: |
| 1887 | return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | AssignmentInstRange at::getAssignmentInsts(DIAssignID *ID) { |
| 1892 | assert(ID && "Expected non-null ID" ); |
| 1893 | LLVMContext &Ctx = ID->getContext(); |
| 1894 | auto &Map = Ctx.pImpl->AssignmentIDToInstrs; |
| 1895 | |
| 1896 | auto MapIt = Map.find(Val: ID); |
| 1897 | if (MapIt == Map.end()) |
| 1898 | return make_range(x: nullptr, y: nullptr); |
| 1899 | |
| 1900 | return make_range(x: MapIt->second.begin(), y: MapIt->second.end()); |
| 1901 | } |
| 1902 | |
| 1903 | AssignmentMarkerRange at::getAssignmentMarkers(DIAssignID *ID) { |
| 1904 | assert(ID && "Expected non-null ID" ); |
| 1905 | LLVMContext &Ctx = ID->getContext(); |
| 1906 | |
| 1907 | auto *IDAsValue = MetadataAsValue::getIfExists(Context&: Ctx, MD: ID); |
| 1908 | |
| 1909 | // The ID is only used wrapped in MetadataAsValue(ID), so lets check that |
| 1910 | // one of those already exists first. |
| 1911 | if (!IDAsValue) |
| 1912 | return make_range(x: Value::user_iterator(), y: Value::user_iterator()); |
| 1913 | |
| 1914 | return make_range(x: IDAsValue->user_begin(), y: IDAsValue->user_end()); |
| 1915 | } |
| 1916 | |
| 1917 | void at::deleteAssignmentMarkers(const Instruction *Inst) { |
| 1918 | auto Range = getAssignmentMarkers(Inst); |
| 1919 | SmallVector<DbgVariableRecord *> DVRAssigns = getDVRAssignmentMarkers(Inst); |
| 1920 | if (Range.empty() && DVRAssigns.empty()) |
| 1921 | return; |
| 1922 | SmallVector<DbgAssignIntrinsic *> ToDelete(Range.begin(), Range.end()); |
| 1923 | for (auto *DAI : ToDelete) |
| 1924 | DAI->eraseFromParent(); |
| 1925 | for (auto *DVR : DVRAssigns) |
| 1926 | DVR->eraseFromParent(); |
| 1927 | } |
| 1928 | |
| 1929 | void at::RAUW(DIAssignID *Old, DIAssignID *New) { |
| 1930 | // Replace attachments. |
| 1931 | AssignmentInstRange InstRange = getAssignmentInsts(ID: Old); |
| 1932 | // Use intermediate storage for the instruction ptrs because the |
| 1933 | // getAssignmentInsts range iterators will be invalidated by adding and |
| 1934 | // removing DIAssignID attachments. |
| 1935 | SmallVector<Instruction *> InstVec(InstRange.begin(), InstRange.end()); |
| 1936 | for (auto *I : InstVec) |
| 1937 | I->setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: New); |
| 1938 | |
| 1939 | Old->replaceAllUsesWith(MD: New); |
| 1940 | } |
| 1941 | |
| 1942 | void at::deleteAll(Function *F) { |
| 1943 | SmallVector<DbgAssignIntrinsic *, 12> ToDelete; |
| 1944 | SmallVector<DbgVariableRecord *, 12> DPToDelete; |
| 1945 | for (BasicBlock &BB : *F) { |
| 1946 | for (Instruction &I : BB) { |
| 1947 | for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) |
| 1948 | if (DVR.isDbgAssign()) |
| 1949 | DPToDelete.push_back(Elt: &DVR); |
| 1950 | if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(Val: &I)) |
| 1951 | ToDelete.push_back(Elt: DAI); |
| 1952 | else |
| 1953 | I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: nullptr); |
| 1954 | } |
| 1955 | } |
| 1956 | for (auto *DAI : ToDelete) |
| 1957 | DAI->eraseFromParent(); |
| 1958 | for (auto *DVR : DPToDelete) |
| 1959 | DVR->eraseFromParent(); |
| 1960 | } |
| 1961 | |
| 1962 | /// FIXME: Remove this wrapper function and call |
| 1963 | /// DIExpression::calculateFragmentIntersect directly. |
| 1964 | template <typename T> |
| 1965 | bool calculateFragmentIntersectImpl( |
| 1966 | const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, |
| 1967 | uint64_t SliceSizeInBits, const T *AssignRecord, |
| 1968 | std::optional<DIExpression::FragmentInfo> &Result) { |
| 1969 | // No overlap if this DbgRecord describes a killed location. |
| 1970 | if (AssignRecord->isKillAddress()) |
| 1971 | return false; |
| 1972 | |
| 1973 | int64_t AddrOffsetInBits; |
| 1974 | { |
| 1975 | int64_t AddrOffsetInBytes; |
| 1976 | SmallVector<uint64_t> PostOffsetOps; //< Unused. |
| 1977 | // Bail if we can't find a constant offset (or none) in the expression. |
| 1978 | if (!AssignRecord->getAddressExpression()->extractLeadingOffset( |
| 1979 | AddrOffsetInBytes, PostOffsetOps)) |
| 1980 | return false; |
| 1981 | AddrOffsetInBits = AddrOffsetInBytes * 8; |
| 1982 | } |
| 1983 | |
| 1984 | Value *Addr = AssignRecord->getAddress(); |
| 1985 | // FIXME: It may not always be zero. |
| 1986 | int64_t = 0; |
| 1987 | DIExpression::FragmentInfo VarFrag = |
| 1988 | AssignRecord->getFragmentOrEntireVariable(); |
| 1989 | |
| 1990 | int64_t OffsetFromLocationInBits; //< Unused. |
| 1991 | return DIExpression::calculateFragmentIntersect( |
| 1992 | DL, SliceStart: Dest, SliceOffsetInBits, SliceSizeInBits, DbgPtr: Addr, DbgPtrOffsetInBits: AddrOffsetInBits, |
| 1993 | DbgExtractOffsetInBits: BitExtractOffsetInBits, VarFrag, Result, OffsetFromLocationInBits); |
| 1994 | } |
| 1995 | |
| 1996 | /// FIXME: Remove this wrapper function and call |
| 1997 | /// DIExpression::calculateFragmentIntersect directly. |
| 1998 | bool at::calculateFragmentIntersect( |
| 1999 | const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, |
| 2000 | uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DbgAssign, |
| 2001 | std::optional<DIExpression::FragmentInfo> &Result) { |
| 2002 | return calculateFragmentIntersectImpl(DL, Dest, SliceOffsetInBits, |
| 2003 | SliceSizeInBits, AssignRecord: DbgAssign, Result); |
| 2004 | } |
| 2005 | |
| 2006 | /// FIXME: Remove this wrapper function and call |
| 2007 | /// DIExpression::calculateFragmentIntersect directly. |
| 2008 | bool at::calculateFragmentIntersect( |
| 2009 | const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, |
| 2010 | uint64_t SliceSizeInBits, const DbgVariableRecord *DVRAssign, |
| 2011 | std::optional<DIExpression::FragmentInfo> &Result) { |
| 2012 | return calculateFragmentIntersectImpl(DL, Dest, SliceOffsetInBits, |
| 2013 | SliceSizeInBits, AssignRecord: DVRAssign, Result); |
| 2014 | } |
| 2015 | |
| 2016 | /// Update inlined instructions' DIAssignID metadata. We need to do this |
| 2017 | /// otherwise a function inlined more than once into the same function |
| 2018 | /// will cause DIAssignID to be shared by many instructions. |
| 2019 | void at::remapAssignID(DenseMap<DIAssignID *, DIAssignID *> &Map, |
| 2020 | Instruction &I) { |
| 2021 | auto GetNewID = [&Map](Metadata *Old) { |
| 2022 | DIAssignID *OldID = cast<DIAssignID>(Val: Old); |
| 2023 | if (DIAssignID *NewID = Map.lookup(Val: OldID)) |
| 2024 | return NewID; |
| 2025 | DIAssignID *NewID = DIAssignID::getDistinct(Context&: OldID->getContext()); |
| 2026 | Map[OldID] = NewID; |
| 2027 | return NewID; |
| 2028 | }; |
| 2029 | // If we find a DIAssignID attachment or use, replace it with a new version. |
| 2030 | for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) { |
| 2031 | if (DVR.isDbgAssign()) |
| 2032 | DVR.setAssignId(GetNewID(DVR.getAssignID())); |
| 2033 | } |
| 2034 | if (auto *ID = I.getMetadata(KindID: LLVMContext::MD_DIAssignID)) |
| 2035 | I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: GetNewID(ID)); |
| 2036 | else if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(Val: &I)) |
| 2037 | DAI->setAssignId(GetNewID(DAI->getAssignID())); |
| 2038 | } |
| 2039 | |
| 2040 | /// Collect constant properies (base, size, offset) of \p StoreDest. |
| 2041 | /// Return std::nullopt if any properties are not constants or the |
| 2042 | /// offset from the base pointer is negative. |
| 2043 | static std::optional<AssignmentInfo> |
| 2044 | getAssignmentInfoImpl(const DataLayout &DL, const Value *StoreDest, |
| 2045 | TypeSize SizeInBits) { |
| 2046 | if (SizeInBits.isScalable()) |
| 2047 | return std::nullopt; |
| 2048 | APInt GEPOffset(DL.getIndexTypeSizeInBits(Ty: StoreDest->getType()), 0); |
| 2049 | const Value *Base = StoreDest->stripAndAccumulateConstantOffsets( |
| 2050 | DL, Offset&: GEPOffset, /*AllowNonInbounds*/ true); |
| 2051 | |
| 2052 | if (GEPOffset.isNegative()) |
| 2053 | return std::nullopt; |
| 2054 | |
| 2055 | uint64_t OffsetInBytes = GEPOffset.getLimitedValue(); |
| 2056 | // Check for overflow. |
| 2057 | if (OffsetInBytes == UINT64_MAX) |
| 2058 | return std::nullopt; |
| 2059 | if (const auto *Alloca = dyn_cast<AllocaInst>(Val: Base)) |
| 2060 | return AssignmentInfo(DL, Alloca, OffsetInBytes * 8, SizeInBits); |
| 2061 | return std::nullopt; |
| 2062 | } |
| 2063 | |
| 2064 | std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL, |
| 2065 | const MemIntrinsic *I) { |
| 2066 | const Value *StoreDest = I->getRawDest(); |
| 2067 | // Assume 8 bit bytes. |
| 2068 | auto *ConstLengthInBytes = dyn_cast<ConstantInt>(Val: I->getLength()); |
| 2069 | if (!ConstLengthInBytes) |
| 2070 | // We can't use a non-const size, bail. |
| 2071 | return std::nullopt; |
| 2072 | uint64_t SizeInBits = 8 * ConstLengthInBytes->getZExtValue(); |
| 2073 | return getAssignmentInfoImpl(DL, StoreDest, SizeInBits: TypeSize::getFixed(ExactSize: SizeInBits)); |
| 2074 | } |
| 2075 | |
| 2076 | std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL, |
| 2077 | const StoreInst *SI) { |
| 2078 | TypeSize SizeInBits = DL.getTypeSizeInBits(Ty: SI->getValueOperand()->getType()); |
| 2079 | return getAssignmentInfoImpl(DL, StoreDest: SI->getPointerOperand(), SizeInBits); |
| 2080 | } |
| 2081 | |
| 2082 | std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL, |
| 2083 | const AllocaInst *AI) { |
| 2084 | TypeSize SizeInBits = DL.getTypeSizeInBits(Ty: AI->getAllocatedType()); |
| 2085 | return getAssignmentInfoImpl(DL, StoreDest: AI, SizeInBits); |
| 2086 | } |
| 2087 | |
| 2088 | /// Returns nullptr if the assignment shouldn't be attributed to this variable. |
| 2089 | static void emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest, |
| 2090 | Instruction &StoreLikeInst, const VarRecord &VarRec, |
| 2091 | DIBuilder &DIB) { |
| 2092 | auto *ID = StoreLikeInst.getMetadata(KindID: LLVMContext::MD_DIAssignID); |
| 2093 | assert(ID && "Store instruction must have DIAssignID metadata" ); |
| 2094 | (void)ID; |
| 2095 | |
| 2096 | const uint64_t StoreStartBit = Info.OffsetInBits; |
| 2097 | const uint64_t StoreEndBit = Info.OffsetInBits + Info.SizeInBits; |
| 2098 | |
| 2099 | uint64_t FragStartBit = StoreStartBit; |
| 2100 | uint64_t FragEndBit = StoreEndBit; |
| 2101 | |
| 2102 | bool StoreToWholeVariable = Info.StoreToWholeAlloca; |
| 2103 | if (auto Size = VarRec.Var->getSizeInBits()) { |
| 2104 | // NOTE: trackAssignments doesn't understand base expressions yet, so all |
| 2105 | // variables that reach here are guaranteed to start at offset 0 in the |
| 2106 | // alloca. |
| 2107 | const uint64_t VarStartBit = 0; |
| 2108 | const uint64_t VarEndBit = *Size; |
| 2109 | |
| 2110 | // FIXME: trim FragStartBit when nonzero VarStartBit is supported. |
| 2111 | FragEndBit = std::min(a: FragEndBit, b: VarEndBit); |
| 2112 | |
| 2113 | // Discard stores to bits outside this variable. |
| 2114 | if (FragStartBit >= FragEndBit) |
| 2115 | return; |
| 2116 | |
| 2117 | StoreToWholeVariable = FragStartBit <= VarStartBit && FragEndBit >= *Size; |
| 2118 | } |
| 2119 | |
| 2120 | DIExpression *Expr = DIExpression::get(Context&: StoreLikeInst.getContext(), Elements: {}); |
| 2121 | if (!StoreToWholeVariable) { |
| 2122 | auto R = DIExpression::createFragmentExpression(Expr, OffsetInBits: FragStartBit, |
| 2123 | SizeInBits: FragEndBit - FragStartBit); |
| 2124 | assert(R.has_value() && "failed to create fragment expression" ); |
| 2125 | Expr = *R; |
| 2126 | } |
| 2127 | DIExpression *AddrExpr = DIExpression::get(Context&: StoreLikeInst.getContext(), Elements: {}); |
| 2128 | auto *Assign = DbgVariableRecord::createLinkedDVRAssign( |
| 2129 | LinkedInstr: &StoreLikeInst, Val, Variable: VarRec.Var, Expression: Expr, Address: Dest, AddressExpression: AddrExpr, DI: VarRec.DL); |
| 2130 | (void)Assign; |
| 2131 | LLVM_DEBUG(if (Assign) errs() << " > INSERT: " << *Assign << "\n" ); |
| 2132 | } |
| 2133 | |
| 2134 | #undef DEBUG_TYPE // Silence redefinition warning (from ConstantsContext.h). |
| 2135 | #define DEBUG_TYPE "assignment-tracking" |
| 2136 | |
| 2137 | void at::trackAssignments(Function::iterator Start, Function::iterator End, |
| 2138 | const StorageToVarsMap &Vars, const DataLayout &DL, |
| 2139 | bool DebugPrints) { |
| 2140 | // Early-exit if there are no interesting variables. |
| 2141 | if (Vars.empty()) |
| 2142 | return; |
| 2143 | |
| 2144 | auto &Ctx = Start->getContext(); |
| 2145 | auto &Module = *Start->getModule(); |
| 2146 | |
| 2147 | // Poison type doesn't matter, so long as it isn't void. Let's just use i1. |
| 2148 | auto *Poison = PoisonValue::get(T: Type::getInt1Ty(C&: Ctx)); |
| 2149 | DIBuilder DIB(Module, /*AllowUnresolved*/ false); |
| 2150 | |
| 2151 | // Scan the instructions looking for stores to local variables' storage. |
| 2152 | LLVM_DEBUG(errs() << "# Scanning instructions\n" ); |
| 2153 | for (auto BBI = Start; BBI != End; ++BBI) { |
| 2154 | for (Instruction &I : *BBI) { |
| 2155 | |
| 2156 | std::optional<AssignmentInfo> Info; |
| 2157 | Value *ValueComponent = nullptr; |
| 2158 | Value *DestComponent = nullptr; |
| 2159 | if (auto *AI = dyn_cast<AllocaInst>(Val: &I)) { |
| 2160 | // We want to track the variable's stack home from its alloca's |
| 2161 | // position onwards so we treat it as an assignment (where the stored |
| 2162 | // value is poison). |
| 2163 | Info = getAssignmentInfo(DL, AI); |
| 2164 | ValueComponent = Poison; |
| 2165 | DestComponent = AI; |
| 2166 | } else if (auto *SI = dyn_cast<StoreInst>(Val: &I)) { |
| 2167 | Info = getAssignmentInfo(DL, SI); |
| 2168 | ValueComponent = SI->getValueOperand(); |
| 2169 | DestComponent = SI->getPointerOperand(); |
| 2170 | } else if (auto *MI = dyn_cast<MemTransferInst>(Val: &I)) { |
| 2171 | Info = getAssignmentInfo(DL, I: MI); |
| 2172 | // May not be able to represent this value easily. |
| 2173 | ValueComponent = Poison; |
| 2174 | DestComponent = MI->getOperand(i_nocapture: 0); |
| 2175 | } else if (auto *MI = dyn_cast<MemSetInst>(Val: &I)) { |
| 2176 | Info = getAssignmentInfo(DL, I: MI); |
| 2177 | // If we're zero-initing we can state the assigned value is zero, |
| 2178 | // otherwise use undef. |
| 2179 | auto *ConstValue = dyn_cast<ConstantInt>(Val: MI->getOperand(i_nocapture: 1)); |
| 2180 | if (ConstValue && ConstValue->isZero()) |
| 2181 | ValueComponent = ConstValue; |
| 2182 | else |
| 2183 | ValueComponent = Poison; |
| 2184 | DestComponent = MI->getOperand(i_nocapture: 0); |
| 2185 | } else { |
| 2186 | // Not a store-like instruction. |
| 2187 | continue; |
| 2188 | } |
| 2189 | |
| 2190 | assert(ValueComponent && DestComponent); |
| 2191 | LLVM_DEBUG(errs() << "SCAN: Found store-like: " << I << "\n" ); |
| 2192 | |
| 2193 | // Check if getAssignmentInfo failed to understand this store. |
| 2194 | if (!Info.has_value()) { |
| 2195 | LLVM_DEBUG( |
| 2196 | errs() |
| 2197 | << " | SKIP: Untrackable store (e.g. through non-const gep)\n" ); |
| 2198 | continue; |
| 2199 | } |
| 2200 | LLVM_DEBUG(errs() << " | BASE: " << *Info->Base << "\n" ); |
| 2201 | |
| 2202 | // Check if the store destination is a local variable with debug info. |
| 2203 | auto LocalIt = Vars.find(Val: Info->Base); |
| 2204 | if (LocalIt == Vars.end()) { |
| 2205 | LLVM_DEBUG( |
| 2206 | errs() |
| 2207 | << " | SKIP: Base address not associated with local variable\n" ); |
| 2208 | continue; |
| 2209 | } |
| 2210 | |
| 2211 | DIAssignID *ID = |
| 2212 | cast_or_null<DIAssignID>(Val: I.getMetadata(KindID: LLVMContext::MD_DIAssignID)); |
| 2213 | if (!ID) { |
| 2214 | ID = DIAssignID::getDistinct(Context&: Ctx); |
| 2215 | I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: ID); |
| 2216 | } |
| 2217 | |
| 2218 | for (const VarRecord &R : LocalIt->second) |
| 2219 | emitDbgAssign(Info: *Info, Val: ValueComponent, Dest: DestComponent, StoreLikeInst&: I, VarRec: R, DIB); |
| 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | bool AssignmentTrackingPass::runOnFunction(Function &F) { |
| 2225 | // No value in assignment tracking without optimisations. |
| 2226 | if (F.hasFnAttribute(Kind: Attribute::OptimizeNone)) |
| 2227 | return /*Changed*/ false; |
| 2228 | |
| 2229 | bool Changed = false; |
| 2230 | auto *DL = &F.getDataLayout(); |
| 2231 | // Collect a map of {backing storage : dbg.declares} (currently "backing |
| 2232 | // storage" is limited to Allocas). We'll use this to find dbg.declares to |
| 2233 | // delete after running `trackAssignments`. |
| 2234 | DenseMap<const AllocaInst *, SmallPtrSet<DbgDeclareInst *, 2>> DbgDeclares; |
| 2235 | DenseMap<const AllocaInst *, SmallPtrSet<DbgVariableRecord *, 2>> DVRDeclares; |
| 2236 | // Create another similar map of {storage : variables} that we'll pass to |
| 2237 | // trackAssignments. |
| 2238 | StorageToVarsMap Vars; |
| 2239 | auto ProcessDeclare = [&](auto *Declare, auto &DeclareList) { |
| 2240 | // FIXME: trackAssignments doesn't let you specify any modifiers to the |
| 2241 | // variable (e.g. fragment) or location (e.g. offset), so we have to |
| 2242 | // leave dbg.declares with non-empty expressions in place. |
| 2243 | if (Declare->getExpression()->getNumElements() != 0) |
| 2244 | return; |
| 2245 | if (!Declare->getAddress()) |
| 2246 | return; |
| 2247 | if (AllocaInst *Alloca = |
| 2248 | dyn_cast<AllocaInst>(Declare->getAddress()->stripPointerCasts())) { |
| 2249 | // FIXME: Skip VLAs for now (let these variables use dbg.declares). |
| 2250 | if (!Alloca->isStaticAlloca()) |
| 2251 | return; |
| 2252 | // Similarly, skip scalable vectors (use dbg.declares instead). |
| 2253 | if (auto Sz = Alloca->getAllocationSize(DL: *DL); Sz && Sz->isScalable()) |
| 2254 | return; |
| 2255 | DeclareList[Alloca].insert(Declare); |
| 2256 | Vars[Alloca].insert(X: VarRecord(Declare)); |
| 2257 | } |
| 2258 | }; |
| 2259 | for (auto &BB : F) { |
| 2260 | for (auto &I : BB) { |
| 2261 | for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) { |
| 2262 | if (DVR.isDbgDeclare()) |
| 2263 | ProcessDeclare(&DVR, DVRDeclares); |
| 2264 | } |
| 2265 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(Val: &I)) |
| 2266 | ProcessDeclare(DDI, DbgDeclares); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | // FIXME: Locals can be backed by caller allocas (sret, byval). |
| 2271 | // Note: trackAssignments doesn't respect dbg.declare's IR positions (as it |
| 2272 | // doesn't "understand" dbg.declares). However, this doesn't appear to break |
| 2273 | // any rules given this description of dbg.declare from |
| 2274 | // llvm/docs/SourceLevelDebugging.rst: |
| 2275 | // |
| 2276 | // It is not control-dependent, meaning that if a call to llvm.dbg.declare |
| 2277 | // exists and has a valid location argument, that address is considered to |
| 2278 | // be the true home of the variable across its entire lifetime. |
| 2279 | trackAssignments(Start: F.begin(), End: F.end(), Vars, DL: *DL); |
| 2280 | |
| 2281 | // Delete dbg.declares for variables now tracked with assignment tracking. |
| 2282 | auto DeleteSubsumedDeclare = [&](const auto &Markers, auto &Declares) { |
| 2283 | (void)Markers; |
| 2284 | for (auto *Declare : Declares) { |
| 2285 | // Assert that the alloca that Declare uses is now linked to a dbg.assign |
| 2286 | // describing the same variable (i.e. check that this dbg.declare has |
| 2287 | // been replaced by a dbg.assign). Use DebugVariableAggregate to Discard |
| 2288 | // the fragment part because trackAssignments may alter the |
| 2289 | // fragment. e.g. if the alloca is smaller than the variable, then |
| 2290 | // trackAssignments will create an alloca-sized fragment for the |
| 2291 | // dbg.assign. |
| 2292 | assert(llvm::any_of(Markers, [Declare](auto *Assign) { |
| 2293 | return DebugVariableAggregate(Assign) == |
| 2294 | DebugVariableAggregate(Declare); |
| 2295 | })); |
| 2296 | // Delete Declare because the variable location is now tracked using |
| 2297 | // assignment tracking. |
| 2298 | Declare->eraseFromParent(); |
| 2299 | Changed = true; |
| 2300 | } |
| 2301 | }; |
| 2302 | for (auto &P : DbgDeclares) |
| 2303 | DeleteSubsumedDeclare(at::getAssignmentMarkers(Inst: P.first), P.second); |
| 2304 | for (auto &P : DVRDeclares) |
| 2305 | DeleteSubsumedDeclare(at::getDVRAssignmentMarkers(Inst: P.first), P.second); |
| 2306 | return Changed; |
| 2307 | } |
| 2308 | |
| 2309 | static const char *AssignmentTrackingModuleFlag = |
| 2310 | "debug-info-assignment-tracking" ; |
| 2311 | |
| 2312 | static void setAssignmentTrackingModuleFlag(Module &M) { |
| 2313 | M.setModuleFlag(Behavior: Module::ModFlagBehavior::Max, Key: AssignmentTrackingModuleFlag, |
| 2314 | Val: ConstantAsMetadata::get( |
| 2315 | C: ConstantInt::get(Ty: Type::getInt1Ty(C&: M.getContext()), V: 1))); |
| 2316 | } |
| 2317 | |
| 2318 | static bool getAssignmentTrackingModuleFlag(const Module &M) { |
| 2319 | Metadata *Value = M.getModuleFlag(Key: AssignmentTrackingModuleFlag); |
| 2320 | return Value && !cast<ConstantAsMetadata>(Val: Value)->getValue()->isZeroValue(); |
| 2321 | } |
| 2322 | |
| 2323 | bool llvm::isAssignmentTrackingEnabled(const Module &M) { |
| 2324 | return getAssignmentTrackingModuleFlag(M); |
| 2325 | } |
| 2326 | |
| 2327 | PreservedAnalyses AssignmentTrackingPass::run(Function &F, |
| 2328 | FunctionAnalysisManager &AM) { |
| 2329 | if (!runOnFunction(F)) |
| 2330 | return PreservedAnalyses::all(); |
| 2331 | |
| 2332 | // Record that this module uses assignment tracking. It doesn't matter that |
| 2333 | // some functons in the module may not use it - the debug info in those |
| 2334 | // functions will still be handled properly. |
| 2335 | setAssignmentTrackingModuleFlag(*F.getParent()); |
| 2336 | |
| 2337 | // Q: Can we return a less conservative set than just CFGAnalyses? Can we |
| 2338 | // return PreservedAnalyses::all()? |
| 2339 | PreservedAnalyses PA; |
| 2340 | PA.preserveSet<CFGAnalyses>(); |
| 2341 | return PA; |
| 2342 | } |
| 2343 | |
| 2344 | PreservedAnalyses AssignmentTrackingPass::run(Module &M, |
| 2345 | ModuleAnalysisManager &AM) { |
| 2346 | bool Changed = false; |
| 2347 | for (auto &F : M) |
| 2348 | Changed |= runOnFunction(F); |
| 2349 | |
| 2350 | if (!Changed) |
| 2351 | return PreservedAnalyses::all(); |
| 2352 | |
| 2353 | // Record that this module uses assignment tracking. |
| 2354 | setAssignmentTrackingModuleFlag(M); |
| 2355 | |
| 2356 | // Q: Can we return a less conservative set than just CFGAnalyses? Can we |
| 2357 | // return PreservedAnalyses::all()? |
| 2358 | PreservedAnalyses PA; |
| 2359 | PA.preserveSet<CFGAnalyses>(); |
| 2360 | return PA; |
| 2361 | } |
| 2362 | |
| 2363 | #undef DEBUG_TYPE |
| 2364 | |