1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the helper classes used to build and interpret debug 11 // information in LLVM IR form. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/DebugInfo.h" 16 #include "LLVMContextImpl.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/Analysis/ValueTracking.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/DIBuilder.h" 23 #include "llvm/IR/DerivedTypes.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/IntrinsicInst.h" 26 #include "llvm/IR/Intrinsics.h" 27 #include "llvm/IR/GVMaterializer.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/IR/ValueHandle.h" 30 #include "llvm/Support/Debug.h" 31 #include "llvm/Support/Dwarf.h" 32 #include "llvm/Support/raw_ostream.h" 33 using namespace llvm; 34 using namespace llvm::dwarf; 35 36 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { 37 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) 38 return LocalScope->getSubprogram(); 39 return nullptr; 40 } 41 42 DISubprogram *llvm::getDISubprogram(const Function *F) { 43 // We look for the first instr that has a debug annotation leading back to F. 44 for (auto &BB : *F) { 45 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { 46 return Inst.getDebugLoc(); 47 }); 48 if (Inst == BB.end()) 49 continue; 50 DebugLoc DLoc = Inst->getDebugLoc(); 51 const MDNode *Scope = DLoc.getInlinedAtScope(); 52 auto *Subprogram = getDISubprogram(Scope); 53 return Subprogram->describes(F) ? Subprogram : nullptr; 54 } 55 56 return nullptr; 57 } 58 59 DICompositeTypeBase *llvm::getDICompositeType(DIType *T) { 60 if (auto *C = dyn_cast_or_null<DICompositeTypeBase>(T)) 61 return C; 62 63 if (auto *D = dyn_cast_or_null<DIDerivedTypeBase>(T)) { 64 // This function is currently used by dragonegg and dragonegg does 65 // not generate identifier for types, so using an empty map to resolve 66 // DerivedFrom should be fine. 67 DITypeIdentifierMap EmptyMap; 68 return getDICompositeType(D->getBaseType().resolve(EmptyMap)); 69 } 70 71 return nullptr; 72 } 73 74 DITypeIdentifierMap 75 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { 76 DITypeIdentifierMap Map; 77 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { 78 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi)); 79 DINodeArray Retain = CU->getRetainedTypes(); 80 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) { 81 if (!isa<DICompositeType>(Retain[Ti])) 82 continue; 83 auto *Ty = cast<DICompositeType>(Retain[Ti]); 84 if (MDString *TypeId = Ty->getRawIdentifier()) { 85 // Definition has priority over declaration. 86 // Try to insert (TypeId, Ty) to Map. 87 std::pair<DITypeIdentifierMap::iterator, bool> P = 88 Map.insert(std::make_pair(TypeId, Ty)); 89 // If TypeId already exists in Map and this is a definition, replace 90 // whatever we had (declaration or definition) with the definition. 91 if (!P.second && !Ty->isForwardDecl()) 92 P.first->second = Ty; 93 } 94 } 95 } 96 return Map; 97 } 98 99 //===----------------------------------------------------------------------===// 100 // DebugInfoFinder implementations. 101 //===----------------------------------------------------------------------===// 102 103 void DebugInfoFinder::reset() { 104 CUs.clear(); 105 SPs.clear(); 106 GVs.clear(); 107 TYs.clear(); 108 Scopes.clear(); 109 NodesSeen.clear(); 110 TypeIdentifierMap.clear(); 111 TypeMapInitialized = false; 112 } 113 114 void DebugInfoFinder::InitializeTypeMap(const Module &M) { 115 if (!TypeMapInitialized) 116 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 117 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); 118 TypeMapInitialized = true; 119 } 120 } 121 122 void DebugInfoFinder::processModule(const Module &M) { 123 InitializeTypeMap(M); 124 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 125 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { 126 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i)); 127 addCompileUnit(CU); 128 for (auto *DIG : CU->getGlobalVariables()) { 129 if (addGlobalVariable(DIG)) { 130 processScope(DIG->getScope()); 131 processType(DIG->getType().resolve(TypeIdentifierMap)); 132 } 133 } 134 for (auto *SP : CU->getSubprograms()) 135 processSubprogram(SP); 136 for (auto *ET : CU->getEnumTypes()) 137 processType(ET); 138 for (auto *RT : CU->getRetainedTypes()) 139 processType(RT); 140 for (auto *Import : CU->getImportedEntities()) { 141 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap); 142 if (auto *T = dyn_cast<DIType>(Entity)) 143 processType(T); 144 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 145 processSubprogram(SP); 146 else if (auto *NS = dyn_cast<DINamespace>(Entity)) 147 processScope(NS->getScope()); 148 else if (auto *M = dyn_cast<DIModule>(Entity)) 149 processScope(M->getScope()); 150 } 151 } 152 } 153 } 154 155 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { 156 if (!Loc) 157 return; 158 InitializeTypeMap(M); 159 processScope(Loc->getScope()); 160 processLocation(M, Loc->getInlinedAt()); 161 } 162 163 void DebugInfoFinder::processType(DIType *DT) { 164 if (!addType(DT)) 165 return; 166 processScope(DT->getScope().resolve(TypeIdentifierMap)); 167 if (auto *DCT = dyn_cast<DICompositeTypeBase>(DT)) { 168 processType(DCT->getBaseType().resolve(TypeIdentifierMap)); 169 if (auto *ST = dyn_cast<DISubroutineType>(DCT)) { 170 for (DITypeRef Ref : ST->getTypeArray()) 171 processType(Ref.resolve(TypeIdentifierMap)); 172 return; 173 } 174 for (Metadata *D : DCT->getElements()) { 175 if (auto *T = dyn_cast<DIType>(D)) 176 processType(T); 177 else if (auto *SP = dyn_cast<DISubprogram>(D)) 178 processSubprogram(SP); 179 } 180 } else if (auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) { 181 processType(DDT->getBaseType().resolve(TypeIdentifierMap)); 182 } 183 } 184 185 void DebugInfoFinder::processScope(DIScope *Scope) { 186 if (!Scope) 187 return; 188 if (auto *Ty = dyn_cast<DIType>(Scope)) { 189 processType(Ty); 190 return; 191 } 192 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { 193 addCompileUnit(CU); 194 return; 195 } 196 if (auto *SP = dyn_cast<DISubprogram>(Scope)) { 197 processSubprogram(SP); 198 return; 199 } 200 if (!addScope(Scope)) 201 return; 202 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { 203 processScope(LB->getScope()); 204 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { 205 processScope(NS->getScope()); 206 } else if (auto *M = dyn_cast<DIModule>(Scope)) { 207 processScope(M->getScope()); 208 } 209 } 210 211 void DebugInfoFinder::processSubprogram(DISubprogram *SP) { 212 if (!addSubprogram(SP)) 213 return; 214 processScope(SP->getScope().resolve(TypeIdentifierMap)); 215 processType(SP->getType()); 216 for (auto *Element : SP->getTemplateParams()) { 217 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { 218 processType(TType->getType().resolve(TypeIdentifierMap)); 219 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { 220 processType(TVal->getType().resolve(TypeIdentifierMap)); 221 } 222 } 223 } 224 225 void DebugInfoFinder::processDeclare(const Module &M, 226 const DbgDeclareInst *DDI) { 227 auto *N = dyn_cast<MDNode>(DDI->getVariable()); 228 if (!N) 229 return; 230 InitializeTypeMap(M); 231 232 auto *DV = dyn_cast<DILocalVariable>(N); 233 if (!DV) 234 return; 235 236 if (!NodesSeen.insert(DV).second) 237 return; 238 processScope(DV->getScope()); 239 processType(DV->getType().resolve(TypeIdentifierMap)); 240 } 241 242 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 243 auto *N = dyn_cast<MDNode>(DVI->getVariable()); 244 if (!N) 245 return; 246 InitializeTypeMap(M); 247 248 auto *DV = dyn_cast<DILocalVariable>(N); 249 if (!DV) 250 return; 251 252 if (!NodesSeen.insert(DV).second) 253 return; 254 processScope(DV->getScope()); 255 processType(DV->getType().resolve(TypeIdentifierMap)); 256 } 257 258 bool DebugInfoFinder::addType(DIType *DT) { 259 if (!DT) 260 return false; 261 262 if (!NodesSeen.insert(DT).second) 263 return false; 264 265 TYs.push_back(const_cast<DIType *>(DT)); 266 return true; 267 } 268 269 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { 270 if (!CU) 271 return false; 272 if (!NodesSeen.insert(CU).second) 273 return false; 274 275 CUs.push_back(CU); 276 return true; 277 } 278 279 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) { 280 if (!DIG) 281 return false; 282 283 if (!NodesSeen.insert(DIG).second) 284 return false; 285 286 GVs.push_back(DIG); 287 return true; 288 } 289 290 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { 291 if (!SP) 292 return false; 293 294 if (!NodesSeen.insert(SP).second) 295 return false; 296 297 SPs.push_back(SP); 298 return true; 299 } 300 301 bool DebugInfoFinder::addScope(DIScope *Scope) { 302 if (!Scope) 303 return false; 304 // FIXME: Ocaml binding generates a scope with no content, we treat it 305 // as null for now. 306 if (Scope->getNumOperands() == 0) 307 return false; 308 if (!NodesSeen.insert(Scope).second) 309 return false; 310 Scopes.push_back(Scope); 311 return true; 312 } 313 314 bool llvm::stripDebugInfo(Function &F) { 315 bool Changed = false; 316 for (BasicBlock &BB : F) { 317 for (Instruction &I : BB) { 318 if (I.getDebugLoc()) { 319 Changed = true; 320 I.setDebugLoc(DebugLoc()); 321 } 322 } 323 } 324 return Changed; 325 } 326 327 bool llvm::StripDebugInfo(Module &M) { 328 bool Changed = false; 329 330 // Remove all of the calls to the debugger intrinsics, and remove them from 331 // the module. 332 if (Function *Declare = M.getFunction("llvm.dbg.declare")) { 333 while (!Declare->use_empty()) { 334 CallInst *CI = cast<CallInst>(Declare->user_back()); 335 CI->eraseFromParent(); 336 } 337 Declare->eraseFromParent(); 338 Changed = true; 339 } 340 341 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { 342 while (!DbgVal->use_empty()) { 343 CallInst *CI = cast<CallInst>(DbgVal->user_back()); 344 CI->eraseFromParent(); 345 } 346 DbgVal->eraseFromParent(); 347 Changed = true; 348 } 349 350 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 351 NME = M.named_metadata_end(); NMI != NME;) { 352 NamedMDNode *NMD = NMI; 353 ++NMI; 354 if (NMD->getName().startswith("llvm.dbg.")) { 355 NMD->eraseFromParent(); 356 Changed = true; 357 } 358 } 359 360 for (Function &F : M) 361 Changed |= stripDebugInfo(F); 362 363 if (GVMaterializer *Materializer = M.getMaterializer()) 364 Materializer->setStripDebugInfo(); 365 366 return Changed; 367 } 368 369 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 370 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 371 M.getModuleFlag("Debug Info Version"))) 372 return Val->getZExtValue(); 373 return 0; 374 } 375 376 DenseMap<const llvm::Function *, DISubprogram *> 377 llvm::makeSubprogramMap(const Module &M) { 378 DenseMap<const Function *, DISubprogram *> R; 379 380 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); 381 if (!CU_Nodes) 382 return R; 383 384 for (MDNode *N : CU_Nodes->operands()) { 385 auto *CUNode = cast<DICompileUnit>(N); 386 for (auto *SP : CUNode->getSubprograms()) { 387 if (Function *F = SP->getFunction()) 388 R.insert(std::make_pair(F, SP)); 389 } 390 } 391 return R; 392 } 393