blob: b44b8ff162e0b652eaf68f65146bf5fff597cc9f [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)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000020 if (ConstantInt *CPI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000021 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
Chris Lattnere9bb2df2001-12-03 22:26:30 +000033ExprType::ExprType(const ConstantInt *scale, Value *var,
34 const ConstantInt *offset) {
Chris Lattner50020222001-11-26 18:53:07 +000035 Scale = var ? scale : 0; Var = var; Offset = offset;
Chris Lattner69f8ce02001-09-11 04:27:34 +000036 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 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000053 const ConstantInt * const Val;
Chris Lattner19f31f22001-07-21 19:07:19 +000054 const Type * const Ty;
55protected:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000056 inline DefVal(const ConstantInt *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 Lattnere9bb2df2001-12-03 22:26:30 +000059 inline const ConstantInt *getVal() const { return Val; }
60 inline operator const ConstantInt * () const { return Val; }
61 inline const ConstantInt *operator->() const { return Val; }
Chris Lattner19f31f22001-07-21 19:07:19 +000062};
63
64struct DefZero : public DefVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000065 inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
66 inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000067};
68
69struct DefOne : public DefVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000070 inline DefOne(const ConstantInt *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 Lattnere9bb2df2001-12-03 22:26:30 +000074static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
Chris Lattner793d6782001-07-25 22:47:32 +000075 if (Ty->isPointerType()) Ty = Type::ULongTy;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000076 return Ty->isSigned() ? (ConstantInt*)ConstantSInt::get(Ty, V)
77 : (ConstantInt*)ConstantUInt::get(Ty, V);
Chris Lattner369bbeb2001-07-20 19:17:55 +000078}
79
Chris Lattner369bbeb2001-07-20 19:17:55 +000080// 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.
91//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000092static const ConstantInt *Add(const ConstantInt *Arg1,
93 const ConstantInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +000094 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +000095 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +000096
97 // Actually perform the computation now!
Chris Lattnere9bb2df2001-12-03 22:26:30 +000098 Constant *Result = *Arg1 + *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +000099 assert(Result && Result->getType() == Arg1->getType() &&
100 "Couldn't perform addition!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000101 ConstantInt *ResultI = cast<ConstantInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000102
103 // Check to see if the result is one of the special cases that we want to
104 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000105 if (ResultI->equalsInt(DefOne ? 1 : 0))
106 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000107
Chris Lattner369bbeb2001-07-20 19:17:55 +0000108 return ResultI;
109}
110
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000111inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000112 if (L == 0) return R;
113 if (R == 0) return L;
Chris Lattner8e195e02001-09-07 16:31:04 +0000114 return Add(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000115}
Chris Lattner369bbeb2001-07-20 19:17:55 +0000116
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000117inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000118 if (L == 0) {
119 if (R == 0)
Chris Lattner69f8ce02001-09-11 04:27:34 +0000120 return getUnsignedConstant(2, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000121 else
Chris Lattner69f8ce02001-09-11 04:27:34 +0000122 return Add(getUnsignedConstant(1, L.getType()), R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000123 } else if (R == 0) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000124 return Add(L, getUnsignedConstant(1, L.getType()), true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000125 }
Chris Lattner8e195e02001-09-07 16:31:04 +0000126 return Add(L, R, true);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000127}
128
129
Chris Lattner19f31f22001-07-21 19:07:19 +0000130// Mul - Helper function to make later code simpler. Basically it just
Chris Lattner369bbeb2001-07-20 19:17:55 +0000131// 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.
141//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000142inline const ConstantInt *Mul(const ConstantInt *Arg1,
143 const ConstantInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000144 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000145 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000146
147 // Actually perform the computation now!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000148 Constant *Result = *Arg1 * *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +0000149 assert(Result && Result->getType() == Arg1->getType() &&
Chris Lattner50020222001-11-26 18:53:07 +0000150 "Couldn't perform multiplication!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000151 ConstantInt *ResultI = cast<ConstantInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000152
153 // Check to see if the result is one of the special cases that we want to
154 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000155 if (ResultI->equalsInt(DefOne ? 1 : 0))
156 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000157
Chris Lattner369bbeb2001-07-20 19:17:55 +0000158 return ResultI;
159}
160
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000161inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000162 if (L == 0 || R == 0) return 0;
Chris Lattner8e195e02001-09-07 16:31:04 +0000163 return Mul(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000164}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000165inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000166 if (R == 0) return getUnsignedConstant(0, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000167 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
Chris Lattner50020222001-11-26 18:53:07 +0000168 return Mul(L, R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000169}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000170inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) {
Chris Lattner50020222001-11-26 18:53:07 +0000171 if (L == 0 || R == 0) return L.getVal();
172 return Mul(R, L, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000173}
174
Chris Lattner69f8ce02001-09-11 04:27:34 +0000175// handleAddition - Add two expressions together, creating a new expression that
176// represents the composite of the two...
177//
178static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
179 const Type *Ty = V->getType();
180 if (Left.ExprTy > Right.ExprTy)
181 swap(Left, Right); // Make left be simpler than right
182
183 switch (Left.ExprTy) {
184 case ExprType::Constant:
Chris Lattner50020222001-11-26 18:53:07 +0000185 return ExprType(Right.Scale, Right.Var,
186 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000187 case ExprType::Linear: // RHS side must be linear or scaled
188 case ExprType::ScaledLinear: // RHS must be scaled
189 if (Left.Var != Right.Var) // Are they the same variables?
Chris Lattner50020222001-11-26 18:53:07 +0000190 return V; // if not, we don't know anything!
Chris Lattner69f8ce02001-09-11 04:27:34 +0000191
192 return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
Chris Lattner50020222001-11-26 18:53:07 +0000193 Right.Var,
Chris Lattner69f8ce02001-09-11 04:27:34 +0000194 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
195 default:
196 assert(0 && "Dont' know how to handle this case!");
197 return ExprType();
198 }
199}
200
201// negate - Negate the value of the specified expression...
202//
203static inline ExprType negate(const ExprType &E, Value *V) {
204 const Type *Ty = V->getType();
205 const Type *ETy = E.getExprType(Ty);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000206 ConstantInt *Zero = getUnsignedConstant(0, ETy);
207 ConstantInt *One = getUnsignedConstant(1, ETy);
208 ConstantInt *NegOne = cast<ConstantInt>(*Zero - *One);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000209 if (NegOne == 0) return V; // Couldn't subtract values...
210
211 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
212 DefZero(E.Offset, Ty) * NegOne);
213}
Chris Lattner19f31f22001-07-21 19:07:19 +0000214
Chris Lattner369bbeb2001-07-20 19:17:55 +0000215
216// ClassifyExpression: Analyze an expression to determine the complexity of the
217// expression, and which other values it depends on.
218//
219// Note that this analysis cannot get into infinite loops because it treats PHI
220// nodes as being an unknown linear expression.
221//
Chris Lattner19f31f22001-07-21 19:07:19 +0000222ExprType analysis::ClassifyExpression(Value *Expr) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000223 assert(Expr != 0 && "Can't classify a null expression!");
Chris Lattner0da29c82001-12-05 19:38:29 +0000224 if (Expr->getType() == Type::FloatTy || Expr->getType() == Type::DoubleTy)
225 return Expr; // FIXME: Can't handle FP expressions
226
Chris Lattner369bbeb2001-07-20 19:17:55 +0000227 switch (Expr->getValueType()) {
228 case Value::InstructionVal: break; // Instruction... hmmm... investigate.
229 case Value::TypeVal: case Value::BasicBlockVal:
Chris Lattnerfad0d4f2001-09-10 20:07:57 +0000230 case Value::MethodVal: case Value::ModuleVal: default:
Chris Lattner369bbeb2001-07-20 19:17:55 +0000231 assert(0 && "Unexpected expression type to classify!");
Chris Lattneref9c23f2001-10-03 14:53:21 +0000232 case Value::GlobalVariableVal: // Global Variable & Method argument:
Chris Lattnerfad0d4f2001-09-10 20:07:57 +0000233 case Value::MethodArgumentVal: // nothing known, return variable itself
Chris Lattner369bbeb2001-07-20 19:17:55 +0000234 return Expr;
235 case Value::ConstantVal: // Constant value, just return constant
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000236 Constant *CPV = cast<Constant>(Expr);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000237 if (CPV->getType()->isIntegral()) { // It's an integral constant!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000238 ConstantInt *CPI = cast<ConstantInt>(Expr);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000239 return ExprType(CPI->equalsInt(0) ? 0 : CPI);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000240 }
241 return Expr;
242 }
243
Chris Lattner9636a912001-10-01 16:18:37 +0000244 Instruction *I = cast<Instruction>(Expr);
Chris Lattner19f31f22001-07-21 19:07:19 +0000245 const Type *Ty = I->getType();
Chris Lattner369bbeb2001-07-20 19:17:55 +0000246
247 switch (I->getOpcode()) { // Handle each instruction type seperately
248 case Instruction::Add: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000249 ExprType Left (ClassifyExpression(I->getOperand(0)));
250 ExprType Right(ClassifyExpression(I->getOperand(1)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000251 return handleAddition(Left, Right, I);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000252 } // end case Instruction::Add
253
Chris Lattner69f8ce02001-09-11 04:27:34 +0000254 case Instruction::Sub: {
255 ExprType Left (ClassifyExpression(I->getOperand(0)));
256 ExprType Right(ClassifyExpression(I->getOperand(1)));
Chris Lattner50020222001-11-26 18:53:07 +0000257 ExprType RightNeg = negate(Right, I);
258 if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale)
259 return I; // Could not negate value...
260 return handleAddition(Left, RightNeg, I);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000261 } // end case Instruction::Sub
262
Chris Lattner369bbeb2001-07-20 19:17:55 +0000263 case Instruction::Shl: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000264 ExprType Right(ClassifyExpression(I->getOperand(1)));
265 if (Right.ExprTy != ExprType::Constant) break;
266 ExprType Left(ClassifyExpression(I->getOperand(0)));
267 if (Right.Offset == 0) return Left; // shl x, 0 = x
268 assert(Right.Offset->getType() == Type::UByteTy &&
Chris Lattner369bbeb2001-07-20 19:17:55 +0000269 "Shift amount must always be a unsigned byte!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000270 uint64_t ShiftAmount = ((ConstantUInt*)Right.Offset)->getValue();
271 ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000272
Chris Lattner8e195e02001-09-07 16:31:04 +0000273 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
274 DefZero(Left.Offset, Ty) * Multiplier);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000275 } // end case Instruction::Shl
276
Chris Lattner19f31f22001-07-21 19:07:19 +0000277 case Instruction::Mul: {
278 ExprType Left (ClassifyExpression(I->getOperand(0)));
279 ExprType Right(ClassifyExpression(I->getOperand(1)));
280 if (Left.ExprTy > Right.ExprTy)
281 swap(Left, Right); // Make left be simpler than right
282
283 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
284 return I; // Quadratic eqn! :(
285
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000286 const ConstantInt *Offs = Left.Offset;
Chris Lattner19f31f22001-07-21 19:07:19 +0000287 if (Offs == 0) return ExprType();
Chris Lattner8e195e02001-09-07 16:31:04 +0000288 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
289 DefZero(Right.Offset, Ty) * Offs);
Chris Lattner19f31f22001-07-21 19:07:19 +0000290 } // end case Instruction::Mul
291
292 case Instruction::Cast: {
293 ExprType Src(ClassifyExpression(I->getOperand(0)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000294 const Type *DestTy = I->getType();
295 if (DestTy->isPointerType())
296 DestTy = Type::ULongTy; // Pointer types are represented as ulong
Chris Lattner19f31f22001-07-21 19:07:19 +0000297
Chris Lattner882572a2001-11-26 16:53:50 +0000298 /*
299 if (!Src.getExprType(0)->isLosslesslyConvertableTo(DestTy)) {
300 if (Src.ExprTy != ExprType::Constant)
301 return I; // Converting cast, and not a constant value...
302 }
303 */
Chris Lattner19f31f22001-07-21 19:07:19 +0000304
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000305 const ConstantInt *Offset = Src.Offset;
306 const ConstantInt *Scale = Src.Scale;
Chris Lattner882572a2001-11-26 16:53:50 +0000307 if (Offset) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000308 const Constant *CPV = ConstantFoldCastInstruction(Offset, DestTy);
Chris Lattner882572a2001-11-26 16:53:50 +0000309 if (!CPV) return I;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000310 Offset = cast<ConstantInt>(CPV);
Chris Lattner882572a2001-11-26 16:53:50 +0000311 }
312 if (Scale) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 const Constant *CPV = ConstantFoldCastInstruction(Scale, DestTy);
Chris Lattner882572a2001-11-26 16:53:50 +0000314 if (!CPV) return I;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 Scale = cast<ConstantInt>(CPV);
Chris Lattner882572a2001-11-26 16:53:50 +0000316 }
317 return ExprType(Scale, Src.Var, Offset);
Chris Lattner19f31f22001-07-21 19:07:19 +0000318 } // end case Instruction::Cast
Chris Lattner793d6782001-07-25 22:47:32 +0000319 // TODO: Handle SUB, SHR?
Chris Lattner369bbeb2001-07-20 19:17:55 +0000320
321 } // end switch
322
323 // Otherwise, I don't know anything about this value!
Chris Lattner19f31f22001-07-21 19:07:19 +0000324 return I;
Chris Lattner369bbeb2001-07-20 19:17:55 +0000325}