blob: e6f4cfa94783c8bd60fc196618d61ad44cdba988 [file] [log] [blame]
Chris Lattner369bbeb2001-07-20 19:17:55 +00001//===- Expressions.cpp - Expression Analysis Utilities ----------------------=//
2//
3// This file defines a package of expression analysis utilties:
4//
5// ClassifyExpression: Analyze an expression to determine the complexity of the
6// expression, and which other variables it depends on.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/Expressions.h"
11#include "llvm/Optimizations/ConstantHandling.h"
Chris Lattner369bbeb2001-07-20 19:17:55 +000012#include "llvm/Method.h"
13#include "llvm/BasicBlock.h"
14
15using namespace opt; // Get all the constant handling stuff
Chris Lattner19f31f22001-07-21 19:07:19 +000016using namespace analysis;
17
Chris Lattner69f8ce02001-09-11 04:27:34 +000018ExprType::ExprType(Value *Val) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000019 if (Val)
20 if (ConstPoolInt *CPI = dyn_cast<ConstPoolInt>(Val)) {
21 Offset = CPI;
22 Var = 0;
23 ExprTy = Constant;
24 Scale = 0;
25 return;
26 }
27
28 Var = Val; Offset = 0;
29 ExprTy = Var ? Linear : Constant;
Chris Lattner69f8ce02001-09-11 04:27:34 +000030 Scale = 0;
31}
32
33ExprType::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
38 Scale = 0; Var = 0;
39 ExprTy = Constant;
40 }
41}
42
43
44const 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;
48}
49
50
51
Chris Lattner19f31f22001-07-21 19:07:19 +000052class DefVal {
53 const ConstPoolInt * const Val;
Chris Lattner19f31f22001-07-21 19:07:19 +000054 const Type * const Ty;
55protected:
Chris Lattner8e195e02001-09-07 16:31:04 +000056 inline DefVal(const ConstPoolInt *val, const Type *ty) : Val(val), Ty(ty) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000057public:
58 inline const Type *getType() const { return Ty; }
Chris Lattner19f31f22001-07-21 19:07:19 +000059 inline const ConstPoolInt *getVal() const { return Val; }
60 inline operator const ConstPoolInt * () const { return Val; }
61 inline const ConstPoolInt *operator->() const { return Val; }
62};
63
64struct DefZero : public DefVal {
Chris Lattner8e195e02001-09-07 16:31:04 +000065 inline DefZero(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
66 inline DefZero(const ConstPoolInt *val) : DefVal(val, val->getType()) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000067};
68
69struct DefOne : public DefVal {
Chris Lattner8e195e02001-09-07 16:31:04 +000070 inline DefOne(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000071};
72
Chris Lattner369bbeb2001-07-20 19:17:55 +000073
Chris Lattner8e195e02001-09-07 16:31:04 +000074static ConstPoolInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
Chris Lattner793d6782001-07-25 22:47:32 +000075 if (Ty->isPointerType()) Ty = Type::ULongTy;
Chris Lattner8e195e02001-09-07 16:31:04 +000076 return Ty->isSigned() ? ConstPoolSInt::get(Ty, V) : ConstPoolUInt::get(Ty, V);
Chris Lattner369bbeb2001-07-20 19:17:55 +000077}
78
Chris Lattner369bbeb2001-07-20 19:17:55 +000079// Add - Helper function to make later code simpler. Basically it just adds
80// the two constants together, inserts the result into the constant pool, and
81// returns it. Of course life is not simple, and this is no exception. Factors
82// that complicate matters:
83// 1. Either argument may be null. If this is the case, the null argument is
84// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
85// 2. Types get in the way. We want to do arithmetic operations without
86// regard for the underlying types. It is assumed that the constants are
87// integral constants. The new value takes the type of the left argument.
88// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
89// is false, a null return value indicates a value of 0.
90//
Chris Lattner8e195e02001-09-07 16:31:04 +000091static const ConstPoolInt *Add(const ConstPoolInt *Arg1,
Chris Lattner19f31f22001-07-21 19:07:19 +000092 const ConstPoolInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +000093 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +000094 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +000095
96 // Actually perform the computation now!
97 ConstPoolVal *Result = *Arg1 + *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +000098 assert(Result && Result->getType() == Arg1->getType() &&
99 "Couldn't perform addition!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000100 ConstPoolInt *ResultI = cast<ConstPoolInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000101
102 // Check to see if the result is one of the special cases that we want to
103 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000104 if (ResultI->equalsInt(DefOne ? 1 : 0))
105 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000106
Chris Lattner369bbeb2001-07-20 19:17:55 +0000107 return ResultI;
108}
109
Chris Lattner19f31f22001-07-21 19:07:19 +0000110inline const ConstPoolInt *operator+(const DefZero &L, const DefZero &R) {
111 if (L == 0) return R;
112 if (R == 0) return L;
Chris Lattner8e195e02001-09-07 16:31:04 +0000113 return Add(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000114}
Chris Lattner369bbeb2001-07-20 19:17:55 +0000115
Chris Lattner19f31f22001-07-21 19:07:19 +0000116inline const ConstPoolInt *operator+(const DefOne &L, const DefOne &R) {
117 if (L == 0) {
118 if (R == 0)
Chris Lattner69f8ce02001-09-11 04:27:34 +0000119 return getUnsignedConstant(2, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000120 else
Chris Lattner69f8ce02001-09-11 04:27:34 +0000121 return Add(getUnsignedConstant(1, L.getType()), R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000122 } else if (R == 0) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000123 return Add(L, getUnsignedConstant(1, L.getType()), true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000124 }
Chris Lattner8e195e02001-09-07 16:31:04 +0000125 return Add(L, R, true);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000126}
127
128
Chris Lattner19f31f22001-07-21 19:07:19 +0000129// Mul - Helper function to make later code simpler. Basically it just
Chris Lattner369bbeb2001-07-20 19:17:55 +0000130// multiplies the two constants together, inserts the result into the constant
131// pool, and returns it. Of course life is not simple, and this is no
132// exception. Factors that complicate matters:
133// 1. Either argument may be null. If this is the case, the null argument is
134// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
135// 2. Types get in the way. We want to do arithmetic operations without
136// regard for the underlying types. It is assumed that the constants are
137// integral constants.
138// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
139// is false, a null return value indicates a value of 0.
140//
Chris Lattner8e195e02001-09-07 16:31:04 +0000141inline const ConstPoolInt *Mul(const ConstPoolInt *Arg1,
Chris Lattner19f31f22001-07-21 19:07:19 +0000142 const ConstPoolInt *Arg2, bool DefOne = false) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000143 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000144 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000145
146 // Actually perform the computation now!
147 ConstPoolVal *Result = *Arg1 * *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +0000148 assert(Result && Result->getType() == Arg1->getType() &&
149 "Couldn't perform mult!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000150 ConstPoolInt *ResultI = cast<ConstPoolInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000151
152 // Check to see if the result is one of the special cases that we want to
153 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000154 if (ResultI->equalsInt(DefOne ? 1 : 0))
155 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000156
Chris Lattner369bbeb2001-07-20 19:17:55 +0000157 return ResultI;
158}
159
Chris Lattner19f31f22001-07-21 19:07:19 +0000160inline const ConstPoolInt *operator*(const DefZero &L, const DefZero &R) {
161 if (L == 0 || R == 0) return 0;
Chris Lattner8e195e02001-09-07 16:31:04 +0000162 return Mul(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000163}
164inline const ConstPoolInt *operator*(const DefOne &L, const DefZero &R) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000165 if (R == 0) return getUnsignedConstant(0, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000166 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
Chris Lattner8e195e02001-09-07 16:31:04 +0000167 return Mul(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000168}
169inline const ConstPoolInt *operator*(const DefZero &L, const DefOne &R) {
170 return R*L;
171}
172
Chris Lattner69f8ce02001-09-11 04:27:34 +0000173// handleAddition - Add two expressions together, creating a new expression that
174// represents the composite of the two...
175//
176static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
177 const Type *Ty = V->getType();
178 if (Left.ExprTy > Right.ExprTy)
179 swap(Left, Right); // Make left be simpler than right
180
181 switch (Left.ExprTy) {
182 case ExprType::Constant:
183 return ExprType(Right.Scale, Right.Var,
184 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
185 case ExprType::Linear: // RHS side must be linear or scaled
186 case ExprType::ScaledLinear: // RHS must be scaled
187 if (Left.Var != Right.Var) // Are they the same variables?
188 return ExprType(V); // if not, we don't know anything!
189
190 return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
191 Left.Var,
192 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
193 default:
194 assert(0 && "Dont' know how to handle this case!");
195 return ExprType();
196 }
197}
198
199// negate - Negate the value of the specified expression...
200//
201static inline ExprType negate(const ExprType &E, Value *V) {
202 const Type *Ty = V->getType();
203 const Type *ETy = E.getExprType(Ty);
204 ConstPoolInt *Zero = getUnsignedConstant(0, ETy);
205 ConstPoolInt *One = getUnsignedConstant(1, ETy);
Chris Lattnerb00c5822001-10-02 03:41:24 +0000206 ConstPoolInt *NegOne = cast<ConstPoolInt>(*Zero - *One);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000207 if (NegOne == 0) return V; // Couldn't subtract values...
208
209 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
210 DefZero(E.Offset, Ty) * NegOne);
211}
Chris Lattner19f31f22001-07-21 19:07:19 +0000212
Chris Lattner369bbeb2001-07-20 19:17:55 +0000213
214// ClassifyExpression: Analyze an expression to determine the complexity of the
215// expression, and which other values it depends on.
216//
217// Note that this analysis cannot get into infinite loops because it treats PHI
218// nodes as being an unknown linear expression.
219//
Chris Lattner19f31f22001-07-21 19:07:19 +0000220ExprType analysis::ClassifyExpression(Value *Expr) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000221 assert(Expr != 0 && "Can't classify a null expression!");
222 switch (Expr->getValueType()) {
223 case Value::InstructionVal: break; // Instruction... hmmm... investigate.
224 case Value::TypeVal: case Value::BasicBlockVal:
Chris Lattnerfad0d4f2001-09-10 20:07:57 +0000225 case Value::MethodVal: case Value::ModuleVal: default:
Chris Lattner369bbeb2001-07-20 19:17:55 +0000226 assert(0 && "Unexpected expression type to classify!");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000227 case Value::GlobalVariableVal: // Global Variable & Method argument:
Chris Lattnerfad0d4f2001-09-10 20:07:57 +0000228 case Value::MethodArgumentVal: // nothing known, return variable itself
Chris Lattner369bbeb2001-07-20 19:17:55 +0000229 return Expr;
230 case Value::ConstantVal: // Constant value, just return constant
Chris Lattnercfe26c92001-10-01 18:26:53 +0000231 ConstPoolVal *CPV = cast<ConstPoolVal>(Expr);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000232 if (CPV->getType()->isIntegral()) { // It's an integral constant!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000233 ConstPoolInt *CPI = cast<ConstPoolInt>(Expr);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000234 return ExprType(CPI->equalsInt(0) ? 0 : CPI);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000235 }
236 return Expr;
237 }
238
Chris Lattner9636a912001-10-01 16:18:37 +0000239 Instruction *I = cast<Instruction>(Expr);
Chris Lattner19f31f22001-07-21 19:07:19 +0000240 const Type *Ty = I->getType();
Chris Lattner369bbeb2001-07-20 19:17:55 +0000241
242 switch (I->getOpcode()) { // Handle each instruction type seperately
243 case Instruction::Add: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000244 ExprType Left (ClassifyExpression(I->getOperand(0)));
245 ExprType Right(ClassifyExpression(I->getOperand(1)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000246 return handleAddition(Left, Right, I);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000247 } // end case Instruction::Add
248
Chris Lattner69f8ce02001-09-11 04:27:34 +0000249 case Instruction::Sub: {
250 ExprType Left (ClassifyExpression(I->getOperand(0)));
251 ExprType Right(ClassifyExpression(I->getOperand(1)));
252 return handleAddition(Left, negate(Right, I), I);
253 } // end case Instruction::Sub
254
Chris Lattner369bbeb2001-07-20 19:17:55 +0000255 case Instruction::Shl: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000256 ExprType Right(ClassifyExpression(I->getOperand(1)));
257 if (Right.ExprTy != ExprType::Constant) break;
258 ExprType Left(ClassifyExpression(I->getOperand(0)));
259 if (Right.Offset == 0) return Left; // shl x, 0 = x
260 assert(Right.Offset->getType() == Type::UByteTy &&
Chris Lattner369bbeb2001-07-20 19:17:55 +0000261 "Shift amount must always be a unsigned byte!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000262 uint64_t ShiftAmount = ((ConstPoolUInt*)Right.Offset)->getValue();
Chris Lattner8e195e02001-09-07 16:31:04 +0000263 ConstPoolInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000264
Chris Lattner8e195e02001-09-07 16:31:04 +0000265 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
266 DefZero(Left.Offset, Ty) * Multiplier);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000267 } // end case Instruction::Shl
268
Chris Lattner19f31f22001-07-21 19:07:19 +0000269 case Instruction::Mul: {
270 ExprType Left (ClassifyExpression(I->getOperand(0)));
271 ExprType Right(ClassifyExpression(I->getOperand(1)));
272 if (Left.ExprTy > Right.ExprTy)
273 swap(Left, Right); // Make left be simpler than right
274
275 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
276 return I; // Quadratic eqn! :(
277
278 const ConstPoolInt *Offs = Left.Offset;
279 if (Offs == 0) return ExprType();
Chris Lattner8e195e02001-09-07 16:31:04 +0000280 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
281 DefZero(Right.Offset, Ty) * Offs);
Chris Lattner19f31f22001-07-21 19:07:19 +0000282 } // end case Instruction::Mul
283
284 case Instruction::Cast: {
285 ExprType Src(ClassifyExpression(I->getOperand(0)));
286 if (Src.ExprTy != ExprType::Constant)
287 return I;
288 const ConstPoolInt *Offs = Src.Offset;
289 if (Offs == 0) return ExprType();
290
Chris Lattner69f8ce02001-09-11 04:27:34 +0000291 const Type *DestTy = I->getType();
292 if (DestTy->isPointerType())
293 DestTy = Type::ULongTy; // Pointer types are represented as ulong
Chris Lattner19f31f22001-07-21 19:07:19 +0000294
Chris Lattner69f8ce02001-09-11 04:27:34 +0000295 assert(DestTy->isIntegral() && "Can only handle integral types!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000296
Chris Lattner69f8ce02001-09-11 04:27:34 +0000297 const ConstPoolVal *CPV =ConstRules::get(*Offs)->castTo(Offs, DestTy);
Chris Lattner19f31f22001-07-21 19:07:19 +0000298 if (!CPV) return I;
299 assert(CPV->getType()->isIntegral() && "Must have an integral type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000300 return cast<ConstPoolInt>(CPV);
Chris Lattner19f31f22001-07-21 19:07:19 +0000301 } // end case Instruction::Cast
Chris Lattner793d6782001-07-25 22:47:32 +0000302 // TODO: Handle SUB, SHR?
Chris Lattner369bbeb2001-07-20 19:17:55 +0000303
304 } // end switch
305
306 // Otherwise, I don't know anything about this value!
Chris Lattner19f31f22001-07-21 19:07:19 +0000307 return I;
Chris Lattner369bbeb2001-07-20 19:17:55 +0000308}