Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
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 | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 15 | #include "ConstantFolding.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalValue.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/SymbolTable.h" |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Reid Spencer | cf394bf | 2004-07-04 11:51:24 +0000 | [diff] [blame] | 26 | #include <iostream> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 30 | // Constant Class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 33 | void Constant::destroyConstantImpl() { |
| 34 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 35 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 36 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 37 | // but they don't know that. Because we only find out when the CPV is |
| 38 | // deleted, we must now notify all of our users (that should only be |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 39 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 40 | // |
| 41 | while (!use_empty()) { |
| 42 | Value *V = use_back(); |
| 43 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | d9f4ac66 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 44 | if (!isa<Constant>(V)) |
| 45 | std::cerr << "While deleting: " << *this |
| 46 | << "\n\nUse still stuck around after Def is destroyed: " |
| 47 | << *V << "\n\n"; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 48 | #endif |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 49 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 50 | Constant *CV = cast<Constant>(V); |
| 51 | CV->destroyConstant(); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 52 | |
| 53 | // The constant should remove itself from our use list... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 54 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Value has no outstanding references it is safe to delete it now... |
| 58 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 59 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 61 | // Static constructor to create a '0' constant of arbitrary type... |
| 62 | Constant *Constant::getNullValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 63 | switch (Ty->getTypeID()) { |
Chris Lattner | 3e88ef9 | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 64 | case Type::BoolTyID: { |
| 65 | static Constant *NullBool = ConstantBool::get(false); |
| 66 | return NullBool; |
| 67 | } |
| 68 | case Type::SByteTyID: { |
| 69 | static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0); |
| 70 | return NullSByte; |
| 71 | } |
| 72 | case Type::UByteTyID: { |
| 73 | static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0); |
| 74 | return NullUByte; |
| 75 | } |
| 76 | case Type::ShortTyID: { |
| 77 | static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0); |
| 78 | return NullShort; |
| 79 | } |
| 80 | case Type::UShortTyID: { |
| 81 | static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0); |
| 82 | return NullUShort; |
| 83 | } |
| 84 | case Type::IntTyID: { |
| 85 | static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0); |
| 86 | return NullInt; |
| 87 | } |
| 88 | case Type::UIntTyID: { |
| 89 | static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0); |
| 90 | return NullUInt; |
| 91 | } |
| 92 | case Type::LongTyID: { |
| 93 | static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0); |
| 94 | return NullLong; |
| 95 | } |
| 96 | case Type::ULongTyID: { |
| 97 | static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0); |
| 98 | return NullULong; |
| 99 | } |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 3e88ef9 | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 101 | case Type::FloatTyID: { |
| 102 | static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0); |
| 103 | return NullFloat; |
| 104 | } |
| 105 | case Type::DoubleTyID: { |
| 106 | static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0); |
| 107 | return NullDouble; |
| 108 | } |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 109 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 110 | case Type::PointerTyID: |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 111 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | 3e88ef9 | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 113 | case Type::StructTyID: |
| 114 | case Type::ArrayTyID: |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 115 | case Type::PackedTyID: |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 116 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 117 | default: |
Reid Spencer | cf394bf | 2004-07-04 11:51:24 +0000 | [diff] [blame] | 118 | // Function, Label, or Opaque type? |
| 119 | assert(!"Cannot create a null constant of that type!"); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Static constructor to create the maximum constant of an integral type... |
| 125 | ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 126 | switch (Ty->getTypeID()) { |
Chris Lattner | a84df0a2 | 2006-09-28 23:36:21 +0000 | [diff] [blame^] | 127 | case Type::BoolTyID: return ConstantBool::getTrue(); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 128 | case Type::SByteTyID: |
| 129 | case Type::ShortTyID: |
| 130 | case Type::IntTyID: |
| 131 | case Type::LongTyID: { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 132 | // Calculate 011111111111111... |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 133 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 134 | int64_t Val = INT64_MAX; // All ones |
| 135 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 136 | return ConstantSInt::get(Ty, Val); |
| 137 | } |
| 138 | |
| 139 | case Type::UByteTyID: |
| 140 | case Type::UShortTyID: |
| 141 | case Type::UIntTyID: |
| 142 | case Type::ULongTyID: return getAllOnesValue(Ty); |
| 143 | |
Chris Lattner | 31408f7 | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 144 | default: return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | // Static constructor to create the minimum constant for an integral type... |
| 149 | ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 150 | switch (Ty->getTypeID()) { |
Chris Lattner | a84df0a2 | 2006-09-28 23:36:21 +0000 | [diff] [blame^] | 151 | case Type::BoolTyID: return ConstantBool::getFalse(); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 152 | case Type::SByteTyID: |
| 153 | case Type::ShortTyID: |
| 154 | case Type::IntTyID: |
| 155 | case Type::LongTyID: { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 156 | // Calculate 1111111111000000000000 |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 157 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 158 | int64_t Val = -1; // All ones |
| 159 | Val <<= TypeBits-1; // Shift over to the right spot |
| 160 | return ConstantSInt::get(Ty, Val); |
| 161 | } |
| 162 | |
| 163 | case Type::UByteTyID: |
| 164 | case Type::UShortTyID: |
| 165 | case Type::UIntTyID: |
| 166 | case Type::ULongTyID: return ConstantUInt::get(Ty, 0); |
| 167 | |
Chris Lattner | 31408f7 | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 168 | default: return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // Static constructor to create an integral constant with all bits set |
| 173 | ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 174 | switch (Ty->getTypeID()) { |
Chris Lattner | a84df0a2 | 2006-09-28 23:36:21 +0000 | [diff] [blame^] | 175 | case Type::BoolTyID: return ConstantBool::getTrue(); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 176 | case Type::SByteTyID: |
| 177 | case Type::ShortTyID: |
| 178 | case Type::IntTyID: |
| 179 | case Type::LongTyID: return ConstantSInt::get(Ty, -1); |
| 180 | |
| 181 | case Type::UByteTyID: |
| 182 | case Type::UShortTyID: |
| 183 | case Type::UIntTyID: |
| 184 | case Type::ULongTyID: { |
| 185 | // Calculate ~0 of the right type... |
| 186 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 187 | uint64_t Val = ~0ULL; // All ones |
| 188 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 189 | return ConstantUInt::get(Ty, Val); |
| 190 | } |
Chris Lattner | 31408f7 | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 191 | default: return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Chris Lattner | 83e5d39 | 2003-03-10 22:39:02 +0000 | [diff] [blame] | 195 | bool ConstantUInt::isAllOnesValue() const { |
| 196 | unsigned TypeBits = getType()->getPrimitiveSize()*8; |
| 197 | uint64_t Val = ~0ULL; // All ones |
| 198 | Val >>= 64-TypeBits; // Shift out inappropriate bits |
| 199 | return getValue() == Val; |
| 200 | } |
| 201 | |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 204 | // ConstantXXX Classes |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
| 206 | |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | // Normal Constructors |
| 209 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 210 | ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V) |
| 211 | : Constant(Ty, VT, 0, 0) { |
Chris Lattner | 265eb64 | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 212 | Val.Unsigned = V; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 214 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 215 | ConstantBool::ConstantBool(bool V) |
| 216 | : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) { |
Chris Lattner | 265eb64 | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 219 | ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V) |
| 220 | : ConstantIntegral(Ty, VT, V) { |
Chris Lattner | 7309d66 | 2001-07-21 19:16:08 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 222 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 223 | ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) |
| 224 | : ConstantInt(Ty, ConstantSIntVal, V) { |
Chris Lattner | 236c129 | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 225 | assert(Ty->isInteger() && Ty->isSigned() && |
Reid Spencer | f064bb2 | 2005-03-09 15:19:41 +0000 | [diff] [blame] | 226 | "Illegal type for signed integer constant!"); |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 227 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 230 | ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) |
| 231 | : ConstantInt(Ty, ConstantUIntVal, V) { |
Chris Lattner | 236c129 | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 232 | assert(Ty->isInteger() && Ty->isUnsigned() && |
| 233 | "Illegal type for unsigned integer constant!"); |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 234 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 237 | ConstantFP::ConstantFP(const Type *Ty, double V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 238 | : Constant(Ty, ConstantFPVal, 0, 0) { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 239 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 240 | Val = V; |
| 241 | } |
| 242 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 243 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 244 | const std::vector<Constant*> &V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 245 | : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) { |
Alkis Evlogimenos | 0507ffe | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 246 | assert(V.size() == T->getNumElements() && |
| 247 | "Invalid initializer vector for constant array"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 248 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 249 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 250 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 251 | Constant *C = *I; |
| 252 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 253 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 254 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 255 | "Initializer for array element doesn't match array element type!"); |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 256 | OL->init(C, this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 260 | ConstantArray::~ConstantArray() { |
| 261 | delete [] OperandList; |
| 262 | } |
| 263 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 264 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 265 | const std::vector<Constant*> &V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 266 | : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) { |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 267 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 268 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 269 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 270 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 271 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 272 | Constant *C = *I; |
| 273 | assert((C->getType() == T->getElementType(I-V.begin()) || |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 274 | ((T->getElementType(I-V.begin())->isAbstract() || |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 275 | C->getType()->isAbstract()) && |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 276 | T->getElementType(I-V.begin())->getTypeID() == |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 277 | C->getType()->getTypeID())) && |
Chris Lattner | 93c8f14 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 278 | "Initializer for struct element doesn't match struct element type!"); |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 279 | OL->init(C, this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 283 | ConstantStruct::~ConstantStruct() { |
| 284 | delete [] OperandList; |
| 285 | } |
| 286 | |
| 287 | |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 288 | ConstantPacked::ConstantPacked(const PackedType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 289 | const std::vector<Constant*> &V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 290 | : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 291 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 292 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 293 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 294 | Constant *C = *I; |
| 295 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 296 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 297 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 298 | "Initializer for packed element doesn't match packed element type!"); |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 299 | OL->init(C, this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 303 | ConstantPacked::~ConstantPacked() { |
| 304 | delete [] OperandList; |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 307 | /// UnaryConstantExpr - This class is private to Constants.cpp, and is used |
| 308 | /// behind the scenes to implement unary constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 309 | namespace { |
| 310 | class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 311 | Use Op; |
| 312 | public: |
| 313 | UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
| 314 | : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {} |
| 315 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 22ced56 | 2003-06-22 20:48:30 +0000 | [diff] [blame] | 318 | static bool isSetCC(unsigned Opcode) { |
| 319 | return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE || |
| 320 | Opcode == Instruction::SetLT || Opcode == Instruction::SetGT || |
| 321 | Opcode == Instruction::SetLE || Opcode == Instruction::SetGE; |
| 322 | } |
| 323 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 324 | /// BinaryConstantExpr - This class is private to Constants.cpp, and is used |
| 325 | /// behind the scenes to implement binary constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 326 | namespace { |
| 327 | class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 328 | Use Ops[2]; |
| 329 | public: |
| 330 | BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) |
| 331 | : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), |
| 332 | Opcode, Ops, 2) { |
| 333 | Ops[0].init(C1, this); |
| 334 | Ops[1].init(C2, this); |
| 335 | } |
| 336 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 337 | } |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 338 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 339 | /// SelectConstantExpr - This class is private to Constants.cpp, and is used |
| 340 | /// behind the scenes to implement select constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 341 | namespace { |
| 342 | class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 343 | Use Ops[3]; |
| 344 | public: |
| 345 | SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 346 | : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) { |
| 347 | Ops[0].init(C1, this); |
| 348 | Ops[1].init(C2, this); |
| 349 | Ops[2].init(C3, this); |
| 350 | } |
| 351 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 352 | } |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 353 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 354 | /// ExtractElementConstantExpr - This class is private to |
| 355 | /// Constants.cpp, and is used behind the scenes to implement |
| 356 | /// extractelement constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 357 | namespace { |
| 358 | class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr { |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 359 | Use Ops[2]; |
| 360 | public: |
| 361 | ExtractElementConstantExpr(Constant *C1, Constant *C2) |
| 362 | : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(), |
| 363 | Instruction::ExtractElement, Ops, 2) { |
| 364 | Ops[0].init(C1, this); |
| 365 | Ops[1].init(C2, this); |
| 366 | } |
| 367 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 368 | } |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 369 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 370 | /// InsertElementConstantExpr - This class is private to |
| 371 | /// Constants.cpp, and is used behind the scenes to implement |
| 372 | /// insertelement constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 373 | namespace { |
| 374 | class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr { |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 375 | Use Ops[3]; |
| 376 | public: |
| 377 | InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 378 | : ConstantExpr(C1->getType(), Instruction::InsertElement, |
| 379 | Ops, 3) { |
| 380 | Ops[0].init(C1, this); |
| 381 | Ops[1].init(C2, this); |
| 382 | Ops[2].init(C3, this); |
| 383 | } |
| 384 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 385 | } |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 386 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 387 | /// ShuffleVectorConstantExpr - This class is private to |
| 388 | /// Constants.cpp, and is used behind the scenes to implement |
| 389 | /// shufflevector constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 390 | namespace { |
| 391 | class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 392 | Use Ops[3]; |
| 393 | public: |
| 394 | ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 395 | : ConstantExpr(C1->getType(), Instruction::ShuffleVector, |
| 396 | Ops, 3) { |
| 397 | Ops[0].init(C1, this); |
| 398 | Ops[1].init(C2, this); |
| 399 | Ops[2].init(C3, this); |
| 400 | } |
| 401 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 403 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 404 | /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is |
| 405 | /// used behind the scenes to implement getelementpr constant exprs. |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 406 | namespace { |
| 407 | struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 408 | GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
| 409 | const Type *DestTy) |
| 410 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 411 | new Use[IdxList.size()+1], IdxList.size()+1) { |
| 412 | OperandList[0].init(C, this); |
| 413 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 414 | OperandList[i+1].init(IdxList[i], this); |
| 415 | } |
| 416 | ~GetElementPtrConstantExpr() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 417 | delete [] OperandList; |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 418 | } |
| 419 | }; |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 420 | } |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 422 | /// ConstantExpr::get* - Return some common constants without having to |
| 423 | /// specify the full Instruction::OPCODE identifier. |
| 424 | /// |
| 425 | Constant *ConstantExpr::getNeg(Constant *C) { |
Chris Lattner | 3cdc27c | 2004-03-29 19:51:24 +0000 | [diff] [blame] | 426 | if (!C->getType()->isFloatingPoint()) |
| 427 | return get(Instruction::Sub, getNullValue(C->getType()), C); |
| 428 | else |
| 429 | return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 430 | } |
| 431 | Constant *ConstantExpr::getNot(Constant *C) { |
| 432 | assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!"); |
| 433 | return get(Instruction::Xor, C, |
| 434 | ConstantIntegral::getAllOnesValue(C->getType())); |
| 435 | } |
| 436 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { |
| 437 | return get(Instruction::Add, C1, C2); |
| 438 | } |
| 439 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { |
| 440 | return get(Instruction::Sub, C1, C2); |
| 441 | } |
| 442 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { |
| 443 | return get(Instruction::Mul, C1, C2); |
| 444 | } |
| 445 | Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) { |
| 446 | return get(Instruction::Div, C1, C2); |
| 447 | } |
| 448 | Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) { |
| 449 | return get(Instruction::Rem, C1, C2); |
| 450 | } |
| 451 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
| 452 | return get(Instruction::And, C1, C2); |
| 453 | } |
| 454 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
| 455 | return get(Instruction::Or, C1, C2); |
| 456 | } |
| 457 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
| 458 | return get(Instruction::Xor, C1, C2); |
| 459 | } |
| 460 | Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) { |
| 461 | return get(Instruction::SetEQ, C1, C2); |
| 462 | } |
| 463 | Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) { |
| 464 | return get(Instruction::SetNE, C1, C2); |
| 465 | } |
| 466 | Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) { |
| 467 | return get(Instruction::SetLT, C1, C2); |
| 468 | } |
| 469 | Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) { |
| 470 | return get(Instruction::SetGT, C1, C2); |
| 471 | } |
| 472 | Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) { |
| 473 | return get(Instruction::SetLE, C1, C2); |
| 474 | } |
| 475 | Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) { |
| 476 | return get(Instruction::SetGE, C1, C2); |
| 477 | } |
| 478 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { |
| 479 | return get(Instruction::Shl, C1, C2); |
| 480 | } |
| 481 | Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) { |
| 482 | return get(Instruction::Shr, C1, C2); |
| 483 | } |
| 484 | |
Chris Lattner | db8bdba | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 485 | Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) { |
| 486 | if (C1->getType()->isUnsigned()) return getShr(C1, C2); |
| 487 | return getCast(getShr(getCast(C1, |
| 488 | C1->getType()->getUnsignedVersion()), C2), C1->getType()); |
| 489 | } |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 490 | |
Chris Lattner | db8bdba | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 491 | Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) { |
| 492 | if (C1->getType()->isSigned()) return getShr(C1, C2); |
| 493 | return getCast(getShr(getCast(C1, |
| 494 | C1->getType()->getSignedVersion()), C2), C1->getType()); |
| 495 | } |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 497 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 498 | /// one, but with the specified operand set to the specified value. |
| 499 | Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo, |
| 500 | Constant *Op) const { |
| 501 | assert(OpNo < getNumOperands() && "Operand num is out of range!"); |
| 502 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 503 | "Replacing operand with value of different type!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 504 | if (getOperand(OpNo) == Op) |
| 505 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 507 | Constant *Op0, *Op1, *Op2; |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 508 | switch (getOpcode()) { |
| 509 | case Instruction::Cast: |
| 510 | return ConstantExpr::getCast(Op, getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 511 | case Instruction::Select: |
| 512 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 513 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 514 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 515 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 516 | case Instruction::InsertElement: |
| 517 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 518 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 519 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 520 | return ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 521 | case Instruction::ExtractElement: |
| 522 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 523 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 524 | return ConstantExpr::getExtractElement(Op0, Op1); |
| 525 | case Instruction::ShuffleVector: |
| 526 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 527 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 528 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 529 | return ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 530 | case Instruction::GetElementPtr: { |
| 531 | std::vector<Constant*> Ops; |
| 532 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
| 533 | Ops.push_back(getOperand(i)); |
| 534 | if (OpNo == 0) |
| 535 | return ConstantExpr::getGetElementPtr(Op, Ops); |
| 536 | Ops[OpNo-1] = Op; |
| 537 | return ConstantExpr::getGetElementPtr(getOperand(0), Ops); |
| 538 | } |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 539 | default: |
| 540 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 541 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 542 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 543 | return ConstantExpr::get(getOpcode(), Op0, Op1); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /// getWithOperands - This returns the current constant expression with the |
| 548 | /// operands replaced with the specified values. The specified operands must |
| 549 | /// match count and type with the existing ones. |
| 550 | Constant *ConstantExpr:: |
| 551 | getWithOperands(const std::vector<Constant*> &Ops) const { |
| 552 | assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); |
| 553 | bool AnyChange = false; |
| 554 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 555 | assert(Ops[i]->getType() == getOperand(i)->getType() && |
| 556 | "Operand type mismatch!"); |
| 557 | AnyChange |= Ops[i] != getOperand(i); |
| 558 | } |
| 559 | if (!AnyChange) // No operands changed, return self. |
| 560 | return const_cast<ConstantExpr*>(this); |
| 561 | |
| 562 | switch (getOpcode()) { |
| 563 | case Instruction::Cast: |
| 564 | return ConstantExpr::getCast(Ops[0], getType()); |
| 565 | case Instruction::Select: |
| 566 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 567 | case Instruction::InsertElement: |
| 568 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 569 | case Instruction::ExtractElement: |
| 570 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 571 | case Instruction::ShuffleVector: |
| 572 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
| 573 | case Instruction::GetElementPtr: { |
| 574 | std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end()); |
| 575 | return ConstantExpr::getGetElementPtr(Ops[0], ActualOps); |
| 576 | } |
| 577 | default: |
| 578 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 579 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 583 | |
| 584 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 585 | // isValueValidForType implementations |
| 586 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 587 | bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 588 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 589 | default: |
| 590 | return false; // These can't be represented as integers!!! |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 591 | // Signed types... |
| 592 | case Type::SByteTyID: |
| 593 | return (Val <= INT8_MAX && Val >= INT8_MIN); |
| 594 | case Type::ShortTyID: |
| 595 | return (Val <= INT16_MAX && Val >= INT16_MIN); |
| 596 | case Type::IntTyID: |
Chris Lattner | 7424851 | 2004-06-08 23:21:39 +0000 | [diff] [blame] | 597 | return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 598 | case Type::LongTyID: |
| 599 | return true; // This is the largest type... |
| 600 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 603 | bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 604 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 605 | default: |
| 606 | return false; // These can't be represented as integers!!! |
| 607 | |
| 608 | // Unsigned types... |
| 609 | case Type::UByteTyID: |
| 610 | return (Val <= UINT8_MAX); |
| 611 | case Type::UShortTyID: |
| 612 | return (Val <= UINT16_MAX); |
| 613 | case Type::UIntTyID: |
| 614 | return (Val <= UINT32_MAX); |
| 615 | case Type::ULongTyID: |
| 616 | return true; // This is the largest type... |
| 617 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 620 | bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 621 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 622 | default: |
| 623 | return false; // These can't be represented as floating point! |
| 624 | |
Reid Spencer | b95f8ab | 2004-12-07 07:38:08 +0000 | [diff] [blame] | 625 | // TODO: Figure out how to test if a double can be cast to a float! |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 626 | case Type::FloatTyID: |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 627 | case Type::DoubleTyID: |
| 628 | return true; // This is the largest type... |
| 629 | } |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 630 | } |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 632 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 633 | // Factory Function Implementation |
| 634 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 635 | // ConstantCreator - A class that is used to create constants by |
| 636 | // ValueMap*. This class should be partially specialized if there is |
| 637 | // something strange that needs to be done to interface to the ctor for the |
| 638 | // constant. |
| 639 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 640 | namespace llvm { |
| 641 | template<class ConstantClass, class TypeClass, class ValType> |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 642 | struct VISIBILITY_HIDDEN ConstantCreator { |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 643 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
| 644 | return new ConstantClass(Ty, V); |
| 645 | } |
| 646 | }; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 648 | template<class ConstantClass, class TypeClass> |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 649 | struct VISIBILITY_HIDDEN ConvertConstantType { |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 650 | static void convert(ConstantClass *OldC, const TypeClass *NewTy) { |
| 651 | assert(0 && "This type cannot be converted!\n"); |
| 652 | abort(); |
| 653 | } |
| 654 | }; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 656 | template<class ValType, class TypeClass, class ConstantClass, |
| 657 | bool HasLargeKey = false /*true for arrays and structs*/ > |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 658 | class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser { |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 659 | public: |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 660 | typedef std::pair<const Type*, ValType> MapKey; |
| 661 | typedef std::map<MapKey, Constant *> MapTy; |
| 662 | typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy; |
| 663 | typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy; |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 664 | private: |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 665 | /// Map - This is the main map from the element descriptor to the Constants. |
| 666 | /// This is the primary way we avoid creating two of the same shape |
| 667 | /// constant. |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 668 | MapTy Map; |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 669 | |
| 670 | /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping |
| 671 | /// from the constants to their element in Map. This is important for |
| 672 | /// removal of constants from the array, which would otherwise have to scan |
| 673 | /// through the map with very large keys. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 674 | InverseMapTy InverseMap; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 675 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 676 | /// AbstractTypeMap - Map for abstract type constants. |
| 677 | /// |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 678 | AbstractTypeMapTy AbstractTypeMap; |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 680 | private: |
| 681 | void clear(std::vector<Constant *> &Constants) { |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 682 | for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I) |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 683 | Constants.push_back(I->second); |
| 684 | Map.clear(); |
| 685 | AbstractTypeMap.clear(); |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 686 | InverseMap.clear(); |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 689 | public: |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 690 | typename MapTy::iterator map_end() { return Map.end(); } |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 691 | |
| 692 | /// InsertOrGetItem - Return an iterator for the specified element. |
| 693 | /// If the element exists in the map, the returned iterator points to the |
| 694 | /// entry and Exists=true. If not, the iterator points to the newly |
| 695 | /// inserted entry and returns Exists=false. Newly inserted entries have |
| 696 | /// I->second == 0, and should be filled in. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 697 | typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *> |
| 698 | &InsertVal, |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 699 | bool &Exists) { |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 700 | std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 701 | Exists = !IP.second; |
| 702 | return IP.first; |
| 703 | } |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 705 | private: |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 706 | typename MapTy::iterator FindExistingElement(ConstantClass *CP) { |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 707 | if (HasLargeKey) { |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 708 | typename InverseMapTy::iterator IMI = InverseMap.find(CP); |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 709 | assert(IMI != InverseMap.end() && IMI->second != Map.end() && |
| 710 | IMI->second->second == CP && |
| 711 | "InverseMap corrupt!"); |
| 712 | return IMI->second; |
| 713 | } |
| 714 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 715 | typename MapTy::iterator I = |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 716 | Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP))); |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 717 | if (I == Map.end() || I->second != CP) { |
| 718 | // FIXME: This should not use a linear scan. If this gets to be a |
| 719 | // performance problem, someone should look at this. |
| 720 | for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) |
| 721 | /* empty */; |
| 722 | } |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 723 | return I; |
| 724 | } |
| 725 | public: |
| 726 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 727 | /// getOrCreate - Return the specified constant from the map, creating it if |
| 728 | /// necessary. |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 729 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 730 | MapKey Lookup(Ty, V); |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 731 | typename MapTy::iterator I = Map.lower_bound(Lookup); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 732 | if (I != Map.end() && I->first == Lookup) |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 733 | return static_cast<ConstantClass *>(I->second); // Is it in the map? |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 734 | |
| 735 | // If no preexisting value, create one now... |
| 736 | ConstantClass *Result = |
| 737 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 738 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 739 | /// FIXME: why does this assert fail when loading 176.gcc? |
| 740 | //assert(Result->getType() == Ty && "Type specified is not correct!"); |
| 741 | I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); |
| 742 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 743 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 744 | InverseMap.insert(std::make_pair(Result, I)); |
| 745 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 746 | // If the type of the constant is abstract, make sure that an entry exists |
| 747 | // for it in the AbstractTypeMap. |
| 748 | if (Ty->isAbstract()) { |
| 749 | typename AbstractTypeMapTy::iterator TI = |
| 750 | AbstractTypeMap.lower_bound(Ty); |
| 751 | |
| 752 | if (TI == AbstractTypeMap.end() || TI->first != Ty) { |
| 753 | // Add ourselves to the ATU list of the type. |
| 754 | cast<DerivedType>(Ty)->addAbstractTypeUser(this); |
| 755 | |
| 756 | AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); |
| 757 | } |
| 758 | } |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 759 | return Result; |
| 760 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 762 | void remove(ConstantClass *CP) { |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 763 | typename MapTy::iterator I = FindExistingElement(CP); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 764 | assert(I != Map.end() && "Constant not found in constant table!"); |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 765 | assert(I->second == CP && "Didn't find correct element?"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 767 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 768 | InverseMap.erase(CP); |
| 769 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 770 | // Now that we found the entry, make sure this isn't the entry that |
| 771 | // the AbstractTypeMap points to. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 772 | const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 773 | if (Ty->isAbstract()) { |
| 774 | assert(AbstractTypeMap.count(Ty) && |
| 775 | "Abstract type not in AbstractTypeMap?"); |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 776 | typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty]; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 777 | if (ATMEntryIt == I) { |
| 778 | // Yes, we are removing the representative entry for this type. |
| 779 | // See if there are any other entries of the same type. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 780 | typename MapTy::iterator TmpIt = ATMEntryIt; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 781 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 782 | // First check the entry before this one... |
| 783 | if (TmpIt != Map.begin()) { |
| 784 | --TmpIt; |
| 785 | if (TmpIt->first.first != Ty) // Not the same type, move back... |
| 786 | ++TmpIt; |
| 787 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 788 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 789 | // If we didn't find the same type, try to move forward... |
| 790 | if (TmpIt == ATMEntryIt) { |
| 791 | ++TmpIt; |
| 792 | if (TmpIt == Map.end() || TmpIt->first.first != Ty) |
| 793 | --TmpIt; // No entry afterwards with the same type |
| 794 | } |
| 795 | |
| 796 | // If there is another entry in the map of the same abstract type, |
| 797 | // update the AbstractTypeMap entry now. |
| 798 | if (TmpIt != ATMEntryIt) { |
| 799 | ATMEntryIt = TmpIt; |
| 800 | } else { |
| 801 | // Otherwise, we are removing the last instance of this type |
| 802 | // from the table. Remove from the ATM, and from user list. |
| 803 | cast<DerivedType>(Ty)->removeAbstractTypeUser(this); |
| 804 | AbstractTypeMap.erase(Ty); |
| 805 | } |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 806 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 807 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 808 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 809 | Map.erase(I); |
| 810 | } |
| 811 | |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 812 | |
| 813 | /// MoveConstantToNewSlot - If we are about to change C to be the element |
| 814 | /// specified by I, update our internal data structures to reflect this |
| 815 | /// fact. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 816 | void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) { |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 817 | // First, remove the old location of the specified constant in the map. |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 818 | typename MapTy::iterator OldI = FindExistingElement(C); |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 819 | assert(OldI != Map.end() && "Constant not found in constant table!"); |
| 820 | assert(OldI->second == C && "Didn't find correct element?"); |
| 821 | |
| 822 | // If this constant is the representative element for its abstract type, |
| 823 | // update the AbstractTypeMap so that the representative element is I. |
| 824 | if (C->getType()->isAbstract()) { |
| 825 | typename AbstractTypeMapTy::iterator ATI = |
| 826 | AbstractTypeMap.find(C->getType()); |
| 827 | assert(ATI != AbstractTypeMap.end() && |
| 828 | "Abstract type not in AbstractTypeMap?"); |
| 829 | if (ATI->second == OldI) |
| 830 | ATI->second = I; |
| 831 | } |
| 832 | |
| 833 | // Remove the old entry from the map. |
| 834 | Map.erase(OldI); |
| 835 | |
| 836 | // Update the inverse map so that we know that this constant is now |
| 837 | // located at descriptor I. |
| 838 | if (HasLargeKey) { |
| 839 | assert(I->second == C && "Bad inversemap entry!"); |
| 840 | InverseMap[C] = I; |
| 841 | } |
| 842 | } |
| 843 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 844 | void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 845 | typename AbstractTypeMapTy::iterator I = |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 846 | AbstractTypeMap.find(cast<Type>(OldTy)); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 847 | |
| 848 | assert(I != AbstractTypeMap.end() && |
| 849 | "Abstract type not in AbstractTypeMap?"); |
| 850 | |
| 851 | // Convert a constant at a time until the last one is gone. The last one |
| 852 | // leaving will remove() itself, causing the AbstractTypeMapEntry to be |
| 853 | // eliminated eventually. |
| 854 | do { |
| 855 | ConvertConstantType<ConstantClass, |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 856 | TypeClass>::convert( |
| 857 | static_cast<ConstantClass *>(I->second->second), |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 858 | cast<TypeClass>(NewTy)); |
| 859 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 860 | I = AbstractTypeMap.find(cast<Type>(OldTy)); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 861 | } while (I != AbstractTypeMap.end()); |
| 862 | } |
| 863 | |
| 864 | // If the type became concrete without being refined to any other existing |
| 865 | // type, we just remove ourselves from the ATU list. |
| 866 | void typeBecameConcrete(const DerivedType *AbsTy) { |
| 867 | AbsTy->removeAbstractTypeUser(this); |
| 868 | } |
| 869 | |
| 870 | void dump() const { |
| 871 | std::cerr << "Constant.cpp: ValueMap\n"; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 872 | } |
| 873 | }; |
| 874 | } |
| 875 | |
Chris Lattner | a84df0a2 | 2006-09-28 23:36:21 +0000 | [diff] [blame^] | 876 | |
| 877 | //---- ConstantBool::get*() implementation. |
| 878 | |
| 879 | ConstantBool *ConstantBool::getTrue() { |
| 880 | static ConstantBool *T = 0; |
| 881 | if (T) return T; |
| 882 | return T = new ConstantBool(true); |
| 883 | } |
| 884 | ConstantBool *ConstantBool::getFalse() { |
| 885 | static ConstantBool *F = 0; |
| 886 | if (F) return F; |
| 887 | return F = new ConstantBool(false); |
| 888 | } |
| 889 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 890 | //---- ConstantUInt::get() and ConstantSInt::get() implementations... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 891 | // |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 892 | static ManagedStatic<ValueMap< int64_t, Type, ConstantSInt> > SIntConstants; |
| 893 | static ManagedStatic<ValueMap<uint64_t, Type, ConstantUInt> > UIntConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 894 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 895 | ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 896 | return SIntConstants->getOrCreate(Ty, V); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 899 | ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 900 | return UIntConstants->getOrCreate(Ty, V); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 903 | ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) { |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 904 | assert(V <= 127 && "Can only be used with very small positive constants!"); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 905 | if (Ty->isSigned()) return ConstantSInt::get(Ty, V); |
| 906 | return ConstantUInt::get(Ty, V); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 909 | //---- ConstantFP::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 910 | // |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 911 | namespace llvm { |
| 912 | template<> |
| 913 | struct ConstantCreator<ConstantFP, Type, uint64_t> { |
| 914 | static ConstantFP *create(const Type *Ty, uint64_t V) { |
| 915 | assert(Ty == Type::DoubleTy); |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 916 | return new ConstantFP(Ty, BitsToDouble(V)); |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 917 | } |
| 918 | }; |
| 919 | template<> |
| 920 | struct ConstantCreator<ConstantFP, Type, uint32_t> { |
| 921 | static ConstantFP *create(const Type *Ty, uint32_t V) { |
| 922 | assert(Ty == Type::FloatTy); |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 923 | return new ConstantFP(Ty, BitsToFloat(V)); |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 924 | } |
| 925 | }; |
| 926 | } |
| 927 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 928 | static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants; |
| 929 | static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 930 | |
Jim Laskey | 8ad8f71 | 2005-08-17 20:06:22 +0000 | [diff] [blame] | 931 | bool ConstantFP::isNullValue() const { |
| 932 | return DoubleToBits(Val) == 0; |
| 933 | } |
| 934 | |
| 935 | bool ConstantFP::isExactlyValue(double V) const { |
| 936 | return DoubleToBits(V) == DoubleToBits(Val); |
| 937 | } |
| 938 | |
| 939 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 940 | ConstantFP *ConstantFP::get(const Type *Ty, double V) { |
Chris Lattner | 241ed4c | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 941 | if (Ty == Type::FloatTy) { |
| 942 | // Force the value through memory to normalize it. |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 943 | return FloatConstants->getOrCreate(Ty, FloatToBits(V)); |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 944 | } else { |
| 945 | assert(Ty == Type::DoubleTy); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 946 | return DoubleConstants->getOrCreate(Ty, DoubleToBits(V)); |
Chris Lattner | 241ed4c | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 947 | } |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 950 | //---- ConstantAggregateZero::get() implementation... |
| 951 | // |
| 952 | namespace llvm { |
| 953 | // ConstantAggregateZero does not take extra "value" argument... |
| 954 | template<class ValType> |
| 955 | struct ConstantCreator<ConstantAggregateZero, Type, ValType> { |
| 956 | static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ |
| 957 | return new ConstantAggregateZero(Ty); |
| 958 | } |
| 959 | }; |
| 960 | |
| 961 | template<> |
| 962 | struct ConvertConstantType<ConstantAggregateZero, Type> { |
| 963 | static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { |
| 964 | // Make everyone now use a constant of the new type... |
| 965 | Constant *New = ConstantAggregateZero::get(NewTy); |
| 966 | assert(New != OldC && "Didn't replace constant??"); |
| 967 | OldC->uncheckedReplaceAllUsesWith(New); |
| 968 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 969 | } |
| 970 | }; |
| 971 | } |
| 972 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 973 | static ManagedStatic<ValueMap<char, Type, |
| 974 | ConstantAggregateZero> > AggZeroConstants; |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 975 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 976 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 977 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 978 | Constant *ConstantAggregateZero::get(const Type *Ty) { |
Chris Lattner | bfd0b6d | 2006-06-10 04:16:23 +0000 | [diff] [blame] | 979 | assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) && |
| 980 | "Cannot create an aggregate zero of non-aggregate type!"); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 981 | return AggZeroConstants->getOrCreate(Ty, 0); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // destroyConstant - Remove the constant from the constant table... |
| 985 | // |
| 986 | void ConstantAggregateZero::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 987 | AggZeroConstants->remove(this); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 988 | destroyConstantImpl(); |
| 989 | } |
| 990 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 991 | //---- ConstantArray::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 992 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 993 | namespace llvm { |
| 994 | template<> |
| 995 | struct ConvertConstantType<ConstantArray, ArrayType> { |
| 996 | static void convert(ConstantArray *OldC, const ArrayType *NewTy) { |
| 997 | // Make everyone now use a constant of the new type... |
| 998 | std::vector<Constant*> C; |
| 999 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1000 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1001 | Constant *New = ConstantArray::get(NewTy, C); |
| 1002 | assert(New != OldC && "Didn't replace constant??"); |
| 1003 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1004 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1005 | } |
| 1006 | }; |
| 1007 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1009 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 1010 | std::vector<Constant*> Elements; |
| 1011 | Elements.reserve(CA->getNumOperands()); |
| 1012 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1013 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 1014 | return Elements; |
| 1015 | } |
| 1016 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1017 | typedef ValueMap<std::vector<Constant*>, ArrayType, |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1018 | ConstantArray, true /*largekey*/> ArrayConstantsTy; |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1019 | static ManagedStatic<ArrayConstantsTy> ArrayConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1021 | Constant *ConstantArray::get(const ArrayType *Ty, |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1022 | const std::vector<Constant*> &V) { |
| 1023 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 1024 | if (!V.empty()) { |
| 1025 | Constant *C = V[0]; |
| 1026 | if (!C->isNullValue()) |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1027 | return ArrayConstants->getOrCreate(Ty, V); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1028 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 1029 | if (V[i] != C) |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1030 | return ArrayConstants->getOrCreate(Ty, V); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1031 | } |
| 1032 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1035 | // destroyConstant - Remove the constant from the constant table... |
| 1036 | // |
| 1037 | void ConstantArray::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1038 | ArrayConstants->remove(this); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1039 | destroyConstantImpl(); |
| 1040 | } |
| 1041 | |
Reid Spencer | 6f61453 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1042 | /// ConstantArray::get(const string&) - Return an array that is initialized to |
| 1043 | /// contain the specified string. If length is zero then a null terminator is |
| 1044 | /// added to the specified string so that it may be used in a natural way. |
| 1045 | /// Otherwise, the length parameter specifies how much of the string to use |
| 1046 | /// and it won't be null terminated. |
| 1047 | /// |
Reid Spencer | 82ebaba | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1048 | Constant *ConstantArray::get(const std::string &Str, bool AddNull) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1049 | std::vector<Constant*> ElementVals; |
Reid Spencer | 82ebaba | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1050 | for (unsigned i = 0; i < Str.length(); ++i) |
Chris Lattner | bec9b0b | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 1051 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 1052 | |
| 1053 | // Add a null terminator to the string... |
Reid Spencer | 82ebaba | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1054 | if (AddNull) { |
Reid Spencer | 6f61453 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1055 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0)); |
Reid Spencer | 6f61453 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1056 | } |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 1057 | |
Reid Spencer | 82ebaba | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1058 | ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size()); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1059 | return ConstantArray::get(ATy, ElementVals); |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1062 | /// isString - This method returns true if the array is an array of sbyte or |
| 1063 | /// ubyte, and if the elements of the array are all ConstantInt's. |
| 1064 | bool ConstantArray::isString() const { |
| 1065 | // Check the element type for sbyte or ubyte... |
Chris Lattner | e8701f6 | 2004-01-14 17:51:53 +0000 | [diff] [blame] | 1066 | if (getType()->getElementType() != Type::UByteTy && |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1067 | getType()->getElementType() != Type::SByteTy) |
| 1068 | return false; |
| 1069 | // Check the elements to make sure they are all integers, not constant |
| 1070 | // expressions. |
| 1071 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1072 | if (!isa<ConstantInt>(getOperand(i))) |
| 1073 | return false; |
| 1074 | return true; |
| 1075 | } |
| 1076 | |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1077 | // getAsString - If the sub-element type of this array is either sbyte or ubyte, |
| 1078 | // then this method converts the array to an std::string and returns it. |
| 1079 | // Otherwise, it asserts out. |
| 1080 | // |
| 1081 | std::string ConstantArray::getAsString() const { |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1082 | assert(isString() && "Not a string!"); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1083 | std::string Result; |
Chris Lattner | 6077c31 | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 1084 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1085 | Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue(); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1086 | return Result; |
| 1087 | } |
| 1088 | |
| 1089 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1090 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1091 | // |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1093 | namespace llvm { |
| 1094 | template<> |
| 1095 | struct ConvertConstantType<ConstantStruct, StructType> { |
| 1096 | static void convert(ConstantStruct *OldC, const StructType *NewTy) { |
| 1097 | // Make everyone now use a constant of the new type... |
| 1098 | std::vector<Constant*> C; |
| 1099 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1100 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1101 | Constant *New = ConstantStruct::get(NewTy, C); |
| 1102 | assert(New != OldC && "Didn't replace constant??"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1104 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1105 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1106 | } |
| 1107 | }; |
| 1108 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1110 | typedef ValueMap<std::vector<Constant*>, StructType, |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1111 | ConstantStruct, true /*largekey*/> StructConstantsTy; |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1112 | static ManagedStatic<StructConstantsTy> StructConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1114 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 1115 | std::vector<Constant*> Elements; |
| 1116 | Elements.reserve(CS->getNumOperands()); |
| 1117 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 1118 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 1119 | return Elements; |
| 1120 | } |
| 1121 | |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1122 | Constant *ConstantStruct::get(const StructType *Ty, |
| 1123 | const std::vector<Constant*> &V) { |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1124 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 1125 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1126 | if (!V[i]->isNullValue()) |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1127 | return StructConstants->getOrCreate(Ty, V); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1128 | |
| 1129 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1130 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1131 | |
Chris Lattner | d6108ca | 2004-07-12 20:35:11 +0000 | [diff] [blame] | 1132 | Constant *ConstantStruct::get(const std::vector<Constant*> &V) { |
| 1133 | std::vector<const Type*> StructEls; |
| 1134 | StructEls.reserve(V.size()); |
| 1135 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1136 | StructEls.push_back(V[i]->getType()); |
| 1137 | return get(StructType::get(StructEls), V); |
| 1138 | } |
| 1139 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1140 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1141 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1142 | void ConstantStruct::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1143 | StructConstants->remove(this); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1144 | destroyConstantImpl(); |
| 1145 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1146 | |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1147 | //---- ConstantPacked::get() implementation... |
| 1148 | // |
| 1149 | namespace llvm { |
| 1150 | template<> |
| 1151 | struct ConvertConstantType<ConstantPacked, PackedType> { |
| 1152 | static void convert(ConstantPacked *OldC, const PackedType *NewTy) { |
| 1153 | // Make everyone now use a constant of the new type... |
| 1154 | std::vector<Constant*> C; |
| 1155 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1156 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1157 | Constant *New = ConstantPacked::get(NewTy, C); |
| 1158 | assert(New != OldC && "Didn't replace constant??"); |
| 1159 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1160 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1161 | } |
| 1162 | }; |
| 1163 | } |
| 1164 | |
| 1165 | static std::vector<Constant*> getValType(ConstantPacked *CP) { |
| 1166 | std::vector<Constant*> Elements; |
| 1167 | Elements.reserve(CP->getNumOperands()); |
| 1168 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 1169 | Elements.push_back(CP->getOperand(i)); |
| 1170 | return Elements; |
| 1171 | } |
| 1172 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1173 | static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType, |
| 1174 | ConstantPacked> > PackedConstants; |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1175 | |
| 1176 | Constant *ConstantPacked::get(const PackedType *Ty, |
| 1177 | const std::vector<Constant*> &V) { |
| 1178 | // If this is an all-zero packed, return a ConstantAggregateZero object |
| 1179 | if (!V.empty()) { |
| 1180 | Constant *C = V[0]; |
| 1181 | if (!C->isNullValue()) |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1182 | return PackedConstants->getOrCreate(Ty, V); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1183 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 1184 | if (V[i] != C) |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1185 | return PackedConstants->getOrCreate(Ty, V); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1186 | } |
| 1187 | return ConstantAggregateZero::get(Ty); |
| 1188 | } |
| 1189 | |
| 1190 | Constant *ConstantPacked::get(const std::vector<Constant*> &V) { |
| 1191 | assert(!V.empty() && "Cannot infer type if V is empty"); |
| 1192 | return get(PackedType::get(V.front()->getType(),V.size()), V); |
| 1193 | } |
| 1194 | |
| 1195 | // destroyConstant - Remove the constant from the constant table... |
| 1196 | // |
| 1197 | void ConstantPacked::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1198 | PackedConstants->remove(this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1199 | destroyConstantImpl(); |
| 1200 | } |
| 1201 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1202 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1203 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1205 | namespace llvm { |
| 1206 | // ConstantPointerNull does not take extra "value" argument... |
| 1207 | template<class ValType> |
| 1208 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 1209 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 1210 | return new ConstantPointerNull(Ty); |
| 1211 | } |
| 1212 | }; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1214 | template<> |
| 1215 | struct ConvertConstantType<ConstantPointerNull, PointerType> { |
| 1216 | static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { |
| 1217 | // Make everyone now use a constant of the new type... |
| 1218 | Constant *New = ConstantPointerNull::get(NewTy); |
| 1219 | assert(New != OldC && "Didn't replace constant??"); |
| 1220 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1221 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1222 | } |
| 1223 | }; |
| 1224 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1226 | static ManagedStatic<ValueMap<char, PointerType, |
| 1227 | ConstantPointerNull> > NullPtrConstants; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1229 | static char getValType(ConstantPointerNull *) { |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1234 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1235 | return NullPtrConstants->getOrCreate(Ty, 0); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1238 | // destroyConstant - Remove the constant from the constant table... |
| 1239 | // |
| 1240 | void ConstantPointerNull::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1241 | NullPtrConstants->remove(this); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1242 | destroyConstantImpl(); |
| 1243 | } |
| 1244 | |
| 1245 | |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1246 | //---- UndefValue::get() implementation... |
| 1247 | // |
| 1248 | |
| 1249 | namespace llvm { |
| 1250 | // UndefValue does not take extra "value" argument... |
| 1251 | template<class ValType> |
| 1252 | struct ConstantCreator<UndefValue, Type, ValType> { |
| 1253 | static UndefValue *create(const Type *Ty, const ValType &V) { |
| 1254 | return new UndefValue(Ty); |
| 1255 | } |
| 1256 | }; |
| 1257 | |
| 1258 | template<> |
| 1259 | struct ConvertConstantType<UndefValue, Type> { |
| 1260 | static void convert(UndefValue *OldC, const Type *NewTy) { |
| 1261 | // Make everyone now use a constant of the new type. |
| 1262 | Constant *New = UndefValue::get(NewTy); |
| 1263 | assert(New != OldC && "Didn't replace constant??"); |
| 1264 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1265 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1266 | } |
| 1267 | }; |
| 1268 | } |
| 1269 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1270 | static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants; |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1271 | |
| 1272 | static char getValType(UndefValue *) { |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | UndefValue *UndefValue::get(const Type *Ty) { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1278 | return UndefValueConstants->getOrCreate(Ty, 0); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | // destroyConstant - Remove the constant from the constant table. |
| 1282 | // |
| 1283 | void UndefValue::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1284 | UndefValueConstants->remove(this); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1285 | destroyConstantImpl(); |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1291 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1292 | // |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1293 | typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1294 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1295 | namespace llvm { |
| 1296 | template<> |
| 1297 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
| 1298 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { |
| 1299 | if (V.first == Instruction::Cast) |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1300 | return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1301 | if ((V.first >= Instruction::BinaryOpsBegin && |
| 1302 | V.first < Instruction::BinaryOpsEnd) || |
| 1303 | V.first == Instruction::Shl || V.first == Instruction::Shr) |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1304 | return new BinaryConstantExpr(V.first, V.second[0], V.second[1]); |
Chris Lattner | b7897ce | 2004-03-30 22:51:03 +0000 | [diff] [blame] | 1305 | if (V.first == Instruction::Select) |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1306 | return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1307 | if (V.first == Instruction::ExtractElement) |
| 1308 | return new ExtractElementConstantExpr(V.second[0], V.second[1]); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1309 | if (V.first == Instruction::InsertElement) |
| 1310 | return new InsertElementConstantExpr(V.second[0], V.second[1], |
| 1311 | V.second[2]); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1312 | if (V.first == Instruction::ShuffleVector) |
| 1313 | return new ShuffleVectorConstantExpr(V.second[0], V.second[1], |
| 1314 | V.second[2]); |
| 1315 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1316 | assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1318 | std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end()); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1319 | return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1320 | } |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1321 | }; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1323 | template<> |
| 1324 | struct ConvertConstantType<ConstantExpr, Type> { |
| 1325 | static void convert(ConstantExpr *OldC, const Type *NewTy) { |
| 1326 | Constant *New; |
| 1327 | switch (OldC->getOpcode()) { |
| 1328 | case Instruction::Cast: |
| 1329 | New = ConstantExpr::getCast(OldC->getOperand(0), NewTy); |
| 1330 | break; |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1331 | case Instruction::Select: |
| 1332 | New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), |
| 1333 | OldC->getOperand(1), |
| 1334 | OldC->getOperand(2)); |
| 1335 | break; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1336 | case Instruction::Shl: |
| 1337 | case Instruction::Shr: |
| 1338 | New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(), |
| 1339 | OldC->getOperand(0), OldC->getOperand(1)); |
| 1340 | break; |
| 1341 | default: |
| 1342 | assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && |
| 1343 | OldC->getOpcode() < Instruction::BinaryOpsEnd); |
| 1344 | New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), |
| 1345 | OldC->getOperand(1)); |
| 1346 | break; |
| 1347 | case Instruction::GetElementPtr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1348 | // Make everyone now use a constant of the new type... |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1349 | std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); |
| 1350 | New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1351 | break; |
| 1352 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1354 | assert(New != OldC && "Didn't replace constant??"); |
| 1355 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1356 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1357 | } |
| 1358 | }; |
| 1359 | } // end namespace llvm |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1360 | |
| 1361 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1362 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1363 | std::vector<Constant*> Operands; |
| 1364 | Operands.reserve(CE->getNumOperands()); |
| 1365 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1366 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
| 1367 | return ExprMapKeyType(CE->getOpcode(), Operands); |
| 1368 | } |
| 1369 | |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1370 | static ManagedStatic<ValueMap<ExprMapKeyType, Type, |
| 1371 | ConstantExpr> > ExprConstants; |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | 46b3d30 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 1373 | Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { |
Chris Lattner | 815ae2b | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1374 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1375 | |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1376 | if (Constant *FC = ConstantFoldCastInstruction(C, Ty)) |
| 1377 | return FC; // Fold a few common cases... |
| 1378 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1379 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1380 | std::vector<Constant*> argVec(1, C); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1381 | ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1382 | return ExprConstants->getOrCreate(Ty, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1383 | } |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1385 | Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1386 | assert(C->getType()->isIntegral() && Ty->isIntegral() && |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1387 | C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && |
| 1388 | "This is an illegal sign extension!"); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1389 | if (C->getType() != Type::BoolTy) { |
| 1390 | C = ConstantExpr::getCast(C, C->getType()->getSignedVersion()); |
| 1391 | return ConstantExpr::getCast(C, Ty); |
| 1392 | } else { |
Chris Lattner | a84df0a2 | 2006-09-28 23:36:21 +0000 | [diff] [blame^] | 1393 | if (C == ConstantBool::getTrue()) |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1394 | return ConstantIntegral::getAllOnesValue(Ty); |
| 1395 | else |
| 1396 | return ConstantIntegral::getNullValue(Ty); |
| 1397 | } |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) { |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1401 | assert(C->getType()->isIntegral() && Ty->isIntegral() && |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1402 | C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && |
| 1403 | "This is an illegal zero extension!"); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1404 | if (C->getType() != Type::BoolTy) |
| 1405 | C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion()); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1406 | return ConstantExpr::getCast(C, Ty); |
| 1407 | } |
| 1408 | |
Alkis Evlogimenos | da5de05 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1409 | Constant *ConstantExpr::getSizeOf(const Type *Ty) { |
Chris Lattner | acc4e54 | 2004-12-13 19:48:51 +0000 | [diff] [blame] | 1410 | // sizeof is implemented as: (ulong) gep (Ty*)null, 1 |
Alkis Evlogimenos | da5de05 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1411 | return getCast( |
Chris Lattner | acc4e54 | 2004-12-13 19:48:51 +0000 | [diff] [blame] | 1412 | getGetElementPtr(getNullValue(PointerType::get(Ty)), |
| 1413 | std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))), |
| 1414 | Type::ULongTy); |
Alkis Evlogimenos | da5de05 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Alkis Evlogimenos | 9160d5f | 2005-03-19 11:40:31 +0000 | [diff] [blame] | 1417 | Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) { |
| 1418 | // pointer from array is implemented as: getelementptr arr ptr, 0, 0 |
| 1419 | static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0)); |
| 1420 | |
| 1421 | return ConstantExpr::getGetElementPtr(C, Indices); |
| 1422 | } |
| 1423 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1424 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
| 1425 | Constant *C1, Constant *C2) { |
Chris Lattner | 5645e8a | 2004-01-12 19:04:55 +0000 | [diff] [blame] | 1426 | if (Opcode == Instruction::Shl || Opcode == Instruction::Shr) |
| 1427 | return getShiftTy(ReqTy, Opcode, C1, C2); |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1428 | // Check the operands for consistency first |
| 1429 | assert((Opcode >= Instruction::BinaryOpsBegin && |
| 1430 | Opcode < Instruction::BinaryOpsEnd) && |
| 1431 | "Invalid opcode in binary constant expression"); |
| 1432 | assert(C1->getType() == C2->getType() && |
| 1433 | "Operand types in binary constant expression should match"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1434 | |
Chris Lattner | e1496fb | 2006-09-17 19:14:47 +0000 | [diff] [blame] | 1435 | if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) && |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1436 | ReqTy == Type::BoolTy)) |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1437 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 1438 | return FC; // Fold a few common cases... |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1439 | |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1440 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1441 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1442 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1445 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1446 | #ifndef NDEBUG |
| 1447 | switch (Opcode) { |
| 1448 | case Instruction::Add: case Instruction::Sub: |
| 1449 | case Instruction::Mul: case Instruction::Div: |
| 1450 | case Instruction::Rem: |
| 1451 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Chris Lattner | c421a26 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1452 | assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() || |
| 1453 | isa<PackedType>(C1->getType())) && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1454 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1455 | break; |
| 1456 | case Instruction::And: |
| 1457 | case Instruction::Or: |
| 1458 | case Instruction::Xor: |
| 1459 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Chris Lattner | c421a26 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1460 | assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1461 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE: |
| 1464 | case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE: |
| 1465 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1466 | break; |
| 1467 | case Instruction::Shl: |
| 1468 | case Instruction::Shr: |
| 1469 | assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!"); |
Chris Lattner | c421a26 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1470 | assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1471 | "Tried to create a shift operation on a non-integer type!"); |
| 1472 | break; |
| 1473 | default: |
| 1474 | break; |
| 1475 | } |
| 1476 | #endif |
| 1477 | |
Chris Lattner | e1496fb | 2006-09-17 19:14:47 +0000 | [diff] [blame] | 1478 | if (Instruction::isComparison(Opcode)) |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1479 | return getTy(Type::BoolTy, Opcode, C1, C2); |
| 1480 | else |
| 1481 | return getTy(C1->getType(), Opcode, C1, C2); |
| 1482 | } |
| 1483 | |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1484 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
| 1485 | Constant *V1, Constant *V2) { |
| 1486 | assert(C->getType() == Type::BoolTy && "Select condition must be bool!"); |
| 1487 | assert(V1->getType() == V2->getType() && "Select value types must match!"); |
| 1488 | assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!"); |
| 1489 | |
| 1490 | if (ReqTy == V1->getType()) |
| 1491 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
| 1492 | return SC; // Fold common cases |
| 1493 | |
| 1494 | std::vector<Constant*> argVec(3, C); |
| 1495 | argVec[1] = V1; |
| 1496 | argVec[2] = V2; |
| 1497 | ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1498 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Chris Lattner | 9eb2b52 | 2004-01-12 19:12:58 +0000 | [diff] [blame] | 1501 | /// getShiftTy - Return a shift left or shift right constant expr |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1502 | Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, |
| 1503 | Constant *C1, Constant *C2) { |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1504 | // Check the operands for consistency first |
| 1505 | assert((Opcode == Instruction::Shl || |
| 1506 | Opcode == Instruction::Shr) && |
| 1507 | "Invalid opcode in binary constant expression"); |
| 1508 | assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy && |
| 1509 | "Invalid operand types for Shift constant expr!"); |
| 1510 | |
Chris Lattner | 0bba771 | 2004-01-12 20:40:42 +0000 | [diff] [blame] | 1511 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1512 | return FC; // Fold a few common cases... |
| 1513 | |
| 1514 | // Look up the constant in the table first to ensure uniqueness |
| 1515 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1516 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1517 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1521 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1522 | const std::vector<Value*> &IdxList) { |
| 1523 | assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) && |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1524 | "GEP indices invalid!"); |
| 1525 | |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1526 | if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) |
| 1527 | return FC; // Fold a few common cases... |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1529 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1530 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1531 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1532 | std::vector<Constant*> ArgVec; |
| 1533 | ArgVec.reserve(IdxList.size()+1); |
| 1534 | ArgVec.push_back(C); |
| 1535 | for (unsigned i = 0, e = IdxList.size(); i != e; ++i) |
| 1536 | ArgVec.push_back(cast<Constant>(IdxList[i])); |
| 1537 | const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1538 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1541 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1542 | const std::vector<Constant*> &IdxList){ |
| 1543 | // Get the result type of the getelementptr! |
| 1544 | std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); |
| 1545 | |
| 1546 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, |
| 1547 | true); |
| 1548 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1549 | return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList); |
| 1550 | } |
| 1551 | |
| 1552 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1553 | const std::vector<Value*> &IdxList) { |
| 1554 | // Get the result type of the getelementptr! |
| 1555 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
| 1556 | true); |
| 1557 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1558 | return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); |
| 1559 | } |
| 1560 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1561 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 1562 | Constant *Idx) { |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1563 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
| 1564 | return FC; // Fold a few common cases... |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1565 | // Look up the constant in the table first to ensure uniqueness |
| 1566 | std::vector<Constant*> ArgVec(1, Val); |
| 1567 | ArgVec.push_back(Idx); |
| 1568 | const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1569 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
| 1573 | assert(isa<PackedType>(Val->getType()) && |
| 1574 | "Tried to create extractelement operation on non-packed type!"); |
| 1575 | assert(Idx->getType() == Type::UIntTy && |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1576 | "Extractelement index must be uint type!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1577 | return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(), |
| 1578 | Val, Idx); |
| 1579 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1580 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1581 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 1582 | Constant *Elt, Constant *Idx) { |
| 1583 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
| 1584 | return FC; // Fold a few common cases... |
| 1585 | // Look up the constant in the table first to ensure uniqueness |
| 1586 | std::vector<Constant*> ArgVec(1, Val); |
| 1587 | ArgVec.push_back(Elt); |
| 1588 | ArgVec.push_back(Idx); |
| 1589 | const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1590 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1594 | Constant *Idx) { |
| 1595 | assert(isa<PackedType>(Val->getType()) && |
| 1596 | "Tried to create insertelement operation on non-packed type!"); |
| 1597 | assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType() |
| 1598 | && "Insertelement types must match!"); |
| 1599 | assert(Idx->getType() == Type::UIntTy && |
| 1600 | "Insertelement index must be uint type!"); |
| 1601 | return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(), |
| 1602 | Val, Elt, Idx); |
| 1603 | } |
| 1604 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1605 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 1606 | Constant *V2, Constant *Mask) { |
| 1607 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
| 1608 | return FC; // Fold a few common cases... |
| 1609 | // Look up the constant in the table first to ensure uniqueness |
| 1610 | std::vector<Constant*> ArgVec(1, V1); |
| 1611 | ArgVec.push_back(V2); |
| 1612 | ArgVec.push_back(Mask); |
| 1613 | const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec); |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1614 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1618 | Constant *Mask) { |
| 1619 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1620 | "Invalid shuffle vector constant expr operands!"); |
| 1621 | return getShuffleVectorTy(V1->getType(), V1, V2, Mask); |
| 1622 | } |
| 1623 | |
| 1624 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1625 | // destroyConstant - Remove the constant from the constant table... |
| 1626 | // |
| 1627 | void ConstantExpr::destroyConstant() { |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1628 | ExprConstants->remove(this); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1629 | destroyConstantImpl(); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1632 | const char *ConstantExpr::getOpcodeName() const { |
| 1633 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1634 | } |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1636 | //===----------------------------------------------------------------------===// |
| 1637 | // replaceUsesOfWithOnConstant implementations |
| 1638 | |
| 1639 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1640 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1641 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1642 | Constant *ToC = cast<Constant>(To); |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1643 | |
| 1644 | unsigned OperandToUpdate = U-OperandList; |
| 1645 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1646 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1647 | std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup; |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1648 | Lookup.first.first = getType(); |
| 1649 | Lookup.second = this; |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1651 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1652 | Values.reserve(getNumOperands()); // Build replacement array. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1653 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1654 | // Fill values with the modified operands of the constant array. Also, |
| 1655 | // compute whether this turns into an all-zeros array. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1656 | bool isAllZeros = false; |
| 1657 | if (!ToC->isNullValue()) { |
| 1658 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 1659 | Values.push_back(cast<Constant>(O->get())); |
| 1660 | } else { |
| 1661 | isAllZeros = true; |
| 1662 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1663 | Constant *Val = cast<Constant>(O->get()); |
| 1664 | Values.push_back(Val); |
| 1665 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1666 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1667 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1668 | Values[OperandToUpdate] = ToC; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1670 | Constant *Replacement = 0; |
| 1671 | if (isAllZeros) { |
| 1672 | Replacement = ConstantAggregateZero::get(getType()); |
| 1673 | } else { |
| 1674 | // Check to see if we have this array type already. |
| 1675 | bool Exists; |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1676 | ArrayConstantsTy::MapTy::iterator I = |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1677 | ArrayConstants->InsertOrGetItem(Lookup, Exists); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1678 | |
| 1679 | if (Exists) { |
| 1680 | Replacement = I->second; |
| 1681 | } else { |
| 1682 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1683 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 1684 | // old with the new, then deleting the old... just update the current one |
| 1685 | // in place! |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1686 | ArrayConstants->MoveConstantToNewSlot(this, I); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1687 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1688 | // Update to the new value. |
| 1689 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1690 | return; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1695 | assert(Replacement != this && "I didn't contain From!"); |
| 1696 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1697 | // Everyone using this now uses the replacement. |
| 1698 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Delete the old constant! |
| 1701 | destroyConstant(); |
| 1702 | } |
| 1703 | |
| 1704 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1705 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1706 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1707 | Constant *ToC = cast<Constant>(To); |
| 1708 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1709 | unsigned OperandToUpdate = U-OperandList; |
| 1710 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1711 | |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1712 | std::pair<StructConstantsTy::MapKey, Constant*> Lookup; |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1713 | Lookup.first.first = getType(); |
| 1714 | Lookup.second = this; |
| 1715 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1716 | Values.reserve(getNumOperands()); // Build replacement struct. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1718 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1719 | // Fill values with the modified operands of the constant struct. Also, |
| 1720 | // compute whether this turns into an all-zeros struct. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1721 | bool isAllZeros = false; |
| 1722 | if (!ToC->isNullValue()) { |
| 1723 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 1724 | Values.push_back(cast<Constant>(O->get())); |
| 1725 | } else { |
| 1726 | isAllZeros = true; |
| 1727 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1728 | Constant *Val = cast<Constant>(O->get()); |
| 1729 | Values.push_back(Val); |
| 1730 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1731 | } |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1732 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1733 | Values[OperandToUpdate] = ToC; |
| 1734 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1735 | Constant *Replacement = 0; |
| 1736 | if (isAllZeros) { |
| 1737 | Replacement = ConstantAggregateZero::get(getType()); |
| 1738 | } else { |
| 1739 | // Check to see if we have this array type already. |
| 1740 | bool Exists; |
Jim Laskey | c03caef | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1741 | StructConstantsTy::MapTy::iterator I = |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1742 | StructConstants->InsertOrGetItem(Lookup, Exists); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1743 | |
| 1744 | if (Exists) { |
| 1745 | Replacement = I->second; |
| 1746 | } else { |
| 1747 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1748 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 1749 | // old with the new, then deleting the old... just update the current one |
| 1750 | // in place! |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1751 | StructConstants->MoveConstantToNewSlot(this, I); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1753 | // Update to the new value. |
| 1754 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1759 | assert(Replacement != this && "I didn't contain From!"); |
| 1760 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1761 | // Everyone using this now uses the replacement. |
| 1762 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1763 | |
| 1764 | // Delete the old constant! |
| 1765 | destroyConstant(); |
| 1766 | } |
| 1767 | |
| 1768 | void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1769 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1770 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1771 | |
| 1772 | std::vector<Constant*> Values; |
| 1773 | Values.reserve(getNumOperands()); // Build replacement array... |
| 1774 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1775 | Constant *Val = getOperand(i); |
| 1776 | if (Val == From) Val = cast<Constant>(To); |
| 1777 | Values.push_back(Val); |
| 1778 | } |
| 1779 | |
| 1780 | Constant *Replacement = ConstantPacked::get(getType(), Values); |
| 1781 | assert(Replacement != this && "I didn't contain From!"); |
| 1782 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1783 | // Everyone using this now uses the replacement. |
| 1784 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1785 | |
| 1786 | // Delete the old constant! |
| 1787 | destroyConstant(); |
| 1788 | } |
| 1789 | |
| 1790 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1791 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1792 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 1793 | Constant *To = cast<Constant>(ToV); |
| 1794 | |
| 1795 | Constant *Replacement = 0; |
| 1796 | if (getOpcode() == Instruction::GetElementPtr) { |
| 1797 | std::vector<Constant*> Indices; |
| 1798 | Constant *Pointer = getOperand(0); |
| 1799 | Indices.reserve(getNumOperands()-1); |
| 1800 | if (Pointer == From) Pointer = To; |
| 1801 | |
| 1802 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1803 | Constant *Val = getOperand(i); |
| 1804 | if (Val == From) Val = To; |
| 1805 | Indices.push_back(Val); |
| 1806 | } |
| 1807 | Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); |
| 1808 | } else if (getOpcode() == Instruction::Cast) { |
| 1809 | assert(getOperand(0) == From && "Cast only has one use!"); |
| 1810 | Replacement = ConstantExpr::getCast(To, getType()); |
| 1811 | } else if (getOpcode() == Instruction::Select) { |
| 1812 | Constant *C1 = getOperand(0); |
| 1813 | Constant *C2 = getOperand(1); |
| 1814 | Constant *C3 = getOperand(2); |
| 1815 | if (C1 == From) C1 = To; |
| 1816 | if (C2 == From) C2 = To; |
| 1817 | if (C3 == From) C3 = To; |
| 1818 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1819 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 1820 | Constant *C1 = getOperand(0); |
| 1821 | Constant *C2 = getOperand(1); |
| 1822 | if (C1 == From) C1 = To; |
| 1823 | if (C2 == From) C2 = To; |
| 1824 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | a93b4b5 | 2006-04-08 05:09:48 +0000 | [diff] [blame] | 1825 | } else if (getOpcode() == Instruction::InsertElement) { |
| 1826 | Constant *C1 = getOperand(0); |
| 1827 | Constant *C2 = getOperand(1); |
| 1828 | Constant *C3 = getOperand(1); |
| 1829 | if (C1 == From) C1 = To; |
| 1830 | if (C2 == From) C2 = To; |
| 1831 | if (C3 == From) C3 = To; |
| 1832 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 1833 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 1834 | Constant *C1 = getOperand(0); |
| 1835 | Constant *C2 = getOperand(1); |
| 1836 | Constant *C3 = getOperand(2); |
| 1837 | if (C1 == From) C1 = To; |
| 1838 | if (C2 == From) C2 = To; |
| 1839 | if (C3 == From) C3 = To; |
| 1840 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1841 | } else if (getNumOperands() == 2) { |
| 1842 | Constant *C1 = getOperand(0); |
| 1843 | Constant *C2 = getOperand(1); |
| 1844 | if (C1 == From) C1 = To; |
| 1845 | if (C2 == From) C2 = To; |
| 1846 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 1847 | } else { |
| 1848 | assert(0 && "Unknown ConstantExpr type!"); |
| 1849 | return; |
| 1850 | } |
| 1851 | |
| 1852 | assert(Replacement != this && "I didn't contain From!"); |
| 1853 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1854 | // Everyone using this now uses the replacement. |
| 1855 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1856 | |
| 1857 | // Delete the old constant! |
| 1858 | destroyConstant(); |
| 1859 | } |
| 1860 | |
| 1861 | |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1862 | /// getStringValue - Turn an LLVM constant pointer that eventually points to a |
| 1863 | /// global into a string value. Return an empty string if we can't do it. |
Evan Cheng | 38280c0 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 1864 | /// Parameter Chop determines if the result is chopped at the first null |
| 1865 | /// terminator. |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1866 | /// |
Evan Cheng | 38280c0 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 1867 | std::string Constant::getStringValue(bool Chop, unsigned Offset) { |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1868 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) { |
| 1869 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 1870 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 1871 | if (Init->isString()) { |
| 1872 | std::string Result = Init->getAsString(); |
| 1873 | if (Offset < Result.size()) { |
| 1874 | // If we are pointing INTO The string, erase the beginning... |
| 1875 | Result.erase(Result.begin(), Result.begin()+Offset); |
| 1876 | |
| 1877 | // Take off the null terminator, and any string fragments after it. |
Evan Cheng | 38280c0 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 1878 | if (Chop) { |
| 1879 | std::string::size_type NullPos = Result.find_first_of((char)0); |
| 1880 | if (NullPos != std::string::npos) |
| 1881 | Result.erase(Result.begin()+NullPos, Result.end()); |
| 1882 | } |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1883 | return Result; |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | } else if (Constant *C = dyn_cast<Constant>(this)) { |
| 1888 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Evan Cheng | 2c5e530 | 2006-03-11 00:13:10 +0000 | [diff] [blame] | 1889 | return GV->getStringValue(Chop, Offset); |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1890 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 1891 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1892 | // Turn a gep into the specified offset. |
| 1893 | if (CE->getNumOperands() == 3 && |
| 1894 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 1895 | isa<ConstantInt>(CE->getOperand(2))) { |
| 1896 | Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue(); |
Evan Cheng | 2c5e530 | 2006-03-11 00:13:10 +0000 | [diff] [blame] | 1897 | return CE->getOperand(0)->getStringValue(Chop, Offset); |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | return ""; |
| 1903 | } |
| 1904 | |