LLVM  mainline
DebugInfo.cpp
Go to the documentation of this file.
00001 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the helper classes used to build and interpret debug
00011 // information in LLVM IR form.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/IR/DebugInfo.h"
00016 #include "LLVMContextImpl.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/ADT/SmallPtrSet.h"
00019 #include "llvm/ADT/SmallString.h"
00020 #include "llvm/Analysis/ValueTracking.h"
00021 #include "llvm/IR/Constants.h"
00022 #include "llvm/IR/DIBuilder.h"
00023 #include "llvm/IR/DerivedTypes.h"
00024 #include "llvm/IR/Instructions.h"
00025 #include "llvm/IR/IntrinsicInst.h"
00026 #include "llvm/IR/Intrinsics.h"
00027 #include "llvm/IR/GVMaterializer.h"
00028 #include "llvm/IR/Module.h"
00029 #include "llvm/IR/ValueHandle.h"
00030 #include "llvm/Support/Debug.h"
00031 #include "llvm/Support/Dwarf.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 using namespace llvm;
00034 using namespace llvm::dwarf;
00035 
00036 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
00037   if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
00038     return LocalScope->getSubprogram();
00039   return nullptr;
00040 }
00041 
00042 DISubprogram *llvm::getDISubprogram(const Function *F) {
00043   // We look for the first instr that has a debug annotation leading back to F.
00044   for (auto &BB : *F) {
00045     auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
00046       return Inst.getDebugLoc();
00047     });
00048     if (Inst == BB.end())
00049       continue;
00050     DebugLoc DLoc = Inst->getDebugLoc();
00051     const MDNode *Scope = DLoc.getInlinedAtScope();
00052     auto *Subprogram = getDISubprogram(Scope);
00053     return Subprogram->describes(F) ? Subprogram : nullptr;
00054   }
00055 
00056   return nullptr;
00057 }
00058 
00059 DITypeIdentifierMap
00060 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
00061   DITypeIdentifierMap Map;
00062   for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
00063     auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
00064     DINodeArray Retain = CU->getRetainedTypes();
00065     for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
00066       if (!isa<DICompositeType>(Retain[Ti]))
00067         continue;
00068       auto *Ty = cast<DICompositeType>(Retain[Ti]);
00069       if (MDString *TypeId = Ty->getRawIdentifier()) {
00070         // Definition has priority over declaration.
00071         // Try to insert (TypeId, Ty) to Map.
00072         std::pair<DITypeIdentifierMap::iterator, bool> P =
00073             Map.insert(std::make_pair(TypeId, Ty));
00074         // If TypeId already exists in Map and this is a definition, replace
00075         // whatever we had (declaration or definition) with the definition.
00076         if (!P.second && !Ty->isForwardDecl())
00077           P.first->second = Ty;
00078       }
00079     }
00080   }
00081   return Map;
00082 }
00083 
00084 //===----------------------------------------------------------------------===//
00085 // DebugInfoFinder implementations.
00086 //===----------------------------------------------------------------------===//
00087 
00088 void DebugInfoFinder::reset() {
00089   CUs.clear();
00090   SPs.clear();
00091   GVs.clear();
00092   TYs.clear();
00093   Scopes.clear();
00094   NodesSeen.clear();
00095   TypeIdentifierMap.clear();
00096   TypeMapInitialized = false;
00097 }
00098 
00099 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
00100   if (!TypeMapInitialized)
00101     if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
00102       TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
00103       TypeMapInitialized = true;
00104     }
00105 }
00106 
00107 void DebugInfoFinder::processModule(const Module &M) {
00108   InitializeTypeMap(M);
00109   if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
00110     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
00111       auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
00112       addCompileUnit(CU);
00113       for (auto *DIG : CU->getGlobalVariables()) {
00114         if (addGlobalVariable(DIG)) {
00115           processScope(DIG->getScope());
00116           processType(DIG->getType().resolve(TypeIdentifierMap));
00117         }
00118       }
00119       for (auto *SP : CU->getSubprograms())
00120         processSubprogram(SP);
00121       for (auto *ET : CU->getEnumTypes())
00122         processType(ET);
00123       for (auto *RT : CU->getRetainedTypes())
00124         processType(RT);
00125       for (auto *Import : CU->getImportedEntities()) {
00126         auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
00127         if (auto *T = dyn_cast<DIType>(Entity))
00128           processType(T);
00129         else if (auto *SP = dyn_cast<DISubprogram>(Entity))
00130           processSubprogram(SP);
00131         else if (auto *NS = dyn_cast<DINamespace>(Entity))
00132           processScope(NS->getScope());
00133         else if (auto *M = dyn_cast<DIModule>(Entity))
00134           processScope(M->getScope());
00135       }
00136     }
00137   }
00138 }
00139 
00140 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
00141   if (!Loc)
00142     return;
00143   InitializeTypeMap(M);
00144   processScope(Loc->getScope());
00145   processLocation(M, Loc->getInlinedAt());
00146 }
00147 
00148 void DebugInfoFinder::processType(DIType *DT) {
00149   if (!addType(DT))
00150     return;
00151   processScope(DT->getScope().resolve(TypeIdentifierMap));
00152   if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
00153     for (DITypeRef Ref : ST->getTypeArray())
00154       processType(Ref.resolve(TypeIdentifierMap));
00155     return;
00156   }
00157   if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
00158     processType(DCT->getBaseType().resolve(TypeIdentifierMap));
00159     for (Metadata *D : DCT->getElements()) {
00160       if (auto *T = dyn_cast<DIType>(D))
00161         processType(T);
00162       else if (auto *SP = dyn_cast<DISubprogram>(D))
00163         processSubprogram(SP);
00164     }
00165     return;
00166   }
00167   if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
00168     processType(DDT->getBaseType().resolve(TypeIdentifierMap));
00169   }
00170 }
00171 
00172 void DebugInfoFinder::processScope(DIScope *Scope) {
00173   if (!Scope)
00174     return;
00175   if (auto *Ty = dyn_cast<DIType>(Scope)) {
00176     processType(Ty);
00177     return;
00178   }
00179   if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
00180     addCompileUnit(CU);
00181     return;
00182   }
00183   if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
00184     processSubprogram(SP);
00185     return;
00186   }
00187   if (!addScope(Scope))
00188     return;
00189   if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
00190     processScope(LB->getScope());
00191   } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
00192     processScope(NS->getScope());
00193   } else if (auto *M = dyn_cast<DIModule>(Scope)) {
00194     processScope(M->getScope());
00195   }
00196 }
00197 
00198 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
00199   if (!addSubprogram(SP))
00200     return;
00201   processScope(SP->getScope().resolve(TypeIdentifierMap));
00202   processType(SP->getType());
00203   for (auto *Element : SP->getTemplateParams()) {
00204     if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
00205       processType(TType->getType().resolve(TypeIdentifierMap));
00206     } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
00207       processType(TVal->getType().resolve(TypeIdentifierMap));
00208     }
00209   }
00210 }
00211 
00212 void DebugInfoFinder::processDeclare(const Module &M,
00213                                      const DbgDeclareInst *DDI) {
00214   auto *N = dyn_cast<MDNode>(DDI->getVariable());
00215   if (!N)
00216     return;
00217   InitializeTypeMap(M);
00218 
00219   auto *DV = dyn_cast<DILocalVariable>(N);
00220   if (!DV)
00221     return;
00222 
00223   if (!NodesSeen.insert(DV).second)
00224     return;
00225   processScope(DV->getScope());
00226   processType(DV->getType().resolve(TypeIdentifierMap));
00227 }
00228 
00229 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
00230   auto *N = dyn_cast<MDNode>(DVI->getVariable());
00231   if (!N)
00232     return;
00233   InitializeTypeMap(M);
00234 
00235   auto *DV = dyn_cast<DILocalVariable>(N);
00236   if (!DV)
00237     return;
00238 
00239   if (!NodesSeen.insert(DV).second)
00240     return;
00241   processScope(DV->getScope());
00242   processType(DV->getType().resolve(TypeIdentifierMap));
00243 }
00244 
00245 bool DebugInfoFinder::addType(DIType *DT) {
00246   if (!DT)
00247     return false;
00248 
00249   if (!NodesSeen.insert(DT).second)
00250     return false;
00251 
00252   TYs.push_back(const_cast<DIType *>(DT));
00253   return true;
00254 }
00255 
00256 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
00257   if (!CU)
00258     return false;
00259   if (!NodesSeen.insert(CU).second)
00260     return false;
00261 
00262   CUs.push_back(CU);
00263   return true;
00264 }
00265 
00266 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
00267   if (!DIG)
00268     return false;
00269 
00270   if (!NodesSeen.insert(DIG).second)
00271     return false;
00272 
00273   GVs.push_back(DIG);
00274   return true;
00275 }
00276 
00277 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
00278   if (!SP)
00279     return false;
00280 
00281   if (!NodesSeen.insert(SP).second)
00282     return false;
00283 
00284   SPs.push_back(SP);
00285   return true;
00286 }
00287 
00288 bool DebugInfoFinder::addScope(DIScope *Scope) {
00289   if (!Scope)
00290     return false;
00291   // FIXME: Ocaml binding generates a scope with no content, we treat it
00292   // as null for now.
00293   if (Scope->getNumOperands() == 0)
00294     return false;
00295   if (!NodesSeen.insert(Scope).second)
00296     return false;
00297   Scopes.push_back(Scope);
00298   return true;
00299 }
00300 
00301 bool llvm::stripDebugInfo(Function &F) {
00302   bool Changed = false;
00303   if (F.getSubprogram()) {
00304     Changed = true;
00305     F.setSubprogram(nullptr);
00306   }
00307   for (BasicBlock &BB : F) {
00308     for (Instruction &I : BB) {
00309       if (I.getDebugLoc()) {
00310         Changed = true;
00311         I.setDebugLoc(DebugLoc());
00312       }
00313     }
00314   }
00315   return Changed;
00316 }
00317 
00318 bool llvm::StripDebugInfo(Module &M) {
00319   bool Changed = false;
00320 
00321   // Remove all of the calls to the debugger intrinsics, and remove them from
00322   // the module.
00323   if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
00324     while (!Declare->use_empty()) {
00325       CallInst *CI = cast<CallInst>(Declare->user_back());
00326       CI->eraseFromParent();
00327     }
00328     Declare->eraseFromParent();
00329     Changed = true;
00330   }
00331 
00332   if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
00333     while (!DbgVal->use_empty()) {
00334       CallInst *CI = cast<CallInst>(DbgVal->user_back());
00335       CI->eraseFromParent();
00336     }
00337     DbgVal->eraseFromParent();
00338     Changed = true;
00339   }
00340 
00341   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
00342          NME = M.named_metadata_end(); NMI != NME;) {
00343     NamedMDNode *NMD = &*NMI;
00344     ++NMI;
00345     if (NMD->getName().startswith("llvm.dbg.")) {
00346       NMD->eraseFromParent();
00347       Changed = true;
00348     }
00349   }
00350 
00351   for (Function &F : M)
00352     Changed |= stripDebugInfo(F);
00353 
00354   if (GVMaterializer *Materializer = M.getMaterializer())
00355     Materializer->setStripDebugInfo();
00356 
00357   return Changed;
00358 }
00359 
00360 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
00361   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
00362           M.getModuleFlag("Debug Info Version")))
00363     return Val->getZExtValue();
00364   return 0;
00365 }