Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 1 | //===- ConstantFolding.cpp - LLVM constant folder -------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | cbfd406 | 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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | eab20b5 | 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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 21 | #include "ConstantFolding.h" |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | b228f9c | 2004-02-22 06:25:38 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Chris Lattner | 27287de | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 802b5ab | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 25 | #include "llvm/Function.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 0eff5ad | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 28 | #include "llvm/Support/ManagedStatic.h" |
| 29 | #include "llvm/Support/MathExtras.h" |
Jeff Cohen | a97e8db | 2005-05-03 03:13:01 +0000 | [diff] [blame] | 30 | #include <limits> |
Chris Lattner | 0dc3969 | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Chris Lattner | 14712a6 | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 32 | |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 33 | namespace { |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 34 | struct VISIBILITY_HIDDEN ConstRules { |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 35 | ConstRules() {} |
Reid Spencer | 2b2a528 | 2005-04-24 22:27:20 +0000 | [diff] [blame] | 36 | virtual ~ConstRules() {} |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 37 | |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 38 | // Binary Operators... |
| 39 | virtual Constant *add(const Constant *V1, const Constant *V2) const = 0; |
| 40 | virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0; |
| 41 | virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 42 | virtual Constant *urem(const Constant *V1, const Constant *V2) const = 0; |
| 43 | virtual Constant *srem(const Constant *V1, const Constant *V2) const = 0; |
| 44 | virtual Constant *frem(const Constant *V1, const Constant *V2) const = 0; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 45 | virtual Constant *udiv(const Constant *V1, const Constant *V2) const = 0; |
| 46 | virtual Constant *sdiv(const Constant *V1, const Constant *V2) const = 0; |
| 47 | virtual Constant *fdiv(const Constant *V1, const Constant *V2) const = 0; |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 48 | virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0; |
| 49 | virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0; |
| 50 | virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0; |
| 51 | virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 52 | virtual Constant *lshr(const Constant *V1, const Constant *V2) const = 0; |
| 53 | virtual Constant *ashr(const Constant *V1, const Constant *V2) const = 0; |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 54 | virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0; |
| 55 | virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0; |
| 56 | |
| 57 | // Casting operators. |
| 58 | virtual Constant *castToBool (const Constant *V) const = 0; |
| 59 | virtual Constant *castToSByte (const Constant *V) const = 0; |
| 60 | virtual Constant *castToUByte (const Constant *V) const = 0; |
| 61 | virtual Constant *castToShort (const Constant *V) const = 0; |
| 62 | virtual Constant *castToUShort(const Constant *V) const = 0; |
| 63 | virtual Constant *castToInt (const Constant *V) const = 0; |
| 64 | virtual Constant *castToUInt (const Constant *V) const = 0; |
| 65 | virtual Constant *castToLong (const Constant *V) const = 0; |
| 66 | virtual Constant *castToULong (const Constant *V) const = 0; |
| 67 | virtual Constant *castToFloat (const Constant *V) const = 0; |
| 68 | virtual Constant *castToDouble(const Constant *V) const = 0; |
| 69 | virtual Constant *castToPointer(const Constant *V, |
| 70 | const PointerType *Ty) const = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 71 | |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 72 | // ConstRules::get - Return an instance of ConstRules for the specified |
| 73 | // constant operands. |
| 74 | // |
| 75 | static ConstRules &get(const Constant *V1, const Constant *V2); |
| 76 | private: |
| 77 | ConstRules(const ConstRules &); // Do not implement |
| 78 | ConstRules &operator=(const ConstRules &); // Do not implement |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
| 84 | // TemplateRules Class |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 87 | // TemplateRules - Implement a subclass of ConstRules that provides all |
| 88 | // operations as noops. All other rules classes inherit from this class so |
| 89 | // that if functionality is needed in the future, it can simply be added here |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 90 | // and to ConstRules without changing anything else... |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 91 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 92 | // This class also provides subclasses with typesafe implementations of methods |
| 93 | // so that don't have to do type casting. |
| 94 | // |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 95 | namespace { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 96 | template<class ArgType, class SubClassName> |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 97 | class VISIBILITY_HIDDEN TemplateRules : public ConstRules { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 98 | |
Reid Spencer | 2b2a528 | 2005-04-24 22:27:20 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 100 | //===--------------------------------------------------------------------===// |
| 101 | // Redirecting functions that cast to the appropriate types |
| 102 | //===--------------------------------------------------------------------===// |
| 103 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 104 | virtual Constant *add(const Constant *V1, const Constant *V2) const { |
| 105 | return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 106 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 107 | virtual Constant *sub(const Constant *V1, const Constant *V2) const { |
| 108 | return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 109 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 110 | virtual Constant *mul(const Constant *V1, const Constant *V2) const { |
| 111 | return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | b5bcbc3 | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 112 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 113 | virtual Constant *udiv(const Constant *V1, const Constant *V2) const { |
| 114 | return SubClassName::UDiv((const ArgType *)V1, (const ArgType *)V2); |
| 115 | } |
| 116 | virtual Constant *sdiv(const Constant *V1, const Constant *V2) const { |
| 117 | return SubClassName::SDiv((const ArgType *)V1, (const ArgType *)V2); |
| 118 | } |
| 119 | virtual Constant *fdiv(const Constant *V1, const Constant *V2) const { |
| 120 | return SubClassName::FDiv((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 05c05ea | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 121 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 122 | virtual Constant *urem(const Constant *V1, const Constant *V2) const { |
| 123 | return SubClassName::URem((const ArgType *)V1, (const ArgType *)V2); |
| 124 | } |
| 125 | virtual Constant *srem(const Constant *V1, const Constant *V2) const { |
| 126 | return SubClassName::SRem((const ArgType *)V1, (const ArgType *)V2); |
| 127 | } |
| 128 | virtual Constant *frem(const Constant *V1, const Constant *V2) const { |
| 129 | return SubClassName::FRem((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 44540dd | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 130 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 131 | virtual Constant *op_and(const Constant *V1, const Constant *V2) const { |
| 132 | return SubClassName::And((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 133 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 134 | virtual Constant *op_or(const Constant *V1, const Constant *V2) const { |
| 135 | return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 136 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 137 | virtual Constant *op_xor(const Constant *V1, const Constant *V2) const { |
| 138 | return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 139 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 140 | virtual Constant *shl(const Constant *V1, const Constant *V2) const { |
| 141 | return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | cf4929f | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 142 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 143 | virtual Constant *lshr(const Constant *V1, const Constant *V2) const { |
| 144 | return SubClassName::LShr((const ArgType *)V1, (const ArgType *)V2); |
| 145 | } |
| 146 | virtual Constant *ashr(const Constant *V1, const Constant *V2) const { |
| 147 | return SubClassName::AShr((const ArgType *)V1, (const ArgType *)V2); |
Chris Lattner | cf4929f | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | b5bcbc3 | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 149 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 150 | virtual Constant *lessthan(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 151 | return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2); |
| 152 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 153 | virtual Constant *equalto(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 5d5a714 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 154 | return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2); |
| 155 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 156 | |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 157 | // Casting operators. ick |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 158 | virtual Constant *castToBool(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 159 | return SubClassName::CastToBool((const ArgType*)V); |
| 160 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 161 | virtual Constant *castToSByte(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 162 | return SubClassName::CastToSByte((const ArgType*)V); |
| 163 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 164 | virtual Constant *castToUByte(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 165 | return SubClassName::CastToUByte((const ArgType*)V); |
| 166 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 167 | virtual Constant *castToShort(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 168 | return SubClassName::CastToShort((const ArgType*)V); |
| 169 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 170 | virtual Constant *castToUShort(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 171 | return SubClassName::CastToUShort((const ArgType*)V); |
| 172 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 173 | virtual Constant *castToInt(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 174 | return SubClassName::CastToInt((const ArgType*)V); |
| 175 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 176 | virtual Constant *castToUInt(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 177 | return SubClassName::CastToUInt((const ArgType*)V); |
| 178 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 179 | virtual Constant *castToLong(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 180 | return SubClassName::CastToLong((const ArgType*)V); |
| 181 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 182 | virtual Constant *castToULong(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 183 | return SubClassName::CastToULong((const ArgType*)V); |
| 184 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 185 | virtual Constant *castToFloat(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 186 | return SubClassName::CastToFloat((const ArgType*)V); |
| 187 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 188 | virtual Constant *castToDouble(const Constant *V) const { |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 189 | return SubClassName::CastToDouble((const ArgType*)V); |
| 190 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 191 | virtual Constant *castToPointer(const Constant *V, |
Chris Lattner | 27287de | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 192 | const PointerType *Ty) const { |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 193 | return SubClassName::CastToPointer((const ArgType*)V, Ty); |
| 194 | } |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 196 | //===--------------------------------------------------------------------===// |
| 197 | // Default "noop" implementations |
| 198 | //===--------------------------------------------------------------------===// |
| 199 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 200 | static Constant *Add (const ArgType *V1, const ArgType *V2) { return 0; } |
| 201 | static Constant *Sub (const ArgType *V1, const ArgType *V2) { return 0; } |
| 202 | static Constant *Mul (const ArgType *V1, const ArgType *V2) { return 0; } |
| 203 | static Constant *SDiv(const ArgType *V1, const ArgType *V2) { return 0; } |
| 204 | static Constant *UDiv(const ArgType *V1, const ArgType *V2) { return 0; } |
| 205 | static Constant *FDiv(const ArgType *V1, const ArgType *V2) { return 0; } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 206 | static Constant *URem(const ArgType *V1, const ArgType *V2) { return 0; } |
| 207 | static Constant *SRem(const ArgType *V1, const ArgType *V2) { return 0; } |
| 208 | static Constant *FRem(const ArgType *V1, const ArgType *V2) { return 0; } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 209 | static Constant *And (const ArgType *V1, const ArgType *V2) { return 0; } |
| 210 | static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; } |
| 211 | static Constant *Xor (const ArgType *V1, const ArgType *V2) { return 0; } |
| 212 | static Constant *Shl (const ArgType *V1, const ArgType *V2) { return 0; } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 213 | static Constant *LShr(const ArgType *V1, const ArgType *V2) { return 0; } |
| 214 | static Constant *AShr(const ArgType *V1, const ArgType *V2) { return 0; } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 215 | static Constant *LessThan(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 218 | static Constant *EqualTo(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 5d5a714 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 219 | return 0; |
| 220 | } |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 221 | |
| 222 | // Casting operators. ick |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 223 | static Constant *CastToBool (const Constant *V) { return 0; } |
| 224 | static Constant *CastToSByte (const Constant *V) { return 0; } |
| 225 | static Constant *CastToUByte (const Constant *V) { return 0; } |
| 226 | static Constant *CastToShort (const Constant *V) { return 0; } |
| 227 | static Constant *CastToUShort(const Constant *V) { return 0; } |
| 228 | static Constant *CastToInt (const Constant *V) { return 0; } |
| 229 | static Constant *CastToUInt (const Constant *V) { return 0; } |
| 230 | static Constant *CastToLong (const Constant *V) { return 0; } |
| 231 | static Constant *CastToULong (const Constant *V) { return 0; } |
| 232 | static Constant *CastToFloat (const Constant *V) { return 0; } |
| 233 | static Constant *CastToDouble(const Constant *V) { return 0; } |
| 234 | static Constant *CastToPointer(const Constant *, |
| 235 | const PointerType *) {return 0;} |
Reid Spencer | 2b2a528 | 2005-04-24 22:27:20 +0000 | [diff] [blame] | 236 | |
| 237 | public: |
| 238 | virtual ~TemplateRules() {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 239 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 240 | } // end anonymous namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 241 | |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
| 244 | // EmptyRules Class |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | // |
| 247 | // EmptyRules provides a concrete base class of ConstRules that does nothing |
| 248 | // |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 249 | namespace { |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 250 | struct VISIBILITY_HIDDEN EmptyRules |
| 251 | : public TemplateRules<Constant, EmptyRules> { |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 252 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 253 | if (V1 == V2) return ConstantBool::getTrue(); |
Chris Lattner | 5d5a714 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
Chris Lattner | 14712a6 | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 256 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 257 | } // end anonymous namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | |
| 261 | //===----------------------------------------------------------------------===// |
| 262 | // BoolRules Class |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | // |
| 265 | // BoolRules provides a concrete base class of ConstRules for the 'bool' type. |
| 266 | // |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 267 | namespace { |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 268 | struct VISIBILITY_HIDDEN BoolRules |
| 269 | : public TemplateRules<ConstantBool, BoolRules> { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 2c822cc | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 271 | static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) { |
Chris Lattner | 252030e | 2002-09-03 20:09:49 +0000 | [diff] [blame] | 272 | return ConstantBool::get(V1->getValue() < V2->getValue()); |
| 273 | } |
| 274 | |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 275 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | 5d5a714 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 276 | return ConstantBool::get(V1 == V2); |
| 277 | } |
| 278 | |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 279 | static Constant *And(const ConstantBool *V1, const ConstantBool *V2) { |
| 280 | return ConstantBool::get(V1->getValue() & V2->getValue()); |
| 281 | } |
| 282 | |
| 283 | static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 284 | return ConstantBool::get(V1->getValue() | V2->getValue()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 287 | static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) { |
| 288 | return ConstantBool::get(V1->getValue() ^ V2->getValue()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 289 | } |
Chris Lattner | 3ccd0fd | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 290 | |
| 291 | // Casting operators. ick |
| 292 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 293 | static Constant *CastTo##TYPE (const ConstantBool *V) { \ |
Chris Lattner | 3ccd0fd | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 294 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \ |
| 295 | } |
| 296 | |
| 297 | DEF_CAST(Bool , ConstantBool, bool) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 298 | DEF_CAST(SByte , ConstantInt, signed char) |
| 299 | DEF_CAST(UByte , ConstantInt, unsigned char) |
| 300 | DEF_CAST(Short , ConstantInt, signed short) |
| 301 | DEF_CAST(UShort, ConstantInt, unsigned short) |
| 302 | DEF_CAST(Int , ConstantInt, signed int) |
| 303 | DEF_CAST(UInt , ConstantInt, unsigned int) |
| 304 | DEF_CAST(Long , ConstantInt, int64_t) |
| 305 | DEF_CAST(ULong , ConstantInt, uint64_t) |
Chris Lattner | 3ccd0fd | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 306 | DEF_CAST(Float , ConstantFP , float) |
| 307 | DEF_CAST(Double, ConstantFP , double) |
| 308 | #undef DEF_CAST |
Chris Lattner | 14712a6 | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 309 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 310 | } // end anonymous namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | |
| 312 | |
| 313 | //===----------------------------------------------------------------------===// |
Chris Lattner | cf09f51 | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 314 | // NullPointerRules Class |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 315 | //===----------------------------------------------------------------------===// |
| 316 | // |
Chris Lattner | cf09f51 | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 317 | // NullPointerRules provides a concrete base class of ConstRules for null |
| 318 | // pointers. |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 319 | // |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 320 | namespace { |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 321 | struct VISIBILITY_HIDDEN NullPointerRules |
| 322 | : public TemplateRules<ConstantPointerNull, NullPointerRules> { |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 323 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 324 | return ConstantBool::getTrue(); // Null pointers are always equal |
Chris Lattner | 5d5a714 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 325 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 326 | static Constant *CastToBool(const Constant *V) { |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 327 | return ConstantBool::getFalse(); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 328 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 329 | static Constant *CastToSByte (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 330 | return ConstantInt::get(Type::SByteTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 332 | static Constant *CastToUByte (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 333 | return ConstantInt::get(Type::UByteTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 335 | static Constant *CastToShort (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 336 | return ConstantInt::get(Type::ShortTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 337 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 338 | static Constant *CastToUShort(const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 339 | return ConstantInt::get(Type::UShortTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 340 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 341 | static Constant *CastToInt (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 342 | return ConstantInt::get(Type::IntTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 343 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 344 | static Constant *CastToUInt (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 345 | return ConstantInt::get(Type::UIntTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 346 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 347 | static Constant *CastToLong (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 348 | return ConstantInt::get(Type::LongTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 350 | static Constant *CastToULong (const Constant *V) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 351 | return ConstantInt::get(Type::ULongTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 352 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 353 | static Constant *CastToFloat (const Constant *V) { |
Chris Lattner | cf09f51 | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 354 | return ConstantFP::get(Type::FloatTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 355 | } |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 356 | static Constant *CastToDouble(const Constant *V) { |
Chris Lattner | cf09f51 | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 357 | return ConstantFP::get(Type::DoubleTy, 0); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Chris Lattner | a83a6e4 | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 360 | static Constant *CastToPointer(const ConstantPointerNull *V, |
Chris Lattner | 27287de | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 361 | const PointerType *PTy) { |
Chris Lattner | cf09f51 | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 362 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 363 | } |
| 364 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 365 | } // end anonymous namespace |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 366 | |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 367 | //===----------------------------------------------------------------------===// |
| 368 | // ConstantPackedRules Class |
| 369 | //===----------------------------------------------------------------------===// |
| 370 | |
Chris Lattner | d2a7ea4 | 2006-01-04 02:15:02 +0000 | [diff] [blame] | 371 | /// DoVectorOp - Given two packed constants and a function pointer, apply the |
| 372 | /// function pointer to each element pair, producing a new ConstantPacked |
| 373 | /// constant. |
| 374 | static Constant *EvalVectorOp(const ConstantPacked *V1, |
| 375 | const ConstantPacked *V2, |
| 376 | Constant *(*FP)(Constant*, Constant*)) { |
| 377 | std::vector<Constant*> Res; |
| 378 | for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) |
| 379 | Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)), |
| 380 | const_cast<Constant*>(V2->getOperand(i)))); |
| 381 | return ConstantPacked::get(Res); |
| 382 | } |
| 383 | |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 384 | /// PackedTypeRules provides a concrete base class of ConstRules for |
| 385 | /// ConstantPacked operands. |
| 386 | /// |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 387 | namespace { |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 388 | struct VISIBILITY_HIDDEN ConstantPackedRules |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 389 | : public TemplateRules<ConstantPacked, ConstantPackedRules> { |
Chris Lattner | d2a7ea4 | 2006-01-04 02:15:02 +0000 | [diff] [blame] | 390 | |
| 391 | static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 392 | return EvalVectorOp(V1, V2, ConstantExpr::getAdd); |
| 393 | } |
| 394 | static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 395 | return EvalVectorOp(V1, V2, ConstantExpr::getSub); |
| 396 | } |
| 397 | static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 398 | return EvalVectorOp(V1, V2, ConstantExpr::getMul); |
| 399 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 400 | static Constant *UDiv(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 401 | return EvalVectorOp(V1, V2, ConstantExpr::getUDiv); |
| 402 | } |
| 403 | static Constant *SDiv(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 404 | return EvalVectorOp(V1, V2, ConstantExpr::getSDiv); |
| 405 | } |
| 406 | static Constant *FDiv(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 407 | return EvalVectorOp(V1, V2, ConstantExpr::getFDiv); |
Chris Lattner | d2a7ea4 | 2006-01-04 02:15:02 +0000 | [diff] [blame] | 408 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 409 | static Constant *URem(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 410 | return EvalVectorOp(V1, V2, ConstantExpr::getURem); |
| 411 | } |
| 412 | static Constant *SRem(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 413 | return EvalVectorOp(V1, V2, ConstantExpr::getSRem); |
| 414 | } |
| 415 | static Constant *FRem(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 416 | return EvalVectorOp(V1, V2, ConstantExpr::getFRem); |
Chris Lattner | d2a7ea4 | 2006-01-04 02:15:02 +0000 | [diff] [blame] | 417 | } |
| 418 | static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 419 | return EvalVectorOp(V1, V2, ConstantExpr::getAnd); |
| 420 | } |
| 421 | static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) { |
| 422 | return EvalVectorOp(V1, V2, ConstantExpr::getOr); |
| 423 | } |
| 424 | static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) { |
| 425 | return EvalVectorOp(V1, V2, ConstantExpr::getXor); |
| 426 | } |
Chris Lattner | d2a7ea4 | 2006-01-04 02:15:02 +0000 | [diff] [blame] | 427 | static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){ |
| 428 | return 0; |
| 429 | } |
| 430 | static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) { |
Chris Lattner | 84e1064 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 431 | for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) { |
| 432 | Constant *C = |
| 433 | ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)), |
| 434 | const_cast<Constant*>(V2->getOperand(i))); |
| 435 | if (ConstantBool *CB = dyn_cast<ConstantBool>(C)) |
| 436 | return CB; |
| 437 | } |
| 438 | // Otherwise, could not decide from any element pairs. |
Chris Lattner | d2a7ea4 | 2006-01-04 02:15:02 +0000 | [diff] [blame] | 439 | return 0; |
| 440 | } |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 441 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 442 | } // end anonymous namespace |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 443 | |
| 444 | |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | // GeneralPackedRules Class |
| 447 | //===----------------------------------------------------------------------===// |
| 448 | |
| 449 | /// GeneralPackedRules provides a concrete base class of ConstRules for |
| 450 | /// PackedType operands, where both operands are not ConstantPacked. The usual |
| 451 | /// cause for this is that one operand is a ConstantAggregateZero. |
| 452 | /// |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 453 | namespace { |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 454 | struct VISIBILITY_HIDDEN GeneralPackedRules |
| 455 | : public TemplateRules<Constant, GeneralPackedRules> { |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 456 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 457 | } // end anonymous namespace |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 459 | |
| 460 | //===----------------------------------------------------------------------===// |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 461 | // DirectIntRules Class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 462 | //===----------------------------------------------------------------------===// |
| 463 | // |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 464 | // DirectIntRules provides implementations of functions that are valid on |
| 465 | // integer types, but not all types in general. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 466 | // |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 467 | namespace { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 468 | template <class BuiltinType, Type **Ty> |
| 469 | struct VISIBILITY_HIDDEN DirectIntRules |
| 470 | : public TemplateRules<ConstantInt, DirectIntRules<BuiltinType, Ty> > { |
| 471 | |
| 472 | static Constant *Add(const ConstantInt *V1, const ConstantInt *V2) { |
| 473 | BuiltinType R = (BuiltinType)V1->getZExtValue() + |
| 474 | (BuiltinType)V2->getZExtValue(); |
| 475 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 478 | static Constant *Sub(const ConstantInt *V1, const ConstantInt *V2) { |
| 479 | BuiltinType R = (BuiltinType)V1->getZExtValue() - |
| 480 | (BuiltinType)V2->getZExtValue(); |
| 481 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 484 | static Constant *Mul(const ConstantInt *V1, const ConstantInt *V2) { |
| 485 | BuiltinType R = (BuiltinType)V1->getZExtValue() * |
| 486 | (BuiltinType)V2->getZExtValue(); |
| 487 | return ConstantInt::get(*Ty, R); |
Chris Lattner | b5bcbc3 | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 490 | static Constant *LessThan(const ConstantInt *V1, const ConstantInt *V2) { |
| 491 | bool R = (BuiltinType)V1->getZExtValue() < (BuiltinType)V2->getZExtValue(); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 492 | return ConstantBool::get(R); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 493 | } |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 494 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 495 | static Constant *EqualTo(const ConstantInt *V1, const ConstantInt *V2) { |
| 496 | bool R = (BuiltinType)V1->getZExtValue() == (BuiltinType)V2->getZExtValue(); |
Chris Lattner | 5d5a714 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 497 | return ConstantBool::get(R); |
| 498 | } |
| 499 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 500 | static Constant *CastToPointer(const ConstantInt *V, |
Chris Lattner | 27287de | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 501 | const PointerType *PTy) { |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 502 | if (V->isNullValue()) // Is it a FP or Integral null value? |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 503 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 76ac1a4 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 504 | return 0; // Can't const prop other types of pointers |
| 505 | } |
| 506 | |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 507 | // Casting operators. ick |
| 508 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 509 | static Constant *CastTo##TYPE (const ConstantInt *V) { \ |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 510 | return CLASS::get(Type::TYPE##Ty, (CTYPE)((BuiltinType)V->getZExtValue()));\ |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 513 | DEF_CAST(Bool , ConstantBool, bool) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 514 | DEF_CAST(SByte , ConstantInt, signed char) |
| 515 | DEF_CAST(UByte , ConstantInt, unsigned char) |
| 516 | DEF_CAST(Short , ConstantInt, signed short) |
| 517 | DEF_CAST(UShort, ConstantInt, unsigned short) |
| 518 | DEF_CAST(Int , ConstantInt, signed int) |
| 519 | DEF_CAST(UInt , ConstantInt, unsigned int) |
| 520 | DEF_CAST(Long , ConstantInt, int64_t) |
| 521 | DEF_CAST(ULong , ConstantInt, uint64_t) |
| 522 | DEF_CAST(Float , ConstantFP , float) |
| 523 | DEF_CAST(Double, ConstantFP , double) |
Chris Lattner | f59bd53 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 524 | #undef DEF_CAST |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 525 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 526 | static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 527 | if (V2->isNullValue()) // X / 0 |
Chris Lattner | 893af8c | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 528 | return 0; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 529 | BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 530 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 893af8c | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 533 | static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 534 | if (V2->isNullValue()) // X / 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 535 | return 0; |
| 536 | if (V2->isAllOnesValue() && // MIN_INT / -1 |
| 537 | (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue()) |
| 538 | return 0; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 539 | BuiltinType R = (BuiltinType)(V1->getSExtValue() / V2->getSExtValue()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 540 | return ConstantInt::get(*Ty, R); |
| 541 | } |
| 542 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 543 | static Constant *URem(const ConstantInt *V1, |
| 544 | const ConstantInt *V2) { |
Chris Lattner | 893af8c | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 545 | if (V2->isNullValue()) return 0; // X / 0 |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 546 | BuiltinType R = (BuiltinType)(V1->getZExtValue() % V2->getZExtValue()); |
| 547 | return ConstantInt::get(*Ty, R); |
| 548 | } |
| 549 | |
| 550 | static Constant *SRem(const ConstantInt *V1, |
| 551 | const ConstantInt *V2) { |
| 552 | if (V2->isNullValue()) return 0; // X % 0 |
| 553 | if (V2->isAllOnesValue() && // MIN_INT % -1 |
| 554 | (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue()) |
Chris Lattner | 893af8c | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 555 | return 0; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 556 | BuiltinType R = (BuiltinType)(V1->getSExtValue() % V2->getSExtValue()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 557 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 44540dd | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 558 | } |
Chris Lattner | cf4929f | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 559 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 560 | static Constant *And(const ConstantInt *V1, const ConstantInt *V2) { |
| 561 | BuiltinType R = |
| 562 | (BuiltinType)V1->getZExtValue() & (BuiltinType)V2->getZExtValue(); |
| 563 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 564 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 565 | static Constant *Or(const ConstantInt *V1, const ConstantInt *V2) { |
| 566 | BuiltinType R = |
| 567 | (BuiltinType)V1->getZExtValue() | (BuiltinType)V2->getZExtValue(); |
| 568 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 569 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 570 | static Constant *Xor(const ConstantInt *V1, const ConstantInt *V2) { |
| 571 | BuiltinType R = |
| 572 | (BuiltinType)V1->getZExtValue() ^ (BuiltinType)V2->getZExtValue(); |
| 573 | return ConstantInt::get(*Ty, R); |
Chris Lattner | cf4929f | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 576 | static Constant *Shl(const ConstantInt *V1, const ConstantInt *V2) { |
| 577 | BuiltinType R = |
| 578 | (BuiltinType)V1->getZExtValue() << (BuiltinType)V2->getZExtValue(); |
| 579 | return ConstantInt::get(*Ty, R); |
Chris Lattner | 5fa829c | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 582 | static Constant *LShr(const ConstantInt *V1, const ConstantInt *V2) { |
| 583 | BuiltinType R = BuiltinType(V1->getZExtValue() >> V2->getZExtValue()); |
| 584 | return ConstantInt::get(*Ty, R); |
| 585 | } |
| 586 | |
| 587 | static Constant *AShr(const ConstantInt *V1, const ConstantInt *V2) { |
| 588 | BuiltinType R = BuiltinType(V1->getSExtValue() >> V2->getZExtValue()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 589 | return ConstantInt::get(*Ty, R); |
Chris Lattner | cf4929f | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | 44540dd | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 591 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 592 | } // end anonymous namespace |
Chris Lattner | 44540dd | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 593 | |
| 594 | |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | // DirectFPRules Class |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | // |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 599 | /// DirectFPRules provides implementations of functions that are valid on |
| 600 | /// floating point types, but not all types in general. |
| 601 | /// |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 602 | namespace { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 603 | template <class BuiltinType, Type **Ty> |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 604 | struct VISIBILITY_HIDDEN DirectFPRules |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 605 | : public TemplateRules<ConstantFP, DirectFPRules<BuiltinType, Ty> > { |
| 606 | |
| 607 | static Constant *Add(const ConstantFP *V1, const ConstantFP *V2) { |
| 608 | BuiltinType R = (BuiltinType)V1->getValue() + |
| 609 | (BuiltinType)V2->getValue(); |
| 610 | return ConstantFP::get(*Ty, R); |
| 611 | } |
| 612 | |
| 613 | static Constant *Sub(const ConstantFP *V1, const ConstantFP *V2) { |
| 614 | BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue(); |
| 615 | return ConstantFP::get(*Ty, R); |
| 616 | } |
| 617 | |
| 618 | static Constant *Mul(const ConstantFP *V1, const ConstantFP *V2) { |
| 619 | BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue(); |
| 620 | return ConstantFP::get(*Ty, R); |
| 621 | } |
| 622 | |
| 623 | static Constant *LessThan(const ConstantFP *V1, const ConstantFP *V2) { |
| 624 | bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); |
| 625 | return ConstantBool::get(R); |
| 626 | } |
| 627 | |
| 628 | static Constant *EqualTo(const ConstantFP *V1, const ConstantFP *V2) { |
| 629 | bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue(); |
| 630 | return ConstantBool::get(R); |
| 631 | } |
| 632 | |
| 633 | static Constant *CastToPointer(const ConstantFP *V, |
| 634 | const PointerType *PTy) { |
| 635 | if (V->isNullValue()) // Is it a FP or Integral null value? |
| 636 | return ConstantPointerNull::get(PTy); |
| 637 | return 0; // Can't const prop other types of pointers |
| 638 | } |
| 639 | |
| 640 | // Casting operators. ick |
| 641 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
| 642 | static Constant *CastTo##TYPE (const ConstantFP *V) { \ |
| 643 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ |
| 644 | } |
| 645 | |
| 646 | DEF_CAST(Bool , ConstantBool, bool) |
| 647 | DEF_CAST(SByte , ConstantInt, signed char) |
| 648 | DEF_CAST(UByte , ConstantInt, unsigned char) |
| 649 | DEF_CAST(Short , ConstantInt, signed short) |
| 650 | DEF_CAST(UShort, ConstantInt, unsigned short) |
| 651 | DEF_CAST(Int , ConstantInt, signed int) |
| 652 | DEF_CAST(UInt , ConstantInt, unsigned int) |
| 653 | DEF_CAST(Long , ConstantInt, int64_t) |
| 654 | DEF_CAST(ULong , ConstantInt, uint64_t) |
| 655 | DEF_CAST(Float , ConstantFP , float) |
| 656 | DEF_CAST(Double, ConstantFP , double) |
| 657 | #undef DEF_CAST |
| 658 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 659 | static Constant *FRem(const ConstantFP *V1, const ConstantFP *V2) { |
Chris Lattner | 44540dd | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 660 | if (V2->isNullValue()) return 0; |
| 661 | BuiltinType Result = std::fmod((BuiltinType)V1->getValue(), |
| 662 | (BuiltinType)V2->getValue()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 663 | return ConstantFP::get(*Ty, Result); |
Chris Lattner | 44540dd | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 664 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 665 | static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) { |
Jeff Cohen | a97e8db | 2005-05-03 03:13:01 +0000 | [diff] [blame] | 666 | BuiltinType inf = std::numeric_limits<BuiltinType>::infinity(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 667 | if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf); |
| 668 | if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf); |
Andrew Lenharth | 8b9e3e6 | 2005-05-02 21:25:47 +0000 | [diff] [blame] | 669 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 670 | return ConstantFP::get(*Ty, R); |
Andrew Lenharth | 8b9e3e6 | 2005-05-02 21:25:47 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 905f311 | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 672 | }; |
Chris Lattner | 51df6d5 | 2006-06-21 18:13:36 +0000 | [diff] [blame] | 673 | } // end anonymous namespace |
Chris Lattner | 905f311 | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 0eff5ad | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 675 | static ManagedStatic<EmptyRules> EmptyR; |
| 676 | static ManagedStatic<BoolRules> BoolR; |
| 677 | static ManagedStatic<NullPointerRules> NullPointerR; |
| 678 | static ManagedStatic<ConstantPackedRules> ConstantPackedR; |
| 679 | static ManagedStatic<GeneralPackedRules> GeneralPackedR; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 680 | static ManagedStatic<DirectIntRules<signed char , &Type::SByteTy> > SByteR; |
| 681 | static ManagedStatic<DirectIntRules<unsigned char , &Type::UByteTy> > UByteR; |
| 682 | static ManagedStatic<DirectIntRules<signed short , &Type::ShortTy> > ShortR; |
| 683 | static ManagedStatic<DirectIntRules<unsigned short, &Type::UShortTy> > UShortR; |
| 684 | static ManagedStatic<DirectIntRules<signed int , &Type::IntTy> > IntR; |
| 685 | static ManagedStatic<DirectIntRules<unsigned int , &Type::UIntTy> > UIntR; |
| 686 | static ManagedStatic<DirectIntRules<int64_t , &Type::LongTy> > LongR; |
| 687 | static ManagedStatic<DirectIntRules<uint64_t , &Type::ULongTy> > ULongR; |
| 688 | static ManagedStatic<DirectFPRules <float , &Type::FloatTy> > FloatR; |
| 689 | static ManagedStatic<DirectFPRules <double , &Type::DoubleTy> > DoubleR; |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 690 | |
| 691 | /// ConstRules::get - This method returns the constant rules implementation that |
| 692 | /// implements the semantics of the two specified constants. |
Chris Lattner | 4a0ecc8 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 693 | ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) { |
Chris Lattner | cf09f51 | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 694 | if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) || |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 695 | isa<GlobalValue>(V1) || isa<GlobalValue>(V2) || |
| 696 | isa<UndefValue>(V1) || isa<UndefValue>(V2)) |
Chris Lattner | 0eff5ad | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 697 | return *EmptyR; |
Chris Lattner | 0dc3969 | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 698 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 699 | switch (V1->getType()->getTypeID()) { |
Chris Lattner | 0dc3969 | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 700 | default: assert(0 && "Unknown value type for constant folding!"); |
Chris Lattner | 0eff5ad | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 701 | case Type::BoolTyID: return *BoolR; |
| 702 | case Type::PointerTyID: return *NullPointerR; |
| 703 | case Type::SByteTyID: return *SByteR; |
| 704 | case Type::UByteTyID: return *UByteR; |
| 705 | case Type::ShortTyID: return *ShortR; |
| 706 | case Type::UShortTyID: return *UShortR; |
| 707 | case Type::IntTyID: return *IntR; |
| 708 | case Type::UIntTyID: return *UIntR; |
| 709 | case Type::LongTyID: return *LongR; |
| 710 | case Type::ULongTyID: return *ULongR; |
| 711 | case Type::FloatTyID: return *FloatR; |
| 712 | case Type::DoubleTyID: return *DoubleR; |
Chris Lattner | d15ed59 | 2006-01-04 02:03:29 +0000 | [diff] [blame] | 713 | case Type::PackedTyID: |
| 714 | if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2)) |
Chris Lattner | 0eff5ad | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 715 | return *ConstantPackedR; |
| 716 | return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 717 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 718 | } |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | //===----------------------------------------------------------------------===// |
| 722 | // ConstantFold*Instruction Implementations |
| 723 | //===----------------------------------------------------------------------===// |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 725 | /// CastConstantPacked - Convert the specified ConstantPacked node to the |
| 726 | /// specified packed type. At this point, we know that the elements of the |
| 727 | /// input packed constant are all simple integer or FP values. |
| 728 | static Constant *CastConstantPacked(ConstantPacked *CP, |
| 729 | const PackedType *DstTy) { |
| 730 | unsigned SrcNumElts = CP->getType()->getNumElements(); |
| 731 | unsigned DstNumElts = DstTy->getNumElements(); |
| 732 | const Type *SrcEltTy = CP->getType()->getElementType(); |
| 733 | const Type *DstEltTy = DstTy->getElementType(); |
| 734 | |
| 735 | // If both vectors have the same number of elements (thus, the elements |
| 736 | // are the same size), perform the conversion now. |
| 737 | if (SrcNumElts == DstNumElts) { |
| 738 | std::vector<Constant*> Result; |
| 739 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 740 | // If the src and dest elements are both integers, or both floats, we can |
| 741 | // just BitCast each element because the elements are the same size. |
| 742 | if ((SrcEltTy->isIntegral() && DstEltTy->isIntegral()) || |
| 743 | (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) { |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 744 | for (unsigned i = 0; i != SrcNumElts; ++i) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 745 | Result.push_back( |
Chris Lattner | 7da3bde | 2006-12-01 05:55:25 +0000 | [diff] [blame] | 746 | ConstantExpr::getCast(Instruction::BitCast, CP->getOperand(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 747 | DstEltTy)); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 748 | return ConstantPacked::get(Result); |
| 749 | } |
| 750 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 751 | // If this is an int-to-fp cast .. |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 752 | if (SrcEltTy->isIntegral()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 753 | // Ensure that it is int-to-fp cast |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 754 | assert(DstEltTy->isFloatingPoint()); |
| 755 | if (DstEltTy->getTypeID() == Type::DoubleTyID) { |
| 756 | for (unsigned i = 0; i != SrcNumElts; ++i) { |
| 757 | double V = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 758 | BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 759 | Result.push_back(ConstantFP::get(Type::DoubleTy, V)); |
| 760 | } |
| 761 | return ConstantPacked::get(Result); |
| 762 | } |
| 763 | assert(DstEltTy == Type::FloatTy && "Unknown fp type!"); |
| 764 | for (unsigned i = 0; i != SrcNumElts; ++i) { |
| 765 | float V = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 766 | BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 767 | Result.push_back(ConstantFP::get(Type::FloatTy, V)); |
| 768 | } |
| 769 | return ConstantPacked::get(Result); |
| 770 | } |
| 771 | |
| 772 | // Otherwise, this is an fp-to-int cast. |
| 773 | assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral()); |
| 774 | |
| 775 | if (SrcEltTy->getTypeID() == Type::DoubleTyID) { |
| 776 | for (unsigned i = 0; i != SrcNumElts; ++i) { |
| 777 | uint64_t V = |
| 778 | DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 779 | Constant *C = ConstantInt::get(Type::ULongTy, V); |
Reid Spencer | 6f40b83 | 2006-12-05 07:18:07 +0000 | [diff] [blame] | 780 | Result.push_back(ConstantExpr::getBitCast(C, DstEltTy )); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 781 | } |
| 782 | return ConstantPacked::get(Result); |
| 783 | } |
| 784 | |
| 785 | assert(SrcEltTy->getTypeID() == Type::FloatTyID); |
| 786 | for (unsigned i = 0; i != SrcNumElts; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 787 | uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue()); |
| 788 | Constant *C = ConstantInt::get(Type::UIntTy, V); |
Reid Spencer | 6f40b83 | 2006-12-05 07:18:07 +0000 | [diff] [blame] | 789 | Result.push_back(ConstantExpr::getBitCast(C, DstEltTy)); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 790 | } |
| 791 | return ConstantPacked::get(Result); |
| 792 | } |
| 793 | |
| 794 | // Otherwise, this is a cast that changes element count and size. Handle |
| 795 | // casts which shrink the elements here. |
| 796 | |
| 797 | // FIXME: We need to know endianness to do this! |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 802 | /// This function determines which opcode to use to fold two constant cast |
| 803 | /// expressions together. It uses CastInst::isEliminableCastPair to determine |
| 804 | /// the opcode. Consequently its just a wrapper around that function. |
| 805 | /// @Determine if it is valid to fold a cast of a cast |
| 806 | static unsigned |
| 807 | foldConstantCastPair( |
| 808 | unsigned opc, ///< opcode of the second cast constant expression |
| 809 | const ConstantExpr*Op, ///< the first cast constant expression |
| 810 | const Type *DstTy ///< desintation type of the first cast |
| 811 | ) { |
| 812 | assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); |
| 813 | assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); |
| 814 | assert(CastInst::isCast(opc) && "Invalid cast opcode"); |
| 815 | |
| 816 | // The the types and opcodes for the two Cast constant expressions |
| 817 | const Type *SrcTy = Op->getOperand(0)->getType(); |
| 818 | const Type *MidTy = Op->getType(); |
| 819 | Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); |
| 820 | Instruction::CastOps secondOp = Instruction::CastOps(opc); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 821 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 822 | // Let CastInst::isEliminableCastPair do the heavy lifting. |
| 823 | return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, |
| 824 | Type::ULongTy); |
| 825 | } |
| 826 | |
| 827 | Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 828 | const Type *DestTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 829 | const Type *SrcTy = V->getType(); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 830 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 831 | // Handle some simple cases |
| 832 | if (SrcTy == DestTy) |
| 833 | return (Constant*)V; // no-op cast |
| 834 | |
| 835 | if (isa<UndefValue>(V)) |
| 836 | return UndefValue::get(DestTy); |
| 837 | |
| 838 | // If the cast operand is a constant expression, there's a few things we can |
| 839 | // do to try to simplify it. |
| 840 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 841 | if (CE->isCast()) { |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 842 | // Try hard to fold cast of cast because they are often eliminable. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 843 | if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) |
| 844 | return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 845 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 846 | // If all of the indexes in the GEP are null values, there is no pointer |
| 847 | // adjustment going on. We might as well cast the source pointer. |
| 848 | bool isAllNull = true; |
| 849 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 850 | if (!CE->getOperand(i)->isNullValue()) { |
| 851 | isAllNull = false; |
| 852 | break; |
| 853 | } |
| 854 | if (isAllNull) |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 855 | // This is casting one pointer type to another, always BitCast |
Reid Spencer | 57c6993 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 856 | return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 857 | } |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 858 | } |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 859 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 860 | // We actually have to do a cast now, but first, we might need to fix up |
| 861 | // the value of the operand. |
| 862 | switch (opc) { |
Chris Lattner | 5be6625 | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 863 | case Instruction::PtrToInt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 864 | case Instruction::FPTrunc: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 865 | case Instruction::FPExt: |
Chris Lattner | 5be6625 | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 866 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 867 | case Instruction::FPToUI: { |
| 868 | ConstRules &Rules = ConstRules::get(V, V); |
| 869 | V = Rules.castToULong(V); // make sure we get an unsigned value first |
| 870 | break; |
| 871 | } |
| 872 | case Instruction::FPToSI: { |
| 873 | ConstRules &Rules = ConstRules::get(V, V); |
| 874 | V = Rules.castToLong(V); // make sure we get a signed value first |
| 875 | break; |
| 876 | } |
| 877 | case Instruction::IntToPtr: //always treated as unsigned |
| 878 | case Instruction::UIToFP: |
| 879 | case Instruction::ZExt: |
| 880 | // A ZExt always produces an unsigned value so we need to cast the value |
| 881 | // now before we try to cast it to the destination type |
| 882 | if (isa<ConstantInt>(V)) |
| 883 | V = ConstantInt::get(SrcTy->getUnsignedVersion(), |
| 884 | cast<ConstantIntegral>(V)->getZExtValue()); |
| 885 | break; |
| 886 | case Instruction::SIToFP: |
| 887 | case Instruction::SExt: |
| 888 | // A SExt always produces a signed value so we need to cast the value |
| 889 | // now before we try to cast it to the destiniation type. |
| 890 | if (isa<ConstantInt>(V)) |
| 891 | V = ConstantInt::get(SrcTy->getSignedVersion(), |
| 892 | cast<ConstantIntegral>(V)->getSExtValue()); |
Chris Lattner | 9dec3a2 | 2006-12-01 19:50:54 +0000 | [diff] [blame] | 893 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) |
| 894 | V = ConstantInt::get(Type::SByteTy, CB->getValue() ? -1 : 0); |
| 895 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 896 | break; |
Chris Lattner | 5be6625 | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 897 | case Instruction::Trunc: |
| 898 | // We just handle trunc directly here. The code below doesn't work for |
| 899 | // trunc to bool. |
| 900 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 901 | return ConstantIntegral::get(DestTy, CI->getZExtValue()); |
| 902 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 903 | case Instruction::BitCast: |
| 904 | // Check to see if we are casting a pointer to an aggregate to a pointer to |
| 905 | // the first element. If so, return the appropriate GEP instruction. |
| 906 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) |
| 907 | if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) { |
| 908 | std::vector<Value*> IdxList; |
| 909 | IdxList.push_back(Constant::getNullValue(Type::IntTy)); |
| 910 | const Type *ElTy = PTy->getElementType(); |
| 911 | while (ElTy != DPTy->getElementType()) { |
| 912 | if (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 913 | if (STy->getNumElements() == 0) break; |
| 914 | ElTy = STy->getElementType(0); |
| 915 | IdxList.push_back(Constant::getNullValue(Type::UIntTy)); |
| 916 | } else if (const SequentialType *STy = |
| 917 | dyn_cast<SequentialType>(ElTy)) { |
| 918 | if (isa<PointerType>(ElTy)) break; // Can't index into pointers! |
| 919 | ElTy = STy->getElementType(); |
| 920 | IdxList.push_back(IdxList[0]); |
| 921 | } else { |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 922 | break; |
| 923 | } |
| 924 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 925 | |
| 926 | if (ElTy == DPTy->getElementType()) |
| 927 | return ConstantExpr::getGetElementPtr( |
| 928 | const_cast<Constant*>(V),IdxList); |
| 929 | } |
| 930 | |
| 931 | // Handle casts from one packed constant to another. We know that the src |
| 932 | // and dest type have the same size (otherwise its an illegal cast). |
| 933 | if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { |
| 934 | if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) { |
| 935 | assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() && |
| 936 | "Not cast between same sized vectors!"); |
| 937 | // First, check for null and undef |
| 938 | if (isa<ConstantAggregateZero>(V)) |
| 939 | return Constant::getNullValue(DestTy); |
| 940 | if (isa<UndefValue>(V)) |
| 941 | return UndefValue::get(DestTy); |
| 942 | |
| 943 | if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) { |
| 944 | // This is a cast from a ConstantPacked of one type to a |
| 945 | // ConstantPacked of another type. Check to see if all elements of |
| 946 | // the input are simple. |
| 947 | bool AllSimpleConstants = true; |
| 948 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { |
| 949 | if (!isa<ConstantInt>(CP->getOperand(i)) && |
| 950 | !isa<ConstantFP>(CP->getOperand(i))) { |
| 951 | AllSimpleConstants = false; |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // If all of the elements are simple constants, we can fold this. |
| 957 | if (AllSimpleConstants) |
| 958 | return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy); |
| 959 | } |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 962 | |
| 963 | // Handle sign conversion for integer no-op casts. We need to cast the |
| 964 | // value to the correct signedness before we try to cast it to the |
| 965 | // destination type. Be careful to do this only for integer types. |
| 966 | if (isa<ConstantIntegral>(V) && SrcTy->isInteger()) { |
| 967 | if (SrcTy->isSigned()) |
| 968 | V = ConstantInt::get(SrcTy->getUnsignedVersion(), |
| 969 | cast<ConstantIntegral>(V)->getZExtValue()); |
| 970 | else |
| 971 | V = ConstantInt::get(SrcTy->getSignedVersion(), |
| 972 | cast<ConstantIntegral>(V)->getSExtValue()); |
| 973 | } |
| 974 | break; |
| 975 | default: |
| 976 | assert(!"Invalid CE CastInst opcode"); |
| 977 | break; |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 978 | } |
Chris Lattner | f8d1097 | 2004-10-11 03:57:30 +0000 | [diff] [blame] | 979 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 980 | // Okay, no more folding possible, time to cast |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 981 | ConstRules &Rules = ConstRules::get(V, V); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 982 | switch (DestTy->getTypeID()) { |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 983 | case Type::BoolTyID: return Rules.castToBool(V); |
| 984 | case Type::UByteTyID: return Rules.castToUByte(V); |
| 985 | case Type::SByteTyID: return Rules.castToSByte(V); |
| 986 | case Type::UShortTyID: return Rules.castToUShort(V); |
| 987 | case Type::ShortTyID: return Rules.castToShort(V); |
| 988 | case Type::UIntTyID: return Rules.castToUInt(V); |
| 989 | case Type::IntTyID: return Rules.castToInt(V); |
| 990 | case Type::ULongTyID: return Rules.castToULong(V); |
| 991 | case Type::LongTyID: return Rules.castToLong(V); |
| 992 | case Type::FloatTyID: return Rules.castToFloat(V); |
| 993 | case Type::DoubleTyID: return Rules.castToDouble(V); |
| 994 | case Type::PointerTyID: |
| 995 | return Rules.castToPointer(V, cast<PointerType>(DestTy)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 996 | // what about packed ? |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 997 | default: return 0; |
| 998 | } |
| 999 | } |
| 1000 | |
Chris Lattner | e971486 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 1001 | Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, |
| 1002 | const Constant *V1, |
| 1003 | const Constant *V2) { |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1004 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond)) |
| 1005 | return const_cast<Constant*>(CB->getValue() ? V1 : V2); |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1006 | |
| 1007 | if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2); |
| 1008 | if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1); |
| 1009 | if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1); |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1010 | if (V1 == V2) return const_cast<Constant*>(V1); |
Chris Lattner | e971486 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
| 1013 | |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1014 | Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, |
| 1015 | const Constant *Idx) { |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 1016 | if (isa<UndefValue>(Val)) // ee(undef, x) -> undef |
| 1017 | return UndefValue::get(cast<PackedType>(Val->getType())->getElementType()); |
Chris Lattner | f38d471 | 2006-04-07 04:44:06 +0000 | [diff] [blame] | 1018 | if (Val->isNullValue()) // ee(zero, x) -> zero |
| 1019 | return Constant::getNullValue( |
| 1020 | cast<PackedType>(Val->getType())->getElementType()); |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 1021 | |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1022 | if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1023 | if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { |
| 1024 | return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue())); |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 1025 | } else if (isa<UndefValue>(Idx)) { |
| 1026 | // ee({w,x,y,z}, undef) -> w (an arbitrary value). |
| 1027 | return const_cast<Constant*>(CVal->getOperand(0)); |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1028 | } |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 1029 | } |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1033 | Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, |
| 1034 | const Constant *Elt, |
| 1035 | const Constant *Idx) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1036 | const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1037 | if (!CIdx) return 0; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1038 | uint64_t idxVal = CIdx->getZExtValue(); |
Reid Spencer | 4ab09d4 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 1039 | if (isa<UndefValue>(Val)) { |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1040 | // Insertion of scalar constant into packed undef |
| 1041 | // Optimize away insertion of undef |
| 1042 | if (isa<UndefValue>(Elt)) |
| 1043 | return const_cast<Constant*>(Val); |
| 1044 | // Otherwise break the aggregate undef into multiple undefs and do |
| 1045 | // the insertion |
| 1046 | unsigned numOps = |
| 1047 | cast<PackedType>(Val->getType())->getNumElements(); |
| 1048 | std::vector<Constant*> Ops; |
| 1049 | Ops.reserve(numOps); |
| 1050 | for (unsigned i = 0; i < numOps; ++i) { |
| 1051 | const Constant *Op = |
| 1052 | (i == idxVal) ? Elt : UndefValue::get(Elt->getType()); |
| 1053 | Ops.push_back(const_cast<Constant*>(Op)); |
| 1054 | } |
| 1055 | return ConstantPacked::get(Ops); |
| 1056 | } |
Reid Spencer | 4ab09d4 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 1057 | if (isa<ConstantAggregateZero>(Val)) { |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1058 | // Insertion of scalar constant into packed aggregate zero |
| 1059 | // Optimize away insertion of zero |
| 1060 | if (Elt->isNullValue()) |
| 1061 | return const_cast<Constant*>(Val); |
| 1062 | // Otherwise break the aggregate zero into multiple zeros and do |
| 1063 | // the insertion |
| 1064 | unsigned numOps = |
| 1065 | cast<PackedType>(Val->getType())->getNumElements(); |
| 1066 | std::vector<Constant*> Ops; |
| 1067 | Ops.reserve(numOps); |
| 1068 | for (unsigned i = 0; i < numOps; ++i) { |
| 1069 | const Constant *Op = |
| 1070 | (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType()); |
| 1071 | Ops.push_back(const_cast<Constant*>(Op)); |
| 1072 | } |
| 1073 | return ConstantPacked::get(Ops); |
| 1074 | } |
| 1075 | if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) { |
| 1076 | // Insertion of scalar constant into packed constant |
| 1077 | std::vector<Constant*> Ops; |
| 1078 | Ops.reserve(CVal->getNumOperands()); |
| 1079 | for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { |
| 1080 | const Constant *Op = |
| 1081 | (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i)); |
| 1082 | Ops.push_back(const_cast<Constant*>(Op)); |
| 1083 | } |
| 1084 | return ConstantPacked::get(Ops); |
| 1085 | } |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1089 | Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1, |
| 1090 | const Constant *V2, |
| 1091 | const Constant *Mask) { |
| 1092 | // TODO: |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1097 | /// isZeroSizedType - This type is zero sized if its an array or structure of |
| 1098 | /// zero sized types. The only leaf zero sized type is an empty structure. |
| 1099 | static bool isMaybeZeroSizedType(const Type *Ty) { |
| 1100 | if (isa<OpaqueType>(Ty)) return true; // Can't say. |
| 1101 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 1102 | |
| 1103 | // If all of elements have zero size, this does too. |
| 1104 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Chris Lattner | f4aa335 | 2005-01-28 23:17:27 +0000 | [diff] [blame] | 1105 | if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | |
| 1108 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 1109 | return isMaybeZeroSizedType(ATy->getElementType()); |
| 1110 | } |
| 1111 | return false; |
| 1112 | } |
Chris Lattner | e971486 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1114 | /// IdxCompare - Compare the two constants as though they were getelementptr |
| 1115 | /// indices. This allows coersion of the types to be the same thing. |
| 1116 | /// |
| 1117 | /// If the two constants are the "same" (after coersion), return 0. If the |
| 1118 | /// first is less than the second, return -1, if the second is less than the |
| 1119 | /// first, return 1. If the constants are not integral, return -2. |
| 1120 | /// |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1121 | static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1122 | if (C1 == C2) return 0; |
| 1123 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1124 | // Ok, we found a different index. Are either of the operands ConstantExprs? |
| 1125 | // If so, we can't do anything with them. |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1126 | if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) |
| 1127 | return -2; // don't know! |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 1129 | // Ok, we have two differing integer indices. Sign extend them to be the same |
| 1130 | // type. Long is always big enough, so we use it. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1131 | if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy) |
| 1132 | C1 = ConstantExpr::getSignExtend(C1, Type::LongTy); |
| 1133 | else |
| 1134 | C1 = ConstantExpr::getBitCast(C1, Type::LongTy); |
| 1135 | if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy) |
| 1136 | C2 = ConstantExpr::getSignExtend(C2, Type::LongTy); |
| 1137 | else |
| 1138 | C2 = ConstantExpr::getBitCast(C2, Type::LongTy); |
| 1139 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1140 | if (C1 == C2) return 0; // Are they just differing types? |
| 1141 | |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1142 | // If the type being indexed over is really just a zero sized type, there is |
| 1143 | // no pointer difference being made here. |
| 1144 | if (isMaybeZeroSizedType(ElTy)) |
| 1145 | return -2; // dunno. |
| 1146 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1147 | // If they are really different, now that they are the same type, then we |
| 1148 | // found a difference! |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1149 | if (cast<ConstantInt>(C1)->getSExtValue() < |
| 1150 | cast<ConstantInt>(C2)->getSExtValue()) |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1151 | return -1; |
| 1152 | else |
| 1153 | return 1; |
| 1154 | } |
| 1155 | |
| 1156 | /// evaluateRelation - This function determines if there is anything we can |
| 1157 | /// decide about the two constants provided. This doesn't need to handle simple |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1158 | /// things like integer comparisons, but should instead handle ConstantExprs |
| 1159 | /// and GlobalValuess. If we can determine that the two constants have a |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1160 | /// particular relation to each other, we should return the corresponding SetCC |
| 1161 | /// code, otherwise return Instruction::BinaryOpsEnd. |
| 1162 | /// |
| 1163 | /// To simplify this code we canonicalize the relation so that the first |
| 1164 | /// operand is always the most "complex" of the two. We consider simple |
| 1165 | /// constants (like ConstantInt) to be the simplest, followed by |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1166 | /// GlobalValues, followed by ConstantExpr's (the most complex). |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1167 | /// |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1168 | static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1169 | assert(V1->getType() == V2->getType() && |
| 1170 | "Cannot compare different types of values!"); |
| 1171 | if (V1 == V2) return Instruction::SetEQ; |
| 1172 | |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1173 | if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) { |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1174 | if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) { |
| 1175 | // We distilled this down to a simple case, use the standard constant |
| 1176 | // folder. |
| 1177 | ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2)); |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1178 | if (R && R->getValue()) return Instruction::SetEQ; |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1179 | R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2)); |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1180 | if (R && R->getValue()) return Instruction::SetLT; |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1181 | R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2)); |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1182 | if (R && R->getValue()) return Instruction::SetGT; |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1183 | |
| 1184 | // If we couldn't figure it out, bail. |
| 1185 | return Instruction::BinaryOpsEnd; |
| 1186 | } |
| 1187 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1188 | // If the first operand is simple, swap operands. |
Chris Lattner | b97e278 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1189 | Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); |
| 1190 | if (SwappedRelation != Instruction::BinaryOpsEnd) |
| 1191 | return SetCondInst::getSwappedCondition(SwappedRelation); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | 2c822cc | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1193 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) { |
Chris Lattner | b97e278 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1194 | if (isa<ConstantExpr>(V2)) { // Swap as necessary. |
Chris Lattner | 2c822cc | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1195 | Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); |
| 1196 | if (SwappedRelation != Instruction::BinaryOpsEnd) |
| 1197 | return SetCondInst::getSwappedCondition(SwappedRelation); |
| 1198 | else |
| 1199 | return Instruction::BinaryOpsEnd; |
Chris Lattner | b97e278 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1200 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1201 | |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1202 | // Now we know that the RHS is a GlobalValue or simple constant, |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1203 | // which (since the types must match) means that it's a ConstantPointerNull. |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1204 | if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame^] | 1205 | if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage()) |
| 1206 | return Instruction::SetNE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1207 | } else { |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame^] | 1208 | // GlobalVals can never be null. |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1209 | assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame^] | 1210 | if (!CPR1->hasExternalWeakLinkage()) |
| 1211 | return Instruction::SetNE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1212 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1213 | } else { |
| 1214 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 1215 | // constantexpr, a CPR, or a simple constant. |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1216 | ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1217 | Constant *CE1Op0 = CE1->getOperand(0); |
| 1218 | |
| 1219 | switch (CE1->getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1220 | case Instruction::Trunc: |
| 1221 | case Instruction::FPTrunc: |
| 1222 | case Instruction::FPExt: |
| 1223 | case Instruction::FPToUI: |
| 1224 | case Instruction::FPToSI: |
| 1225 | break; // We don't do anything with floating point. |
| 1226 | case Instruction::ZExt: |
| 1227 | case Instruction::SExt: |
| 1228 | case Instruction::UIToFP: |
| 1229 | case Instruction::SIToFP: |
| 1230 | case Instruction::PtrToInt: |
| 1231 | case Instruction::IntToPtr: |
| 1232 | case Instruction::BitCast: |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1233 | // If the cast is not actually changing bits, and the second operand is a |
| 1234 | // null pointer, do the comparison with the pre-casted value. |
| 1235 | if (V2->isNullValue() && |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1236 | (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral())) |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1237 | return evaluateRelation(CE1Op0, |
| 1238 | Constant::getNullValue(CE1Op0->getType())); |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1239 | |
| 1240 | // If the dest type is a pointer type, and the RHS is a constantexpr cast |
| 1241 | // from the same type as the src of the LHS, evaluate the inputs. This is |
| 1242 | // important for things like "seteq (cast 4 to int*), (cast 5 to int*)", |
| 1243 | // which happens a lot in compilers with tagged integers. |
| 1244 | if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1245 | if (isa<PointerType>(CE1->getType()) && CE2->isCast() && |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1246 | CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() && |
| 1247 | CE1->getOperand(0)->getType()->isIntegral()) { |
| 1248 | return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0)); |
| 1249 | } |
Chris Lattner | a0ae819 | 2004-04-11 01:29:30 +0000 | [diff] [blame] | 1250 | break; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1251 | |
| 1252 | case Instruction::GetElementPtr: |
| 1253 | // Ok, since this is a getelementptr, we know that the constant has a |
| 1254 | // pointer type. Check the various cases. |
| 1255 | if (isa<ConstantPointerNull>(V2)) { |
| 1256 | // If we are comparing a GEP to a null pointer, check to see if the base |
| 1257 | // of the GEP equals the null pointer. |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame^] | 1258 | if (GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { |
| 1259 | if (GV->hasExternalWeakLinkage()) |
| 1260 | // Weak linkage GVals could be zero or not. We're comparing that |
| 1261 | // to null pointer so its greater-or-equal |
| 1262 | return Instruction::SetGE; |
| 1263 | else |
| 1264 | // If its not weak linkage, the GVal must have a non-zero address |
| 1265 | // so the result is greater-than |
| 1266 | return Instruction::SetGT; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1267 | } else if (isa<ConstantPointerNull>(CE1Op0)) { |
| 1268 | // If we are indexing from a null pointer, check to see if we have any |
| 1269 | // non-zero indices. |
| 1270 | for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) |
| 1271 | if (!CE1->getOperand(i)->isNullValue()) |
| 1272 | // Offsetting from null, must not be equal. |
| 1273 | return Instruction::SetGT; |
| 1274 | // Only zero indexes from null, must still be zero. |
| 1275 | return Instruction::SetEQ; |
| 1276 | } |
| 1277 | // Otherwise, we can't really say if the first operand is null or not. |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1278 | } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1279 | if (isa<ConstantPointerNull>(CE1Op0)) { |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame^] | 1280 | if (CPR2->hasExternalWeakLinkage()) |
| 1281 | // Weak linkage GVals could be zero or not. We're comparing it to |
| 1282 | // a null pointer, so its less-or-equal |
| 1283 | return Instruction::SetLE; |
| 1284 | else |
| 1285 | // If its not weak linkage, the GVal must have a non-zero address |
| 1286 | // so the result is less-than |
| 1287 | return Instruction::SetLT; |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1288 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1289 | if (CPR1 == CPR2) { |
| 1290 | // If this is a getelementptr of the same global, then it must be |
| 1291 | // different. Because the types must match, the getelementptr could |
| 1292 | // only have at most one index, and because we fold getelementptr's |
| 1293 | // with a single zero index, it must be nonzero. |
| 1294 | assert(CE1->getNumOperands() == 2 && |
| 1295 | !CE1->getOperand(1)->isNullValue() && |
| 1296 | "Suprising getelementptr!"); |
| 1297 | return Instruction::SetGT; |
| 1298 | } else { |
| 1299 | // If they are different globals, we don't know what the value is, |
| 1300 | // but they can't be equal. |
| 1301 | return Instruction::SetNE; |
| 1302 | } |
| 1303 | } |
| 1304 | } else { |
| 1305 | const ConstantExpr *CE2 = cast<ConstantExpr>(V2); |
| 1306 | const Constant *CE2Op0 = CE2->getOperand(0); |
| 1307 | |
| 1308 | // There are MANY other foldings that we could perform here. They will |
| 1309 | // probably be added on demand, as they seem needed. |
| 1310 | switch (CE2->getOpcode()) { |
| 1311 | default: break; |
| 1312 | case Instruction::GetElementPtr: |
| 1313 | // By far the most common case to handle is when the base pointers are |
| 1314 | // obviously to the same or different globals. |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1315 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1316 | if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal |
| 1317 | return Instruction::SetNE; |
| 1318 | // Ok, we know that both getelementptr instructions are based on the |
| 1319 | // same global. From this, we can precisely determine the relative |
| 1320 | // ordering of the resultant pointers. |
| 1321 | unsigned i = 1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1323 | // Compare all of the operands the GEP's have in common. |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1324 | gep_type_iterator GTI = gep_type_begin(CE1); |
| 1325 | for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); |
| 1326 | ++i, ++GTI) |
| 1327 | switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i), |
| 1328 | GTI.getIndexedType())) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1329 | case -1: return Instruction::SetLT; |
| 1330 | case 1: return Instruction::SetGT; |
| 1331 | case -2: return Instruction::BinaryOpsEnd; |
| 1332 | } |
| 1333 | |
| 1334 | // Ok, we ran out of things they have in common. If any leftovers |
| 1335 | // are non-zero then we have a difference, otherwise we are equal. |
| 1336 | for (; i < CE1->getNumOperands(); ++i) |
| 1337 | if (!CE1->getOperand(i)->isNullValue()) |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1338 | if (isa<ConstantIntegral>(CE1->getOperand(i))) |
| 1339 | return Instruction::SetGT; |
| 1340 | else |
| 1341 | return Instruction::BinaryOpsEnd; // Might be equal. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1343 | for (; i < CE2->getNumOperands(); ++i) |
| 1344 | if (!CE2->getOperand(i)->isNullValue()) |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1345 | if (isa<ConstantIntegral>(CE2->getOperand(i))) |
| 1346 | return Instruction::SetLT; |
| 1347 | else |
| 1348 | return Instruction::BinaryOpsEnd; // Might be equal. |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1349 | return Instruction::SetEQ; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1354 | default: |
| 1355 | break; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | return Instruction::BinaryOpsEnd; |
| 1360 | } |
| 1361 | |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1362 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 1363 | const Constant *V1, |
| 1364 | const Constant *V2) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1365 | Constant *C = 0; |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1366 | switch (Opcode) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1367 | default: break; |
| 1368 | case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break; |
| 1369 | case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break; |
| 1370 | case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1371 | case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break; |
| 1372 | case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break; |
| 1373 | case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1374 | case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break; |
| 1375 | case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break; |
| 1376 | case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1377 | case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break; |
| 1378 | case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break; |
| 1379 | case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break; |
| 1380 | case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1381 | case Instruction::LShr: C = ConstRules::get(V1, V2).lshr(V1, V2); break; |
| 1382 | case Instruction::AShr: C = ConstRules::get(V1, V2).ashr(V1, V2); break; |
Reid Spencer | b8611ec | 2006-12-01 03:56:30 +0000 | [diff] [blame] | 1383 | case Instruction::SetEQ: |
| 1384 | // SetEQ(null,GV) -> false |
| 1385 | if (V1->isNullValue()) { |
| 1386 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2)) |
| 1387 | if (!GV->hasExternalWeakLinkage()) |
| 1388 | return ConstantBool::getFalse(); |
| 1389 | // SetEQ(GV,null) -> false |
| 1390 | } else if (V2->isNullValue()) { |
| 1391 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) |
| 1392 | if (!GV->hasExternalWeakLinkage()) |
| 1393 | return ConstantBool::getFalse(); |
| 1394 | } |
| 1395 | C = ConstRules::get(V1, V2).equalto(V1, V2); |
| 1396 | break; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1397 | case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break; |
| 1398 | case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break; |
Reid Spencer | b8611ec | 2006-12-01 03:56:30 +0000 | [diff] [blame] | 1399 | case Instruction::SetNE: |
| 1400 | // SetNE(null,GV) -> true |
| 1401 | if (V1->isNullValue()) { |
| 1402 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2)) |
| 1403 | if (!GV->hasExternalWeakLinkage()) |
| 1404 | return ConstantBool::getTrue(); |
| 1405 | // SetNE(GV,null) -> true |
| 1406 | } else if (V2->isNullValue()) { |
| 1407 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) |
| 1408 | if (!GV->hasExternalWeakLinkage()) |
| 1409 | return ConstantBool::getTrue(); |
| 1410 | } |
| 1411 | // V1 != V2 === !(V1 == V2) |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1412 | C = ConstRules::get(V1, V2).equalto(V1, V2); |
Chris Lattner | 84e1064 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 1413 | if (C) return ConstantExpr::getNot(C); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | case Instruction::SetLE: // V1 <= V2 === !(V2 < V1) |
| 1416 | C = ConstRules::get(V1, V2).lessthan(V2, V1); |
Chris Lattner | 84e1064 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 1417 | if (C) return ConstantExpr::getNot(C); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | case Instruction::SetGE: // V1 >= V2 === !(V1 < V2) |
| 1420 | C = ConstRules::get(V1, V2).lessthan(V1, V2); |
Chris Lattner | 84e1064 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 1421 | if (C) return ConstantExpr::getNot(C); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1422 | break; |
| 1423 | } |
| 1424 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1425 | // If we successfully folded the expression, return it now. |
| 1426 | if (C) return C; |
| 1427 | |
Chris Lattner | a5b0740 | 2006-09-17 19:14:47 +0000 | [diff] [blame] | 1428 | if (SetCondInst::isComparison(Opcode)) { |
Chris Lattner | 35f92ae | 2004-10-17 04:01:51 +0000 | [diff] [blame] | 1429 | if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) |
| 1430 | return UndefValue::get(Type::BoolTy); |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1431 | switch (evaluateRelation(const_cast<Constant*>(V1), |
| 1432 | const_cast<Constant*>(V2))) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1433 | default: assert(0 && "Unknown relational!"); |
| 1434 | case Instruction::BinaryOpsEnd: |
| 1435 | break; // Couldn't determine anything about these constants. |
| 1436 | case Instruction::SetEQ: // We know the constants are equal! |
| 1437 | // If we know the constants are equal, we can decide the result of this |
| 1438 | // computation precisely. |
| 1439 | return ConstantBool::get(Opcode == Instruction::SetEQ || |
| 1440 | Opcode == Instruction::SetLE || |
| 1441 | Opcode == Instruction::SetGE); |
| 1442 | case Instruction::SetLT: |
| 1443 | // If we know that V1 < V2, we can decide the result of this computation |
| 1444 | // precisely. |
| 1445 | return ConstantBool::get(Opcode == Instruction::SetLT || |
| 1446 | Opcode == Instruction::SetNE || |
| 1447 | Opcode == Instruction::SetLE); |
| 1448 | case Instruction::SetGT: |
| 1449 | // If we know that V1 > V2, we can decide the result of this computation |
| 1450 | // precisely. |
| 1451 | return ConstantBool::get(Opcode == Instruction::SetGT || |
| 1452 | Opcode == Instruction::SetNE || |
| 1453 | Opcode == Instruction::SetGE); |
| 1454 | case Instruction::SetLE: |
| 1455 | // If we know that V1 <= V2, we can only partially decide this relation. |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1456 | if (Opcode == Instruction::SetGT) return ConstantBool::getFalse(); |
| 1457 | if (Opcode == Instruction::SetLT) return ConstantBool::getTrue(); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | |
| 1460 | case Instruction::SetGE: |
| 1461 | // If we know that V1 >= V2, we can only partially decide this relation. |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1462 | if (Opcode == Instruction::SetLT) return ConstantBool::getFalse(); |
| 1463 | if (Opcode == Instruction::SetGT) return ConstantBool::getTrue(); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1464 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1466 | case Instruction::SetNE: |
| 1467 | // If we know that V1 != V2, we can only partially decide this relation. |
Chris Lattner | 850ba99 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1468 | if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse(); |
| 1469 | if (Opcode == Instruction::SetNE) return ConstantBool::getTrue(); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | } |
Chris Lattner | 35f92ae | 2004-10-17 04:01:51 +0000 | [diff] [blame] | 1472 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1474 | if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) { |
| 1475 | switch (Opcode) { |
| 1476 | case Instruction::Add: |
| 1477 | case Instruction::Sub: |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1478 | case Instruction::Xor: |
| 1479 | return UndefValue::get(V1->getType()); |
| 1480 | |
| 1481 | case Instruction::Mul: |
| 1482 | case Instruction::And: |
| 1483 | return Constant::getNullValue(V1->getType()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1484 | case Instruction::UDiv: |
| 1485 | case Instruction::SDiv: |
| 1486 | case Instruction::FDiv: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1487 | case Instruction::URem: |
| 1488 | case Instruction::SRem: |
| 1489 | case Instruction::FRem: |
| 1490 | if (!isa<UndefValue>(V2)) // undef / X -> 0 |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1491 | return Constant::getNullValue(V1->getType()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1492 | return const_cast<Constant*>(V2); // X / undef -> undef |
| 1493 | case Instruction::Or: // X | undef -> -1 |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1494 | return ConstantInt::getAllOnesValue(V1->getType()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1495 | case Instruction::LShr: |
| 1496 | if (isa<UndefValue>(V2) && isa<UndefValue>(V1)) |
| 1497 | return const_cast<Constant*>(V1); // undef lshr undef -> undef |
| 1498 | return Constant::getNullValue(V1->getType()); // X lshr undef -> 0 |
| 1499 | // undef lshr X -> 0 |
| 1500 | case Instruction::AShr: |
| 1501 | if (!isa<UndefValue>(V2)) |
| 1502 | return const_cast<Constant*>(V1); // undef ashr X --> undef |
| 1503 | else if (isa<UndefValue>(V1)) |
| 1504 | return const_cast<Constant*>(V1); // undef ashr undef -> undef |
| 1505 | else |
| 1506 | return const_cast<Constant*>(V1); // X ashr undef --> X |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1507 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1508 | // undef << X -> 0 or X << undef -> 0 |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1509 | return Constant::getNullValue(V1->getType()); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1513 | if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) { |
Reid Spencer | 4ab09d4 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 1514 | if (isa<ConstantExpr>(V2)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1515 | // There are many possible foldings we could do here. We should probably |
| 1516 | // at least fold add of a pointer with an integer into the appropriate |
| 1517 | // getelementptr. This will improve alias analysis a bit. |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1518 | } else { |
| 1519 | // Just implement a couple of simple identities. |
| 1520 | switch (Opcode) { |
| 1521 | case Instruction::Add: |
| 1522 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X |
| 1523 | break; |
| 1524 | case Instruction::Sub: |
| 1525 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X |
| 1526 | break; |
| 1527 | case Instruction::Mul: |
| 1528 | if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0 |
| 1529 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1530 | if (CI->getZExtValue() == 1) |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1531 | return const_cast<Constant*>(V1); // X * 1 == X |
| 1532 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1533 | case Instruction::UDiv: |
| 1534 | case Instruction::SDiv: |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1535 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1536 | if (CI->getZExtValue() == 1) |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1537 | return const_cast<Constant*>(V1); // X / 1 == X |
| 1538 | break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1539 | case Instruction::URem: |
| 1540 | case Instruction::SRem: |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1541 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1542 | if (CI->getZExtValue() == 1) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1543 | return Constant::getNullValue(CI->getType()); // X % 1 == 0 |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1544 | break; |
| 1545 | case Instruction::And: |
| 1546 | if (cast<ConstantIntegral>(V2)->isAllOnesValue()) |
| 1547 | return const_cast<Constant*>(V1); // X & -1 == X |
| 1548 | if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1549 | if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) { |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1550 | GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0)); |
Chris Lattner | 802b5ab | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 1551 | |
| 1552 | // Functions are at least 4-byte aligned. If and'ing the address of a |
| 1553 | // function with a constant < 4, fold it to zero. |
| 1554 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1555 | if (CI->getZExtValue() < 4 && isa<Function>(CPR)) |
Chris Lattner | 802b5ab | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 1556 | return Constant::getNullValue(CI->getType()); |
| 1557 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | case Instruction::Or: |
| 1560 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X |
| 1561 | if (cast<ConstantIntegral>(V2)->isAllOnesValue()) |
| 1562 | return const_cast<Constant*>(V2); // X | -1 == -1 |
| 1563 | break; |
| 1564 | case Instruction::Xor: |
| 1565 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
| 1569 | |
Reid Spencer | 4ab09d4 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 1570 | } else if (isa<ConstantExpr>(V2)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1571 | // If V2 is a constant expr and V1 isn't, flop them around and fold the |
| 1572 | // other way if possible. |
| 1573 | switch (Opcode) { |
| 1574 | case Instruction::Add: |
| 1575 | case Instruction::Mul: |
| 1576 | case Instruction::And: |
| 1577 | case Instruction::Or: |
| 1578 | case Instruction::Xor: |
| 1579 | case Instruction::SetEQ: |
| 1580 | case Instruction::SetNE: |
| 1581 | // No change of opcode required. |
| 1582 | return ConstantFoldBinaryInstruction(Opcode, V2, V1); |
| 1583 | |
| 1584 | case Instruction::SetLT: |
| 1585 | case Instruction::SetGT: |
| 1586 | case Instruction::SetLE: |
| 1587 | case Instruction::SetGE: |
| 1588 | // Change the opcode as necessary to swap the operands. |
| 1589 | Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode); |
| 1590 | return ConstantFoldBinaryInstruction(Opcode, V2, V1); |
| 1591 | |
| 1592 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1593 | case Instruction::LShr: |
| 1594 | case Instruction::AShr: |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1595 | case Instruction::Sub: |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1596 | case Instruction::SDiv: |
| 1597 | case Instruction::UDiv: |
| 1598 | case Instruction::FDiv: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1599 | case Instruction::URem: |
| 1600 | case Instruction::SRem: |
| 1601 | case Instruction::FRem: |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1602 | default: // These instructions cannot be flopped around. |
| 1603 | break; |
| 1604 | } |
| 1605 | } |
| 1606 | return 0; |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Reid Spencer | 2f8bbf5 | 2006-12-04 05:19:34 +0000 | [diff] [blame] | 1609 | Constant *llvm::ConstantFoldCompare( |
| 1610 | unsigned opcode, Constant *C1, Constant *C2, unsigned short predicate) |
| 1611 | { |
| 1612 | // Place holder for future folding of ICmp and FCmp instructions |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1616 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1617 | const std::vector<Value*> &IdxList) { |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1618 | if (IdxList.size() == 0 || |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1619 | (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue())) |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1620 | return const_cast<Constant*>(C); |
| 1621 | |
Chris Lattner | cfbf9fa | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1622 | if (isa<UndefValue>(C)) { |
| 1623 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
| 1624 | true); |
| 1625 | assert(Ty != 0 && "Invalid indices for GEP!"); |
| 1626 | return UndefValue::get(PointerType::get(Ty)); |
| 1627 | } |
| 1628 | |
| 1629 | Constant *Idx0 = cast<Constant>(IdxList[0]); |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1630 | if (C->isNullValue()) { |
| 1631 | bool isNull = true; |
| 1632 | for (unsigned i = 0, e = IdxList.size(); i != e; ++i) |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1633 | if (!cast<Constant>(IdxList[i])->isNullValue()) { |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1634 | isNull = false; |
| 1635 | break; |
| 1636 | } |
| 1637 | if (isNull) { |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1638 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1639 | true); |
| 1640 | assert(Ty != 0 && "Invalid indices for GEP!"); |
| 1641 | return ConstantPointerNull::get(PointerType::get(Ty)); |
| 1642 | } |
Chris Lattner | dd68469 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 1643 | |
| 1644 | if (IdxList.size() == 1) { |
| 1645 | const Type *ElTy = cast<PointerType>(C->getType())->getElementType(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1646 | if (uint32_t ElSize = ElTy->getPrimitiveSize()) { |
Chris Lattner | dd68469 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 1647 | // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm |
| 1648 | // type, we can statically fold this. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1649 | Constant *R = ConstantInt::get(Type::UIntTy, ElSize); |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1650 | // We know R is unsigned, Idx0 is signed because it must be an index |
| 1651 | // through a sequential type (gep pointer operand) which is always |
| 1652 | // signed. |
Reid Spencer | 57c6993 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 1653 | R = ConstantExpr::getSExtOrBitCast(R, Idx0->getType()); |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1654 | R = ConstantExpr::getMul(R, Idx0); // signed multiply |
| 1655 | // R is a signed integer, C is the GEP pointer so -> IntToPtr |
| 1656 | return ConstantExpr::getCast(Instruction::IntToPtr, R, C->getType()); |
Chris Lattner | dd68469 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 1657 | } |
| 1658 | } |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1659 | } |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
| 1662 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 1663 | // is a getelementptr instruction, combine the indices of the two |
| 1664 | // getelementptr instructions into a single instruction. |
| 1665 | // |
| 1666 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1667 | const Type *LastTy = 0; |
| 1668 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 1669 | I != E; ++I) |
| 1670 | LastTy = *I; |
| 1671 | |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1672 | if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) { |
| 1673 | std::vector<Value*> NewIndices; |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1674 | NewIndices.reserve(IdxList.size() + CE->getNumOperands()); |
| 1675 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1676 | NewIndices.push_back(CE->getOperand(i)); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Add the last index of the source with the first index of the new GEP. |
| 1679 | // Make sure to handle the case when they are actually different types. |
| 1680 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1681 | // Otherwise it must be an array. |
| 1682 | if (!Idx0->isNullValue()) { |
Chris Lattner | d340867 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1683 | const Type *IdxTy = Combined->getType(); |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1684 | if (IdxTy != Idx0->getType()) { |
Reid Spencer | 57c6993 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 1685 | Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::LongTy); |
| 1686 | Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, |
| 1687 | Type::LongTy); |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1688 | Combined = ConstantExpr::get(Instruction::Add, C1, C2); |
| 1689 | } else { |
| 1690 | Combined = |
| 1691 | ConstantExpr::get(Instruction::Add, Idx0, Combined); |
| 1692 | } |
Chris Lattner | d340867 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1693 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1695 | NewIndices.push_back(Combined); |
| 1696 | NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); |
| 1697 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | // Implement folding of: |
| 1702 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 1703 | // long 0, long 0) |
| 1704 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 1705 | // |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1706 | if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue()) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1707 | if (const PointerType *SPT = |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1708 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 1709 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 1710 | if (const ArrayType *CAT = |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1711 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1712 | if (CAT->getElementType() == SAT->getElementType()) |
| 1713 | return ConstantExpr::getGetElementPtr( |
| 1714 | (Constant*)CE->getOperand(0), IdxList); |
| 1715 | } |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |