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 | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 683 | return ConstantFP::get(DestTy, cast<ConstantFP>(V)->getValue()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 684 | case Instruction::FPToUI: { |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 685 | double dVal = cast<ConstantFP>(V)->getValue(); |
| 686 | uint64_t iVal = (uint64_t) dVal; |
| 687 | return ConstantIntegral::get(DestTy, iVal); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 688 | } |
| 689 | case Instruction::FPToSI: { |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 690 | double dVal = cast<ConstantFP>(V)->getValue(); |
| 691 | int64_t iVal = (int64_t) dVal; |
| 692 | return ConstantIntegral::get(DestTy, iVal); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 693 | } |
| 694 | case Instruction::IntToPtr: //always treated as unsigned |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 695 | if (V->isNullValue()) // Is it a FP or Integral null value? |
| 696 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
| 697 | return 0; // Other pointer types cannot be casted |
| 698 | case Instruction::PtrToInt: // always treated as unsigned |
| 699 | if (V->isNullValue()) |
| 700 | return ConstantIntegral::get(DestTy, 0); |
| 701 | return 0; // Other pointer types cannot be casted |
| 702 | case Instruction::UIToFP: { |
| 703 | // First, extract the unsigned integer value |
| 704 | uint64_t Val; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 705 | if (isa<ConstantInt>(V)) |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 706 | Val = cast<ConstantIntegral>(V)->getZExtValue(); |
| 707 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) |
| 708 | Val = CB->getValue() ? 1 : 0; |
| 709 | // Now generate the equivalent floating point value |
| 710 | double dVal = (double) Val; |
| 711 | return ConstantFP::get(DestTy, dVal); |
| 712 | } |
| 713 | case Instruction::SIToFP: { |
| 714 | // First, extract the signed integer value |
| 715 | int64_t Val; |
| 716 | if (isa<ConstantInt>(V)) |
| 717 | Val = cast<ConstantIntegral>(V)->getSExtValue(); |
| 718 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) |
| 719 | Val = CB->getValue() ? -1 : 0; |
| 720 | // Now generate the equivalent floating point value |
| 721 | double dVal = (double) Val; |
| 722 | return ConstantFP::get(DestTy, dVal); |
| 723 | } |
| 724 | case Instruction::ZExt: |
| 725 | // Handle trunc directly here if it is a ConstantIntegral. |
| 726 | if (isa<ConstantInt>(V)) |
| 727 | return ConstantInt::get(DestTy, cast<ConstantInt>(V)->getZExtValue()); |
| 728 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) |
| 729 | return ConstantInt::get(DestTy, CB->getValue() ? 1 : 0); |
| 730 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 731 | case Instruction::SExt: |
| 732 | // A SExt always produces a signed value so we need to cast the value |
| 733 | // now before we try to cast it to the destiniation type. |
| 734 | if (isa<ConstantInt>(V)) |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 735 | return ConstantInt::get(DestTy, cast<ConstantInt>(V)->getSExtValue()); |
Chris Lattner | defd847 | 2006-12-01 19:50:54 +0000 | [diff] [blame] | 736 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 737 | return ConstantInt::get(DestTy, CB->getValue() ? -1 : 0); |
| 738 | return 0; |
Chris Lattner | 710ebaf | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 739 | case Instruction::Trunc: |
| 740 | // We just handle trunc directly here. The code below doesn't work for |
| 741 | // trunc to bool. |
| 742 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 743 | return ConstantIntegral::get(DestTy, CI->getZExtValue()); |
| 744 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 745 | case Instruction::BitCast: |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 746 | if (SrcTy == DestTy) |
| 747 | return (Constant*)V; // no-op cast |
Chris Lattner | 4d1da16 | 2006-12-11 18:30:27 +0000 | [diff] [blame] | 748 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 749 | // Check to see if we are casting a pointer to an aggregate to a pointer to |
| 750 | // the first element. If so, return the appropriate GEP instruction. |
| 751 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) |
| 752 | if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) { |
| 753 | std::vector<Value*> IdxList; |
| 754 | IdxList.push_back(Constant::getNullValue(Type::IntTy)); |
| 755 | const Type *ElTy = PTy->getElementType(); |
| 756 | while (ElTy != DPTy->getElementType()) { |
| 757 | if (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 758 | if (STy->getNumElements() == 0) break; |
| 759 | ElTy = STy->getElementType(0); |
| 760 | IdxList.push_back(Constant::getNullValue(Type::UIntTy)); |
| 761 | } else if (const SequentialType *STy = |
| 762 | dyn_cast<SequentialType>(ElTy)) { |
| 763 | if (isa<PointerType>(ElTy)) break; // Can't index into pointers! |
| 764 | ElTy = STy->getElementType(); |
| 765 | IdxList.push_back(IdxList[0]); |
| 766 | } else { |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 770 | |
| 771 | if (ElTy == DPTy->getElementType()) |
| 772 | return ConstantExpr::getGetElementPtr( |
| 773 | const_cast<Constant*>(V),IdxList); |
| 774 | } |
| 775 | |
| 776 | // Handle casts from one packed constant to another. We know that the src |
| 777 | // and dest type have the same size (otherwise its an illegal cast). |
| 778 | if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { |
| 779 | if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) { |
| 780 | assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() && |
| 781 | "Not cast between same sized vectors!"); |
| 782 | // First, check for null and undef |
| 783 | if (isa<ConstantAggregateZero>(V)) |
| 784 | return Constant::getNullValue(DestTy); |
| 785 | if (isa<UndefValue>(V)) |
| 786 | return UndefValue::get(DestTy); |
| 787 | |
| 788 | if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) { |
| 789 | // This is a cast from a ConstantPacked of one type to a |
| 790 | // ConstantPacked of another type. Check to see if all elements of |
| 791 | // the input are simple. |
| 792 | bool AllSimpleConstants = true; |
| 793 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { |
| 794 | if (!isa<ConstantInt>(CP->getOperand(i)) && |
| 795 | !isa<ConstantFP>(CP->getOperand(i))) { |
| 796 | AllSimpleConstants = false; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | // If all of the elements are simple constants, we can fold this. |
| 802 | if (AllSimpleConstants) |
| 803 | return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy); |
| 804 | } |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 807 | |
Chris Lattner | 4d1da16 | 2006-12-11 18:30:27 +0000 | [diff] [blame] | 808 | // Finally, implement bitcast folding now. The code below doesn't handle |
| 809 | // bitcast right. |
| 810 | if (isa<ConstantPointerNull>(V)) // ptr->ptr cast. |
| 811 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
| 812 | |
| 813 | // Handle integral constant input. |
| 814 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 815 | // Integral -> Integral, must be changing sign. |
| 816 | if (DestTy->isIntegral()) |
| 817 | return ConstantInt::get(DestTy, CI->getZExtValue()); |
| 818 | |
| 819 | if (DestTy->isFloatingPoint()) { |
| 820 | if (DestTy == Type::FloatTy) |
| 821 | return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue())); |
| 822 | assert(DestTy == Type::DoubleTy && "Unknown FP type!"); |
| 823 | return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue())); |
| 824 | } |
| 825 | // Otherwise, can't fold this (packed?) |
| 826 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 827 | } |
Chris Lattner | 4d1da16 | 2006-12-11 18:30:27 +0000 | [diff] [blame] | 828 | |
| 829 | // Handle ConstantFP input. |
| 830 | if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) { |
| 831 | // FP -> Integral. |
| 832 | if (DestTy->isIntegral()) { |
Reid Spencer | 3db7d37 | 2006-12-11 21:27:28 +0000 | [diff] [blame] | 833 | if (DestTy == Type::IntTy || DestTy == Type::UIntTy) |
Chris Lattner | 4d1da16 | 2006-12-11 18:30:27 +0000 | [diff] [blame] | 834 | return ConstantInt::get(DestTy, FloatToBits(FP->getValue())); |
Reid Spencer | 3db7d37 | 2006-12-11 21:27:28 +0000 | [diff] [blame] | 835 | assert((DestTy == Type::LongTy || DestTy == Type::ULongTy) |
| 836 | && "Incorrect integer type for bitcast!"); |
Chris Lattner | 4d1da16 | 2006-12-11 18:30:27 +0000 | [diff] [blame] | 837 | return ConstantInt::get(DestTy, DoubleToBits(FP->getValue())); |
| 838 | } |
| 839 | } |
| 840 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 841 | default: |
| 842 | assert(!"Invalid CE CastInst opcode"); |
| 843 | break; |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 844 | } |
Chris Lattner | b2b7f90 | 2004-10-11 03:57:30 +0000 | [diff] [blame] | 845 | |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame^] | 846 | assert(0 && "Failed to cast constant expression"); |
| 847 | return 0; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 850 | Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, |
| 851 | const Constant *V1, |
| 852 | const Constant *V2) { |
Chris Lattner | 7843066 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 853 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond)) |
| 854 | return const_cast<Constant*>(CB->getValue() ? V1 : V2); |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 855 | |
| 856 | if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2); |
| 857 | if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1); |
| 858 | if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1); |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 859 | if (V1 == V2) return const_cast<Constant*>(V1); |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 860 | return 0; |
| 861 | } |
| 862 | |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 863 | Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, |
| 864 | const Constant *Idx) { |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 865 | if (isa<UndefValue>(Val)) // ee(undef, x) -> undef |
| 866 | return UndefValue::get(cast<PackedType>(Val->getType())->getElementType()); |
Chris Lattner | e4f9d7b | 2006-04-07 04:44:06 +0000 | [diff] [blame] | 867 | if (Val->isNullValue()) // ee(zero, x) -> zero |
| 868 | return Constant::getNullValue( |
| 869 | cast<PackedType>(Val->getType())->getElementType()); |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 870 | |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 871 | if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 872 | if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { |
| 873 | return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue())); |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 874 | } else if (isa<UndefValue>(Idx)) { |
| 875 | // ee({w,x,y,z}, undef) -> w (an arbitrary value). |
| 876 | return const_cast<Constant*>(CVal->getOperand(0)); |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 877 | } |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 878 | } |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 882 | Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, |
| 883 | const Constant *Elt, |
| 884 | const Constant *Idx) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 885 | const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 886 | if (!CIdx) return 0; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 887 | uint64_t idxVal = CIdx->getZExtValue(); |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 888 | if (isa<UndefValue>(Val)) { |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 889 | // Insertion of scalar constant into packed undef |
| 890 | // Optimize away insertion of undef |
| 891 | if (isa<UndefValue>(Elt)) |
| 892 | return const_cast<Constant*>(Val); |
| 893 | // Otherwise break the aggregate undef into multiple undefs and do |
| 894 | // the insertion |
| 895 | unsigned numOps = |
| 896 | cast<PackedType>(Val->getType())->getNumElements(); |
| 897 | std::vector<Constant*> Ops; |
| 898 | Ops.reserve(numOps); |
| 899 | for (unsigned i = 0; i < numOps; ++i) { |
| 900 | const Constant *Op = |
| 901 | (i == idxVal) ? Elt : UndefValue::get(Elt->getType()); |
| 902 | Ops.push_back(const_cast<Constant*>(Op)); |
| 903 | } |
| 904 | return ConstantPacked::get(Ops); |
| 905 | } |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 906 | if (isa<ConstantAggregateZero>(Val)) { |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 907 | // Insertion of scalar constant into packed aggregate zero |
| 908 | // Optimize away insertion of zero |
| 909 | if (Elt->isNullValue()) |
| 910 | return const_cast<Constant*>(Val); |
| 911 | // Otherwise break the aggregate zero into multiple zeros and do |
| 912 | // the insertion |
| 913 | unsigned numOps = |
| 914 | cast<PackedType>(Val->getType())->getNumElements(); |
| 915 | std::vector<Constant*> Ops; |
| 916 | Ops.reserve(numOps); |
| 917 | for (unsigned i = 0; i < numOps; ++i) { |
| 918 | const Constant *Op = |
| 919 | (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType()); |
| 920 | Ops.push_back(const_cast<Constant*>(Op)); |
| 921 | } |
| 922 | return ConstantPacked::get(Ops); |
| 923 | } |
| 924 | if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) { |
| 925 | // Insertion of scalar constant into packed constant |
| 926 | std::vector<Constant*> Ops; |
| 927 | Ops.reserve(CVal->getNumOperands()); |
| 928 | for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { |
| 929 | const Constant *Op = |
| 930 | (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i)); |
| 931 | Ops.push_back(const_cast<Constant*>(Op)); |
| 932 | } |
| 933 | return ConstantPacked::get(Ops); |
| 934 | } |
| 935 | return 0; |
| 936 | } |
| 937 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 938 | Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1, |
| 939 | const Constant *V2, |
| 940 | const Constant *Mask) { |
| 941 | // TODO: |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 946 | /// isZeroSizedType - This type is zero sized if its an array or structure of |
| 947 | /// zero sized types. The only leaf zero sized type is an empty structure. |
| 948 | static bool isMaybeZeroSizedType(const Type *Ty) { |
| 949 | if (isa<OpaqueType>(Ty)) return true; // Can't say. |
| 950 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 951 | |
| 952 | // If all of elements have zero size, this does too. |
| 953 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Chris Lattner | feaf92f | 2005-01-28 23:17:27 +0000 | [diff] [blame] | 954 | if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 955 | return true; |
| 956 | |
| 957 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 958 | return isMaybeZeroSizedType(ATy->getElementType()); |
| 959 | } |
| 960 | return false; |
| 961 | } |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 962 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 963 | /// IdxCompare - Compare the two constants as though they were getelementptr |
| 964 | /// indices. This allows coersion of the types to be the same thing. |
| 965 | /// |
| 966 | /// If the two constants are the "same" (after coersion), return 0. If the |
| 967 | /// first is less than the second, return -1, if the second is less than the |
| 968 | /// first, return 1. If the constants are not integral, return -2. |
| 969 | /// |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 970 | static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 971 | if (C1 == C2) return 0; |
| 972 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 973 | // Ok, we found a different index. Are either of the operands ConstantExprs? |
| 974 | // If so, we can't do anything with them. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 975 | if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) |
| 976 | return -2; // don't know! |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 978 | // Ok, we have two differing integer indices. Sign extend them to be the same |
| 979 | // type. Long is always big enough, so we use it. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 980 | if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy) |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 981 | C1 = ConstantExpr::getSExt(C1, Type::LongTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 982 | else |
| 983 | C1 = ConstantExpr::getBitCast(C1, Type::LongTy); |
| 984 | if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy) |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 985 | C2 = ConstantExpr::getSExt(C2, Type::LongTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 986 | else |
| 987 | C2 = ConstantExpr::getBitCast(C2, Type::LongTy); |
| 988 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 989 | if (C1 == C2) return 0; // Are they just differing types? |
| 990 | |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 991 | // If the type being indexed over is really just a zero sized type, there is |
| 992 | // no pointer difference being made here. |
| 993 | if (isMaybeZeroSizedType(ElTy)) |
| 994 | return -2; // dunno. |
| 995 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 996 | // If they are really different, now that they are the same type, then we |
| 997 | // found a difference! |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 998 | if (cast<ConstantInt>(C1)->getSExtValue() < |
| 999 | cast<ConstantInt>(C2)->getSExtValue()) |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1000 | return -1; |
| 1001 | else |
| 1002 | return 1; |
| 1003 | } |
| 1004 | |
| 1005 | /// evaluateRelation - This function determines if there is anything we can |
| 1006 | /// 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] | 1007 | /// things like integer comparisons, but should instead handle ConstantExprs |
Chris Lattner | 8410beb | 2006-12-11 02:16:58 +0000 | [diff] [blame] | 1008 | /// and GlobalValues. If we can determine that the two constants have a |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1009 | /// particular relation to each other, we should return the corresponding SetCC |
| 1010 | /// code, otherwise return Instruction::BinaryOpsEnd. |
| 1011 | /// |
| 1012 | /// To simplify this code we canonicalize the relation so that the first |
| 1013 | /// operand is always the most "complex" of the two. We consider simple |
| 1014 | /// constants (like ConstantInt) to be the simplest, followed by |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1015 | /// GlobalValues, followed by ConstantExpr's (the most complex). |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1016 | /// |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1017 | static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1018 | assert(V1->getType() == V2->getType() && |
| 1019 | "Cannot compare different types of values!"); |
| 1020 | if (V1 == V2) return Instruction::SetEQ; |
| 1021 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1022 | if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) { |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1023 | if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) { |
| 1024 | // We distilled this down to a simple case, use the standard constant |
| 1025 | // folder. |
| 1026 | ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2)); |
Chris Lattner | 7843066 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1027 | if (R && R->getValue()) return Instruction::SetEQ; |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1028 | R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2)); |
Chris Lattner | 7843066 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1029 | if (R && R->getValue()) return Instruction::SetLT; |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1030 | R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2)); |
Chris Lattner | 7843066 | 2006-09-28 23:34:49 +0000 | [diff] [blame] | 1031 | if (R && R->getValue()) return Instruction::SetGT; |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1032 | |
| 1033 | // If we couldn't figure it out, bail. |
| 1034 | return Instruction::BinaryOpsEnd; |
| 1035 | } |
| 1036 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1037 | // If the first operand is simple, swap operands. |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1038 | Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); |
| 1039 | if (SwappedRelation != Instruction::BinaryOpsEnd) |
| 1040 | return SetCondInst::getSwappedCondition(SwappedRelation); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1041 | |
Chris Lattner | 0f7e9f5 | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1042 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) { |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1043 | if (isa<ConstantExpr>(V2)) { // Swap as necessary. |
Chris Lattner | 0f7e9f5 | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1044 | Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1); |
| 1045 | if (SwappedRelation != Instruction::BinaryOpsEnd) |
| 1046 | return SetCondInst::getSwappedCondition(SwappedRelation); |
| 1047 | else |
| 1048 | return Instruction::BinaryOpsEnd; |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1049 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1050 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1051 | // Now we know that the RHS is a GlobalValue or simple constant, |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1052 | // which (since the types must match) means that it's a ConstantPointerNull. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1053 | if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1054 | if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage()) |
| 1055 | return Instruction::SetNE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1056 | } else { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1057 | // GlobalVals can never be null. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1058 | assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1059 | if (!CPR1->hasExternalWeakLinkage()) |
| 1060 | return Instruction::SetNE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1061 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1062 | } else { |
| 1063 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 1064 | // constantexpr, a CPR, or a simple constant. |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1065 | ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1066 | Constant *CE1Op0 = CE1->getOperand(0); |
| 1067 | |
| 1068 | switch (CE1->getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1069 | case Instruction::Trunc: |
| 1070 | case Instruction::FPTrunc: |
| 1071 | case Instruction::FPExt: |
| 1072 | case Instruction::FPToUI: |
| 1073 | case Instruction::FPToSI: |
| 1074 | break; // We don't do anything with floating point. |
| 1075 | case Instruction::ZExt: |
| 1076 | case Instruction::SExt: |
| 1077 | case Instruction::UIToFP: |
| 1078 | case Instruction::SIToFP: |
| 1079 | case Instruction::PtrToInt: |
| 1080 | case Instruction::IntToPtr: |
| 1081 | case Instruction::BitCast: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1082 | // If the cast is not actually changing bits, and the second operand is a |
| 1083 | // null pointer, do the comparison with the pre-casted value. |
| 1084 | if (V2->isNullValue() && |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1085 | (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral())) |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1086 | return evaluateRelation(CE1Op0, |
| 1087 | Constant::getNullValue(CE1Op0->getType())); |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1088 | |
| 1089 | // If the dest type is a pointer type, and the RHS is a constantexpr cast |
| 1090 | // from the same type as the src of the LHS, evaluate the inputs. This is |
| 1091 | // important for things like "seteq (cast 4 to int*), (cast 5 to int*)", |
| 1092 | // which happens a lot in compilers with tagged integers. |
| 1093 | if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1094 | if (isa<PointerType>(CE1->getType()) && CE2->isCast() && |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1095 | CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() && |
| 1096 | CE1->getOperand(0)->getType()->isIntegral()) { |
| 1097 | return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0)); |
| 1098 | } |
Chris Lattner | 192e326 | 2004-04-11 01:29:30 +0000 | [diff] [blame] | 1099 | break; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1100 | |
| 1101 | case Instruction::GetElementPtr: |
| 1102 | // Ok, since this is a getelementptr, we know that the constant has a |
| 1103 | // pointer type. Check the various cases. |
| 1104 | if (isa<ConstantPointerNull>(V2)) { |
| 1105 | // If we are comparing a GEP to a null pointer, check to see if the base |
| 1106 | // of the GEP equals the null pointer. |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1107 | if (GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { |
| 1108 | if (GV->hasExternalWeakLinkage()) |
| 1109 | // Weak linkage GVals could be zero or not. We're comparing that |
| 1110 | // to null pointer so its greater-or-equal |
| 1111 | return Instruction::SetGE; |
| 1112 | else |
| 1113 | // If its not weak linkage, the GVal must have a non-zero address |
| 1114 | // so the result is greater-than |
| 1115 | return Instruction::SetGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1116 | } else if (isa<ConstantPointerNull>(CE1Op0)) { |
| 1117 | // If we are indexing from a null pointer, check to see if we have any |
| 1118 | // non-zero indices. |
| 1119 | for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) |
| 1120 | if (!CE1->getOperand(i)->isNullValue()) |
| 1121 | // Offsetting from null, must not be equal. |
| 1122 | return Instruction::SetGT; |
| 1123 | // Only zero indexes from null, must still be zero. |
| 1124 | return Instruction::SetEQ; |
| 1125 | } |
| 1126 | // 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] | 1127 | } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1128 | if (isa<ConstantPointerNull>(CE1Op0)) { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1129 | if (CPR2->hasExternalWeakLinkage()) |
| 1130 | // Weak linkage GVals could be zero or not. We're comparing it to |
| 1131 | // a null pointer, so its less-or-equal |
| 1132 | return Instruction::SetLE; |
| 1133 | else |
| 1134 | // If its not weak linkage, the GVal must have a non-zero address |
| 1135 | // so the result is less-than |
| 1136 | return Instruction::SetLT; |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1137 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1138 | if (CPR1 == CPR2) { |
| 1139 | // If this is a getelementptr of the same global, then it must be |
| 1140 | // different. Because the types must match, the getelementptr could |
| 1141 | // only have at most one index, and because we fold getelementptr's |
| 1142 | // with a single zero index, it must be nonzero. |
| 1143 | assert(CE1->getNumOperands() == 2 && |
| 1144 | !CE1->getOperand(1)->isNullValue() && |
| 1145 | "Suprising getelementptr!"); |
| 1146 | return Instruction::SetGT; |
| 1147 | } else { |
| 1148 | // If they are different globals, we don't know what the value is, |
| 1149 | // but they can't be equal. |
| 1150 | return Instruction::SetNE; |
| 1151 | } |
| 1152 | } |
| 1153 | } else { |
| 1154 | const ConstantExpr *CE2 = cast<ConstantExpr>(V2); |
| 1155 | const Constant *CE2Op0 = CE2->getOperand(0); |
| 1156 | |
| 1157 | // There are MANY other foldings that we could perform here. They will |
| 1158 | // probably be added on demand, as they seem needed. |
| 1159 | switch (CE2->getOpcode()) { |
| 1160 | default: break; |
| 1161 | case Instruction::GetElementPtr: |
| 1162 | // By far the most common case to handle is when the base pointers are |
| 1163 | // obviously to the same or different globals. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1164 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1165 | if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal |
| 1166 | return Instruction::SetNE; |
| 1167 | // Ok, we know that both getelementptr instructions are based on the |
| 1168 | // same global. From this, we can precisely determine the relative |
| 1169 | // ordering of the resultant pointers. |
| 1170 | unsigned i = 1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1172 | // Compare all of the operands the GEP's have in common. |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1173 | gep_type_iterator GTI = gep_type_begin(CE1); |
| 1174 | for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); |
| 1175 | ++i, ++GTI) |
| 1176 | switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i), |
| 1177 | GTI.getIndexedType())) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1178 | case -1: return Instruction::SetLT; |
| 1179 | case 1: return Instruction::SetGT; |
| 1180 | case -2: return Instruction::BinaryOpsEnd; |
| 1181 | } |
| 1182 | |
| 1183 | // Ok, we ran out of things they have in common. If any leftovers |
| 1184 | // are non-zero then we have a difference, otherwise we are equal. |
| 1185 | for (; i < CE1->getNumOperands(); ++i) |
| 1186 | if (!CE1->getOperand(i)->isNullValue()) |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1187 | if (isa<ConstantIntegral>(CE1->getOperand(i))) |
| 1188 | return Instruction::SetGT; |
| 1189 | else |
| 1190 | return Instruction::BinaryOpsEnd; // Might be equal. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1192 | for (; i < CE2->getNumOperands(); ++i) |
| 1193 | if (!CE2->getOperand(i)->isNullValue()) |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1194 | if (isa<ConstantIntegral>(CE2->getOperand(i))) |
| 1195 | return Instruction::SetLT; |
| 1196 | else |
| 1197 | return Instruction::BinaryOpsEnd; // Might be equal. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1198 | return Instruction::SetEQ; |
| 1199 | } |
| 1200 | } |
| 1201 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1203 | default: |
| 1204 | break; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | return Instruction::BinaryOpsEnd; |
| 1209 | } |
| 1210 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1211 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 1212 | const Constant *V1, |
| 1213 | const Constant *V2) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1214 | Constant *C = 0; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1215 | switch (Opcode) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1216 | default: break; |
| 1217 | case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break; |
| 1218 | case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break; |
| 1219 | case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1220 | case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break; |
| 1221 | case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break; |
| 1222 | case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1223 | case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break; |
| 1224 | case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break; |
| 1225 | case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1226 | case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break; |
| 1227 | case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break; |
| 1228 | case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break; |
| 1229 | case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1230 | case Instruction::LShr: C = ConstRules::get(V1, V2).lshr(V1, V2); break; |
| 1231 | case Instruction::AShr: C = ConstRules::get(V1, V2).ashr(V1, V2); break; |
Reid Spencer | 6f05d73 | 2006-12-01 03:56:30 +0000 | [diff] [blame] | 1232 | case Instruction::SetEQ: |
| 1233 | // SetEQ(null,GV) -> false |
| 1234 | if (V1->isNullValue()) { |
| 1235 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2)) |
| 1236 | if (!GV->hasExternalWeakLinkage()) |
| 1237 | return ConstantBool::getFalse(); |
| 1238 | // SetEQ(GV,null) -> false |
| 1239 | } else if (V2->isNullValue()) { |
| 1240 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) |
| 1241 | if (!GV->hasExternalWeakLinkage()) |
| 1242 | return ConstantBool::getFalse(); |
| 1243 | } |
| 1244 | C = ConstRules::get(V1, V2).equalto(V1, V2); |
| 1245 | break; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1246 | case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break; |
| 1247 | case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break; |
Reid Spencer | 6f05d73 | 2006-12-01 03:56:30 +0000 | [diff] [blame] | 1248 | case Instruction::SetNE: |
| 1249 | // SetNE(null,GV) -> true |
| 1250 | if (V1->isNullValue()) { |
| 1251 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2)) |
| 1252 | if (!GV->hasExternalWeakLinkage()) |
| 1253 | return ConstantBool::getTrue(); |
| 1254 | // SetNE(GV,null) -> true |
| 1255 | } else if (V2->isNullValue()) { |
| 1256 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) |
| 1257 | if (!GV->hasExternalWeakLinkage()) |
| 1258 | return ConstantBool::getTrue(); |
| 1259 | } |
| 1260 | // V1 != V2 === !(V1 == V2) |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1261 | C = ConstRules::get(V1, V2).equalto(V1, V2); |
Chris Lattner | 6b52be6 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 1262 | if (C) return ConstantExpr::getNot(C); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | case Instruction::SetLE: // V1 <= V2 === !(V2 < V1) |
| 1265 | C = ConstRules::get(V1, V2).lessthan(V2, V1); |
Chris Lattner | 6b52be6 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 1266 | if (C) return ConstantExpr::getNot(C); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1267 | break; |
| 1268 | case Instruction::SetGE: // V1 >= V2 === !(V1 < V2) |
| 1269 | C = ConstRules::get(V1, V2).lessthan(V1, V2); |
Chris Lattner | 6b52be6 | 2006-01-04 02:20:54 +0000 | [diff] [blame] | 1270 | if (C) return ConstantExpr::getNot(C); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1271 | break; |
| 1272 | } |
| 1273 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1274 | // If we successfully folded the expression, return it now. |
| 1275 | if (C) return C; |
| 1276 | |
Chris Lattner | e1496fb | 2006-09-17 19:14:47 +0000 | [diff] [blame] | 1277 | if (SetCondInst::isComparison(Opcode)) { |
Chris Lattner | 192eacc | 2004-10-17 04:01:51 +0000 | [diff] [blame] | 1278 | if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) |
| 1279 | return UndefValue::get(Type::BoolTy); |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1280 | switch (evaluateRelation(const_cast<Constant*>(V1), |
| 1281 | const_cast<Constant*>(V2))) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1282 | default: assert(0 && "Unknown relational!"); |
| 1283 | case Instruction::BinaryOpsEnd: |
| 1284 | break; // Couldn't determine anything about these constants. |
| 1285 | case Instruction::SetEQ: // We know the constants are equal! |
| 1286 | // If we know the constants are equal, we can decide the result of this |
| 1287 | // computation precisely. |
| 1288 | return ConstantBool::get(Opcode == Instruction::SetEQ || |
| 1289 | Opcode == Instruction::SetLE || |
| 1290 | Opcode == Instruction::SetGE); |
| 1291 | case Instruction::SetLT: |
| 1292 | // If we know that V1 < V2, we can decide the result of this computation |
| 1293 | // precisely. |
| 1294 | return ConstantBool::get(Opcode == Instruction::SetLT || |
| 1295 | Opcode == Instruction::SetNE || |
| 1296 | Opcode == Instruction::SetLE); |
| 1297 | case Instruction::SetGT: |
| 1298 | // If we know that V1 > V2, we can decide the result of this computation |
| 1299 | // precisely. |
| 1300 | return ConstantBool::get(Opcode == Instruction::SetGT || |
| 1301 | Opcode == Instruction::SetNE || |
| 1302 | Opcode == Instruction::SetGE); |
| 1303 | case Instruction::SetLE: |
| 1304 | // 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] | 1305 | if (Opcode == Instruction::SetGT) return ConstantBool::getFalse(); |
| 1306 | if (Opcode == Instruction::SetLT) return ConstantBool::getTrue(); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1307 | break; |
| 1308 | |
| 1309 | case Instruction::SetGE: |
| 1310 | // 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] | 1311 | if (Opcode == Instruction::SetLT) return ConstantBool::getFalse(); |
| 1312 | if (Opcode == Instruction::SetGT) return ConstantBool::getTrue(); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1313 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1315 | case Instruction::SetNE: |
| 1316 | // 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] | 1317 | if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse(); |
| 1318 | if (Opcode == Instruction::SetNE) return ConstantBool::getTrue(); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | } |
Chris Lattner | 192eacc | 2004-10-17 04:01:51 +0000 | [diff] [blame] | 1321 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1323 | if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) { |
| 1324 | switch (Opcode) { |
| 1325 | case Instruction::Add: |
| 1326 | case Instruction::Sub: |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1327 | case Instruction::Xor: |
| 1328 | return UndefValue::get(V1->getType()); |
| 1329 | |
| 1330 | case Instruction::Mul: |
| 1331 | case Instruction::And: |
| 1332 | return Constant::getNullValue(V1->getType()); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1333 | case Instruction::UDiv: |
| 1334 | case Instruction::SDiv: |
| 1335 | case Instruction::FDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1336 | case Instruction::URem: |
| 1337 | case Instruction::SRem: |
| 1338 | case Instruction::FRem: |
| 1339 | if (!isa<UndefValue>(V2)) // undef / X -> 0 |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1340 | return Constant::getNullValue(V1->getType()); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1341 | return const_cast<Constant*>(V2); // X / undef -> undef |
| 1342 | case Instruction::Or: // X | undef -> -1 |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1343 | return ConstantInt::getAllOnesValue(V1->getType()); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1344 | case Instruction::LShr: |
| 1345 | if (isa<UndefValue>(V2) && isa<UndefValue>(V1)) |
| 1346 | return const_cast<Constant*>(V1); // undef lshr undef -> undef |
| 1347 | return Constant::getNullValue(V1->getType()); // X lshr undef -> 0 |
| 1348 | // undef lshr X -> 0 |
| 1349 | case Instruction::AShr: |
| 1350 | if (!isa<UndefValue>(V2)) |
| 1351 | return const_cast<Constant*>(V1); // undef ashr X --> undef |
| 1352 | else if (isa<UndefValue>(V1)) |
| 1353 | return const_cast<Constant*>(V1); // undef ashr undef -> undef |
| 1354 | else |
| 1355 | return const_cast<Constant*>(V1); // X ashr undef --> X |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1356 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1357 | // undef << X -> 0 or X << undef -> 0 |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 1358 | return Constant::getNullValue(V1->getType()); |
| 1359 | } |
| 1360 | } |
| 1361 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1362 | if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) { |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 1363 | if (isa<ConstantExpr>(V2)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1364 | // There are many possible foldings we could do here. We should probably |
| 1365 | // at least fold add of a pointer with an integer into the appropriate |
| 1366 | // getelementptr. This will improve alias analysis a bit. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | // Just implement a couple of simple identities. |
| 1369 | switch (Opcode) { |
| 1370 | case Instruction::Add: |
| 1371 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X |
| 1372 | break; |
| 1373 | case Instruction::Sub: |
| 1374 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X |
| 1375 | break; |
| 1376 | case Instruction::Mul: |
| 1377 | if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0 |
| 1378 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1379 | if (CI->getZExtValue() == 1) |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1380 | return const_cast<Constant*>(V1); // X * 1 == X |
| 1381 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1382 | case Instruction::UDiv: |
| 1383 | case Instruction::SDiv: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1384 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1385 | if (CI->getZExtValue() == 1) |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1386 | return const_cast<Constant*>(V1); // X / 1 == X |
| 1387 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1388 | case Instruction::URem: |
| 1389 | case Instruction::SRem: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1390 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1391 | if (CI->getZExtValue() == 1) |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1392 | return Constant::getNullValue(CI->getType()); // X % 1 == 0 |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | case Instruction::And: |
| 1395 | if (cast<ConstantIntegral>(V2)->isAllOnesValue()) |
| 1396 | return const_cast<Constant*>(V1); // X & -1 == X |
| 1397 | if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0 |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1398 | if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) { |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1399 | GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0)); |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Functions are at least 4-byte aligned. If and'ing the address of a |
| 1402 | // function with a constant < 4, fold it to zero. |
| 1403 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1404 | if (CI->getZExtValue() < 4 && isa<Function>(CPR)) |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 1405 | return Constant::getNullValue(CI->getType()); |
| 1406 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1407 | break; |
| 1408 | case Instruction::Or: |
| 1409 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X |
| 1410 | if (cast<ConstantIntegral>(V2)->isAllOnesValue()) |
| 1411 | return const_cast<Constant*>(V2); // X | -1 == -1 |
| 1412 | break; |
| 1413 | case Instruction::Xor: |
| 1414 | if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X |
| 1415 | break; |
| 1416 | } |
| 1417 | } |
| 1418 | |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 1419 | } else if (isa<ConstantExpr>(V2)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1420 | // If V2 is a constant expr and V1 isn't, flop them around and fold the |
| 1421 | // other way if possible. |
| 1422 | switch (Opcode) { |
| 1423 | case Instruction::Add: |
| 1424 | case Instruction::Mul: |
| 1425 | case Instruction::And: |
| 1426 | case Instruction::Or: |
| 1427 | case Instruction::Xor: |
| 1428 | case Instruction::SetEQ: |
| 1429 | case Instruction::SetNE: |
| 1430 | // No change of opcode required. |
| 1431 | return ConstantFoldBinaryInstruction(Opcode, V2, V1); |
| 1432 | |
| 1433 | case Instruction::SetLT: |
| 1434 | case Instruction::SetGT: |
| 1435 | case Instruction::SetLE: |
| 1436 | case Instruction::SetGE: |
| 1437 | // Change the opcode as necessary to swap the operands. |
| 1438 | Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode); |
| 1439 | return ConstantFoldBinaryInstruction(Opcode, V2, V1); |
| 1440 | |
| 1441 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1442 | case Instruction::LShr: |
| 1443 | case Instruction::AShr: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1444 | case Instruction::Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1445 | case Instruction::SDiv: |
| 1446 | case Instruction::UDiv: |
| 1447 | case Instruction::FDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1448 | case Instruction::URem: |
| 1449 | case Instruction::SRem: |
| 1450 | case Instruction::FRem: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1451 | default: // These instructions cannot be flopped around. |
| 1452 | break; |
| 1453 | } |
| 1454 | } |
| 1455 | return 0; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Reid Spencer | b471d8e | 2006-12-04 05:19:34 +0000 | [diff] [blame] | 1458 | Constant *llvm::ConstantFoldCompare( |
| 1459 | unsigned opcode, Constant *C1, Constant *C2, unsigned short predicate) |
| 1460 | { |
| 1461 | // Place holder for future folding of ICmp and FCmp instructions |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1465 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1466 | const std::vector<Value*> &IdxList) { |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1467 | if (IdxList.size() == 0 || |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1468 | (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue())) |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1469 | return const_cast<Constant*>(C); |
| 1470 | |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1471 | if (isa<UndefValue>(C)) { |
| 1472 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
| 1473 | true); |
| 1474 | assert(Ty != 0 && "Invalid indices for GEP!"); |
| 1475 | return UndefValue::get(PointerType::get(Ty)); |
| 1476 | } |
| 1477 | |
| 1478 | Constant *Idx0 = cast<Constant>(IdxList[0]); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1479 | if (C->isNullValue()) { |
| 1480 | bool isNull = true; |
| 1481 | for (unsigned i = 0, e = IdxList.size(); i != e; ++i) |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1482 | if (!cast<Constant>(IdxList[i])->isNullValue()) { |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1483 | isNull = false; |
| 1484 | break; |
| 1485 | } |
| 1486 | if (isNull) { |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1487 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1488 | true); |
| 1489 | assert(Ty != 0 && "Invalid indices for GEP!"); |
| 1490 | return ConstantPointerNull::get(PointerType::get(Ty)); |
| 1491 | } |
Chris Lattner | 4bbd409 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 1492 | |
| 1493 | if (IdxList.size() == 1) { |
| 1494 | const Type *ElTy = cast<PointerType>(C->getType())->getElementType(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1495 | if (uint32_t ElSize = ElTy->getPrimitiveSize()) { |
Chris Lattner | 4bbd409 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 1496 | // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm |
| 1497 | // type, we can statically fold this. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1498 | Constant *R = ConstantInt::get(Type::UIntTy, ElSize); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1499 | // We know R is unsigned, Idx0 is signed because it must be an index |
| 1500 | // through a sequential type (gep pointer operand) which is always |
| 1501 | // signed. |
Reid Spencer | 27720a9 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 1502 | R = ConstantExpr::getSExtOrBitCast(R, Idx0->getType()); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1503 | R = ConstantExpr::getMul(R, Idx0); // signed multiply |
| 1504 | // R is a signed integer, C is the GEP pointer so -> IntToPtr |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1505 | return ConstantExpr::getIntToPtr(R, C->getType()); |
Chris Lattner | 4bbd409 | 2004-07-15 01:16:59 +0000 | [diff] [blame] | 1506 | } |
| 1507 | } |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1508 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1509 | |
| 1510 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
| 1511 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 1512 | // is a getelementptr instruction, combine the indices of the two |
| 1513 | // getelementptr instructions into a single instruction. |
| 1514 | // |
| 1515 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1516 | const Type *LastTy = 0; |
| 1517 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 1518 | I != E; ++I) |
| 1519 | LastTy = *I; |
| 1520 | |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1521 | if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) { |
| 1522 | std::vector<Value*> NewIndices; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1523 | NewIndices.reserve(IdxList.size() + CE->getNumOperands()); |
| 1524 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1525 | NewIndices.push_back(CE->getOperand(i)); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Add the last index of the source with the first index of the new GEP. |
| 1528 | // Make sure to handle the case when they are actually different types. |
| 1529 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1530 | // Otherwise it must be an array. |
| 1531 | if (!Idx0->isNullValue()) { |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1532 | const Type *IdxTy = Combined->getType(); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1533 | if (IdxTy != Idx0->getType()) { |
Reid Spencer | 27720a9 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 1534 | Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::LongTy); |
| 1535 | Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, |
| 1536 | Type::LongTy); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1537 | Combined = ConstantExpr::get(Instruction::Add, C1, C2); |
| 1538 | } else { |
| 1539 | Combined = |
| 1540 | ConstantExpr::get(Instruction::Add, Idx0, Combined); |
| 1541 | } |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1542 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1544 | NewIndices.push_back(Combined); |
| 1545 | NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); |
| 1546 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | // Implement folding of: |
| 1551 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 1552 | // long 0, long 0) |
| 1553 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 1554 | // |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1555 | if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue()) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1556 | if (const PointerType *SPT = |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1557 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 1558 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 1559 | if (const ArrayType *CAT = |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1560 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1561 | if (CAT->getElementType() == SAT->getElementType()) |
| 1562 | return ConstantExpr::getGetElementPtr( |
| 1563 | (Constant*)CE->getOperand(0), IdxList); |
| 1564 | } |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |