[llvm-commits] CVS: llvm/lib/Bytecode/Writer/SlotCalculator.cpp SlotCalculator.h Writer.cpp

Reid Spencer reid at x10sys.com
Thu Jan 11 23:06:07 PST 2007



Changes in directory llvm/lib/Bytecode/Writer:

SlotCalculator.cpp updated: 1.78 -> 1.79
SlotCalculator.h updated: 1.24 -> 1.25
Writer.cpp updated: 1.145 -> 1.146
---
Log message:

For PR1064: http://llvm.org/PR1064 :
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8, 
16, 32, and 64 bit integers.  

This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
   bits in an integer. The Type classes SubclassData field is used to
   store the number of bits. This allows 2^23 bits in an integer type. 
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
   64-bit integers. These are replaced with just IntegerType which is not
   a primitive any more. 
3. Adjust the rest of LLVM to account for this change.

Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with 
them in any significant way. Most optimization passes, for example, will 
still only deal with the byte-width integer types.  Future increments
will rectify this situation.



---
Diffs of the changes:  (+84 -56)

 SlotCalculator.cpp |   75 +++++++++++++++++++++++++++++++++--------------------
 SlotCalculator.h   |    3 ++
 Writer.cpp         |   62 +++++++++++++++++++++++--------------------
 3 files changed, 84 insertions(+), 56 deletions(-)


Index: llvm/lib/Bytecode/Writer/SlotCalculator.cpp
diff -u llvm/lib/Bytecode/Writer/SlotCalculator.cpp:1.78 llvm/lib/Bytecode/Writer/SlotCalculator.cpp:1.79
--- llvm/lib/Bytecode/Writer/SlotCalculator.cpp:1.78	Sat Jan  6 01:24:43 2007
+++ llvm/lib/Bytecode/Writer/SlotCalculator.cpp	Fri Jan 12 01:05:13 2007
@@ -31,26 +31,45 @@
 #include <functional>
 using namespace llvm;
 
-#if 0
+#ifndef NDEBUG
 #include "llvm/Support/Streams.h"
-#define SC_DEBUG(X) cerr << X
+#include "llvm/Support/CommandLine.h"
+static cl::opt<bool> SlotCalculatorDebugOption("scdebug",cl::init(false), 
+    cl::desc("Enable SlotCalculator debug output"), cl::Hidden);
+#define SC_DEBUG(X) if (SlotCalculatorDebugOption) cerr << X
 #else
 #define SC_DEBUG(X)
 #endif
 
+void SlotCalculator::insertPrimitives() {
+  // Preload the table with the built-in types. These built-in types are
+  // inserted first to ensure that they have low integer indices which helps to
+  // keep bytecode sizes small. Note that the first group of indices must match
+  // the Type::TypeIDs for the primitive types. After that the integer types are
+  // added, but the order and value is not critical. What is critical is that 
+  // the indices of these "well known" slot numbers be properly maintained in
+  // Reader.h which uses them directly to extract values of these types.
+  SC_DEBUG("Inserting primitive types:\n");
+                                    // See WellKnownTypeSlots in Reader.h
+  insertType(Type::VoidTy,   true); // 0: VoidTySlot
+  insertType(Type::FloatTy,  true); // 1: FloatTySlot
+  insertType(Type::DoubleTy, true); // 2: DoubleTySlot
+  insertType(Type::LabelTy,  true); // 3: LabelTySlot
+  assert(TypeMap.size() == Type::FirstDerivedTyID && "Invalid primitive insert");
+  // Above here *must* correspond 1:1 with the primitive types.
+  insertType(Type::Int1Ty,   true); // 4: BoolTySlot
+  insertType(Type::Int8Ty,   true); // 5: Int8TySlot
+  insertType(Type::Int16Ty,  true); // 6: Int16TySlot
+  insertType(Type::Int32Ty,  true); // 7: Int32TySlot
+  insertType(Type::Int64Ty,  true); // 8: Int64TySlot
+}
+
 SlotCalculator::SlotCalculator(const Module *M ) {
   ModuleContainsAllFunctionConstants = false;
   ModuleTypeLevel = 0;
   TheModule = M;
 
-  // Preload table... Make sure that all of the primitive types are in the table
-  // and that their Primitive ID is equal to their slot #
-  //
-  SC_DEBUG("Inserting primitive types:\n");
-  for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
-    assert(Type::getPrimitiveType((Type::TypeID)i));
-    insertType(Type::getPrimitiveType((Type::TypeID)i), true);
-  }
+  insertPrimitives();
 
   if (M == 0) return;   // Empty table...
   processModule();
@@ -60,14 +79,7 @@
   ModuleContainsAllFunctionConstants = false;
   TheModule = M ? M->getParent() : 0;
 
