blob: 2194d1f1ee869f599c96b808ceaf48daf2d5e414 [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
18class DefVal {
19 const ConstPoolInt * const Val;
Chris Lattner19f31f22001-07-21 19:07:19 +000020 const Type * const Ty;
21protected:
Chris Lattner8e195e02001-09-07 16:31:04 +000022 inline DefVal(const ConstPoolInt *val, const Type *ty) : Val(val), Ty(ty) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000023public:
24 inline const Type *getType() const { return Ty; }
Chris Lattner19f31f22001-07-21 19:07:19 +000025 inline const ConstPoolInt *getVal() const { return Val; }
26 inline operator const ConstPoolInt * () const { return Val; }
27 inline const ConstPoolInt *operator->() const { return Val; }
28};
29
30struct DefZero : public DefVal {
Chris Lattner8e195e02001-09-07 16:31:04 +000031 inline DefZero(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
32 inline DefZero(const ConstPoolInt *val) : DefVal(val, val->getType()) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000033};
34
35struct DefOne : public DefVal {
Chris Lattner8e195e02001-09-07 16:31:04 +000036 inline DefOne(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000037};
38
Chris Lattner369bbeb2001-07-20 19:17:55 +000039
40// getIntegralConstant - Wrapper around the ConstPoolInt member of the same
41// name. This method first checks to see if the desired constant is already in
42// the constant pool. If it is, it is quickly recycled, otherwise a new one
43// is allocated and added to the constant pool.
44//
Chris Lattner8e195e02001-09-07 16:31:04 +000045static ConstPoolInt *getIntegralConstant(unsigned char V, const Type *Ty) {
46 return ConstPoolInt::get(Ty, V);
Chris Lattner369bbeb2001-07-20 19:17:55 +000047}
48
Chris Lattner8e195e02001-09-07 16:31:04 +000049static ConstPoolInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
Chris Lattner793d6782001-07-25 22:47:32 +000050 if (Ty->isPointerType()) Ty = Type::ULongTy;
Chris Lattner369bbeb2001-07-20 19:17:55 +000051
Chris Lattner8e195e02001-09-07 16:31:04 +000052 return Ty->isSigned() ? ConstPoolSInt::get(Ty, V) : ConstPoolUInt::get(Ty, V);
Chris Lattner369bbeb2001-07-20 19:17:55 +000053}
54
Chris Lattner369bbeb2001-07-20 19:17:55 +000055// Add - Helper function to make later code simpler. Basically it just adds
56// the two constants together, inserts the result into the constant pool, and
57// returns it. Of course life is not simple, and this is no exception. Factors
58// that complicate matters:
59// 1. Either argument may be null. If this is the case, the null argument is
60// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
61// 2. Types get in the way. We want to do arithmetic operations without
62// regard for the underlying types. It is assumed that the constants are
63// integral constants. The new value takes the type of the left argument.
64// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
65// is false, a null return value indicates a value of 0.
66//
Chris Lattner8e195e02001-09-07 16:31:04 +000067static const ConstPoolInt *Add(const ConstPoolInt *Arg1,
Chris Lattner19f31f22001-07-21 19:07:19 +000068 const ConstPoolInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +000069 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +000070 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +000071
72 // Actually perform the computation now!
73 ConstPoolVal *Result = *Arg1 + *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +000074 assert(Result && Result->getType() == Arg1->getType() &&
75 "Couldn't perform addition!");
Chris Lattner369bbeb2001-07-20 19:17:55 +000076 ConstPoolInt *ResultI = (ConstPoolInt*)Result;
77
78 // Check to see if the result is one of the special cases that we want to
79 // recognize...
Chris Lattner19f31f22001-07-21 19:07:19 +000080 if (ResultI->equalsInt(DefOne ? 1 : 0)) {
Chris Lattner369bbeb2001-07-20 19:17:55 +000081 // Yes it is, simply delete the constant and return null.
82 delete ResultI;
83 return 0;
84 }
85
Chris Lattner369bbeb2001-07-20 19:17:55 +000086 return ResultI;
87}
88
Chris Lattner19f31f22001-07-21 19:07:19 +000089inline const ConstPoolInt *operator+(const DefZero &L, const DefZero &R) {
90 if (L == 0) return R;
91 if (R == 0) return L;
Chris Lattner8e195e02001-09-07 16:31:04 +000092 return Add(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +000093}
Chris Lattner369bbeb2001-07-20 19:17:55 +000094
Chris Lattner19f31f22001-07-21 19:07:19 +000095inline const ConstPoolInt *operator+(const DefOne &L, const DefOne &R) {
96 if (L == 0) {
97 if (R == 0)
Chris Lattner8e195e02001-09-07 16:31:04 +000098 return getIntegralConstant(2, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +000099 else
Chris Lattner8e195e02001-09-07 16:31:04 +0000100 return Add(getIntegralConstant(1, L.getType()), R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000101 } else if (R == 0) {
Chris Lattner8e195e02001-09-07 16:31:04 +0000102 return Add(L, getIntegralConstant(1, L.getType()), true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000103 }
Chris Lattner8e195e02001-09-07 16:31:04 +0000104 return Add(L, R, true);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000105}
106
107
Chris Lattner19f31f22001-07-21 19:07:19 +0000108// Mul - Helper function to make later code simpler. Basically it just
Chris Lattner369bbeb2001-07-20 19:17:55 +0000109// multiplies the two constants together, inserts the result into the constant
110// pool, and returns it. Of course life is not simple, and this is no
111// exception. Factors that complicate matters:
112// 1. Either argument may be null. If this is the case, the null argument is
113// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
114// 2. Types get in the way. We want to do arithmetic operations without
115// regard for the underlying types. It is assumed that the constants are
116// integral constants.
117// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
118// is false, a null return value indicates a value of 0.
119//
Chris Lattner8e195e02001-09-07 16:31:04 +0000120inline const ConstPoolInt *Mul(const ConstPoolInt *Arg1,
Chris Lattner19f31f22001-07-21 19:07:19 +0000121 const ConstPoolInt *Arg2, bool DefOne = false) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000122 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000123 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000124
125 // Actually perform the computation now!
126 ConstPoolVal *Result = *Arg1 * *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +0000127 assert(Result && Result->getType() == Arg1->getType() &&
128 "Couldn't perform mult!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000129 ConstPoolInt *ResultI = (ConstPoolInt*)Result;
130
131 // Check to see if the result is one of the special cases that we want to
132 // recognize...
Chris Lattner19f31f22001-07-21 19:07:19 +0000133 if (ResultI->equalsInt(DefOne ? 1 : 0)) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000134 // Yes it is, simply delete the constant and return null.
135 delete ResultI;
136 return 0;
137 }
138
Chris Lattner369bbeb2001-07-20 19:17:55 +0000139 return ResultI;
140}
141
Chris Lattner19f31f22001-07-21 19:07:19 +0000142inline const ConstPoolInt *operator*(const DefZero &L, const DefZero &R) {
143 if (L == 0 || R == 0) return 0;
Chris Lattner8e195e02001-09-07 16:31:04 +0000144 return Mul(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000145}
146inline const ConstPoolInt *operator*(const DefOne &L, const DefZero &R) {
Chris Lattner8e195e02001-09-07 16:31:04 +0000147 if (R == 0) return getIntegralConstant(0, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000148 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
Chris Lattner8e195e02001-09-07 16:31:04 +0000149 return Mul(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000150}
151inline const ConstPoolInt *operator*(const DefZero &L, const DefOne &R) {
152 return R*L;
153}
154
155
Chris Lattner369bbeb2001-07-20 19:17:55 +0000156
157// ClassifyExpression: Analyze an expression to determine the complexity of the
158// expression, and which other values it depends on.
159//
160// Note that this analysis cannot get into infinite loops because it treats PHI
161// nodes as being an unknown linear expression.
162//
Chris Lattner19f31f22001-07-21 19:07:19 +0000163ExprType analysis::ClassifyExpression(Value *Expr) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000164 assert(Expr != 0 && "Can't classify a null expression!");
165 switch (Expr->getValueType()) {
166 case Value::InstructionVal: break; // Instruction... hmmm... investigate.
167 case Value::TypeVal: case Value::BasicBlockVal:
168 case Value::MethodVal: case Value::ModuleVal:
169 assert(0 && "Unexpected expression type to classify!");
170 case Value::MethodArgumentVal: // Method arg: nothing known, return var
171 return Expr;
172 case Value::ConstantVal: // Constant value, just return constant
173 ConstPoolVal *CPV = Expr->castConstantAsserting();
174 if (CPV->getType()->isIntegral()) { // It's an integral constant!
175 ConstPoolInt *CPI = (ConstPoolInt*)Expr;
Chris Lattner19f31f22001-07-21 19:07:19 +0000176 return ExprType(CPI->equalsInt(0) ? 0 : (ConstPoolInt*)Expr);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000177 }
178 return Expr;
179 }
180
181 Instruction *I = Expr->castInstructionAsserting();
Chris Lattner19f31f22001-07-21 19:07:19 +0000182 const Type *Ty = I->getType();
Chris Lattner369bbeb2001-07-20 19:17:55 +0000183
184 switch (I->getOpcode()) { // Handle each instruction type seperately
185 case Instruction::Add: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000186 ExprType Left (ClassifyExpression(I->getOperand(0)));
187 ExprType Right(ClassifyExpression(I->getOperand(1)));
188 if (Left.ExprTy > Right.ExprTy)
189 swap(Left, Right); // Make left be simpler than right
Chris Lattner369bbeb2001-07-20 19:17:55 +0000190
Chris Lattner19f31f22001-07-21 19:07:19 +0000191 switch (Left.ExprTy) {
192 case ExprType::Constant:
193 return ExprType(Right.Scale, Right.Var,
Chris Lattner8e195e02001-09-07 16:31:04 +0000194 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
Chris Lattner19f31f22001-07-21 19:07:19 +0000195 case ExprType::Linear: // RHS side must be linear or scaled
196 case ExprType::ScaledLinear: // RHS must be scaled
197 if (Left.Var != Right.Var) // Are they the same variables?
198 return ExprType(I); // if not, we don't know anything!
Chris Lattner369bbeb2001-07-20 19:17:55 +0000199
Chris Lattner8e195e02001-09-07 16:31:04 +0000200 return ExprType( DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
Chris Lattner19f31f22001-07-21 19:07:19 +0000201 Left.Var,
Chris Lattner8e195e02001-09-07 16:31:04 +0000202 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
Chris Lattner369bbeb2001-07-20 19:17:55 +0000203 }
204 } // end case Instruction::Add
205
206 case Instruction::Shl: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000207 ExprType Right(ClassifyExpression(I->getOperand(1)));
208 if (Right.ExprTy != ExprType::Constant) break;
209 ExprType Left(ClassifyExpression(I->getOperand(0)));
210 if (Right.Offset == 0) return Left; // shl x, 0 = x
211 assert(Right.Offset->getType() == Type::UByteTy &&
Chris Lattner369bbeb2001-07-20 19:17:55 +0000212 "Shift amount must always be a unsigned byte!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000213 uint64_t ShiftAmount = ((ConstPoolUInt*)Right.Offset)->getValue();
Chris Lattner8e195e02001-09-07 16:31:04 +0000214 ConstPoolInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000215
Chris Lattner8e195e02001-09-07 16:31:04 +0000216 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
217 DefZero(Left.Offset, Ty) * Multiplier);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000218 } // end case Instruction::Shl
219
Chris Lattner19f31f22001-07-21 19:07:19 +0000220 case Instruction::Mul: {
221 ExprType Left (ClassifyExpression(I->getOperand(0)));
222 ExprType Right(ClassifyExpression(I->getOperand(1)));
223 if (Left.ExprTy > Right.ExprTy)
224 swap(Left, Right); // Make left be simpler than right
225
226 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
227 return I; // Quadratic eqn! :(
228
229 const ConstPoolInt *Offs = Left.Offset;
230 if (Offs == 0) return ExprType();
Chris Lattner8e195e02001-09-07 16:31:04 +0000231 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
232 DefZero(Right.Offset, Ty) * Offs);
Chris Lattner19f31f22001-07-21 19:07:19 +0000233 } // end case Instruction::Mul
234
235 case Instruction::Cast: {
236 ExprType Src(ClassifyExpression(I->getOperand(0)));
237 if (Src.ExprTy != ExprType::Constant)
238 return I;
239 const ConstPoolInt *Offs = Src.Offset;
240 if (Offs == 0) return ExprType();
241
242 if (I->getType()->isPointerType())
243 return Offs; // Pointer types do not lose precision
244
245 assert(I->getType()->isIntegral() && "Can only handle integral types!");
246
Chris Lattner8e195e02001-09-07 16:31:04 +0000247 const ConstPoolVal *CPV =ConstRules::get(*Offs)->castTo(Offs, I->getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000248 if (!CPV) return I;
249 assert(CPV->getType()->isIntegral() && "Must have an integral type!");
250 return (ConstPoolInt*)CPV;
251 } // end case Instruction::Cast
Chris Lattner793d6782001-07-25 22:47:32 +0000252 // TODO: Handle SUB, SHR?
Chris Lattner369bbeb2001-07-20 19:17:55 +0000253
254 } // end switch
255
256 // Otherwise, I don't know anything about this value!
Chris Lattner19f31f22001-07-21 19:07:19 +0000257 return I;
Chris Lattner369bbeb2001-07-20 19:17:55 +0000258}