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