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