Revert "[llvm] Reapply "Prevent duplicate files in debug line header in dwarf 5.""

This reverts commit rL357020.

The commit broke the test llvm/test/tools/llvm-objdump/embedded-source.test
on some builds including clang-ppc64be-linux-multistage,
clang-s390x-linux, clang-with-lto-ubuntu, clang-x64-windows-msvc,
llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast (and others).

llvm-svn: 357026
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h
index 683d324..d9addb5 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h
@@ -101,7 +101,6 @@
   }
 
 public:
-  unsigned getDwarfVersion() const { return DD->getDwarfVersion(); }
   DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
                    DwarfDebug *DW, DwarfFile *DWU);
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 226014c..0e0a5b2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -319,7 +319,6 @@
     : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
       InfoHolder(A, "info_string", DIEValueAllocator),
       SkeletonHolder(A, "skel_string", DIEValueAllocator),
-      SplitTypeUnitFileTable(A->getDwarfVersion()),
       IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
   const Triple &TT = Asm->TM.getTargetTriple();
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 5d8e236..6e54746 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -397,6 +397,7 @@
     return;
 
   unsigned FileID = getOrCreateSourceID(File);
+  assert(FileID && "Invalid file id");
   addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
   addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
 }
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
index 6ad06a8..5342018 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
@@ -1017,28 +1017,14 @@
 }
 
 bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
-  uint16_t DwarfVersion = Prologue.getVersion();
-  assert(DwarfVersion != 0 && "LineTable has no dwarf version information");
-  if (DwarfVersion >= 5)
-    return FileIndex < Prologue.FileNames.size();
-  else
-    return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
-}
-const llvm::DWARFDebugLine::FileNameEntry &
-DWARFDebugLine::LineTable::getFileNameEntry(uint64_t Index) const {
-  uint16_t DwarfVersion = Prologue.getVersion();
-  assert(DwarfVersion != 0 && "LineTable has no dwarf version information");
-  if (DwarfVersion >= 5)
-    return Prologue.FileNames[Index];
-  else
-    return Prologue.FileNames[Index - 1];
+  return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
 }
 
 Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
                                                                 FileLineInfoKind Kind) const {
   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
     return None;
-  const FileNameEntry &Entry = getFileNameEntry(FileIndex);
+  const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
   if (Optional<const char *> source = Entry.Source.getAsCString())
     return StringRef(*source);
   return None;
@@ -1058,7 +1044,7 @@
                                                    std::string &Result) const {
   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
     return false;
-  const FileNameEntry &Entry = getFileNameEntry(FileIndex);
+  const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
   StringRef FileName = Entry.Name.getAsCString().getValue();
   if (Kind != FileLineInfoKind::AbsoluteFilePath ||
       isPathAbsoluteOnWindowsOrPosix(FileName)) {
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 348436f4..49fad13 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -603,8 +603,7 @@
                                            MD5::MD5Result *Checksum,
                                            Optional<StringRef> Source,
                                            unsigned CUID) {
-  MCDwarfLineTable &Table =
-      MCDwarfLineTablesCUMap.emplace(CUID, DwarfVersion).first->second;
+  MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
   return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
 }
 
@@ -613,7 +612,7 @@
 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
   const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
   if (FileNumber == 0)
-    return getDwarfVersion() >= 5;
+    return getDwarfVersion() >= 5 && LineTable.hasRootFile();
   if (FileNumber >= LineTable.getMCDwarfFiles().size())
     return false;
 
diff --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp
index 73b4d4b..83b6b4f 100644
--- a/llvm/lib/MC/MCDwarf.cpp
+++ b/llvm/lib/MC/MCDwarf.cpp
@@ -542,15 +542,6 @@
   return Header.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
 }
 
-bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory,
-                StringRef &FileName, MD5::MD5Result *Checksum) {
-  if (RootFile.Name.empty() || RootFile.Name != FileName.data())
-    return false;
-  if (!RootFile.Checksum)
-    return !Checksum;
-  return *RootFile.Checksum == *Checksum;
-}
-
 Expected<unsigned>
 MCDwarfLineTableHeader::tryGetFile(StringRef &Directory,
                                    StringRef &FileName,
@@ -570,8 +561,6 @@
     trackMD5Usage(Checksum);
     HasSource = (Source != None);
   }
-  if (isRootFile(RootFile, Directory, FileName, Checksum) && DwarfVersion >= 5)
-    return 0;
   if (FileNumber == 0) {
     // File numbers start with 1 and/or after any file numbers
     // allocated by inline-assembler .file directives.