1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===// 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 defines a bunch of datatypes that are useful for creating and 10 // walking debug info in LLVM IR form. They essentially provide wrappers around 11 // the information in the global variables that's needed when constructing the 12 // DWARF information. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_IR_DEBUGINFO_H 17 #define LLVM_IR_DEBUGINFO_H 18 19 #include "llvm/ADT/DenseMapInfo.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/SetVector.h" 22 #include "llvm/ADT/SmallPtrSet.h" 23 #include "llvm/ADT/SmallSet.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/ADT/TinyPtrVector.h" 26 #include "llvm/ADT/iterator_range.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/IntrinsicInst.h" 29 #include "llvm/IR/PassManager.h" 30 #include <optional> 31 32 namespace llvm { 33 34 class DbgDeclareInst; 35 class DbgValueInst; 36 class DbgVariableIntrinsic; 37 class DbgVariableRecord; 38 class Instruction; 39 class Module; 40 41 /// Finds dbg.declare intrinsics declaring local variables as living in the 42 /// memory that 'V' points to. 43 TinyPtrVector<DbgDeclareInst *> findDbgDeclares(Value *V); 44 /// As above, for DVRDeclares. 45 TinyPtrVector<DbgVariableRecord *> findDVRDeclares(Value *V); 46 /// As above, for DVRValues. 47 TinyPtrVector<DbgVariableRecord *> findDVRValues(Value *V); 48 49 /// Finds the llvm.dbg.value intrinsics describing a value. 50 void findDbgValues( 51 SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V, 52 SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords = nullptr); 53 54 /// Finds the debug info intrinsics describing a value. 55 void findDbgUsers( 56 SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V, 57 SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords = nullptr); 58 59 /// Find subprogram that is enclosing this scope. 60 DISubprogram *getDISubprogram(const MDNode *Scope); 61 62 /// Produce a DebugLoc to use for each dbg.declare that is promoted to a 63 /// dbg.value. 64 DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII); 65 DebugLoc getDebugValueLoc(DbgVariableRecord *DVR); 66 67 /// Strip debug info in the module if it exists. 68 /// 69 /// To do this, we remove all calls to the debugger intrinsics and any named 70 /// metadata for debugging. We also remove debug locations for instructions. 71 /// Return true if module is modified. 72 bool StripDebugInfo(Module &M); 73 bool stripDebugInfo(Function &F); 74 75 /// Downgrade the debug info in a module to contain only line table information. 76 /// 77 /// In order to convert debug info to what -gline-tables-only would have 78 /// created, this does the following: 79 /// 1) Delete all debug intrinsics. 80 /// 2) Delete all non-CU named metadata debug info nodes. 81 /// 3) Create new DebugLocs for each instruction. 82 /// 4) Create a new CU debug info, and similarly for every metadata node 83 /// that's reachable from the CU debug info. 84 /// All debug type metadata nodes are unreachable and garbage collected. 85 bool stripNonLineTableDebugInfo(Module &M); 86 87 /// Update the debug locations contained within the MD_loop metadata attached 88 /// to the instruction \p I, if one exists. \p Updater is applied to Metadata 89 /// operand in the MD_loop metadata: the returned value is included in the 90 /// updated loop metadata node if it is non-null. 91 void updateLoopMetadataDebugLocations( 92 Instruction &I, function_ref<Metadata *(Metadata *)> Updater); 93 94 /// Return Debug Info Metadata Version by checking module flags. 95 unsigned getDebugMetadataVersionFromModule(const Module &M); 96 97 /// Utility to find all debug info in a module. 98 /// 99 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To 100 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses 101 /// processDeclare, processValue and processLocation to handle DbgDeclareInst, 102 /// DbgValueInst and DbgLoc attached to instructions. processModule will go 103 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes 104 /// used by the CUs. 105 class DebugInfoFinder { 106 public: 107 /// Process entire module and collect debug info anchors. 108 void processModule(const Module &M); 109 /// Process a single instruction and collect debug info anchors. 110 void processInstruction(const Module &M, const Instruction &I); 111 112 /// Process a DILocalVariable. 113 void processVariable(const Module &M, const DILocalVariable *DVI); 114 /// Process debug info location. 115 void processLocation(const Module &M, const DILocation *Loc); 116 /// Process a DbgRecord (e.g, treat a DbgVariableRecord like a 117 /// DbgVariableIntrinsic). 118 void processDbgRecord(const Module &M, const DbgRecord &DR); 119 120 /// Process subprogram. 121 void processSubprogram(DISubprogram *SP); 122 123 /// Clear all lists. 124 void reset(); 125 126 private: 127 void processCompileUnit(DICompileUnit *CU); 128 void processScope(DIScope *Scope); 129 void processType(DIType *DT); 130 bool addCompileUnit(DICompileUnit *CU); 131 bool addGlobalVariable(DIGlobalVariableExpression *DIG); 132 bool addScope(DIScope *Scope); 133 bool addSubprogram(DISubprogram *SP); 134 bool addType(DIType *DT); 135 136 public: 137 using compile_unit_iterator = 138 SmallVectorImpl<DICompileUnit *>::const_iterator; 139 using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator; 140 using global_variable_expression_iterator = 141 SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator; 142 using type_iterator = SmallVectorImpl<DIType *>::const_iterator; 143 using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator; 144 145 iterator_range<compile_unit_iterator> compile_units() const { 146 return make_range(CUs.begin(), CUs.end()); 147 } 148 149 iterator_range<subprogram_iterator> subprograms() const { 150 return make_range(SPs.begin(), SPs.end()); 151 } 152 153 iterator_range<global_variable_expression_iterator> global_variables() const { 154 return make_range(GVs.begin(), GVs.end()); 155 } 156 157 iterator_range<type_iterator> types() const { 158 return make_range(TYs.begin(), TYs.end()); 159 } 160 161 iterator_range<scope_iterator> scopes() const { 162 return make_range(Scopes.begin(), Scopes.end()); 163 } 164 165 unsigned compile_unit_count() const { return CUs.size(); } 166 unsigned global_variable_count() const { return GVs.size(); } 167 unsigned subprogram_count() const { return SPs.size(); } 168 unsigned type_count() const { return TYs.size(); } 169 unsigned scope_count() const { return Scopes.size(); } 170 171 private: 172 SmallVector<DICompileUnit *, 8> CUs; 173 SmallVector<DISubprogram *, 8> SPs; 174 SmallVector<DIGlobalVariableExpression *, 8> GVs; 175 SmallVector<DIType *, 8> TYs; 176 SmallVector<DIScope *, 8> Scopes; 177 SmallPtrSet<const MDNode *, 32> NodesSeen; 178 }; 179 180 /// Assignment Tracking (at). 181 namespace at { 182 // 183 // Utilities for enumerating storing instructions from an assignment ID. 184 // 185 /// A range of instructions. 186 using AssignmentInstRange = 187 iterator_range<SmallVectorImpl<Instruction *>::iterator>; 188 /// Return a range of instructions (typically just one) that have \p ID 189 /// as an attachment. 190 /// Iterators invalidated by adding or removing DIAssignID metadata to/from any 191 /// instruction (including by deleting or cloning instructions). 192 AssignmentInstRange getAssignmentInsts(DIAssignID *ID); 193 /// Return a range of instructions (typically just one) that perform the 194 /// assignment that \p DAI encodes. 195 /// Iterators invalidated by adding or removing DIAssignID metadata to/from any 196 /// instruction (including by deleting or cloning instructions). 197 inline AssignmentInstRange getAssignmentInsts(const DbgAssignIntrinsic *DAI) { 198 return getAssignmentInsts(DAI->getAssignID()); 199 } 200 201 inline AssignmentInstRange getAssignmentInsts(const DbgVariableRecord *DVR) { 202 assert(DVR->isDbgAssign() && 203 "Can't get assignment instructions for non-assign DVR!"); 204 return getAssignmentInsts(DVR->getAssignID()); 205 } 206 207 // 208 // Utilities for enumerating llvm.dbg.assign intrinsic from an assignment ID. 209 // 210 /// High level: this is an iterator for llvm.dbg.assign intrinsics. 211 /// Implementation details: this is a wrapper around Value's User iterator that 212 /// dereferences to a DbgAssignIntrinsic ptr rather than a User ptr. 213 class DbgAssignIt 214 : public iterator_adaptor_base<DbgAssignIt, Value::user_iterator, 215 typename std::iterator_traits< 216 Value::user_iterator>::iterator_category, 217 DbgAssignIntrinsic *, std::ptrdiff_t, 218 DbgAssignIntrinsic **, 219 DbgAssignIntrinsic *&> { 220 public: 221 DbgAssignIt(Value::user_iterator It) : iterator_adaptor_base(It) {} 222 DbgAssignIntrinsic *operator*() const { return cast<DbgAssignIntrinsic>(*I); } 223 }; 224 /// A range of llvm.dbg.assign intrinsics. 225 using AssignmentMarkerRange = iterator_range<DbgAssignIt>; 226 /// Return a range of dbg.assign intrinsics which use \ID as an operand. 227 /// Iterators invalidated by deleting an intrinsic contained in this range. 228 AssignmentMarkerRange getAssignmentMarkers(DIAssignID *ID); 229 /// Return a range of dbg.assign intrinsics for which \p Inst performs the 230 /// assignment they encode. 231 /// Iterators invalidated by deleting an intrinsic contained in this range. 232 inline AssignmentMarkerRange getAssignmentMarkers(const Instruction *Inst) { 233 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID)) 234 return getAssignmentMarkers(cast<DIAssignID>(ID)); 235 else 236 return make_range(Value::user_iterator(), Value::user_iterator()); 237 } 238 239 inline SmallVector<DbgVariableRecord *> 240 getDVRAssignmentMarkers(const Instruction *Inst) { 241 if (auto *ID = Inst->getMetadata(LLVMContext::MD_DIAssignID)) 242 return cast<DIAssignID>(ID)->getAllDbgVariableRecordUsers(); 243 return {}; 244 } 245 246 /// Delete the llvm.dbg.assign intrinsics linked to \p Inst. 247 void deleteAssignmentMarkers(const Instruction *Inst); 248 249 /// Replace all uses (and attachments) of \p Old with \p New. 250 void RAUW(DIAssignID *Old, DIAssignID *New); 251 252 /// Remove all Assignment Tracking related intrinsics and metadata from \p F. 253 void deleteAll(Function *F); 254 255 /// Calculate the fragment of the variable in \p DAI covered 256 /// from (Dest + SliceOffsetInBits) to 257 /// to (Dest + SliceOffsetInBits + SliceSizeInBits) 258 /// 259 /// Return false if it can't be calculated for any reason. 260 /// Result is set to nullopt if the intersect equals the variable fragment (or 261 /// variable size) in DAI. 262 /// 263 /// Result contains a zero-sized fragment if there's no intersect. 264 bool calculateFragmentIntersect( 265 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, 266 uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DbgAssign, 267 std::optional<DIExpression::FragmentInfo> &Result); 268 bool calculateFragmentIntersect( 269 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, 270 uint64_t SliceSizeInBits, const DbgVariableRecord *DVRAssign, 271 std::optional<DIExpression::FragmentInfo> &Result); 272 273 /// Replace DIAssignID uses and attachments with IDs from \p Map. 274 /// If an ID is unmapped a new ID is generated and added to \p Map. 275 void remapAssignID(DenseMap<DIAssignID *, DIAssignID *> &Map, Instruction &I); 276 277 /// Helper struct for trackAssignments, below. We don't use the similar 278 /// DebugVariable class because trackAssignments doesn't (yet?) understand 279 /// partial variables (fragment info) as input and want to make that clear and 280 /// explicit using types. In addition, eventually we will want to understand 281 /// expressions that modify the base address too, which a DebugVariable doesn't 282 /// capture. 283 struct VarRecord { 284 DILocalVariable *Var; 285 DILocation *DL; 286 287 VarRecord(DbgVariableIntrinsic *DVI) 288 : Var(DVI->getVariable()), DL(getDebugValueLoc(DVI)) {} 289 VarRecord(DbgVariableRecord *DVR) 290 : Var(DVR->getVariable()), DL(getDebugValueLoc(DVR)) {} 291 VarRecord(DILocalVariable *Var, DILocation *DL) : Var(Var), DL(DL) {} 292 friend bool operator<(const VarRecord &LHS, const VarRecord &RHS) { 293 return std::tie(LHS.Var, LHS.DL) < std::tie(RHS.Var, RHS.DL); 294 } 295 friend bool operator==(const VarRecord &LHS, const VarRecord &RHS) { 296 return std::tie(LHS.Var, LHS.DL) == std::tie(RHS.Var, RHS.DL); 297 } 298 }; 299 300 } // namespace at 301 302 template <> struct DenseMapInfo<at::VarRecord> { 303 static inline at::VarRecord getEmptyKey() { 304 return at::VarRecord(DenseMapInfo<DILocalVariable *>::getEmptyKey(), 305 DenseMapInfo<DILocation *>::getEmptyKey()); 306 } 307 308 static inline at::VarRecord getTombstoneKey() { 309 return at::VarRecord(DenseMapInfo<DILocalVariable *>::getTombstoneKey(), 310 DenseMapInfo<DILocation *>::getTombstoneKey()); 311 } 312 313 static unsigned getHashValue(const at::VarRecord &Var) { 314 return hash_combine(Var.Var, Var.DL); 315 } 316 317 static bool isEqual(const at::VarRecord &A, const at::VarRecord &B) { 318 return A == B; 319 } 320 }; 321 322 namespace at { 323 /// Map of backing storage to a set of variables that are stored to it. 324 /// TODO: Backing storage shouldn't be limited to allocas only. Some local 325 /// variables have their storage allocated by the calling function (addresses 326 /// passed in with sret & byval parameters). 327 using StorageToVarsMap = 328 DenseMap<const AllocaInst *, SmallSetVector<VarRecord, 2>>; 329 330 /// Track assignments to \p Vars between \p Start and \p End. 331 332 void trackAssignments(Function::iterator Start, Function::iterator End, 333 const StorageToVarsMap &Vars, const DataLayout &DL, 334 bool DebugPrints = false); 335 336 /// Describes properties of a store that has a static size and offset into a 337 /// some base storage. Used by the getAssignmentInfo functions. 338 struct AssignmentInfo { 339 AllocaInst const *Base; ///< Base storage. 340 uint64_t OffsetInBits; ///< Offset into Base. 341 uint64_t SizeInBits; ///< Number of bits stored. 342 bool StoreToWholeAlloca; ///< SizeInBits equals the size of the base storage. 343 344 AssignmentInfo(const DataLayout &DL, AllocaInst const *Base, 345 uint64_t OffsetInBits, uint64_t SizeInBits) 346 : Base(Base), OffsetInBits(OffsetInBits), SizeInBits(SizeInBits), 347 StoreToWholeAlloca( 348 OffsetInBits == 0 && 349 SizeInBits == DL.getTypeSizeInBits(Base->getAllocatedType())) {} 350 }; 351 352 std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL, 353 const MemIntrinsic *I); 354 std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL, 355 const StoreInst *SI); 356 std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL, 357 const AllocaInst *AI); 358 359 } // end namespace at 360 361 /// Convert @llvm.dbg.declare intrinsics into sets of @llvm.dbg.assign 362 /// intrinsics by treating stores to the dbg.declare'd address as assignments 363 /// to the variable. Not all kinds of variables are supported yet; those will 364 /// be left with their dbg.declare intrinsics. 365 /// The pass sets the debug-info-assignment-tracking module flag to true to 366 /// indicate assignment tracking has been enabled. 367 class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> { 368 /// Note: this method does not set the debug-info-assignment-tracking module 369 /// flag. 370 bool runOnFunction(Function &F); 371 372 public: 373 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 374 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 375 }; 376 377 /// Return true if assignment tracking is enabled for module \p M. 378 bool isAssignmentTrackingEnabled(const Module &M); 379 380 } // end namespace llvm 381 382 #endif // LLVM_IR_DEBUGINFO_H 383