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