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 MDSubprogram *llvm::getDISubprogram(const MDNode *Scope) {
00037   if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
00038     return LocalScope->getSubprogram();
00039   return nullptr;
00040 }
00041 
00042 MDSubprogram *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 MDCompositeTypeBase *llvm::getDICompositeType(MDType *T) {
00060   if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
00061     return C;
00062 
00063   if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
00064     // This function is currently used by dragonegg and dragonegg does
00065     // not generate identifier for types, so using an empty map to resolve
00066     // DerivedFrom should be fine.
00067     DITypeIdentifierMap EmptyMap;
00068     return getDICompositeType(D->getBaseType().resolve(EmptyMap));
00069   }
00070 
00071   return nullptr;
00072 }
00073 
00074 DITypeIdentifierMap
00075 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
00076   DITypeIdentifierMap Map;
00077   for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
00078     auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
00079     DIArray Retain = CU->getRetainedTypes();
00080     for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
00081       if (!isa<MDCompositeType>(Retain[Ti]))
00082         continue;
00083       auto *Ty = cast<MDCompositeType>(Retain[Ti]);
00084       if (MDString *TypeId = Ty->getRawIdentifier()) {
00085         // Definition has priority over declaration.
00086         // Try to insert (TypeId, Ty) to Map.
00087         std::pair<DITypeIdentifierMap::iterator, bool> P =
00088             Map.insert(std::make_pair(TypeId, Ty));
00089         // If TypeId already exists in Map and this is a definition, replace
00090         // whatever we had (declaration or definition) with the definition.
00091         if (!P.second && !Ty->isForwardDecl())
00092           P.first->second = Ty;
00093       }
00094     }
00095   }
00096   return Map;
00097 }
00098 
00099 //===----------------------------------------------------------------------===//
00100 // DebugInfoFinder implementations.
00101 //===----------------------------------------------------------------------===//
00102 
00103 void DebugInfoFinder::reset() {
00104   CUs.clear();
00105   SPs.clear();
00106   GVs.clear();
00107   TYs.clear();
00108   Scopes.clear();
00109   NodesSeen.clear();
00110   TypeIdentifierMap.clear();
00111   TypeMapInitialized = false;
00112 }
00113 
00114 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
00115   if (!TypeMapInitialized)
00116     if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
00117       TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
00118       TypeMapInitialized = true;
00119     }
00120 }
00121 
00122 void DebugInfoFinder::processModule(const Module &M) {
00123   InitializeTypeMap(M);
00124   if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
00125     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
00126       auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
00127       addCompileUnit(CU);
00128       for (auto *DIG : CU->getGlobalVariables()) {
00129         if (addGlobalVariable(DIG)) {
00130           processScope(DIG->getScope());
00131           processType(DIG->getType().resolve(TypeIdentifierMap));
00132         }
00133       }
00134       for (auto *SP : CU->getSubprograms())
00135         processSubprogram(SP);
00136       for (auto *ET : CU->getEnumTypes())
00137         processType(ET);
00138       for (auto *RT : CU->getRetainedTypes())
00139         processType(RT);
00140       for (auto *Import : CU->getImportedEntities()) {
00141         auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
00142         if (auto *T = dyn_cast<MDType>(Entity))
00143           processType(T);
00144         else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
00145           processSubprogram(SP);
00146         else if (auto *NS = dyn_cast<MDNamespace>(Entity))
00147           processScope(NS->getScope());
00148       }
00149     }
00150   }
00151 }
00152 
00153 void DebugInfoFinder::processLocation(const Module &M, const MDLocation *Loc) {
00154   if (!Loc)
00155     return;
00156   InitializeTypeMap(M);
00157   processScope(Loc->getScope());
00158   processLocation(M, Loc->getInlinedAt());
00159 }
00160 
00161 void DebugInfoFinder::processType(MDType *DT) {
00162   if (!addType(DT))
00163     return;
00164   processScope(DT->getScope().resolve(TypeIdentifierMap));
00165   if (auto *DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
00166     processType(DCT->getBaseType().resolve(TypeIdentifierMap));
00167     if (auto *ST = dyn_cast<MDSubroutineType>(DCT)) {
00168       for (MDTypeRef Ref : ST->getTypeArray())
00169         processType(Ref.resolve(TypeIdentifierMap));
00170       return;
00171     }
00172     for (Metadata *D : DCT->getElements()) {
00173       if (auto *T = dyn_cast<MDType>(D))
00174         processType(T);
00175       else if (auto *SP = dyn_cast<MDSubprogram>(D))
00176         processSubprogram(SP);
00177     }
00178   } else if (auto *DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
00179     processType(DDT->getBaseType().resolve(TypeIdentifierMap));
00180   }
00181 }
00182 
00183 void DebugInfoFinder::processScope(MDScope *Scope) {
00184   if (!Scope)
00185     return;
00186   if (auto *Ty = dyn_cast<MDType>(Scope)) {
00187     processType(Ty);
00188     return;
00189   }
00190   if (auto *CU = dyn_cast<MDCompileUnit>(Scope)) {
00191     addCompileUnit(CU);
00192     return;
00193   }
00194   if (auto *SP = dyn_cast<MDSubprogram>(Scope)) {
00195     processSubprogram(SP);
00196     return;
00197   }
00198   if (!addScope(Scope))
00199     return;
00200   if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
00201     processScope(LB->getScope());
00202   } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
00203     processScope(NS->getScope());
00204   }
00205 }
00206 
00207 void DebugInfoFinder::processSubprogram(MDSubprogram *SP) {
00208   if (!addSubprogram(SP))
00209     return;
00210   processScope(SP->getScope().resolve(TypeIdentifierMap));
00211   processType(SP->getType());
00212   for (auto *Element : SP->getTemplateParams()) {
00213     if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
00214       processType(TType->getType().resolve(TypeIdentifierMap));
00215     } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
00216       processType(TVal->getType().resolve(TypeIdentifierMap));
00217     }
00218   }
00219 }
00220 
00221 void DebugInfoFinder::processDeclare(const Module &M,
00222                                      const DbgDeclareInst *DDI) {
00223   auto *N = dyn_cast<MDNode>(DDI->getVariable());
00224   if (!N)
00225     return;
00226   InitializeTypeMap(M);
00227 
00228   auto *DV = dyn_cast<MDLocalVariable>(N);
00229   if (!DV)
00230     return;
00231 
00232   if (!NodesSeen.insert(DV).second)
00233     return;
00234   processScope(DV->getScope());
00235   processType(DV->getType().resolve(TypeIdentifierMap));
00236 }
00237 
00238 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
00239   auto *N = dyn_cast<MDNode>(DVI->getVariable());
00240   if (!N)
00241     return;
00242   InitializeTypeMap(M);
00243 
00244   auto *DV = dyn_cast<MDLocalVariable>(N);
00245   if (!DV)
00246     return;
00247 
00248   if (!NodesSeen.insert(DV).second)
00249     return;
00250   processScope(DV->getScope());
00251   processType(DV->getType().resolve(TypeIdentifierMap));
00252 }
00253 
00254 bool DebugInfoFinder::addType(MDType *DT) {
00255   if (!DT)
00256     return false;
00257 
00258   if (!NodesSeen.insert(DT).second)
00259     return false;
00260 
00261   TYs.push_back(const_cast<MDType *>(DT));
00262   return true;
00263 }
00264 
00265 bool DebugInfoFinder::addCompileUnit(MDCompileUnit *CU) {
00266   if (!CU)
00267     return false;
00268   if (!NodesSeen.insert(CU).second)
00269     return false;
00270 
00271   CUs.push_back(CU);
00272   return true;
00273 }
00274 
00275 bool DebugInfoFinder::addGlobalVariable(MDGlobalVariable *DIG) {
00276   if (!DIG)
00277     return false;
00278 
00279   if (!NodesSeen.insert(DIG).second)
00280     return false;
00281 
00282   GVs.push_back(DIG);
00283   return true;
00284 }
00285 
00286 bool DebugInfoFinder::addSubprogram(MDSubprogram *SP) {
00287   if (!SP)
00288     return false;
00289 
00290   if (!NodesSeen.insert(SP).second)
00291     return false;
00292 
00293   SPs.push_back(SP);
00294   return true;
00295 }
00296 
00297 bool DebugInfoFinder::addScope(MDScope *Scope) {
00298   if (!Scope)
00299     return false;
00300   // FIXME: Ocaml binding generates a scope with no content, we treat it
00301   // as null for now.
00302   if (Scope->getNumOperands() == 0)
00303     return false;
00304   if (!NodesSeen.insert(Scope).second)
00305     return false;
00306   Scopes.push_back(Scope);
00307   return true;
00308 }
00309 
00310 bool llvm::stripDebugInfo(Function &F) {
00311   bool Changed = false;
00312   for (BasicBlock &BB : F) {
00313     for (Instruction &I : BB) {
00314       if (I.getDebugLoc()) {
00315         Changed = true;
00316         I.setDebugLoc(DebugLoc());
00317       }
00318     }
00319   }
00320   return Changed;
00321 }
00322 
00323 bool llvm::StripDebugInfo(Module &M) {
00324   bool Changed = false;
00325 
00326   // Remove all of the calls to the debugger intrinsics, and remove them from
00327   // the module.
00328   if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
00329     while (!Declare->use_empty()) {
00330       CallInst *CI = cast<CallInst>(Declare->user_back());
00331       CI->eraseFromParent();
00332     }
00333     Declare->eraseFromParent();
00334     Changed = true;
00335   }
00336 
00337   if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
00338     while (!DbgVal->use_empty()) {
00339       CallInst *CI = cast<CallInst>(DbgVal->user_back());
00340       CI->eraseFromParent();
00341     }
00342     DbgVal->eraseFromParent();
00343     Changed = true;
00344   }
00345 
00346   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
00347          NME = M.named_metadata_end(); NMI != NME;) {
00348     NamedMDNode *NMD = NMI;
00349     ++NMI;
00350     if (NMD->getName().startswith("llvm.dbg.")) {
00351       NMD->eraseFromParent();
00352       Changed = true;
00353     }
00354   }
00355 
00356   for (Function &F : M)
00357     Changed |= stripDebugInfo(F);
00358 
00359   if (GVMaterializer *Materializer = M.getMaterializer())
00360     Materializer->setStripDebugInfo();
00361 
00362   return Changed;
00363 }
00364 
00365 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
00366   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
00367           M.getModuleFlag("Debug Info Version")))
00368     return Val->getZExtValue();
00369   return 0;
00370 }
00371 
00372 DenseMap<const llvm::Function *, MDSubprogram *>
00373 llvm::makeSubprogramMap(const Module &M) {
00374   DenseMap<const Function *, MDSubprogram *> R;
00375 
00376   NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
00377   if (!CU_Nodes)
00378     return R;
00379 
00380   for (MDNode *N : CU_Nodes->operands()) {
00381     auto *CUNode = cast<MDCompileUnit>(N);
00382     for (auto *SP : CUNode->getSubprograms()) {
00383       if (Function *F = SP->getFunction())
00384         R.insert(std::make_pair(F, SP));
00385     }
00386   }
00387   return R;
00388 }