For PR411:
This patch replaces the SymbolTable class with ValueSymbolTable which does
not support types planes. This means that all symbol names in LLVM must now
be unique. The patch addresses the necessary changes to deal with this and
removes code no longer needed as a result. This completes the bulk of the
changes for this PR. Some cleanup patches will follow.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33918 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Bytecode/Reader/Analyzer.cpp b/lib/Bytecode/Reader/Analyzer.cpp
index 64907a8..71cb4b2 100644
--- a/lib/Bytecode/Reader/Analyzer.cpp
+++ b/lib/Bytecode/Reader/Analyzer.cpp
@@ -270,19 +270,15 @@
       *os << "      } END BLOCK: CompactionTable\n";
   }
 
-  virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
+  virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
     bca.numSymTab++;
     if (os)
-      *os << "    BLOCK: SymbolTable {\n";
+      *os << "    BLOCK: TypeSymbolTable {\n";
   }
-
-  virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
-    const Type* Typ) {
-    if (os) {
-      *os << "      Plane: Ty=" << Ty << " Size=" << NumEntries << " Type: ";
-      WriteTypeSymbolic(*os,Typ,M);
-      *os << "\n";
-    }
+  virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
+    bca.numSymTab++;
+    if (os)
+      *os << "    BLOCK: ValueSymbolTable {\n";
   }
 
   virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
@@ -292,18 +288,23 @@
          << " Name: " << name << "\n";
   }
 
-  virtual void handleSymbolTableValue(unsigned i, unsigned ValSlot,
-    const std::string& name ) {
+  virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot, 
+                                      const std::string& name) {
     if (os)
-      *os << "        Value " << i << " Slot=" << ValSlot
+      *os << "        Value " << TySlot << " Slot=" << ValSlot
          << " Name: " << name << "\n";
     if (ValSlot > bca.maxValueSlot)
       bca.maxValueSlot = ValSlot;
   }
 
-  virtual void handleSymbolTableEnd() {
+  virtual void handleValueSymbolTableEnd() {
     if (os)
-      *os << "    } END BLOCK: SymbolTable\n";
+      *os << "    } END BLOCK: ValueSymbolTable\n";
+  }
+
+  virtual void handleTypeSymbolTableEnd() {
+    if (os)
+      *os << "    } END BLOCK: TypeSymbolTable\n";
   }
 
   virtual void handleFunctionBegin(Function* Func, unsigned Size) {
@@ -358,15 +359,15 @@
   }
 
   virtual bool handleInstruction( unsigned Opcode, const Type* iType,
-                                std::vector<unsigned>& Operands, unsigned Size){
+                                std::vector<unsigned>& Operands, 
+                                Instruction *Inst,
+                                unsigned Size){
     if (os) {
       *os << "        INST: OpCode="
-         << Instruction::getOpcodeName(Opcode) << " Type=\"";
-      WriteTypeSymbolic(*os,iType,M);
-      *os << "\"";
+         << Instruction::getOpcodeName(Opcode);
       for ( unsigned i = 0; i < Operands.size(); ++i )
-        *os << " Op(" << i << ")=Slot(" << Operands[i] << ")";
-      *os << "\n";
+        *os << " Op(" << Operands[i] << ")";
+      *os << *Inst;
     }
 
     bca.numInstructions++;
diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp
index e2505cc..ff6d8f0 100644
--- a/lib/Bytecode/Reader/Reader.cpp
+++ b/lib/Bytecode/Reader/Reader.cpp
@@ -23,7 +23,6 @@
 #include "llvm/Constants.h"
 #include "llvm/InlineAsm.h"
 #include "llvm/Instructions.h"
-#include "llvm/SymbolTable.h"
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/Bytecode/Format.h"
 #include "llvm/Config/alloca.h"
@@ -55,6 +54,7 @@
 inline void BytecodeReader::error(const std::string& err) {
   ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos=" 
     + itostr(At-MemStart) + ")";
+  if (Handler) Handler->handleError(ErrorMsg);
   longjmp(context,1);
 }
 
@@ -443,10 +443,6 @@
   // of opcodes.
   Instruction* Result = 0;
 
-  // We have enough info to inform the handler now.
-  if (Handler) 
-    Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
-
   // First, handle the easy binary operators case
   if (Opcode >= Instruction::BinaryOpsBegin &&
       Opcode <  Instruction::BinaryOpsEnd  && Oprnds.size() == 2) {
@@ -861,6 +857,10 @@
   else
     TypeSlot = getTypeSlot(Result->getType());
 
+  // We have enough info to inform the handler now.
+  if (Handler) 
+    Handler->handleInstruction(Opcode, InstTy, Oprnds, Result, At-SaveAt);
+
   insertValue(Result, TypeSlot, FunctionValues);
 }
 
@@ -936,9 +936,9 @@
 /// CurrentFunction's symbol table. For Module level symbol tables, the
 /// CurrentFunction argument must be zero.
 void BytecodeReader::ParseValueSymbolTable(Function *CurrentFunction,
-                                           SymbolTable *ST) {
+                                           ValueSymbolTable *VST) {
                                       
-  if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
+  if (Handler) Handler->handleValueSymbolTableBegin(CurrentFunction,VST);
 
   // Allow efficient basic block lookup by number.
   std::vector<BasicBlock*> BBMap;
@@ -963,13 +963,15 @@
       } else {
         V = getValue(Typ, slot, false); // Find mapping...
       }
+      if (Handler) Handler->handleSymbolTableValue(Typ, slot, Name);
       if (V == 0)
-        error("Failed value look-up for name '" + Name + "'");
+        error("Failed value look-up for name '" + Name + "', type #" + 
+              utostr(Typ) + " slot #" + utostr(slot));
       V->setName(Name);
     }
   }
   checkPastBlockEnd("Symbol Table");
