Enhance the TD pass to build composite graphs when we have indirect call
[oota-llvm.git] / lib / Analysis / Expressions.cpp
index 5e07150f2f40d2c0d44b0b9795e249eaa2306b79..7549a19e09ec2dff60e38cbbbcf48564fd333b8a 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/Expressions.h"
-#include "llvm/ConstantHandling.h"
+#include "llvm/Constants.h"
 #include "llvm/Function.h"
+#include "llvm/Type.h"
+#include <iostream>
+
 using namespace llvm;
 
 ExprType::ExprType(Value *Val) {
@@ -114,9 +117,8 @@ static const ConstantInt *Add(const ConstantInt *Arg1,
   assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
 
   // Actually perform the computation now!
-  Constant *Result = *Arg1 + *Arg2;
-  assert(Result && Result->getType() == Arg1->getType() &&
-        "Couldn't perform addition!");
+  Constant *Result = ConstantExpr::get(Instruction::Add, (Constant*)Arg1,
+                                       (Constant*)Arg2);
   ConstantInt *ResultI = cast<ConstantInt>(Result);
 
   // Check to see if the result is one of the special cases that we want to
@@ -164,7 +166,8 @@ static inline const ConstantInt *Mul(const ConstantInt *Arg1,
   assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
 
   // Actually perform the computation now!
-  Constant *Result = *Arg1 * *Arg2;
+  Constant *Result = ConstantExpr::get(Instruction::Mul, (Constant*)Arg1,
+                                       (Constant*)Arg2);
   assert(Result && Result->getType() == Arg1->getType() && 
         "Couldn't perform multiplication!");
   ConstantInt *ResultI = cast<ConstantInt>(Result);
@@ -225,7 +228,8 @@ static inline ExprType negate(const ExprType &E, Value *V) {
   const Type *Ty = V->getType();
   ConstantInt *Zero   = getUnsignedConstant(0, Ty);
   ConstantInt *One    = getUnsignedConstant(1, Ty);
-  ConstantInt *NegOne = cast<ConstantInt>(*Zero - *One);
+  ConstantInt *NegOne = cast<ConstantInt>(ConstantExpr::get(Instruction::Sub,
+                                                            Zero, One));
   if (NegOne == 0) return V;  // Couldn't subtract values...
 
   return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
@@ -233,47 +237,40 @@ static inline ExprType negate(const ExprType &E, Value *V) {
 }
 
 
-// ClassifyExpression: Analyze an expression to determine the complexity of the
-// expression, and which other values it depends on.  
+// ClassifyExpr: Analyze an expression to determine the complexity of the
+// expression, and which other values it depends on.
 //
 // Note that this analysis cannot get into infinite loops because it treats PHI
 // nodes as being an unknown linear expression.
 //
-ExprType llvm::ClassifyExpression(Value *Expr) {
+ExprType llvm::ClassifyExpr(Value *Expr) {
   assert(Expr != 0 && "Can't classify a null expression!");
-  if (Expr->getType() == Type::FloatTy || Expr->getType() == Type::DoubleTy)
+  if (Expr->getType()->isFloatingPoint())
     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::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
+  if (Constant *C = dyn_cast<Constant>(Expr)) {
     if (ConstantInt *CPI = dyn_cast<ConstantInt>(cast<Constant>(Expr)))
       // It's an integral constant!
       return ExprType(CPI->isNullValue() ? 0 : CPI);
     return Expr;
+  } else if (!isa<Instruction>(Expr)) {
+    return Expr;
   }
+
   
   Instruction *I = cast<Instruction>(Expr);
   const Type *Ty = I->getType();
 
   switch (I->getOpcode()) {       // Handle each instruction type separately
   case Instruction::Add: {
-    ExprType Left (ClassifyExpression(I->getOperand(0)));
-    ExprType Right(ClassifyExpression(I->getOperand(1)));
+    ExprType Left (ClassifyExpr(I->getOperand(0)));
+    ExprType Right(ClassifyExpr(I->getOperand(1)));
     return handleAddition(Left, Right, I);
   }  // end case Instruction::Add
 
   case Instruction::Sub: {
-    ExprType Left (ClassifyExpression(I->getOperand(0)));
-    ExprType Right(ClassifyExpression(I->getOperand(1)));
+    ExprType Left (ClassifyExpr(I->getOperand(0)));
+    ExprType Right(ClassifyExpr(I->getOperand(1)));
     ExprType RightNeg = negate(Right, I);
     if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale)
       return I;   // Could not negate value...
@@ -281,9 +278,9 @@ ExprType llvm::ClassifyExpression(Value *Expr) {
   }  // end case Instruction::Sub
 
   case Instruction::Shl: { 
-    ExprType Right(ClassifyExpression(I->getOperand(1)));
+    ExprType Right(ClassifyExpr(I->getOperand(1)));
     if (Right.ExprTy != ExprType::Constant) break;
-    ExprType Left(ClassifyExpression(I->getOperand(0)));
+    ExprType Left(ClassifyExpr(I->getOperand(0)));
     if (Right.Offset == 0) return Left;   // shl x, 0 = x
     assert(Right.Offset->getType() == Type::UByteTy &&
           "Shift amount must always be a unsigned byte!");
@@ -308,8 +305,8 @@ ExprType llvm::ClassifyExpression(Value *Expr) {
   }  // end case Instruction::Shl
 
   case Instruction::Mul: {
-    ExprType Left (ClassifyExpression(I->getOperand(0)));
-    ExprType Right(ClassifyExpression(I->getOperand(1)));
+    ExprType Left (ClassifyExpr(I->getOperand(0)));
+    ExprType Right(ClassifyExpr(I->getOperand(1)));
     if (Left.ExprTy > Right.ExprTy)
       std::swap(Left, Right);   // Make left be simpler than right
 
@@ -323,7 +320,7 @@ ExprType llvm::ClassifyExpression(Value *Expr) {
   } // end case Instruction::Mul
 
   case Instruction::Cast: {
-    ExprType Src(ClassifyExpression(I->getOperand(0)));
+    ExprType Src(ClassifyExpr(I->getOperand(0)));
     const Type *DestTy = I->getType();
     if (isa<PointerType>(DestTy))
       DestTy = Type::ULongTy;  // Pointer types are represented as ulong
@@ -338,12 +335,12 @@ ExprType llvm::ClassifyExpression(Value *Expr) {
     const ConstantInt *Offset = Src.Offset;
     const ConstantInt *Scale  = Src.Scale;
     if (Offset) {
-      const Constant *CPV = ConstantFoldCastInstruction(Offset, DestTy);
-      if (!CPV) return I;
+      const Constant *CPV = ConstantExpr::getCast((Constant*)Offset, DestTy);
+      if (!isa<ConstantInt>(CPV)) return I;
       Offset = cast<ConstantInt>(CPV);
     }
     if (Scale) {
-      const Constant *CPV = ConstantFoldCastInstruction(Scale, DestTy);
+      const Constant *CPV = ConstantExpr::getCast((Constant*)Scale, DestTy);
       if (!CPV) return I;
       Scale = cast<ConstantInt>(CPV);
     }