TargetLowering::isOperandValidForConstraint
[oota-llvm.git] / lib / VMCore / ConstantFold.cpp
index ee5ee0ee42c2b59203d62d7f11d0664de3524a3d..2589739d52df0ead30d398f6dedb6d48da804c48 100644 (file)
 #include "llvm/Instructions.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/MathExtras.h"
 #include <limits>
 #include <cmath>
 using namespace llvm;
 
 namespace {
-  struct ConstRules {
+  struct VISIBILITY_HIDDEN ConstRules {
     ConstRules() {}
     virtual ~ConstRules() {}
 
@@ -37,7 +40,9 @@ namespace {
     virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
     virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
     virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
-    virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
+    virtual Constant *udiv(const Constant *V1, const Constant *V2) const = 0;
+    virtual Constant *sdiv(const Constant *V1, const Constant *V2) const = 0;
+    virtual Constant *fdiv(const Constant *V1, const Constant *V2) const = 0;
     virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
     virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
     virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
@@ -85,8 +90,9 @@ namespace {
 // This class also provides subclasses with typesafe implementations of methods
 // so that don't have to do type casting.
 //
+namespace {
 template<class ArgType, class SubClassName>
-class TemplateRules : public ConstRules {
+class VISIBILITY_HIDDEN TemplateRules : public ConstRules {
 
 
   //===--------------------------------------------------------------------===//
@@ -102,8 +108,14 @@ class TemplateRules : public ConstRules {
   virtual Constant *mul(const Constant *V1, const Constant *V2) const {
     return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
   }
-  virtual Constant *div(const Constant *V1, const Constant *V2) const {
-    return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
+  virtual Constant *udiv(const Constant *V1, const Constant *V2) const {
+    return SubClassName::UDiv((const ArgType *)V1, (const ArgType *)V2);
+  }
+  virtual Constant *sdiv(const Constant *V1, const Constant *V2) const {
+    return SubClassName::SDiv((const ArgType *)V1, (const ArgType *)V2);
+  }
+  virtual Constant *fdiv(const Constant *V1, const Constant *V2) const {
+    return SubClassName::FDiv((const ArgType *)V1, (const ArgType *)V2);
   }
   virtual Constant *rem(const Constant *V1, const Constant *V2) const {
     return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
@@ -174,16 +186,18 @@ class TemplateRules : public ConstRules {
   // Default "noop" implementations
   //===--------------------------------------------------------------------===//
 
-  static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
-  static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Add (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Sub (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Mul (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *SDiv(const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *UDiv(const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *FDiv(const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Rem (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *And (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Or  (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Xor (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Shl (const ArgType *V1, const ArgType *V2) { return 0; }
+  static Constant *Shr (const ArgType *V1, const ArgType *V2) { return 0; }
   static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
     return 0;
   }
@@ -209,7 +223,7 @@ class TemplateRules : public ConstRules {
 public:
   virtual ~TemplateRules() {}
 };
-
+}  // end anonymous namespace
 
 
 //===----------------------------------------------------------------------===//
@@ -218,12 +232,15 @@ public:
 //
 // EmptyRules provides a concrete base class of ConstRules that does nothing
 //
-struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
+namespace {
+struct VISIBILITY_HIDDEN EmptyRules
+  : public TemplateRules<Constant, EmptyRules> {
   static Constant *EqualTo(const Constant *V1, const Constant *V2) {
-    if (V1 == V2) return ConstantBool::True;
+    if (V1 == V2) return ConstantBool::getTrue();
     return 0;
   }
 };
+}  // end anonymous namespace
 
 
 
@@ -233,7 +250,9 @@ struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
 //
 // BoolRules provides a concrete base class of ConstRules for the 'bool' type.
 //
-struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
+namespace {
+struct VISIBILITY_HIDDEN BoolRules
+  : public TemplateRules<ConstantBool, BoolRules> {
 
   static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
     return ConstantBool::get(V1->getValue() < V2->getValue());
@@ -262,18 +281,19 @@ struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
   }
 
   DEF_CAST(Bool  , ConstantBool, bool)
-  DEF_CAST(SByte , ConstantSInt, signed char)
-  DEF_CAST(UByte , ConstantUInt, unsigned char)
-  DEF_CAST(Short , ConstantSInt, signed short)
-  DEF_CAST(UShort, ConstantUInt, unsigned short)
-  DEF_CAST(Int   , ConstantSInt, signed int)
-  DEF_CAST(UInt  , ConstantUInt, unsigned int)
-  DEF_CAST(Long  , ConstantSInt, int64_t)
-  DEF_CAST(ULong , ConstantUInt, uint64_t)
+  DEF_CAST(SByte , ConstantInt, signed char)
+  DEF_CAST(UByte , ConstantInt, unsigned char)
+  DEF_CAST(Short , ConstantInt, signed short)
+  DEF_CAST(UShort, ConstantInt, unsigned short)
+  DEF_CAST(Int   , ConstantInt, signed int)
+  DEF_CAST(UInt  , ConstantInt, unsigned int)
+  DEF_CAST(Long  , ConstantInt, int64_t)
+  DEF_CAST(ULong , ConstantInt, uint64_t)
   DEF_CAST(Float , ConstantFP  , float)
   DEF_CAST(Double, ConstantFP  , double)
 #undef DEF_CAST
 };
+}  // end anonymous namespace
 
 
 //===----------------------------------------------------------------------===//
@@ -283,37 +303,38 @@ struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
 // NullPointerRules provides a concrete base class of ConstRules for null
 // pointers.
 //
-struct NullPointerRules : public TemplateRules<ConstantPointerNull,
-                                               NullPointerRules> {
+namespace {
+struct VISIBILITY_HIDDEN NullPointerRules
+  : public TemplateRules<ConstantPointerNull, NullPointerRules> {
   static Constant *EqualTo(const Constant *V1, const Constant *V2) {
-    return ConstantBool::True;  // Null pointers are always equal
+    return ConstantBool::getTrue();  // Null pointers are always equal
   }
   static Constant *CastToBool(const Constant *V) {
-    return ConstantBool::False;
+    return ConstantBool::getFalse();
   }
   static Constant *CastToSByte (const Constant *V) {
-    return ConstantSInt::get(Type::SByteTy, 0);
+    return ConstantInt::get(Type::SByteTy, 0);
   }
   static Constant *CastToUByte (const Constant *V) {
-    return ConstantUInt::get(Type::UByteTy, 0);
+    return ConstantInt::get(Type::UByteTy, 0);
   }
   static Constant *CastToShort (const Constant *V) {
-    return ConstantSInt::get(Type::ShortTy, 0);
+    return ConstantInt::get(Type::ShortTy, 0);
   }
   static Constant *CastToUShort(const Constant *V) {
-    return ConstantUInt::get(Type::UShortTy, 0);
+    return ConstantInt::get(Type::UShortTy, 0);
   }
   static Constant *CastToInt   (const Constant *V) {
-    return ConstantSInt::get(Type::IntTy, 0);
+    return ConstantInt::get(Type::IntTy, 0);
   }
   static Constant *CastToUInt  (const Constant *V) {
-    return ConstantUInt::get(Type::UIntTy, 0);
+    return ConstantInt::get(Type::UIntTy, 0);
   }
   static Constant *CastToLong  (const Constant *V) {
-    return ConstantSInt::get(Type::LongTy, 0);
+    return ConstantInt::get(Type::LongTy, 0);
   }
   static Constant *CastToULong (const Constant *V) {
-    return ConstantUInt::get(Type::ULongTy, 0);
+    return ConstantInt::get(Type::ULongTy, 0);
   }
   static Constant *CastToFloat (const Constant *V) {
     return ConstantFP::get(Type::FloatTy, 0);
@@ -327,6 +348,7 @@ struct NullPointerRules : public TemplateRules<ConstantPointerNull,
     return ConstantPointerNull::get(PTy);
   }
 };
+}  // end anonymous namespace
 
 //===----------------------------------------------------------------------===//
 //                          ConstantPackedRules Class
@@ -348,7 +370,8 @@ static Constant *EvalVectorOp(const ConstantPacked *V1,
 /// PackedTypeRules provides a concrete base class of ConstRules for
 /// ConstantPacked operands.
 ///
-struct ConstantPackedRules
+namespace {
+struct VISIBILITY_HIDDEN ConstantPackedRules
   : public TemplateRules<ConstantPacked, ConstantPackedRules> {
   
   static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
@@ -360,8 +383,14 @@ struct ConstantPackedRules
   static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
     return EvalVectorOp(V1, V2, ConstantExpr::getMul);
   }
-  static Constant *Div(const ConstantPacked *V1, const ConstantPacked *V2) {
-    return EvalVectorOp(V1, V2, ConstantExpr::getDiv);
+  static Constant *UDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
+    return EvalVectorOp(V1, V2, ConstantExpr::getUDiv);
+  }
+  static Constant *SDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
+    return EvalVectorOp(V1, V2, ConstantExpr::getSDiv);
+  }
+  static Constant *FDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
+    return EvalVectorOp(V1, V2, ConstantExpr::getFDiv);
   }
   static Constant *Rem(const ConstantPacked *V1, const ConstantPacked *V2) {
     return EvalVectorOp(V1, V2, ConstantExpr::getRem);
@@ -396,6 +425,7 @@ struct ConstantPackedRules
     return 0;
   }
 };
+}  // end anonymous namespace
 
 
 //===----------------------------------------------------------------------===//
@@ -406,52 +436,54 @@ struct ConstantPackedRules
 /// PackedType operands, where both operands are not ConstantPacked.  The usual
 /// cause for this is that one operand is a ConstantAggregateZero.
 ///
-struct GeneralPackedRules : public TemplateRules<Constant, GeneralPackedRules> {
+namespace {
+struct VISIBILITY_HIDDEN GeneralPackedRules
+  : public TemplateRules<Constant, GeneralPackedRules> {
 };
+}  // end anonymous namespace
 
 
 //===----------------------------------------------------------------------===//
-//                             DirectRules Class
+//                           DirectIntRules Class
 //===----------------------------------------------------------------------===//
 //
-// DirectRules provides a concrete base classes of ConstRules for a variety of
-// different types.  This allows the C++ compiler to automatically generate our
-// constant handling operations in a typesafe and accurate manner.
+// DirectIntRules provides implementations of functions that are valid on
+// integer types, but not all types in general.
 //
-template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
-struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
-  static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
-  }
+namespace {
+template <class BuiltinType, Type **Ty>
+struct VISIBILITY_HIDDEN DirectIntRules
+  : public TemplateRules<ConstantInt, DirectIntRules<BuiltinType, Ty> > {
 
-  static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Add(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = (BuiltinType)V1->getZExtValue() + 
+                    (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Sub(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = (BuiltinType)V1->getZExtValue() - 
+                    (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
-    if (V2->isNullValue()) return 0;
-    BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Mul(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = (BuiltinType)V1->getZExtValue() * 
+                    (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
-    bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
+  static Constant *LessThan(const ConstantInt *V1, const ConstantInt *V2) {
+    bool R = (BuiltinType)V1->getZExtValue() < (BuiltinType)V2->getZExtValue();
     return ConstantBool::get(R);
   }
 
-  static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
-    bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
+  static Constant *EqualTo(const ConstantInt *V1, const ConstantInt *V2) {
+    bool R = (BuiltinType)V1->getZExtValue() == (BuiltinType)V2->getZExtValue();
     return ConstantBool::get(R);
   }
 
-  static Constant *CastToPointer(const ConstantClass *V,
+  static Constant *CastToPointer(const ConstantInt *V,
                                  const PointerType *PTy) {
     if (V->isNullValue())    // Is it a FP or Integral null value?
       return ConstantPointerNull::get(PTy);
@@ -460,79 +492,80 @@ struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
 
   // Casting operators.  ick
 #define DEF_CAST(TYPE, CLASS, CTYPE) \
-  static Constant *CastTo##TYPE  (const ConstantClass *V) {    \
-    return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
+  static Constant *CastTo##TYPE  (const ConstantInt *V) {    \
+    return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getZExtValue()); \
   }
 
   DEF_CAST(Bool  , ConstantBool, bool)
-  DEF_CAST(SByte , ConstantSInt, signed char)
-  DEF_CAST(UByte , ConstantUInt, unsigned char)
-  DEF_CAST(Short , ConstantSInt, signed short)
-  DEF_CAST(UShort, ConstantUInt, unsigned short)
-  DEF_CAST(Int   , ConstantSInt, signed int)
-  DEF_CAST(UInt  , ConstantUInt, unsigned int)
-  DEF_CAST(Long  , ConstantSInt, int64_t)
-  DEF_CAST(ULong , ConstantUInt, uint64_t)
-  DEF_CAST(Float , ConstantFP  , float)
-  DEF_CAST(Double, ConstantFP  , double)
+  DEF_CAST(SByte , ConstantInt, signed char)
+  DEF_CAST(UByte , ConstantInt, unsigned char)
+  DEF_CAST(Short , ConstantInt, signed short)
+  DEF_CAST(UShort, ConstantInt, unsigned short)
+  DEF_CAST(Int   , ConstantInt, signed int)
+  DEF_CAST(UInt  , ConstantInt, unsigned int)
+  DEF_CAST(Long  , ConstantInt, int64_t)
+  DEF_CAST(ULong , ConstantInt, uint64_t)
+  DEF_CAST(Float , ConstantFP , float)
+  DEF_CAST(Double, ConstantFP , double)
 #undef DEF_CAST
-};
-
 
-//===----------------------------------------------------------------------===//
-//                           DirectIntRules Class
-//===----------------------------------------------------------------------===//
-//
-// DirectIntRules provides implementations of functions that are valid on
-// integer types, but not all types in general.
-//
-template <class ConstantClass, class BuiltinType, Type **Ty>
-struct DirectIntRules
-  : public DirectRules<ConstantClass, BuiltinType, Ty,
-                       DirectIntRules<ConstantClass, BuiltinType, Ty> > {
+  static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) {
+    if (V2->isNullValue()) 
+      return 0;
+    BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue());
+    return ConstantInt::get(*Ty, R);
+  }
 
-  static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
-    if (V2->isNullValue()) return 0;
+  static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) {
+    if (V2->isNullValue()) 
+      return 0;
     if (V2->isAllOnesValue() &&              // MIN_INT / -1
-        (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
+        (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
       return 0;
-    BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+    BuiltinType R = 
+      (BuiltinType)(V1->getSExtValue() / V2->getSExtValue());
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *Rem(const ConstantClass *V1,
-                       const ConstantClass *V2) {
+  static Constant *Rem(const ConstantInt *V1, const ConstantInt *V2) {
     if (V2->isNullValue()) return 0;         // X / 0
     if (V2->isAllOnesValue() &&              // MIN_INT / -1
-        (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
+        (BuiltinType)V1->getZExtValue() == -(BuiltinType)V1->getZExtValue())
       return 0;
-    BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+    BuiltinType R = 
+      (BuiltinType)V1->getZExtValue() % (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *And(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = 
+      (BuiltinType)V1->getZExtValue() & (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
-  static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Or(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = 
+      (BuiltinType)V1->getZExtValue() | (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
-  static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Xor(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = 
+      (BuiltinType)V1->getZExtValue() ^ (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Shl(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = 
+      (BuiltinType)V1->getZExtValue() << (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 
-  static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
-    BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+  static Constant *Shr(const ConstantInt *V1, const ConstantInt *V2) {
+    BuiltinType R = 
+      (BuiltinType)V1->getZExtValue() >> (BuiltinType)V2->getZExtValue();
+    return ConstantInt::get(*Ty, R);
   }
 };
+}  // end anonymous namespace
 
 
 //===----------------------------------------------------------------------===//
@@ -542,68 +575,121 @@ struct DirectIntRules
 /// DirectFPRules provides implementations of functions that are valid on
 /// floating point types, but not all types in general.
 ///
-template <class ConstantClass, class BuiltinType, Type **Ty>
-struct DirectFPRules
-  : public DirectRules<ConstantClass, BuiltinType, Ty,
-                       DirectFPRules<ConstantClass, BuiltinType, Ty> > {
-  static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
+namespace {
+template <class BuiltinType, Type **Ty>
+struct VISIBILITY_HIDDEN DirectFPRules
+  : public TemplateRules<ConstantFP, DirectFPRules<BuiltinType, Ty> > {
+
+  static Constant *Add(const ConstantFP *V1, const ConstantFP *V2) {
+    BuiltinType R = (BuiltinType)V1->getValue() + 
+                    (BuiltinType)V2->getValue();
+    return ConstantFP::get(*Ty, R);
+  }
+
+  static Constant *Sub(const ConstantFP *V1, const ConstantFP *V2) {
+    BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
+    return ConstantFP::get(*Ty, R);
+  }
+
+  static Constant *Mul(const ConstantFP *V1, const ConstantFP *V2) {
+    BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
+    return ConstantFP::get(*Ty, R);
+  }
+
+  static Constant *LessThan(const ConstantFP *V1, const ConstantFP *V2) {
+    bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
+    return ConstantBool::get(R);
+  }
+
+  static Constant *EqualTo(const ConstantFP *V1, const ConstantFP *V2) {
+    bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
+    return ConstantBool::get(R);
+  }
+
+  static Constant *CastToPointer(const ConstantFP *V,
+                                 const PointerType *PTy) {
+    if (V->isNullValue())    // Is it a FP or Integral null value?
+      return ConstantPointerNull::get(PTy);
+    return 0;  // Can't const prop other types of pointers
+  }
+
+  // Casting operators.  ick
+#define DEF_CAST(TYPE, CLASS, CTYPE) \
+  static Constant *CastTo##TYPE  (const ConstantFP *V) {    \
+    return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
+  }
+
+  DEF_CAST(Bool  , ConstantBool, bool)
+  DEF_CAST(SByte , ConstantInt, signed char)
+  DEF_CAST(UByte , ConstantInt, unsigned char)
+  DEF_CAST(Short , ConstantInt, signed short)
+  DEF_CAST(UShort, ConstantInt, unsigned short)
+  DEF_CAST(Int   , ConstantInt, signed int)
+  DEF_CAST(UInt  , ConstantInt, unsigned int)
+  DEF_CAST(Long  , ConstantInt, int64_t)
+  DEF_CAST(ULong , ConstantInt, uint64_t)
+  DEF_CAST(Float , ConstantFP , float)
+  DEF_CAST(Double, ConstantFP , double)
+#undef DEF_CAST
+
+  static Constant *Rem(const ConstantFP *V1, const ConstantFP *V2) {
     if (V2->isNullValue()) return 0;
     BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
                                    (BuiltinType)V2->getValue());
-    return ConstantClass::get(*Ty, Result);
+    return ConstantFP::get(*Ty, Result);
   }
-  static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
+  static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) {
     BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
-    if (V2->isExactlyValue(0.0)) return ConstantClass::get(*Ty, inf);
-    if (V2->isExactlyValue(-0.0)) return ConstantClass::get(*Ty, -inf);
+    if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf);
+    if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf);
     BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
-    return ConstantClass::get(*Ty, R);
+    return ConstantFP::get(*Ty, R);
   }
 };
-
+}  // end anonymous namespace
+
+static ManagedStatic<EmptyRules>       EmptyR;
+static ManagedStatic<BoolRules>        BoolR;
+static ManagedStatic<NullPointerRules> NullPointerR;
+static ManagedStatic<ConstantPackedRules> ConstantPackedR;
+static ManagedStatic<GeneralPackedRules> GeneralPackedR;
+static ManagedStatic<DirectIntRules<signed char   , &Type::SByteTy> > SByteR;
+static ManagedStatic<DirectIntRules<unsigned char , &Type::UByteTy> > UByteR;
+static ManagedStatic<DirectIntRules<signed short  , &Type::ShortTy> > ShortR;
+static ManagedStatic<DirectIntRules<unsigned short, &Type::UShortTy> > UShortR;
+static ManagedStatic<DirectIntRules<signed int    , &Type::IntTy> >   IntR;
+static ManagedStatic<DirectIntRules<unsigned int  , &Type::UIntTy> >  UIntR;
+static ManagedStatic<DirectIntRules<int64_t       , &Type::LongTy> >  LongR;
+static ManagedStatic<DirectIntRules<uint64_t      , &Type::ULongTy> > ULongR;
+static ManagedStatic<DirectFPRules <float         , &Type::FloatTy> > FloatR;
+static ManagedStatic<DirectFPRules <double        , &Type::DoubleTy> > DoubleR;
 
 /// ConstRules::get - This method returns the constant rules implementation that
 /// implements the semantics of the two specified constants.
 ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
-  static EmptyRules       EmptyR;
-  static BoolRules        BoolR;
-  static NullPointerRules NullPointerR;
-  static ConstantPackedRules ConstantPackedR;
-  static GeneralPackedRules GeneralPackedR;
-  static DirectIntRules<ConstantSInt,   signed char , &Type::SByteTy>  SByteR;
-  static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>  UByteR;
-  static DirectIntRules<ConstantSInt,   signed short, &Type::ShortTy>  ShortR;
-  static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
-  static DirectIntRules<ConstantSInt,   signed int  , &Type::IntTy>    IntR;
-  static DirectIntRules<ConstantUInt, unsigned int  , &Type::UIntTy>   UIntR;
-  static DirectIntRules<ConstantSInt,  int64_t      , &Type::LongTy>   LongR;
-  static DirectIntRules<ConstantUInt, uint64_t      , &Type::ULongTy>  ULongR;
-  static DirectFPRules <ConstantFP  , float         , &Type::FloatTy>  FloatR;
-  static DirectFPRules <ConstantFP  , double        , &Type::DoubleTy> DoubleR;
-
   if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
       isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
       isa<UndefValue>(V1) || isa<UndefValue>(V2))
-    return EmptyR;
+    return *EmptyR;
 
   switch (V1->getType()->getTypeID()) {
   default: assert(0 && "Unknown value type for constant folding!");
-  case Type::BoolTyID:    return BoolR;
-  case Type::PointerTyID: return NullPointerR;
-  case Type::SByteTyID:   return SByteR;
-  case Type::UByteTyID:   return UByteR;
-  case Type::ShortTyID:   return ShortR;
-  case Type::UShortTyID:  return UShortR;
-  case Type::IntTyID:     return IntR;
-  case Type::UIntTyID:    return UIntR;
-  case Type::LongTyID:    return LongR;
-  case Type::ULongTyID:   return ULongR;
-  case Type::FloatTyID:   return FloatR;
-  case Type::DoubleTyID:  return DoubleR;
+  case Type::BoolTyID:    return *BoolR;
+  case Type::PointerTyID: return *NullPointerR;
+  case Type::SByteTyID:   return *SByteR;
+  case Type::UByteTyID:   return *UByteR;
+  case Type::ShortTyID:   return *ShortR;
+  case Type::UShortTyID:  return *UShortR;
+  case Type::IntTyID:     return *IntR;
+  case Type::UIntTyID:    return *UIntR;
+  case Type::LongTyID:    return *LongR;
+  case Type::ULongTyID:   return *ULongR;
+  case Type::FloatTyID:   return *FloatR;
+  case Type::DoubleTyID:  return *DoubleR;
   case Type::PackedTyID:
     if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
-      return ConstantPackedR;
-    return GeneralPackedR;  // Constant folding rules for ConstantAggregateZero.
+      return *ConstantPackedR;
+    return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
   }
 }
 
@@ -621,6 +707,81 @@ static unsigned getSize(const Type *Ty) {
   return S ? S : 8;  // Treat pointers at 8 bytes
 }
 
+/// CastConstantPacked - Convert the specified ConstantPacked node to the
+/// specified packed type.  At this point, we know that the elements of the
+/// input packed constant are all simple integer or FP values.
+static Constant *CastConstantPacked(ConstantPacked *CP,
+                                    const PackedType *DstTy) {
+  unsigned SrcNumElts = CP->getType()->getNumElements();
+  unsigned DstNumElts = DstTy->getNumElements();
+  const Type *SrcEltTy = CP->getType()->getElementType();
+  const Type *DstEltTy = DstTy->getElementType();
+  
+  // If both vectors have the same number of elements (thus, the elements
+  // are the same size), perform the conversion now.
+  if (SrcNumElts == DstNumElts) {
+    std::vector<Constant*> Result;
+    
+    // If the src and dest elements are both integers, just cast each one
+    // which will do the appropriate bit-convert.
+    if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) {
+      for (unsigned i = 0; i != SrcNumElts; ++i)
+        Result.push_back(ConstantExpr::getCast(CP->getOperand(i),
+                                               DstEltTy));
+      return ConstantPacked::get(Result);
+    }
+    
+    if (SrcEltTy->isIntegral()) {
+      // Otherwise, this is an int-to-fp cast.
+      assert(DstEltTy->isFloatingPoint());
+      if (DstEltTy->getTypeID() == Type::DoubleTyID) {
+        for (unsigned i = 0; i != SrcNumElts; ++i) {
+          double V =
+            BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
+          Result.push_back(ConstantFP::get(Type::DoubleTy, V));
+        }
+        return ConstantPacked::get(Result);
+      }
+      assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
+      for (unsigned i = 0; i != SrcNumElts; ++i) {
+        float V =
+        BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
+        Result.push_back(ConstantFP::get(Type::FloatTy, V));
+      }
+      return ConstantPacked::get(Result);
+    }
+    
+    // Otherwise, this is an fp-to-int cast.
+    assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
+    
+    if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
+      for (unsigned i = 0; i != SrcNumElts; ++i) {
+        uint64_t V =
+          DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
+        Constant *C = ConstantInt::get(Type::ULongTy, V);
+        Result.push_back(ConstantExpr::getCast(C, DstEltTy));
+      }
+      return ConstantPacked::get(Result);
+    }
+
+    assert(SrcEltTy->getTypeID() == Type::FloatTyID);
+    for (unsigned i = 0; i != SrcNumElts; ++i) {
+      uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
+      Constant *C = ConstantInt::get(Type::UIntTy, V);
+      Result.push_back(ConstantExpr::getCast(C, DstEltTy));
+    }
+    return ConstantPacked::get(Result);
+  }
+  
+  // Otherwise, this is a cast that changes element count and size.  Handle
+  // casts which shrink the elements here.
+  
+  // FIXME: We need to know endianness to do this!
+  
+  return 0;
+}
+
+
 Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
                                             const Type *DestTy) {
   if (V->getType() == DestTy) return (Constant*)V;
@@ -631,7 +792,7 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
       // FIXME: When we support 'external weak' references, we have to prevent
       // this transformation from happening.  This code will need to be updated
       // to ignore external weak symbols when we support it.
-      return ConstantBool::True;
+      return ConstantBool::getTrue();
   } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
     if (CE->getOpcode() == Instruction::Cast) {
       Constant *Op = const_cast<Constant*>(CE->getOperand(0));
@@ -688,6 +849,38 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
       if (ElTy == DPTy->getElementType())
         return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
     }
+      
+  // Handle casts from one packed constant to another.  We know that the src and
+  // dest type have the same size.
+  if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
+    if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
+      assert(DestPTy->getElementType()->getPrimitiveSizeInBits() * 
+                 DestPTy->getNumElements()  ==
+             SrcTy->getElementType()->getPrimitiveSizeInBits() * 
+             SrcTy->getNumElements() && "Not cast between same sized vectors!");
+      if (isa<ConstantAggregateZero>(V))
+        return Constant::getNullValue(DestTy);
+      if (isa<UndefValue>(V))
+        return UndefValue::get(DestTy);
+      if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
+        // This is a cast from a ConstantPacked of one type to a ConstantPacked
+        // of another type.  Check to see if all elements of the input are
+        // simple.
+        bool AllSimpleConstants = true;
+        for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
+          if (!isa<ConstantInt>(CP->getOperand(i)) &&
+              !isa<ConstantFP>(CP->getOperand(i))) {
+            AllSimpleConstants = false;
+            break;
+          }
+        }
+            
+        // If all of the elements are simple constants, we can fold this.
+        if (AllSimpleConstants)
+          return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
+      }
+    }
+  }
 
   ConstRules &Rules = ConstRules::get(V, V);
 
@@ -712,10 +905,8 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
 Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
                                               const Constant *V1,
                                               const Constant *V2) {
-  if (Cond == ConstantBool::True)
-    return const_cast<Constant*>(V1);
-  else if (Cond == ConstantBool::False)
-    return const_cast<Constant*>(V2);
+  if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
+    return const_cast<Constant*>(CB->getValue() ? V1 : V2);
 
   if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
   if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
@@ -728,10 +919,13 @@ Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
                                                       const Constant *Idx) {
   if (isa<UndefValue>(Val))  // ee(undef, x) -> undef
     return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
+  if (Val->isNullValue())  // ee(zero, x) -> zero
+    return Constant::getNullValue(
+                          cast<PackedType>(Val->getType())->getElementType());
   
   if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
-    if (const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx)) {
-      return const_cast<Constant*>(CVal->getOperand(CIdx->getValue()));
+    if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
+      return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
     } else if (isa<UndefValue>(Idx)) {
       // ee({w,x,y,z}, undef) -> w (an arbitrary value).
       return const_cast<Constant*>(CVal->getOperand(0));
@@ -743,9 +937,9 @@ Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
 Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
                                                      const Constant *Elt,
                                                      const Constant *Idx) {
-  const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx);
+  const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
   if (!CIdx) return 0;
-  unsigned idxVal = CIdx->getValue();
+  uint64_t idxVal = CIdx->getZExtValue();
   if (const UndefValue *UVal = dyn_cast<UndefValue>(Val)) {
     // Insertion of scalar constant into packed undef
     // Optimize away insertion of undef
@@ -797,6 +991,14 @@ Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
   return 0;
 }
 
+Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
+                                                     const Constant *V2,
+                                                     const Constant *Mask) {
+  // TODO:
+  return 0;
+}
+
+
 /// isZeroSizedType - This type is zero sized if its an array or structure of
 /// zero sized types.  The only leaf zero sized type is an empty structure.
 static bool isMaybeZeroSizedType(const Type *Ty) {
@@ -842,7 +1044,8 @@ static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
 
   // If they are really different, now that they are the same type, then we
   // found a difference!
-  if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
+  if (cast<ConstantInt>(C1)->getSExtValue() < 
+      cast<ConstantInt>(C2)->getSExtValue())
     return -1;
   else
     return 1;
@@ -870,11 +1073,11 @@ static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
       // We distilled this down to a simple case, use the standard constant
       // folder.
       ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
-      if (R == ConstantBool::True) return Instruction::SetEQ;
+      if (R && R->getValue()) return Instruction::SetEQ;
       R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
-      if (R == ConstantBool::True) return Instruction::SetLT;
+      if (R && R->getValue()) return Instruction::SetLT;
       R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
-      if (R == ConstantBool::True) return Instruction::SetGT;
+      if (R && R->getValue()) return Instruction::SetGT;
       
       // If we couldn't figure it out, bail.
       return Instruction::BinaryOpsEnd;
@@ -1044,7 +1247,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
   case Instruction::Add:     C = ConstRules::get(V1, V2).add(V1, V2); break;
   case Instruction::Sub:     C = ConstRules::get(V1, V2).sub(V1, V2); break;
   case Instruction::Mul:     C = ConstRules::get(V1, V2).mul(V1, V2); break;
-  case Instruction::Div:     C = ConstRules::get(V1, V2).div(V1, V2); break;
+  case Instruction::UDiv:    C = ConstRules::get(V1, V2).udiv(V1, V2); break;
+  case Instruction::SDiv:    C = ConstRules::get(V1, V2).sdiv(V1, V2); break;
+  case Instruction::FDiv:    C = ConstRules::get(V1, V2).fdiv(V1, V2); break;
   case Instruction::Rem:     C = ConstRules::get(V1, V2).rem(V1, V2); break;
   case Instruction::And:     C = ConstRules::get(V1, V2).op_and(V1, V2); break;
   case Instruction::Or:      C = ConstRules::get(V1, V2).op_or (V1, V2); break;
@@ -1071,7 +1276,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
   // If we successfully folded the expression, return it now.
   if (C) return C;
 
-  if (SetCondInst::isRelational(Opcode)) {
+  if (SetCondInst::isComparison(Opcode)) {
     if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
       return UndefValue::get(Type::BoolTy);
     switch (evaluateRelation(const_cast<Constant*>(V1),
@@ -1099,20 +1304,20 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
                                Opcode == Instruction::SetGE);
     case Instruction::SetLE:
       // If we know that V1 <= V2, we can only partially decide this relation.
-      if (Opcode == Instruction::SetGT) return ConstantBool::False;
-      if (Opcode == Instruction::SetLT) return ConstantBool::True;
+      if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
+      if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
       break;
 
     case Instruction::SetGE:
       // If we know that V1 >= V2, we can only partially decide this relation.
-      if (Opcode == Instruction::SetLT) return ConstantBool::False;
-      if (Opcode == Instruction::SetGT) return ConstantBool::True;
+      if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
+      if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
       break;
 
     case Instruction::SetNE:
       // If we know that V1 != V2, we can only partially decide this relation.
-      if (Opcode == Instruction::SetEQ) return ConstantBool::False;
-      if (Opcode == Instruction::SetNE) return ConstantBool::True;
+      if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
+      if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
       break;
     }
   }
@@ -1127,7 +1332,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
     case Instruction::Mul:
     case Instruction::And:
       return Constant::getNullValue(V1->getType());
-    case Instruction::Div:
+    case Instruction::UDiv:
+    case Instruction::SDiv:
+    case Instruction::FDiv:
     case Instruction::Rem:
       if (!isa<UndefValue>(V2))     // undef/X -> 0
         return Constant::getNullValue(V1->getType());
@@ -1175,17 +1382,18 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
       case Instruction::Mul:
         if (V2->isNullValue()) return const_cast<Constant*>(V2);  // X * 0 == 0
         if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
-          if (CI->getRawValue() == 1)
+          if (CI->getZExtValue() == 1)
             return const_cast<Constant*>(V1);                     // X * 1 == X
         break;
-      case Instruction::Div:
+      case Instruction::UDiv:
+      case Instruction::SDiv:
         if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
-          if (CI->getRawValue() == 1)
+          if (CI->getZExtValue() == 1)
             return const_cast<Constant*>(V1);                     // X / 1 == X
         break;
       case Instruction::Rem:
         if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
-          if (CI->getRawValue() == 1)
+          if (CI->getZExtValue() == 1)
             return Constant::getNullValue(CI->getType()); // X % 1 == 0
         break;
       case Instruction::And:
@@ -1199,7 +1407,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
           // Functions are at least 4-byte aligned.  If and'ing the address of a
           // function with a constant < 4, fold it to zero.
           if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
-            if (CI->getRawValue() < 4 && isa<Function>(CPR))
+            if (CI->getZExtValue() < 4 && isa<Function>(CPR))
               return Constant::getNullValue(CI->getType());
         }
         break;
@@ -1239,7 +1447,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
     case Instruction::Shl:
     case Instruction::Shr:
     case Instruction::Sub:
-    case Instruction::Div:
+    case Instruction::SDiv:
+    case Instruction::UDiv:
+    case Instruction::FDiv:
     case Instruction::Rem:
     default:  // These instructions cannot be flopped around.
       break;
@@ -1278,10 +1488,10 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
 
     if (IdxList.size() == 1) {
       const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
-      if (unsigned ElSize = ElTy->getPrimitiveSize()) {
+      if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
         // gep null, C is equal to C*sizeof(nullty).  If nullty is a known llvm
         // type, we can statically fold this.
-        Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
+        Constant *R = ConstantInt::get(Type::UIntTy, ElSize);
         R = ConstantExpr::getCast(R, Idx0->getType());
         R = ConstantExpr::getMul(R, Idx0);
         return ConstantExpr::getCast(R, C->getType());
@@ -1336,7 +1546,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
           dyn_cast<PointerType>(CE->getOperand(0)->getType()))
         if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
           if (const ArrayType *CAT =
-              dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
+        dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
             if (CAT->getElementType() == SAT->getElementType())
               return ConstantExpr::getGetElementPtr(
                       (Constant*)CE->getOperand(0), IdxList);