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