clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DwarfCompileUnit.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
| 1 |  | 
| 2 |  | 
| 3 |  | 
| 4 |  | 
| 5 |  | 
| 6 |  | 
| 7 |  | 
| 8 |  | 
| 9 |  | 
| 10 |  | 
| 11 |  | 
| 12 |  | 
| 13 | #include "DwarfCompileUnit.h" | 
| 14 | #include "AddressPool.h" | 
| 15 | #include "DwarfExpression.h" | 
| 16 | #include "llvm/ADT/None.h" | 
| 17 | #include "llvm/ADT/STLExtras.h" | 
| 18 | #include "llvm/ADT/SmallString.h" | 
| 19 | #include "llvm/BinaryFormat/Dwarf.h" | 
| 20 | #include "llvm/CodeGen/AsmPrinter.h" | 
| 21 | #include "llvm/CodeGen/DIE.h" | 
| 22 | #include "llvm/CodeGen/MachineFunction.h" | 
| 23 | #include "llvm/CodeGen/MachineInstr.h" | 
| 24 | #include "llvm/CodeGen/MachineOperand.h" | 
| 25 | #include "llvm/CodeGen/TargetFrameLowering.h" | 
| 26 | #include "llvm/CodeGen/TargetRegisterInfo.h" | 
| 27 | #include "llvm/CodeGen/TargetSubtargetInfo.h" | 
| 28 | #include "llvm/IR/DataLayout.h" | 
| 29 | #include "llvm/IR/DebugInfo.h" | 
| 30 | #include "llvm/IR/GlobalVariable.h" | 
| 31 | #include "llvm/MC/MCSection.h" | 
| 32 | #include "llvm/MC/MCStreamer.h" | 
| 33 | #include "llvm/MC/MCSymbol.h" | 
| 34 | #include "llvm/MC/MCSymbolWasm.h" | 
| 35 | #include "llvm/MC/MachineLocation.h" | 
| 36 | #include "llvm/Target/TargetLoweringObjectFile.h" | 
| 37 | #include "llvm/Target/TargetMachine.h" | 
| 38 | #include "llvm/Target/TargetOptions.h" | 
| 39 | #include <iterator> | 
| 40 | #include <string> | 
| 41 | #include <utility> | 
| 42 |  | 
| 43 | using namespace llvm; | 
| 44 |  | 
| 45 | static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) { | 
| 46 |  | 
| 47 |  | 
| 48 |  | 
| 49 |  | 
| 50 |  | 
| 51 |  | 
| 52 | if (DW->getDwarfVersion() >= 5 && Kind == UnitKind::Skeleton) | 
| 53 | return dwarf::DW_TAG_skeleton_unit; | 
| 54 |  | 
| 55 | return dwarf::DW_TAG_compile_unit; | 
| 56 | } | 
| 57 |  | 
| 58 | DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, | 
| 59 | AsmPrinter *A, DwarfDebug *DW, | 
| 60 | DwarfFile *DWU, UnitKind Kind) | 
| 61 | : DwarfUnit(GetCompileUnitType(Kind, DW), Node, A, DW, DWU), UniqueID(UID) { | 
| 62 | insertDIE(Node, &getUnitDie()); | 
| 63 | MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin"); | 
| 64 | } | 
| 65 |  | 
| 66 |  | 
| 67 |  | 
| 68 | void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, | 
| 69 | const MCSymbol *Label) { | 
| 70 |  | 
| 71 | if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5) | 
|  | | 7 |  | Assuming the condition is false |  | 
 | 
|  | | 8 |  | Assuming field 'Skeleton' is non-null |  | 
 | 
| 72 | return addLocalLabelAddress(Die, Attribute, Label); | 
| 73 |  | 
| 74 | if (Label) | 
|  | | 9 |  | Assuming 'Label' is null |  | 
 | 
|  |  | 
| 75 | DD->addArangeLabel(SymbolCU(this, Label)); | 
| 76 |  | 
| 77 | bool UseAddrOffsetFormOrExpressions = | 
| 78 | DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions(); | 
| 79 |  | 
| 80 | const MCSymbol *Base = nullptr; | 
| 81 | if (Label->isInSection() && UseAddrOffsetFormOrExpressions) | 
|  | | 11 |  | Called C++ object pointer is null | 
 | 
