Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 15 | #include "ConstantFolding.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalValue.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/SymbolTable.h" |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 30 | // Constant Class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | e9bb2df | 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 | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 35 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | ef6a6a6 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 36 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | f5ec48d | 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 | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 39 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | f5ec48d | 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 | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 44 | if (!isa<Constant>(V)) |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 45 | DOUT << "While deleting: " << *this |
| 46 | << "\n\nUse still stuck around after Def is destroyed: " |
| 47 | << *V << "\n\n"; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 48 | #endif |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 49 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 50 | Constant *CV = cast<Constant>(V); |
| 51 | CV->destroyConstant(); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 52 | |
| 53 | // The constant should remove itself from our use list... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 54 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | f5ec48d | 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 | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 59 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 35b89fa | 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 | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 78 | case Instruction::UDiv: |
| 79 | case Instruction::SDiv: |
| 80 | case Instruction::FDiv: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 81 | case Instruction::URem: |
| 82 | case Instruction::SRem: |
| 83 | case Instruction::FRem: |
Chris Lattner | 35b89fa | 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 | 9fb9641 | 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 | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 94 | switch (Ty->getTypeID()) { |
Chris Lattner | 70b06fc | 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 | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 100 | static Constant *NullSByte = ConstantInt::get(Type::SByteTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 101 | return NullSByte; |
| 102 | } |
| 103 | case Type::UByteTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 104 | static Constant *NullUByte = ConstantInt::get(Type::UByteTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 105 | return NullUByte; |
| 106 | } |
| 107 | case Type::ShortTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 108 | static Constant *NullShort = ConstantInt::get(Type::ShortTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 109 | return NullShort; |
| 110 | } |
| 111 | case Type::UShortTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 112 | static Constant *NullUShort = ConstantInt::get(Type::UShortTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 113 | return NullUShort; |
| 114 | } |
| 115 | case Type::IntTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 116 | static Constant *NullInt = ConstantInt::get(Type::IntTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 117 | return NullInt; |
| 118 | } |
| 119 | case Type::UIntTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 120 | static Constant *NullUInt = ConstantInt::get(Type::UIntTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 121 | return NullUInt; |
| 122 | } |
| 123 | case Type::LongTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 124 | static Constant *NullLong = ConstantInt::get(Type::LongTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 125 | return NullLong; |
| 126 | } |
| 127 | case Type::ULongTyID: { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 128 | static Constant *NullULong = ConstantInt::get(Type::ULongTy, 0); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 129 | return NullULong; |
| 130 | } |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 70b06fc | 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 | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 140 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 141 | case Type::PointerTyID: |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 142 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 144 | case Type::StructTyID: |
| 145 | case Type::ArrayTyID: |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 146 | case Type::PackedTyID: |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 147 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 148 | default: |
Reid Spencer | 57f6efc | 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 | 9fb9641 | 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 | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 157 | switch (Ty->getTypeID()) { |
Chris Lattner | 003cbf3 | 2006-09-28 23:36:21 +0000 | [diff] [blame] | 158 | case Type::BoolTyID: return ConstantBool::getTrue(); |
Chris Lattner | 9fb9641 | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 163 | // Calculate 011111111111111... |
Chris Lattner | 9fb9641 | 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 | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 167 | return ConstantInt::get(Ty, Val); |
Chris Lattner | 9fb9641 | 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 | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 175 | default: return 0; |
Chris Lattner | 9fb9641 | 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 | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 181 | switch (Ty->getTypeID()) { |
Chris Lattner | 003cbf3 | 2006-09-28 23:36:21 +0000 | [diff] [blame] | 182 | case Type::BoolTyID: return ConstantBool::getFalse(); |
Chris Lattner | 9fb9641 | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 187 | // Calculate 1111111111000000000000 |
Chris Lattner | 9fb9641 | 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 | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 191 | return ConstantInt::get(Ty, Val); |
Chris Lattner | 9fb9641 | 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 | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 197 | case Type::ULongTyID: return ConstantInt::get(Ty, 0); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 199 | default: return 0; |
Chris Lattner | 9fb9641 | 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 | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 205 | switch (Ty->getTypeID()) { |
Chris Lattner | 003cbf3 | 2006-09-28 23:36:21 +0000 | [diff] [blame] | 206 | case Type::BoolTyID: return ConstantBool::getTrue(); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 207 | case Type::SByteTyID: |
| 208 | case Type::ShortTyID: |
| 209 | case Type::IntTyID: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 210 | case Type::LongTyID: return ConstantInt::get(Ty, -1); |
Chris Lattner | 9fb9641 | 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 | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 220 | return ConstantInt::get(Ty, Val); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 222 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 227 | // ConstantXXX Classes |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | //===----------------------------------------------------------------------===// |
| 229 | |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | // Normal Constructors |
| 232 | |
Chris Lattner | df0ef1d | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 233 | ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 234 | : Constant(Ty, VT, 0, 0), Val(V) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | } |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 236 | |
Chris Lattner | df0ef1d | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 237 | ConstantBool::ConstantBool(bool V) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 238 | : ConstantIntegral(Type::BoolTy, ConstantBoolVal, uint64_t(V)) { |
Chris Lattner | c2dfb8b | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Reid Spencer | b83eb64 | 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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 245 | ConstantFP::ConstantFP(const Type *Ty, double V) |
Chris Lattner | df0ef1d | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 246 | : Constant(Ty, ConstantFPVal, 0, 0) { |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 247 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 248 | Val = V; |
| 249 | } |
| 250 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 251 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 252 | const std::vector<Constant*> &V) |
Chris Lattner | df0ef1d | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 253 | : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) { |
Alkis Evlogimenos | e0de1d6 | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 254 | assert(V.size() == T->getNumElements() && |
| 255 | "Invalid initializer vector for constant array"); |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 256 | Use *OL = OperandList; |
Chris Lattner | dfdd6c5 | 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 | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 259 | Constant *C = *I; |
| 260 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 261 | (T->isAbstract() && |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 262 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 263 | "Initializer for array element doesn't match array element type!"); |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 264 | OL->init(C, this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 268 | ConstantArray::~ConstantArray() { |
| 269 | delete [] OperandList; |
| 270 | } |
| 271 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 272 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 273 | const std::vector<Constant*> &V) |
Chris Lattner | df0ef1d | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 274 | : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) { |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 275 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 276 | "Invalid initializer vector for constant structure"); |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 277 | Use *OL = OperandList; |
Chris Lattner | dfdd6c5 | 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 | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 280 | Constant *C = *I; |
| 281 | assert((C->getType() == T->getElementType(I-V.begin()) || |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 282 | ((T->getElementType(I-V.begin())->isAbstract() || |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 283 | C->getType()->isAbstract()) && |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 284 | T->getElementType(I-V.begin())->getTypeID() == |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 285 | C->getType()->getTypeID())) && |
Chris Lattner | b843889 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 286 | "Initializer for struct element doesn't match struct element type!"); |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 287 | OL->init(C, this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 291 | ConstantStruct::~ConstantStruct() { |
| 292 | delete [] OperandList; |
| 293 | } |
| 294 | |
| 295 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 296 | ConstantPacked::ConstantPacked(const PackedType *T, |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 297 | const std::vector<Constant*> &V) |
Chris Lattner | df0ef1d | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 298 | : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) { |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 299 | Use *OL = OperandList; |
Chris Lattner | dfdd6c5 | 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 | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 302 | Constant *C = *I; |
| 303 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 304 | (T->isAbstract() && |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 305 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 306 | "Initializer for packed element doesn't match packed element type!"); |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 307 | OL->init(C, this); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 311 | ConstantPacked::~ConstantPacked() { |
| 312 | delete [] OperandList; |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chris Lattner | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 317 | namespace { |
| 318 | class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr { |
Chris Lattner | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 037d258 | 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 | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 334 | namespace { |
| 335 | class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr { |
Chris Lattner | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 345 | } |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 346 | |
Chris Lattner | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 349 | namespace { |
| 350 | class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr { |
Chris Lattner | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 361 | |
Robert Bocchino | c152f9c | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 365 | namespace { |
| 366 | class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr { |
Robert Bocchino | b52ee7f | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 376 | } |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 377 | |
Robert Bocchino | c152f9c | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 381 | namespace { |
| 382 | class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr { |
Robert Bocchino | c152f9c | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 393 | } |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 00f1023 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 398 | namespace { |
| 399 | class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr { |
Chris Lattner | 00f1023 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 411 | |
Chris Lattner | e467147 | 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 | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 414 | namespace { |
| 415 | struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { |
Chris Lattner | e467147 | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 425 | delete [] OperandList; |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 426 | } |
| 427 | }; |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 428 | } |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 429 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 430 | |
| 431 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 432 | // can't be inline because we don't want to #include Instruction.h into |
| 433 | // Constant.h |
| 434 | bool ConstantExpr::isCast() const { |
| 435 | return Instruction::isCast(getOpcode()); |
| 436 | } |
| 437 | |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 438 | /// ConstantExpr::get* - Return some common constants without having to |
| 439 | /// specify the full Instruction::OPCODE identifier. |
| 440 | /// |
| 441 | Constant *ConstantExpr::getNeg(Constant *C) { |
Chris Lattner | d25e3ec | 2004-03-29 19:51:24 +0000 | [diff] [blame] | 442 | if (!C->getType()->isFloatingPoint()) |
| 443 | return get(Instruction::Sub, getNullValue(C->getType()), C); |
| 444 | else |
| 445 | return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C); |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 446 | } |
| 447 | Constant *ConstantExpr::getNot(Constant *C) { |
| 448 | assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!"); |
| 449 | return get(Instruction::Xor, C, |
| 450 | ConstantIntegral::getAllOnesValue(C->getType())); |
| 451 | } |
| 452 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { |
| 453 | return get(Instruction::Add, C1, C2); |
| 454 | } |
| 455 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { |
| 456 | return get(Instruction::Sub, C1, C2); |
| 457 | } |
| 458 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { |
| 459 | return get(Instruction::Mul, C1, C2); |
| 460 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 461 | Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) { |
| 462 | return get(Instruction::UDiv, C1, C2); |
| 463 | } |
| 464 | Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) { |
| 465 | return get(Instruction::SDiv, C1, C2); |
| 466 | } |
| 467 | Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { |
| 468 | return get(Instruction::FDiv, C1, C2); |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 469 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 470 | Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) { |
| 471 | return get(Instruction::URem, C1, C2); |
| 472 | } |
| 473 | Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) { |
| 474 | return get(Instruction::SRem, C1, C2); |
| 475 | } |
| 476 | Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) { |
| 477 | return get(Instruction::FRem, C1, C2); |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 478 | } |
| 479 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
| 480 | return get(Instruction::And, C1, C2); |
| 481 | } |
| 482 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
| 483 | return get(Instruction::Or, C1, C2); |
| 484 | } |
| 485 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
| 486 | return get(Instruction::Xor, C1, C2); |
| 487 | } |
| 488 | Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) { |
| 489 | return get(Instruction::SetEQ, C1, C2); |
| 490 | } |
| 491 | Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) { |
| 492 | return get(Instruction::SetNE, C1, C2); |
| 493 | } |
| 494 | Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) { |
| 495 | return get(Instruction::SetLT, C1, C2); |
| 496 | } |
| 497 | Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) { |
| 498 | return get(Instruction::SetGT, C1, C2); |
| 499 | } |
| 500 | Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) { |
| 501 | return get(Instruction::SetLE, C1, C2); |
| 502 | } |
| 503 | Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) { |
| 504 | return get(Instruction::SetGE, C1, C2); |
| 505 | } |
| 506 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { |
| 507 | return get(Instruction::Shl, C1, C2); |
| 508 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 509 | Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) { |
| 510 | return get(Instruction::LShr, C1, C2); |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 511 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 512 | Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) { |
| 513 | return get(Instruction::AShr, C1, C2); |
Chris Lattner | c971025 | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | f4ba6c7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 516 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 517 | /// one, but with the specified operand set to the specified value. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 518 | Constant * |
| 519 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 520 | assert(OpNo < getNumOperands() && "Operand num is out of range!"); |
| 521 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 522 | "Replacing operand with value of different type!"); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 523 | if (getOperand(OpNo) == Op) |
| 524 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 525 | |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 526 | Constant *Op0, *Op1, *Op2; |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 527 | switch (getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 528 | case Instruction::Trunc: |
| 529 | case Instruction::ZExt: |
| 530 | case Instruction::SExt: |
| 531 | case Instruction::FPTrunc: |
| 532 | case Instruction::FPExt: |
| 533 | case Instruction::UIToFP: |
| 534 | case Instruction::SIToFP: |
| 535 | case Instruction::FPToUI: |
| 536 | case Instruction::FPToSI: |
| 537 | case Instruction::PtrToInt: |
| 538 | case Instruction::IntToPtr: |
| 539 | case Instruction::BitCast: |
| 540 | return ConstantExpr::getCast(getOpcode(), Op, getType()); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 541 | case Instruction::Select: |
| 542 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 543 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 544 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 545 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 546 | case Instruction::InsertElement: |
| 547 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 548 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 549 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 550 | return ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 551 | case Instruction::ExtractElement: |
| 552 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 553 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 554 | return ConstantExpr::getExtractElement(Op0, Op1); |
| 555 | case Instruction::ShuffleVector: |
| 556 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 557 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 558 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 559 | return ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 560 | case Instruction::GetElementPtr: { |
| 561 | std::vector<Constant*> Ops; |
| 562 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
| 563 | Ops.push_back(getOperand(i)); |
| 564 | if (OpNo == 0) |
| 565 | return ConstantExpr::getGetElementPtr(Op, Ops); |
| 566 | Ops[OpNo-1] = Op; |
| 567 | return ConstantExpr::getGetElementPtr(getOperand(0), Ops); |
| 568 | } |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 569 | default: |
| 570 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 571 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 572 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 573 | return ConstantExpr::get(getOpcode(), Op0, Op1); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /// getWithOperands - This returns the current constant expression with the |
| 578 | /// operands replaced with the specified values. The specified operands must |
| 579 | /// match count and type with the existing ones. |
| 580 | Constant *ConstantExpr:: |
| 581 | getWithOperands(const std::vector<Constant*> &Ops) const { |
| 582 | assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); |
| 583 | bool AnyChange = false; |
| 584 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 585 | assert(Ops[i]->getType() == getOperand(i)->getType() && |
| 586 | "Operand type mismatch!"); |
| 587 | AnyChange |= Ops[i] != getOperand(i); |
| 588 | } |
| 589 | if (!AnyChange) // No operands changed, return self. |
| 590 | return const_cast<ConstantExpr*>(this); |
| 591 | |
| 592 | switch (getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 593 | case Instruction::Trunc: |
| 594 | case Instruction::ZExt: |
| 595 | case Instruction::SExt: |
| 596 | case Instruction::FPTrunc: |
| 597 | case Instruction::FPExt: |
| 598 | case Instruction::UIToFP: |
| 599 | case Instruction::SIToFP: |
| 600 | case Instruction::FPToUI: |
| 601 | case Instruction::FPToSI: |
| 602 | case Instruction::PtrToInt: |
| 603 | case Instruction::IntToPtr: |
| 604 | case Instruction::BitCast: |
| 605 | return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 606 | case Instruction::Select: |
| 607 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 608 | case Instruction::InsertElement: |
| 609 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 610 | case Instruction::ExtractElement: |
| 611 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 612 | case Instruction::ShuffleVector: |
| 613 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
| 614 | case Instruction::GetElementPtr: { |
| 615 | std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end()); |
| 616 | return ConstantExpr::getGetElementPtr(Ops[0], ActualOps); |
| 617 | } |
| 618 | default: |
| 619 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 620 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 624 | |
| 625 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 626 | // isValueValidForType implementations |
| 627 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 628 | bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 629 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 630 | default: |
| 631 | return false; // These can't be represented as integers!!! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 632 | // Signed types... |
| 633 | case Type::SByteTyID: |
| 634 | return (Val <= INT8_MAX && Val >= INT8_MIN); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 635 | case Type::UByteTyID: |
| 636 | return (Val >= 0) && (Val <= UINT8_MAX); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 637 | case Type::ShortTyID: |
| 638 | return (Val <= INT16_MAX && Val >= INT16_MIN); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 639 | case Type::UShortTyID: |
| 640 | return (Val >= 0) && (Val <= UINT16_MAX); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 641 | case Type::IntTyID: |
Chris Lattner | 2a50201 | 2004-06-08 23:21:39 +0000 | [diff] [blame] | 642 | return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | case Type::UIntTyID: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 644 | return (Val >= 0) && (Val <= UINT32_MAX); |
| 645 | case Type::LongTyID: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 646 | case Type::ULongTyID: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 647 | return true; // always true, has to fit in largest type |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 651 | bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 652 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 653 | default: |
| 654 | return false; // These can't be represented as floating point! |
| 655 | |
Reid Spencer | 574cdb3 | 2004-12-07 07:38:08 +0000 | [diff] [blame] | 656 | // TODO: Figure out how to test if a double can be cast to a float! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 657 | case Type::FloatTyID: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 658 | case Type::DoubleTyID: |
| 659 | return true; // This is the largest type... |
| 660 | } |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 661 | } |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 663 | //===----------------------------------------------------------------------===// |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 664 | // Factory Function Implementation |
| 665 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 666 | // ConstantCreator - A class that is used to create constants by |
| 667 | // ValueMap*. This class should be partially specialized if there is |
| 668 | // something strange that needs to be done to interface to the ctor for the |
| 669 | // constant. |
| 670 | // |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 671 | namespace llvm { |
| 672 | template<class ConstantClass, class TypeClass, class ValType> |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 673 | struct VISIBILITY_HIDDEN ConstantCreator { |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 674 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
| 675 | return new ConstantClass(Ty, V); |
| 676 | } |
| 677 | }; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 679 | template<class ConstantClass, class TypeClass> |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 680 | struct VISIBILITY_HIDDEN ConvertConstantType { |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 681 | static void convert(ConstantClass *OldC, const TypeClass *NewTy) { |
| 682 | assert(0 && "This type cannot be converted!\n"); |
| 683 | abort(); |
| 684 | } |
| 685 | }; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 686 | |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 687 | template<class ValType, class TypeClass, class ConstantClass, |
| 688 | bool HasLargeKey = false /*true for arrays and structs*/ > |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 689 | class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser { |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 690 | public: |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 691 | typedef std::pair<const Type*, ValType> MapKey; |
| 692 | typedef std::map<MapKey, Constant *> MapTy; |
| 693 | typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy; |
| 694 | typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy; |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 695 | private: |
Chris Lattner | d7a3fc6 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 696 | /// Map - This is the main map from the element descriptor to the Constants. |
| 697 | /// This is the primary way we avoid creating two of the same shape |
| 698 | /// constant. |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 699 | MapTy Map; |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 700 | |
| 701 | /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping |
| 702 | /// from the constants to their element in Map. This is important for |
| 703 | /// removal of constants from the array, which would otherwise have to scan |
| 704 | /// through the map with very large keys. |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 705 | InverseMapTy InverseMap; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 706 | |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 707 | /// AbstractTypeMap - Map for abstract type constants. |
| 708 | /// |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 709 | AbstractTypeMapTy AbstractTypeMap; |
Chris Lattner | 8a7ad2d | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 8a7ad2d | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 711 | private: |
| 712 | void clear(std::vector<Constant *> &Constants) { |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 713 | for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I) |
Chris Lattner | 8a7ad2d | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 714 | Constants.push_back(I->second); |
| 715 | Map.clear(); |
| 716 | AbstractTypeMap.clear(); |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 717 | InverseMap.clear(); |
Chris Lattner | 8a7ad2d | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 720 | public: |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 721 | typename MapTy::iterator map_end() { return Map.end(); } |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 722 | |
| 723 | /// InsertOrGetItem - Return an iterator for the specified element. |
| 724 | /// If the element exists in the map, the returned iterator points to the |
| 725 | /// entry and Exists=true. If not, the iterator points to the newly |
| 726 | /// inserted entry and returns Exists=false. Newly inserted entries have |
| 727 | /// I->second == 0, and should be filled in. |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 728 | typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *> |
| 729 | &InsertVal, |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 730 | bool &Exists) { |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 731 | std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal); |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 732 | Exists = !IP.second; |
| 733 | return IP.first; |
| 734 | } |
Chris Lattner | d7a3fc6 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 735 | |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 736 | private: |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 737 | typename MapTy::iterator FindExistingElement(ConstantClass *CP) { |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 738 | if (HasLargeKey) { |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 739 | typename InverseMapTy::iterator IMI = InverseMap.find(CP); |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 740 | assert(IMI != InverseMap.end() && IMI->second != Map.end() && |
| 741 | IMI->second->second == CP && |
| 742 | "InverseMap corrupt!"); |
| 743 | return IMI->second; |
| 744 | } |
| 745 | |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 746 | typename MapTy::iterator I = |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 747 | Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP))); |
Chris Lattner | d7a3fc6 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 748 | if (I == Map.end() || I->second != CP) { |
| 749 | // FIXME: This should not use a linear scan. If this gets to be a |
| 750 | // performance problem, someone should look at this. |
| 751 | for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) |
| 752 | /* empty */; |
| 753 | } |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 754 | return I; |
| 755 | } |
| 756 | public: |
| 757 | |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 758 | /// getOrCreate - Return the specified constant from the map, creating it if |
| 759 | /// necessary. |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 760 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 761 | MapKey Lookup(Ty, V); |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 762 | typename MapTy::iterator I = Map.lower_bound(Lookup); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 763 | // Is it in the map? |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 764 | if (I != Map.end() && I->first == Lookup) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 765 | return static_cast<ConstantClass *>(I->second); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 766 | |
| 767 | // If no preexisting value, create one now... |
| 768 | ConstantClass *Result = |
| 769 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 770 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 771 | /// FIXME: why does this assert fail when loading 176.gcc? |
| 772 | //assert(Result->getType() == Ty && "Type specified is not correct!"); |
| 773 | I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); |
| 774 | |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 775 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 776 | InverseMap.insert(std::make_pair(Result, I)); |
| 777 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 778 | // If the type of the constant is abstract, make sure that an entry exists |
| 779 | // for it in the AbstractTypeMap. |
| 780 | if (Ty->isAbstract()) { |
| 781 | typename AbstractTypeMapTy::iterator TI = |
| 782 | AbstractTypeMap.lower_bound(Ty); |
| 783 | |
| 784 | if (TI == AbstractTypeMap.end() || TI->first != Ty) { |
| 785 | // Add ourselves to the ATU list of the type. |
| 786 | cast<DerivedType>(Ty)->addAbstractTypeUser(this); |
| 787 | |
| 788 | AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); |
| 789 | } |
| 790 | } |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 791 | return Result; |
| 792 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 793 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 794 | void remove(ConstantClass *CP) { |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 795 | typename MapTy::iterator I = FindExistingElement(CP); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 796 | assert(I != Map.end() && "Constant not found in constant table!"); |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 797 | assert(I->second == CP && "Didn't find correct element?"); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 798 | |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 799 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 800 | InverseMap.erase(CP); |
| 801 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 802 | // Now that we found the entry, make sure this isn't the entry that |
| 803 | // the AbstractTypeMap points to. |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 804 | const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 805 | if (Ty->isAbstract()) { |
| 806 | assert(AbstractTypeMap.count(Ty) && |
| 807 | "Abstract type not in AbstractTypeMap?"); |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 808 | typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty]; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 809 | if (ATMEntryIt == I) { |
| 810 | // Yes, we are removing the representative entry for this type. |
| 811 | // See if there are any other entries of the same type. |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 812 | typename MapTy::iterator TmpIt = ATMEntryIt; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 813 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 814 | // First check the entry before this one... |
| 815 | if (TmpIt != Map.begin()) { |
| 816 | --TmpIt; |
| 817 | if (TmpIt->first.first != Ty) // Not the same type, move back... |
| 818 | ++TmpIt; |
| 819 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 820 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 821 | // If we didn't find the same type, try to move forward... |
| 822 | if (TmpIt == ATMEntryIt) { |
| 823 | ++TmpIt; |
| 824 | if (TmpIt == Map.end() || TmpIt->first.first != Ty) |
| 825 | --TmpIt; // No entry afterwards with the same type |
| 826 | } |
| 827 | |
| 828 | // If there is another entry in the map of the same abstract type, |
| 829 | // update the AbstractTypeMap entry now. |
| 830 | if (TmpIt != ATMEntryIt) { |
| 831 | ATMEntryIt = TmpIt; |
| 832 | } else { |
| 833 | // Otherwise, we are removing the last instance of this type |
| 834 | // from the table. Remove from the ATM, and from user list. |
| 835 | cast<DerivedType>(Ty)->removeAbstractTypeUser(this); |
| 836 | AbstractTypeMap.erase(Ty); |
| 837 | } |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 838 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 839 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 840 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 841 | Map.erase(I); |
| 842 | } |
| 843 | |
Chris Lattner | a1e3f54 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 844 | |
| 845 | /// MoveConstantToNewSlot - If we are about to change C to be the element |
| 846 | /// specified by I, update our internal data structures to reflect this |
| 847 | /// fact. |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 848 | void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) { |
Chris Lattner | a1e3f54 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 849 | // First, remove the old location of the specified constant in the map. |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 850 | typename MapTy::iterator OldI = FindExistingElement(C); |
Chris Lattner | a1e3f54 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 851 | assert(OldI != Map.end() && "Constant not found in constant table!"); |
| 852 | assert(OldI->second == C && "Didn't find correct element?"); |
| 853 | |
| 854 | // If this constant is the representative element for its abstract type, |
| 855 | // update the AbstractTypeMap so that the representative element is I. |
| 856 | if (C->getType()->isAbstract()) { |
| 857 | typename AbstractTypeMapTy::iterator ATI = |
| 858 | AbstractTypeMap.find(C->getType()); |
| 859 | assert(ATI != AbstractTypeMap.end() && |
| 860 | "Abstract type not in AbstractTypeMap?"); |
| 861 | if (ATI->second == OldI) |
| 862 | ATI->second = I; |
| 863 | } |
| 864 | |
| 865 | // Remove the old entry from the map. |
| 866 | Map.erase(OldI); |
| 867 | |
| 868 | // Update the inverse map so that we know that this constant is now |
| 869 | // located at descriptor I. |
| 870 | if (HasLargeKey) { |
| 871 | assert(I->second == C && "Bad inversemap entry!"); |
| 872 | InverseMap[C] = I; |
| 873 | } |
| 874 | } |
| 875 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 876 | void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 877 | typename AbstractTypeMapTy::iterator I = |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 878 | AbstractTypeMap.find(cast<Type>(OldTy)); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 879 | |
| 880 | assert(I != AbstractTypeMap.end() && |
| 881 | "Abstract type not in AbstractTypeMap?"); |
| 882 | |
| 883 | // Convert a constant at a time until the last one is gone. The last one |
| 884 | // leaving will remove() itself, causing the AbstractTypeMapEntry to be |
| 885 | // eliminated eventually. |
| 886 | do { |
| 887 | ConvertConstantType<ConstantClass, |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 888 | TypeClass>::convert( |
| 889 | static_cast<ConstantClass *>(I->second->second), |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 890 | cast<TypeClass>(NewTy)); |
| 891 | |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 892 | I = AbstractTypeMap.find(cast<Type>(OldTy)); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 893 | } while (I != AbstractTypeMap.end()); |
| 894 | } |
| 895 | |
| 896 | // If the type became concrete without being refined to any other existing |
| 897 | // type, we just remove ourselves from the ATU list. |
| 898 | void typeBecameConcrete(const DerivedType *AbsTy) { |
| 899 | AbsTy->removeAbstractTypeUser(this); |
| 900 | } |
| 901 | |
| 902 | void dump() const { |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 903 | DOUT << "Constant.cpp: ValueMap\n"; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 904 | } |
| 905 | }; |
| 906 | } |
| 907 | |
Chris Lattner | 003cbf3 | 2006-09-28 23:36:21 +0000 | [diff] [blame] | 908 | |
| 909 | //---- ConstantBool::get*() implementation. |
| 910 | |
| 911 | ConstantBool *ConstantBool::getTrue() { |
| 912 | static ConstantBool *T = 0; |
| 913 | if (T) return T; |
| 914 | return T = new ConstantBool(true); |
| 915 | } |
| 916 | ConstantBool *ConstantBool::getFalse() { |
| 917 | static ConstantBool *F = 0; |
| 918 | if (F) return F; |
| 919 | return F = new ConstantBool(false); |
| 920 | } |
| 921 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 922 | //---- ConstantInt::get() implementations... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 923 | // |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 924 | static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 925 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 926 | // Get a ConstantInt from an int64_t. Note here that we canoncialize the value |
| 927 | // to a uint64_t value that has been zero extended down to the size of the |
| 928 | // integer type of the ConstantInt. This allows the getZExtValue method to |
| 929 | // just return the stored value while getSExtValue has to convert back to sign |
| 930 | // extended. getZExtValue is more common in LLVM than getSExtValue(). |
| 931 | ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) { |
Chris Lattner | 68329b2 | 2006-12-01 19:20:02 +0000 | [diff] [blame^] | 932 | return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask()); |
| 933 | } |
| 934 | |
| 935 | ConstantIntegral *ConstantIntegral::get(const Type *Ty, int64_t V) { |
| 936 | if (Ty == Type::BoolTy) return ConstantBool::get(V&1); |
| 937 | return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask()); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 940 | //---- ConstantFP::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 941 | // |
Chris Lattner | cfdf241 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 942 | namespace llvm { |
| 943 | template<> |
| 944 | struct ConstantCreator<ConstantFP, Type, uint64_t> { |
| 945 | static ConstantFP *create(const Type *Ty, uint64_t V) { |
| 946 | assert(Ty == Type::DoubleTy); |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 947 | return new ConstantFP(Ty, BitsToDouble(V)); |
Chris Lattner | cfdf241 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 948 | } |
| 949 | }; |
| 950 | template<> |
| 951 | struct ConstantCreator<ConstantFP, Type, uint32_t> { |
| 952 | static ConstantFP *create(const Type *Ty, uint32_t V) { |
| 953 | assert(Ty == Type::FloatTy); |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 954 | return new ConstantFP(Ty, BitsToFloat(V)); |
Chris Lattner | cfdf241 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 955 | } |
| 956 | }; |
| 957 | } |
| 958 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 959 | static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants; |
| 960 | static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 961 | |
Jim Laskey | 3a1eff7 | 2005-08-17 20:06:22 +0000 | [diff] [blame] | 962 | bool ConstantFP::isNullValue() const { |
| 963 | return DoubleToBits(Val) == 0; |
| 964 | } |
| 965 | |
| 966 | bool ConstantFP::isExactlyValue(double V) const { |
| 967 | return DoubleToBits(V) == DoubleToBits(Val); |
| 968 | } |
| 969 | |
| 970 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 971 | ConstantFP *ConstantFP::get(const Type *Ty, double V) { |
Chris Lattner | 90fb19e | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 972 | if (Ty == Type::FloatTy) { |
| 973 | // Force the value through memory to normalize it. |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 974 | return FloatConstants->getOrCreate(Ty, FloatToBits(V)); |
Chris Lattner | cfdf241 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 975 | } else { |
| 976 | assert(Ty == Type::DoubleTy); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 977 | return DoubleConstants->getOrCreate(Ty, DoubleToBits(V)); |
Chris Lattner | 90fb19e | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 978 | } |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 981 | //---- ConstantAggregateZero::get() implementation... |
| 982 | // |
| 983 | namespace llvm { |
| 984 | // ConstantAggregateZero does not take extra "value" argument... |
| 985 | template<class ValType> |
| 986 | struct ConstantCreator<ConstantAggregateZero, Type, ValType> { |
| 987 | static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ |
| 988 | return new ConstantAggregateZero(Ty); |
| 989 | } |
| 990 | }; |
| 991 | |
| 992 | template<> |
| 993 | struct ConvertConstantType<ConstantAggregateZero, Type> { |
| 994 | static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { |
| 995 | // Make everyone now use a constant of the new type... |
| 996 | Constant *New = ConstantAggregateZero::get(NewTy); |
| 997 | assert(New != OldC && "Didn't replace constant??"); |
| 998 | OldC->uncheckedReplaceAllUsesWith(New); |
| 999 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1000 | } |
| 1001 | }; |
| 1002 | } |
| 1003 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1004 | static ManagedStatic<ValueMap<char, Type, |
| 1005 | ConstantAggregateZero> > AggZeroConstants; |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1007 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 1008 | |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1009 | Constant *ConstantAggregateZero::get(const Type *Ty) { |
Chris Lattner | 31d1a2c | 2006-06-10 04:16:23 +0000 | [diff] [blame] | 1010 | assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) && |
| 1011 | "Cannot create an aggregate zero of non-aggregate type!"); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1012 | return AggZeroConstants->getOrCreate(Ty, 0); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // destroyConstant - Remove the constant from the constant table... |
| 1016 | // |
| 1017 | void ConstantAggregateZero::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1018 | AggZeroConstants->remove(this); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1019 | destroyConstantImpl(); |
| 1020 | } |
| 1021 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1022 | //---- ConstantArray::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1023 | // |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1024 | namespace llvm { |
| 1025 | template<> |
| 1026 | struct ConvertConstantType<ConstantArray, ArrayType> { |
| 1027 | static void convert(ConstantArray *OldC, const ArrayType *NewTy) { |
| 1028 | // Make everyone now use a constant of the new type... |
| 1029 | std::vector<Constant*> C; |
| 1030 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1031 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1032 | Constant *New = ConstantArray::get(NewTy, C); |
| 1033 | assert(New != OldC && "Didn't replace constant??"); |
| 1034 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1035 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1036 | } |
| 1037 | }; |
| 1038 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1040 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 1041 | std::vector<Constant*> Elements; |
| 1042 | Elements.reserve(CA->getNumOperands()); |
| 1043 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1044 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 1045 | return Elements; |
| 1046 | } |
| 1047 | |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1048 | typedef ValueMap<std::vector<Constant*>, ArrayType, |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1049 | ConstantArray, true /*largekey*/> ArrayConstantsTy; |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1050 | static ManagedStatic<ArrayConstantsTy> ArrayConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | ca705fa | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1052 | Constant *ConstantArray::get(const ArrayType *Ty, |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1053 | const std::vector<Constant*> &V) { |
| 1054 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 1055 | if (!V.empty()) { |
| 1056 | Constant *C = V[0]; |
| 1057 | if (!C->isNullValue()) |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1058 | return ArrayConstants->getOrCreate(Ty, V); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1059 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 1060 | if (V[i] != C) |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1061 | return ArrayConstants->getOrCreate(Ty, V); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1062 | } |
| 1063 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1066 | // destroyConstant - Remove the constant from the constant table... |
| 1067 | // |
| 1068 | void ConstantArray::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1069 | ArrayConstants->remove(this); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1070 | destroyConstantImpl(); |
| 1071 | } |
| 1072 | |
Reid Spencer | 8949477 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1073 | /// ConstantArray::get(const string&) - Return an array that is initialized to |
| 1074 | /// contain the specified string. If length is zero then a null terminator is |
| 1075 | /// added to the specified string so that it may be used in a natural way. |
| 1076 | /// Otherwise, the length parameter specifies how much of the string to use |
| 1077 | /// and it won't be null terminated. |
| 1078 | /// |
Reid Spencer | 461bed2 | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1079 | Constant *ConstantArray::get(const std::string &Str, bool AddNull) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1080 | std::vector<Constant*> ElementVals; |
Reid Spencer | 461bed2 | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1081 | for (unsigned i = 0; i < Str.length(); ++i) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1082 | ElementVals.push_back(ConstantInt::get(Type::SByteTy, Str[i])); |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Add a null terminator to the string... |
Reid Spencer | 461bed2 | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1085 | if (AddNull) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1086 | ElementVals.push_back(ConstantInt::get(Type::SByteTy, 0)); |
Reid Spencer | 8949477 | 2006-05-30 08:23:18 +0000 | [diff] [blame] | 1087 | } |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 1088 | |
Reid Spencer | 461bed2 | 2006-05-30 18:15:07 +0000 | [diff] [blame] | 1089 | ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size()); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1090 | return ConstantArray::get(ATy, ElementVals); |
Vikram S. Adve | db2da49 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1093 | /// isString - This method returns true if the array is an array of sbyte or |
| 1094 | /// ubyte, and if the elements of the array are all ConstantInt's. |
| 1095 | bool ConstantArray::isString() const { |
| 1096 | // Check the element type for sbyte or ubyte... |
Chris Lattner | 7aa162b | 2004-01-14 17:51:53 +0000 | [diff] [blame] | 1097 | if (getType()->getElementType() != Type::UByteTy && |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1098 | getType()->getElementType() != Type::SByteTy) |
| 1099 | return false; |
| 1100 | // Check the elements to make sure they are all integers, not constant |
| 1101 | // expressions. |
| 1102 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1103 | if (!isa<ConstantInt>(getOperand(i))) |
| 1104 | return false; |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1108 | /// isCString - This method returns true if the array is a string (see |
| 1109 | /// isString) and it ends in a null byte \0 and does not contains any other |
| 1110 | /// null bytes except its terminator. |
| 1111 | bool ConstantArray::isCString() const { |
Evan Cheng | abf6345 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1112 | // Check the element type for sbyte or ubyte... |
| 1113 | if (getType()->getElementType() != Type::UByteTy && |
| 1114 | getType()->getElementType() != Type::SByteTy) |
| 1115 | return false; |
| 1116 | Constant *Zero = Constant::getNullValue(getOperand(0)->getType()); |
| 1117 | // Last element must be a null. |
| 1118 | if (getOperand(getNumOperands()-1) != Zero) |
| 1119 | return false; |
| 1120 | // Other elements must be non-null integers. |
| 1121 | for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { |
| 1122 | if (!isa<ConstantInt>(getOperand(i))) |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1123 | return false; |
Evan Cheng | abf6345 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1124 | if (getOperand(i) == Zero) |
| 1125 | return false; |
| 1126 | } |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1127 | return true; |
| 1128 | } |
| 1129 | |
| 1130 | |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1131 | // getAsString - If the sub-element type of this array is either sbyte or ubyte, |
| 1132 | // then this method converts the array to an std::string and returns it. |
| 1133 | // Otherwise, it asserts out. |
| 1134 | // |
| 1135 | std::string ConstantArray::getAsString() const { |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1136 | assert(isString() && "Not a string!"); |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1137 | std::string Result; |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 1138 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1139 | Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue(); |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1140 | return Result; |
| 1141 | } |
| 1142 | |
| 1143 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1144 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1145 | // |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1147 | namespace llvm { |
| 1148 | template<> |
| 1149 | struct ConvertConstantType<ConstantStruct, StructType> { |
| 1150 | static void convert(ConstantStruct *OldC, const StructType *NewTy) { |
| 1151 | // Make everyone now use a constant of the new type... |
| 1152 | std::vector<Constant*> C; |
| 1153 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1154 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1155 | Constant *New = ConstantStruct::get(NewTy, C); |
| 1156 | assert(New != OldC && "Didn't replace constant??"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1158 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1159 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1160 | } |
| 1161 | }; |
| 1162 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1164 | typedef ValueMap<std::vector<Constant*>, StructType, |
Chris Lattner | a55b30a | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 1165 | ConstantStruct, true /*largekey*/> StructConstantsTy; |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1166 | static ManagedStatic<StructConstantsTy> StructConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1168 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 1169 | std::vector<Constant*> Elements; |
| 1170 | Elements.reserve(CS->getNumOperands()); |
| 1171 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 1172 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 1173 | return Elements; |
| 1174 | } |
| 1175 | |
Chris Lattner | ca705fa | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1176 | Constant *ConstantStruct::get(const StructType *Ty, |
| 1177 | const std::vector<Constant*> &V) { |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1178 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 1179 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1180 | if (!V[i]->isNullValue()) |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1181 | return StructConstants->getOrCreate(Ty, V); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1182 | |
| 1183 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1184 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | b370c7a | 2004-07-12 20:35:11 +0000 | [diff] [blame] | 1186 | Constant *ConstantStruct::get(const std::vector<Constant*> &V) { |
| 1187 | std::vector<const Type*> StructEls; |
| 1188 | StructEls.reserve(V.size()); |
| 1189 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1190 | StructEls.push_back(V[i]->getType()); |
| 1191 | return get(StructType::get(StructEls), V); |
| 1192 | } |
| 1193 | |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1194 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1195 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1196 | void ConstantStruct::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1197 | StructConstants->remove(this); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1198 | destroyConstantImpl(); |
| 1199 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1200 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1201 | //---- ConstantPacked::get() implementation... |
| 1202 | // |
| 1203 | namespace llvm { |
| 1204 | template<> |
| 1205 | struct ConvertConstantType<ConstantPacked, PackedType> { |
| 1206 | static void convert(ConstantPacked *OldC, const PackedType *NewTy) { |
| 1207 | // Make everyone now use a constant of the new type... |
| 1208 | std::vector<Constant*> C; |
| 1209 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1210 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1211 | Constant *New = ConstantPacked::get(NewTy, C); |
| 1212 | assert(New != OldC && "Didn't replace constant??"); |
| 1213 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1214 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1215 | } |
| 1216 | }; |
| 1217 | } |
| 1218 | |
| 1219 | static std::vector<Constant*> getValType(ConstantPacked *CP) { |
| 1220 | std::vector<Constant*> Elements; |
| 1221 | Elements.reserve(CP->getNumOperands()); |
| 1222 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 1223 | Elements.push_back(CP->getOperand(i)); |
| 1224 | return Elements; |
| 1225 | } |
| 1226 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1227 | static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType, |
| 1228 | ConstantPacked> > PackedConstants; |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1229 | |
| 1230 | Constant *ConstantPacked::get(const PackedType *Ty, |
| 1231 | const std::vector<Constant*> &V) { |
| 1232 | // If this is an all-zero packed, return a ConstantAggregateZero object |
| 1233 | if (!V.empty()) { |
| 1234 | Constant *C = V[0]; |
| 1235 | if (!C->isNullValue()) |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1236 | return PackedConstants->getOrCreate(Ty, V); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1237 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 1238 | if (V[i] != C) |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1239 | return PackedConstants->getOrCreate(Ty, V); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1240 | } |
| 1241 | return ConstantAggregateZero::get(Ty); |
| 1242 | } |
| 1243 | |
| 1244 | Constant *ConstantPacked::get(const std::vector<Constant*> &V) { |
| 1245 | assert(!V.empty() && "Cannot infer type if V is empty"); |
| 1246 | return get(PackedType::get(V.front()->getType(),V.size()), V); |
| 1247 | } |
| 1248 | |
| 1249 | // destroyConstant - Remove the constant from the constant table... |
| 1250 | // |
| 1251 | void ConstantPacked::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1252 | PackedConstants->remove(this); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1253 | destroyConstantImpl(); |
| 1254 | } |
| 1255 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1256 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1257 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1259 | namespace llvm { |
| 1260 | // ConstantPointerNull does not take extra "value" argument... |
| 1261 | template<class ValType> |
| 1262 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 1263 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 1264 | return new ConstantPointerNull(Ty); |
| 1265 | } |
| 1266 | }; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1268 | template<> |
| 1269 | struct ConvertConstantType<ConstantPointerNull, PointerType> { |
| 1270 | static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { |
| 1271 | // Make everyone now use a constant of the new type... |
| 1272 | Constant *New = ConstantPointerNull::get(NewTy); |
| 1273 | assert(New != OldC && "Didn't replace constant??"); |
| 1274 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1275 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1276 | } |
| 1277 | }; |
| 1278 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1280 | static ManagedStatic<ValueMap<char, PointerType, |
| 1281 | ConstantPointerNull> > NullPtrConstants; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1283 | static char getValType(ConstantPointerNull *) { |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1288 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1289 | return NullPtrConstants->getOrCreate(Ty, 0); |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1292 | // destroyConstant - Remove the constant from the constant table... |
| 1293 | // |
| 1294 | void ConstantPointerNull::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1295 | NullPtrConstants->remove(this); |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1296 | destroyConstantImpl(); |
| 1297 | } |
| 1298 | |
| 1299 | |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1300 | //---- UndefValue::get() implementation... |
| 1301 | // |
| 1302 | |
| 1303 | namespace llvm { |
| 1304 | // UndefValue does not take extra "value" argument... |
| 1305 | template<class ValType> |
| 1306 | struct ConstantCreator<UndefValue, Type, ValType> { |
| 1307 | static UndefValue *create(const Type *Ty, const ValType &V) { |
| 1308 | return new UndefValue(Ty); |
| 1309 | } |
| 1310 | }; |
| 1311 | |
| 1312 | template<> |
| 1313 | struct ConvertConstantType<UndefValue, Type> { |
| 1314 | static void convert(UndefValue *OldC, const Type *NewTy) { |
| 1315 | // Make everyone now use a constant of the new type. |
| 1316 | Constant *New = UndefValue::get(NewTy); |
| 1317 | assert(New != OldC && "Didn't replace constant??"); |
| 1318 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1319 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1320 | } |
| 1321 | }; |
| 1322 | } |
| 1323 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1324 | static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants; |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1325 | |
| 1326 | static char getValType(UndefValue *) { |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | UndefValue *UndefValue::get(const Type *Ty) { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1332 | return UndefValueConstants->getOrCreate(Ty, 0); |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | // destroyConstant - Remove the constant from the constant table. |
| 1336 | // |
| 1337 | void UndefValue::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1338 | UndefValueConstants->remove(this); |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1339 | destroyConstantImpl(); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1345 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1346 | // |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1347 | typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1349 | namespace llvm { |
| 1350 | template<> |
| 1351 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
| 1352 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1353 | if (Instruction::isCast(V.first)) |
| 1354 | return new UnaryConstantExpr(V.first, V.second[0], Ty); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1355 | if ((V.first >= Instruction::BinaryOpsBegin && |
| 1356 | V.first < Instruction::BinaryOpsEnd) || |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1357 | V.first == Instruction::Shl || |
| 1358 | V.first == Instruction::LShr || |
| 1359 | V.first == Instruction::AShr) |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1360 | return new BinaryConstantExpr(V.first, V.second[0], V.second[1]); |
Chris Lattner | 92e8422 | 2004-03-30 22:51:03 +0000 | [diff] [blame] | 1361 | if (V.first == Instruction::Select) |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1362 | return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1363 | if (V.first == Instruction::ExtractElement) |
| 1364 | return new ExtractElementConstantExpr(V.second[0], V.second[1]); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1365 | if (V.first == Instruction::InsertElement) |
| 1366 | return new InsertElementConstantExpr(V.second[0], V.second[1], |
| 1367 | V.second[2]); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1368 | if (V.first == Instruction::ShuffleVector) |
| 1369 | return new ShuffleVectorConstantExpr(V.second[0], V.second[1], |
| 1370 | V.second[2]); |
| 1371 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1372 | assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1374 | std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end()); |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1375 | return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1376 | } |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1377 | }; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1379 | template<> |
| 1380 | struct ConvertConstantType<ConstantExpr, Type> { |
| 1381 | static void convert(ConstantExpr *OldC, const Type *NewTy) { |
| 1382 | Constant *New; |
| 1383 | switch (OldC->getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1384 | case Instruction::Trunc: |
| 1385 | case Instruction::ZExt: |
| 1386 | case Instruction::SExt: |
| 1387 | case Instruction::FPTrunc: |
| 1388 | case Instruction::FPExt: |
| 1389 | case Instruction::UIToFP: |
| 1390 | case Instruction::SIToFP: |
| 1391 | case Instruction::FPToUI: |
| 1392 | case Instruction::FPToSI: |
| 1393 | case Instruction::PtrToInt: |
| 1394 | case Instruction::IntToPtr: |
| 1395 | case Instruction::BitCast: |
| 1396 | New = ConstantExpr::getCast( |
| 1397 | OldC->getOpcode(), OldC->getOperand(0), NewTy); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1398 | break; |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1399 | case Instruction::Select: |
| 1400 | New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), |
| 1401 | OldC->getOperand(1), |
| 1402 | OldC->getOperand(2)); |
| 1403 | break; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1404 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1405 | case Instruction::LShr: |
| 1406 | case Instruction::AShr: |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1407 | New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(), |
| 1408 | OldC->getOperand(0), OldC->getOperand(1)); |
| 1409 | break; |
| 1410 | default: |
| 1411 | assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1412 | OldC->getOpcode() < Instruction::BinaryOpsEnd); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1413 | New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), |
| 1414 | OldC->getOperand(1)); |
| 1415 | break; |
| 1416 | case Instruction::GetElementPtr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1417 | // Make everyone now use a constant of the new type... |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1418 | std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); |
| 1419 | New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1420 | break; |
| 1421 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1423 | assert(New != OldC && "Didn't replace constant??"); |
| 1424 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1425 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1426 | } |
| 1427 | }; |
| 1428 | } // end namespace llvm |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1429 | |
| 1430 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1431 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1432 | std::vector<Constant*> Operands; |
| 1433 | Operands.reserve(CE->getNumOperands()); |
| 1434 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1435 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
| 1436 | return ExprMapKeyType(CE->getOpcode(), Operands); |
| 1437 | } |
| 1438 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1439 | static ManagedStatic<ValueMap<ExprMapKeyType, Type, |
| 1440 | ConstantExpr> > ExprConstants; |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1441 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1442 | /// This is a utility function to handle folding of casts and lookup of the |
| 1443 | /// cast in the ExprConstants map. It is usedby the various get* methods below. |
| 1444 | static inline Constant *getFoldedCast( |
| 1445 | Instruction::CastOps opc, Constant *C, const Type *Ty) { |
Chris Lattner | 9eacf8a | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1446 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1447 | // Fold a few common cases |
| 1448 | if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) |
| 1449 | return FC; |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1450 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1451 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1452 | std::vector<Constant*> argVec(1, C); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1453 | ExprMapKeyType Key = std::make_pair(opc, argVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1454 | return ExprConstants->getOrCreate(Ty, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1455 | } |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1456 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1457 | Constant *ConstantExpr::getCast( Constant *C, const Type *Ty ) { |
| 1458 | // Note: we can't inline this because it requires the Instructions.h header |
| 1459 | return getCast(CastInst::getCastOpcode(C, Ty), C, Ty); |
| 1460 | } |
| 1461 | |
| 1462 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { |
| 1463 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1464 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1465 | assert(C && Ty && "Null arguments to getCast"); |
| 1466 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1467 | |
| 1468 | switch (opc) { |
| 1469 | default: |
| 1470 | assert(0 && "Invalid cast opcode"); |
| 1471 | break; |
| 1472 | case Instruction::Trunc: return getTrunc(C, Ty); |
| 1473 | case Instruction::ZExt: return getZeroExtend(C, Ty); |
| 1474 | case Instruction::SExt: return getSignExtend(C, Ty); |
| 1475 | case Instruction::FPTrunc: return getFPTrunc(C, Ty); |
| 1476 | case Instruction::FPExt: return getFPExtend(C, Ty); |
| 1477 | case Instruction::UIToFP: return getUIToFP(C, Ty); |
| 1478 | case Instruction::SIToFP: return getSIToFP(C, Ty); |
| 1479 | case Instruction::FPToUI: return getFPToUI(C, Ty); |
| 1480 | case Instruction::FPToSI: return getFPToSI(C, Ty); |
| 1481 | case Instruction::PtrToInt: return getPtrToInt(C, Ty); |
| 1482 | case Instruction::IntToPtr: return getIntToPtr(C, Ty); |
| 1483 | case Instruction::BitCast: return getBitCast(C, Ty); |
Chris Lattner | f5ac6c2 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1484 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { |
| 1489 | assert(C->getType()->isInteger() && "Trunc operand must be integer"); |
| 1490 | assert(Ty->isIntegral() && "Trunc produces only integral"); |
| 1491 | assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&& |
| 1492 | "SrcTy must be larger than DestTy for Trunc!"); |
| 1493 | |
| 1494 | return getFoldedCast(Instruction::Trunc, C, Ty); |
| 1495 | } |
| 1496 | |
| 1497 | Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { |
| 1498 | assert(C->getType()->isIntegral() && "SEXt operand must be integral"); |
| 1499 | assert(Ty->isInteger() && "SExt produces only integer"); |
| 1500 | assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& |
| 1501 | "SrcTy must be smaller than DestTy for SExt!"); |
| 1502 | |
| 1503 | return getFoldedCast(Instruction::SExt, C, Ty); |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1507 | assert(C->getType()->isIntegral() && "ZEXt operand must be integral"); |
| 1508 | assert(Ty->isInteger() && "ZExt produces only integer"); |
| 1509 | assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& |
| 1510 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 1511 | |
| 1512 | return getFoldedCast(Instruction::ZExt, C, Ty); |
| 1513 | } |
| 1514 | |
| 1515 | Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { |
| 1516 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1517 | C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&& |
| 1518 | "This is an illegal floating point truncation!"); |
| 1519 | return getFoldedCast(Instruction::FPTrunc, C, Ty); |
| 1520 | } |
| 1521 | |
| 1522 | Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { |
| 1523 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1524 | C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& |
| 1525 | "This is an illegal floating point extension!"); |
| 1526 | return getFoldedCast(Instruction::FPExt, C, Ty); |
| 1527 | } |
| 1528 | |
| 1529 | Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { |
| 1530 | assert(C->getType()->isIntegral() && Ty->isFloatingPoint() && |
| 1531 | "This is an illegal uint to floating point cast!"); |
| 1532 | return getFoldedCast(Instruction::UIToFP, C, Ty); |
| 1533 | } |
| 1534 | |
| 1535 | Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { |
| 1536 | assert(C->getType()->isIntegral() && Ty->isFloatingPoint() && |
| 1537 | "This is an illegal sint to floating point cast!"); |
| 1538 | return getFoldedCast(Instruction::SIToFP, C, Ty); |
| 1539 | } |
| 1540 | |
| 1541 | Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { |
| 1542 | assert(C->getType()->isFloatingPoint() && Ty->isIntegral() && |
| 1543 | "This is an illegal floating point to uint cast!"); |
| 1544 | return getFoldedCast(Instruction::FPToUI, C, Ty); |
| 1545 | } |
| 1546 | |
| 1547 | Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { |
| 1548 | assert(C->getType()->isFloatingPoint() && Ty->isIntegral() && |
| 1549 | "This is an illegal floating point to sint cast!"); |
| 1550 | return getFoldedCast(Instruction::FPToSI, C, Ty); |
| 1551 | } |
| 1552 | |
| 1553 | Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { |
| 1554 | assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer"); |
| 1555 | assert(DstTy->isIntegral() && "PtrToInt destination must be integral"); |
| 1556 | return getFoldedCast(Instruction::PtrToInt, C, DstTy); |
| 1557 | } |
| 1558 | |
| 1559 | Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { |
| 1560 | assert(C->getType()->isIntegral() && "IntToPtr source must be integral"); |
| 1561 | assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer"); |
| 1562 | return getFoldedCast(Instruction::IntToPtr, C, DstTy); |
| 1563 | } |
| 1564 | |
| 1565 | Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { |
| 1566 | // BitCast implies a no-op cast of type only. No bits change. However, you |
| 1567 | // can't cast pointers to anything but pointers. |
| 1568 | const Type *SrcTy = C->getType(); |
| 1569 | assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) && |
| 1570 | "Bitcast cannot cast pointer to non-pointer and vice versa"); |
| 1571 | |
| 1572 | // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr |
| 1573 | // or nonptr->ptr). For all the other types, the cast is okay if source and |
| 1574 | // destination bit widths are identical. |
| 1575 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1576 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1577 | assert(SrcBitSize == DstBitSize && "Bitcast requies types of same width"); |
| 1578 | return getFoldedCast(Instruction::BitCast, C, DstTy); |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Alkis Evlogimenos | 60ab140 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1581 | Constant *ConstantExpr::getSizeOf(const Type *Ty) { |
Chris Lattner | fcdd82e | 2004-12-13 19:48:51 +0000 | [diff] [blame] | 1582 | // sizeof is implemented as: (ulong) gep (Ty*)null, 1 |
Alkis Evlogimenos | 60ab140 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1583 | return getCast( |
Chris Lattner | fcdd82e | 2004-12-13 19:48:51 +0000 | [diff] [blame] | 1584 | getGetElementPtr(getNullValue(PointerType::get(Ty)), |
| 1585 | std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))), |
| 1586 | Type::ULongTy); |
Alkis Evlogimenos | 60ab140 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Alkis Evlogimenos | 1cecd3a | 2005-03-19 11:40:31 +0000 | [diff] [blame] | 1589 | Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) { |
| 1590 | // pointer from array is implemented as: getelementptr arr ptr, 0, 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1591 | static std::vector<Constant*> Indices(2, ConstantInt::get(Type::UIntTy, 0)); |
Alkis Evlogimenos | 1cecd3a | 2005-03-19 11:40:31 +0000 | [diff] [blame] | 1592 | |
| 1593 | return ConstantExpr::getGetElementPtr(C, Indices); |
| 1594 | } |
| 1595 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1596 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
| 1597 | Constant *C1, Constant *C2) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1598 | if (Opcode == Instruction::Shl || Opcode == Instruction::LShr || |
| 1599 | Opcode == Instruction::AShr) |
Chris Lattner | 3edd397 | 2004-01-12 19:04:55 +0000 | [diff] [blame] | 1600 | return getShiftTy(ReqTy, Opcode, C1, C2); |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1601 | // Check the operands for consistency first |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1602 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 1603 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1604 | "Invalid opcode in binary constant expression"); |
| 1605 | assert(C1->getType() == C2->getType() && |
| 1606 | "Operand types in binary constant expression should match"); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | a5b0740 | 2006-09-17 19:14:47 +0000 | [diff] [blame] | 1608 | if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) && |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1609 | ReqTy == Type::BoolTy)) |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1610 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 1611 | return FC; // Fold a few common cases... |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1613 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1614 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1615 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1618 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1619 | #ifndef NDEBUG |
| 1620 | switch (Opcode) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1621 | case Instruction::Add: |
| 1622 | case Instruction::Sub: |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1623 | case Instruction::Mul: |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1624 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Chris Lattner | 2054295 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1625 | assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() || |
| 1626 | isa<PackedType>(C1->getType())) && |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1627 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1628 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1629 | case Instruction::UDiv: |
| 1630 | case Instruction::SDiv: |
| 1631 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1632 | assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) && |
| 1633 | cast<PackedType>(C1->getType())->getElementType()->isInteger())) && |
| 1634 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1635 | break; |
| 1636 | case Instruction::FDiv: |
| 1637 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1638 | assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType()) |
| 1639 | && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint())) |
| 1640 | && "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1641 | break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1642 | case Instruction::URem: |
| 1643 | case Instruction::SRem: |
| 1644 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1645 | assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) && |
| 1646 | cast<PackedType>(C1->getType())->getElementType()->isInteger())) && |
| 1647 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1648 | break; |
| 1649 | case Instruction::FRem: |
| 1650 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1651 | assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType()) |
| 1652 | && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint())) |
| 1653 | && "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1654 | break; |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1655 | case Instruction::And: |
| 1656 | case Instruction::Or: |
| 1657 | case Instruction::Xor: |
| 1658 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Chris Lattner | 2054295 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1659 | assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) && |
Misha Brukman | 1bae291 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1660 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE: |
| 1663 | case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE: |
| 1664 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1665 | break; |
| 1666 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1667 | case Instruction::LShr: |
| 1668 | case Instruction::AShr: |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1669 | assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!"); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1670 | assert(C1->getType()->isInteger() && |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1671 | "Tried to create a shift operation on a non-integer type!"); |
| 1672 | break; |
| 1673 | default: |
| 1674 | break; |
| 1675 | } |
| 1676 | #endif |
| 1677 | |
Chris Lattner | a5b0740 | 2006-09-17 19:14:47 +0000 | [diff] [blame] | 1678 | if (Instruction::isComparison(Opcode)) |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1679 | return getTy(Type::BoolTy, Opcode, C1, C2); |
| 1680 | else |
| 1681 | return getTy(C1->getType(), Opcode, C1, C2); |
| 1682 | } |
| 1683 | |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1684 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
| 1685 | Constant *V1, Constant *V2) { |
| 1686 | assert(C->getType() == Type::BoolTy && "Select condition must be bool!"); |
| 1687 | assert(V1->getType() == V2->getType() && "Select value types must match!"); |
| 1688 | assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!"); |
| 1689 | |
| 1690 | if (ReqTy == V1->getType()) |
| 1691 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
| 1692 | return SC; // Fold common cases |
| 1693 | |
| 1694 | std::vector<Constant*> argVec(3, C); |
| 1695 | argVec[1] = V1; |
| 1696 | argVec[2] = V2; |
| 1697 | ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1698 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Chris Lattner | 69b9e89 | 2004-01-12 19:12:58 +0000 | [diff] [blame] | 1701 | /// getShiftTy - Return a shift left or shift right constant expr |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1702 | Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, |
| 1703 | Constant *C1, Constant *C2) { |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1704 | // Check the operands for consistency first |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1705 | assert((Opcode == Instruction::Shl || |
| 1706 | Opcode == Instruction::LShr || |
| 1707 | Opcode == Instruction::AShr) && |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1708 | "Invalid opcode in binary constant expression"); |
| 1709 | assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy && |
| 1710 | "Invalid operand types for Shift constant expr!"); |
| 1711 | |
Chris Lattner | d4403b4 | 2004-01-12 20:40:42 +0000 | [diff] [blame] | 1712 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1713 | return FC; // Fold a few common cases... |
| 1714 | |
| 1715 | // Look up the constant in the table first to ensure uniqueness |
| 1716 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1717 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1718 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1722 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1723 | const std::vector<Value*> &IdxList) { |
| 1724 | assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) && |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1725 | "GEP indices invalid!"); |
| 1726 | |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1727 | if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) |
| 1728 | return FC; // Fold a few common cases... |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1729 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1730 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1731 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1732 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1733 | std::vector<Constant*> ArgVec; |
| 1734 | ArgVec.reserve(IdxList.size()+1); |
| 1735 | ArgVec.push_back(C); |
| 1736 | for (unsigned i = 0, e = IdxList.size(); i != e; ++i) |
| 1737 | ArgVec.push_back(cast<Constant>(IdxList[i])); |
| 1738 | const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1739 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1742 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1743 | const std::vector<Constant*> &IdxList){ |
| 1744 | // Get the result type of the getelementptr! |
| 1745 | std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); |
| 1746 | |
| 1747 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, |
| 1748 | true); |
| 1749 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1750 | return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList); |
| 1751 | } |
| 1752 | |
| 1753 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1754 | const std::vector<Value*> &IdxList) { |
| 1755 | // Get the result type of the getelementptr! |
| 1756 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
| 1757 | true); |
| 1758 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1759 | return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); |
| 1760 | } |
| 1761 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1762 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 1763 | Constant *Idx) { |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1764 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
| 1765 | return FC; // Fold a few common cases... |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1766 | // Look up the constant in the table first to ensure uniqueness |
| 1767 | std::vector<Constant*> ArgVec(1, Val); |
| 1768 | ArgVec.push_back(Idx); |
| 1769 | const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1770 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
| 1774 | assert(isa<PackedType>(Val->getType()) && |
| 1775 | "Tried to create extractelement operation on non-packed type!"); |
| 1776 | assert(Idx->getType() == Type::UIntTy && |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1777 | "Extractelement index must be uint type!"); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1778 | return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(), |
| 1779 | Val, Idx); |
| 1780 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1781 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1782 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 1783 | Constant *Elt, Constant *Idx) { |
| 1784 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
| 1785 | return FC; // Fold a few common cases... |
| 1786 | // Look up the constant in the table first to ensure uniqueness |
| 1787 | std::vector<Constant*> ArgVec(1, Val); |
| 1788 | ArgVec.push_back(Elt); |
| 1789 | ArgVec.push_back(Idx); |
| 1790 | const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1791 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1795 | Constant *Idx) { |
| 1796 | assert(isa<PackedType>(Val->getType()) && |
| 1797 | "Tried to create insertelement operation on non-packed type!"); |
| 1798 | assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType() |
| 1799 | && "Insertelement types must match!"); |
| 1800 | assert(Idx->getType() == Type::UIntTy && |
| 1801 | "Insertelement index must be uint type!"); |
| 1802 | return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(), |
| 1803 | Val, Elt, Idx); |
| 1804 | } |
| 1805 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1806 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 1807 | Constant *V2, Constant *Mask) { |
| 1808 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
| 1809 | return FC; // Fold a few common cases... |
| 1810 | // Look up the constant in the table first to ensure uniqueness |
| 1811 | std::vector<Constant*> ArgVec(1, V1); |
| 1812 | ArgVec.push_back(V2); |
| 1813 | ArgVec.push_back(Mask); |
| 1814 | const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec); |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1815 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1819 | Constant *Mask) { |
| 1820 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1821 | "Invalid shuffle vector constant expr operands!"); |
| 1822 | return getShuffleVectorTy(V1->getType(), V1, V2, Mask); |
| 1823 | } |
| 1824 | |
| 1825 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1826 | // destroyConstant - Remove the constant from the constant table... |
| 1827 | // |
| 1828 | void ConstantExpr::destroyConstant() { |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1829 | ExprConstants->remove(this); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1830 | destroyConstantImpl(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1833 | const char *ConstantExpr::getOpcodeName() const { |
| 1834 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1835 | } |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 1836 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1837 | //===----------------------------------------------------------------------===// |
| 1838 | // replaceUsesOfWithOnConstant implementations |
| 1839 | |
| 1840 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1841 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1842 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1843 | Constant *ToC = cast<Constant>(To); |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1844 | |
| 1845 | unsigned OperandToUpdate = U-OperandList; |
| 1846 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1847 | |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1848 | std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup; |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1849 | Lookup.first.first = getType(); |
| 1850 | Lookup.second = this; |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1852 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1853 | Values.reserve(getNumOperands()); // Build replacement array. |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1854 | |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1855 | // Fill values with the modified operands of the constant array. Also, |
| 1856 | // compute whether this turns into an all-zeros array. |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1857 | bool isAllZeros = false; |
| 1858 | if (!ToC->isNullValue()) { |
| 1859 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 1860 | Values.push_back(cast<Constant>(O->get())); |
| 1861 | } else { |
| 1862 | isAllZeros = true; |
| 1863 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1864 | Constant *Val = cast<Constant>(O->get()); |
| 1865 | Values.push_back(Val); |
| 1866 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1867 | } |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1868 | } |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1869 | Values[OperandToUpdate] = ToC; |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1871 | Constant *Replacement = 0; |
| 1872 | if (isAllZeros) { |
| 1873 | Replacement = ConstantAggregateZero::get(getType()); |
| 1874 | } else { |
| 1875 | // Check to see if we have this array type already. |
| 1876 | bool Exists; |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1877 | ArrayConstantsTy::MapTy::iterator I = |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1878 | ArrayConstants->InsertOrGetItem(Lookup, Exists); |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1879 | |
| 1880 | if (Exists) { |
| 1881 | Replacement = I->second; |
| 1882 | } else { |
| 1883 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1884 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 1885 | // old with the new, then deleting the old... just update the current one |
| 1886 | // in place! |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1887 | ArrayConstants->MoveConstantToNewSlot(this, I); |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1889 | // Update to the new value. |
| 1890 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1891 | return; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1896 | assert(Replacement != this && "I didn't contain From!"); |
| 1897 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1898 | // Everyone using this now uses the replacement. |
| 1899 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1900 | |
| 1901 | // Delete the old constant! |
| 1902 | destroyConstant(); |
| 1903 | } |
| 1904 | |
| 1905 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1906 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1907 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1908 | Constant *ToC = cast<Constant>(To); |
| 1909 | |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1910 | unsigned OperandToUpdate = U-OperandList; |
| 1911 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1912 | |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1913 | std::pair<StructConstantsTy::MapKey, Constant*> Lookup; |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1914 | Lookup.first.first = getType(); |
| 1915 | Lookup.second = this; |
| 1916 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1917 | Values.reserve(getNumOperands()); // Build replacement struct. |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1920 | // Fill values with the modified operands of the constant struct. Also, |
| 1921 | // compute whether this turns into an all-zeros struct. |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1922 | bool isAllZeros = false; |
| 1923 | if (!ToC->isNullValue()) { |
| 1924 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 1925 | Values.push_back(cast<Constant>(O->get())); |
| 1926 | } else { |
| 1927 | isAllZeros = true; |
| 1928 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1929 | Constant *Val = cast<Constant>(O->get()); |
| 1930 | Values.push_back(Val); |
| 1931 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1932 | } |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1933 | } |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1934 | Values[OperandToUpdate] = ToC; |
| 1935 | |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1936 | Constant *Replacement = 0; |
| 1937 | if (isAllZeros) { |
| 1938 | Replacement = ConstantAggregateZero::get(getType()); |
| 1939 | } else { |
| 1940 | // Check to see if we have this array type already. |
| 1941 | bool Exists; |
Jim Laskey | ede5aa4 | 2006-07-17 17:38:29 +0000 | [diff] [blame] | 1942 | StructConstantsTy::MapTy::iterator I = |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1943 | StructConstants->InsertOrGetItem(Lookup, Exists); |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1944 | |
| 1945 | if (Exists) { |
| 1946 | Replacement = I->second; |
| 1947 | } else { |
| 1948 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1949 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 1950 | // old with the new, then deleting the old... just update the current one |
| 1951 | // in place! |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1952 | StructConstants->MoveConstantToNewSlot(this, I); |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1953 | |
Chris Lattner | 23ec01f | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1954 | // Update to the new value. |
| 1955 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | c182a88 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1956 | return; |
| 1957 | } |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1960 | assert(Replacement != this && "I didn't contain From!"); |
| 1961 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1962 | // Everyone using this now uses the replacement. |
| 1963 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1964 | |
| 1965 | // Delete the old constant! |
| 1966 | destroyConstant(); |
| 1967 | } |
| 1968 | |
| 1969 | void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1970 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1971 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1972 | |
| 1973 | std::vector<Constant*> Values; |
| 1974 | Values.reserve(getNumOperands()); // Build replacement array... |
| 1975 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1976 | Constant *Val = getOperand(i); |
| 1977 | if (Val == From) Val = cast<Constant>(To); |
| 1978 | Values.push_back(Val); |
| 1979 | } |
| 1980 | |
| 1981 | Constant *Replacement = ConstantPacked::get(getType(), Values); |
| 1982 | assert(Replacement != this && "I didn't contain From!"); |
| 1983 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1984 | // Everyone using this now uses the replacement. |
| 1985 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1986 | |
| 1987 | // Delete the old constant! |
| 1988 | destroyConstant(); |
| 1989 | } |
| 1990 | |
| 1991 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1992 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1993 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 1994 | Constant *To = cast<Constant>(ToV); |
| 1995 | |
| 1996 | Constant *Replacement = 0; |
| 1997 | if (getOpcode() == Instruction::GetElementPtr) { |
| 1998 | std::vector<Constant*> Indices; |
| 1999 | Constant *Pointer = getOperand(0); |
| 2000 | Indices.reserve(getNumOperands()-1); |
| 2001 | if (Pointer == From) Pointer = To; |
| 2002 | |
| 2003 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 2004 | Constant *Val = getOperand(i); |
| 2005 | if (Val == From) Val = To; |
| 2006 | Indices.push_back(Val); |
| 2007 | } |
| 2008 | Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2009 | } else if (isCast()) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2010 | assert(getOperand(0) == From && "Cast only has one use!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2011 | Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2012 | } else if (getOpcode() == Instruction::Select) { |
| 2013 | Constant *C1 = getOperand(0); |
| 2014 | Constant *C2 = getOperand(1); |
| 2015 | Constant *C3 = getOperand(2); |
| 2016 | if (C1 == From) C1 = To; |
| 2017 | if (C2 == From) C2 = To; |
| 2018 | if (C3 == From) C3 = To; |
| 2019 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2020 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 2021 | Constant *C1 = getOperand(0); |
| 2022 | Constant *C2 = getOperand(1); |
| 2023 | if (C1 == From) C1 = To; |
| 2024 | if (C2 == From) C2 = To; |
| 2025 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | 42b5580 | 2006-04-08 05:09:48 +0000 | [diff] [blame] | 2026 | } else if (getOpcode() == Instruction::InsertElement) { |
| 2027 | Constant *C1 = getOperand(0); |
| 2028 | Constant *C2 = getOperand(1); |
| 2029 | Constant *C3 = getOperand(1); |
| 2030 | if (C1 == From) C1 = To; |
| 2031 | if (C2 == From) C2 = To; |
| 2032 | if (C3 == From) C3 = To; |
| 2033 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 2034 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 2035 | Constant *C1 = getOperand(0); |
| 2036 | Constant *C2 = getOperand(1); |
| 2037 | Constant *C3 = getOperand(2); |
| 2038 | if (C1 == From) C1 = To; |
| 2039 | if (C2 == From) C2 = To; |
| 2040 | if (C3 == From) C3 = To; |
| 2041 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2042 | } else if (getNumOperands() == 2) { |
| 2043 | Constant *C1 = getOperand(0); |
| 2044 | Constant *C2 = getOperand(1); |
| 2045 | if (C1 == From) C1 = To; |
| 2046 | if (C2 == From) C2 = To; |
| 2047 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 2048 | } else { |
| 2049 | assert(0 && "Unknown ConstantExpr type!"); |
| 2050 | return; |
| 2051 | } |
| 2052 | |
| 2053 | assert(Replacement != this && "I didn't contain From!"); |
| 2054 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2055 | // Everyone using this now uses the replacement. |
| 2056 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Delete the old constant! |
| 2059 | destroyConstant(); |
| 2060 | } |
| 2061 | |
| 2062 | |
Jim Laskey | 21b6c9d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 2063 | /// getStringValue - Turn an LLVM constant pointer that eventually points to a |
| 2064 | /// global into a string value. Return an empty string if we can't do it. |
Evan Cheng | 0937103 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 2065 | /// Parameter Chop determines if the result is chopped at the first null |
| 2066 | /// terminator. |
Jim Laskey | 21b6c9d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 2067 | /// |
Evan Cheng | 0937103 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 2068 | std::string Constant::getStringValue(bool Chop, unsigned Offset) { |
Jim Laskey | 21b6c9d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 2069 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) { |
| 2070 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 2071 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 2072 | if (Init->isString()) { |
| 2073 | std::string Result = Init->getAsString(); |
| 2074 | if (Offset < Result.size()) { |
| 2075 | // If we are pointing INTO The string, erase the beginning... |
| 2076 | Result.erase(Result.begin(), Result.begin()+Offset); |
| 2077 | |
| 2078 | // Take off the null terminator, and any string fragments after it. |
Evan Cheng | 0937103 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 2079 | if (Chop) { |
| 2080 | std::string::size_type NullPos = Result.find_first_of((char)0); |
| 2081 | if (NullPos != std::string::npos) |
| 2082 | Result.erase(Result.begin()+NullPos, Result.end()); |
| 2083 | } |
Jim Laskey | 21b6c9d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 2084 | return Result; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | } else if (Constant *C = dyn_cast<Constant>(this)) { |
| 2089 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Evan Cheng | 2352ce9 | 2006-03-11 00:13:10 +0000 | [diff] [blame] | 2090 | return GV->getStringValue(Chop, Offset); |
Jim Laskey | 21b6c9d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 2091 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 2092 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 2093 | // Turn a gep into the specified offset. |
| 2094 | if (CE->getNumOperands() == 3 && |
| 2095 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 2096 | isa<ConstantInt>(CE->getOperand(2))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2097 | Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue(); |
Evan Cheng | 2352ce9 | 2006-03-11 00:13:10 +0000 | [diff] [blame] | 2098 | return CE->getOperand(0)->getStringValue(Chop, Offset); |
Jim Laskey | 21b6c9d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | return ""; |
| 2104 | } |
| 2105 | |