X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FAnalysis%2FExpressions.cpp;h=7549a19e09ec2dff60e38cbbbcf48564fd333b8a;hb=4bbf3dfbe614b8d62a0e695ab28ba70824c3a36e;hp=9b305bbe4097ea32d1700e001ed4f691ff021480;hpb=5fd60913da4e348b8d75ae3e7fced4a26ae4bb2f;p=oota-llvm.git diff --git a/lib/Analysis/Expressions.cpp b/lib/Analysis/Expressions.cpp index 9b305bbe409..7549a19e09e 100644 --- a/lib/Analysis/Expressions.cpp +++ b/lib/Analysis/Expressions.cpp @@ -1,4 +1,11 @@ -//===- Expressions.cpp - Expression Analysis Utilities ----------------------=// +//===- Expressions.cpp - Expression Analysis Utilities --------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// // // This file defines a package of expression analysis utilties: // @@ -8,8 +15,12 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/Expressions.h" -#include "llvm/ConstantHandling.h" +#include "llvm/Constants.h" #include "llvm/Function.h" +#include "llvm/Type.h" +#include + +using namespace llvm; ExprType::ExprType(Value *Val) { if (Val) @@ -44,27 +55,28 @@ const Type *ExprType::getExprType(const Type *Default) const { } - -class DefVal { - const ConstantInt * const Val; - const Type * const Ty; -protected: - inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {} -public: - inline const Type *getType() const { return Ty; } - inline const ConstantInt *getVal() const { return Val; } - inline operator const ConstantInt * () const { return Val; } - inline const ConstantInt *operator->() const { return Val; } -}; - -struct DefZero : public DefVal { - inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {} - inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {} -}; - -struct DefOne : public DefVal { - inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {} -}; +namespace { + class DefVal { + const ConstantInt * const Val; + const Type * const Ty; + protected: + inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {} + public: + inline const Type *getType() const { return Ty; } + inline const ConstantInt *getVal() const { return Val; } + inline operator const ConstantInt * () const { return Val; } + inline const ConstantInt *operator->() const { return Val; } + }; + + struct DefZero : public DefVal { + inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {} + inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {} + }; + + struct DefOne : public DefVal { + inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {} + }; +} // getUnsignedConstant - Return a constant value of the specified type. If the @@ -105,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(Result); // Check to see if the result is one of the special cases that we want to @@ -118,13 +129,13 @@ static const ConstantInt *Add(const ConstantInt *Arg1, return ResultI; } -inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) { +static inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) { if (L == 0) return R; if (R == 0) return L; return Add(L, R, false); } -inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) { +static inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) { if (L == 0) { if (R == 0) return getUnsignedConstant(2, L.getType()); @@ -149,13 +160,14 @@ inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) { // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne // is false, a null return value indicates a value of 0. // -inline const ConstantInt *Mul(const ConstantInt *Arg1, - const ConstantInt *Arg2, bool DefOne) { +static inline const ConstantInt *Mul(const ConstantInt *Arg1, + const ConstantInt *Arg2, bool DefOne) { assert(Arg1 && Arg2 && "No null arguments should exist now!"); 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(Result); @@ -168,18 +180,20 @@ inline const ConstantInt *Mul(const ConstantInt *Arg1, return ResultI; } -inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) { - if (L == 0 || R == 0) return 0; - return Mul(L, R, false); -} -inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) { - if (R == 0) return getUnsignedConstant(0, L.getType()); - if (L == 0) return R->equalsInt(1) ? 0 : R.getVal(); - return Mul(L, R, true); -} -inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) { - if (L == 0 || R == 0) return L.getVal(); - return Mul(R, L, false); +namespace { + inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) { + if (L == 0 || R == 0) return 0; + return Mul(L, R, false); + } + inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) { + if (R == 0) return getUnsignedConstant(0, L.getType()); + if (L == 0) return R->equalsInt(1) ? 0 : R.getVal(); + return Mul(L, R, true); + } + inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) { + if (L == 0 || R == 0) return L.getVal(); + return Mul(R, L, false); + } } // handleAddition - Add two expressions together, creating a new expression that @@ -214,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(*Zero - *One); + ConstantInt *NegOne = cast(ConstantExpr::get(Instruction::Sub, + Zero, One)); if (NegOne == 0) return V; // Couldn't subtract values... return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var, @@ -222,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 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(Expr)) { if (ConstantInt *CPI = dyn_cast(cast(Expr))) // It's an integral constant! return ExprType(CPI->isNullValue() ? 0 : CPI); return Expr; + } else if (!isa(Expr)) { + return Expr; } + Instruction *I = cast(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))); + 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... @@ -270,13 +278,13 @@ ExprType 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!"); - uint64_t ShiftAmount = ((ConstantUInt*)Right.Offset)->getValue(); + uint64_t ShiftAmount = cast(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 @@ -297,8 +305,8 @@ ExprType 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 @@ -312,7 +320,7 @@ ExprType 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(DestTy)) DestTy = Type::ULongTy; // Pointer types are represented as ulong @@ -327,12 +335,12 @@ ExprType 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(CPV)) return I; Offset = cast(CPV); } if (Scale) { - const Constant *CPV = ConstantFoldCastInstruction(Scale, DestTy); + const Constant *CPV = ConstantExpr::getCast((Constant*)Scale, DestTy); if (!CPV) return I; Scale = cast(CPV); }