For 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.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp
index 23465c5..a0b4ceb 100644
--- a/lib/Target/CBackend/CBackend.cpp
+++ b/lib/Target/CBackend/CBackend.cpp
@@ -118,7 +118,7 @@
                             bool isSigned = false,
                             const std::string &VariableName = "",
                             bool IgnoreName = false);
-    std::ostream &printPrimitiveType(std::ostream &Out, const Type *Ty, 
+    std::ostream &printSimpleType(std::ostream &Out, const Type *Ty, 
                                      bool isSigned, 
                                      const std::string &NameSoFar = "");
 
@@ -364,22 +364,29 @@
 }
 
 std::ostream &
-CWriter::printPrimitiveType(std::ostream &Out, const Type *Ty, bool isSigned,
+CWriter::printSimpleType(std::ostream &Out, const Type *Ty, bool isSigned,
                             const std::string &NameSoFar) {
-  assert(Ty->isPrimitiveType() && "Invalid type for printPrimitiveType");
+  assert((Ty->isPrimitiveType() || Ty->isIntegral()) && 
+         "Invalid type for printSimpleType");
   switch (Ty->getTypeID()) {
-  case Type::VoidTyID:   return Out << "void "               << NameSoFar;
-  case Type::Int1TyID:   return Out << "bool "               << NameSoFar;
-  case Type::Int8TyID:
-    return Out << (isSigned?"signed":"unsigned") << " char " << NameSoFar;
-  case Type::Int16TyID:  
-    return Out << (isSigned?"signed":"unsigned") << " short " << NameSoFar;
-  case Type::Int32TyID:    
-    return Out << (isSigned?"signed":"unsigned") << " int " << NameSoFar;
-  case Type::Int64TyID:   
-    return Out << (isSigned?"signed":"unsigned") << " long long " << NameSoFar;
-  case Type::FloatTyID:  return Out << "float "              << NameSoFar;
-  case Type::DoubleTyID: return Out << "double "             << NameSoFar;
+  case Type::VoidTyID:   return Out << "void " << NameSoFar;
+  case Type::IntegerTyID: {
+    unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth();
+    if (NumBits == 1) 
+      return Out << "bool " << NameSoFar;
+    else if (NumBits <= 8)
+      return Out << (isSigned?"signed":"unsigned") << " char " << NameSoFar;
+    else if (NumBits <= 16)
+      return Out << (isSigned?"signed":"unsigned") << " short " << NameSoFar;
+    else if (NumBits <= 32)
+      return Out << (isSigned?"signed":"unsigned") << " int " << NameSoFar;
+    else { 
+      assert(NumBits <= 64 && "Bit widths > 64 not implemented yet");
+      return Out << (isSigned?"signed":"unsigned") << " long long "<< NameSoFar;
+    }
+  }
+  case Type::FloatTyID:  return Out << "float "   << NameSoFar;
+  case Type::DoubleTyID: return Out << "double "  << NameSoFar;
   default :
     cerr << "Unknown primitive type: " << *Ty << "\n";
     abort();
@@ -392,11 +399,11 @@
 std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
                                  bool isSigned, const std::string &NameSoFar,
                                  bool IgnoreName) {
-  if (Ty->isPrimitiveType()) {
+  if (Ty->isPrimitiveType() || Ty->isIntegral()) {
     // FIXME:Signedness. When integer types are signless, this should just
     // always pass "false" for the sign of the primitive type. The instructions
     // will figure out how the value is to be interpreted.
-    printPrimitiveType(Out, Ty, isSigned, NameSoFar);
+    printSimpleType(Out, Ty, isSigned, NameSoFar);
     return Out;
   }
 
@@ -624,13 +631,13 @@
     case Instruction::PtrToInt:
     case Instruction::FPToUI: // For these, make sure we get an unsigned dest
       Out << '(';
-      printPrimitiveType(Out, DstTy, false);
+      printSimpleType(Out, DstTy, false);
       Out << ')';
       break;
     case Instruction::SExt: 
     case Instruction::FPToSI: // For these, make sure we get a signed dest
       Out << '(';
-      printPrimitiveType(Out, DstTy, true);
+      printSimpleType(Out, DstTy, true);
       Out << ')';
       break;
     default:
@@ -642,13 +649,13 @@
     case Instruction::UIToFP:
     case Instruction::ZExt:
       Out << '(';
-      printPrimitiveType(Out, SrcTy, false);
+      printSimpleType(Out, SrcTy, false);
       Out << ')';
       break;
     case Instruction::SIToFP:
     case Instruction::SExt:
       Out << '(';
-      printPrimitiveType(Out, SrcTy, true); 
+      printSimpleType(Out, SrcTy, true); 
       Out << ')';
       break;
     case Instruction::IntToPtr:
@@ -832,7 +839,7 @@
       Out << (CI->getZExtValue() ? '1' : '0') ;
     else {
       Out << "((";
-      printPrimitiveType(Out, Ty, false) << ')';
+      printSimpleType(Out, Ty, false) << ')';
       if (CI->isMinValue(true)) 
         Out << CI->getZExtValue() << 'u';
       else
@@ -1019,7 +1026,7 @@
   if (NeedsExplicitCast) {
     Out << "((";
     if (Ty->isInteger())
-      printPrimitiveType(Out, Ty, TypeIsSigned);
+      printSimpleType(Out, Ty, TypeIsSigned);
     else
       printType(Out, Ty); // not integer, sign doesn't matter
     Out << ")(";
@@ -1064,7 +1071,7 @@
   // operand.
   if (shouldCast) {
     Out << "((";
-    printPrimitiveType(Out, OpTy, typeIsSigned);
+    printSimpleType(Out, OpTy, typeIsSigned);
     Out << ")";
     printConstant(CPV);
     Out << ")";
@@ -1120,14 +1127,14 @@
   case Instruction::URem: 
   case Instruction::UDiv: 
     Out << "((";
-    printPrimitiveType(Out, Ty, false);
+    printSimpleType(Out, Ty, false);
     Out << ")(";
     return true;
   case Instruction::AShr:
   case Instruction::SRem: 
   case Instruction::SDiv: 
     Out << "((";
-    printPrimitiveType(Out, Ty, true);
+    printSimpleType(Out, Ty, true);
     Out << ")(";
     return true;
   default: break;
@@ -1174,7 +1181,7 @@
   // operand.
   if (shouldCast) {
     Out << "((";
-    printPrimitiveType(Out, OpTy, castIsSigned);
+    printSimpleType(Out, OpTy, castIsSigned);
     Out << ")";
     writeOperand(Operand);
     Out << ")";
@@ -1222,7 +1229,7 @@
   if (shouldCast) {
     Out << "((";
     if (OpTy->isInteger())
-      printPrimitiveType(Out, OpTy, castIsSigned);
+      printSimpleType(Out, OpTy, castIsSigned);
     else
       printType(Out, OpTy); // not integer, sign doesn't matter
     Out << ")";
@@ -1711,7 +1718,7 @@
 void CWriter::printContainedStructs(const Type *Ty,
                                     std::set<const StructType*> &StructPrinted){
   // Don't walk through pointers.
-  if (isa<PointerType>(Ty) || Ty->isPrimitiveType()) return;
+  if (isa<PointerType>(Ty) || Ty->isPrimitiveType() || Ty->isIntegral()) return;
   
   // Print all contained types first.
   for (Type::subtype_iterator I = Ty->subtype_begin(),
@@ -2237,9 +2244,14 @@
   switch (Ty->getTypeID()) {
     default: assert(0 && "Invalid Type");
     case Type::FloatTyID:  return "Float";
-    case Type::Int32TyID:  return "Int32";
     case Type::DoubleTyID: return "Double";
-    case Type::Int64TyID:  return "Int64";
+    case Type::IntegerTyID: {
+      unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth();
+      if (NumBits <= 32)
+        return "Int32";
+      else
+        return "Int64";
+    }
   }
 }
 
diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp
index 59dc0dc..c0b0670 100644
--- a/lib/Target/TargetData.cpp
+++ b/lib/Target/TargetData.cpp
@@ -241,12 +241,21 @@
                                uint64_t &Size, unsigned char &Alignment) {
   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
   switch (Ty->getTypeID()) {
-  case Type::Int1TyID:   Size = 1; Alignment = TD->getBoolAlignment(); return;
-  case Type::VoidTyID:
-  case Type::Int8TyID:   Size = 1; Alignment = TD->getByteAlignment(); return;
-  case Type::Int16TyID:  Size = 2; Alignment = TD->getShortAlignment(); return;
-  case Type::Int32TyID:  Size = 4; Alignment = TD->getIntAlignment(); return;
-  case Type::Int64TyID:  Size = 8; Alignment = TD->getLongAlignment(); return;
+  case Type::IntegerTyID: {
+    unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
+    if (BitWidth <= 8) {
+      Size = 1; Alignment = TD->getByteAlignment();
+    } else if (BitWidth <= 16) {
+      Size = 2; Alignment = TD->getShortAlignment();
+    } else if (BitWidth <= 32) {
+      Size = 4; Alignment = TD->getIntAlignment();
+    } else if (BitWidth <= 64) {
+      Size = 8; Alignment = TD->getLongAlignment();
+    } else
+      assert(0 && "Integer types > 64 bits not supported.");
+    return;
+  }
+  case Type::VoidTyID:   Size = 1; Alignment = TD->getByteAlignment(); return;
   case Type::FloatTyID:  Size = 4; Alignment = TD->getFloatAlignment(); return;
   case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
   case Type::LabelTyID:
diff --git a/lib/Target/X86/X86TargetAsmInfo.cpp b/lib/Target/X86/X86TargetAsmInfo.cpp
index 8c212b6..9249b8d 100644
--- a/lib/Target/X86/X86TargetAsmInfo.cpp
+++ b/lib/Target/X86/X86TargetAsmInfo.cpp
@@ -176,12 +176,18 @@
   
   const Type *Ty = CI->getType();
   const char *IntName;
-  switch (Ty->getTypeID()) {
-  default: return false;
-  case Type::Int16TyID: IntName = "llvm.bswap.i16"; break;
-  case Type::Int32TyID:   IntName = "llvm.bswap.i32"; break;
-  case Type::Int64TyID:  IntName = "llvm.bswap.i64"; break;
-  }
+  if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
+    unsigned BitWidth = ITy->getBitWidth();
+    if (BitWidth > 8 && BitWidth <= 16)
+      IntName = "llvm.bswap.i16";
+    else if (BitWidth > 24  && BitWidth <= 32)
+      IntName = "llvm.bswap.i32";
+    else if (BitWidth > 56 && BitWidth <= 64)
+      IntName = "llvm.bswap.i64";
+    else
+      return false;
+  } else
+    return false;
 
   // Okay, we can do this xform, do so now.
   Module *M = CI->getParent()->getParent()->getParent();