blob: 7549a19e09ec2dff60e38cbbbcf48564fd333b8a [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- Expressions.cpp - Expression Analysis Utilities --------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner369bbeb2001-07-20 19:17:55 +00009//
10// This file defines a package of expression analysis utilties:
11//
12// ClassifyExpression: Analyze an expression to determine the complexity of the
13// expression, and which other variables it depends on.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/Expressions.h"
Chris Lattnere9028632004-01-12 18:02:15 +000018#include "llvm/Constants.h"
Chris Lattnere590ff22002-03-26 17:55:33 +000019#include "llvm/Function.h"
Chris Lattnere9028632004-01-12 18:02:15 +000020#include "llvm/Type.h"
Reid Spencer954da372004-07-04 12:19:56 +000021#include <iostream>
22
Chris Lattner790462c2003-12-23 06:44:41 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner69f8ce02001-09-11 04:27:34 +000025ExprType::ExprType(Value *Val) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000026 if (Val)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000027 if (ConstantInt *CPI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000028 Offset = CPI;
29 Var = 0;
30 ExprTy = Constant;
31 Scale = 0;
32 return;
33 }
34
35 Var = Val; Offset = 0;
36 ExprTy = Var ? Linear : Constant;
Chris Lattner69f8ce02001-09-11 04:27:34 +000037 Scale = 0;
38}
39
Chris Lattnere9bb2df2001-12-03 22:26:30 +000040ExprType::ExprType(const ConstantInt *scale, Value *var,
41 const ConstantInt *offset) {
Chris Lattner50020222001-11-26 18:53:07 +000042 Scale = var ? scale : 0; Var = var; Offset = offset;
Chris Lattner69f8ce02001-09-11 04:27:34 +000043 ExprTy = Scale ? ScaledLinear : (Var ? Linear : Constant);
Chris Lattnerce8a1492002-09-03 01:05:48 +000044 if (Scale && Scale->isNullValue()) { // Simplify 0*Var + const
Chris Lattner69f8ce02001-09-11 04:27:34 +000045 Scale = 0; Var = 0;
46 ExprTy = Constant;
47 }
48}
49
50
51const Type *ExprType::getExprType(const Type *Default) const {
52 if (Offset) return Offset->getType();
53 if (Scale) return Scale->getType();
54 return Var ? Var->getType() : Default;
55}
56
57
Chris Lattner790462c2003-12-23 06:44:41 +000058namespace {
59 class DefVal {
60 const ConstantInt * const Val;
61 const Type * const Ty;
62 protected:
63 inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {}
64 public:
65 inline const Type *getType() const { return Ty; }
66 inline const ConstantInt *getVal() const { return Val; }
67 inline operator const ConstantInt * () const { return Val; }
68 inline const ConstantInt *operator->() const { return Val; }
69 };
70
71 struct DefZero : public DefVal {
72 inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
73 inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {}
74 };
75
76 struct DefOne : public DefVal {
77 inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
78 };
79}
Chris Lattner19f31f22001-07-21 19:07:19 +000080
Chris Lattner369bbeb2001-07-20 19:17:55 +000081
Chris Lattner9b534262002-03-14 22:35:50 +000082// getUnsignedConstant - Return a constant value of the specified type. If the
83// constant value is not valid for the specified type, return null. This cannot
84// happen for values in the range of 0 to 127.
85//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000086static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
Chris Lattner9b625032002-05-06 16:15:30 +000087 if (isa<PointerType>(Ty)) Ty = Type::ULongTy;
Chris Lattner9b534262002-03-14 22:35:50 +000088 if (Ty->isSigned()) {
89 // If this value is not a valid unsigned value for this type, return null!
90 if (V > 127 && ((int64_t)V < 0 ||
91 !ConstantSInt::isValueValidForType(Ty, (int64_t)V)))
92 return 0;
93 return ConstantSInt::get(Ty, V);
94 } else {
95 // If this value is not a valid unsigned value for this type, return null!
96 if (V > 255 && !ConstantUInt::isValueValidForType(Ty, V))
97 return 0;
98 return ConstantUInt::get(Ty, V);
99 }
Chris Lattner369bbeb2001-07-20 19:17:55 +0000100}
101
Chris Lattner369bbeb2001-07-20 19:17:55 +0000102// Add - Helper function to make later code simpler. Basically it just adds
103// the two constants together, inserts the result into the constant pool, and
104// returns it. Of course life is not simple, and this is no exception. Factors
105// that complicate matters:
106// 1. Either argument may be null. If this is the case, the null argument is
107// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
108// 2. Types get in the way. We want to do arithmetic operations without
109// regard for the underlying types. It is assumed that the constants are
110// integral constants. The new value takes the type of the left argument.
111// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
112// is false, a null return value indicates a value of 0.
113//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000114static const ConstantInt *Add(const ConstantInt *Arg1,
115 const ConstantInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000116 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000117 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000118
119 // Actually perform the computation now!
Chris Lattnere9028632004-01-12 18:02:15 +0000120 Constant *Result = ConstantExpr::get(Instruction::Add, (Constant*)Arg1,
121 (Constant*)Arg2);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000122 ConstantInt *ResultI = cast<ConstantInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000123
124 // Check to see if the result is one of the special cases that we want to
125 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000126 if (ResultI->equalsInt(DefOne ? 1 : 0))
127 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000128
Chris Lattner369bbeb2001-07-20 19:17:55 +0000129 return ResultI;
130}
131
Chris Lattner790462c2003-12-23 06:44:41 +0000132static inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000133 if (L == 0) return R;
134 if (R == 0) return L;
Chris Lattner8e195e02001-09-07 16:31:04 +0000135 return Add(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000136}
Chris Lattner369bbeb2001-07-20 19:17:55 +0000137
Chris Lattner790462c2003-12-23 06:44:41 +0000138static inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000139 if (L == 0) {
140 if (R == 0)
Chris Lattner69f8ce02001-09-11 04:27:34 +0000141 return getUnsignedConstant(2, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000142 else
Chris Lattner69f8ce02001-09-11 04:27:34 +0000143 return Add(getUnsignedConstant(1, L.getType()), R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000144 } else if (R == 0) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000145 return Add(L, getUnsignedConstant(1, L.getType()), true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000146 }
Chris Lattner8e195e02001-09-07 16:31:04 +0000147 return Add(L, R, true);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000148}
149
150
Chris Lattner19f31f22001-07-21 19:07:19 +0000151// Mul - Helper function to make later code simpler. Basically it just
Chris Lattner369bbeb2001-07-20 19:17:55 +0000152// multiplies the two constants together, inserts the result into the constant
153// pool, and returns it. Of course life is not simple, and this is no
154// exception. Factors that complicate matters:
155// 1. Either argument may be null. If this is the case, the null argument is
156// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
157// 2. Types get in the way. We want to do arithmetic operations without
158// regard for the underlying types. It is assumed that the constants are
159// integral constants.
160// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
161// is false, a null return value indicates a value of 0.
162//
Chris Lattner790462c2003-12-23 06:44:41 +0000163static inline const ConstantInt *Mul(const ConstantInt *Arg1,
164 const ConstantInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000165 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000166 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000167
168 // Actually perform the computation now!
Chris Lattnere9028632004-01-12 18:02:15 +0000169 Constant *Result = ConstantExpr::get(Instruction::Mul, (Constant*)Arg1,
170 (Constant*)Arg2);
Chris Lattner19f31f22001-07-21 19:07:19 +0000171 assert(Result && Result->getType() == Arg1->getType() &&
Chris Lattner50020222001-11-26 18:53:07 +0000172 "Couldn't perform multiplication!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000173 ConstantInt *ResultI = cast<ConstantInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000174
175 // Check to see if the result is one of the special cases that we want to
176 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000177 if (ResultI->equalsInt(DefOne ? 1 : 0))
178 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000179
Chris Lattner369bbeb2001-07-20 19:17:55 +0000180 return ResultI;
181}
182
Chris Lattner790462c2003-12-23 06:44:41 +0000183namespace {
184 inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) {
185 if (L == 0 || R == 0) return 0;
186 return Mul(L, R, false);
187 }
188 inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) {
189 if (R == 0) return getUnsignedConstant(0, L.getType());
190 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
191 return Mul(L, R, true);
192 }
193 inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) {
194 if (L == 0 || R == 0) return L.getVal();
195 return Mul(R, L, false);
196 }
Chris Lattner19f31f22001-07-21 19:07:19 +0000197}
198
Chris Lattner69f8ce02001-09-11 04:27:34 +0000199// handleAddition - Add two expressions together, creating a new expression that
200// represents the composite of the two...
201//
202static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
203 const Type *Ty = V->getType();
204 if (Left.ExprTy > Right.ExprTy)
Chris Lattner697954c2002-01-20 22:54:45 +0000205 std::swap(Left, Right); // Make left be simpler than right
Chris Lattner69f8ce02001-09-11 04:27:34 +0000206
207 switch (Left.ExprTy) {
208 case ExprType::Constant:
Chris Lattner50020222001-11-26 18:53:07 +0000209 return ExprType(Right.Scale, Right.Var,
210 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000211 case ExprType::Linear: // RHS side must be linear or scaled
212 case ExprType::ScaledLinear: // RHS must be scaled
213 if (Left.Var != Right.Var) // Are they the same variables?
Chris Lattner50020222001-11-26 18:53:07 +0000214 return V; // if not, we don't know anything!
Chris Lattner69f8ce02001-09-11 04:27:34 +0000215
216 return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
Chris Lattner50020222001-11-26 18:53:07 +0000217 Right.Var,
Chris Lattner69f8ce02001-09-11 04:27:34 +0000218 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
219 default:
220 assert(0 && "Dont' know how to handle this case!");
221 return ExprType();
222 }
223}
224
225// negate - Negate the value of the specified expression...
226//
227static inline ExprType negate(const ExprType &E, Value *V) {
228 const Type *Ty = V->getType();
Chris Lattnercb05e782002-03-11 20:50:24 +0000229 ConstantInt *Zero = getUnsignedConstant(0, Ty);
230 ConstantInt *One = getUnsignedConstant(1, Ty);
Chris Lattnere9028632004-01-12 18:02:15 +0000231 ConstantInt *NegOne = cast<ConstantInt>(ConstantExpr::get(Instruction::Sub,
232 Zero, One));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000233 if (NegOne == 0) return V; // Couldn't subtract values...
234
235 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
236 DefZero(E.Offset, Ty) * NegOne);
237}
Chris Lattner19f31f22001-07-21 19:07:19 +0000238
Chris Lattner369bbeb2001-07-20 19:17:55 +0000239
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000240// ClassifyExpr: Analyze an expression to determine the complexity of the
241// expression, and which other values it depends on.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000242//
243// Note that this analysis cannot get into infinite loops because it treats PHI
244// nodes as being an unknown linear expression.
245//
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000246ExprType llvm::ClassifyExpr(Value *Expr) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000247 assert(Expr != 0 && "Can't classify a null expression!");
Chris Lattneraf754db2004-06-26 19:31:26 +0000248 if (Expr->getType()->isFloatingPoint())
Chris Lattner0da29c82001-12-05 19:38:29 +0000249 return Expr; // FIXME: Can't handle FP expressions
250
Chris Lattneraf754db2004-06-26 19:31:26 +0000251 if (Constant *C = dyn_cast<Constant>(Expr)) {
Chris Lattnerac7ad682003-04-16 22:50:19 +0000252 if (ConstantInt *CPI = dyn_cast<ConstantInt>(cast<Constant>(Expr)))
253 // It's an integral constant!
Chris Lattnerce8a1492002-09-03 01:05:48 +0000254 return ExprType(CPI->isNullValue() ? 0 : CPI);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000255 return Expr;
Chris Lattneraf754db2004-06-26 19:31:26 +0000256 } else if (!isa<Instruction>(Expr)) {
257 return Expr;
Chris Lattner369bbeb2001-07-20 19:17:55 +0000258 }
Chris Lattneraf754db2004-06-26 19:31:26 +0000259
Chris Lattner369bbeb2001-07-20 19:17:55 +0000260
Chris Lattner9636a912001-10-01 16:18:37 +0000261 Instruction *I = cast<Instruction>(Expr);
Chris Lattner19f31f22001-07-21 19:07:19 +0000262 const Type *Ty = I->getType();
Chris Lattner369bbeb2001-07-20 19:17:55 +0000263
Misha Brukman1ba31382003-07-14 17:26:34 +0000264 switch (I->getOpcode()) { // Handle each instruction type separately
Chris Lattner369bbeb2001-07-20 19:17:55 +0000265 case Instruction::Add: {
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000266 ExprType Left (ClassifyExpr(I->getOperand(0)));
267 ExprType Right(ClassifyExpr(I->getOperand(1)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000268 return handleAddition(Left, Right, I);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000269 } // end case Instruction::Add
270
Chris Lattner69f8ce02001-09-11 04:27:34 +0000271 case Instruction::Sub: {
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000272 ExprType Left (ClassifyExpr(I->getOperand(0)));
273 ExprType Right(ClassifyExpr(I->getOperand(1)));
Chris Lattner50020222001-11-26 18:53:07 +0000274 ExprType RightNeg = negate(Right, I);
275 if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale)
276 return I; // Could not negate value...
277 return handleAddition(Left, RightNeg, I);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000278 } // end case Instruction::Sub
279
Chris Lattner369bbeb2001-07-20 19:17:55 +0000280 case Instruction::Shl: {
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000281 ExprType Right(ClassifyExpr(I->getOperand(1)));
Chris Lattner19f31f22001-07-21 19:07:19 +0000282 if (Right.ExprTy != ExprType::Constant) break;
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000283 ExprType Left(ClassifyExpr(I->getOperand(0)));
Chris Lattner19f31f22001-07-21 19:07:19 +0000284 if (Right.Offset == 0) return Left; // shl x, 0 = x
285 assert(Right.Offset->getType() == Type::UByteTy &&
Chris Lattner369bbeb2001-07-20 19:17:55 +0000286 "Shift amount must always be a unsigned byte!");
Chris Lattner28a128e2003-07-23 15:16:40 +0000287 uint64_t ShiftAmount = cast<ConstantUInt>(Right.Offset)->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000288 ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
Chris Lattner9b534262002-03-14 22:35:50 +0000289
290 // We don't know how to classify it if they are shifting by more than what
291 // is reasonable. In most cases, the result will be zero, but there is one
292 // class of cases where it is not, so we cannot optimize without checking
293 // for it. The case is when you are shifting a signed value by 1 less than
294 // the number of bits in the value. For example:
295 // %X = shl sbyte %Y, ubyte 7
296 // will try to form an sbyte multiplier of 128, which will give a null
297 // multiplier, even though the result is not 0. Until we can check for this
298 // case, be conservative. TODO.
299 //
300 if (Multiplier == 0)
301 return Expr;
302
Chris Lattner8e195e02001-09-07 16:31:04 +0000303 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
304 DefZero(Left.Offset, Ty) * Multiplier);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000305 } // end case Instruction::Shl
306
Chris Lattner19f31f22001-07-21 19:07:19 +0000307 case Instruction::Mul: {
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000308 ExprType Left (ClassifyExpr(I->getOperand(0)));
309 ExprType Right(ClassifyExpr(I->getOperand(1)));
Chris Lattner19f31f22001-07-21 19:07:19 +0000310 if (Left.ExprTy > Right.ExprTy)
Chris Lattner697954c2002-01-20 22:54:45 +0000311 std::swap(Left, Right); // Make left be simpler than right
Chris Lattner19f31f22001-07-21 19:07:19 +0000312
313 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
314 return I; // Quadratic eqn! :(
315
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 const ConstantInt *Offs = Left.Offset;
Chris Lattner19f31f22001-07-21 19:07:19 +0000317 if (Offs == 0) return ExprType();
Chris Lattner8e195e02001-09-07 16:31:04 +0000318 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
319 DefZero(Right.Offset, Ty) * Offs);
Chris Lattner19f31f22001-07-21 19:07:19 +0000320 } // end case Instruction::Mul
321
322 case Instruction::Cast: {
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000323 ExprType Src(ClassifyExpr(I->getOperand(0)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000324 const Type *DestTy = I->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000325 if (isa<PointerType>(DestTy))
Chris Lattner69f8ce02001-09-11 04:27:34 +0000326 DestTy = Type::ULongTy; // Pointer types are represented as ulong
Chris Lattner19f31f22001-07-21 19:07:19 +0000327
Chris Lattner5fd60912003-07-01 21:08:52 +0000328 const Type *SrcValTy = Src.getExprType(0);
329 if (!SrcValTy) return I;
330 if (!SrcValTy->isLosslesslyConvertibleTo(DestTy)) {
Chris Lattner882572a2001-11-26 16:53:50 +0000331 if (Src.ExprTy != ExprType::Constant)
332 return I; // Converting cast, and not a constant value...
333 }
Chris Lattner19f31f22001-07-21 19:07:19 +0000334
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000335 const ConstantInt *Offset = Src.Offset;
336 const ConstantInt *Scale = Src.Scale;
Chris Lattner882572a2001-11-26 16:53:50 +0000337 if (Offset) {
Chris Lattnere9028632004-01-12 18:02:15 +0000338 const Constant *CPV = ConstantExpr::getCast((Constant*)Offset, DestTy);
339 if (!isa<ConstantInt>(CPV)) return I;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000340 Offset = cast<ConstantInt>(CPV);
Chris Lattner882572a2001-11-26 16:53:50 +0000341 }
342 if (Scale) {
Chris Lattnere9028632004-01-12 18:02:15 +0000343 const Constant *CPV = ConstantExpr::getCast((Constant*)Scale, DestTy);
Chris Lattner882572a2001-11-26 16:53:50 +0000344 if (!CPV) return I;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 Scale = cast<ConstantInt>(CPV);
Chris Lattner882572a2001-11-26 16:53:50 +0000346 }
347 return ExprType(Scale, Src.Var, Offset);
Chris Lattner19f31f22001-07-21 19:07:19 +0000348 } // end case Instruction::Cast
Chris Lattner793d6782001-07-25 22:47:32 +0000349 // TODO: Handle SUB, SHR?
Chris Lattner369bbeb2001-07-20 19:17:55 +0000350
351 } // end switch
352
353 // Otherwise, I don't know anything about this value!
Chris Lattner19f31f22001-07-21 19:07:19 +0000354 return I;
Chris Lattner369bbeb2001-07-20 19:17:55 +0000355}