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