Extract scope information from the variable itself, instead of relying on alloca or llvm.dbg.declare location.

While recording beginning of a function, use scope info from the first location entry instead of just relying on first location entry itself.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@83684 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 86532e8..f0579fe 100644
--- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -1784,23 +1784,19 @@
 }
 
 /// CollectVariableInfo - Populate DbgScope entries with variables' info.
-bool DwarfDebug::CollectVariableInfo() {
-  if (!MMI) return false;
-  bool ArgsCollected = false;
+void DwarfDebug::CollectVariableInfo() {
+  if (!MMI) return;
   MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
   for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
          VE = VMap.end(); VI != VE; ++VI) {
-    MDNode *Var = VI->first;
+    MetadataBase *MB = VI->first;
+    MDNode *Var = dyn_cast_or_null<MDNode>(MB);
     DIVariable DV (Var);
     if (DV.isNull()) continue;
-    if (DV.getTag() == dwarf::DW_TAG_arg_variable)
-      ArgsCollected = true;
-    DILocation VLoc(VI->second.first);
-    unsigned VSlot = VI->second.second;
-    DbgScope *Scope = getDbgScope(VLoc.getScope().getNode(), NULL);
+    unsigned VSlot = VI->second;
+    DbgScope *Scope = getDbgScope(DV.getContext().getNode(),  NULL);
     Scope->AddVariable(new DbgVariable(DV, VSlot, false));
   }
-  return ArgsCollected;
 }
 
 /// SetDbgScopeBeginLabels - Update DbgScope begin labels for the scopes that
@@ -1911,7 +1907,7 @@
 #ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
   if (!ExtractScopeInformation(MF))
     return;
-  bool ArgsCollected = CollectVariableInfo();
+  CollectVariableInfo();
 #endif
 
   // Begin accumulating function debug information.
@@ -1923,18 +1919,27 @@
   // Emit label for the implicitly defined dbg.stoppoint at the start of the
   // function.
 #ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
-  if (!ArgsCollected) {
-#else
-  if (1) {
-#endif
-    DebugLoc FDL = MF->getDefaultDebugLoc();
-    if (!FDL.isUnknown()) {
-      DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
-      unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.CompileUnit);
-      Asm->printLabel(LabelID);
-      O << '\n';
-    }
+  DebugLoc FDL = MF->getDefaultDebugLoc();
+  if (!FDL.isUnknown()) {
+    DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
+    unsigned LabelID = 0;
+    DISubprogram SP(DLT.CompileUnit);
+    if (!SP.isNull())
+      LabelID = RecordSourceLine(SP.getLineNumber(), 0, DLT.CompileUnit);
+    else
+      LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.CompileUnit);
+    Asm->printLabel(LabelID);
+    O << '\n';
   }
+#else
+  DebugLoc FDL = MF->getDefaultDebugLoc();
+  if (!FDL.isUnknown()) {
+    DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
+    unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, DLT.CompileUnit);
+    Asm->printLabel(LabelID);
+    O << '\n';
+  }
+#endif
   if (TimePassesIsEnabled)
     DebugTimer->stopTimer();
 }
@@ -1947,6 +1952,10 @@
   if (TimePassesIsEnabled)
     DebugTimer->startTimer();
 
+#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
+  if (DbgScopeMap.empty())
+    return;
+#endif
   // Define end label for subprogram.
   EmitLabel("func_end", SubprogramCount);