Do a better job at emitting prefrabricated skeleton CUs.

This is a better fix than r308708 for the problem introduced in
r304020. It restores the skeleton CU testcases modified by that commit
to their original form and most importantly ensures that
frontend-generated skeleton CUs (such as used to point to Clang
modules) come after the regular CUs. This broke for DICompileUnit
nodes that don't have any immediate children because they are now
constructed lazily instead of the order in which they are listed in
!llvm.dbg.cu. After this commit we still don't guarantee that order,
but we do guarantee that empty skeletons come last.

Shipping versions of LLDB are very sensitive to the ordering of
CUs. I'll track a fix for LLDB to be more permissive separately.
This fixes a test failure in the LLDB testsuite.

rdar://problem/33357252

llvm-svn: 309154
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 9533d61..70fe1e2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -520,6 +520,14 @@
   return GVEs;
 }
 
+static bool isEmptyCU(DICompileUnit *CUNode) {
+  return CUNode->getEnumTypes().empty() &&
+         CUNode->getRetainedTypes().empty() &&
+         CUNode->getGlobalVariables().empty() &&
+         CUNode->getImportedEntities().empty() &&
+         CUNode->getMacros().empty();
+}
+
 // Emit all Dwarf sections that should come prior to the content. Create
 // global DIEs and emit initial debug info sections. This is invoked by
 // the target AsmPrinter.
@@ -546,10 +554,7 @@
   }
 
   for (DICompileUnit *CUNode : M->debug_compile_units()) {
-    if (CUNode->getEnumTypes().empty() && CUNode->getRetainedTypes().empty() &&
-        CUNode->getGlobalVariables().empty() &&
-        CUNode->getImportedEntities().empty() && CUNode->getMacros().empty() &&
-        !CUNode->getDWOId())
+    if (isEmptyCU(CUNode))
       continue;
 
     DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
@@ -683,6 +688,11 @@
                         TLOF.getDwarfMacinfoSection()->getBeginSymbol());
   }
 
+  // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
+  for (auto *CUNode : MMI->getModule()->debug_compile_units())
+    if (CUNode->getDWOId() && isEmptyCU(CUNode))
+      getOrCreateDwarfCompileUnit(CUNode);
+
   // Compute DIE offsets and sizes.
   InfoHolder.computeSizeAndOffsets();
   if (useSplitDwarf())
diff --git a/llvm/test/DebugInfo/Generic/skeletoncu.ll b/llvm/test/DebugInfo/Generic/skeletoncu.ll
index b9761b2..1bdfe1f 100644
--- a/llvm/test/DebugInfo/Generic/skeletoncu.ll
+++ b/llvm/test/DebugInfo/Generic/skeletoncu.ll
@@ -14,4 +14,3 @@
 !4 = !{i32 2, !"Debug Info Version", i32 3}
 !5 = !{!6}
 !6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
-
diff --git a/llvm/test/DebugInfo/dwo.ll b/llvm/test/DebugInfo/dwo.ll
index b6de943..7b02c3c 100644
--- a/llvm/test/DebugInfo/dwo.ll
+++ b/llvm/test/DebugInfo/dwo.ll
@@ -1,17 +1,23 @@
 ; RUN: %llc_dwarf %s -filetype=obj -o %t
 ; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s
+; REQUIRES: default_triple
+;
+; CHECK: DW_TAG_compile_unit
+; CHECK-NOT: dwo_id
+;
+; The skeleton must come second or LLDB may get confused.
 ; CHECK: DW_TAG_compile_unit
 ; CHECK: DW_AT_GNU_dwo_id {{.*}}abcd
-; CHECK-NOT: DW_AT_GNU_dwo_name
-; REQUIRES: default_triple
+; CHECK: DW_AT_GNU_dwo_name {{.*}}"my.dwo"
 
-!llvm.dbg.cu = !{!0}
+!llvm.dbg.cu = !{!7, !0}
 !llvm.module.flags = !{!3, !4}
 
-!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "LLVM", isOptimized: false, runtimeVersion: 2, emissionKind: FullDebug, enums: !2, retainedTypes: !5, globals: !2, imports: !2, dwoId: 43981)
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Clang", isOptimized: false, runtimeVersion: 2, splitDebugFilename: "my.dwo", emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !2, dwoId: 43981)
 !1 = !DIFile(filename: "<stdin>", directory: "/")
 !2 = !{}
 !3 = !{i32 2, !"Dwarf Version", i32 4}
 !4 = !{i32 2, !"Debug Info Version", i32 3}
 !5 = !{!6}
 !6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Clang", isOptimized: false, runtimeVersion: 2, emissionKind: FullDebug, retainedTypes: !5)
diff --git a/llvm/test/DebugInfo/skeletoncu.ll b/llvm/test/DebugInfo/skeletoncu.ll
index 4c96d82..a90c8b3 100644
--- a/llvm/test/DebugInfo/skeletoncu.ll
+++ b/llvm/test/DebugInfo/skeletoncu.ll
@@ -8,11 +8,9 @@
 !llvm.dbg.cu = !{!0}
 !llvm.module.flags = !{!3, !4}
 
-!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "LLVM", isOptimized: false, runtimeVersion: 2, splitDebugFilename: "my.dwo", emissionKind: FullDebug, enums: !2, retainedTypes: !6, globals: !2, imports: !2, dwoId: 43981)
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "LLVM", isOptimized: false, runtimeVersion: 2, splitDebugFilename: "my.dwo", emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !2, dwoId: 43981)
 !1 = !DIFile(filename: "<stdin>", directory: "/")
 !2 = !{}
 !3 = !{i32 2, !"Dwarf Version", i32 4}
 !4 = !{i32 2, !"Debug Info Version", i32 3}
-!5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
-!6 = !{!5}