+
+//===----------------------------------------------------------------------===//
+// DebugInfoFinder implementations.
+//===----------------------------------------------------------------------===//
+
+/// processModule - Process entire module and collect debug info.
+void DebugInfoFinder::processModule(Module &M) {
+
+
+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
+ for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
+ for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
+ ++BI) {
+ if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
+ processStopPoint(SPI);
+ else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
+ processFuncStart(FSI);
+ else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
+ processRegionStart(DRS);
+ else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
+ processRegionEnd(DRE);
+ else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
+ processDeclare(DDI);
+ }
+
+ NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
+ if (!NMD)
+ return;
+
+ for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
+ DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
+ if (addGlobalVariable(DIG)) {
+ addCompileUnit(DIG.getCompileUnit());
+ processType(DIG.getType());
+ }
+ }
+}
+
+/// processType - Process DIType.
+void DebugInfoFinder::processType(DIType DT) {
+ if (!addType(DT))
+ return;
+
+ addCompileUnit(DT.getCompileUnit());
+ if (DT.isCompositeType(DT.getTag())) {
+ DICompositeType DCT(DT.getNode());
+ processType(DCT.getTypeDerivedFrom());
+ DIArray DA = DCT.getTypeArray();
+ if (!DA.isNull())
+ for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
+ DIDescriptor D = DA.getElement(i);
+ DIType TypeE = DIType(D.getNode());
+ if (!TypeE.isNull())
+ processType(TypeE);
+ else
+ processSubprogram(DISubprogram(D.getNode()));
+ }
+ } else if (DT.isDerivedType(DT.getTag())) {
+ DIDerivedType DDT(DT.getNode());
+ if (!DDT.isNull())
+ processType(DDT.getTypeDerivedFrom());
+ }
+}
+
+/// processSubprogram - Process DISubprogram.
+void DebugInfoFinder::processSubprogram(DISubprogram SP) {
+ if (SP.isNull())
+ return;
+ if (!addSubprogram(SP))
+ return;
+ addCompileUnit(SP.getCompileUnit());
+ processType(SP.getType());
+}
+
+/// processStopPoint - Process DbgStopPointInst.
+void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
+ MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
+ addCompileUnit(DICompileUnit(Context));
+}
+
+/// processFuncStart - Process DbgFuncStartInst.
+void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
+ MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
+ processSubprogram(DISubprogram(SP));
+}
+
+/// processRegionStart - Process DbgRegionStart.
+void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
+ MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
+ processSubprogram(DISubprogram(SP));
+}
+
+/// processRegionEnd - Process DbgRegionEnd.
+void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
+ MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
+ processSubprogram(DISubprogram(SP));
+}
+
+/// processDeclare - Process DbgDeclareInst.
+void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
+ DIVariable DV(cast<MDNode>(DDI->getVariable()));
+ if (DV.isNull())
+ return;
+
+ if (!NodesSeen.insert(DV.getNode()))
+ return;
+
+ addCompileUnit(DV.getCompileUnit());
+ processType(DV.getType());
+}
+
+/// addType - Add type into Tys.
+bool DebugInfoFinder::addType(DIType DT) {
+ if (DT.isNull())
+ return false;
+
+ if (!NodesSeen.insert(DT.getNode()))
+ return false;
+
+ TYs.push_back(DT.getNode());
+ return true;
+}
+
+/// addCompileUnit - Add compile unit into CUs.
+bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
+ if (CU.isNull())
+ return false;
+
+ if (!NodesSeen.insert(CU.getNode()))
+ return false;
+
+ CUs.push_back(CU.getNode());
+ return true;
+}
+
+/// addGlobalVariable - Add global variable into GVs.
+bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
+ if (DIG.isNull())
+ return false;
+
+ if (!NodesSeen.insert(DIG.getNode()))
+ return false;
+
+ GVs.push_back(DIG.getNode());
+ return true;
+}
+
+// addSubprogram - Add subprgoram into SPs.
+bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
+ if (SP.isNull())
+ return false;
+
+ if (!NodesSeen.insert(SP.getNode()))
+ return false;
+
+ SPs.push_back(SP.getNode());
+ return true;
+}
+