-  // Preload table... Make sure that all of the primitive types are in the table
-  // and that their Primitive ID is equal to their slot #
-  //
-  SC_DEBUG("Inserting primitive types:\n");
-  for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
-    assert(Type::getPrimitiveType((Type::TypeID)i));
-    insertType(Type::getPrimitiveType((Type::TypeID)i), true);
-  }
+  insertPrimitives();
 
   if (TheModule == 0) return;   // Empty table...
 
@@ -423,15 +435,14 @@
 /// getOrCreateCompactionTableSlot - This method is used to build up the initial
 /// approximation of the compaction table.
 unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
-  std::map<const Type*, unsigned>::iterator I =
-    CompactionTypeMap.lower_bound(T);
+  CompactionTypeMapType::iterator I = CompactionTypeMap.lower_bound(T);
   if (I != CompactionTypeMap.end() && I->first == T)
     return I->second;  // Already exists?
 
   unsigned SlotNo = CompactionTypes.size();
-  SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
+  SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << *T << "\n");
   CompactionTypes.push_back(T);
-  CompactionTypeMap.insert(std::make_pair(T, SlotNo));
+  CompactionTypeMap[T] = SlotNo; 
   return SlotNo;
 }
 
@@ -452,6 +463,16 @@
     CompactionTypes.push_back(PrimTy);
     CompactionTypeMap[PrimTy] = i;
   }
+  CompactionTypeMap[Type::Int1Ty] = CompactionTypes.size();
+  CompactionTypes.push_back(Type::Int1Ty);
+  CompactionTypeMap[Type::Int8Ty] = CompactionTypes.size();
+  CompactionTypes.push_back(Type::Int8Ty);
+  CompactionTypeMap[Type::Int16Ty] = CompactionTypes.size();
+  CompactionTypes.push_back(Type::Int16Ty);
+  CompactionTypeMap[Type::Int32Ty] = CompactionTypes.size();
+  CompactionTypes.push_back(Type::Int32Ty);
+  CompactionTypeMap[Type::Int64Ty] = CompactionTypes.size();
+  CompactionTypes.push_back(Type::Int64Ty);
 
   // Next, include any types used by function arguments.
   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
@@ -485,7 +506,7 @@
     if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
         i != Type::LabelTyID) {
       const Type *Ty = CompactionTypes[i];
-      SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
+      SC_DEBUG("Getting Null Value #" << i << " for Type " << *Ty << "\n");
       assert(Ty->getTypeID() != Type::VoidTyID);
       assert(Ty->getTypeID() != Type::LabelTyID);
       getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
@@ -618,7 +639,8 @@
 /// to determine if its actually empty.
 bool SlotCalculator::CompactionTableIsEmpty() const {
   // Check a degenerate case, just in case.
-  if (CompactionTable.size() == 0) return true;
+  if (CompactionTable.size() == 0) 
+    return true;
 
   // Check each plane
   for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
@@ -830,7 +852,7 @@
   unsigned DestSlot = NodeMap[D] = Table[Ty].size();
   Table[Ty].push_back(D);
 
-  SC_DEBUG("  Inserting value [" << Ty << "] = " << D << " slot=" <<
+  SC_DEBUG("  Inserting value [" << Ty << "] = " << *D << " slot=" <<
            DestSlot << " [");
   // G = Global, C = Constant, T = Type, F = Function, o = other
   SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
@@ -848,7 +870,6 @@
   unsigned DestSlot = TypeMap[Ty] = Types.size();
   Types.push_back(Ty);
 
-  SC_DEBUG("  Inserting type [" << DestSlot << "] = " << Ty << "\n" );
+  SC_DEBUG("  Inserting type [" << DestSlot << "] = " << *Ty << "\n" );
   return (int)DestSlot;
 }
-


Index: llvm/lib/Bytecode/Writer/SlotCalculator.h
diff -u llvm/lib/Bytecode/Writer/SlotCalculator.h:1.24 llvm/lib/Bytecode/Writer/SlotCalculator.h:1.25
--- llvm/lib/Bytecode/Writer/SlotCalculator.h:1.24	Sat Jan  6 01:24:43 2007
+++ llvm/lib/Bytecode/Writer/SlotCalculator.h	Fri Jan 12 01:05:13 2007
@@ -177,6 +177,9 @@
   unsigned getOrCreateCompactionTableSlot(const Value *V);
   unsigned getOrCreateCompactionTableSlot(const Type *V);
   void pruneCompactionTable();
+
+  // insertPrimitives - helper for constructors to insert primitive types.
+  void insertPrimitives();
 };
 
 } // End llvm namespace


