There is nothing special about noops now
[oota-llvm.git] / lib / Analysis / Expressions.cpp
index d0ab480772e89158e073f25432c87390ac2cb4ea..7733b818191d973a489a5c0ad8f96e49c16649c7 100644 (file)
@@ -8,12 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/Expressions.h"
-#include "llvm/Optimizations/ConstantHandling.h"
-#include "llvm/Method.h"
-#include "llvm/BasicBlock.h"
-
-using namespace opt;  // Get all the constant handling stuff
-using namespace analysis;
+#include "llvm/ConstantHandling.h"
+#include "llvm/Function.h"
 
 ExprType::ExprType(Value *Val) {
   if (Val) 
@@ -34,7 +30,7 @@ ExprType::ExprType(const ConstantInt *scale, Value *var,
                   const ConstantInt *offset) {
   Scale = var ? scale : 0; Var = var; Offset = offset;
   ExprTy = Scale ? ScaledLinear : (Var ? Linear : Constant);
-  if (Scale && Scale->equalsInt(0)) {  // Simplify 0*Var + const
+  if (Scale && Scale->isNullValue()) {  // Simplify 0*Var + const
     Scale = 0; Var = 0;
     ExprTy = Constant;
   }
@@ -71,10 +67,24 @@ struct DefOne : public DefVal {
 };
 
 
+// getUnsignedConstant - Return a constant value of the specified type.  If the
+// constant value is not valid for the specified type, return null.  This cannot
+// happen for values in the range of 0 to 127.
+//
 static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
-  if (Ty->isPointerType()) Ty = Type::ULongTy;
-  return Ty->isSigned() ? (ConstantInt*)ConstantSInt::get(Ty, V)
-                        : (ConstantInt*)ConstantUInt::get(Ty, V);
+  if (isa<PointerType>(Ty)) Ty = Type::ULongTy;
+  if (Ty->isSigned()) {
+    // If this value is not a valid unsigned value for this type, return null!
+    if (V > 127 && ((int64_t)V < 0 ||
+                    !ConstantSInt::isValueValidForType(Ty, (int64_t)V)))
+      return 0;
+    return ConstantSInt::get(Ty, V);
+  } else {
+    // If this value is not a valid unsigned value for this type, return null!
+    if (V > 255 && !ConstantUInt::isValueValidForType(Ty, V))
+      return 0;
+    return ConstantUInt::get(Ty, V);
+  }
 }
 
 // Add - Helper function to make later code simpler.  Basically it just adds
@@ -178,7 +188,7 @@ inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) {
 static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
   const Type *Ty = V->getType();
   if (Left.ExprTy > Right.ExprTy)
-    swap(Left, Right);   // Make left be simpler than right
+    std::swap(Left, Right);   // Make left be simpler than right
 
   switch (Left.ExprTy) {
   case ExprType::Constant:
@@ -202,9 +212,8 @@ static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
 //
 static inline ExprType negate(const ExprType &E, Value *V) {
   const Type *Ty = V->getType();
-  const Type *ETy = E.getExprType(Ty);
-  ConstantInt *Zero   = getUnsignedConstant(0, ETy);
-  ConstantInt *One    = getUnsignedConstant(1, ETy);
+  ConstantInt *Zero   = getUnsignedConstant(0, Ty);
+  ConstantInt *One    = getUnsignedConstant(1, Ty);
   ConstantInt *NegOne = cast<ConstantInt>(*Zero - *One);
   if (NegOne == 0) return V;  // Couldn't subtract values...
 
@@ -219,29 +228,32 @@ static inline ExprType negate(const ExprType &E, Value *V) {
 // Note that this analysis cannot get into infinite loops because it treats PHI
 // nodes as being an unknown linear expression.
 //
-ExprType analysis::ClassifyExpression(Value *Expr) {
+ExprType ClassifyExpression(Value *Expr) {
   assert(Expr != 0 && "Can't classify a null expression!");
+  if (Expr->getType() == Type::FloatTy || Expr->getType() == Type::DoubleTy)
+    return Expr;   // FIXME: Can't handle FP expressions
+
   switch (Expr->getValueType()) {
   case Value::InstructionVal: break;    // Instruction... hmmm... investigate.
   case Value::TypeVal:   case Value::BasicBlockVal:
-  case Value::MethodVal: case Value::ModuleVal: default:
-    assert(0 && "Unexpected expression type to classify!");
-  case Value::GlobalVariableVal:        // Global Variable & Method argument:
-  case Value::MethodArgumentVal:        // nothing known, return variable itself
+  case Value::FunctionVal: default:
+    //assert(0 && "Unexpected expression type to classify!");
+    std::cerr << "Bizarre thing to expr classify: " << Expr << "\n";
+    return Expr;
+  case Value::GlobalVariableVal:        // Global Variable & Function argument:
+  case Value::ArgumentVal:              // nothing known, return variable itself
     return Expr;
   case Value::ConstantVal:              // Constant value, just return constant
-    Constant *CPV = cast<Constant>(Expr);
-    if (CPV->getType()->isIntegral()) { // It's an integral constant!
-      ConstantInt *CPI = cast<ConstantInt>(Expr);
-      return ExprType(CPI->equalsInt(0) ? 0 : CPI);
-    }
+    if (ConstantInt *CPI = dyn_cast<ConstantInt>(cast<Constant>(Expr)))
+      // It's an integral constant!
+      return ExprType(CPI->isNullValue() ? 0 : CPI);
     return Expr;
   }
   
   Instruction *I = cast<Instruction>(Expr);
   const Type *Ty = I->getType();
 
-  switch (I->getOpcode()) {       // Handle each instruction type seperately
+  switch (I->getOpcode()) {       // Handle each instruction type separately
   case Instruction::Add: {
     ExprType Left (ClassifyExpression(I->getOperand(0)));
     ExprType Right(ClassifyExpression(I->getOperand(1)));
@@ -264,9 +276,22 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
     if (Right.Offset == 0) return Left;   // shl x, 0 = x
     assert(Right.Offset->getType() == Type::UByteTy &&
           "Shift amount must always be a unsigned byte!");
-    uint64_t ShiftAmount = ((ConstantUInt*)Right.Offset)->getValue();
+    uint64_t ShiftAmount = cast<ConstantUInt>(Right.Offset)->getValue();
     ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
-    
+
+    // We don't know how to classify it if they are shifting by more than what
+    // is reasonable.  In most cases, the result will be zero, but there is one
+    // class of cases where it is not, so we cannot optimize without checking
+    // for it.  The case is when you are shifting a signed value by 1 less than
+    // the number of bits in the value.  For example:
+    //    %X = shl sbyte %Y, ubyte 7
+    // will try to form an sbyte multiplier of 128, which will give a null
+    // multiplier, even though the result is not 0.  Until we can check for this
+    // case, be conservative.  TODO.
+    //
+    if (Multiplier == 0)
+      return Expr;
+
     return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
                    DefZero(Left.Offset, Ty) * Multiplier);
   }  // end case Instruction::Shl
@@ -275,7 +300,7 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
     ExprType Left (ClassifyExpression(I->getOperand(0)));
     ExprType Right(ClassifyExpression(I->getOperand(1)));
     if (Left.ExprTy > Right.ExprTy)
-      swap(Left, Right);   // Make left be simpler than right
+      std::swap(Left, Right);   // Make left be simpler than right
 
     if (Left.ExprTy != ExprType::Constant)  // RHS must be > constant
       return I;         // Quadratic eqn! :(
@@ -289,15 +314,15 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
   case Instruction::Cast: {
     ExprType Src(ClassifyExpression(I->getOperand(0)));
     const Type *DestTy = I->getType();
-    if (DestTy->isPointerType())
+    if (isa<PointerType>(DestTy))
       DestTy = Type::ULongTy;  // Pointer types are represented as ulong
 
-    /*
-    if (!Src.getExprType(0)->isLosslesslyConvertableTo(DestTy)) {
+    const Type *SrcValTy = Src.getExprType(0);
+    if (!SrcValTy) return I;
+    if (!SrcValTy->isLosslesslyConvertibleTo(DestTy)) {
       if (Src.ExprTy != ExprType::Constant)
         return I;  // Converting cast, and not a constant value...
     }
-    */
 
     const ConstantInt *Offset = Src.Offset;
     const ConstantInt *Scale  = Src.Scale;