Home | History | Annotate | Download | only in IR
      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/IR/Constants.h"
     20 #include "llvm/IR/DIBuilder.h"
     21 #include "llvm/IR/DerivedTypes.h"
     22 #include "llvm/IR/GVMaterializer.h"
     23 #include "llvm/IR/Instructions.h"
     24 #include "llvm/IR/IntrinsicInst.h"
     25 #include "llvm/IR/Intrinsics.h"
     26 #include "llvm/IR/Module.h"
     27 #include "llvm/IR/ValueHandle.h"
     28 #include "llvm/Support/Debug.h"
     29 #include "llvm/Support/Dwarf.h"
     30 #include "llvm/Support/raw_ostream.h"
     31 using namespace llvm;
     32 using namespace llvm::dwarf;
     33 
     34 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
     35   if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
     36     return LocalScope->getSubprogram();
     37   return nullptr;
     38 }
     39 
     40 //===----------------------------------------------------------------------===//
     41 // DebugInfoFinder implementations.
     42 //===----------------------------------------------------------------------===//
     43 
     44 void DebugInfoFinder::reset() {
     45   CUs.clear();
     46   SPs.clear();
     47   GVs.clear();
     48   TYs.clear();
     49   Scopes.clear();
     50   NodesSeen.clear();
     51 }
     52 
     53 void DebugInfoFinder::processModule(const Module &M) {
     54   for (auto *CU : M.debug_compile_units()) {
     55     addCompileUnit(CU);
     56     for (auto *DIG : CU->getGlobalVariables()) {
     57       if (addGlobalVariable(DIG)) {
     58         processScope(DIG->getScope());
     59         processType(DIG->getType().resolve());
     60       }
     61     }
     62     for (auto *ET : CU->getEnumTypes())
     63       processType(ET);
     64     for (auto *RT : CU->getRetainedTypes())
     65       if (auto *T = dyn_cast<DIType>(RT))
     66         processType(T);
     67       else
     68         processSubprogram(cast<DISubprogram>(RT));
     69     for (auto *Import : CU->getImportedEntities()) {
     70       auto *Entity = Import->getEntity().resolve();
     71       if (auto *T = dyn_cast<DIType>(Entity))
     72         processType(T);
     73       else if (auto *SP = dyn_cast<DISubprogram>(Entity))
     74         processSubprogram(SP);
     75       else if (auto *NS = dyn_cast<DINamespace>(Entity))
     76         processScope(NS->getScope());
     77       else if (auto *M = dyn_cast<DIModule>(Entity))
     78         processScope(M->getScope());
     79     }
     80   }
     81   for (auto &F : M.functions())
     82     if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
     83       processSubprogram(SP);
     84 }
     85 
     86 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
     87   if (!Loc)
     88     return;
     89   processScope(Loc->getScope());
     90   processLocation(M, Loc->getInlinedAt());
     91 }
     92 
     93 void DebugInfoFinder::processType(DIType *DT) {
     94   if (!addType(DT))
     95     return;
     96   processScope(DT->getScope().resolve());
     97   if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
     98     for (DITypeRef Ref : ST->getTypeArray())
     99       processType(Ref.resolve());
    100     return;
    101   }
    102   if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
    103     processType(DCT->getBaseType().resolve());
    104     for (Metadata *D : DCT->getElements()) {
    105       if (auto *T = dyn_cast<DIType>(D))
    106         processType(T);
    107       else if (auto *SP = dyn_cast<DISubprogram>(D))
    108         processSubprogram(SP);
    109     }
    110     return;
    111   }
    112   if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
    113     processType(DDT->getBaseType().resolve());
    114   }
    115 }
    116 
    117 void DebugInfoFinder::processScope(DIScope *Scope) {
    118   if (!Scope)
    119     return;
    120   if (auto *Ty = dyn_cast<DIType>(Scope)) {
    121     processType(Ty);
    122     return;
    123   }
    124   if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
    125     addCompileUnit(CU);
    126     return;
    127   }
    128   if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
    129     processSubprogram(SP);
    130     return;
    131   }
    132   if (!addScope(Scope))
    133     return;
    134   if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
    135     processScope(LB->getScope());
    136   } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
    137     processScope(NS->getScope());
    138   } else if (auto *M = dyn_cast<DIModule>(Scope)) {
    139     processScope(M->getScope());
    140   }
    141 }
    142 
    143 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
    144   if (!addSubprogram(SP))
    145     return;
    146   processScope(SP->getScope().resolve());
    147   processType(SP->getType());
    148   for (auto *Element : SP->getTemplateParams()) {
    149     if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
    150       processType(TType->getType().resolve());
    151     } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
    152       processType(TVal->getType().resolve());
    153     }
    154   }
    155 }
    156 
    157 void DebugInfoFinder::processDeclare(const Module &M,
    158                                      const DbgDeclareInst *DDI) {
    159   auto *N = dyn_cast<MDNode>(DDI->getVariable());
    160   if (!N)
    161     return;
    162 
    163   auto *DV = dyn_cast<DILocalVariable>(N);
    164   if (!DV)
    165     return;
    166 
    167   if (!NodesSeen.insert(DV).second)
    168     return;
    169   processScope(DV->getScope());
    170   processType(DV->getType().resolve());
    171 }
    172 
    173 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
    174   auto *N = dyn_cast<MDNode>(DVI->getVariable());
    175   if (!N)
    176     return;
    177 
    178   auto *DV = dyn_cast<DILocalVariable>(N);
    179   if (!DV)
    180     return;
    181 
    182   if (!NodesSeen.insert(DV).second)
    183     return;
    184   processScope(DV->getScope());
    185   processType(DV->getType().resolve());
    186 }
    187 
    188 bool DebugInfoFinder::addType(DIType *DT) {
    189   if (!DT)
    190     return false;
    191 
    192   if (!NodesSeen.insert(DT).second)
    193     return false;
    194 
    195   TYs.push_back(const_cast<DIType *>(DT));
    196   return true;
    197 }
    198 
    199 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
    200   if (!CU)
    201     return false;
    202   if (!NodesSeen.insert(CU).second)
    203     return false;
    204 
    205   CUs.push_back(CU);
    206   return true;
    207 }
    208 
    209 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
    210   if (!DIG)
    211     return false;
    212 
    213   if (!NodesSeen.insert(DIG).second)
    214     return false;
    215 
    216   GVs.push_back(DIG);
    217   return true;
    218 }
    219 
    220 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
    221   if (!SP)
    222     return false;
    223 
    224   if (!NodesSeen.insert(SP).second)
    225     return false;
    226 
    227   SPs.push_back(SP);
    228   return true;
    229 }
    230 
    231 bool DebugInfoFinder::addScope(DIScope *Scope) {
    232   if (!Scope)
    233     return false;
    234   // FIXME: Ocaml binding generates a scope with no content, we treat it
    235   // as null for now.
    236   if (Scope->getNumOperands() == 0)
    237     return false;
    238   if (!NodesSeen.insert(Scope).second)
    239     return false;
    240   Scopes.push_back(Scope);
    241   return true;
    242 }
    243 
    244 bool llvm::stripDebugInfo(Function &F) {
    245   bool Changed = false;
    246   if (F.getSubprogram()) {
    247     Changed = true;
    248     F.setSubprogram(nullptr);
    249   }
    250 
    251   for (BasicBlock &BB : F) {
    252     for (auto II = BB.begin(), End = BB.end(); II != End;) {
    253       Instruction &I = *II++; // We may delete the instruction, increment now.
    254       if (isa<DbgInfoIntrinsic>(&I)) {
    255         I.eraseFromParent();
    256         Changed = true;
    257         continue;
    258       }
    259       if (I.getDebugLoc()) {
    260         Changed = true;
    261         I.setDebugLoc(DebugLoc());
    262       }
    263     }
    264   }
    265   return Changed;
    266 }
    267 
    268 bool llvm::StripDebugInfo(Module &M) {
    269   bool Changed = false;
    270 
    271   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
    272          NME = M.named_metadata_end(); NMI != NME;) {
    273     NamedMDNode *NMD = &*NMI;
    274     ++NMI;
    275     if (NMD->getName().startswith("llvm.dbg.")) {
    276       NMD->eraseFromParent();
    277       Changed = true;
    278     }
    279   }
    280 
    281   for (Function &F : M)
    282     Changed |= stripDebugInfo(F);
    283 
    284   if (GVMaterializer *Materializer = M.getMaterializer())
    285     Materializer->setStripDebugInfo();
    286 
    287   return Changed;
    288 }
    289 
    290 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
    291   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
    292           M.getModuleFlag("Debug Info Version")))
    293     return Val->getZExtValue();
    294   return 0;
    295 }
    296