Refactor code that finds context for a given die.
authorDevang Patel <dpatel@apple.com>
Thu, 10 Dec 2009 19:14:49 +0000 (19:14 +0000)
committerDevang Patel <dpatel@apple.com>
Thu, 10 Dec 2009 19:14:49 +0000 (19:14 +0000)
Create global variable DIEs after creating subprogram DIEs. This allows function level static variable's to find their context at the time of DIE creation.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91055 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/DwarfDebug.cpp
lib/CodeGen/AsmPrinter/DwarfDebug.h

index 21c7dcb227c586cd0998f12f2781d44f4bdc76d5..c7b3cfed87184125958cf3057ebf773fe668d45d 100644 (file)
@@ -738,6 +738,19 @@ void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
   addBlock(Die, Attribute, 0, Block);
 }
 
+/// addToContextOwner - Add Die into the list of its context owner's children.
+void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
+  if (Context.isNull())
+    ModuleCU->addDie(Die);
+  else if (Context.isType()) {
+    DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
+    ContextDIE->addChild(Die);
+  } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
+    ContextDIE->addChild(Die);
+  else 
+    ModuleCU->addDie(Die);
+}
+
 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
 /// given DIType.
 DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
@@ -757,18 +770,7 @@ DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
     constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
   }
 
-  DIDescriptor Context = Ty.getContext();
-  if (Context.isNull())
-    // Add this type into the module cu.
-    ModuleCU->addDie(TyDIE);
-  else if (Context.isType()) {
-    DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
-    ContextDIE->addChild(TyDIE);
-  } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
-    ContextDIE->addChild(TyDIE);
-  else 
-    ModuleCU->addDie(TyDIE);
-
+  addToContextOwner(TyDIE, Ty.getContext());
   return TyDIE;
 }
 
@@ -1317,19 +1319,6 @@ DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
  if (!DISubprogram(SPNode).isLocalToUnit())
    addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
 
- // If there are global variables at this scope then add their dies.
- for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
-        SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
-   MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
-   if (!N) continue;
-   DIGlobalVariable GV(N);
-   if (GV.getContext().getNode() == SPNode) {
-     DIE *ScopedGVDie = createGlobalVariableDIE(GV);
-     if (ScopedGVDie)
-       SPDie->addChild(ScopedGVDie);
-   }
- }
  return SPDie;
 }
 
@@ -1648,9 +1637,8 @@ void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
   ModuleCU->insertDIE(N, VariableDie);
 
   // Add to context owner.
-  if (TopLevelDIEs.insert(VariableDie))
-    TopLevelDIEsVector.push_back(VariableDie);
-
+  addToContextOwner(VariableDie, DI_GV.getContext());
+  
   // Expose as global. FIXME - need to check external flag.
   ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
 
@@ -1723,21 +1711,16 @@ void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
   if (!ModuleCU)
     ModuleCU = CompileUnits[0];
 
-  // Create DIEs for each of the externally visible global variables.
-  for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
-         E = DbgFinder.global_variable_end(); I != E; ++I) {
-    DIGlobalVariable GV(*I);
-    if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
-      ScopedGVs.push_back(*I);
-    else
-      constructGlobalVariableDIE(*I);
-  }
-
   // Create DIEs for each subprogram.
   for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
          E = DbgFinder.subprogram_end(); I != E; ++I)
     constructSubprogramDIE(*I);
 
+  // Create DIEs for each global variable.
+  for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
+         E = DbgFinder.global_variable_end(); I != E; ++I)
+    constructGlobalVariableDIE(*I);
+
   MMI = mmi;
   shouldEmit = true;
   MMI->setDebugInfoAvailability(true);
index 580262039de3c78b4b8fc46513a488e42fc5cf5a..38bfaf6fe91e9e2693525397f0598ae0ba7d3789 100644 (file)
@@ -163,10 +163,6 @@ class DwarfDebug : public Dwarf {
   SmallPtrSet<DIE *, 4> TopLevelDIEs;
   SmallVector<DIE *, 4> TopLevelDIEsVector;
 
-  /// ScopedGVs - Tracks global variables that are not at file scope.
-  /// For example void f() { static int b = 42; }
-  SmallVector<WeakVH, 4> ScopedGVs;
-
   typedef SmallVector<DbgScope *, 2> ScopeVector;
   typedef DenseMap<const MachineInstr *, ScopeVector>
     InsnToDbgScopeMapTy;
@@ -313,6 +309,9 @@ class DwarfDebug : public Dwarf {
   void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
                             const MachineLocation &Location);
 
+  /// addToContextOwner - Add Die into the list of its context owner's children.
+  void addToContextOwner(DIE *Die, DIDescriptor Context);
+
   /// addType - Add a new type attribute to the specified entity.
   void addType(DIE *Entity, DIType Ty);