For PR950:
The long awaited CAST patch. This introduces 12 new instructions into LLVM
to replace the cast instruction. Corresponding changes throughout LLVM are
provided. This passes llvm-test, llvm/test, and SPEC CPUINT2000 with the
exception of 175.vpr which fails only on a slight floating point output
difference.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31931 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp
index cd70005..8c809f8 100644
--- a/lib/Analysis/ScalarEvolution.cpp
+++ b/lib/Analysis/ScalarEvolution.cpp
@@ -203,7 +203,6 @@
 SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty)
   : SCEV(scTruncate), Op(op), Ty(ty) {
   assert(Op->getType()->isInteger() && Ty->isInteger() &&
-         Ty->isUnsigned() &&
          "Cannot truncate non-integer value!");
   assert(Op->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
          "This is not a truncating conversion!");
@@ -230,7 +229,6 @@
 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty)
   : SCEV(scZeroExtend), Op(op), Ty(ty) {
   assert(Op->getType()->isInteger() && Ty->isInteger() &&
-         Ty->isUnsigned() &&
          "Cannot zero extend non-integer value!");
   assert(Op->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
          "This is not an extending conversion!");
@@ -1139,7 +1137,6 @@
     /// createSCEV - We know that there is no SCEV for the specified value.
     /// Analyze the expression.
     SCEVHandle createSCEV(Value *V);
-    SCEVHandle createNodeForCast(CastInst *CI);
 
     /// createNodeForPHI - Provide the special handling we need to analyze PHI
     /// SCEVs.
@@ -1341,35 +1338,6 @@
   return SCEVUnknown::get(PN);
 }
 
-/// createNodeForCast - Handle the various forms of casts that we support.
-///
-SCEVHandle ScalarEvolutionsImpl::createNodeForCast(CastInst *CI) {
-  const Type *SrcTy = CI->getOperand(0)->getType();
-  const Type *DestTy = CI->getType();
-
-  // If this is a noop cast (ie, conversion from int to uint), ignore it.
-  if (SrcTy->isLosslesslyConvertibleTo(DestTy))
-    return getSCEV(CI->getOperand(0));
-
-  if (SrcTy->isInteger() && DestTy->isInteger()) {
-    // Otherwise, if this is a truncating integer cast, we can represent this
-    // cast.
-    if (SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
-      return SCEVTruncateExpr::get(getSCEV(CI->getOperand(0)),
-                                   CI->getType()->getUnsignedVersion());
-    if (SrcTy->isUnsigned() &&
-        SrcTy->getPrimitiveSize() <= DestTy->getPrimitiveSize())
-      return SCEVZeroExtendExpr::get(getSCEV(CI->getOperand(0)),
-                                     CI->getType()->getUnsignedVersion());
-  }
-
-  // If this is an sign or zero extending cast and we can prove that the value
-  // will never overflow, we could do similar transformations.
-
-  // Otherwise, we can't handle this cast!
-  return SCEVUnknown::get(CI);
-}
-
 
 /// createSCEV - We know that there is no SCEV for the specified value.
 /// Analyze the expression.
@@ -1401,8 +1369,21 @@
       }
       break;
 
-    case Instruction::Cast:
-      return createNodeForCast(cast<CastInst>(I));
+    case Instruction::Trunc:
+      if (I->getType()->isInteger() && I->getOperand(0)->getType()->isInteger())
+        return SCEVTruncateExpr::get(getSCEV(I->getOperand(0)), 
+                                     I->getType()->getUnsignedVersion());
+      break;
+
+    case Instruction::ZExt:
+      if (I->getType()->isInteger() && I->getOperand(0)->getType()->isInteger())
+        return SCEVZeroExtendExpr::get(getSCEV(I->getOperand(0)), 
+                                       I->getType()->getUnsignedVersion());
+      break;
+
+    case Instruction::BitCast:
+      // BitCasts are no-op casts so we just eliminate the cast.
+      return getSCEV(I->getOperand(0));
 
     case Instruction::PHI:
       return createNodeForPHI(cast<PHINode>(I));
@@ -1724,9 +1705,10 @@
   if (isa<BinaryOperator>(I) || isa<ShiftInst>(I))
     return ConstantExpr::get(I->getOpcode(), Operands[0], Operands[1]);
 
+  if (isa<CastInst>(I))
+    return ConstantExpr::getCast(I->getOpcode(), Operands[0], I->getType());
+
   switch (I->getOpcode()) {
-  case Instruction::Cast:
-    return ConstantExpr::getCast(Operands[0], I->getType());
   case Instruction::Select:
     return ConstantExpr::getSelect(Operands[0], Operands[1], Operands[2]);
   case Instruction::Call:
@@ -1734,7 +1716,6 @@
       Operands.erase(Operands.begin());
       return ConstantFoldCall(cast<Function>(GV), Operands);
     }
-
     return 0;
   case Instruction::GetElementPtr:
     Constant *Base = Operands[0];