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