1 //===- Expressions.cpp - Expression Analysis Utilities ----------------------=//
3 // This file defines a package of expression analysis utilties:
5 // ClassifyExpression: Analyze an expression to determine the complexity of the
6 // expression, and which other variables it depends on.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/Expressions.h"
11 #include "llvm/Optimizations/ConstantHandling.h"
12 #include "llvm/Method.h"
13 #include "llvm/BasicBlock.h"
15 using namespace opt; // Get all the constant handling stuff
16 using namespace analysis;
18 ExprType::ExprType(Value *Val) {
20 if (ConstPoolInt *CPI = dyn_cast<ConstPoolInt>(Val)) {
28 Var = Val; Offset = 0;
29 ExprTy = Var ? Linear : Constant;
33 ExprType::ExprType(const ConstPoolInt *scale, Value *var,
34 const ConstPoolInt *offset) {
35 Scale = scale; Var = var; Offset = offset;
36 ExprTy = Scale ? ScaledLinear : (Var ? Linear : Constant);
37 if (Scale && Scale->equalsInt(0)) { // Simplify 0*Var + const
44 const Type *ExprType::getExprType(const Type *Default) const {
45 if (Offset) return Offset->getType();
46 if (Scale) return Scale->getType();
47 return Var ? Var->getType() : Default;
53 const ConstPoolInt * const Val;
54 const Type * const Ty;
56 inline DefVal(const ConstPoolInt *val, const Type *ty) : Val(val), Ty(ty) {}
58 inline const Type *getType() const { return Ty; }
59 inline const ConstPoolInt *getVal() const { return Val; }
60 inline operator const ConstPoolInt * () const { return Val; }
61 inline const ConstPoolInt *operator->() const { return Val; }
64 struct DefZero : public DefVal {
65 inline DefZero(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
66 inline DefZero(const ConstPoolInt *val) : DefVal(val, val->getType()) {}
69 struct DefOne : public DefVal {
70 inline DefOne(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
74 static ConstPoolInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
75 if (Ty->isPointerType()) Ty = Type::ULongTy;
76 return Ty->isSigned() ? (ConstPoolInt*)ConstPoolSInt::get(Ty, V)
77 : (ConstPoolInt*)ConstPoolUInt::get(Ty, V);
80 // Add - Helper function to make later code simpler. Basically it just adds
81 // the two constants together, inserts the result into the constant pool, and
82 // returns it. Of course life is not simple, and this is no exception. Factors
83 // that complicate matters:
84 // 1. Either argument may be null. If this is the case, the null argument is
85 // treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
86 // 2. Types get in the way. We want to do arithmetic operations without
87 // regard for the underlying types. It is assumed that the constants are
88 // integral constants. The new value takes the type of the left argument.
89 // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
90 // is false, a null return value indicates a value of 0.
92 static const ConstPoolInt *Add(const ConstPoolInt *Arg1,
93 const ConstPoolInt *Arg2, bool DefOne) {
94 assert(Arg1 && Arg2 && "No null arguments should exist now!");
95 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
97 // Actually perform the computation now!
98 ConstPoolVal *Result = *Arg1 + *Arg2;
99 assert(Result && Result->getType() == Arg1->getType() &&
100 "Couldn't perform addition!");
101 ConstPoolInt *ResultI = cast<ConstPoolInt>(Result);
103 // Check to see if the result is one of the special cases that we want to
105 if (ResultI->equalsInt(DefOne ? 1 : 0))
106 return 0; // Yes it is, simply return null.
111 inline const ConstPoolInt *operator+(const DefZero &L, const DefZero &R) {
112 if (L == 0) return R;
113 if (R == 0) return L;
114 return Add(L, R, false);
117 inline const ConstPoolInt *operator+(const DefOne &L, const DefOne &R) {
120 return getUnsignedConstant(2, L.getType());
122 return Add(getUnsignedConstant(1, L.getType()), R, true);
124 return Add(L, getUnsignedConstant(1, L.getType()), true);
126 return Add(L, R, true);
130 // Mul - Helper function to make later code simpler. Basically it just
131 // multiplies the two constants together, inserts the result into the constant
132 // pool, and returns it. Of course life is not simple, and this is no
133 // exception. Factors that complicate matters:
134 // 1. Either argument may be null. If this is the case, the null argument is
135 // treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
136 // 2. Types get in the way. We want to do arithmetic operations without
137 // regard for the underlying types. It is assumed that the constants are
138 // integral constants.
139 // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
140 // is false, a null return value indicates a value of 0.
142 inline const ConstPoolInt *Mul(const ConstPoolInt *Arg1,
143 const ConstPoolInt *Arg2, bool DefOne = false) {
144 assert(Arg1 && Arg2 && "No null arguments should exist now!");
145 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
147 // Actually perform the computation now!
148 ConstPoolVal *Result = *Arg1 * *Arg2;
149 assert(Result && Result->getType() == Arg1->getType() &&
150 "Couldn't perform mult!");
151 ConstPoolInt *ResultI = cast<ConstPoolInt>(Result);
153 // Check to see if the result is one of the special cases that we want to
155 if (ResultI->equalsInt(DefOne ? 1 : 0))
156 return 0; // Yes it is, simply return null.
161 inline const ConstPoolInt *operator*(const DefZero &L, const DefZero &R) {
162 if (L == 0 || R == 0) return 0;
163 return Mul(L, R, false);
165 inline const ConstPoolInt *operator*(const DefOne &L, const DefZero &R) {
166 if (R == 0) return getUnsignedConstant(0, L.getType());
167 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
168 return Mul(L, R, false);
170 inline const ConstPoolInt *operator*(const DefZero &L, const DefOne &R) {
174 // handleAddition - Add two expressions together, creating a new expression that
175 // represents the composite of the two...
177 static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
178 const Type *Ty = V->getType();
179 if (Left.ExprTy > Right.ExprTy)
180 swap(Left, Right); // Make left be simpler than right
182 switch (Left.ExprTy) {
183 case ExprType::Constant:
184 return ExprType(Right.Scale, Right.Var,
185 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
186 case ExprType::Linear: // RHS side must be linear or scaled
187 case ExprType::ScaledLinear: // RHS must be scaled
188 if (Left.Var != Right.Var) // Are they the same variables?
189 return ExprType(V); // if not, we don't know anything!
191 return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
193 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
195 assert(0 && "Dont' know how to handle this case!");
200 // negate - Negate the value of the specified expression...
202 static inline ExprType negate(const ExprType &E, Value *V) {
203 const Type *Ty = V->getType();
204 const Type *ETy = E.getExprType(Ty);
205 ConstPoolInt *Zero = getUnsignedConstant(0, ETy);
206 ConstPoolInt *One = getUnsignedConstant(1, ETy);
207 ConstPoolInt *NegOne = cast<ConstPoolInt>(*Zero - *One);
208 if (NegOne == 0) return V; // Couldn't subtract values...
210 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
211 DefZero(E.Offset, Ty) * NegOne);
215 // ClassifyExpression: Analyze an expression to determine the complexity of the
216 // expression, and which other values it depends on.
218 // Note that this analysis cannot get into infinite loops because it treats PHI
219 // nodes as being an unknown linear expression.
221 ExprType analysis::ClassifyExpression(Value *Expr) {
222 assert(Expr != 0 && "Can't classify a null expression!");
223 switch (Expr->getValueType()) {
224 case Value::InstructionVal: break; // Instruction... hmmm... investigate.
225 case Value::TypeVal: case Value::BasicBlockVal:
226 case Value::MethodVal: case Value::ModuleVal: default:
227 assert(0 && "Unexpected expression type to classify!");
228 case Value::GlobalVariableVal: // Global Variable & Method argument:
229 case Value::MethodArgumentVal: // nothing known, return variable itself
231 case Value::ConstantVal: // Constant value, just return constant
232 ConstPoolVal *CPV = cast<ConstPoolVal>(Expr);
233 if (CPV->getType()->isIntegral()) { // It's an integral constant!
234 ConstPoolInt *CPI = cast<ConstPoolInt>(Expr);
235 return ExprType(CPI->equalsInt(0) ? 0 : CPI);
240 Instruction *I = cast<Instruction>(Expr);
241 const Type *Ty = I->getType();
243 switch (I->getOpcode()) { // Handle each instruction type seperately
244 case Instruction::Add: {
245 ExprType Left (ClassifyExpression(I->getOperand(0)));
246 ExprType Right(ClassifyExpression(I->getOperand(1)));
247 return handleAddition(Left, Right, I);
248 } // end case Instruction::Add
250 case Instruction::Sub: {
251 ExprType Left (ClassifyExpression(I->getOperand(0)));
252 ExprType Right(ClassifyExpression(I->getOperand(1)));
253 return handleAddition(Left, negate(Right, I), I);
254 } // end case Instruction::Sub
256 case Instruction::Shl: {
257 ExprType Right(ClassifyExpression(I->getOperand(1)));
258 if (Right.ExprTy != ExprType::Constant) break;
259 ExprType Left(ClassifyExpression(I->getOperand(0)));
260 if (Right.Offset == 0) return Left; // shl x, 0 = x
261 assert(Right.Offset->getType() == Type::UByteTy &&
262 "Shift amount must always be a unsigned byte!");
263 uint64_t ShiftAmount = ((ConstPoolUInt*)Right.Offset)->getValue();
264 ConstPoolInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
266 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
267 DefZero(Left.Offset, Ty) * Multiplier);
268 } // end case Instruction::Shl
270 case Instruction::Mul: {
271 ExprType Left (ClassifyExpression(I->getOperand(0)));
272 ExprType Right(ClassifyExpression(I->getOperand(1)));
273 if (Left.ExprTy > Right.ExprTy)
274 swap(Left, Right); // Make left be simpler than right
276 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
277 return I; // Quadratic eqn! :(
279 const ConstPoolInt *Offs = Left.Offset;
280 if (Offs == 0) return ExprType();
281 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
282 DefZero(Right.Offset, Ty) * Offs);
283 } // end case Instruction::Mul
285 case Instruction::Cast: {
286 ExprType Src(ClassifyExpression(I->getOperand(0)));
287 if (Src.ExprTy != ExprType::Constant)
289 const ConstPoolInt *Offs = Src.Offset;
290 if (Offs == 0) return ExprType();
292 const Type *DestTy = I->getType();
293 if (DestTy->isPointerType())
294 DestTy = Type::ULongTy; // Pointer types are represented as ulong
296 assert(DestTy->isIntegral() && "Can only handle integral types!");
298 const ConstPoolVal *CPV =ConstRules::get(*Offs)->castTo(Offs, DestTy);
300 assert(CPV->getType()->isIntegral() && "Must have an integral type!");
301 return cast<ConstPoolInt>(CPV);
302 } // end case Instruction::Cast
303 // TODO: Handle SUB, SHR?
307 // Otherwise, I don't know anything about this value!