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