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