2003-10-13 05:32:08 +02:00
|
|
|
//===- Expressions.cpp - Expression Analysis Utilities --------------------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// 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.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-20 21:17:55 +02:00
|
|
|
//
|
|
|
|
// This file defines a package of expression analysis utilties:
|
|
|
|
//
|
|
|
|
// ClassifyExpression: Analyze an expression to determine the complexity of the
|
2005-04-21 23:13:18 +02:00
|
|
|
// expression, and which other variables it depends on.
|
2001-07-20 21:17:55 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/Expressions.h"
|
2004-01-12 19:02:15 +01:00
|
|
|
#include "llvm/Constants.h"
|
2002-03-26 18:55:33 +01:00
|
|
|
#include "llvm/Function.h"
|
2004-01-12 19:02:15 +01:00
|
|
|
#include "llvm/Type.h"
|
2004-07-04 14:19:56 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2001-09-11 06:27:34 +02:00
|
|
|
ExprType::ExprType(Value *Val) {
|
2005-04-21 23:13:18 +02:00
|
|
|
if (Val)
|
2001-12-03 23:26:30 +01:00
|
|
|
if (ConstantInt *CPI = dyn_cast<ConstantInt>(Val)) {
|
2001-10-01 22:11:19 +02:00
|
|
|
Offset = CPI;
|
|
|
|
Var = 0;
|
|
|
|
ExprTy = Constant;
|
|
|
|
Scale = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Var = Val; Offset = 0;
|
|
|
|
ExprTy = Var ? Linear : Constant;
|
2001-09-11 06:27:34 +02:00
|
|
|
Scale = 0;
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:13:18 +02:00
|
|
|
ExprType::ExprType(const ConstantInt *scale, Value *var,
|
2005-04-22 06:01:18 +02:00
|
|
|
const ConstantInt *offset) {
|
2001-11-26 19:53:07 +01:00
|
|
|
Scale = var ? scale : 0; Var = var; Offset = offset;
|
2001-09-11 06:27:34 +02:00
|
|
|
ExprTy = Scale ? ScaledLinear : (Var ? Linear : Constant);
|
2002-09-03 03:05:48 +02:00
|
|
|
if (Scale && Scale->isNullValue()) { // Simplify 0*Var + const
|
2001-09-11 06:27:34 +02:00
|
|
|
Scale = 0; Var = 0;
|
|
|
|
ExprTy = Constant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Type *ExprType::getExprType(const Type *Default) const {
|
|
|
|
if (Offset) return Offset->getType();
|
|
|
|
if (Scale) return Scale->getType();
|
|
|
|
return Var ? Var->getType() : Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
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; }
|
|
|
|
};
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
struct DefZero : public DefVal {
|
|
|
|
inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
|
|
|
|
inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {}
|
|
|
|
};
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
struct DefOne : public DefVal {
|
|
|
|
inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
|
|
|
|
};
|
|
|
|
}
|
2001-07-21 21:07:19 +02:00
|
|
|
|
2001-07-20 21:17:55 +02:00
|
|
|
|
2002-03-14 23:35:50 +01:00
|
|
|
// 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.
|
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
|
2002-05-06 18:15:30 +02:00
|
|
|
if (isa<PointerType>(Ty)) Ty = Type::ULongTy;
|
2002-03-14 23:35:50 +01:00
|
|
|
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);
|
|
|
|
}
|
2001-07-20 21:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add - Helper function to make later code simpler. Basically it just adds
|
|
|
|
// the two constants together, inserts the result into the constant pool, and
|
|
|
|
// returns it. Of course life is not simple, and this is no exception. Factors
|
|
|
|
// that complicate matters:
|
|
|
|
// 1. Either argument may be null. If this is the case, the null argument is
|
|
|
|
// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
|
|
|
|
// 2. Types get in the way. We want to do arithmetic operations without
|
|
|
|
// regard for the underlying types. It is assumed that the constants are
|
|
|
|
// integral constants. The new value takes the type of the left argument.
|
|
|
|
// 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.
|
|
|
|
//
|
2001-12-03 23:26:30 +01:00
|
|
|
static const ConstantInt *Add(const ConstantInt *Arg1,
|
|
|
|
const ConstantInt *Arg2, bool DefOne) {
|
2001-07-20 21:17:55 +02:00
|
|
|
assert(Arg1 && Arg2 && "No null arguments should exist now!");
|
2001-07-21 21:07:19 +02:00
|
|
|
assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
// Actually perform the computation now!
|
2004-01-12 19:02:15 +01:00
|
|
|
Constant *Result = ConstantExpr::get(Instruction::Add, (Constant*)Arg1,
|
|
|
|
(Constant*)Arg2);
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantInt *ResultI = cast<ConstantInt>(Result);
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
// Check to see if the result is one of the special cases that we want to
|
|
|
|
// recognize...
|
2001-09-11 06:27:34 +02:00
|
|
|
if (ResultI->equalsInt(DefOne ? 1 : 0))
|
|
|
|
return 0; // Yes it is, simply return null.
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
return ResultI;
|
|
|
|
}
|
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
static inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) {
|
2001-07-21 21:07:19 +02:00
|
|
|
if (L == 0) return R;
|
|
|
|
if (R == 0) return L;
|
2001-09-07 18:31:04 +02:00
|
|
|
return Add(L, R, false);
|
2001-07-21 21:07:19 +02:00
|
|
|
}
|
2001-07-20 21:17:55 +02:00
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
static inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) {
|
2001-07-21 21:07:19 +02:00
|
|
|
if (L == 0) {
|
|
|
|
if (R == 0)
|
2001-09-11 06:27:34 +02:00
|
|
|
return getUnsignedConstant(2, L.getType());
|
2001-07-21 21:07:19 +02:00
|
|
|
else
|
2001-09-11 06:27:34 +02:00
|
|
|
return Add(getUnsignedConstant(1, L.getType()), R, true);
|
2001-07-21 21:07:19 +02:00
|
|
|
} else if (R == 0) {
|
2001-09-11 06:27:34 +02:00
|
|
|
return Add(L, getUnsignedConstant(1, L.getType()), true);
|
2001-07-21 21:07:19 +02:00
|
|
|
}
|
2001-09-07 18:31:04 +02:00
|
|
|
return Add(L, R, true);
|
2001-07-20 21:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-21 21:07:19 +02:00
|
|
|
// Mul - Helper function to make later code simpler. Basically it just
|
2001-07-20 21:17:55 +02:00
|
|
|
// multiplies the two constants together, inserts the result into the constant
|
|
|
|
// pool, and returns it. Of course life is not simple, and this is no
|
|
|
|
// exception. Factors that complicate matters:
|
|
|
|
// 1. Either argument may be null. If this is the case, the null argument is
|
|
|
|
// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
|
|
|
|
// 2. Types get in the way. We want to do arithmetic operations without
|
|
|
|
// regard for the underlying types. It is assumed that the constants are
|
|
|
|
// integral constants.
|
|
|
|
// 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.
|
|
|
|
//
|
2005-04-21 23:13:18 +02:00
|
|
|
static inline const ConstantInt *Mul(const ConstantInt *Arg1,
|
2003-12-23 07:44:41 +01:00
|
|
|
const ConstantInt *Arg2, bool DefOne) {
|
2001-07-20 21:17:55 +02:00
|
|
|
assert(Arg1 && Arg2 && "No null arguments should exist now!");
|
2001-07-21 21:07:19 +02:00
|
|
|
assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
// Actually perform the computation now!
|
2004-01-12 19:02:15 +01:00
|
|
|
Constant *Result = ConstantExpr::get(Instruction::Mul, (Constant*)Arg1,
|
|
|
|
(Constant*)Arg2);
|
2005-04-21 23:13:18 +02:00
|
|
|
assert(Result && Result->getType() == Arg1->getType() &&
|
2005-04-22 06:01:18 +02:00
|
|
|
"Couldn't perform multiplication!");
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantInt *ResultI = cast<ConstantInt>(Result);
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
// Check to see if the result is one of the special cases that we want to
|
|
|
|
// recognize...
|
2001-09-11 06:27:34 +02:00
|
|
|
if (ResultI->equalsInt(DefOne ? 1 : 0))
|
|
|
|
return 0; // Yes it is, simply return null.
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
return ResultI;
|
|
|
|
}
|
|
|
|
|
2003-12-23 07:44:41 +01:00
|
|
|
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);
|
|
|
|
}
|
2001-07-21 21:07:19 +02:00
|
|
|
}
|
|
|
|
|
2001-09-11 06:27:34 +02:00
|
|
|
// handleAddition - Add two expressions together, creating a new expression that
|
|
|
|
// represents the composite of the two...
|
|
|
|
//
|
|
|
|
static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
|
|
|
|
const Type *Ty = V->getType();
|
|
|
|
if (Left.ExprTy > Right.ExprTy)
|
2002-01-20 23:54:45 +01:00
|
|
|
std::swap(Left, Right); // Make left be simpler than right
|
2001-09-11 06:27:34 +02:00
|
|
|
|
|
|
|
switch (Left.ExprTy) {
|
|
|
|
case ExprType::Constant:
|
2001-11-26 19:53:07 +01:00
|
|
|
return ExprType(Right.Scale, Right.Var,
|
2005-04-22 06:01:18 +02:00
|
|
|
DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
|
2001-09-11 06:27:34 +02:00
|
|
|
case ExprType::Linear: // RHS side must be linear or scaled
|
|
|
|
case ExprType::ScaledLinear: // RHS must be scaled
|
|
|
|
if (Left.Var != Right.Var) // Are they the same variables?
|
2001-11-26 19:53:07 +01:00
|
|
|
return V; // if not, we don't know anything!
|
2001-09-11 06:27:34 +02:00
|
|
|
|
|
|
|
return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
|
2005-04-22 06:01:18 +02:00
|
|
|
Right.Var,
|
|
|
|
DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
|
2001-09-11 06:27:34 +02:00
|
|
|
default:
|
|
|
|
assert(0 && "Dont' know how to handle this case!");
|
|
|
|
return ExprType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// negate - Negate the value of the specified expression...
|
|
|
|
//
|
|
|
|
static inline ExprType negate(const ExprType &E, Value *V) {
|
|
|
|
const Type *Ty = V->getType();
|
2002-03-11 21:50:24 +01:00
|
|
|
ConstantInt *Zero = getUnsignedConstant(0, Ty);
|
|
|
|
ConstantInt *One = getUnsignedConstant(1, Ty);
|
2004-01-12 19:02:15 +01:00
|
|
|
ConstantInt *NegOne = cast<ConstantInt>(ConstantExpr::get(Instruction::Sub,
|
|
|
|
Zero, One));
|
2001-09-11 06:27:34 +02:00
|
|
|
if (NegOne == 0) return V; // Couldn't subtract values...
|
|
|
|
|
|
|
|
return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
|
2005-04-22 06:01:18 +02:00
|
|
|
DefZero(E.Offset, Ty) * NegOne);
|
2001-09-11 06:27:34 +02:00
|
|
|
}
|
2001-07-21 21:07:19 +02:00
|
|
|
|
2001-07-20 21:17:55 +02:00
|
|
|
|
2003-12-23 09:04:08 +01:00
|
|
|
// ClassifyExpr: Analyze an expression to determine the complexity of the
|
|
|
|
// expression, and which other values it depends on.
|
2001-07-20 21:17:55 +02:00
|
|
|
//
|
|
|
|
// Note that this analysis cannot get into infinite loops because it treats PHI
|
|
|
|
// nodes as being an unknown linear expression.
|
|
|
|
//
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType llvm::ClassifyExpr(Value *Expr) {
|
2001-07-20 21:17:55 +02:00
|
|
|
assert(Expr != 0 && "Can't classify a null expression!");
|
2004-06-26 21:31:26 +02:00
|
|
|
if (Expr->getType()->isFloatingPoint())
|
2001-12-05 20:38:29 +01:00
|
|
|
return Expr; // FIXME: Can't handle FP expressions
|
|
|
|
|
2004-06-26 21:31:26 +02:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Expr)) {
|
2003-04-17 00:50:19 +02:00
|
|
|
if (ConstantInt *CPI = dyn_cast<ConstantInt>(cast<Constant>(Expr)))
|
|
|
|
// It's an integral constant!
|
2002-09-03 03:05:48 +02:00
|
|
|
return ExprType(CPI->isNullValue() ? 0 : CPI);
|
2001-07-20 21:17:55 +02:00
|
|
|
return Expr;
|
2004-06-26 21:31:26 +02:00
|
|
|
} else if (!isa<Instruction>(Expr)) {
|
|
|
|
return Expr;
|
2001-07-20 21:17:55 +02:00
|
|
|
}
|
2004-06-26 21:31:26 +02:00
|
|
|
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2001-10-01 18:18:37 +02:00
|
|
|
Instruction *I = cast<Instruction>(Expr);
|
2001-07-21 21:07:19 +02:00
|
|
|
const Type *Ty = I->getType();
|
2001-07-20 21:17:55 +02:00
|
|
|
|
2003-07-14 19:26:34 +02:00
|
|
|
switch (I->getOpcode()) { // Handle each instruction type separately
|
2001-07-20 21:17:55 +02:00
|
|
|
case Instruction::Add: {
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType Left (ClassifyExpr(I->getOperand(0)));
|
|
|
|
ExprType Right(ClassifyExpr(I->getOperand(1)));
|
2001-09-11 06:27:34 +02:00
|
|
|
return handleAddition(Left, Right, I);
|
2001-07-20 21:17:55 +02:00
|
|
|
} // end case Instruction::Add
|
|
|
|
|
2001-09-11 06:27:34 +02:00
|
|
|
case Instruction::Sub: {
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType Left (ClassifyExpr(I->getOperand(0)));
|
|
|
|
ExprType Right(ClassifyExpr(I->getOperand(1)));
|
2001-11-26 19:53:07 +01:00
|
|
|
ExprType RightNeg = negate(Right, I);
|
|
|
|
if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale)
|
|
|
|
return I; // Could not negate value...
|
|
|
|
return handleAddition(Left, RightNeg, I);
|
2001-09-11 06:27:34 +02:00
|
|
|
} // end case Instruction::Sub
|
|
|
|
|
2005-04-21 23:13:18 +02:00
|
|
|
case Instruction::Shl: {
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType Right(ClassifyExpr(I->getOperand(1)));
|
2001-07-21 21:07:19 +02:00
|
|
|
if (Right.ExprTy != ExprType::Constant) break;
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType Left(ClassifyExpr(I->getOperand(0)));
|
2001-07-21 21:07:19 +02:00
|
|
|
if (Right.Offset == 0) return Left; // shl x, 0 = x
|
|
|
|
assert(Right.Offset->getType() == Type::UByteTy &&
|
2005-04-22 06:01:18 +02:00
|
|
|
"Shift amount must always be a unsigned byte!");
|
2003-07-23 17:16:40 +02:00
|
|
|
uint64_t ShiftAmount = cast<ConstantUInt>(Right.Offset)->getValue();
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
|
2002-03-14 23:35:50 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2001-09-07 18:31:04 +02:00
|
|
|
return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
|
2005-04-22 06:01:18 +02:00
|
|
|
DefZero(Left.Offset, Ty) * Multiplier);
|
2001-07-20 21:17:55 +02:00
|
|
|
} // end case Instruction::Shl
|
|
|
|
|
2001-07-21 21:07:19 +02:00
|
|
|
case Instruction::Mul: {
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType Left (ClassifyExpr(I->getOperand(0)));
|
|
|
|
ExprType Right(ClassifyExpr(I->getOperand(1)));
|
2001-07-21 21:07:19 +02:00
|
|
|
if (Left.ExprTy > Right.ExprTy)
|
2002-01-20 23:54:45 +01:00
|
|
|
std::swap(Left, Right); // Make left be simpler than right
|
2001-07-21 21:07:19 +02:00
|
|
|
|
|
|
|
if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
|
|
|
|
return I; // Quadratic eqn! :(
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
const ConstantInt *Offs = Left.Offset;
|
2001-07-21 21:07:19 +02:00
|
|
|
if (Offs == 0) return ExprType();
|
2001-09-07 18:31:04 +02:00
|
|
|
return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
|
2005-04-22 06:01:18 +02:00
|
|
|
DefZero(Right.Offset, Ty) * Offs);
|
2001-07-21 21:07:19 +02:00
|
|
|
} // end case Instruction::Mul
|
|
|
|
|
|
|
|
case Instruction::Cast: {
|
2003-12-23 09:04:08 +01:00
|
|
|
ExprType Src(ClassifyExpr(I->getOperand(0)));
|
2001-09-11 06:27:34 +02:00
|
|
|
const Type *DestTy = I->getType();
|
2002-05-06 18:15:30 +02:00
|
|
|
if (isa<PointerType>(DestTy))
|
2001-09-11 06:27:34 +02:00
|
|
|
DestTy = Type::ULongTy; // Pointer types are represented as ulong
|
2001-07-21 21:07:19 +02:00
|
|
|
|
2003-07-01 23:08:52 +02:00
|
|
|
const Type *SrcValTy = Src.getExprType(0);
|
|
|
|
if (!SrcValTy) return I;
|
|
|
|
if (!SrcValTy->isLosslesslyConvertibleTo(DestTy)) {
|
2001-11-26 17:53:50 +01:00
|
|
|
if (Src.ExprTy != ExprType::Constant)
|
|
|
|
return I; // Converting cast, and not a constant value...
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
const ConstantInt *Offset = Src.Offset;
|
|
|
|
const ConstantInt *Scale = Src.Scale;
|
2001-11-26 17:53:50 +01:00
|
|
|
if (Offset) {
|
2004-01-12 19:02:15 +01:00
|
|
|
const Constant *CPV = ConstantExpr::getCast((Constant*)Offset, DestTy);
|
|
|
|
if (!isa<ConstantInt>(CPV)) return I;
|
2001-12-03 23:26:30 +01:00
|
|
|
Offset = cast<ConstantInt>(CPV);
|
2001-11-26 17:53:50 +01:00
|
|
|
}
|
|
|
|
if (Scale) {
|
2004-01-12 19:02:15 +01:00
|
|
|
const Constant *CPV = ConstantExpr::getCast((Constant*)Scale, DestTy);
|
2001-11-26 17:53:50 +01:00
|
|
|
if (!CPV) return I;
|
2001-12-03 23:26:30 +01:00
|
|
|
Scale = cast<ConstantInt>(CPV);
|
2001-11-26 17:53:50 +01:00
|
|
|
}
|
|
|
|
return ExprType(Scale, Src.Var, Offset);
|
2001-07-21 21:07:19 +02:00
|
|
|
} // end case Instruction::Cast
|
2001-07-26 00:47:32 +02:00
|
|
|
// TODO: Handle SUB, SHR?
|
2001-07-20 21:17:55 +02:00
|
|
|
|
|
|
|
} // end switch
|
|
|
|
|
|
|
|
// Otherwise, I don't know anything about this value!
|
2001-07-21 21:07:19 +02:00
|
|
|
return I;
|
2001-07-20 21:17:55 +02:00
|
|
|
}
|