Support DWARF discriminators in object streamer.

Summary:
This adds support for emitting DWARF path discriminator values in
the object streamer. It also changes the DWARF dumper to show
discriminator values in the line table output.

Reviewers: echristo

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D2794

llvm-svn: 201427
diff --git a/llvm/lib/DebugInfo/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARFDebugLine.cpp
index 13d09dd..b2c8502 100644
--- a/llvm/lib/DebugInfo/DWARFDebugLine.cpp
+++ b/llvm/lib/DebugInfo/DWARFDebugLine.cpp
@@ -62,6 +62,7 @@
   Column = 0;
   File = 1;
   Isa = 0;
+  Discriminator = 0;
   IsStmt = default_is_stmt;
   BasicBlock = false;
   EndSequence = false;
@@ -71,7 +72,7 @@
 
 void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
   OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
-     << format(" %6u %3u ", File, Isa)
+     << format(" %6u %3u %13u ", File, Isa, Discriminator)
      << (IsStmt ? " is_stmt" : "")
      << (BasicBlock ? " basic_block" : "")
      << (PrologueEnd ? " prologue_end" : "")
@@ -85,8 +86,9 @@
   OS << '\n';
 
   if (!Rows.empty()) {
-    OS << "Address            Line   Column File   ISA Flags\n"
-       << "------------------ ------ ------ ------ --- -------------\n";
+    OS << "Address            Line   Column File   ISA Discriminator Flags\n"
+       << "------------------ ------ ------ ------ --- ------------- "
+          "-------------\n";
     for (std::vector<Row>::const_iterator pos = Rows.begin(),
          end = Rows.end(); pos != end; ++pos)
       pos->dump(OS);
@@ -311,6 +313,10 @@
         }
         break;
 
+      case DW_LNE_set_discriminator:
+        state.Discriminator = debug_line_data.getULEB128(offset_ptr);
+        break;
+
       default:
         // Length doesn't include the zero opcode byte or the length itself, but
         // it does include the sub_opcode, so we have to adjust for that below
diff --git a/llvm/lib/DebugInfo/DWARFDebugLine.h b/llvm/lib/DebugInfo/DWARFDebugLine.h
index 2990756..9e93cc8 100644
--- a/llvm/lib/DebugInfo/DWARFDebugLine.h
+++ b/llvm/lib/DebugInfo/DWARFDebugLine.h
@@ -112,6 +112,9 @@
     // An unsigned integer whose value encodes the applicable instruction set
     // architecture for the current instruction.
     uint8_t Isa;
+    // An unsigned integer representing the DWARF path discriminator value
+    // for this location.
+    uint32_t Discriminator;
     // A boolean indicating that the current instruction is the beginning of a
     // statement.
     uint8_t IsStmt:1,
diff --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp
index fe20a6d..d52c7a7 100644
--- a/llvm/lib/MC/MCDwarf.cpp
+++ b/llvm/lib/MC/MCDwarf.cpp
@@ -137,6 +137,7 @@
   unsigned Column = 0;
   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
   unsigned Isa = 0;
+  unsigned Discriminator = 0;
   MCSymbol *LastLabel = NULL;
 
   // Loop through each MCLineEntry and encode the dwarf line number table.
@@ -154,6 +155,15 @@
       MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
       MCOS->EmitULEB128IntValue(Column);
     }
+    if (Discriminator != it->getDiscriminator()) {
+      Discriminator = it->getDiscriminator();
+      unsigned Size =
+          MCOS->getContext().getAsmInfo()->getULEB128Size(Discriminator);
+      MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
+      MCOS->EmitULEB128IntValue(Size + 1);
+      MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
+      MCOS->EmitULEB128IntValue(Discriminator);
+    }
     if (Isa != it->getIsa()) {
       Isa = it->getIsa();
       MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);