Debug Info: Move the sorting and uniqueing of pieces from emitLocPieces()
into buildLocationList(). By keeping the list of Values sorted,
DebugLocEntry::Merge can also merge multi-piece entries.

llvm-svn: 215384
diff --git a/llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h b/llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h
index 97f48aa..41735fc 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h
@@ -76,6 +76,13 @@
       llvm_unreachable("unhandled EntryKind");
     }
 
+    // Compare two pieces based on their offset.
+    bool operator<(const Value &other) const {
+      DIVariable Var(Variable);
+      DIVariable OtherVar(other.Variable);
+      return Var.getPieceOffset() < OtherVar.getPieceOffset();
+    }
+
     bool isLocation() const { return EntryKind == E_Location; }
     bool isInt() const { return EntryKind == E_Integer; }
     bool isConstantFP() const { return EntryKind == E_ConstantFP; }
@@ -87,7 +94,7 @@
     const MDNode *getVariable() const { return Variable; }
   };
 private:
-  /// A list of locations/constants belonging to this entry.
+  /// A list of locations/constants belonging to this entry, sorted by offset.
   SmallVector<Value, 1> Values;
 
 public:
@@ -108,6 +115,7 @@
       if (Var.getName() == NextVar.getName() &&
           Var.isVariablePiece() && NextVar.isVariablePiece()) {
         Values.append(Next.Values.begin(), Next.Values.end());
+        sortUniqueValues();
         End = Next.End;
         return true;
       }
@@ -135,6 +143,14 @@
     assert(DIVariable(Val.Variable).isVariablePiece() &&
            "multi-value DebugLocEntries must be pieces");
     Values.push_back(Val);
+    sortUniqueValues();
+  }
+
+  // Sort the pieces by offset.
+  // Remove any duplicate entries by dropping all but the first.
+  void sortUniqueValues() {
+    std::sort(Values.begin(), Values.end());
+    Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
   }
 };