Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 1 | //===- ConstantFolding.cpp - LLVM constant folder -------------------------===// |
John Criswell | 482202a | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 10 | // This file implements folding of constants for LLVM. This implements the |
| 11 | // (internal) ConstantFolding.h interface, which is used by the |
| 12 | // ConstantExpr::get* methods to automatically fold constants when possible. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 14 | // The current constant folding implementation is implemented in two pieces: the |
| 15 | // template-based folder for simple primitive constants like ConstantInt, and |
| 16 | // the special case hackery that we use to symbolically evaluate expressions |
| 17 | // that use ConstantExprs. |
| 18 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 21 | #include "ConstantFolding.h" |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | a9eddae | 2004-02-22 06:25:38 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 25 | #include "llvm/Function.h" |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 27 | #include <cmath> |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | struct ConstRules { |
| 32 | ConstRules() {} |
| 33 | |
| 34 | // Binary Operators... |
| 35 | virtual Constant *add(const Constant *V1, const Constant *V2) const = 0; |
| 36 | virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0; |
| 37 | virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0; |
| 38 | virtual Constant *div(const Constant *V1, const Constant *V2) const = 0; |
| 39 | virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0; |
| 40 | virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0; |
| 41 | virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0; |
| 42 | virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0; |
| 43 | virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0; |
| 44 | virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0; |
| 45 | virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0; |
| 46 | virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0; |
| 47 | |
| 48 | // Casting operators. |
| 49 | virtual Constant *castToBool (const Constant *V) const = 0; |
| 50 | virtual Constant *castToSByte (const Constant *V) const = 0; |
| 51 | virtual Constant *castToUByte (const Constant *V) const = 0; |
| 52 | virtual Constant *castToShort (const Constant *V) const = 0; |
| 53 | virtual Constant *castToUShort(const Constant *V) const = 0; |
| 54 | virtual Constant *castToInt (const Constant *V) const = 0; |
| 55 | virtual Constant *castToUInt (const Constant *V) const = 0; |
| 56 | virtual Constant *castToLong (const Constant *V) const = 0; |
| 57 | virtual Constant *castToULong (const Constant *V) const = 0; |
| 58 | virtual Constant *castToFloat (const Constant *V) const = 0; |
| 59 | virtual Constant *castToDouble(const Constant *V) const = 0; |
| 60 | virtual Constant *castToPointer(const Constant *V, |
| 61 | const PointerType *Ty) const = 0; |
| 62 | |
| 63 | // ConstRules::get - Return an instance of ConstRules for the specified |
| 64 | // constant operands. |
| 65 | // |
| 66 | static ConstRules &get(const Constant *V1, const Constant *V2); |
| 67 | private: |
| 68 | ConstRules(const ConstRules &); // Do not implement |
| 69 | ConstRules &operator=(const ConstRules &); // Do not implement |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // TemplateRules Class |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // |
| 78 | // TemplateRules - Implement a subclass of ConstRules that provides all |
| 79 | // operations as noops. All other rules classes inherit from this class so |
| 80 | // that if functionality is needed in the future, it can simply be added here |
| 81 | // and to ConstRules without changing anything else... |
| 82 | // |
| 83 | // This class also provides subclasses with typesafe implementations of methods |
| 84 | // so that don't have to do type casting. |
| 85 | // |
| 86 | template<class ArgType, class SubClassName> |
| 87 | class TemplateRules : public ConstRules { |
| 88 | |
| 89 | //===--------------------------------------------------------------------===// |
| 90 | // Redirecting functions that cast to the appropriate types |
| 91 | //===--------------------------------------------------------------------===// |
| 92 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 93 | virtual Constant *add(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 94 | return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2); |
| 95 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 96 | virtual Constant *sub(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 97 | return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2); |
| 98 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 99 | virtual Constant *mul(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 100 | return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); |
| 101 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 102 | virtual Constant *div(const Constant *V1, const Constant *V2) const { |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 103 | return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2); |
| 104 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 105 | virtual Constant *rem(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 106 | return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2); |
| 107 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 108 | virtual Constant *op_and(const Constant *V1, const Constant *V2) const { |
| 109 | return SubClassName::And((const ArgType *)V1, (const ArgType *)V2); |
| 110 | } |
| 111 | virtual Constant *op_or(const Constant *V1, const Constant *V2) const { |
| 112 | return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2); |
| 113 | } |
| 114 | virtual Constant *op_xor(const Constant *V1, const Constant *V2) const { |
| 115 | return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2); |
| 116 | } |
| 117 | virtual Constant *shl(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 118 | return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2); |
| 119 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 120 | virtual Constant *shr(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 121 | return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2); |
| 122 | } |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 124 | virtual Constant *lessthan(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 125 | return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2); |
| 126 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 127 | virtual Constant *equalto(const Constant *V1, const Constant *V2) const { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 128 | return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2); |
| 129 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 131 | // Casting operators. ick |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 132 | virtual Constant *castToBool(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 133 | return SubClassName::CastToBool((const ArgType*)V); |
| 134 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 135 | virtual Constant *castToSByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 136 | return SubClassName::CastToSByte((const ArgType*)V); |
| 137 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 138 | virtual Constant *castToUByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 139 | return SubClassName::CastToUByte((const ArgType*)V); |
| 140 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 141 | virtual Constant *castToShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 142 | return SubClassName::CastToShort((const ArgType*)V); |
| 143 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 144 | virtual Constant *castToUShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 145 | return SubClassName::CastToUShort((const ArgType*)V); |
| 146 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 147 | virtual Constant *castToInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 148 | return SubClassName::CastToInt((const ArgType*)V); |
| 149 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 150 | virtual Constant *castToUInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 151 | return SubClassName::CastToUInt((const ArgType*)V); |
| 152 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 153 | virtual Constant *castToLong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 154 | return SubClassName::CastToLong((const ArgType*)V); |
| 155 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 156 | virtual Constant *castToULong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 157 | return SubClassName::CastToULong((const ArgType*)V); |
| 158 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 159 | virtual Constant *castToFloat(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 160 | return SubClassName::CastToFloat((const ArgType*)V); |
| 161 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 162 | virtual Constant *castToDouble(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 163 | return SubClassName::CastToDouble((const ArgType*)V); |
| 164 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 165 | virtual Constant *castToPointer(const Constant *V, |
| 166 | const PointerType *Ty) const { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 167 | return SubClassName::CastToPointer((const ArgType*)V, Ty); |
| 168 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 170 | //===--------------------------------------------------------------------===// |
| 171 | // Default "noop" implementations |
| 172 | //===--------------------------------------------------------------------===// |
| 173 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 174 | static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; } |
| 175 | static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; } |
| 176 | static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; } |
| 177 | static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; } |
| 178 | static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; } |
| 179 | static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; } |
| 180 | static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; } |
| 181 | static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; } |
| 182 | static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; } |
| 183 | static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 184 | static Constant *LessThan(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 187 | static Constant *EqualTo(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 190 | |
| 191 | // Casting operators. ick |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 192 | static Constant *CastToBool (const Constant *V) { return 0; } |
| 193 | static Constant *CastToSByte (const Constant *V) { return 0; } |
| 194 | static Constant *CastToUByte (const Constant *V) { return 0; } |
| 195 | static Constant *CastToShort (const Constant *V) { return 0; } |
| 196 | static Constant *CastToUShort(const Constant *V) { return 0; } |
| 197 | static Constant *CastToInt (const Constant *V) { return 0; } |
| 198 | static Constant *CastToUInt (const Constant *V) { return 0; } |
| 199 | static Constant *CastToLong (const Constant *V) { return 0; } |
| 200 | static Constant *CastToULong (const Constant *V) { return 0; } |
| 201 | static Constant *CastToFloat (const Constant *V) { return 0; } |
| 202 | static Constant *CastToDouble(const Constant *V) { return 0; } |
| 203 | static Constant *CastToPointer(const Constant *, |
| 204 | const PointerType *) {return 0;} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | |
| 208 | |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | // EmptyRules Class |
| 211 | //===----------------------------------------------------------------------===// |
| 212 | // |
| 213 | // EmptyRules provides a concrete base class of ConstRules that does nothing |
| 214 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 215 | struct EmptyRules : public TemplateRules<Constant, EmptyRules> { |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 216 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 217 | if (V1 == V2) return ConstantBool::True; |
| 218 | return 0; |
| 219 | } |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 220 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | |
| 222 | |
| 223 | |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | // BoolRules Class |
| 226 | //===----------------------------------------------------------------------===// |
| 227 | // |
| 228 | // BoolRules provides a concrete base class of ConstRules for the 'bool' type. |
| 229 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 230 | struct BoolRules : public TemplateRules<ConstantBool, BoolRules> { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 232 | static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){ |
Chris Lattner | 07507a4 | 2002-09-03 20:09:49 +0000 | [diff] [blame] | 233 | return ConstantBool::get(V1->getValue() < V2->getValue()); |
| 234 | } |
| 235 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 236 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 237 | return ConstantBool::get(V1 == V2); |
| 238 | } |
| 239 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 240 | static Constant *And(const ConstantBool *V1, const ConstantBool *V2) { |
| 241 | return ConstantBool::get(V1->getValue() & V2->getValue()); |
| 242 | } |
| 243 | |
| 244 | static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 245 | return ConstantBool::get(V1->getValue() | V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 248 | static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) { |
| 249 | return ConstantBool::get(V1->getValue() ^ V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | cea4d8c | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 251 | |
| 252 | // Casting operators. ick |
| 253 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 254 | static Constant *CastTo##TYPE (const ConstantBool *V) { \ |
Chris Lattner | cea4d8c | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 255 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \ |
| 256 | } |
| 257 | |
| 258 | DEF_CAST(Bool , ConstantBool, bool) |
| 259 | DEF_CAST(SByte , ConstantSInt, signed char) |
| 260 | DEF_CAST(UByte , ConstantUInt, unsigned char) |
| 261 | DEF_CAST(Short , ConstantSInt, signed short) |
| 262 | DEF_CAST(UShort, ConstantUInt, unsigned short) |
| 263 | DEF_CAST(Int , ConstantSInt, signed int) |
| 264 | DEF_CAST(UInt , ConstantUInt, unsigned int) |
| 265 | DEF_CAST(Long , ConstantSInt, int64_t) |
| 266 | DEF_CAST(ULong , ConstantUInt, uint64_t) |
| 267 | DEF_CAST(Float , ConstantFP , float) |
| 268 | DEF_CAST(Double, ConstantFP , double) |
| 269 | #undef DEF_CAST |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 270 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 274 | // NullPointerRules Class |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 275 | //===----------------------------------------------------------------------===// |
| 276 | // |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 277 | // NullPointerRules provides a concrete base class of ConstRules for null |
| 278 | // pointers. |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 279 | // |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 280 | struct NullPointerRules : public TemplateRules<ConstantPointerNull, |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 281 | NullPointerRules> { |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 282 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 283 | return ConstantBool::True; // Null pointers are always equal |
| 284 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 285 | static Constant *CastToBool(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 286 | return ConstantBool::False; |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 287 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 288 | static Constant *CastToSByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 289 | return ConstantSInt::get(Type::SByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 291 | static Constant *CastToUByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 292 | return ConstantUInt::get(Type::UByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 294 | static Constant *CastToShort (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 295 | return ConstantSInt::get(Type::ShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 296 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 297 | static Constant *CastToUShort(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 298 | return ConstantUInt::get(Type::UShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 299 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 300 | static Constant *CastToInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 301 | return ConstantSInt::get(Type::IntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 302 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 303 | static Constant *CastToUInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 304 | return ConstantUInt::get(Type::UIntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 306 | static Constant *CastToLong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 307 | return ConstantSInt::get(Type::LongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 309 | static Constant *CastToULong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 310 | return ConstantUInt::get(Type::ULongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 312 | static Constant *CastToFloat (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 313 | return ConstantFP::get(Type::FloatTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 315 | static Constant *CastToDouble(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 316 | return ConstantFP::get(Type::DoubleTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 319 | static Constant *CastToPointer(const ConstantPointerNull *V, |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 320 | const PointerType *PTy) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 321 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 322 | } |
| 323 | }; |
| 324 | |
| 325 | |
| 326 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 327 | // DirectRules Class |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | // |
| 330 | // DirectRules provides a concrete base classes of ConstRules for a variety of |
| 331 | // different types. This allows the C++ compiler to automatically generate our |
| 332 | // constant handling operations in a typesafe and accurate manner. |
| 333 | // |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 334 | template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass> |
| 335 | struct DirectRules : public TemplateRules<ConstantClass, SuperClass> { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 336 | static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) { |
| 337 | BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue(); |
| 338 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 341 | static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) { |
| 342 | BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue(); |
| 343 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 346 | static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) { |
| 347 | BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue(); |
| 348 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 351 | static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 352 | if (V2->isNullValue()) return 0; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 353 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
| 354 | return ConstantClass::get(*Ty, R); |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 357 | static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 358 | bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); |
| 359 | return ConstantBool::get(R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 362 | static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 363 | bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue(); |
| 364 | return ConstantBool::get(R); |
| 365 | } |
| 366 | |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 367 | static Constant *CastToPointer(const ConstantClass *V, |
| 368 | const PointerType *PTy) { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 369 | if (V->isNullValue()) // Is it a FP or Integral null value? |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 370 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 371 | return 0; // Can't const prop other types of pointers |
| 372 | } |
| 373 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 374 | // Casting operators. ick |
| 375 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 376 | static Constant *CastTo##TYPE (const ConstantClass *V) { \ |
Chris Lattner | bbb2296 | 2001-09-07 16:40:34 +0000 | [diff] [blame] | 377 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 380 | DEF_CAST(Bool , ConstantBool, bool) |
| 381 | DEF_CAST(SByte , ConstantSInt, signed char) |
| 382 | DEF_CAST(UByte , ConstantUInt, unsigned char) |
| 383 | DEF_CAST(Short , ConstantSInt, signed short) |
| 384 | DEF_CAST(UShort, ConstantUInt, unsigned short) |
| 385 | DEF_CAST(Int , ConstantSInt, signed int) |
| 386 | DEF_CAST(UInt , ConstantUInt, unsigned int) |
| 387 | DEF_CAST(Long , ConstantSInt, int64_t) |
| 388 | DEF_CAST(ULong , ConstantUInt, uint64_t) |
| 389 | DEF_CAST(Float , ConstantFP , float) |
| 390 | DEF_CAST(Double, ConstantFP , double) |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 391 | #undef DEF_CAST |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
Chris Lattner | 62af86e | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 394 | |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | // DirectIntRules Class |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | // |
| 399 | // DirectIntRules provides implementations of functions that are valid on |
| 400 | // integer types, but not all types in general. |
| 401 | // |
| 402 | template <class ConstantClass, class BuiltinType, Type **Ty> |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 403 | struct DirectIntRules |
| 404 | : public DirectRules<ConstantClass, BuiltinType, Ty, |
| 405 | DirectIntRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 26891626 | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 407 | static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { |
| 408 | if (V2->isNullValue()) return 0; |
| 409 | if (V2->isAllOnesValue() && // MIN_INT / -1 |
| 410 | (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue()) |
| 411 | return 0; |
| 412 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
| 413 | return ConstantClass::get(*Ty, R); |
| 414 | } |
| 415 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 416 | static Constant *Rem(const ConstantClass *V1, |
| 417 | const ConstantClass *V2) { |
Chris Lattner | 26891626 | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 418 | if (V2->isNullValue()) return 0; // X / 0 |
| 419 | if (V2->isAllOnesValue() && // MIN_INT / -1 |
| 420 | (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue()) |
| 421 | return 0; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 422 | BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue(); |
| 423 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 424 | } |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 425 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 426 | static Constant *And(const ConstantClass *V1, const ConstantClass *V2) { |
| 427 | BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue(); |
| 428 | return ConstantClass::get(*Ty, R); |
| 429 | } |
| 430 | static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) { |
| 431 | BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue(); |
| 432 | return ConstantClass::get(*Ty, R); |
| 433 | } |
| 434 | static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) { |
| 435 | BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue(); |
| 436 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 439 | static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) { |
| 440 | BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue(); |
| 441 | return ConstantClass::get(*Ty, R); |
| 442 | } |
| 443 | |
| 444 | static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) { |
| 445 | BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue(); |
| 446 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 447 | } |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | |
| 451 | //===----------------------------------------------------------------------===// |
| 452 | // DirectFPRules Class |
| 453 | //===----------------------------------------------------------------------===// |
| 454 | // |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 455 | /// DirectFPRules provides implementations of functions that are valid on |
| 456 | /// floating point types, but not all types in general. |
| 457 | /// |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 458 | template <class ConstantClass, class BuiltinType, Type **Ty> |
| 459 | struct DirectFPRules |
| 460 | : public DirectRules<ConstantClass, BuiltinType, Ty, |
| 461 | DirectFPRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 462 | static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 463 | if (V2->isNullValue()) return 0; |
| 464 | BuiltinType Result = std::fmod((BuiltinType)V1->getValue(), |
| 465 | (BuiltinType)V2->getValue()); |
| 466 | return ConstantClass::get(*Ty, Result); |
| 467 | } |
Chris Lattner | 62af86e | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 468 | }; |
| 469 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 470 | |
| 471 | /// ConstRules::get - This method returns the constant rules implementation that |
| 472 | /// implements the semantics of the two specified constants. |
Chris Lattner | f8348c3 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 473 | ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 474 | static EmptyRules EmptyR; |
| 475 | static BoolRules BoolR; |
| 476 | static NullPointerRules NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 477 | static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR; |
| 478 | static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR; |
| 479 | static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR; |
| 480 | static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR; |
| 481 | static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR; |
| 482 | static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR; |
| 483 | static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR; |
| 484 | static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR; |
| 485 | static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR; |
| 486 | static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 488 | if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) || |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 489 | isa<GlobalValue>(V1) || isa<GlobalValue>(V2)) |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 490 | return EmptyR; |
| 491 | |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 492 | switch (V1->getType()->getTypeID()) { |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 493 | default: assert(0 && "Unknown value type for constant folding!"); |
| 494 | case Type::BoolTyID: return BoolR; |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 495 | case Type::PointerTyID: return NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 496 | case Type::SByteTyID: return SByteR; |
| 497 | case Type::UByteTyID: return UByteR; |
| 498 | case Type::ShortTyID: return ShortR; |
| 499 | case Type::UShortTyID: return UShortR; |
| 500 | case Type::IntTyID: return IntR; |
| 501 | case Type::UIntTyID: return UIntR; |
| 502 | case Type::LongTyID: return LongR; |
| 503 | case Type::ULongTyID: return ULongR; |
| 504 | case Type::FloatTyID: return FloatR; |
| 505 | case Type::DoubleTyID: return DoubleR; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 506 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 508 | |
| 509 | |
| 510 | //===----------------------------------------------------------------------===// |
| 511 | // ConstantFold*Instruction Implementations |
| 512 | //===----------------------------------------------------------------------===// |
| 513 | // |
| 514 | // These methods contain the special case hackery required to symbolically |
| 515 | // evaluate some constant expression cases, and use the ConstantRules class to |
| 516 | // evaluate normal constants. |
| 517 | // |
| 518 | static unsigned getSize(const Type *Ty) { |
| 519 | unsigned S = Ty->getPrimitiveSize(); |
| 520 | return S ? S : 8; // Treat pointers at 8 bytes |
| 521 | } |
| 522 | |
| 523 | Constant *llvm::ConstantFoldCastInstruction(const Constant *V, |
| 524 | const Type *DestTy) { |
| 525 | if (V->getType() == DestTy) return (Constant*)V; |
| 526 | |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 527 | // Cast of a global address to boolean is always true. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 528 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 529 | if (DestTy == Type::BoolTy) |
| 530 | // FIXME: When we support 'external weak' references, we have to prevent |
| 531 | // this transformation from happening. In the meantime we avoid folding |
| 532 | // any cast of an external symbol. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 533 | if (!GV->isExternal()) |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 534 | return ConstantBool::True; |
| 535 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 536 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 537 | if (CE->getOpcode() == Instruction::Cast) { |
| 538 | Constant *Op = const_cast<Constant*>(CE->getOperand(0)); |
| 539 | // Try to not produce a cast of a cast, which is almost always redundant. |
| 540 | if (!Op->getType()->isFloatingPoint() && |
| 541 | !CE->getType()->isFloatingPoint() && |
Reid Spencer | 8eb06df | 2004-05-30 01:19:48 +0000 | [diff] [blame] | 542 | !DestTy->isFloatingPoint()) { |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 543 | unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType()); |
| 544 | unsigned S3 = getSize(DestTy); |
| 545 | if (Op->getType() == DestTy && S3 >= S2) |
| 546 | return Op; |
| 547 | if (S1 >= S2 && S2 >= S3) |
| 548 | return ConstantExpr::getCast(Op, DestTy); |
| 549 | if (S1 <= S2 && S2 >= S3 && S1 <= S3) |
| 550 | return ConstantExpr::getCast(Op, DestTy); |
| 551 | } |
| 552 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 553 | // If all of the indexes in the GEP are null values, there is no pointer |
| 554 | // adjustment going on. We might as well cast the source pointer. |
| 555 | bool isAllNull = true; |
| 556 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 557 | if (!CE->getOperand(i)->isNullValue()) { |
| 558 | isAllNull = false; |
| 559 | break; |
| 560 | } |
| 561 | if (isAllNull) |
| 562 | return ConstantExpr::getCast(CE->getOperand(0), DestTy); |
| 563 | } |
| 564 | |
Chris Lattner | b2b7f90 | 2004-10-11 03:57:30 +0000 | [diff] [blame^] | 565 | // Check to see if we are casting an array of X to a pointer to X. If so, use |
| 566 | // a GEP to get to the first element of the array instead of a cast! |
| 567 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) |
| 568 | if (const ArrayType *ATy = dyn_cast<ArrayType>(PTy->getElementType())) |
| 569 | if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) |
| 570 | if (DPTy->getElementType() == ATy->getElementType()) { |
| 571 | std::vector<Constant*> IdxList(2,Constant::getNullValue(Type::IntTy)); |
| 572 | return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V), |
| 573 | IdxList); |
| 574 | } |
| 575 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 576 | ConstRules &Rules = ConstRules::get(V, V); |
| 577 | |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 578 | switch (DestTy->getTypeID()) { |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 579 | case Type::BoolTyID: return Rules.castToBool(V); |
| 580 | case Type::UByteTyID: return Rules.castToUByte(V); |
| 581 | case Type::SByteTyID: return Rules.castToSByte(V); |
| 582 | case Type::UShortTyID: return Rules.castToUShort(V); |
| 583 | case Type::ShortTyID: return Rules.castToShort(V); |
| 584 | case Type::UIntTyID: return Rules.castToUInt(V); |
| 585 | case Type::IntTyID: return Rules.castToInt(V); |
| 586 | case Type::ULongTyID: return Rules.castToULong(V); |
| 587 | case Type::LongTyID: return Rules.castToLong(V); |
| 588 | case Type::FloatTyID: return Rules.castToFloat(V); |
| 589 | case Type::DoubleTyID: return Rules.castToDouble(V); |
| 590 | case Type::PointerTyID: |
| 591 | return Rules.castToPointer(V, cast<PointerType>(DestTy)); |
| 592 | default: return 0; |
| 593 | } |
| 594 | } |
| 595 | |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 596 | Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, |
| 597 | const Constant *V1, |
| 598 | const Constant *V2) { |
| 599 | if (Cond == ConstantBool::True) |
| 600 | return const_cast<Constant*>(V1); |
| 601 | else if (Cond == ConstantBool::False) |
| 602 | return const_cast<Constant*>(V2); |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 607 | /// IdxCompare - Compare the two constants as though they were getelementptr |
| 608 | /// indices. This allows coersion of the types to be the same thing. |
| 609 | /// |
| 610 | /// If the two constants are the "same" (after coersion), return 0. If the |
| 611 | /// first is less than the second, return -1, if the second is less than the |
| 612 | /// first, return 1. If the constants are not integral, return -2. |
| 613 | /// |
| 614 | static int IdxCompare(Constant *C1, Constant *C2) { |
| 615 | if (C1 == C2) return 0; |
| 616 | |
| 617 | // Ok, we found a different index. Are either of the operands |
| 618 | // ConstantExprs? If so, we can't do anything with them. |
| 619 | if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) |
| 620 | return -2; // don't know! |
| 621 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 622 | // Ok, we have two differing integer indices. Sign extend them to be the same |
| 623 | // type. Long is always big enough, so we use it. |
| 624 | C1 = ConstantExpr::getSignExtend(C1, Type::LongTy); |
| 625 | C2 = ConstantExpr::getSignExtend(C2, Type::LongTy); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 626 | if (C1 == C2) return 0; // Are they just differing types? |
| 627 | |
| 628 | // If they are really different, now that they are the same type, then we |
| 629 | // found a difference! |
| 630 | if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue()) |
| 631 | return -1; |
| 632 | else |
| 633 | return 1; |
| 634 | } |
| 635 | |
| 636 | /// evaluateRelation - This function determines if there is anything we can |
| 637 | /// decide about the two constants provided. This doesn't need to handle simple |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 638 | /// things like integer comparisons, but should instead handle ConstantExprs |
| 639 | /// and GlobalValuess. If we can determine that the two constants have a |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 640 | /// particular relation to each other, we should return the corresponding SetCC |
| 641 | /// code, otherwise return Instruction::BinaryOpsEnd. |
| 642 | /// |
| 643 | /// To simplify this code we canonicalize the relation so that the first |
| 644 | /// operand is always the most "complex" of the two. We consider simple |
| 645 | /// constants (like ConstantInt) to be the simplest, followed by |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 646 | /// GlobalValues, followed by ConstantExpr's (the most complex). |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 647 | /// |
| 648 | static Instruction::BinaryOps evaluateRelation(const Constant *V1, |
| 649 | const Constant *V2) { |
| 650 | assert(V1->getType() == V2->getType() && |
| 651 | "Cannot compare different types of values!"); |
| 652 | if (V1 == V2) return Instruction::SetEQ; |
| 653 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 654 | if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 655 | // If the first operand is simple, swap operands. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 656 | assert((isa<GlobalValue>(V2) || isa<ConstantExpr>(V2)) && |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 657 | "Simple cases should have been handled by caller!"); |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 658 | Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); |
| 659 | if (SwappedRelation != Instruction::BinaryOpsEnd) |
| 660 | return SetCondInst::getSwappedCondition(SwappedRelation); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 661 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 662 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)){ |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 663 | if (isa<ConstantExpr>(V2)) { // Swap as necessary. |
| 664 | Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); |
| 665 | if (SwappedRelation != Instruction::BinaryOpsEnd) |
| 666 | return SetCondInst::getSwappedCondition(SwappedRelation); |
| 667 | else |
| 668 | return Instruction::BinaryOpsEnd; |
| 669 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 670 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 671 | // Now we know that the RHS is a GlobalValue or simple constant, |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 672 | // which (since the types must match) means that it's a ConstantPointerNull. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 673 | if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
| 674 | assert(CPR1 != CPR2 && |
| 675 | "GVs for the same value exist at different addresses??"); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 676 | // FIXME: If both globals are external weak, they might both be null! |
| 677 | return Instruction::SetNE; |
| 678 | } else { |
| 679 | assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); |
| 680 | // Global can never be null. FIXME: if we implement external weak |
| 681 | // linkage, this is not necessarily true! |
| 682 | return Instruction::SetNE; |
| 683 | } |
| 684 | |
| 685 | } else { |
| 686 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 687 | // constantexpr, a CPR, or a simple constant. |
| 688 | const ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
| 689 | Constant *CE1Op0 = CE1->getOperand(0); |
| 690 | |
| 691 | switch (CE1->getOpcode()) { |
| 692 | case Instruction::Cast: |
| 693 | // If the cast is not actually changing bits, and the second operand is a |
| 694 | // null pointer, do the comparison with the pre-casted value. |
| 695 | if (V2->isNullValue() && |
| 696 | CE1->getType()->isLosslesslyConvertibleTo(CE1Op0->getType())) |
| 697 | return evaluateRelation(CE1Op0, |
| 698 | Constant::getNullValue(CE1Op0->getType())); |
Chris Lattner | 192e326 | 2004-04-11 01:29:30 +0000 | [diff] [blame] | 699 | break; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 700 | |
| 701 | case Instruction::GetElementPtr: |
| 702 | // Ok, since this is a getelementptr, we know that the constant has a |
| 703 | // pointer type. Check the various cases. |
| 704 | if (isa<ConstantPointerNull>(V2)) { |
| 705 | // If we are comparing a GEP to a null pointer, check to see if the base |
| 706 | // of the GEP equals the null pointer. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 707 | if (isa<GlobalValue>(CE1Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 708 | // FIXME: this is not true when we have external weak references! |
| 709 | // No offset can go from a global to a null pointer. |
| 710 | return Instruction::SetGT; |
| 711 | } else if (isa<ConstantPointerNull>(CE1Op0)) { |
| 712 | // If we are indexing from a null pointer, check to see if we have any |
| 713 | // non-zero indices. |
| 714 | for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) |
| 715 | if (!CE1->getOperand(i)->isNullValue()) |
| 716 | // Offsetting from null, must not be equal. |
| 717 | return Instruction::SetGT; |
| 718 | // Only zero indexes from null, must still be zero. |
| 719 | return Instruction::SetEQ; |
| 720 | } |
| 721 | // Otherwise, we can't really say if the first operand is null or not. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 722 | } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 723 | if (isa<ConstantPointerNull>(CE1Op0)) { |
| 724 | // FIXME: This is not true with external weak references. |
| 725 | return Instruction::SetLT; |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 726 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 727 | if (CPR1 == CPR2) { |
| 728 | // If this is a getelementptr of the same global, then it must be |
| 729 | // different. Because the types must match, the getelementptr could |
| 730 | // only have at most one index, and because we fold getelementptr's |
| 731 | // with a single zero index, it must be nonzero. |
| 732 | assert(CE1->getNumOperands() == 2 && |
| 733 | !CE1->getOperand(1)->isNullValue() && |
| 734 | "Suprising getelementptr!"); |
| 735 | return Instruction::SetGT; |
| 736 | } else { |
| 737 | // If they are different globals, we don't know what the value is, |
| 738 | // but they can't be equal. |
| 739 | return Instruction::SetNE; |
| 740 | } |
| 741 | } |
| 742 | } else { |
| 743 | const ConstantExpr *CE2 = cast<ConstantExpr>(V2); |
| 744 | const Constant *CE2Op0 = CE2->getOperand(0); |
| 745 | |
| 746 | // There are MANY other foldings that we could perform here. They will |
| 747 | // probably be added on demand, as they seem needed. |
| 748 | switch (CE2->getOpcode()) { |
| 749 | default: break; |
| 750 | case Instruction::GetElementPtr: |
| 751 | // By far the most common case to handle is when the base pointers are |
| 752 | // obviously to the same or different globals. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 753 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 754 | if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal |
| 755 | return Instruction::SetNE; |
| 756 | // Ok, we know that both getelementptr instructions are based on the |
| 757 | // same global. From this, we can precisely determine the relative |
| 758 | // ordering of the resultant pointers. |
| 759 | unsigned i = 1; |
| 760 | |
| 761 | // Compare all of the operands the GEP's have in common. |
| 762 | for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); ++i) |
| 763 | switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i))) { |
| 764 | case -1: return Instruction::SetLT; |
| 765 | case 1: return Instruction::SetGT; |
| 766 | case -2: return Instruction::BinaryOpsEnd; |
| 767 | } |
| 768 | |
| 769 | // Ok, we ran out of things they have in common. If any leftovers |
| 770 | // are non-zero then we have a difference, otherwise we are equal. |
| 771 | for (; i < CE1->getNumOperands(); ++i) |
| 772 | if (!CE1->getOperand(i)->isNullValue()) |
| 773 | return Instruction::SetGT; |
| 774 | for (; i < CE2->getNumOperands(); ++i) |
| 775 | if (!CE2->getOperand(i)->isNullValue()) |
| 776 | return Instruction::SetLT; |
| 777 | return Instruction::SetEQ; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | default: |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | return Instruction::BinaryOpsEnd; |
| 788 | } |
| 789 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 790 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 791 | const Constant *V1, |
| 792 | const Constant *V2) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 793 | Constant *C = 0; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 794 | switch (Opcode) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 795 | default: break; |
| 796 | case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break; |
| 797 | case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break; |
| 798 | case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break; |
| 799 | case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break; |
| 800 | case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break; |
| 801 | case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break; |
| 802 | case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break; |
| 803 | case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break; |
| 804 | case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break; |
| 805 | case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break; |
| 806 | case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break; |
| 807 | case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break; |
| 808 | case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 809 | case Instruction::SetNE: // V1 != V2 === !(V1 == V2) |
| 810 | C = ConstRules::get(V1, V2).equalto(V1, V2); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 811 | if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 812 | break; |
| 813 | case Instruction::SetLE: // V1 <= V2 === !(V2 < V1) |
| 814 | C = ConstRules::get(V1, V2).lessthan(V2, V1); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 815 | if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 816 | break; |
| 817 | case Instruction::SetGE: // V1 >= V2 === !(V1 < V2) |
| 818 | C = ConstRules::get(V1, V2).lessthan(V1, V2); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 819 | if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 820 | break; |
| 821 | } |
| 822 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 823 | // If we successfully folded the expression, return it now. |
| 824 | if (C) return C; |
| 825 | |
| 826 | if (SetCondInst::isRelational(Opcode)) |
| 827 | switch (evaluateRelation(V1, V2)) { |
| 828 | default: assert(0 && "Unknown relational!"); |
| 829 | case Instruction::BinaryOpsEnd: |
| 830 | break; // Couldn't determine anything about these constants. |
| 831 | case Instruction::SetEQ: // We know the constants are equal! |
| 832 | // If we know the constants are equal, we can decide the result of this |
| 833 | // computation precisely. |
| 834 | return ConstantBool::get(Opcode == Instruction::SetEQ || |
| 835 | Opcode == Instruction::SetLE || |
| 836 | Opcode == Instruction::SetGE); |
| 837 | case Instruction::SetLT: |
| 838 | // If we know that V1 < V2, we can decide the result of this computation |
| 839 | // precisely. |
| 840 | return ConstantBool::get(Opcode == Instruction::SetLT || |
| 841 | Opcode == Instruction::SetNE || |
| 842 | Opcode == Instruction::SetLE); |
| 843 | case Instruction::SetGT: |
| 844 | // If we know that V1 > V2, we can decide the result of this computation |
| 845 | // precisely. |
| 846 | return ConstantBool::get(Opcode == Instruction::SetGT || |
| 847 | Opcode == Instruction::SetNE || |
| 848 | Opcode == Instruction::SetGE); |
| 849 | case Instruction::SetLE: |
| 850 | // If we know that V1 <= V2, we can only partially decide this relation. |
| 851 | if (Opcode == Instruction::SetGT) return ConstantBool::False; |
| 852 | if (Opcode == Instruction::SetLT) return ConstantBool::True; |
| 853 | break; |
| 854 | |
| 855 | case Instruction::SetGE: |
| 856 | // If we know that V1 >= V2, we can only partially decide this relation. |
| 857 | if (Opcode == Instruction::SetLT) return ConstantBool::False; |
| 858 | if (Opcode == Instruction::SetGT) return ConstantBool::True; |
| 859 | break; |
| 860 | |
| 861 | case Instruction::SetNE: |
| 862 | // If we know that V1 != V2, we can only partially decide this relation. |
| 863 | if (Opcode == Instruction::SetEQ) return ConstantBool::False; |
| 864 | if (Opcode == Instruction::SetNE) return ConstantBool::True; |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) { |
| 869 | if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) { |
| 870 | // There are many possible foldings we could do here. We should probably |
| 871 | // at least fold add of a pointer with an integer into the appropriate |
| 872 | // getelementptr. This will improve alias analysis a bit. |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | } else { |
| 878 | // Just implement a couple of simple identities. |
| 879 | switch (Opcode) { |
| 880 | case Instruction::Add: |
| 881 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X |
| 882 | break; |
| 883 | case Instruction::Sub: |
| 884 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X |
| 885 | break; |
| 886 | case Instruction::Mul: |
| 887 | if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0 |
| 888 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
| 889 | if (CI->getRawValue() == 1) |
| 890 | return const_cast<Constant*>(V1); // X * 1 == X |
| 891 | break; |
| 892 | case Instruction::Div: |
| 893 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
| 894 | if (CI->getRawValue() == 1) |
| 895 | return const_cast<Constant*>(V1); // X / 1 == X |
| 896 | break; |
| 897 | case Instruction::Rem: |
| 898 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
| 899 | if (CI->getRawValue() == 1) |
| 900 | return Constant::getNullValue(CI->getType()); // X % 1 == 0 |
| 901 | break; |
| 902 | case Instruction::And: |
| 903 | if (cast<ConstantIntegral>(V2)->isAllOnesValue()) |
| 904 | return const_cast<Constant*>(V1); // X & -1 == X |
| 905 | if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0 |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 906 | if (CE1->getOpcode() == Instruction::Cast && |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 907 | isa<GlobalValue>(CE1->getOperand(0))) { |
| 908 | GlobalValue *CPR =cast<GlobalValue>(CE1->getOperand(0)); |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 909 | |
| 910 | // Functions are at least 4-byte aligned. If and'ing the address of a |
| 911 | // function with a constant < 4, fold it to zero. |
| 912 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 913 | if (CI->getRawValue() < 4 && isa<Function>(CPR)) |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 914 | return Constant::getNullValue(CI->getType()); |
| 915 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 916 | break; |
| 917 | case Instruction::Or: |
| 918 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X |
| 919 | if (cast<ConstantIntegral>(V2)->isAllOnesValue()) |
| 920 | return const_cast<Constant*>(V2); // X | -1 == -1 |
| 921 | break; |
| 922 | case Instruction::Xor: |
| 923 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) { |
| 929 | // If V2 is a constant expr and V1 isn't, flop them around and fold the |
| 930 | // other way if possible. |
| 931 | switch (Opcode) { |
| 932 | case Instruction::Add: |
| 933 | case Instruction::Mul: |
| 934 | case Instruction::And: |
| 935 | case Instruction::Or: |
| 936 | case Instruction::Xor: |
| 937 | case Instruction::SetEQ: |
| 938 | case Instruction::SetNE: |
| 939 | // No change of opcode required. |
| 940 | return ConstantFoldBinaryInstruction(Opcode, V2, V1); |
| 941 | |
| 942 | case Instruction::SetLT: |
| 943 | case Instruction::SetGT: |
| 944 | case Instruction::SetLE: |
| 945 | case Instruction::SetGE: |
| 946 | // Change the opcode as necessary to swap the operands. |
| 947 | Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode); |
| 948 | return ConstantFoldBinaryInstruction(Opcode, V2, V1); |
| 949 | |
| 950 | case Instruction::Shl: |
| 951 | case Instruction::Shr: |
| 952 | case Instruction::Sub: |
| 953 | case Instruction::Div: |
| 954 | case Instruction::Rem: |
| 955 | default: // These instructions cannot be flopped around. |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | return 0; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
| 963 | const std::vector<Constant*> &IdxList) { |
| 964 | if (IdxList.size() == 0 || |
| 965 | (IdxList.size() == 1 && IdxList[0]->isNullValue())) |
| 966 | return const_cast<Constant*>(C); |
| 967 | |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 968 | if (C->isNullValue()) { |
| 969 | bool isNull = true; |
| 970 | for (unsigned i = 0, e = IdxList.size(); i != e; ++i) |
| 971 | if (!IdxList[i]->isNullValue()) { |
| 972 | isNull = false; |
| 973 | break; |
| 974 | } |
| 975 | if (isNull) { |
| 976 | std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); |
| 977 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, |
| 978 | true); |
| 979 | assert(Ty != 0 && "Invalid indices for GEP!"); |
| 980 | return ConstantPointerNull::get(PointerType::get(Ty)); |
| 981 | } |
Chris Lattner | 4bbd409 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 982 | |
| 983 | if (IdxList.size() == 1) { |
| 984 | const Type *ElTy = cast<PointerType>(C->getType())->getElementType(); |
| 985 | if (unsigned ElSize = ElTy->getPrimitiveSize()) { |
| 986 | // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm |
| 987 | // type, we can statically fold this. |
| 988 | Constant *R = ConstantUInt::get(Type::UIntTy, ElSize); |
| 989 | R = ConstantExpr::getCast(R, IdxList[0]->getType()); |
| 990 | R = ConstantExpr::getMul(R, IdxList[0]); |
| 991 | return ConstantExpr::getCast(R, C->getType()); |
| 992 | } |
| 993 | } |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 994 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 995 | |
| 996 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
| 997 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 998 | // is a getelementptr instruction, combine the indices of the two |
| 999 | // getelementptr instructions into a single instruction. |
| 1000 | // |
| 1001 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1002 | const Type *LastTy = 0; |
| 1003 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 1004 | I != E; ++I) |
| 1005 | LastTy = *I; |
| 1006 | |
| 1007 | if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) { |
| 1008 | std::vector<Constant*> NewIndices; |
| 1009 | NewIndices.reserve(IdxList.size() + CE->getNumOperands()); |
| 1010 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
| 1011 | NewIndices.push_back(cast<Constant>(CE->getOperand(i))); |
| 1012 | |
| 1013 | // Add the last index of the source with the first index of the new GEP. |
| 1014 | // Make sure to handle the case when they are actually different types. |
| 1015 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1016 | if (!IdxList[0]->isNullValue()) { // Otherwise it must be an array |
| 1017 | const Type *IdxTy = Combined->getType(); |
| 1018 | if (IdxTy != IdxList[0]->getType()) IdxTy = Type::LongTy; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1019 | Combined = |
| 1020 | ConstantExpr::get(Instruction::Add, |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1021 | ConstantExpr::getCast(IdxList[0], IdxTy), |
| 1022 | ConstantExpr::getCast(Combined, IdxTy)); |
| 1023 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1024 | |
| 1025 | NewIndices.push_back(Combined); |
| 1026 | NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); |
| 1027 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | // Implement folding of: |
| 1032 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 1033 | // long 0, long 0) |
| 1034 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 1035 | // |
| 1036 | if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 && |
| 1037 | IdxList[0]->isNullValue()) |
| 1038 | if (const PointerType *SPT = |
| 1039 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 1040 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 1041 | if (const ArrayType *CAT = |
| 1042 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
| 1043 | if (CAT->getElementType() == SAT->getElementType()) |
| 1044 | return ConstantExpr::getGetElementPtr( |
| 1045 | (Constant*)CE->getOperand(0), IdxList); |
| 1046 | } |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |