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