Index: llvm/lib/Bytecode/Writer/Writer.cpp
diff -u llvm/lib/Bytecode/Writer/Writer.cpp:1.145 llvm/lib/Bytecode/Writer/Writer.cpp:1.146
--- llvm/lib/Bytecode/Writer/Writer.cpp:1.145	Thu Jan 11 22:24:45 2007
+++ llvm/lib/Bytecode/Writer/Writer.cpp	Fri Jan 12 01:05:13 2007
@@ -200,16 +200,18 @@
 void BytecodeWriter::outputType(const Type *T) {
   const StructType* STy = dyn_cast<StructType>(T);
   if(STy && STy->isPacked())
-    output_vbr((unsigned)Type::BC_ONLY_PackedStructTyID);
+    output_vbr((unsigned)Type::PackedStructTyID);
   else
     output_vbr((unsigned)T->getTypeID());
 
   // That's all there is to handling primitive types...
-  if (T->isPrimitiveType()) {
+  if (T->isPrimitiveType())
     return;     // We might do this if we alias a prim type: %x = type int
-  }
 
   switch (T->getTypeID()) {   // Handle derived types now.
+  case Type::IntegerTyID:
+    output_vbr(cast<IntegerType>(T)->getBitWidth());
+    break;
   case Type::FunctionTyID: {
     const FunctionType *MT = cast<FunctionType>(T);
     int Slot = Table.getSlot(MT->getReturnType());
@@ -290,8 +292,8 @@
 }
 
 void BytecodeWriter::outputConstant(const Constant *CPV) {
-  assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
-         "Shouldn't output null constants!");
+  assert(((CPV->getType()->isPrimitiveType() || CPV->getType()->isIntegral()) ||
+          !CPV->isNullValue()) && "Shouldn't output null constants!");
 
   // We must check for a ConstantExpr before switching by type because
   // a ConstantExpr can be of any type, and has no explicit value.
@@ -321,19 +323,21 @@
   }
 
   switch (CPV->getType()->getTypeID()) {
-  case Type::Int1TyID:    // Boolean Types
-    if (cast<ConstantInt>(CPV)->getZExtValue())
-      output_vbr(1U);
-    else
-      output_vbr(0U);
-    break;
-
-  case Type::Int8TyID:   // Unsigned integer types...
-  case Type::Int16TyID:
-  case Type::Int32TyID:
-  case Type::Int64TyID:
-    output_vbr(cast<ConstantInt>(CPV)->getZExtValue());
+  case Type::IntegerTyID: { // Integer types...
+    unsigned NumBits = cast<IntegerType>(CPV->getType())->getBitWidth();
+    if (NumBits == 1)
+      if (cast<ConstantInt>(CPV)->getZExtValue())
+        output_vbr(1U);
+      else
+        output_vbr(0U);
+    else if (NumBits <= 32)
+      output_vbr(uint32_t(cast<ConstantInt>(CPV)->getZExtValue()));
+    else if (NumBits <= 64)
+      output_vbr(uint64_t(cast<ConstantInt>(CPV)->getZExtValue()));
+    else 
+      assert("Integer types > 64 bits not supported.");
     break;
+  }
 
   case Type::ArrayTyID: {
     const ConstantArray *CPA = cast<ConstantArray>(CPV);
@@ -484,12 +488,12 @@
       assert(Slot >= 0 && "No slot number for value!?!?");
 
       if (isa<SequentialType>(*TI)) {
-        unsigned IdxId;
-        switch (I->getOperand(Idx)->getType()->getTypeID()) {
-        default: assert(0 && "Unknown index type!");
-        case Type::Int32TyID:  IdxId = 0; break;
-        case Type::Int64TyID:  IdxId = 1; break;
-        }
+        // These should be either 32-bits or 64-bits, however, with bit
+        // accurate types we just distinguish between less than or equal to
+        // 32-bits or greater than 32-bits.
+        const IntegerType *IdxTy = 
+          cast<IntegerType>(I->getOperand(Idx)->getType());
+        unsigned IdxId = IdxTy->getBitWidth() <= 32 ? 0 : 1;
         Slot = (Slot << 1) | IdxId;
       }
       output_vbr(unsigned(Slot));
@@ -735,12 +739,12 @@
       for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
            I != E; ++I, ++Idx)
         if (isa<SequentialType>(*I)) {
-          unsigned IdxId;
-          switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
-          default: assert(0 && "Unknown index type!");
-          case Type::Int32TyID: IdxId = 0; break;
-          case Type::Int64TyID: IdxId = 1; break;
-          }
+          // These should be either 32-bits or 64-bits, however, with bit
+          // accurate types we just distinguish between less than or equal to
+          // 32-bits or greater than 32-bits.
+          const IntegerType *IdxTy = 
+            cast<IntegerType>(GEP->getOperand(Idx)->getType());
+          unsigned IdxId = IdxTy->getBitWidth() <= 32 ? 0 : 1;
           Slots[Idx] = (Slots[Idx] << 1) | IdxId;
           if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
         }






More information about the llvm-commits mailing list