| 82 | Base = DD->getSectionLabel(&Label->getSection()); | 
| 83 |  | 
| 84 | if (!Base || Base == Label) { | 
| 85 | unsigned idx = DD->getAddressPool().getIndex(Label); | 
| 86 | addAttribute(Die, Attribute, | 
| 87 | DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx | 
| 88 | : dwarf::DW_FORM_GNU_addr_index, | 
| 89 | DIEInteger(idx)); | 
| 90 | return; | 
| 91 | } | 
| 92 |  | 
| 93 |  | 
| 94 |  | 
| 95 | assert(DD->getDwarfVersion() >= 5 && | 
| 96 | "Addr+offset expressions are only valuable when using debug_addr (to " | 
| 97 | "reduce relocations) available in DWARFv5 or higher"); | 
| 98 | if (DD->useAddrOffsetExpressions()) { | 
| 99 | auto *Loc = new (DIEValueAllocator) DIEBlock(); | 
| 100 | addPoolOpAddress(*Loc, Label); | 
| 101 | addBlock(Die, Attribute, dwarf::DW_FORM_exprloc, Loc); | 
| 102 | } else | 
| 103 | addAttribute(Die, Attribute, dwarf::DW_FORM_LLVM_addrx_offset, | 
| 104 | new (DIEValueAllocator) DIEAddrOffset( | 
| 105 | DD->getAddressPool().getIndex(Base), Label, Base)); | 
| 106 | } | 
| 107 |  | 
| 108 | void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, | 
| 109 | dwarf::Attribute Attribute, | 
| 110 | const MCSymbol *Label) { | 
| 111 | if (Label) | 
| 112 | DD->addArangeLabel(SymbolCU(this, Label)); | 
| 113 |  | 
| 114 | if (Label) | 
| 115 | addAttribute(Die, Attribute, dwarf::DW_FORM_addr, DIELabel(Label)); | 
| 116 | else | 
| 117 | addAttribute(Die, Attribute, dwarf::DW_FORM_addr, DIEInteger(0)); | 
| 118 | } | 
| 119 |  | 
| 120 | unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) { | 
| 121 |  | 
| 122 |  | 
| 123 |  | 
| 124 |  | 
| 125 |  | 
| 126 | unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID(); | 
| 127 | if (!File) | 
| 128 | return Asm->OutStreamer->emitDwarfFileDirective(0, "", "", None, None, | 
| 129 | CUID); | 
| 130 | return Asm->OutStreamer->emitDwarfFileDirective( | 
| 131 | 0, File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File), | 
| 132 | File->getSource(), CUID); | 
| 133 | } | 
| 134 |  | 
| 135 | DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( | 
| 136 | const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { | 
| 137 |  | 
| 138 | if (DIE *Die = getDIE(GV)) | 
| 139 | return Die; | 
| 140 |  | 
| 141 | assert(GV); | 
| 142 |  | 
| 143 | auto *GVContext = GV->getScope(); | 
| 144 | const DIType *GTy = GV->getType(); | 
| 145 |  | 
| 146 |  | 
| 147 |  | 
| 148 | auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr; | 
| 149 | DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs) | 
| 150 | : getOrCreateContextDIE(GVContext); | 
| 151 |  | 
| 152 |  | 
| 153 | DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); | 
| 154 | DIScope *DeclContext; | 
| 155 | if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { | 
| 156 | DeclContext = SDMDecl->getScope(); | 
| 157 | assert(SDMDecl->isStaticMember() && "Expected static member decl"); | 
| 158 | assert(GV->isDefinition()); | 
| 159 |  | 
| 160 | DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); | 
| 161 | addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); | 
| 162 |  | 
| 163 |  | 
| 164 | if (GTy != SDMDecl->getBaseType()) | 
| 165 | addType(*VariableDIE, GTy); | 
| 166 | } else { | 
| 167 | DeclContext = GV->getScope(); | 
| 168 |  | 
| 169 | addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); | 
| 170 | if (GTy) | 
| 171 | addType(*VariableDIE, GTy); | 
| 172 |  | 
| 173 |  | 
| 174 | if (!GV->isLocalToUnit()) | 
| 175 | addFlag(*VariableDIE, dwarf::DW_AT_external); | 
| 176 |  | 
| 177 |  | 
| 178 | addSourceLine(*VariableDIE, GV); | 
| 179 | } | 
| 180 |  | 
| 181 | if (!GV->isDefinition()) | 
| 182 | addFlag(*VariableDIE, dwarf::DW_AT_declaration); | 
| 183 | else | 
| 184 | addGlobalName(GV->getName(), *VariableDIE, DeclContext); | 
| 185 |  | 
| 186 | if (uint32_t AlignInBytes = GV->getAlignInBytes()) | 
| 187 | addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, | 
| 188 | AlignInBytes); | 
| 189 |  | 
| 190 | if (MDTuple *TP = GV->getTemplateParams()) | 
| 191 | addTemplateParams(*VariableDIE, DINodeArray(TP)); | 
| 192 |  | 
| 193 |  | 
| 194 | addLocationAttribute(VariableDIE, GV, GlobalExprs); | 
| 195 |  | 
| 196 | return VariableDIE; | 
| 197 | } | 
| 198 |  | 
| 199 | void DwarfCompileUnit::addLocationAttribute( | 
| 200 | DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { | 
| 201 | bool addToAccelTable = false; | 
| 202 | DIELoc *Loc = nullptr; | 
| 203 | Optional<unsigned> NVPTXAddressSpace; | 
| 204 | std::unique_ptr<DIEDwarfExpression> DwarfExpr; | 
| 205 | for (const auto &GE : GlobalExprs) { | 
| 206 | const GlobalVariable *Global = GE.Var; | 
| 207 | const DIExpression *Expr = GE.Expr; | 
| 208 |  | 
| 209 |  | 
| 210 |  | 
| 211 |  | 
| 212 |  | 
| 213 | if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) { | 
| 214 | addToAccelTable = true; | 
| 215 | addConstantValue( | 
| 216 | *VariableDIE, | 
| 217 | DIExpression::SignedOrUnsignedConstant::UnsignedConstant == | 
| 218 | *Expr->isConstant(), | 
| 219 | Expr->getElement(1)); | 
| 220 | break; | 
| 221 | } | 
| 222 |  | 
| 223 |  | 
| 224 |  | 
| 225 | if (Global && Global->hasDLLImportStorageClass()) | 
| 226 | continue; | 
| 227 |  | 
| 228 |  | 
| 229 | if (!Global && (!Expr || !Expr->isConstant())) | 
| 230 | continue; | 
| 231 |  | 
| 232 | if (Global && Global->isThreadLocal() && | 
| 233 | !Asm->getObjFileLowering().supportDebugThreadLocalLocation()) | 
| 234 | continue; | 
| 235 |  | 
| 236 | if (!Loc) { | 
| 237 | addToAccelTable = true; | 
| 238 | Loc = new (DIEValueAllocator) DIELoc; | 
| 239 | DwarfExpr = std::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc); | 
| 240 | } | 
| 241 |  | 
| 242 | if (Expr) { | 
| 243 |  | 
| 244 |  | 
| 245 |  | 
| 246 |  | 
| 247 |  | 
| 248 |  | 
| 249 | unsigned LocalNVPTXAddressSpace; | 
| 250 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { | 
| 251 | const DIExpression *NewExpr = | 
| 252 | DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); | 
| 253 | if (NewExpr != Expr) { | 
| 254 | Expr = NewExpr; | 
| 255 | NVPTXAddressSpace = LocalNVPTXAddressSpace; | 
| 256 | } | 
| 257 | } | 
| 258 | DwarfExpr->addFragmentOffset(Expr); | 
| 259 | } | 
| 260 |  | 
| 261 | if (Global) { | 
| 262 | const MCSymbol *Sym = Asm->getSymbol(Global); | 
| 263 | if (Global->isThreadLocal()) { | 
| 264 | if (Asm->TM.useEmulatedTLS()) { | 
| 265 |  | 
| 266 | } else { | 
| 267 |  | 
| 268 | unsigned PointerSize = Asm->getDataLayout().getPointerSize(); | 
| 269 | assert((PointerSize == 4 || PointerSize == 8) && | 
| 270 | "Add support for other sizes if necessary"); | 
| 271 |  | 
| 272 | if (!DD->useSplitDwarf()) { | 
| 273 |  | 
| 274 | addUInt(*Loc, dwarf::DW_FORM_data1, | 
| 275 | PointerSize == 4 ? dwarf::DW_OP_const4u | 
| 276 | : dwarf::DW_OP_const8u); | 
| 277 |  | 
| 278 |  | 
| 279 | addExpr(*Loc, | 
| 280 | PointerSize == 4 ? dwarf::DW_FORM_data4 | 
| 281 | : dwarf::DW_FORM_data8, | 
| 282 | Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); | 
| 283 | } else { | 
| 284 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); | 
| 285 | addUInt(*Loc, dwarf::DW_FORM_udata, | 
| 286 | DD->getAddressPool().getIndex(Sym,  true)); | 
| 287 | } | 
| 288 |  | 
| 289 | addUInt(*Loc, dwarf::DW_FORM_data1, | 
| 290 | DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address | 
| 291 | : dwarf::DW_OP_form_tls_address); | 
| 292 | } | 
| 293 | } else { | 
| 294 | DD->addArangeLabel(SymbolCU(this, Sym)); | 
| 295 | addOpAddress(*Loc, Sym); | 
| 296 | } | 
| 297 | } | 
| 298 |  | 
| 299 |  | 
| 300 |  | 
| 301 |  | 
| 302 | if (DwarfExpr->isUnknownLocation()) | 
| 303 | DwarfExpr->setMemoryLocationKind(); | 
| 304 | DwarfExpr->addExpression(Expr); | 
| 305 | } | 
| 306 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { | 
| 307 |  | 
| 308 |  | 
| 309 |  | 
| 310 |  | 
| 311 | const unsigned NVPTX_ADDR_global_space = 5; | 
| 312 | addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, | 
| 313 | NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_global_space); | 
| 314 | } | 
| 315 | if (Loc) | 
| 316 | addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize()); | 
| 317 |  | 
| 318 | if (DD->useAllLinkageNames()) | 
| 319 | addLinkageName(*VariableDIE, GV->getLinkageName()); | 
| 320 |  | 
| 321 | if (addToAccelTable) { | 
| 322 | DD->addAccelName(*CUNode, GV->getName(), *VariableDIE); | 
| 323 |  | 
| 324 |  | 
| 325 |  | 
| 326 | if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() && | 
| 327 | DD->useAllLinkageNames()) | 
| 328 | DD->addAccelName(*CUNode, GV->getLinkageName(), *VariableDIE); | 
| 329 | } | 
| 330 | } | 
| 331 |  | 
| 332 | DIE *DwarfCompileUnit::getOrCreateCommonBlock( | 
| 333 | const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) { | 
| 334 |  | 
| 335 |  | 
| 336 | DIE *ContextDIE = getOrCreateContextDIE(CB->getScope()); | 
| 337 |  | 
| 338 | if (DIE *NDie = getDIE(CB)) | 
| 339 | return NDie; | 
| 340 | DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB); | 
| 341 | StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName(); | 
| 342 | addString(NDie, dwarf::DW_AT_name, Name); | 
| 343 | addGlobalName(Name, NDie, CB->getScope()); | 
| 344 | if (CB->getFile()) | 
| 345 | addSourceLine(NDie, CB->getLineNo(), CB->getFile()); | 
| 346 | if (DIGlobalVariable *V = CB->getDecl()) | 
| 347 | getCU().addLocationAttribute(&NDie, V, GlobalExprs); | 
| 348 | return &NDie; | 
| 349 | } | 
| 350 |  | 
| 351 | void DwarfCompileUnit::addRange(RangeSpan Range) { | 
| 352 | DD->insertSectionLabel(Range.Begin); | 
| 353 |  | 
| 354 | bool SameAsPrevCU = this == DD->getPrevCU(); | 
| 355 | DD->setPrevCU(this); | 
| 356 |  | 
| 357 |  | 
| 358 |  | 
| 359 |  | 
| 360 | if (CURanges.empty() || !SameAsPrevCU || | 
| 361 | (&CURanges.back().End->getSection() != | 
| 362 | &Range.End->getSection())) { | 
| 363 | CURanges.push_back(Range); | 
| 364 | return; | 
| 365 | } | 
| 366 |  | 
| 367 | CURanges.back().End = Range.End; | 
| 368 | } | 
| 369 |  | 
| 370 | void DwarfCompileUnit::initStmtList() { | 
| 371 | if (CUNode->isDebugDirectivesOnly()) | 
| 372 | return; | 
| 373 |  | 
| 374 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); | 
| 375 | if (DD->useSectionsAsReferences()) { | 
| 376 | LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol(); | 
| 377 | } else { | 
| 378 | LineTableStartSym = | 
| 379 | Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); | 
| 380 | } | 
| 381 |  | 
| 382 |  | 
| 383 |  | 
| 384 |  | 
| 385 |  | 
| 386 |  | 
| 387 | addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym, | 
| 388 | TLOF.getDwarfLineSection()->getBeginSymbol()); | 
| 389 | } | 
| 390 |  | 
| 391 | void DwarfCompileUnit::applyStmtList(DIE &D) { | 
| 392 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); | 
| 393 | addSectionLabel(D, dwarf::DW_AT_stmt_list, LineTableStartSym, | 
| 394 | TLOF.getDwarfLineSection()->getBeginSymbol()); | 
| 395 | } | 
| 396 |  | 
| 397 | void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, | 
| 398 | const MCSymbol *End) { | 
| 399 | assert(Begin && "Begin label should not be null!"); | 
| 400 | assert(End && "End label should not be null!"); | 
| 401 | assert(Begin->isDefined() && "Invalid starting label"); | 
| 402 | assert(End->isDefined() && "Invalid end label"); | 
| 403 |  | 
| 404 | addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); | 
| 405 | if (DD->getDwarfVersion() < 4) | 
| 406 | addLabelAddress(D, dwarf::DW_AT_high_pc, End); | 
| 407 | else | 
| 408 | addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); | 
| 409 | } | 
| 410 |  | 
| 411 |  | 
| 412 |  | 
| 413 |  | 
| 414 | DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { | 
| 415 | DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); | 
| 416 |  | 
| 417 | SmallVector<RangeSpan, 2> BB_List; | 
| 418 |  | 
| 419 |  | 
| 420 | for (const auto &R : Asm->MBBSectionRanges) | 
| 421 | BB_List.push_back({R.second.BeginLabel, R.second.EndLabel}); | 
| 422 |  | 
| 423 | attachRangesOrLowHighPC(*SPDie, BB_List); | 
| 424 |  | 
| 425 | if (DD->useAppleExtensionAttributes() && | 
| 426 | !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( | 
| 427 | *DD->getCurrentFunction())) | 
| 428 | addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); | 
| 429 |  | 
| 430 |  | 
| 431 | if (!includeMinimalInlineScopes()) { | 
| 432 | const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); | 
| 433 | TargetFrameLowering::DwarfFrameBase FrameBase = | 
| 434 | TFI->getDwarfFrameBase(*Asm->MF); | 
| 435 | switch (FrameBase.Kind) { | 
| 436 | case TargetFrameLowering::DwarfFrameBase::Register: { | 
| 437 | if (Register::isPhysicalRegister(FrameBase.Location.Reg)) { | 
| 438 | MachineLocation Location(FrameBase.Location.Reg); | 
| 439 | addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); | 
| 440 | } | 
| 441 | break; | 
| 442 | } | 
| 443 | case TargetFrameLowering::DwarfFrameBase::CFA: { | 
| 444 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 445 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa); | 
| 446 | addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc); | 
| 447 | break; | 
| 448 | } | 
| 449 | case TargetFrameLowering::DwarfFrameBase::WasmFrameBase: { | 
| 450 |  | 
| 451 |  | 
| 452 | const unsigned TI_GLOBAL_RELOC = 3; | 
| 453 | if (FrameBase.Location.WasmLoc.Kind == TI_GLOBAL_RELOC) { | 
| 454 |  | 
| 455 | assert(FrameBase.Location.WasmLoc.Index == 0); | 
| 456 | auto SPSym = cast<MCSymbolWasm>( | 
| 457 | Asm->GetExternalSymbolSymbol("__stack_pointer")); | 
| 458 |  | 
| 459 |  | 
| 460 |  | 
| 461 | SPSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); | 
| 462 | SPSym->setGlobalType(wasm::WasmGlobalType{ | 
| 463 | uint8_t(Asm->getSubtargetInfo().getTargetTriple().getArch() == | 
| 464 | Triple::wasm64 | 
| 465 | ? wasm::WASM_TYPE_I64 | 
| 466 | : wasm::WASM_TYPE_I32), | 
| 467 | true}); | 
| 468 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 469 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_WASM_location); | 
| 470 | addSInt(*Loc, dwarf::DW_FORM_sdata, TI_GLOBAL_RELOC); | 
| 471 | if (!isDwoUnit()) { | 
| 472 | addLabel(*Loc, dwarf::DW_FORM_data4, SPSym); | 
| 473 | DD->addArangeLabel(SymbolCU(this, SPSym)); | 
| 474 | } else { | 
| 475 |  | 
| 476 |  | 
| 477 |  | 
| 478 |  | 
| 479 | addUInt(*Loc, dwarf::DW_FORM_data4, FrameBase.Location.WasmLoc.Index); | 
| 480 | } | 
| 481 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); | 
| 482 | addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc); | 
| 483 | } else { | 
| 484 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 485 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 486 | DIExpressionCursor Cursor({}); | 
| 487 | DwarfExpr.addWasmLocation(FrameBase.Location.WasmLoc.Kind, | 
| 488 | FrameBase.Location.WasmLoc.Index); | 
| 489 | DwarfExpr.addExpression(std::move(Cursor)); | 
| 490 | addBlock(*SPDie, dwarf::DW_AT_frame_base, DwarfExpr.finalize()); | 
| 491 | } | 
| 492 | break; | 
| 493 | } | 
| 494 | } | 
| 495 | } | 
| 496 |  | 
| 497 |  | 
| 498 |  | 
| 499 | DD->addSubprogramNames(*CUNode, SP, *SPDie); | 
| 500 |  | 
| 501 | return *SPDie; | 
| 502 | } | 
| 503 |  | 
| 504 |  | 
| 505 | void DwarfCompileUnit::constructScopeDIE( | 
| 506 | LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { | 
| 507 | if (!Scope || !Scope->getScopeNode()) | 
| 508 | return; | 
| 509 |  | 
| 510 | auto *DS = Scope->getScopeNode(); | 
| 511 |  | 
| 512 | assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && | 
| 513 | "Only handle inlined subprograms here, use " | 
| 514 | "constructSubprogramScopeDIE for non-inlined " | 
| 515 | "subprograms"); | 
| 516 |  | 
| 517 | SmallVector<DIE *, 8> Children; | 
| 518 |  | 
| 519 |  | 
| 520 |  | 
| 521 |  | 
| 522 | DIE *ScopeDIE; | 
| 523 | if (Scope->getParent() && isa<DISubprogram>(DS)) { | 
| 524 | ScopeDIE = constructInlinedScopeDIE(Scope); | 
| 525 | if (!ScopeDIE) | 
| 526 | return; | 
| 527 |  | 
| 528 | createScopeChildrenDIE(Scope, Children); | 
| 529 | } else { | 
| 530 |  | 
| 531 | if (DD->isLexicalScopeDIENull(Scope)) | 
| 532 | return; | 
| 533 |  | 
| 534 | bool HasNonScopeChildren = false; | 
| 535 |  | 
| 536 |  | 
| 537 |  | 
| 538 | createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren); | 
| 539 |  | 
| 540 |  | 
| 541 |  | 
| 542 | if (!HasNonScopeChildren) { | 
| 543 | FinalChildren.insert(FinalChildren.end(), | 
| 544 | std::make_move_iterator(Children.begin()), | 
| 545 | std::make_move_iterator(Children.end())); | 
| 546 | return; | 
| 547 | } | 
| 548 | ScopeDIE = constructLexicalScopeDIE(Scope); | 
| 549 | assert(ScopeDIE && "Scope DIE should not be null."); | 
| 550 | } | 
| 551 |  | 
| 552 |  | 
| 553 | for (auto &I : Children) | 
| 554 | ScopeDIE->addChild(std::move(I)); | 
| 555 |  | 
| 556 | FinalChildren.push_back(std::move(ScopeDIE)); | 
| 557 | } | 
| 558 |  | 
| 559 | void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, | 
| 560 | SmallVector<RangeSpan, 2> Range) { | 
| 561 |  | 
| 562 | HasRangeLists = true; | 
| 563 |  | 
| 564 |  | 
| 565 | auto IndexAndList = | 
| 566 | (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU) | 
| 567 | ->addRange(*(Skeleton ? Skeleton : this), std::move(Range)); | 
| 568 |  | 
| 569 | uint32_t Index = IndexAndList.first; | 
| 570 | auto &List = *IndexAndList.second; | 
| 571 |  | 
| 572 |  | 
| 573 |  | 
| 574 |  | 
| 575 |  | 
| 576 |  | 
| 577 | if (DD->getDwarfVersion() >= 5) | 
| 578 | addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index); | 
| 579 | else { | 
| 580 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); | 
| 581 | const MCSymbol *RangeSectionSym = | 
| 582 | TLOF.getDwarfRangesSection()->getBeginSymbol(); | 
| 583 | if (isDwoUnit()) | 
| 584 | addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.Label, | 
| 585 | RangeSectionSym); | 
| 586 | else | 
| 587 | addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.Label, | 
| 588 | RangeSectionSym); | 
| 589 | } | 
| 590 | } | 
| 591 |  | 
| 592 | void DwarfCompileUnit::attachRangesOrLowHighPC( | 
| 593 | DIE &Die, SmallVector<RangeSpan, 2> Ranges) { | 
| 594 | assert(!Ranges.empty()); | 
| 595 | if (!DD->useRangesSection() || | 
| 596 | (Ranges.size() == 1 && | 
| 597 | (!DD->alwaysUseRanges() || | 
| 598 | DD->getSectionLabel(&Ranges.front().Begin->getSection()) == | 
| 599 | Ranges.front().Begin))) { | 
| 600 | const RangeSpan &Front = Ranges.front(); | 
| 601 | const RangeSpan &Back = Ranges.back(); | 
| 602 | attachLowHighPC(Die, Front.Begin, Back.End); | 
| 603 | } else | 
| 604 | addScopeRangeList(Die, std::move(Ranges)); | 
| 605 | } | 
| 606 |  | 
| 607 | void DwarfCompileUnit::attachRangesOrLowHighPC( | 
| 608 | DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { | 
| 609 | SmallVector<RangeSpan, 2> List; | 
| 610 | List.reserve(Ranges.size()); | 
| 611 | for (const InsnRange &R : Ranges) { | 
| 612 | auto *BeginLabel = DD->getLabelBeforeInsn(R.first); | 
| 613 | auto *EndLabel = DD->getLabelAfterInsn(R.second); | 
| 614 |  | 
| 615 | const auto *BeginMBB = R.first->getParent(); | 
| 616 | const auto *EndMBB = R.second->getParent(); | 
| 617 |  | 
| 618 | const auto *MBB = BeginMBB; | 
| 619 |  | 
| 620 |  | 
| 621 |  | 
| 622 |  | 
| 623 |  | 
| 624 |  | 
| 625 | do { | 
| 626 | if (MBB->sameSection(EndMBB) || MBB->isEndSection()) { | 
| 627 | auto MBBSectionRange = Asm->MBBSectionRanges[MBB->getSectionIDNum()]; | 
| 628 | List.push_back( | 
| 629 | {MBB->sameSection(BeginMBB) ? BeginLabel | 
| 630 | : MBBSectionRange.BeginLabel, | 
| 631 | MBB->sameSection(EndMBB) ? EndLabel : MBBSectionRange.EndLabel}); | 
| 632 | } | 
| 633 | if (MBB->sameSection(EndMBB)) | 
| 634 | break; | 
| 635 | MBB = MBB->getNextNode(); | 
| 636 | } while (true); | 
| 637 | } | 
| 638 | attachRangesOrLowHighPC(Die, std::move(List)); | 
| 639 | } | 
| 640 |  | 
| 641 |  | 
| 642 |  | 
| 643 | DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { | 
| 644 | assert(Scope->getScopeNode()); | 
| 645 | auto *DS = Scope->getScopeNode(); | 
| 646 | auto *InlinedSP = getDISubprogram(DS); | 
| 647 |  | 
| 648 |  | 
| 649 | DIE *OriginDIE = getAbstractSPDies()[InlinedSP]; | 
| 650 | assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); | 
| 651 |  | 
| 652 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); | 
| 653 | addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); | 
| 654 |  | 
| 655 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); | 
| 656 |  | 
| 657 |  | 
| 658 | const DILocation *IA = Scope->getInlinedAt(); | 
| 659 | addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, | 
| 660 | getOrCreateSourceID(IA->getFile())); | 
| 661 | addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); | 
| 662 | if (IA->getColumn()) | 
| 663 | addUInt(*ScopeDIE, dwarf::DW_AT_call_column, None, IA->getColumn()); | 
| 664 | if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4) | 
| 665 | addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, | 
| 666 | IA->getDiscriminator()); | 
| 667 |  | 
| 668 |  | 
| 669 |  | 
| 670 | DD->addSubprogramNames(*CUNode, InlinedSP, *ScopeDIE); | 
| 671 |  | 
| 672 | return ScopeDIE; | 
| 673 | } | 
| 674 |  | 
| 675 |  | 
| 676 |  | 
| 677 | DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { | 
| 678 | if (DD->isLexicalScopeDIENull(Scope)) | 
| 679 | return nullptr; | 
| 680 |  | 
| 681 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); | 
| 682 | if (Scope->isAbstractScope()) | 
| 683 | return ScopeDIE; | 
| 684 |  | 
| 685 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); | 
| 686 |  | 
| 687 | return ScopeDIE; | 
| 688 | } | 
| 689 |  | 
| 690 |  | 
| 691 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { | 
| 692 | auto D = constructVariableDIEImpl(DV, Abstract); | 
| 693 | DV.setDIE(*D); | 
| 694 | return D; | 
| 695 | } | 
| 696 |  | 
| 697 | DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL, | 
| 698 | const LexicalScope &Scope) { | 
| 699 | auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag()); | 
| 700 | insertDIE(DL.getLabel(), LabelDie); | 
| 701 | DL.setDIE(*LabelDie); | 
| 702 |  | 
| 703 | if (Scope.isAbstractScope()) | 
| 704 | applyLabelAttributes(DL, *LabelDie); | 
| 705 |  | 
| 706 | return LabelDie; | 
| 707 | } | 
| 708 |  | 
| 709 | DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, | 
| 710 | bool Abstract) { | 
| 711 |  | 
| 712 | auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); | 
| 713 | insertDIE(DV.getVariable(), VariableDie); | 
| 714 |  | 
| 715 | if (Abstract) { | 
| 716 | applyVariableAttributes(DV, *VariableDie); | 
| 717 | return VariableDie; | 
| 718 | } | 
| 719 |  | 
| 720 |  | 
| 721 |  | 
| 722 | unsigned Index = DV.getDebugLocListIndex(); | 
| 723 | if (Index != ~0U) { | 
| 724 | addLocationList(*VariableDie, dwarf::DW_AT_location, Index); | 
| 725 | auto TagOffset = DV.getDebugLocListTagOffset(); | 
| 726 | if (TagOffset) | 
| 727 | addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, | 
| 728 | *TagOffset); | 
| 729 | return VariableDie; | 
| 730 | } | 
| 731 |  | 
| 732 |  | 
| 733 | if (auto *DVal = DV.getValueLoc()) { | 
| 734 | if (!DVal->isVariadic()) { | 
| 735 | const DbgValueLocEntry *Entry = DVal->getLocEntries().begin(); | 
| 736 | if (Entry->isLocation()) { | 
| 737 | addVariableAddress(DV, *VariableDie, Entry->getLoc()); | 
| 738 | } else if (Entry->isInt()) { | 
| 739 | auto *Expr = DV.getSingleExpression(); | 
| 740 | if (Expr && Expr->getNumElements()) { | 
| 741 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 742 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 743 |  | 
| 744 | DwarfExpr.addFragmentOffset(Expr); | 
| 745 | DwarfExpr.addUnsignedConstant(Entry->getInt()); | 
| 746 | DwarfExpr.addExpression(Expr); | 
| 747 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); | 
| 748 | if (DwarfExpr.TagOffset) | 
| 749 | addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, | 
| 750 | dwarf::DW_FORM_data1, *DwarfExpr.TagOffset); | 
| 751 | } else | 
| 752 | addConstantValue(*VariableDie, Entry->getInt(), DV.getType()); | 
| 753 | } else if (Entry->isConstantFP()) { | 
| 754 | addConstantFPValue(*VariableDie, Entry->getConstantFP()); | 
| 755 | } else if (Entry->isConstantInt()) { | 
| 756 | addConstantValue(*VariableDie, Entry->getConstantInt(), DV.getType()); | 
| 757 | } else if (Entry->isTargetIndexLocation()) { | 
| 758 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 759 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 760 | const DIBasicType *BT = dyn_cast<DIBasicType>( | 
| 761 | static_cast<const Metadata *>(DV.getVariable()->getType())); | 
| 762 | DwarfDebug::emitDebugLocValue(*Asm, BT, *DVal, DwarfExpr); | 
| 763 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); | 
| 764 | } | 
| 765 | return VariableDie; | 
| 766 | } | 
| 767 |  | 
| 768 |  | 
| 769 | if (any_of(DVal->getLocEntries(), [](const DbgValueLocEntry &Entry) { | 
| 770 | return Entry.isLocation() && !Entry.getLoc().getReg(); | 
| 771 | })) | 
| 772 | return VariableDie; | 
| 773 | const DIExpression *Expr = DV.getSingleExpression(); | 
| 774 | assert(Expr && "Variadic Debug Value must have an Expression."); | 
| 775 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 776 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 777 | DwarfExpr.addFragmentOffset(Expr); | 
| 778 | DIExpressionCursor Cursor(Expr); | 
| 779 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); | 
| 780 |  | 
| 781 | auto AddEntry = [&](const DbgValueLocEntry &Entry, | 
| 782 | DIExpressionCursor &Cursor) { | 
| 783 | if (Entry.isLocation()) { | 
| 784 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, | 
| 785 | Entry.getLoc().getReg())) | 
| 786 | return false; | 
| 787 | } else if (Entry.isInt()) { | 
| 788 |  | 
| 789 | DwarfExpr.addUnsignedConstant(Entry.getInt()); | 
| 790 | } else if (Entry.isConstantFP()) { | 
| 791 | APInt RawBytes = Entry.getConstantFP()->getValueAPF().bitcastToAPInt(); | 
| 792 | DwarfExpr.addUnsignedConstant(RawBytes); | 
| 793 | } else if (Entry.isConstantInt()) { | 
| 794 | APInt RawBytes = Entry.getConstantInt()->getValue(); | 
| 795 | DwarfExpr.addUnsignedConstant(RawBytes); | 
| 796 | } else if (Entry.isTargetIndexLocation()) { | 
| 797 | TargetIndexLocation Loc = Entry.getTargetIndexLocation(); | 
| 798 |  | 
| 799 |  | 
| 800 | assert(Asm->TM.getTargetTriple().isWasm()); | 
| 801 | DwarfExpr.addWasmLocation(Loc.Index, static_cast<uint64_t>(Loc.Offset)); | 
| 802 | } else { | 
| 803 | llvm_unreachable("Unsupported Entry type."); | 
| 804 | } | 
| 805 | return true; | 
| 806 | }; | 
| 807 |  | 
| 808 | DwarfExpr.addExpression( | 
| 809 | std::move(Cursor), | 
| 810 | [&](unsigned Idx, DIExpressionCursor &Cursor) -> bool { | 
| 811 | return AddEntry(DVal->getLocEntries()[Idx], Cursor); | 
| 812 | }); | 
| 813 |  | 
| 814 |  | 
| 815 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); | 
| 816 | if (DwarfExpr.TagOffset) | 
| 817 | addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, | 
| 818 | *DwarfExpr.TagOffset); | 
| 819 |  | 
| 820 | return VariableDie; | 
| 821 | } | 
| 822 |  | 
| 823 |  | 
| 824 | if (!DV.hasFrameIndexExprs()) | 
| 825 | return VariableDie; | 
| 826 |  | 
| 827 | Optional<unsigned> NVPTXAddressSpace; | 
| 828 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 829 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 830 | for (auto &Fragment : DV.getFrameIndexExprs()) { | 
| 831 | Register FrameReg; | 
| 832 | const DIExpression *Expr = Fragment.Expr; | 
| 833 | const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); | 
| 834 | StackOffset Offset = | 
| 835 | TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg); | 
| 836 | DwarfExpr.addFragmentOffset(Expr); | 
| 837 |  | 
| 838 | auto *TRI = Asm->MF->getSubtarget().getRegisterInfo(); | 
| 839 | SmallVector<uint64_t, 8> Ops; | 
| 840 | TRI->getOffsetOpcodes(Offset, Ops); | 
| 841 |  | 
| 842 |  | 
| 843 |  | 
| 844 |  | 
| 845 |  | 
| 846 |  | 
| 847 |  | 
| 848 | unsigned LocalNVPTXAddressSpace; | 
| 849 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { | 
| 850 | const DIExpression *NewExpr = | 
| 851 | DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); | 
| 852 | if (NewExpr != Expr) { | 
| 853 | Expr = NewExpr; | 
| 854 | NVPTXAddressSpace = LocalNVPTXAddressSpace; | 
| 855 | } | 
| 856 | } | 
| 857 | if (Expr) | 
| 858 | Ops.append(Expr->elements_begin(), Expr->elements_end()); | 
| 859 | DIExpressionCursor Cursor(Ops); | 
| 860 | DwarfExpr.setMemoryLocationKind(); | 
| 861 | if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol()) | 
| 862 | addOpAddress(*Loc, FrameSymbol); | 
| 863 | else | 
| 864 | DwarfExpr.addMachineRegExpression( | 
| 865 | *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg); | 
| 866 | DwarfExpr.addExpression(std::move(Cursor)); | 
| 867 | } | 
| 868 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { | 
| 869 |  | 
| 870 |  | 
| 871 |  | 
| 872 |  | 
| 873 | const unsigned NVPTX_ADDR_local_space = 6; | 
| 874 | addUInt(*VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, | 
| 875 | NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_local_space); | 
| 876 | } | 
| 877 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); | 
| 878 | if (DwarfExpr.TagOffset) | 
| 879 | addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, | 
| 880 | *DwarfExpr.TagOffset); | 
| 881 |  | 
| 882 | return VariableDie; | 
| 883 | } | 
| 884 |  | 
| 885 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, | 
| 886 | const LexicalScope &Scope, | 
| 887 | DIE *&ObjectPointer) { | 
| 888 | auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); | 
| 889 | if (DV.isObjectPointer()) | 
| 890 | ObjectPointer = Var; | 
| 891 | return Var; | 
| 892 | } | 
| 893 |  | 
| 894 |  | 
| 895 | static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) { | 
| 896 | SmallVector<const DIVariable *, 2> Result; | 
| 897 | auto *Array = dyn_cast<DICompositeType>(Var->getType()); | 
| 898 | if (!Array || Array->getTag() != dwarf::DW_TAG_array_type) | 
| 899 | return Result; | 
| 900 | if (auto *DLVar = Array->getDataLocation()) | 
| 901 | Result.push_back(DLVar); | 
| 902 | if (auto *AsVar = Array->getAssociated()) | 
| 903 | Result.push_back(AsVar); | 
| 904 | if (auto *AlVar = Array->getAllocated()) | 
| 905 | Result.push_back(AlVar); | 
| 906 | for (auto *El : Array->getElements()) { | 
| 907 | if (auto *Subrange = dyn_cast<DISubrange>(El)) { | 
| 908 | if (auto Count = Subrange->getCount()) | 
| 909 | if (auto *Dependency = Count.dyn_cast<DIVariable *>()) | 
| 910 | Result.push_back(Dependency); | 
| 911 | if (auto LB = Subrange->getLowerBound()) | 
| 912 | if (auto *Dependency = LB.dyn_cast<DIVariable *>()) | 
| 913 | Result.push_back(Dependency); | 
| 914 | if (auto UB = Subrange->getUpperBound()) | 
| 915 | if (auto *Dependency = UB.dyn_cast<DIVariable *>()) | 
| 916 | Result.push_back(Dependency); | 
| 917 | if (auto ST = Subrange->getStride()) | 
| 918 | if (auto *Dependency = ST.dyn_cast<DIVariable *>()) | 
| 919 | Result.push_back(Dependency); | 
| 920 | } else if (auto *GenericSubrange = dyn_cast<DIGenericSubrange>(El)) { | 
| 921 | if (auto Count = GenericSubrange->getCount()) | 
| 922 | if (auto *Dependency = Count.dyn_cast<DIVariable *>()) | 
| 923 | Result.push_back(Dependency); | 
| 924 | if (auto LB = GenericSubrange->getLowerBound()) | 
| 925 | if (auto *Dependency = LB.dyn_cast<DIVariable *>()) | 
| 926 | Result.push_back(Dependency); | 
| 927 | if (auto UB = GenericSubrange->getUpperBound()) | 
| 928 | if (auto *Dependency = UB.dyn_cast<DIVariable *>()) | 
| 929 | Result.push_back(Dependency); | 
| 930 | if (auto ST = GenericSubrange->getStride()) | 
| 931 | if (auto *Dependency = ST.dyn_cast<DIVariable *>()) | 
| 932 | Result.push_back(Dependency); | 
| 933 | } | 
| 934 | } | 
| 935 | return Result; | 
| 936 | } | 
| 937 |  | 
| 938 |  | 
| 939 |  | 
| 940 | static SmallVector<DbgVariable *, 8> | 
| 941 | sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) { | 
| 942 | SmallVector<DbgVariable *, 8> Result; | 
| 943 | SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList; | 
| 944 |  | 
| 945 | SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar; | 
| 946 |  | 
| 947 | SmallDenseSet<DbgVariable *, 8> Visited; | 
| 948 |  | 
| 949 | SmallDenseSet<DbgVariable *, 8> Visiting; | 
| 950 |  | 
| 951 |  | 
| 952 | for (auto Var : reverse(Input)) { | 
| 953 | DbgVar.insert({Var->getVariable(), Var}); | 
| 954 | WorkList.push_back({Var, 0}); | 
| 955 | } | 
| 956 |  | 
| 957 |  | 
| 958 | while (!WorkList.empty()) { | 
| 959 | auto Item = WorkList.back(); | 
| 960 | DbgVariable *Var = Item.getPointer(); | 
| 961 | bool visitedAllDependencies = Item.getInt(); | 
| 962 | WorkList.pop_back(); | 
| 963 |  | 
| 964 |  | 
| 965 | if (!Var) | 
| 966 | continue; | 
| 967 |  | 
| 968 |  | 
| 969 | if (Visited.count(Var)) | 
| 970 | continue; | 
| 971 |  | 
| 972 |  | 
| 973 | if (visitedAllDependencies) { | 
| 974 | Visited.insert(Var); | 
| 975 | Result.push_back(Var); | 
| 976 | continue; | 
| 977 | } | 
| 978 |  | 
| 979 |  | 
| 980 | auto Res = Visiting.insert(Var); | 
| 981 | if (!Res.second) { | 
| 982 | assert(false && "dependency cycle in local variables"); | 
| 983 | return Result; | 
| 984 | } | 
| 985 |  | 
| 986 |  | 
| 987 |  | 
| 988 | WorkList.push_back({Var, 1}); | 
| 989 | for (auto *Dependency : dependencies(Var)) { | 
| 990 | auto Dep = dyn_cast_or_null<const DILocalVariable>(Dependency); | 
| 991 | WorkList.push_back({DbgVar[Dep], 0}); | 
| 992 | } | 
| 993 | } | 
| 994 | return Result; | 
| 995 | } | 
| 996 |  | 
| 997 | DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, | 
| 998 | SmallVectorImpl<DIE *> &Children, | 
| 999 | bool *HasNonScopeChildren) { | 
| 1000 | assert(Children.empty()); | 
| 1001 | DIE *ObjectPointer = nullptr; | 
| 1002 |  | 
| 1003 |  | 
| 1004 | auto Vars = DU->getScopeVariables().lookup(Scope); | 
| 1005 | for (auto &DV : Vars.Args) | 
| 1006 | Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer)); | 
| 1007 |  | 
| 1008 |  | 
| 1009 | auto Locals = sortLocalVars(Vars.Locals); | 
| 1010 | for (DbgVariable *DV : Locals) | 
| 1011 | Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); | 
| 1012 |  | 
| 1013 |  | 
| 1014 | if (!includeMinimalInlineScopes()) { | 
| 1015 |  | 
| 1016 | for (const auto *IE : ImportedEntities[Scope->getScopeNode()]) | 
| 1017 | Children.push_back( | 
| 1018 | constructImportedEntityDIE(cast<DIImportedEntity>(IE))); | 
| 1019 | } | 
| 1020 |  | 
| 1021 | if (HasNonScopeChildren) | 
| 1022 | *HasNonScopeChildren = !Children.empty(); | 
| 1023 |  | 
| 1024 | for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope)) | 
| 1025 | Children.push_back(constructLabelDIE(*DL, *Scope)); | 
| 1026 |  | 
| 1027 | for (LexicalScope *LS : Scope->getChildren()) | 
| 1028 | constructScopeDIE(LS, Children); | 
| 1029 |  | 
| 1030 | return ObjectPointer; | 
| 1031 | } | 
| 1032 |  | 
| 1033 | DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub, | 
| 1034 | LexicalScope *Scope) { | 
| 1035 | DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); | 
| 1036 |  | 
| 1037 | if (Scope) { | 
| 1038 | assert(!Scope->getInlinedAt()); | 
| 1039 | assert(!Scope->isAbstractScope()); | 
| 1040 |  | 
| 1041 |  | 
| 1042 |  | 
| 1043 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) | 
| 1044 | addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); | 
| 1045 | } | 
| 1046 |  | 
| 1047 |  | 
| 1048 | DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); | 
| 1049 |  | 
| 1050 |  | 
| 1051 |  | 
| 1052 |  | 
| 1053 | if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && | 
| 1054 | !includeMinimalInlineScopes()) | 
| 1055 | ScopeDIE.addChild( | 
| 1056 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); | 
| 1057 |  | 
| 1058 | return ScopeDIE; | 
| 1059 | } | 
| 1060 |  | 
| 1061 | DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, | 
| 1062 | DIE &ScopeDIE) { | 
| 1063 |  | 
| 1064 | SmallVector<DIE *, 8> Children; | 
| 1065 | DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); | 
| 1066 |  | 
| 1067 |  | 
| 1068 | for (auto &I : Children) | 
| 1069 | ScopeDIE.addChild(std::move(I)); | 
| 1070 |  | 
| 1071 | return ObjectPointer; | 
| 1072 | } | 
| 1073 |  | 
| 1074 | void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( | 
| 1075 | LexicalScope *Scope) { | 
| 1076 | DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()]; | 
| 1077 | if (AbsDef) | 
| 1078 | return; | 
| 1079 |  | 
| 1080 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); | 
| 1081 |  | 
| 1082 | DIE *ContextDIE; | 
| 1083 | DwarfCompileUnit *ContextCU = this; | 
| 1084 |  | 
| 1085 | if (includeMinimalInlineScopes()) | 
| 1086 | ContextDIE = &getUnitDie(); | 
| 1087 |  | 
| 1088 |  | 
| 1089 |  | 
| 1090 |  | 
| 1091 | else if (auto *SPDecl = SP->getDeclaration()) { | 
| 1092 | ContextDIE = &getUnitDie(); | 
| 1093 | getOrCreateSubprogramDIE(SPDecl); | 
| 1094 | } else { | 
| 1095 | ContextDIE = getOrCreateContextDIE(SP->getScope()); | 
| 1096 |  | 
| 1097 |  | 
| 1098 |  | 
| 1099 | ContextCU = DD->lookupCU(ContextDIE->getUnitDie()); | 
| 1100 | } | 
| 1101 |  | 
| 1102 |  | 
| 1103 |  | 
| 1104 | AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); | 
| 1105 | ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef); | 
| 1106 |  | 
| 1107 | if (!ContextCU->includeMinimalInlineScopes()) | 
| 1108 | ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); | 
| 1109 | if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef)) | 
| 1110 | ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); | 
| 1111 | } | 
| 1112 |  | 
| 1113 | bool DwarfCompileUnit::useGNUAnalogForDwarf5Feature() const { | 
| 1114 | return DD->getDwarfVersion() == 4 && !DD->tuneForLLDB(); | 
| 1115 | } | 
| 1116 |  | 
| 1117 | dwarf::Tag DwarfCompileUnit::getDwarf5OrGNUTag(dwarf::Tag Tag) const { | 
| 1118 | if (!useGNUAnalogForDwarf5Feature()) | 
| 1119 | return Tag; | 
| 1120 | switch (Tag) { | 
| 1121 | case dwarf::DW_TAG_call_site: | 
| 1122 | return dwarf::DW_TAG_GNU_call_site; | 
| 1123 | case dwarf::DW_TAG_call_site_parameter: | 
| 1124 | return dwarf::DW_TAG_GNU_call_site_parameter; | 
| 1125 | default: | 
| 1126 | llvm_unreachable("DWARF5 tag with no GNU analog"); | 
| 1127 | } | 
| 1128 | } | 
| 1129 |  | 
| 1130 | dwarf::Attribute | 
| 1131 | DwarfCompileUnit::getDwarf5OrGNUAttr(dwarf::Attribute Attr) const { | 
| 1132 | if (!useGNUAnalogForDwarf5Feature()) | 
| 1133 | return Attr; | 
| 1134 | switch (Attr) { | 
| 1135 | case dwarf::DW_AT_call_all_calls: | 
| 1136 | return dwarf::DW_AT_GNU_all_call_sites; | 
| 1137 | case dwarf::DW_AT_call_target: | 
| 1138 | return dwarf::DW_AT_GNU_call_site_target; | 
| 1139 | case dwarf::DW_AT_call_origin: | 
| 1140 | return dwarf::DW_AT_abstract_origin; | 
| 1141 | case dwarf::DW_AT_call_return_pc: | 
| 1142 | return dwarf::DW_AT_low_pc; | 
| 1143 | case dwarf::DW_AT_call_value: | 
| 1144 | return dwarf::DW_AT_GNU_call_site_value; | 
| 1145 | case dwarf::DW_AT_call_tail_call: | 
| 1146 | return dwarf::DW_AT_GNU_tail_call; | 
| 1147 | default: | 
| 1148 | llvm_unreachable("DWARF5 attribute with no GNU analog"); | 
| 1149 | } | 
| 1150 | } | 
| 1151 |  | 
| 1152 | dwarf::LocationAtom | 
| 1153 | DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const { | 
| 1154 | if (!useGNUAnalogForDwarf5Feature()) | 
| 1155 | return Loc; | 
| 1156 | switch (Loc) { | 
| 1157 | case dwarf::DW_OP_entry_value: | 
| 1158 | return dwarf::DW_OP_GNU_entry_value; | 
| 1159 | default: | 
| 1160 | llvm_unreachable("DWARF5 location atom with no GNU analog"); | 
| 1161 | } | 
| 1162 | } | 
| 1163 |  | 
| 1164 | DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE, | 
| 1165 | const DISubprogram *CalleeSP, | 
| 1166 | bool IsTail, | 
| 1167 | const MCSymbol *PCAddr, | 
| 1168 | const MCSymbol *CallAddr, | 
| 1169 | unsigned CallReg) { | 
| 1170 |  | 
| 1171 | DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site), | 
| 1172 | ScopeDIE, nullptr); | 
| 1173 |  | 
| 1174 | if (CallReg) { | 
|  |  | 
|  |  | 
| 1175 |  | 
| 1176 | addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target), | 
| 1177 | MachineLocation(CallReg)); | 
| 1178 | } else { | 
| 1179 | DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP); | 
| 1180 | assert(CalleeDIE && "Could not create DIE for call site entry origin"); | 
| 1181 | addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin), | 
| 1182 | *CalleeDIE); | 
| 1183 | } | 
| 1184 |  | 
| 1185 | if (IsTail) { | 
|  | | 3 |  | Assuming 'IsTail' is false |  | 
 | 
|  |  | 
| 1186 |  | 
| 1187 | addFlag(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_tail_call)); | 
| 1188 |  | 
| 1189 |  | 
| 1190 |  | 
| 1191 |  | 
| 1192 |  | 
| 1193 |  | 
| 1194 |  | 
| 1195 |  | 
| 1196 |  | 
| 1197 |  | 
| 1198 |  | 
| 1199 |  | 
| 1200 |  | 
| 1201 | if (!useGNUAnalogForDwarf5Feature()) | 
| 1202 | addLabelAddress(CallSiteDIE, dwarf::DW_AT_call_pc, CallAddr); | 
| 1203 | } | 
| 1204 |  | 
| 1205 |  | 
| 1206 |  | 
| 1207 |  | 
| 1208 |  | 
| 1209 |  | 
| 1210 |  | 
| 1211 | if (!IsTail || useGNUAnalogForDwarf5Feature()) { | 
| 1212 | assert(PCAddr && "Missing return PC information for a call"); | 
| 1213 | addLabelAddress(CallSiteDIE, | 
|  | | 6 |  | Calling 'DwarfCompileUnit::addLabelAddress' |  | 
 | 
| 1214 | getDwarf5OrGNUAttr(dwarf::DW_AT_call_return_pc), PCAddr); | 
|  | | 5 |  | Passing value via 3rd parameter 'Label' |  | 
 | 
| 1215 | } | 
| 1216 |  | 
| 1217 | return CallSiteDIE; | 
| 1218 | } | 
| 1219 |  | 
| 1220 | void DwarfCompileUnit::constructCallSiteParmEntryDIEs( | 
| 1221 | DIE &CallSiteDIE, SmallVector<DbgCallSiteParam, 4> &Params) { | 
| 1222 | for (const auto &Param : Params) { | 
| 1223 | unsigned Register = Param.getRegister(); | 
| 1224 | auto CallSiteDieParam = | 
| 1225 | DIE::get(DIEValueAllocator, | 
| 1226 | getDwarf5OrGNUTag(dwarf::DW_TAG_call_site_parameter)); | 
| 1227 | insertDIE(CallSiteDieParam); | 
| 1228 | addAddress(*CallSiteDieParam, dwarf::DW_AT_location, | 
| 1229 | MachineLocation(Register)); | 
| 1230 |  | 
| 1231 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 1232 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 1233 | DwarfExpr.setCallSiteParamValueFlag(); | 
| 1234 |  | 
| 1235 | DwarfDebug::emitDebugLocValue(*Asm, nullptr, Param.getValue(), DwarfExpr); | 
| 1236 |  | 
| 1237 | addBlock(*CallSiteDieParam, getDwarf5OrGNUAttr(dwarf::DW_AT_call_value), | 
| 1238 | DwarfExpr.finalize()); | 
| 1239 |  | 
| 1240 | CallSiteDIE.addChild(CallSiteDieParam); | 
| 1241 | } | 
| 1242 | } | 
| 1243 |  | 
| 1244 | DIE *DwarfCompileUnit::constructImportedEntityDIE( | 
| 1245 | const DIImportedEntity *Module) { | 
| 1246 | DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); | 
| 1247 | insertDIE(Module, IMDie); | 
| 1248 | DIE *EntityDie; | 
| 1249 | auto *Entity = Module->getEntity(); | 
| 1250 | if (auto *NS = dyn_cast<DINamespace>(Entity)) | 
| 1251 | EntityDie = getOrCreateNameSpace(NS); | 
| 1252 | else if (auto *M = dyn_cast<DIModule>(Entity)) | 
| 1253 | EntityDie = getOrCreateModule(M); | 
| 1254 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) | 
| 1255 | EntityDie = getOrCreateSubprogramDIE(SP); | 
| 1256 | else if (auto *T = dyn_cast<DIType>(Entity)) | 
| 1257 | EntityDie = getOrCreateTypeDIE(T); | 
| 1258 | else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) | 
| 1259 | EntityDie = getOrCreateGlobalVariableDIE(GV, {}); | 
| 1260 | else | 
| 1261 | EntityDie = getDIE(Entity); | 
| 1262 | assert(EntityDie); | 
| 1263 | addSourceLine(*IMDie, Module->getLine(), Module->getFile()); | 
| 1264 | addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); | 
| 1265 | StringRef Name = Module->getName(); | 
| 1266 | if (!Name.empty()) | 
| 1267 | addString(*IMDie, dwarf::DW_AT_name, Name); | 
| 1268 |  | 
| 1269 | return IMDie; | 
| 1270 | } | 
| 1271 |  | 
| 1272 | void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { | 
| 1273 | DIE *D = getDIE(SP); | 
| 1274 | if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) { | 
| 1275 | if (D) | 
| 1276 |  | 
| 1277 | addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); | 
| 1278 | } else { | 
| 1279 | assert(D || includeMinimalInlineScopes()); | 
| 1280 | if (D) | 
| 1281 |  | 
| 1282 | applySubprogramAttributesToDefinition(SP, *D); | 
| 1283 | } | 
| 1284 | } | 
| 1285 |  | 
| 1286 | void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) { | 
| 1287 | DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity()); | 
| 1288 |  | 
| 1289 | auto *Die = Entity->getDIE(); | 
| 1290 |  | 
| 1291 |  | 
| 1292 | const DbgLabel *Label = nullptr; | 
| 1293 | if (AbsEntity && AbsEntity->getDIE()) { | 
| 1294 | addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE()); | 
| 1295 | Label = dyn_cast<const DbgLabel>(Entity); | 
| 1296 | } else { | 
| 1297 | if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity)) | 
| 1298 | applyVariableAttributes(*Var, *Die); | 
| 1299 | else if ((Label = dyn_cast<const DbgLabel>(Entity))) | 
| 1300 | applyLabelAttributes(*Label, *Die); | 
| 1301 | else | 
| 1302 | llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel."); | 
| 1303 | } | 
| 1304 |  | 
| 1305 | if (Label) | 
| 1306 | if (const auto *Sym = Label->getSymbol()) | 
| 1307 | addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym); | 
| 1308 | } | 
| 1309 |  | 
| 1310 | DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) { | 
| 1311 | auto &AbstractEntities = getAbstractEntities(); | 
| 1312 | auto I = AbstractEntities.find(Node); | 
| 1313 | if (I != AbstractEntities.end()) | 
| 1314 | return I->second.get(); | 
| 1315 | return nullptr; | 
| 1316 | } | 
| 1317 |  | 
| 1318 | void DwarfCompileUnit::createAbstractEntity(const DINode *Node, | 
| 1319 | LexicalScope *Scope) { | 
| 1320 | assert(Scope && Scope->isAbstractScope()); | 
| 1321 | auto &Entity = getAbstractEntities()[Node]; | 
| 1322 | if (isa<const DILocalVariable>(Node)) { | 
| 1323 | Entity = std::make_unique<DbgVariable>( | 
| 1324 | cast<const DILocalVariable>(Node), nullptr );; | 
| 1325 | DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get())); | 
| 1326 | } else if (isa<const DILabel>(Node)) { | 
| 1327 | Entity = std::make_unique<DbgLabel>( | 
| 1328 | cast<const DILabel>(Node), nullptr ); | 
| 1329 | DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get())); | 
| 1330 | } | 
| 1331 | } | 
| 1332 |  | 
| 1333 | void DwarfCompileUnit::emitHeader(bool UseOffsets) { | 
| 1334 |  | 
| 1335 | if (!Skeleton && !DD->useSectionsAsReferences()) { | 
| 1336 | LabelBegin = Asm->createTempSymbol("cu_begin"); | 
| 1337 | Asm->OutStreamer->emitLabel(LabelBegin); | 
| 1338 | } | 
| 1339 |  | 
| 1340 | dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile | 
| 1341 | : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton | 
| 1342 | : dwarf::DW_UT_compile; | 
| 1343 | DwarfUnit::emitCommonHeader(UseOffsets, UT); | 
| 1344 | if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile) | 
| 1345 | Asm->emitInt64(getDWOId()); | 
| 1346 | } | 
| 1347 |  | 
| 1348 | bool DwarfCompileUnit::hasDwarfPubSections() const { | 
| 1349 | switch (CUNode->getNameTableKind()) { | 
| 1350 | case DICompileUnit::DebugNameTableKind::None: | 
| 1351 | return false; | 
| 1352 |  | 
| 1353 |  | 
| 1354 | case DICompileUnit::DebugNameTableKind::GNU: | 
| 1355 | return true; | 
| 1356 | case DICompileUnit::DebugNameTableKind::Default: | 
| 1357 | return DD->tuneForGDB() && !includeMinimalInlineScopes() && | 
| 1358 | !CUNode->isDebugDirectivesOnly() && | 
| 1359 | DD->getAccelTableKind() != AccelTableKind::Apple && | 
| 1360 | DD->getDwarfVersion() < 5; | 
| 1361 | } | 
| 1362 | llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum"); | 
| 1363 | } | 
| 1364 |  | 
| 1365 |  | 
| 1366 | void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die, | 
| 1367 | const DIScope *Context) { | 
| 1368 | if (!hasDwarfPubSections()) | 
| 1369 | return; | 
| 1370 | std::string FullName = getParentContextString(Context) + Name.str(); | 
| 1371 | GlobalNames[FullName] = &Die; | 
| 1372 | } | 
| 1373 |  | 
| 1374 | void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name, | 
| 1375 | const DIScope *Context) { | 
| 1376 | if (!hasDwarfPubSections()) | 
| 1377 | return; | 
| 1378 | std::string FullName = getParentContextString(Context) + Name.str(); | 
| 1379 |  | 
| 1380 |  | 
| 1381 |  | 
| 1382 |  | 
| 1383 | GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie())); | 
| 1384 | } | 
| 1385 |  | 
| 1386 |  | 
| 1387 | void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, | 
| 1388 | const DIScope *Context) { | 
| 1389 | if (!hasDwarfPubSections()) | 
| 1390 | return; | 
| 1391 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); | 
| 1392 | GlobalTypes[FullName] = &Die; | 
| 1393 | } | 
| 1394 |  | 
| 1395 | void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty, | 
| 1396 | const DIScope *Context) { | 
| 1397 | if (!hasDwarfPubSections()) | 
| 1398 | return; | 
| 1399 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); | 
| 1400 |  | 
| 1401 |  | 
| 1402 |  | 
| 1403 |  | 
| 1404 | GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie())); | 
| 1405 | } | 
| 1406 |  | 
| 1407 | void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, | 
| 1408 | MachineLocation Location) { | 
| 1409 | if (DV.hasComplexAddress()) | 
| 1410 | addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); | 
| 1411 | else | 
| 1412 | addAddress(Die, dwarf::DW_AT_location, Location); | 
| 1413 | } | 
| 1414 |  | 
| 1415 |  | 
| 1416 | void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, | 
| 1417 | const MachineLocation &Location) { | 
| 1418 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 1419 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 1420 | if (Location.isIndirect()) | 
| 1421 | DwarfExpr.setMemoryLocationKind(); | 
| 1422 |  | 
| 1423 | DIExpressionCursor Cursor({}); | 
| 1424 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); | 
| 1425 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) | 
| 1426 | return; | 
| 1427 | DwarfExpr.addExpression(std::move(Cursor)); | 
| 1428 |  | 
| 1429 |  | 
| 1430 | addBlock(Die, Attribute, DwarfExpr.finalize()); | 
| 1431 |  | 
| 1432 | if (DwarfExpr.TagOffset) | 
| 1433 | addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, | 
| 1434 | *DwarfExpr.TagOffset); | 
| 1435 | } | 
| 1436 |  | 
| 1437 |  | 
| 1438 |  | 
| 1439 |  | 
| 1440 |  | 
| 1441 | void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, | 
| 1442 | dwarf::Attribute Attribute, | 
| 1443 | const MachineLocation &Location) { | 
| 1444 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; | 
| 1445 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); | 
| 1446 | const DIExpression *DIExpr = DV.getSingleExpression(); | 
| 1447 | DwarfExpr.addFragmentOffset(DIExpr); | 
| 1448 | DwarfExpr.setLocation(Location, DIExpr); | 
| 1449 |  | 
| 1450 | DIExpressionCursor Cursor(DIExpr); | 
| 1451 |  | 
| 1452 | if (DIExpr->isEntryValue()) | 
| 1453 | DwarfExpr.beginEntryValueExpression(Cursor); | 
| 1454 |  | 
| 1455 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); | 
| 1456 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) | 
| 1457 | return; | 
| 1458 | DwarfExpr.addExpression(std::move(Cursor)); | 
| 1459 |  | 
| 1460 |  | 
| 1461 | addBlock(Die, Attribute, DwarfExpr.finalize()); | 
| 1462 |  | 
| 1463 | if (DwarfExpr.TagOffset) | 
| 1464 | addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, | 
| 1465 | *DwarfExpr.TagOffset); | 
| 1466 | } | 
| 1467 |  | 
| 1468 |  | 
| 1469 | void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, | 
| 1470 | unsigned Index) { | 
| 1471 | dwarf::Form Form = (DD->getDwarfVersion() >= 5) | 
| 1472 | ? dwarf::DW_FORM_loclistx | 
| 1473 | : DD->getDwarfSectionOffsetForm(); | 
| 1474 | addAttribute(Die, Attribute, Form, DIELocList(Index)); | 
| 1475 | } | 
| 1476 |  | 
| 1477 | void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, | 
| 1478 | DIE &VariableDie) { | 
| 1479 | StringRef Name = Var.getName(); | 
| 1480 | if (!Name.empty()) | 
| 1481 | addString(VariableDie, dwarf::DW_AT_name, Name); | 
| 1482 | const auto *DIVar = Var.getVariable(); | 
| 1483 | if (DIVar) | 
| 1484 | if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) | 
| 1485 | addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, | 
| 1486 | AlignInBytes); | 
| 1487 |  | 
| 1488 | addSourceLine(VariableDie, DIVar); | 
| 1489 | addType(VariableDie, Var.getType()); | 
| 1490 | if (Var.isArtificial()) | 
| 1491 | addFlag(VariableDie, dwarf::DW_AT_artificial); | 
| 1492 | } | 
| 1493 |  | 
| 1494 | void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label, | 
| 1495 | DIE &LabelDie) { | 
| 1496 | StringRef Name = Label.getName(); | 
| 1497 | if (!Name.empty()) | 
| 1498 | addString(LabelDie, dwarf::DW_AT_name, Name); | 
| 1499 | const auto *DILabel = Label.getLabel(); | 
| 1500 | addSourceLine(LabelDie, DILabel); | 
| 1501 | } | 
| 1502 |  | 
| 1503 |  | 
| 1504 | void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, | 
| 1505 | const MCExpr *Expr) { | 
| 1506 | addAttribute(Die, (dwarf::Attribute)0, Form, DIEExpr(Expr)); | 
| 1507 | } | 
| 1508 |  | 
| 1509 | void DwarfCompileUnit::applySubprogramAttributesToDefinition( | 
| 1510 | const DISubprogram *SP, DIE &SPDie) { | 
| 1511 | auto *SPDecl = SP->getDeclaration(); | 
| 1512 | auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope(); | 
| 1513 | applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); | 
| 1514 | addGlobalName(SP->getName(), SPDie, Context); | 
| 1515 | } | 
| 1516 |  | 
| 1517 | bool DwarfCompileUnit::isDwoUnit() const { | 
| 1518 | return DD->useSplitDwarf() && Skeleton; | 
| 1519 | } | 
| 1520 |  | 
| 1521 | void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { | 
| 1522 | constructTypeDIE(D, CTy); | 
| 1523 | } | 
| 1524 |  | 
| 1525 | bool DwarfCompileUnit::includeMinimalInlineScopes() const { | 
| 1526 | return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly || | 
| 1527 | (DD->useSplitDwarf() && !Skeleton); | 
| 1528 | } | 
| 1529 |  | 
| 1530 | void DwarfCompileUnit::addAddrTableBase() { | 
| 1531 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); | 
| 1532 | MCSymbol *Label = DD->getAddressPool().getLabel(); | 
| 1533 | addSectionLabel(getUnitDie(), | 
| 1534 | DD->getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base | 
| 1535 | : dwarf::DW_AT_GNU_addr_base, | 
| 1536 | Label, TLOF.getDwarfAddrSection()->getBeginSymbol()); | 
| 1537 | } | 
| 1538 |  | 
| 1539 | void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) { | 
| 1540 | addAttribute(Die, (dwarf::Attribute)0, dwarf::DW_FORM_udata, | 
| 1541 | new (DIEValueAllocator) DIEBaseTypeRef(this, Idx)); | 
| 1542 | } | 
| 1543 |  | 
| 1544 | void DwarfCompileUnit::createBaseTypeDIEs() { | 
| 1545 |  | 
| 1546 |  | 
| 1547 |  | 
| 1548 |  | 
| 1549 | for (auto &Btr : reverse(ExprRefedBaseTypes)) { | 
| 1550 | DIE &Die = getUnitDie().addChildFront( | 
| 1551 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type)); | 
| 1552 | SmallString<32> Str; | 
| 1553 | addString(Die, dwarf::DW_AT_name, | 
| 1554 | Twine(dwarf::AttributeEncodingString(Btr.Encoding) + | 
| 1555 | "_" + Twine(Btr.BitSize)).toStringRef(Str)); | 
| 1556 | addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding); | 
| 1557 | addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8); | 
| 1558 |  | 
| 1559 | Btr.Die = &Die; | 
| 1560 | } | 
| 1561 | } |