-  if (Handler) Handler->handleSymbolTableEnd();
+  if (Handler) Handler->handleValueSymbolTableEnd();
 }
 
 // Parse a single type. The typeid is read in first. If its a primitive type
diff --git a/lib/Bytecode/Reader/Reader.h b/lib/Bytecode/Reader/Reader.h
index 92c8fa0..b881f9d 100644
--- a/lib/Bytecode/Reader/Reader.h
+++ b/lib/Bytecode/Reader/Reader.h
@@ -28,8 +28,10 @@
 
 namespace llvm {
 
-class BytecodeHandler; ///< Forward declare the handler interface
-class TypeSymbolTable; ///< Forward declare
+// Forward declarations
+class BytecodeHandler; 
+class TypeSymbolTable; 
+class ValueSymbolTable; 
 
 /// This class defines the interface for parsing a buffer of bytecode. The
 /// parser itself takes no action except to call the various functions of
@@ -204,7 +206,7 @@
   void ParseTypeSymbolTable(TypeSymbolTable *ST);
 
   /// @brief Parse a value symbol table
-  void ParseValueSymbolTable(Function* Func, SymbolTable *ST);
+  void ParseValueSymbolTable(Function* Func, ValueSymbolTable *ST);
 
   /// @brief Parse functions lazily.
   void ParseFunctionLazily();
diff --git a/lib/Bytecode/Writer/SlotCalculator.cpp b/lib/Bytecode/Writer/SlotCalculator.cpp
index 9115ddb..197f206 100644
--- a/lib/Bytecode/Writer/SlotCalculator.cpp
+++ b/lib/Bytecode/Writer/SlotCalculator.cpp
@@ -21,9 +21,9 @@
 #include "llvm/InlineAsm.h"
 #include "llvm/Instructions.h"
 #include "llvm/Module.h"
-#include "llvm/SymbolTable.h"
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/Type.h"
+#include "llvm/ValueSymbolTable.h"
 #include "llvm/Analysis/ConstantsScanner.h"
 #include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/STLExtras.h"
@@ -218,8 +218,8 @@
 
 // processTypeSymbolTable - Insert all of the type sin the specified symbol
 // table.
-void SlotCalculator::processTypeSymbolTable(const TypeSymbolTable *ST) {
-  for (TypeSymbolTable::const_iterator TI = ST->begin(), TE = ST->end(); 
+void SlotCalculator::processTypeSymbolTable(const TypeSymbolTable *TST) {
+  for (TypeSymbolTable::const_iterator TI = TST->begin(), TE = TST->end(); 
        TI != TE; ++TI )
     getOrCreateSlot(TI->second);
 }
@@ -227,23 +227,18 @@
 // processSymbolTable - Insert all of the values in the specified symbol table
 // into the values table...
 //
-void SlotCalculator::processValueSymbolTable(const SymbolTable *ST) {
-  for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
-       PE = ST->plane_end(); PI != PE; ++PI)
-    for (SymbolTable::value_const_iterator VI = PI->second.begin(),
-           VE = PI->second.end(); VI != VE; ++VI)
-      getOrCreateSlot(VI->second);
+void SlotCalculator::processValueSymbolTable(const ValueSymbolTable *VST) {
+  for (ValueSymbolTable::const_iterator VI = VST->begin(), VE = VST->end(); 
+       VI != VE; ++VI)
+    getOrCreateSlot(VI->second);
 }
 
-void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
+void SlotCalculator::processSymbolTableConstants(const ValueSymbolTable *VST) {
   // Now do the constant values in all planes
-  for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
-       PE = ST->plane_end(); PI != PE; ++PI)
-    for (SymbolTable::value_const_iterator VI = PI->second.begin(),
-           VE = PI->second.end(); VI != VE; ++VI)
-      if (isa<Constant>(VI->second) &&
-          !isa<GlobalValue>(VI->second))
-        getOrCreateSlot(VI->second);
+  for (ValueSymbolTable::const_iterator VI = VST->begin(), VE = VST->end(); 
+       VI != VE; ++VI)
+    if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
+      getOrCreateSlot(VI->second);
 }
 
 
diff --git a/lib/Bytecode/Writer/SlotCalculator.h b/lib/Bytecode/Writer/SlotCalculator.h
index 8200992..6cddb30 100644
--- a/lib/Bytecode/Writer/SlotCalculator.h
+++ b/lib/Bytecode/Writer/SlotCalculator.h
@@ -31,6 +31,7 @@
 class Function;
 class SymbolTable;
 class TypeSymbolTable;
+class ValueSymbolTable;
 class ConstantArray;
 
 class SlotCalculator {
@@ -130,8 +131,8 @@
   // into the values table...
   //
   void processTypeSymbolTable(const TypeSymbolTable *ST);
-  void processValueSymbolTable(const SymbolTable *ST);
-  void processSymbolTableConstants(const SymbolTable *ST);
+  void processValueSymbolTable(const ValueSymbolTable *ST);
+  void processSymbolTableConstants(const ValueSymbolTable *ST);
 
   // insertPrimitives - helper for constructors to insert primitive types.
   void insertPrimitives();
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
index 0d4ccbd..a2e8fe5 100644
--- a/lib/Bytecode/Writer/Writer.cpp
+++ b/lib/Bytecode/Writer/Writer.cpp
@@ -26,8 +26,8 @@
 #include "llvm/InlineAsm.h"
 #include "llvm/Instructions.h"
 #include "llvm/Module.h"
-#include "llvm/SymbolTable.h"
 #include "llvm/TypeSymbolTable.h"
+#include "llvm/ValueSymbolTable.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/Compressor.h"
 #include "llvm/Support/MathExtras.h"
@@ -1144,21 +1144,31 @@
   }
 }
 
-void BytecodeWriter::outputValueSymbolTable(const SymbolTable &MST) {
+void BytecodeWriter::outputValueSymbolTable(const ValueSymbolTable &VST) {
   // Do not output the Bytecode block for an empty symbol table, it just wastes
   // space!
-  if (MST.isEmpty()) return;
+  if (VST.empty()) return;
 
   BytecodeBlock SymTabBlock(BytecodeFormat::ValueSymbolTableBlockID, *this,
                             true/*ElideIfEmpty*/);
 
-  // Now do each of the type planes in order.
-  for (SymbolTable::plane_const_iterator PI = MST.plane_begin(),
-       PE = MST.plane_end(); PI != PE;  ++PI) {
-    SymbolTable::value_const_iterator I = MST.value_begin(PI->first);
-    SymbolTable::value_const_iterator End = MST.value_end(PI->first);
+  // Organize the symbol table by type
+  typedef std::pair<std::string, const Value*> PlaneMapEntry;
+  typedef std::vector<PlaneMapEntry> PlaneMapVector;
+  typedef std::map<const Type*, PlaneMapVector > PlaneMap;
+  PlaneMap Planes;
+  for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
+       SI != SE; ++SI) 
+    Planes[SI->second->getType()].push_back(
+        std::make_pair(SI->first,SI->second));
+
+  for (PlaneMap::const_iterator PI = Planes.begin(), PE = Planes.end();
+       PI != PE; ++PI) {
     int Slot;
 
+    PlaneMapVector::const_iterator I = PI->second.begin(); 
+    PlaneMapVector::const_iterator End = PI->second.end(); 
+
     if (I == End) continue;  // Don't mess with an absent type...
 
     // Write the number of values in this plane
diff --git a/lib/Bytecode/Writer/WriterInternals.h b/lib/Bytecode/Writer/WriterInternals.h
index c518c01..f3c59f3 100644
--- a/lib/Bytecode/Writer/WriterInternals.h
+++ b/lib/Bytecode/Writer/WriterInternals.h
@@ -26,6 +26,7 @@
 namespace llvm {
   class InlineAsm;
   class TypeSymbolTable;
+  class ValueSymbolTable;
 
 class BytecodeWriter {
   std::vector<unsigned char> &Out;
@@ -66,7 +67,7 @@
 
   void outputModuleInfoBlock(const Module *C);
   void outputTypeSymbolTable(const TypeSymbolTable &TST);
-  void outputValueSymbolTable(const SymbolTable &ST);
+  void outputValueSymbolTable(const ValueSymbolTable &ST);
   void outputTypes(unsigned StartNo);
   void outputConstantsInPlane(const std::vector<const Value*> &Plane,
                               unsigned StartNo);