1 //===- Expressions.cpp - Expression Analysis Utilities --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a package of expression analysis utilties:
12 // ClassifyExpression: Analyze an expression to determine the complexity of the
13 // expression, and which other variables it depends on.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/Expressions.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Function.h"
20 #include "llvm/Type.h"
23 ExprType::ExprType(Value *Val) {
25 if (ConstantInt *CPI = dyn_cast<ConstantInt>(Val)) {
33 Var = Val; Offset = 0;
34 ExprTy = Var ? Linear : Constant;
38 ExprType::ExprType(const ConstantInt *scale, Value *var,
39 const ConstantInt *offset) {
40 Scale = var ? scale : 0; Var = var; Offset = offset;
41 ExprTy = Scale ? ScaledLinear : (Var ? Linear : Constant);
42 if (Scale && Scale->isNullValue()) { // Simplify 0*Var + const
49 const Type *ExprType::getExprType(const Type *Default) const {
50 if (Offset) return Offset->getType();
51 if (Scale) return Scale->getType();
52 return Var ? Var->getType() : Default;
58 const ConstantInt * const Val;
59 const Type * const Ty;
61 inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {}
63 inline const Type *getType() const { return Ty; }
64 inline const ConstantInt *getVal() const { return Val; }
65 inline operator const ConstantInt * () const { return Val; }
66 inline const ConstantInt *operator->() const { return Val; }
69 struct DefZero : public DefVal {
70 inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
71 inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {}
74 struct DefOne : public DefVal {
75 inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
80 // getUnsignedConstant - Return a constant value of the specified type. If the
81 // constant value is not valid for the specified type, return null. This cannot
82 // happen for values in the range of 0 to 127.
84 static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
85 if (isa<PointerType>(Ty)) Ty = Type::ULongTy;
87 // If this value is not a valid unsigned value for this type, return null!
88 if (V > 127 && ((int64_t)V < 0 ||
89 !ConstantSInt::isValueValidForType(Ty, (int64_t)V)))
91 return ConstantSInt::get(Ty, V);
93 // If this value is not a valid unsigned value for this type, return null!
94 if (V > 255 && !ConstantUInt::isValueValidForType(Ty, V))
96 return ConstantUInt::get(Ty, V);
100 // Add - Helper function to make later code simpler. Basically it just adds
101 // the two constants together, inserts the result into the constant pool, and
102 // returns it. Of course life is not simple, and this is no exception. Factors
103 // that complicate matters:
104 // 1. Either argument may be null. If this is the case, the null argument is
105 // treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
106 // 2. Types get in the way. We want to do arithmetic operations without
107 // regard for the underlying types. It is assumed that the constants are
108 // integral constants. The new value takes the type of the left argument.
109 // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
110 // is false, a null return value indicates a value of 0.
112 static const ConstantInt *Add(const ConstantInt *Arg1,
113 const ConstantInt *Arg2, bool DefOne) {
114 assert(Arg1 && Arg2 && "No null arguments should exist now!");
115 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
117 // Actually perform the computation now!
118 Constant *Result = ConstantExpr::get(Instruction::Add, (Constant*)Arg1,
120 ConstantInt *ResultI = cast<ConstantInt>(Result);
122 // Check to see if the result is one of the special cases that we want to
124 if (ResultI->equalsInt(DefOne ? 1 : 0))
125 return 0; // Yes it is, simply return null.
130 static inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) {
131 if (L == 0) return R;
132 if (R == 0) return L;
133 return Add(L, R, false);
136 static inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) {
139 return getUnsignedConstant(2, L.getType());
141 return Add(getUnsignedConstant(1, L.getType()), R, true);
143 return Add(L, getUnsignedConstant(1, L.getType()), true);
145 return Add(L, R, true);
149 // Mul - Helper function to make later code simpler. Basically it just
150 // multiplies the two constants together, inserts the result into the constant
151 // pool, and returns it. Of course life is not simple, and this is no
152 // exception. Factors that complicate matters:
153 // 1. Either argument may be null. If this is the case, the null argument is
154 // treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
155 // 2. Types get in the way. We want to do arithmetic operations without
156 // regard for the underlying types. It is assumed that the constants are
157 // integral constants.
158 // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
159 // is false, a null return value indicates a value of 0.
161 static inline const ConstantInt *Mul(const ConstantInt *Arg1,
162 const ConstantInt *Arg2, bool DefOne) {
163 assert(Arg1 && Arg2 && "No null arguments should exist now!");
164 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
166 // Actually perform the computation now!
167 Constant *Result = ConstantExpr::get(Instruction::Mul, (Constant*)Arg1,
169 assert(Result && Result->getType() == Arg1->getType() &&
170 "Couldn't perform multiplication!");
171 ConstantInt *ResultI = cast<ConstantInt>(Result);
173 // Check to see if the result is one of the special cases that we want to
175 if (ResultI->equalsInt(DefOne ? 1 : 0))
176 return 0; // Yes it is, simply return null.
182 inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) {
183 if (L == 0 || R == 0) return 0;
184 return Mul(L, R, false);
186 inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) {
187 if (R == 0) return getUnsignedConstant(0, L.getType());
188 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
189 return Mul(L, R, true);
191 inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) {
192 if (L == 0 || R == 0) return L.getVal();
193 return Mul(R, L, false);
197 // handleAddition - Add two expressions together, creating a new expression that
198 // represents the composite of the two...
200 static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
201 const Type *Ty = V->getType();
202 if (Left.ExprTy > Right.ExprTy)
203 std::swap(Left, Right); // Make left be simpler than right
205 switch (Left.ExprTy) {
206 case ExprType::Constant:
207 return ExprType(Right.Scale, Right.Var,
208 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
209 case ExprType::Linear: // RHS side must be linear or scaled
210 case ExprType::ScaledLinear: // RHS must be scaled
211 if (Left.Var != Right.Var) // Are they the same variables?
212 return V; // if not, we don't know anything!
214 return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
216 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
218 assert(0 && "Dont' know how to handle this case!");
223 // negate - Negate the value of the specified expression...
225 static inline ExprType negate(const ExprType &E, Value *V) {
226 const Type *Ty = V->getType();
227 ConstantInt *Zero = getUnsignedConstant(0, Ty);
228 ConstantInt *One = getUnsignedConstant(1, Ty);
229 ConstantInt *NegOne = cast<ConstantInt>(ConstantExpr::get(Instruction::Sub,
231 if (NegOne == 0) return V; // Couldn't subtract values...
233 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
234 DefZero(E.Offset, Ty) * NegOne);
238 // ClassifyExpr: Analyze an expression to determine the complexity of the
239 // expression, and which other values it depends on.
241 // Note that this analysis cannot get into infinite loops because it treats PHI
242 // nodes as being an unknown linear expression.
244 ExprType llvm::ClassifyExpr(Value *Expr) {
245 assert(Expr != 0 && "Can't classify a null expression!");
246 if (Expr->getType()->isFloatingPoint())
247 return Expr; // FIXME: Can't handle FP expressions
249 if (Constant *C = dyn_cast<Constant>(Expr)) {
250 if (ConstantInt *CPI = dyn_cast<ConstantInt>(cast<Constant>(Expr)))
251 // It's an integral constant!
252 return ExprType(CPI->isNullValue() ? 0 : CPI);
254 } else if (!isa<Instruction>(Expr)) {
259 Instruction *I = cast<Instruction>(Expr);
260 const Type *Ty = I->getType();
262 switch (I->getOpcode()) { // Handle each instruction type separately
263 case Instruction::Add: {
264 ExprType Left (ClassifyExpr(I->getOperand(0)));
265 ExprType Right(ClassifyExpr(I->getOperand(1)));
266 return handleAddition(Left, Right, I);
267 } // end case Instruction::Add
269 case Instruction::Sub: {
270 ExprType Left (ClassifyExpr(I->getOperand(0)));
271 ExprType Right(ClassifyExpr(I->getOperand(1)));
272 ExprType RightNeg = negate(Right, I);
273 if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale)
274 return I; // Could not negate value...
275 return handleAddition(Left, RightNeg, I);
276 } // end case Instruction::Sub
278 case Instruction::Shl: {
279 ExprType Right(ClassifyExpr(I->getOperand(1)));
280 if (Right.ExprTy != ExprType::Constant) break;
281 ExprType Left(ClassifyExpr(I->getOperand(0)));
282 if (Right.Offset == 0) return Left; // shl x, 0 = x
283 assert(Right.Offset->getType() == Type::UByteTy &&
284 "Shift amount must always be a unsigned byte!");
285 uint64_t ShiftAmount = cast<ConstantUInt>(Right.Offset)->getValue();
286 ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
288 // We don't know how to classify it if they are shifting by more than what
289 // is reasonable. In most cases, the result will be zero, but there is one
290 // class of cases where it is not, so we cannot optimize without checking
291 // for it. The case is when you are shifting a signed value by 1 less than
292 // the number of bits in the value. For example:
293 // %X = shl sbyte %Y, ubyte 7
294 // will try to form an sbyte multiplier of 128, which will give a null
295 // multiplier, even though the result is not 0. Until we can check for this
296 // case, be conservative. TODO.
301 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
302 DefZero(Left.Offset, Ty) * Multiplier);
303 } // end case Instruction::Shl
305 case Instruction::Mul: {
306 ExprType Left (ClassifyExpr(I->getOperand(0)));
307 ExprType Right(ClassifyExpr(I->getOperand(1)));
308 if (Left.ExprTy > Right.ExprTy)
309 std::swap(Left, Right); // Make left be simpler than right
311 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
312 return I; // Quadratic eqn! :(
314 const ConstantInt *Offs = Left.Offset;
315 if (Offs == 0) return ExprType();
316 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
317 DefZero(Right.Offset, Ty) * Offs);
318 } // end case Instruction::Mul
320 case Instruction::Cast: {
321 ExprType Src(ClassifyExpr(I->getOperand(0)));
322 const Type *DestTy = I->getType();
323 if (isa<PointerType>(DestTy))
324 DestTy = Type::ULongTy; // Pointer types are represented as ulong
326 const Type *SrcValTy = Src.getExprType(0);
327 if (!SrcValTy) return I;
328 if (!SrcValTy->isLosslesslyConvertibleTo(DestTy)) {
329 if (Src.ExprTy != ExprType::Constant)
330 return I; // Converting cast, and not a constant value...
333 const ConstantInt *Offset = Src.Offset;
334 const ConstantInt *Scale = Src.Scale;
336 const Constant *CPV = ConstantExpr::getCast((Constant*)Offset, DestTy);
337 if (!isa<ConstantInt>(CPV)) return I;
338 Offset = cast<ConstantInt>(CPV);
341 const Constant *CPV = ConstantExpr::getCast((Constant*)Scale, DestTy);
343 Scale = cast<ConstantInt>(CPV);
345 return ExprType(Scale, Src.Var, Offset);
346 } // end case Instruction::Cast
347 // TODO: Handle SUB, SHR?
351 // Otherwise, I don't know anything about this value!