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