blob: e94c7960596445fb37c32a12b4bbb048f856a8d0 [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"
Chris Lattner697954c2002-01-20 22:54:45 +000014#include <iostream>
Chris Lattner369bbeb2001-07-20 19:17:55 +000015
16using namespace opt; // Get all the constant handling stuff
Chris Lattner19f31f22001-07-21 19:07:19 +000017using namespace analysis;
18
Chris Lattner69f8ce02001-09-11 04:27:34 +000019ExprType::ExprType(Value *Val) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000020 if (Val)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000021 if (ConstantInt *CPI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000022 Offset = CPI;
23 Var = 0;
24 ExprTy = Constant;
25 Scale = 0;
26 return;
27 }
28
29 Var = Val; Offset = 0;
30 ExprTy = Var ? Linear : Constant;
Chris Lattner69f8ce02001-09-11 04:27:34 +000031 Scale = 0;
32}
33
Chris Lattnere9bb2df2001-12-03 22:26:30 +000034ExprType::ExprType(const ConstantInt *scale, Value *var,
35 const ConstantInt *offset) {
Chris Lattner50020222001-11-26 18:53:07 +000036 Scale = var ? scale : 0; Var = var; Offset = offset;
Chris Lattner69f8ce02001-09-11 04:27:34 +000037 ExprTy = Scale ? ScaledLinear : (Var ? Linear : Constant);
38 if (Scale && Scale->equalsInt(0)) { // Simplify 0*Var + const
39 Scale = 0; Var = 0;
40 ExprTy = Constant;
41 }
42}
43
44
45const Type *ExprType::getExprType(const Type *Default) const {
46 if (Offset) return Offset->getType();
47 if (Scale) return Scale->getType();
48 return Var ? Var->getType() : Default;
49}
50
51
52
Chris Lattner19f31f22001-07-21 19:07:19 +000053class DefVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000054 const ConstantInt * const Val;
Chris Lattner19f31f22001-07-21 19:07:19 +000055 const Type * const Ty;
56protected:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000057 inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000058public:
59 inline const Type *getType() const { return Ty; }
Chris Lattnere9bb2df2001-12-03 22:26:30 +000060 inline const ConstantInt *getVal() const { return Val; }
61 inline operator const ConstantInt * () const { return Val; }
62 inline const ConstantInt *operator->() const { return Val; }
Chris Lattner19f31f22001-07-21 19:07:19 +000063};
64
65struct DefZero : public DefVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000066 inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
67 inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000068};
69
70struct DefOne : public DefVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000071 inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {}
Chris Lattner19f31f22001-07-21 19:07:19 +000072};
73
Chris Lattner369bbeb2001-07-20 19:17:55 +000074
Chris Lattnere9bb2df2001-12-03 22:26:30 +000075static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
Chris Lattner793d6782001-07-25 22:47:32 +000076 if (Ty->isPointerType()) Ty = Type::ULongTy;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000077 return Ty->isSigned() ? (ConstantInt*)ConstantSInt::get(Ty, V)
78 : (ConstantInt*)ConstantUInt::get(Ty, V);
Chris Lattner369bbeb2001-07-20 19:17:55 +000079}
80
Chris Lattner369bbeb2001-07-20 19:17:55 +000081// Add - Helper function to make later code simpler. Basically it just adds
82// the two constants together, inserts the result into the constant pool, and
83// returns it. Of course life is not simple, and this is no exception. Factors
84// that complicate matters:
85// 1. Either argument may be null. If this is the case, the null argument is
86// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
87// 2. Types get in the way. We want to do arithmetic operations without
88// regard for the underlying types. It is assumed that the constants are
89// integral constants. The new value takes the type of the left argument.
90// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
91// is false, a null return value indicates a value of 0.
92//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000093static const ConstantInt *Add(const ConstantInt *Arg1,
94 const ConstantInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +000095 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +000096 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +000097
98 // Actually perform the computation now!
Chris Lattnere9bb2df2001-12-03 22:26:30 +000099 Constant *Result = *Arg1 + *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +0000100 assert(Result && Result->getType() == Arg1->getType() &&
101 "Couldn't perform addition!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000102 ConstantInt *ResultI = cast<ConstantInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000103
104 // Check to see if the result is one of the special cases that we want to
105 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000106 if (ResultI->equalsInt(DefOne ? 1 : 0))
107 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000108
Chris Lattner369bbeb2001-07-20 19:17:55 +0000109 return ResultI;
110}
111
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000112inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000113 if (L == 0) return R;
114 if (R == 0) return L;
Chris Lattner8e195e02001-09-07 16:31:04 +0000115 return Add(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000116}
Chris Lattner369bbeb2001-07-20 19:17:55 +0000117
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000119 if (L == 0) {
120 if (R == 0)
Chris Lattner69f8ce02001-09-11 04:27:34 +0000121 return getUnsignedConstant(2, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000122 else
Chris Lattner69f8ce02001-09-11 04:27:34 +0000123 return Add(getUnsignedConstant(1, L.getType()), R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000124 } else if (R == 0) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000125 return Add(L, getUnsignedConstant(1, L.getType()), true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000126 }
Chris Lattner8e195e02001-09-07 16:31:04 +0000127 return Add(L, R, true);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000128}
129
130
Chris Lattner19f31f22001-07-21 19:07:19 +0000131// Mul - Helper function to make later code simpler. Basically it just
Chris Lattner369bbeb2001-07-20 19:17:55 +0000132// multiplies the two constants together, inserts the result into the constant
133// pool, and returns it. Of course life is not simple, and this is no
134// exception. Factors that complicate matters:
135// 1. Either argument may be null. If this is the case, the null argument is
136// treated as either 0 (if DefOne = false) or 1 (if DefOne = true)
137// 2. Types get in the way. We want to do arithmetic operations without
138// regard for the underlying types. It is assumed that the constants are
139// integral constants.
140// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
141// is false, a null return value indicates a value of 0.
142//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000143inline const ConstantInt *Mul(const ConstantInt *Arg1,
144 const ConstantInt *Arg2, bool DefOne) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000145 assert(Arg1 && Arg2 && "No null arguments should exist now!");
Chris Lattner19f31f22001-07-21 19:07:19 +0000146 assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
Chris Lattner369bbeb2001-07-20 19:17:55 +0000147
148 // Actually perform the computation now!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000149 Constant *Result = *Arg1 * *Arg2;
Chris Lattner19f31f22001-07-21 19:07:19 +0000150 assert(Result && Result->getType() == Arg1->getType() &&
Chris Lattner50020222001-11-26 18:53:07 +0000151 "Couldn't perform multiplication!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000152 ConstantInt *ResultI = cast<ConstantInt>(Result);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000153
154 // Check to see if the result is one of the special cases that we want to
155 // recognize...
Chris Lattner69f8ce02001-09-11 04:27:34 +0000156 if (ResultI->equalsInt(DefOne ? 1 : 0))
157 return 0; // Yes it is, simply return null.
Chris Lattner369bbeb2001-07-20 19:17:55 +0000158
Chris Lattner369bbeb2001-07-20 19:17:55 +0000159 return ResultI;
160}
161
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000162inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) {
Chris Lattner19f31f22001-07-21 19:07:19 +0000163 if (L == 0 || R == 0) return 0;
Chris Lattner8e195e02001-09-07 16:31:04 +0000164 return Mul(L, R, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000165}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000166inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) {
Chris Lattner69f8ce02001-09-11 04:27:34 +0000167 if (R == 0) return getUnsignedConstant(0, L.getType());
Chris Lattner19f31f22001-07-21 19:07:19 +0000168 if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
Chris Lattner50020222001-11-26 18:53:07 +0000169 return Mul(L, R, true);
Chris Lattner19f31f22001-07-21 19:07:19 +0000170}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) {
Chris Lattner50020222001-11-26 18:53:07 +0000172 if (L == 0 || R == 0) return L.getVal();
173 return Mul(R, L, false);
Chris Lattner19f31f22001-07-21 19:07:19 +0000174}
175
Chris Lattner69f8ce02001-09-11 04:27:34 +0000176// handleAddition - Add two expressions together, creating a new expression that
177// represents the composite of the two...
178//
179static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) {
180 const Type *Ty = V->getType();
181 if (Left.ExprTy > Right.ExprTy)
Chris Lattner697954c2002-01-20 22:54:45 +0000182 std::swap(Left, Right); // Make left be simpler than right
Chris Lattner69f8ce02001-09-11 04:27:34 +0000183
184 switch (Left.ExprTy) {
185 case ExprType::Constant:
Chris Lattner50020222001-11-26 18:53:07 +0000186 return ExprType(Right.Scale, Right.Var,
187 DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000188 case ExprType::Linear: // RHS side must be linear or scaled
189 case ExprType::ScaledLinear: // RHS must be scaled
190 if (Left.Var != Right.Var) // Are they the same variables?
Chris Lattner50020222001-11-26 18:53:07 +0000191 return V; // if not, we don't know anything!
Chris Lattner69f8ce02001-09-11 04:27:34 +0000192
193 return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
Chris Lattner50020222001-11-26 18:53:07 +0000194 Right.Var,
Chris Lattner69f8ce02001-09-11 04:27:34 +0000195 DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
196 default:
197 assert(0 && "Dont' know how to handle this case!");
198 return ExprType();
199 }
200}
201
202// negate - Negate the value of the specified expression...
203//
204static inline ExprType negate(const ExprType &E, Value *V) {
205 const Type *Ty = V->getType();
206 const Type *ETy = E.getExprType(Ty);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000207 ConstantInt *Zero = getUnsignedConstant(0, ETy);
208 ConstantInt *One = getUnsignedConstant(1, ETy);
209 ConstantInt *NegOne = cast<ConstantInt>(*Zero - *One);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000210 if (NegOne == 0) return V; // Couldn't subtract values...
211
212 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
213 DefZero(E.Offset, Ty) * NegOne);
214}
Chris Lattner19f31f22001-07-21 19:07:19 +0000215
Chris Lattner369bbeb2001-07-20 19:17:55 +0000216
217// ClassifyExpression: Analyze an expression to determine the complexity of the
218// expression, and which other values it depends on.
219//
220// Note that this analysis cannot get into infinite loops because it treats PHI
221// nodes as being an unknown linear expression.
222//
Chris Lattner19f31f22001-07-21 19:07:19 +0000223ExprType analysis::ClassifyExpression(Value *Expr) {
Chris Lattner369bbeb2001-07-20 19:17:55 +0000224 assert(Expr != 0 && "Can't classify a null expression!");
Chris Lattner0da29c82001-12-05 19:38:29 +0000225 if (Expr->getType() == Type::FloatTy || Expr->getType() == Type::DoubleTy)
226 return Expr; // FIXME: Can't handle FP expressions
227
Chris Lattner369bbeb2001-07-20 19:17:55 +0000228 switch (Expr->getValueType()) {
229 case Value::InstructionVal: break; // Instruction... hmmm... investigate.
230 case Value::TypeVal: case Value::BasicBlockVal:
Chris Lattnerfad0d4f2001-09-10 20:07:57 +0000231 case Value::MethodVal: case Value::ModuleVal: default:
Chris Lattner481fafe2001-12-13 00:45:06 +0000232 //assert(0 && "Unexpected expression type to classify!");
Chris Lattner697954c2002-01-20 22:54:45 +0000233 std::cerr << "Bizarre thing to expr classify: " << Expr << "\n";
Chris Lattner481fafe2001-12-13 00:45:06 +0000234 return Expr;
Chris Lattneref9c23f2001-10-03 14:53:21 +0000235 case Value::GlobalVariableVal: // Global Variable & Method argument:
Chris Lattnerfad0d4f2001-09-10 20:07:57 +0000236 case Value::MethodArgumentVal: // nothing known, return variable itself
Chris Lattner369bbeb2001-07-20 19:17:55 +0000237 return Expr;
238 case Value::ConstantVal: // Constant value, just return constant
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000239 Constant *CPV = cast<Constant>(Expr);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000240 if (CPV->getType()->isIntegral()) { // It's an integral constant!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000241 ConstantInt *CPI = cast<ConstantInt>(Expr);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000242 return ExprType(CPI->equalsInt(0) ? 0 : CPI);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000243 }
244 return Expr;
245 }
246
Chris Lattner9636a912001-10-01 16:18:37 +0000247 Instruction *I = cast<Instruction>(Expr);
Chris Lattner19f31f22001-07-21 19:07:19 +0000248 const Type *Ty = I->getType();
Chris Lattner369bbeb2001-07-20 19:17:55 +0000249
250 switch (I->getOpcode()) { // Handle each instruction type seperately
251 case Instruction::Add: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000252 ExprType Left (ClassifyExpression(I->getOperand(0)));
253 ExprType Right(ClassifyExpression(I->getOperand(1)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000254 return handleAddition(Left, Right, I);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000255 } // end case Instruction::Add
256
Chris Lattner69f8ce02001-09-11 04:27:34 +0000257 case Instruction::Sub: {
258 ExprType Left (ClassifyExpression(I->getOperand(0)));
259 ExprType Right(ClassifyExpression(I->getOperand(1)));
Chris Lattner50020222001-11-26 18:53:07 +0000260 ExprType RightNeg = negate(Right, I);
261 if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale)
262 return I; // Could not negate value...
263 return handleAddition(Left, RightNeg, I);
Chris Lattner69f8ce02001-09-11 04:27:34 +0000264 } // end case Instruction::Sub
265
Chris Lattner369bbeb2001-07-20 19:17:55 +0000266 case Instruction::Shl: {
Chris Lattner19f31f22001-07-21 19:07:19 +0000267 ExprType Right(ClassifyExpression(I->getOperand(1)));
268 if (Right.ExprTy != ExprType::Constant) break;
269 ExprType Left(ClassifyExpression(I->getOperand(0)));
270 if (Right.Offset == 0) return Left; // shl x, 0 = x
271 assert(Right.Offset->getType() == Type::UByteTy &&
Chris Lattner369bbeb2001-07-20 19:17:55 +0000272 "Shift amount must always be a unsigned byte!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000273 uint64_t ShiftAmount = ((ConstantUInt*)Right.Offset)->getValue();
274 ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000275
Chris Lattner8e195e02001-09-07 16:31:04 +0000276 return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
277 DefZero(Left.Offset, Ty) * Multiplier);
Chris Lattner369bbeb2001-07-20 19:17:55 +0000278 } // end case Instruction::Shl
279
Chris Lattner19f31f22001-07-21 19:07:19 +0000280 case Instruction::Mul: {
281 ExprType Left (ClassifyExpression(I->getOperand(0)));
282 ExprType Right(ClassifyExpression(I->getOperand(1)));
283 if (Left.ExprTy > Right.ExprTy)
Chris Lattner697954c2002-01-20 22:54:45 +0000284 std::swap(Left, Right); // Make left be simpler than right
Chris Lattner19f31f22001-07-21 19:07:19 +0000285
286 if (Left.ExprTy != ExprType::Constant) // RHS must be > constant
287 return I; // Quadratic eqn! :(
288
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000289 const ConstantInt *Offs = Left.Offset;
Chris Lattner19f31f22001-07-21 19:07:19 +0000290 if (Offs == 0) return ExprType();
Chris Lattner8e195e02001-09-07 16:31:04 +0000291 return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
292 DefZero(Right.Offset, Ty) * Offs);
Chris Lattner19f31f22001-07-21 19:07:19 +0000293 } // end case Instruction::Mul
294
295 case Instruction::Cast: {
296 ExprType Src(ClassifyExpression(I->getOperand(0)));
Chris Lattner69f8ce02001-09-11 04:27:34 +0000297 const Type *DestTy = I->getType();
298 if (DestTy->isPointerType())
299 DestTy = Type::ULongTy; // Pointer types are represented as ulong
Chris Lattner19f31f22001-07-21 19:07:19 +0000300
Chris Lattner882572a2001-11-26 16:53:50 +0000301 /*
302 if (!Src.getExprType(0)->isLosslesslyConvertableTo(DestTy)) {
303 if (Src.ExprTy != ExprType::Constant)
304 return I; // Converting cast, and not a constant value...
305 }
306 */
Chris Lattner19f31f22001-07-21 19:07:19 +0000307
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000308 const ConstantInt *Offset = Src.Offset;
309 const ConstantInt *Scale = Src.Scale;
Chris Lattner882572a2001-11-26 16:53:50 +0000310 if (Offset) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000311 const Constant *CPV = ConstantFoldCastInstruction(Offset, DestTy);
Chris Lattner882572a2001-11-26 16:53:50 +0000312 if (!CPV) return I;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 Offset = cast<ConstantInt>(CPV);
Chris Lattner882572a2001-11-26 16:53:50 +0000314 }
315 if (Scale) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 const Constant *CPV = ConstantFoldCastInstruction(Scale, DestTy);
Chris Lattner882572a2001-11-26 16:53:50 +0000317 if (!CPV) return I;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 Scale = cast<ConstantInt>(CPV);
Chris Lattner882572a2001-11-26 16:53:50 +0000319 }
320 return ExprType(Scale, Src.Var, Offset);
Chris Lattner19f31f22001-07-21 19:07:19 +0000321 } // end case Instruction::Cast
Chris Lattner793d6782001-07-25 22:47:32 +0000322 // TODO: Handle SUB, SHR?
Chris Lattner369bbeb2001-07-20 19:17:55 +0000323
324 } // end switch
325
326 // Otherwise, I don't know anything about this value!
Chris Lattner19f31f22001-07-21 19:07:19 +0000327 return I;
Chris Lattner369bbeb2001-07-20 19:17:55 +0000328}