Ensure deterministic when printing ARM assembler constant pools
authorDavid Peixotto <dpeixott@codeaurora.org>
Thu, 19 Dec 2013 22:41:56 +0000 (22:41 +0000)
committerDavid Peixotto <dpeixott@codeaurora.org>
Thu, 19 Dec 2013 22:41:56 +0000 (22:41 +0000)
We dump any non-empty assembler constant pools after a successful
parse of an assembly file that uses the ldr pseudo opcode. These
per-section constant pools should be output in a deterministic order
to ensure that we always generate the same output when printing the
output with an AsmStreamer.

This patch changes the map data struture used to associate a section
with its constant pool to a MapVector to ensure deterministic
output. Because this map type does not support deletion, we now
check that the constant pool is not empty before dumping its entries
and clear the entries after emitting them with the streamer.

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

lib/Target/ARM/AsmParser/ARMAsmParser.cpp

index f794e21b2b04dd2fc0b7743b596aa7ff47a40dbc..e67311f655de92f7f02d4f64a4105d305a92637d 100644 (file)
@@ -16,6 +16,7 @@
 #include "MCTargetDesc/ARMBaseInfo.h"
 #include "MCTargetDesc/ARMMCExpr.h"
 #include "llvm/ADT/BitVector.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
@@ -73,7 +74,9 @@ public:
   }
 
   // Emit the contents of the constant pool using the provided streamer.
-  void emitEntries(MCStreamer &Streamer) const {
+  void emitEntries(MCStreamer &Streamer) {
+    if (Entries.empty())
+      return;
     Streamer.EmitCodeAlignment(4); // align to 4-byte address
     Streamer.EmitDataRegion(MCDR_DataRegion);
     for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
@@ -82,6 +85,12 @@ public:
       Streamer.EmitValue(I->second, 4);
     }
     Streamer.EmitDataRegion(MCDR_DataRegionEnd);
+    Entries.clear();
+  }
+
+  // Return true if the constant pool is empty
+  bool empty() {
+    return Entries.empty();
   }
 };
 
@@ -93,7 +102,13 @@ public:
 // an opcode to the ldr. After we have parsed all the user input we
 // output the (label, value) pairs in each constant pool at the end of the
 // section.
-typedef std::map<const MCSection *, ConstantPool> ConstantPoolMapTy;
+//
+// We use the MapVector for the map type to ensure stable iteration of
+// the sections at the end of the parse. We need to iterate over the
+// sections in a stable order to ensure that we have print the
+// constant pools in a deterministic order when printing an assembly
+// file.
+typedef MapVector<const MCSection *, ConstantPool> ConstantPoolMapTy;
 
 class ARMAsmParser : public MCTargetAsmParser {
   MCSubtargetInfo &STI;
@@ -115,10 +130,6 @@ class ARMAsmParser : public MCTargetAsmParser {
     return ConstantPools[Section];
   }
 
-  void destroyConstantPool(const MCSection *Section) {
-    ConstantPools.erase(Section);
-  }
-
   ARMTargetStreamer &getTargetStreamer() {
     MCTargetStreamer &TS = getParser().getStreamer().getTargetStreamer();
     return static_cast<ARMTargetStreamer &>(TS);
@@ -8459,9 +8470,8 @@ bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
   const MCSection *Section = Streamer.getCurrentSection().first;
 
   if (ConstantPool *CP = getConstantPool(Section)) {
-    CP->emitEntries(Streamer);
-    CP = 0;
-    destroyConstantPool(Section);
+    if (!CP->empty())
+      CP->emitEntries(Streamer);
   }
   return false;
 }
@@ -8504,8 +8514,10 @@ void ARMAsmParser::finishParse() {
     const MCSection *Section = CPI->first;
     ConstantPool &CP = CPI->second;
 
-    // Dump assembler constant pools at the end of the section.
-    Streamer.SwitchSection(Section);
-    CP.emitEntries(Streamer);
+    // Dump non-empty assembler constant pools at the end of the section.
+    if (!CP.empty()) {
+      Streamer.SwitchSection(Section);
+      CP.emitEntries(Streamer);
+    }
   }
 }