Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 1 | //===- 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" |
Chris Lattner | 59b6b8e | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 11 | #include "llvm/Transforms/Scalar/ConstantHandling.h" |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 12 | #include "llvm/Method.h" |
| 13 | #include "llvm/BasicBlock.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 14 | #include <iostream> |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 16 | using namespace analysis; |
| 17 | |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 18 | ExprType::ExprType(Value *Val) { |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 19 | if (Val) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 20 | if (ConstantInt *CPI = dyn_cast<ConstantInt>(Val)) { |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 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 Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 30 | Scale = 0; |
| 31 | } |
| 32 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 33 | ExprType::ExprType(const ConstantInt *scale, Value *var, |
| 34 | const ConstantInt *offset) { |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 35 | Scale = var ? scale : 0; Var = var; Offset = offset; |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 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 | |
| 44 | const 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 Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 52 | class DefVal { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 53 | const ConstantInt * const Val; |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 54 | const Type * const Ty; |
| 55 | protected: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 56 | inline DefVal(const ConstantInt *val, const Type *ty) : Val(val), Ty(ty) {} |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 57 | public: |
| 58 | inline const Type *getType() const { return Ty; } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 59 | inline const ConstantInt *getVal() const { return Val; } |
| 60 | inline operator const ConstantInt * () const { return Val; } |
| 61 | inline const ConstantInt *operator->() const { return Val; } |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct DefZero : public DefVal { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 65 | inline DefZero(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {} |
| 66 | inline DefZero(const ConstantInt *val) : DefVal(val, val->getType()) {} |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct DefOne : public DefVal { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 70 | inline DefOne(const ConstantInt *val, const Type *ty) : DefVal(val, ty) {} |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 9b53426 | 2002-03-14 22:35:50 +0000 | [diff] [blame^] | 74 | // getUnsignedConstant - Return a constant value of the specified type. If the |
| 75 | // constant value is not valid for the specified type, return null. This cannot |
| 76 | // happen for values in the range of 0 to 127. |
| 77 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 78 | static ConstantInt *getUnsignedConstant(uint64_t V, const Type *Ty) { |
Chris Lattner | 793d678 | 2001-07-25 22:47:32 +0000 | [diff] [blame] | 79 | if (Ty->isPointerType()) Ty = Type::ULongTy; |
Chris Lattner | 9b53426 | 2002-03-14 22:35:50 +0000 | [diff] [blame^] | 80 | if (Ty->isSigned()) { |
| 81 | // If this value is not a valid unsigned value for this type, return null! |
| 82 | if (V > 127 && ((int64_t)V < 0 || |
| 83 | !ConstantSInt::isValueValidForType(Ty, (int64_t)V))) |
| 84 | return 0; |
| 85 | return ConstantSInt::get(Ty, V); |
| 86 | } else { |
| 87 | // If this value is not a valid unsigned value for this type, return null! |
| 88 | if (V > 255 && !ConstantUInt::isValueValidForType(Ty, V)) |
| 89 | return 0; |
| 90 | return ConstantUInt::get(Ty, V); |
| 91 | } |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 94 | // Add - Helper function to make later code simpler. Basically it just adds |
| 95 | // the two constants together, inserts the result into the constant pool, and |
| 96 | // returns it. Of course life is not simple, and this is no exception. Factors |
| 97 | // that complicate matters: |
| 98 | // 1. Either argument may be null. If this is the case, the null argument is |
| 99 | // treated as either 0 (if DefOne = false) or 1 (if DefOne = true) |
| 100 | // 2. Types get in the way. We want to do arithmetic operations without |
| 101 | // regard for the underlying types. It is assumed that the constants are |
| 102 | // integral constants. The new value takes the type of the left argument. |
| 103 | // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne |
| 104 | // is false, a null return value indicates a value of 0. |
| 105 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 106 | static const ConstantInt *Add(const ConstantInt *Arg1, |
| 107 | const ConstantInt *Arg2, bool DefOne) { |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 108 | assert(Arg1 && Arg2 && "No null arguments should exist now!"); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 109 | assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!"); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 110 | |
| 111 | // Actually perform the computation now! |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 112 | Constant *Result = *Arg1 + *Arg2; |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 113 | assert(Result && Result->getType() == Arg1->getType() && |
| 114 | "Couldn't perform addition!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 115 | ConstantInt *ResultI = cast<ConstantInt>(Result); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 116 | |
| 117 | // Check to see if the result is one of the special cases that we want to |
| 118 | // recognize... |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 119 | if (ResultI->equalsInt(DefOne ? 1 : 0)) |
| 120 | return 0; // Yes it is, simply return null. |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 122 | return ResultI; |
| 123 | } |
| 124 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 125 | inline const ConstantInt *operator+(const DefZero &L, const DefZero &R) { |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 126 | if (L == 0) return R; |
| 127 | if (R == 0) return L; |
Chris Lattner | 8e195e0 | 2001-09-07 16:31:04 +0000 | [diff] [blame] | 128 | return Add(L, R, false); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 130 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 131 | inline const ConstantInt *operator+(const DefOne &L, const DefOne &R) { |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 132 | if (L == 0) { |
| 133 | if (R == 0) |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 134 | return getUnsignedConstant(2, L.getType()); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 135 | else |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 136 | return Add(getUnsignedConstant(1, L.getType()), R, true); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 137 | } else if (R == 0) { |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 138 | return Add(L, getUnsignedConstant(1, L.getType()), true); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 8e195e0 | 2001-09-07 16:31:04 +0000 | [diff] [blame] | 140 | return Add(L, R, true); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 144 | // Mul - Helper function to make later code simpler. Basically it just |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 145 | // multiplies the two constants together, inserts the result into the constant |
| 146 | // pool, and returns it. Of course life is not simple, and this is no |
| 147 | // exception. Factors that complicate matters: |
| 148 | // 1. Either argument may be null. If this is the case, the null argument is |
| 149 | // treated as either 0 (if DefOne = false) or 1 (if DefOne = true) |
| 150 | // 2. Types get in the way. We want to do arithmetic operations without |
| 151 | // regard for the underlying types. It is assumed that the constants are |
| 152 | // integral constants. |
| 153 | // 3. If DefOne is true, a null return value indicates a value of 1, if DefOne |
| 154 | // is false, a null return value indicates a value of 0. |
| 155 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 156 | inline const ConstantInt *Mul(const ConstantInt *Arg1, |
| 157 | const ConstantInt *Arg2, bool DefOne) { |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 158 | assert(Arg1 && Arg2 && "No null arguments should exist now!"); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 159 | assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!"); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 160 | |
| 161 | // Actually perform the computation now! |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 162 | Constant *Result = *Arg1 * *Arg2; |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 163 | assert(Result && Result->getType() == Arg1->getType() && |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 164 | "Couldn't perform multiplication!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 165 | ConstantInt *ResultI = cast<ConstantInt>(Result); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 166 | |
| 167 | // Check to see if the result is one of the special cases that we want to |
| 168 | // recognize... |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 169 | if (ResultI->equalsInt(DefOne ? 1 : 0)) |
| 170 | return 0; // Yes it is, simply return null. |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 172 | return ResultI; |
| 173 | } |
| 174 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 175 | inline const ConstantInt *operator*(const DefZero &L, const DefZero &R) { |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 176 | if (L == 0 || R == 0) return 0; |
Chris Lattner | 8e195e0 | 2001-09-07 16:31:04 +0000 | [diff] [blame] | 177 | return Mul(L, R, false); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 179 | inline const ConstantInt *operator*(const DefOne &L, const DefZero &R) { |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 180 | if (R == 0) return getUnsignedConstant(0, L.getType()); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 181 | if (L == 0) return R->equalsInt(1) ? 0 : R.getVal(); |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 182 | return Mul(L, R, true); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 184 | inline const ConstantInt *operator*(const DefZero &L, const DefOne &R) { |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 185 | if (L == 0 || R == 0) return L.getVal(); |
| 186 | return Mul(R, L, false); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 189 | // handleAddition - Add two expressions together, creating a new expression that |
| 190 | // represents the composite of the two... |
| 191 | // |
| 192 | static ExprType handleAddition(ExprType Left, ExprType Right, Value *V) { |
| 193 | const Type *Ty = V->getType(); |
| 194 | if (Left.ExprTy > Right.ExprTy) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 195 | std::swap(Left, Right); // Make left be simpler than right |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 196 | |
| 197 | switch (Left.ExprTy) { |
| 198 | case ExprType::Constant: |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 199 | return ExprType(Right.Scale, Right.Var, |
| 200 | DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty)); |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 201 | case ExprType::Linear: // RHS side must be linear or scaled |
| 202 | case ExprType::ScaledLinear: // RHS must be scaled |
| 203 | if (Left.Var != Right.Var) // Are they the same variables? |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 204 | return V; // if not, we don't know anything! |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 205 | |
| 206 | return ExprType(DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty), |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 207 | Right.Var, |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 208 | DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty)); |
| 209 | default: |
| 210 | assert(0 && "Dont' know how to handle this case!"); |
| 211 | return ExprType(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // negate - Negate the value of the specified expression... |
| 216 | // |
| 217 | static inline ExprType negate(const ExprType &E, Value *V) { |
| 218 | const Type *Ty = V->getType(); |
Chris Lattner | cb05e78 | 2002-03-11 20:50:24 +0000 | [diff] [blame] | 219 | ConstantInt *Zero = getUnsignedConstant(0, Ty); |
| 220 | ConstantInt *One = getUnsignedConstant(1, Ty); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 221 | ConstantInt *NegOne = cast<ConstantInt>(*Zero - *One); |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 222 | if (NegOne == 0) return V; // Couldn't subtract values... |
| 223 | |
| 224 | return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var, |
| 225 | DefZero(E.Offset, Ty) * NegOne); |
| 226 | } |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 228 | |
| 229 | // ClassifyExpression: Analyze an expression to determine the complexity of the |
| 230 | // expression, and which other values it depends on. |
| 231 | // |
| 232 | // Note that this analysis cannot get into infinite loops because it treats PHI |
| 233 | // nodes as being an unknown linear expression. |
| 234 | // |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 235 | ExprType analysis::ClassifyExpression(Value *Expr) { |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 236 | assert(Expr != 0 && "Can't classify a null expression!"); |
Chris Lattner | 0da29c8 | 2001-12-05 19:38:29 +0000 | [diff] [blame] | 237 | if (Expr->getType() == Type::FloatTy || Expr->getType() == Type::DoubleTy) |
| 238 | return Expr; // FIXME: Can't handle FP expressions |
| 239 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 240 | switch (Expr->getValueType()) { |
| 241 | case Value::InstructionVal: break; // Instruction... hmmm... investigate. |
| 242 | case Value::TypeVal: case Value::BasicBlockVal: |
Chris Lattner | fad0d4f | 2001-09-10 20:07:57 +0000 | [diff] [blame] | 243 | case Value::MethodVal: case Value::ModuleVal: default: |
Chris Lattner | 481fafe | 2001-12-13 00:45:06 +0000 | [diff] [blame] | 244 | //assert(0 && "Unexpected expression type to classify!"); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 245 | std::cerr << "Bizarre thing to expr classify: " << Expr << "\n"; |
Chris Lattner | 481fafe | 2001-12-13 00:45:06 +0000 | [diff] [blame] | 246 | return Expr; |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 247 | case Value::GlobalVariableVal: // Global Variable & Method argument: |
Chris Lattner | fad0d4f | 2001-09-10 20:07:57 +0000 | [diff] [blame] | 248 | case Value::MethodArgumentVal: // nothing known, return variable itself |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 249 | return Expr; |
| 250 | case Value::ConstantVal: // Constant value, just return constant |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 251 | Constant *CPV = cast<Constant>(Expr); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 252 | if (CPV->getType()->isIntegral()) { // It's an integral constant! |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 253 | ConstantInt *CPI = cast<ConstantInt>(Expr); |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 254 | return ExprType(CPI->equalsInt(0) ? 0 : CPI); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 255 | } |
| 256 | return Expr; |
| 257 | } |
| 258 | |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 259 | Instruction *I = cast<Instruction>(Expr); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 260 | const Type *Ty = I->getType(); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 261 | |
| 262 | switch (I->getOpcode()) { // Handle each instruction type seperately |
| 263 | case Instruction::Add: { |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 264 | ExprType Left (ClassifyExpression(I->getOperand(0))); |
| 265 | ExprType Right(ClassifyExpression(I->getOperand(1))); |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 266 | return handleAddition(Left, Right, I); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 267 | } // end case Instruction::Add |
| 268 | |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 269 | case Instruction::Sub: { |
| 270 | ExprType Left (ClassifyExpression(I->getOperand(0))); |
| 271 | ExprType Right(ClassifyExpression(I->getOperand(1))); |
Chris Lattner | 5002022 | 2001-11-26 18:53:07 +0000 | [diff] [blame] | 272 | ExprType RightNeg = negate(Right, I); |
| 273 | if (RightNeg.Var == I && !RightNeg.Offset && !RightNeg.Scale) |
| 274 | return I; // Could not negate value... |
| 275 | return handleAddition(Left, RightNeg, I); |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 276 | } // end case Instruction::Sub |
| 277 | |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 278 | case Instruction::Shl: { |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 279 | ExprType Right(ClassifyExpression(I->getOperand(1))); |
| 280 | if (Right.ExprTy != ExprType::Constant) break; |
| 281 | ExprType Left(ClassifyExpression(I->getOperand(0))); |
| 282 | if (Right.Offset == 0) return Left; // shl x, 0 = x |
| 283 | assert(Right.Offset->getType() == Type::UByteTy && |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 284 | "Shift amount must always be a unsigned byte!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 285 | uint64_t ShiftAmount = ((ConstantUInt*)Right.Offset)->getValue(); |
| 286 | ConstantInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty); |
Chris Lattner | 9b53426 | 2002-03-14 22:35:50 +0000 | [diff] [blame^] | 287 | |
| 288 | // We don't know how to classify it if they are shifting by more than what |
| 289 | // is reasonable. In most cases, the result will be zero, but there is one |
| 290 | // class of cases where it is not, so we cannot optimize without checking |
| 291 | // for it. The case is when you are shifting a signed value by 1 less than |
| 292 | // the number of bits in the value. For example: |
| 293 | // %X = shl sbyte %Y, ubyte 7 |
| 294 | // will try to form an sbyte multiplier of 128, which will give a null |
| 295 | // multiplier, even though the result is not 0. Until we can check for this |
| 296 | // case, be conservative. TODO. |
| 297 | // |
| 298 | if (Multiplier == 0) |
| 299 | return Expr; |
| 300 | |
Chris Lattner | 8e195e0 | 2001-09-07 16:31:04 +0000 | [diff] [blame] | 301 | return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var, |
| 302 | DefZero(Left.Offset, Ty) * Multiplier); |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 303 | } // end case Instruction::Shl |
| 304 | |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 305 | case Instruction::Mul: { |
| 306 | ExprType Left (ClassifyExpression(I->getOperand(0))); |
| 307 | ExprType Right(ClassifyExpression(I->getOperand(1))); |
| 308 | if (Left.ExprTy > Right.ExprTy) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 309 | std::swap(Left, Right); // Make left be simpler than right |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 310 | |
| 311 | if (Left.ExprTy != ExprType::Constant) // RHS must be > constant |
| 312 | return I; // Quadratic eqn! :( |
| 313 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 314 | const ConstantInt *Offs = Left.Offset; |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 315 | if (Offs == 0) return ExprType(); |
Chris Lattner | 8e195e0 | 2001-09-07 16:31:04 +0000 | [diff] [blame] | 316 | return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var, |
| 317 | DefZero(Right.Offset, Ty) * Offs); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 318 | } // end case Instruction::Mul |
| 319 | |
| 320 | case Instruction::Cast: { |
| 321 | ExprType Src(ClassifyExpression(I->getOperand(0))); |
Chris Lattner | 69f8ce0 | 2001-09-11 04:27:34 +0000 | [diff] [blame] | 322 | const Type *DestTy = I->getType(); |
| 323 | if (DestTy->isPointerType()) |
| 324 | DestTy = Type::ULongTy; // Pointer types are represented as ulong |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 882572a | 2001-11-26 16:53:50 +0000 | [diff] [blame] | 326 | /* |
| 327 | if (!Src.getExprType(0)->isLosslesslyConvertableTo(DestTy)) { |
| 328 | if (Src.ExprTy != ExprType::Constant) |
| 329 | return I; // Converting cast, and not a constant value... |
| 330 | } |
| 331 | */ |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 332 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 333 | const ConstantInt *Offset = Src.Offset; |
| 334 | const ConstantInt *Scale = Src.Scale; |
Chris Lattner | 882572a | 2001-11-26 16:53:50 +0000 | [diff] [blame] | 335 | if (Offset) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 336 | const Constant *CPV = ConstantFoldCastInstruction(Offset, DestTy); |
Chris Lattner | 882572a | 2001-11-26 16:53:50 +0000 | [diff] [blame] | 337 | if (!CPV) return I; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 338 | Offset = cast<ConstantInt>(CPV); |
Chris Lattner | 882572a | 2001-11-26 16:53:50 +0000 | [diff] [blame] | 339 | } |
| 340 | if (Scale) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 341 | const Constant *CPV = ConstantFoldCastInstruction(Scale, DestTy); |
Chris Lattner | 882572a | 2001-11-26 16:53:50 +0000 | [diff] [blame] | 342 | if (!CPV) return I; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 343 | Scale = cast<ConstantInt>(CPV); |
Chris Lattner | 882572a | 2001-11-26 16:53:50 +0000 | [diff] [blame] | 344 | } |
| 345 | return ExprType(Scale, Src.Var, Offset); |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 346 | } // end case Instruction::Cast |
Chris Lattner | 793d678 | 2001-07-25 22:47:32 +0000 | [diff] [blame] | 347 | // TODO: Handle SUB, SHR? |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 348 | |
| 349 | } // end switch |
| 350 | |
| 351 | // Otherwise, I don't know anything about this value! |
Chris Lattner | 19f31f2 | 2001-07-21 19:07:19 +0000 | [diff] [blame] | 352 | return I; |
Chris Lattner | 369bbeb | 2001-07-20 19:17:55 +0000 | [diff] [blame] | 353 | } |