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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 14 | #include "LLVMContextImpl.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 92f6fea | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 16 | #include "ConstantFold.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalValue.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Dan Gohman | 5a206ee | 2009-07-18 01:49:22 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/FoldingSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 30 | #include "llvm/System/Mutex.h" |
Owen Anderson | 3e456ab | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 31 | #include "llvm/System/RWMutex.h" |
Owen Anderson | e3cd5ca | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 32 | #include "llvm/System/Threading.h" |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 36 | #include <map> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 40 | // Constant Class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Owen Anderson | ee6aefc | 2009-06-18 19:10:19 +0000 | [diff] [blame] | 43 | // Becomes a no-op when multithreading is disabled. |
| 44 | ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock; |
Owen Anderson | 3e456ab | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 45 | |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 46 | // Constructor to create a '0' constant of arbitrary type... |
| 47 | static const uint64_t zero[2] = {0, 0}; |
| 48 | Constant* Constant::getNullValue(const Type* Ty) { |
| 49 | switch (Ty->getTypeID()) { |
| 50 | case Type::IntegerTyID: |
| 51 | return ConstantInt::get(Ty, 0); |
| 52 | case Type::FloatTyID: |
| 53 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0))); |
| 54 | case Type::DoubleTyID: |
| 55 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0))); |
| 56 | case Type::X86_FP80TyID: |
| 57 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero))); |
| 58 | case Type::FP128TyID: |
| 59 | return ConstantFP::get(Ty->getContext(), |
| 60 | APFloat(APInt(128, 2, zero), true)); |
| 61 | case Type::PPC_FP128TyID: |
| 62 | return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero))); |
| 63 | case Type::PointerTyID: |
| 64 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 65 | case Type::StructTyID: |
| 66 | case Type::ArrayTyID: |
| 67 | case Type::VectorTyID: |
| 68 | return ConstantAggregateZero::get(Ty); |
| 69 | default: |
| 70 | // Function, Label, or Opaque type? |
| 71 | assert(!"Cannot create a null constant of that type!"); |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | Constant* Constant::getAllOnesValue(const Type* Ty) { |
| 77 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) |
| 78 | return ConstantInt::get(Ty->getContext(), |
| 79 | APInt::getAllOnesValue(ITy->getBitWidth())); |
| 80 | |
| 81 | std::vector<Constant*> Elts; |
| 82 | const VectorType* VTy = cast<VectorType>(Ty); |
| 83 | Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); |
| 84 | assert(Elts[0] && "Not a vector integer type!"); |
| 85 | return cast<ConstantVector>(ConstantVector::get(Elts)); |
| 86 | } |
| 87 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 88 | void Constant::destroyConstantImpl() { |
| 89 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 90 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | ef6a6a6 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 91 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 92 | // but they don't know that. Because we only find out when the CPV is |
| 93 | // 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] | 94 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 95 | // |
| 96 | while (!use_empty()) { |
| 97 | Value *V = use_back(); |
| 98 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 99 | if (!isa<Constant>(V)) |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 100 | DOUT << "While deleting: " << *this |
| 101 | << "\n\nUse still stuck around after Def is destroyed: " |
| 102 | << *V << "\n\n"; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 103 | #endif |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 104 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 105 | Constant *CV = cast<Constant>(V); |
| 106 | CV->destroyConstant(); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 107 | |
| 108 | // The constant should remove itself from our use list... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 109 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Value has no outstanding references it is safe to delete it now... |
| 113 | delete this; |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 116 | /// canTrap - Return true if evaluation of this constant could trap. This is |
| 117 | /// true for things like constant expressions that could divide by zero. |
| 118 | bool Constant::canTrap() const { |
| 119 | assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); |
| 120 | // The only thing that could possibly trap are constant exprs. |
| 121 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(this); |
| 122 | if (!CE) return false; |
| 123 | |
| 124 | // ConstantExpr traps if any operands can trap. |
| 125 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 126 | if (getOperand(i)->canTrap()) |
| 127 | return true; |
| 128 | |
| 129 | // Otherwise, only specific operations can trap. |
| 130 | switch (CE->getOpcode()) { |
| 131 | default: |
| 132 | return false; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 133 | case Instruction::UDiv: |
| 134 | case Instruction::SDiv: |
| 135 | case Instruction::FDiv: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 136 | case Instruction::URem: |
| 137 | case Instruction::SRem: |
| 138 | case Instruction::FRem: |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 139 | // Div and rem can trap if the RHS is not known to be non-zero. |
| 140 | if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue()) |
| 141 | return true; |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 146 | |
| 147 | /// getRelocationInfo - This method classifies the entry according to |
| 148 | /// whether or not it may generate a relocation entry. This must be |
| 149 | /// conservative, so if it might codegen to a relocatable entry, it should say |
| 150 | /// so. The return values are: |
| 151 | /// |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 152 | /// NoRelocation: This constant pool entry is guaranteed to never have a |
| 153 | /// relocation applied to it (because it holds a simple constant like |
| 154 | /// '4'). |
| 155 | /// LocalRelocation: This entry has relocations, but the entries are |
| 156 | /// guaranteed to be resolvable by the static linker, so the dynamic |
| 157 | /// linker will never see them. |
| 158 | /// GlobalRelocations: This entry may have arbitrary relocations. |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 159 | /// |
| 160 | /// FIXME: This really should not be in VMCore. |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 161 | Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { |
| 162 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 163 | if (GV->hasLocalLinkage() || GV->hasHiddenVisibility()) |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 164 | return LocalRelocation; // Local to this file/library. |
| 165 | return GlobalRelocations; // Global reference. |
Anton Korobeynikov | ab267a2 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 168 | PossibleRelocationsTy Result = NoRelocation; |
Evan Cheng | afe1581 | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 170 | Result = std::max(Result, getOperand(i)->getRelocationInfo()); |
| 171 | |
| 172 | return Result; |
Evan Cheng | afe1581 | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 8638144 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 176 | /// getVectorElements - This method, which is only valid on constant of vector |
| 177 | /// type, returns the elements of the vector in the specified smallvector. |
Chris Lattner | 071aade | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 178 | /// This handles breaking down a vector undef into undef elements, etc. For |
| 179 | /// constant exprs and other cases we can't handle, we return an empty vector. |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 180 | void Constant::getVectorElements(LLVMContext &Context, |
| 181 | SmallVectorImpl<Constant*> &Elts) const { |
Chris Lattner | 8638144 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 182 | assert(isa<VectorType>(getType()) && "Not a vector constant!"); |
| 183 | |
| 184 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) { |
| 185 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) |
| 186 | Elts.push_back(CV->getOperand(i)); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | const VectorType *VT = cast<VectorType>(getType()); |
| 191 | if (isa<ConstantAggregateZero>(this)) { |
| 192 | Elts.assign(VT->getNumElements(), |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 193 | Constant::getNullValue(VT->getElementType())); |
Chris Lattner | 8638144 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
Chris Lattner | 071aade | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 197 | if (isa<UndefValue>(this)) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 198 | Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType())); |
Chris Lattner | 071aade | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Unknown type, must be constant expr etc. |
Chris Lattner | 8638144 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 208 | // ConstantInt |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 209 | //===----------------------------------------------------------------------===// |
| 210 | |
Reid Spencer | 532d0ce | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 211 | ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V) |
Chris Lattner | eb41bdd | 2007-02-20 05:55:46 +0000 | [diff] [blame] | 212 | : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { |
Reid Spencer | 532d0ce | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 213 | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 216 | ConstantInt* ConstantInt::getTrue(LLVMContext &Context) { |
| 217 | LLVMContextImpl *pImpl = Context.pImpl; |
| 218 | sys::SmartScopedWriter<true>(pImpl->ConstantsLock); |
| 219 | if (pImpl->TheTrueVal) |
| 220 | return pImpl->TheTrueVal; |
| 221 | else |
| 222 | return (pImpl->TheTrueVal = ConstantInt::get(IntegerType::get(1), 1)); |
| 223 | } |
| 224 | |
| 225 | ConstantInt* ConstantInt::getFalse(LLVMContext &Context) { |
| 226 | LLVMContextImpl *pImpl = Context.pImpl; |
| 227 | sys::SmartScopedWriter<true>(pImpl->ConstantsLock); |
| 228 | if (pImpl->TheFalseVal) |
| 229 | return pImpl->TheFalseVal; |
| 230 | else |
| 231 | return (pImpl->TheFalseVal = ConstantInt::get(IntegerType::get(1), 0)); |
| 232 | } |
| 233 | |
| 234 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 235 | // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap |
| 236 | // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the |
| 237 | // operator== and operator!= to ensure that the DenseMap doesn't attempt to |
| 238 | // compare APInt's of different widths, which would violate an APInt class |
| 239 | // invariant which generates an assertion. |
| 240 | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) { |
| 241 | // Get the corresponding integer type for the bit width of the value. |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 242 | const IntegerType *ITy = IntegerType::get(V.getBitWidth()); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 243 | // get an existing value or the insertion position |
| 244 | DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); |
| 245 | |
| 246 | Context.pImpl->ConstantsLock.reader_acquire(); |
| 247 | ConstantInt *&Slot = Context.pImpl->IntConstants[Key]; |
| 248 | Context.pImpl->ConstantsLock.reader_release(); |
| 249 | |
| 250 | if (!Slot) { |
| 251 | sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock); |
| 252 | ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key]; |
| 253 | if (!Slot) { |
| 254 | NewSlot = new ConstantInt(ITy, V); |
| 255 | } |
| 256 | |
| 257 | return NewSlot; |
| 258 | } else { |
| 259 | return Slot; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) { |
| 264 | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), |
| 265 | V, isSigned); |
| 266 | |
| 267 | // For vectors, broadcast the value. |
| 268 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 269 | return ConstantVector::get( |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 270 | std::vector<Constant *>(VTy->getNumElements(), C)); |
| 271 | |
| 272 | return C; |
| 273 | } |
| 274 | |
| 275 | ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V, |
| 276 | bool isSigned) { |
| 277 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); |
| 278 | } |
| 279 | |
| 280 | ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) { |
| 281 | return get(Ty, V, true); |
| 282 | } |
| 283 | |
| 284 | Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) { |
| 285 | return get(Ty, V, true); |
| 286 | } |
| 287 | |
| 288 | Constant* ConstantInt::get(const Type* Ty, const APInt& V) { |
| 289 | ConstantInt *C = get(Ty->getContext(), V); |
| 290 | assert(C->getType() == Ty->getScalarType() && |
| 291 | "ConstantInt type doesn't match the type implied by its value!"); |
| 292 | |
| 293 | // For vectors, broadcast the value. |
| 294 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 295 | return ConstantVector::get( |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 296 | std::vector<Constant *>(VTy->getNumElements(), C)); |
| 297 | |
| 298 | return C; |
| 299 | } |
| 300 | |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 301 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 302 | // ConstantFP |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 303 | //===----------------------------------------------------------------------===// |
| 304 | |
Rafael Espindola | 87d1f47 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 305 | static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { |
| 306 | if (Ty == Type::FloatTy) |
| 307 | return &APFloat::IEEEsingle; |
| 308 | if (Ty == Type::DoubleTy) |
| 309 | return &APFloat::IEEEdouble; |
| 310 | if (Ty == Type::X86_FP80Ty) |
| 311 | return &APFloat::x87DoubleExtended; |
| 312 | else if (Ty == Type::FP128Ty) |
| 313 | return &APFloat::IEEEquad; |
| 314 | |
| 315 | assert(Ty == Type::PPC_FP128Ty && "Unknown FP format"); |
| 316 | return &APFloat::PPCDoubleDouble; |
| 317 | } |
| 318 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 319 | /// get() - This returns a constant fp for the specified value in the |
| 320 | /// specified type. This should only be used for simple constant values like |
| 321 | /// 2.0/1.0 etc, that are known-valid both as double and as the target format. |
| 322 | Constant* ConstantFP::get(const Type* Ty, double V) { |
| 323 | LLVMContext &Context = Ty->getContext(); |
| 324 | |
| 325 | APFloat FV(V); |
| 326 | bool ignored; |
| 327 | FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), |
| 328 | APFloat::rmNearestTiesToEven, &ignored); |
| 329 | Constant *C = get(Context, FV); |
| 330 | |
| 331 | // For vectors, broadcast the value. |
| 332 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 333 | return ConstantVector::get( |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 334 | std::vector<Constant *>(VTy->getNumElements(), C)); |
| 335 | |
| 336 | return C; |
| 337 | } |
| 338 | |
| 339 | ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) { |
| 340 | LLVMContext &Context = Ty->getContext(); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 341 | APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 342 | apf.changeSign(); |
| 343 | return get(Context, apf); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 348 | if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) |
| 349 | if (PTy->getElementType()->isFloatingPoint()) { |
| 350 | std::vector<Constant*> zeros(PTy->getNumElements(), |
| 351 | getNegativeZero(PTy->getElementType())); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 352 | return ConstantVector::get(PTy, zeros); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (Ty->isFloatingPoint()) |
| 356 | return getNegativeZero(Ty); |
| 357 | |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 358 | return Constant::getNullValue(Ty); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
| 362 | // ConstantFP accessors. |
| 363 | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
| 364 | DenseMapAPFloatKeyInfo::KeyTy Key(V); |
| 365 | |
| 366 | LLVMContextImpl* pImpl = Context.pImpl; |
| 367 | |
| 368 | pImpl->ConstantsLock.reader_acquire(); |
| 369 | ConstantFP *&Slot = pImpl->FPConstants[Key]; |
| 370 | pImpl->ConstantsLock.reader_release(); |
| 371 | |
| 372 | if (!Slot) { |
| 373 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 374 | ConstantFP *&NewSlot = pImpl->FPConstants[Key]; |
| 375 | if (!NewSlot) { |
| 376 | const Type *Ty; |
| 377 | if (&V.getSemantics() == &APFloat::IEEEsingle) |
| 378 | Ty = Type::FloatTy; |
| 379 | else if (&V.getSemantics() == &APFloat::IEEEdouble) |
| 380 | Ty = Type::DoubleTy; |
| 381 | else if (&V.getSemantics() == &APFloat::x87DoubleExtended) |
| 382 | Ty = Type::X86_FP80Ty; |
| 383 | else if (&V.getSemantics() == &APFloat::IEEEquad) |
| 384 | Ty = Type::FP128Ty; |
| 385 | else { |
| 386 | assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && |
| 387 | "Unknown FP format"); |
| 388 | Ty = Type::PPC_FP128Ty; |
| 389 | } |
| 390 | NewSlot = new ConstantFP(Ty, V); |
| 391 | } |
| 392 | |
| 393 | return NewSlot; |
| 394 | } |
| 395 | |
| 396 | return Slot; |
| 397 | } |
| 398 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 399 | ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) |
| 400 | : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { |
Chris Lattner | 288e78f | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 401 | assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && |
| 402 | "FP type Mismatch"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 405 | bool ConstantFP::isNullValue() const { |
Dale Johannesen | 343e770 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 406 | return Val.isZero() && !Val.isNegative(); |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 409 | bool ConstantFP::isExactlyValue(const APFloat& V) const { |
| 410 | return Val.bitwiseIsEqual(V); |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 413 | //===----------------------------------------------------------------------===// |
| 414 | // ConstantXXX Classes |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
| 417 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 418 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 419 | const std::vector<Constant*> &V) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 420 | : Constant(T, ConstantArrayVal, |
| 421 | OperandTraits<ConstantArray>::op_end(this) - V.size(), |
| 422 | V.size()) { |
Alkis Evlogimenos | e0de1d6 | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 423 | assert(V.size() == T->getNumElements() && |
| 424 | "Invalid initializer vector for constant array"); |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 425 | Use *OL = OperandList; |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 426 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 427 | I != E; ++I, ++OL) { |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 428 | Constant *C = *I; |
| 429 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 430 | (T->isAbstract() && |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 431 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 432 | "Initializer for array element doesn't match array element type!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 433 | *OL = C; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 437 | Constant *ConstantArray::get(const ArrayType *Ty, |
| 438 | const std::vector<Constant*> &V) { |
| 439 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 440 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 441 | if (!V.empty()) { |
| 442 | Constant *C = V[0]; |
| 443 | if (!C->isNullValue()) { |
| 444 | // Implicitly locked. |
| 445 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
| 446 | } |
| 447 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 448 | if (V[i] != C) { |
| 449 | // Implicitly locked. |
| 450 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
| 451 | } |
| 452 | } |
| 453 | |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 454 | return ConstantAggregateZero::get(Ty); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
| 458 | Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals, |
| 459 | unsigned NumVals) { |
| 460 | // FIXME: make this the primary ctor method. |
| 461 | return get(T, std::vector<Constant*>(Vals, Vals+NumVals)); |
| 462 | } |
| 463 | |
| 464 | /// ConstantArray::get(const string&) - Return an array that is initialized to |
| 465 | /// contain the specified string. If length is zero then a null terminator is |
| 466 | /// added to the specified string so that it may be used in a natural way. |
| 467 | /// Otherwise, the length parameter specifies how much of the string to use |
| 468 | /// and it won't be null terminated. |
| 469 | /// |
| 470 | Constant* ConstantArray::get(const StringRef &Str, bool AddNull) { |
| 471 | std::vector<Constant*> ElementVals; |
| 472 | for (unsigned i = 0; i < Str.size(); ++i) |
| 473 | ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i])); |
| 474 | |
| 475 | // Add a null terminator to the string... |
| 476 | if (AddNull) { |
| 477 | ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0)); |
| 478 | } |
| 479 | |
| 480 | ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size()); |
| 481 | return get(ATy, ElementVals); |
| 482 | } |
| 483 | |
| 484 | |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 485 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 486 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 487 | const std::vector<Constant*> &V) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 488 | : Constant(T, ConstantStructVal, |
| 489 | OperandTraits<ConstantStruct>::op_end(this) - V.size(), |
| 490 | V.size()) { |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 491 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 492 | "Invalid initializer vector for constant structure"); |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 493 | Use *OL = OperandList; |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 494 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 495 | I != E; ++I, ++OL) { |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 496 | Constant *C = *I; |
| 497 | assert((C->getType() == T->getElementType(I-V.begin()) || |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 498 | ((T->getElementType(I-V.begin())->isAbstract() || |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 499 | C->getType()->isAbstract()) && |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 500 | T->getElementType(I-V.begin())->getTypeID() == |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 501 | C->getType()->getTypeID())) && |
Chris Lattner | b843889 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 502 | "Initializer for struct element doesn't match struct element type!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 503 | *OL = C; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 507 | // ConstantStruct accessors. |
| 508 | Constant* ConstantStruct::get(const StructType* T, |
| 509 | const std::vector<Constant*>& V) { |
| 510 | LLVMContextImpl* pImpl = T->getContext().pImpl; |
| 511 | |
| 512 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 513 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 514 | if (!V[i]->isNullValue()) |
| 515 | // Implicitly locked. |
| 516 | return pImpl->StructConstants.getOrCreate(T, V); |
| 517 | |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 518 | return ConstantAggregateZero::get(T); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) { |
| 522 | std::vector<const Type*> StructEls; |
| 523 | StructEls.reserve(V.size()); |
| 524 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 525 | StructEls.push_back(V[i]->getType()); |
| 526 | return get(StructType::get(StructEls, packed), V); |
| 527 | } |
| 528 | |
| 529 | Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals, |
| 530 | bool Packed) { |
| 531 | // FIXME: make this the primary ctor method. |
| 532 | return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed); |
| 533 | } |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 534 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 535 | ConstantVector::ConstantVector(const VectorType *T, |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 536 | const std::vector<Constant*> &V) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 537 | : Constant(T, ConstantVectorVal, |
| 538 | OperandTraits<ConstantVector>::op_end(this) - V.size(), |
| 539 | V.size()) { |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 540 | Use *OL = OperandList; |
Chris Lattner | dfdd6c5 | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 541 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 542 | I != E; ++I, ++OL) { |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 543 | Constant *C = *I; |
| 544 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 545 | (T->isAbstract() && |
Chris Lattner | 71abaab | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 546 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Dan Gohman | fa73ea2 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 547 | "Initializer for vector element doesn't match vector element type!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 548 | *OL = C; |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 552 | // ConstantVector accessors. |
| 553 | Constant* ConstantVector::get(const VectorType* T, |
| 554 | const std::vector<Constant*>& V) { |
| 555 | assert(!V.empty() && "Vectors can't be empty"); |
| 556 | LLVMContext &Context = T->getContext(); |
| 557 | LLVMContextImpl *pImpl = Context.pImpl; |
| 558 | |
| 559 | // If this is an all-undef or alll-zero vector, return a |
| 560 | // ConstantAggregateZero or UndefValue. |
| 561 | Constant *C = V[0]; |
| 562 | bool isZero = C->isNullValue(); |
| 563 | bool isUndef = isa<UndefValue>(C); |
| 564 | |
| 565 | if (isZero || isUndef) { |
| 566 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 567 | if (V[i] != C) { |
| 568 | isZero = isUndef = false; |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (isZero) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 574 | return ConstantAggregateZero::get(T); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 575 | if (isUndef) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 576 | return UndefValue::get(T); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 577 | |
| 578 | // Implicitly locked. |
| 579 | return pImpl->VectorConstants.getOrCreate(T, V); |
| 580 | } |
| 581 | |
| 582 | Constant* ConstantVector::get(const std::vector<Constant*>& V) { |
| 583 | assert(!V.empty() && "Cannot infer type if V is empty"); |
| 584 | return get(VectorType::get(V.front()->getType(),V.size()), V); |
| 585 | } |
| 586 | |
| 587 | Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) { |
| 588 | // FIXME: make this the primary ctor method. |
| 589 | return get(std::vector<Constant*>(Vals, Vals+NumVals)); |
| 590 | } |
| 591 | |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 592 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 593 | namespace llvm { |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 594 | // We declare several classes private to this file, so use an anonymous |
| 595 | // namespace |
| 596 | namespace { |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 597 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 598 | /// UnaryConstantExpr - This class is private to Constants.cpp, and is used |
| 599 | /// behind the scenes to implement unary constant exprs. |
| 600 | class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 601 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 602 | public: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 603 | // allocate space for exactly one operand |
| 604 | void *operator new(size_t s) { |
| 605 | return User::operator new(s, 1); |
| 606 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 607 | UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 608 | : ConstantExpr(Ty, Opcode, &Op<0>(), 1) { |
| 609 | Op<0>() = C; |
| 610 | } |
| 611 | /// Transparently provide more efficient getOperand methods. |
| 612 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 613 | }; |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 614 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 615 | /// BinaryConstantExpr - This class is private to Constants.cpp, and is used |
| 616 | /// behind the scenes to implement binary constant exprs. |
| 617 | class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 618 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 619 | public: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 620 | // allocate space for exactly two operands |
| 621 | void *operator new(size_t s) { |
| 622 | return User::operator new(s, 2); |
| 623 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 624 | BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 625 | : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 626 | Op<0>() = C1; |
| 627 | Op<1>() = C2; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 628 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 629 | /// Transparently provide more efficient getOperand methods. |
| 630 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 631 | }; |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 632 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 633 | /// SelectConstantExpr - This class is private to Constants.cpp, and is used |
| 634 | /// behind the scenes to implement select constant exprs. |
| 635 | class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 636 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 637 | public: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 638 | // allocate space for exactly three operands |
| 639 | void *operator new(size_t s) { |
| 640 | return User::operator new(s, 3); |
| 641 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 642 | SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 643 | : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 644 | Op<0>() = C1; |
| 645 | Op<1>() = C2; |
| 646 | Op<2>() = C3; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 647 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 648 | /// Transparently provide more efficient getOperand methods. |
| 649 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 650 | }; |
Chris Lattner | e467147 | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 651 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 652 | /// ExtractElementConstantExpr - This class is private to |
| 653 | /// Constants.cpp, and is used behind the scenes to implement |
| 654 | /// extractelement constant exprs. |
| 655 | class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 656 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 657 | public: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 658 | // allocate space for exactly two operands |
| 659 | void *operator new(size_t s) { |
| 660 | return User::operator new(s, 2); |
| 661 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 662 | ExtractElementConstantExpr(Constant *C1, Constant *C2) |
| 663 | : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 664 | Instruction::ExtractElement, &Op<0>(), 2) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 665 | Op<0>() = C1; |
| 666 | Op<1>() = C2; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 667 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 668 | /// Transparently provide more efficient getOperand methods. |
| 669 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 670 | }; |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 671 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 672 | /// InsertElementConstantExpr - This class is private to |
| 673 | /// Constants.cpp, and is used behind the scenes to implement |
| 674 | /// insertelement constant exprs. |
| 675 | class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 676 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 677 | public: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 678 | // allocate space for exactly three operands |
| 679 | void *operator new(size_t s) { |
| 680 | return User::operator new(s, 3); |
| 681 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 682 | InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 683 | : ConstantExpr(C1->getType(), Instruction::InsertElement, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 684 | &Op<0>(), 3) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 685 | Op<0>() = C1; |
| 686 | Op<1>() = C2; |
| 687 | Op<2>() = C3; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 688 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 689 | /// Transparently provide more efficient getOperand methods. |
| 690 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 691 | }; |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 692 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 693 | /// ShuffleVectorConstantExpr - This class is private to |
| 694 | /// Constants.cpp, and is used behind the scenes to implement |
| 695 | /// shufflevector constant exprs. |
| 696 | class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 697 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 698 | public: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 699 | // allocate space for exactly three operands |
| 700 | void *operator new(size_t s) { |
| 701 | return User::operator new(s, 3); |
| 702 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 703 | ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
Nate Begeman | 0f123cf | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 704 | : ConstantExpr(VectorType::get( |
| 705 | cast<VectorType>(C1->getType())->getElementType(), |
| 706 | cast<VectorType>(C3->getType())->getNumElements()), |
| 707 | Instruction::ShuffleVector, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 708 | &Op<0>(), 3) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 709 | Op<0>() = C1; |
| 710 | Op<1>() = C2; |
| 711 | Op<2>() = C3; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 712 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 713 | /// Transparently provide more efficient getOperand methods. |
| 714 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 717 | /// ExtractValueConstantExpr - This class is private to |
| 718 | /// Constants.cpp, and is used behind the scenes to implement |
| 719 | /// extractvalue constant exprs. |
| 720 | class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 721 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 722 | public: |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 723 | // allocate space for exactly one operand |
| 724 | void *operator new(size_t s) { |
| 725 | return User::operator new(s, 1); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 726 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 727 | ExtractValueConstantExpr(Constant *Agg, |
| 728 | const SmallVector<unsigned, 4> &IdxList, |
| 729 | const Type *DestTy) |
| 730 | : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1), |
| 731 | Indices(IdxList) { |
| 732 | Op<0>() = Agg; |
| 733 | } |
| 734 | |
Dan Gohman | 35651cd | 2008-05-31 19:09:08 +0000 | [diff] [blame] | 735 | /// Indices - These identify which value to extract. |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 736 | const SmallVector<unsigned, 4> Indices; |
| 737 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 738 | /// Transparently provide more efficient getOperand methods. |
| 739 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 740 | }; |
| 741 | |
| 742 | /// InsertValueConstantExpr - This class is private to |
| 743 | /// Constants.cpp, and is used behind the scenes to implement |
| 744 | /// insertvalue constant exprs. |
| 745 | class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 746 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 747 | public: |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 748 | // allocate space for exactly one operand |
| 749 | void *operator new(size_t s) { |
| 750 | return User::operator new(s, 2); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 751 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 752 | InsertValueConstantExpr(Constant *Agg, Constant *Val, |
| 753 | const SmallVector<unsigned, 4> &IdxList, |
| 754 | const Type *DestTy) |
| 755 | : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2), |
| 756 | Indices(IdxList) { |
| 757 | Op<0>() = Agg; |
| 758 | Op<1>() = Val; |
| 759 | } |
| 760 | |
Dan Gohman | 35651cd | 2008-05-31 19:09:08 +0000 | [diff] [blame] | 761 | /// Indices - These identify the position for the insertion. |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 762 | const SmallVector<unsigned, 4> Indices; |
| 763 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 764 | /// Transparently provide more efficient getOperand methods. |
| 765 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 766 | }; |
| 767 | |
| 768 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 769 | /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is |
| 770 | /// used behind the scenes to implement getelementpr constant exprs. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 771 | class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 772 | GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 773 | const Type *DestTy); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 774 | public: |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 775 | static GetElementPtrConstantExpr *Create(Constant *C, |
| 776 | const std::vector<Constant*>&IdxList, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 777 | const Type *DestTy) { |
Dan Gohman | f241174 | 2009-07-20 17:43:30 +0000 | [diff] [blame] | 778 | return |
| 779 | new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 780 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 781 | /// Transparently provide more efficient getOperand methods. |
| 782 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 783 | }; |
| 784 | |
| 785 | // CompareConstantExpr - This class is private to Constants.cpp, and is used |
| 786 | // behind the scenes to implement ICmp and FCmp constant expressions. This is |
| 787 | // needed in order to store the predicate value for these instructions. |
| 788 | struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 789 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 790 | // allocate space for exactly two operands |
| 791 | void *operator new(size_t s) { |
| 792 | return User::operator new(s, 2); |
| 793 | } |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 794 | unsigned short predicate; |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 795 | CompareConstantExpr(const Type *ty, Instruction::OtherOps opc, |
| 796 | unsigned short pred, Constant* LHS, Constant* RHS) |
| 797 | : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 798 | Op<0>() = LHS; |
| 799 | Op<1>() = RHS; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 800 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 801 | /// Transparently provide more efficient getOperand methods. |
| 802 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | } // end anonymous namespace |
| 806 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 807 | template <> |
| 808 | struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> { |
| 809 | }; |
| 810 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value) |
| 811 | |
| 812 | template <> |
| 813 | struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> { |
| 814 | }; |
| 815 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value) |
| 816 | |
| 817 | template <> |
| 818 | struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> { |
| 819 | }; |
| 820 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value) |
| 821 | |
| 822 | template <> |
| 823 | struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> { |
| 824 | }; |
| 825 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value) |
| 826 | |
| 827 | template <> |
| 828 | struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> { |
| 829 | }; |
| 830 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value) |
| 831 | |
| 832 | template <> |
| 833 | struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> { |
| 834 | }; |
| 835 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value) |
| 836 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 837 | template <> |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 838 | struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 839 | }; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 840 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value) |
| 841 | |
| 842 | template <> |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 843 | struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 844 | }; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 845 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value) |
| 846 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 847 | template <> |
| 848 | struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> { |
| 849 | }; |
| 850 | |
| 851 | GetElementPtrConstantExpr::GetElementPtrConstantExpr |
| 852 | (Constant *C, |
| 853 | const std::vector<Constant*> &IdxList, |
| 854 | const Type *DestTy) |
| 855 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 856 | OperandTraits<GetElementPtrConstantExpr>::op_end(this) |
| 857 | - (IdxList.size()+1), |
| 858 | IdxList.size()+1) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 859 | OperandList[0] = C; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 860 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 861 | OperandList[i+1] = IdxList[i]; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value) |
| 865 | |
| 866 | |
| 867 | template <> |
| 868 | struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> { |
| 869 | }; |
| 870 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) |
| 871 | |
| 872 | |
| 873 | } // End llvm namespace |
| 874 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 875 | |
| 876 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 877 | // can't be inline because we don't want to #include Instruction.h into |
| 878 | // Constant.h |
| 879 | bool ConstantExpr::isCast() const { |
| 880 | return Instruction::isCast(getOpcode()); |
| 881 | } |
| 882 | |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 883 | bool ConstantExpr::isCompare() const { |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 884 | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 887 | bool ConstantExpr::hasIndices() const { |
| 888 | return getOpcode() == Instruction::ExtractValue || |
| 889 | getOpcode() == Instruction::InsertValue; |
| 890 | } |
| 891 | |
| 892 | const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const { |
| 893 | if (const ExtractValueConstantExpr *EVCE = |
| 894 | dyn_cast<ExtractValueConstantExpr>(this)) |
| 895 | return EVCE->Indices; |
Dan Gohman | 1a20357 | 2008-06-23 16:39:44 +0000 | [diff] [blame] | 896 | |
| 897 | return cast<InsertValueConstantExpr>(this)->Indices; |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 900 | unsigned ConstantExpr::getPredicate() const { |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 901 | assert(getOpcode() == Instruction::FCmp || |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 902 | getOpcode() == Instruction::ICmp); |
Chris Lattner | b7daa84 | 2007-10-18 16:26:24 +0000 | [diff] [blame] | 903 | return ((const CompareConstantExpr*)this)->predicate; |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 904 | } |
Chris Lattner | f4ba6c7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 906 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 907 | /// one, but with the specified operand set to the specified value. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 908 | Constant * |
| 909 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 910 | assert(OpNo < getNumOperands() && "Operand num is out of range!"); |
| 911 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 912 | "Replacing operand with value of different type!"); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 913 | if (getOperand(OpNo) == Op) |
| 914 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 915 | |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 916 | Constant *Op0, *Op1, *Op2; |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 917 | switch (getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 918 | case Instruction::Trunc: |
| 919 | case Instruction::ZExt: |
| 920 | case Instruction::SExt: |
| 921 | case Instruction::FPTrunc: |
| 922 | case Instruction::FPExt: |
| 923 | case Instruction::UIToFP: |
| 924 | case Instruction::SIToFP: |
| 925 | case Instruction::FPToUI: |
| 926 | case Instruction::FPToSI: |
| 927 | case Instruction::PtrToInt: |
| 928 | case Instruction::IntToPtr: |
| 929 | case Instruction::BitCast: |
| 930 | return ConstantExpr::getCast(getOpcode(), Op, getType()); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 931 | case Instruction::Select: |
| 932 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 933 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 934 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 935 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 936 | case Instruction::InsertElement: |
| 937 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 938 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 939 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 940 | return ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 941 | case Instruction::ExtractElement: |
| 942 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 943 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 944 | return ConstantExpr::getExtractElement(Op0, Op1); |
| 945 | case Instruction::ShuffleVector: |
| 946 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 947 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 948 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 949 | return ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 950 | case Instruction::GetElementPtr: { |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 951 | SmallVector<Constant*, 8> Ops; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 952 | Ops.resize(getNumOperands()-1); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 953 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 954 | Ops[i-1] = getOperand(i); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 955 | if (OpNo == 0) |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 956 | return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size()); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 957 | Ops[OpNo-1] = Op; |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 958 | return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size()); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 959 | } |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 960 | default: |
| 961 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 962 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 963 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 964 | return ConstantExpr::get(getOpcode(), Op0, Op1); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /// getWithOperands - This returns the current constant expression with the |
| 969 | /// operands replaced with the specified values. The specified operands must |
| 970 | /// match count and type with the existing ones. |
| 971 | Constant *ConstantExpr:: |
Chris Lattner | b054bfd | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 972 | getWithOperands(Constant* const *Ops, unsigned NumOps) const { |
| 973 | assert(NumOps == getNumOperands() && "Operand count mismatch!"); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 974 | bool AnyChange = false; |
Chris Lattner | b054bfd | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 975 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 976 | assert(Ops[i]->getType() == getOperand(i)->getType() && |
| 977 | "Operand type mismatch!"); |
| 978 | AnyChange |= Ops[i] != getOperand(i); |
| 979 | } |
| 980 | if (!AnyChange) // No operands changed, return self. |
| 981 | return const_cast<ConstantExpr*>(this); |
| 982 | |
| 983 | switch (getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 984 | case Instruction::Trunc: |
| 985 | case Instruction::ZExt: |
| 986 | case Instruction::SExt: |
| 987 | case Instruction::FPTrunc: |
| 988 | case Instruction::FPExt: |
| 989 | case Instruction::UIToFP: |
| 990 | case Instruction::SIToFP: |
| 991 | case Instruction::FPToUI: |
| 992 | case Instruction::FPToSI: |
| 993 | case Instruction::PtrToInt: |
| 994 | case Instruction::IntToPtr: |
| 995 | case Instruction::BitCast: |
| 996 | return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 997 | case Instruction::Select: |
| 998 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 999 | case Instruction::InsertElement: |
| 1000 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 1001 | case Instruction::ExtractElement: |
| 1002 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 1003 | case Instruction::ShuffleVector: |
| 1004 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 1005 | case Instruction::GetElementPtr: |
Chris Lattner | b054bfd | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 1006 | return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1007 | case Instruction::ICmp: |
| 1008 | case Instruction::FCmp: |
| 1009 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1010 | default: |
| 1011 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 1012 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1016 | |
| 1017 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1018 | // isValueValidForType implementations |
| 1019 | |
Reid Spencer | 9b11d51 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 1020 | bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1021 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1022 | if (Ty == Type::Int1Ty) |
| 1023 | return Val == 0 || Val == 1; |
Reid Spencer | 554cec6 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 1024 | if (NumBits >= 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1025 | return true; // always true, has to fit in largest type |
| 1026 | uint64_t Max = (1ll << NumBits) - 1; |
| 1027 | return Val <= Max; |
Reid Spencer | 9b11d51 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1030 | bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1031 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1032 | if (Ty == Type::Int1Ty) |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1033 | return Val == 0 || Val == 1 || Val == -1; |
Reid Spencer | 554cec6 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 1034 | if (NumBits >= 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1035 | return true; // always true, has to fit in largest type |
| 1036 | int64_t Min = -(1ll << (NumBits-1)); |
| 1037 | int64_t Max = (1ll << (NumBits-1)) - 1; |
| 1038 | return (Val >= Min && Val <= Max); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1041 | bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { |
| 1042 | // convert modifies in place, so make a copy. |
| 1043 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1044 | bool losesInfo; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 1045 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1046 | default: |
| 1047 | return false; // These can't be represented as floating point! |
| 1048 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1049 | // FIXME rounding mode needs to be more flexible |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1050 | case Type::FloatTyID: { |
| 1051 | if (&Val2.getSemantics() == &APFloat::IEEEsingle) |
| 1052 | return true; |
| 1053 | Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1054 | return !losesInfo; |
| 1055 | } |
| 1056 | case Type::DoubleTyID: { |
| 1057 | if (&Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1058 | &Val2.getSemantics() == &APFloat::IEEEdouble) |
| 1059 | return true; |
| 1060 | Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1061 | return !losesInfo; |
| 1062 | } |
Dale Johannesen | ebbc95d | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1063 | case Type::X86_FP80TyID: |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1064 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1065 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1066 | &Val2.getSemantics() == &APFloat::x87DoubleExtended; |
Dale Johannesen | ebbc95d | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1067 | case Type::FP128TyID: |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1068 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1069 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1070 | &Val2.getSemantics() == &APFloat::IEEEquad; |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 1071 | case Type::PPC_FP128TyID: |
| 1072 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 1073 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1074 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1075 | } |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 1076 | } |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1078 | //===----------------------------------------------------------------------===// |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1079 | // Factory Function Implementation |
| 1080 | |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1081 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 1082 | |
| 1083 | ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) { |
| 1084 | assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) && |
| 1085 | "Cannot create an aggregate zero of non-aggregate type!"); |
| 1086 | |
| 1087 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 1088 | // Implicitly locked. |
| 1089 | return pImpl->AggZeroConstants.getOrCreate(Ty, 0); |
| 1090 | } |
| 1091 | |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1092 | /// destroyConstant - Remove the constant from the constant table... |
| 1093 | /// |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1094 | void ConstantAggregateZero::destroyConstant() { |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1095 | // Implicitly locked. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1096 | getType()->getContext().pImpl->AggZeroConstants.remove(this); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1097 | destroyConstantImpl(); |
| 1098 | } |
| 1099 | |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1100 | /// destroyConstant - Remove the constant from the constant table... |
| 1101 | /// |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1102 | void ConstantArray::destroyConstant() { |
Owen Anderson | 1f0ba8c | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1103 | // Implicitly locked. |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1104 | getType()->getContext().pImpl->ArrayConstants.remove(this); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1105 | destroyConstantImpl(); |
| 1106 | } |
| 1107 | |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1108 | /// isString - This method returns true if the array is an array of i8, and |
| 1109 | /// if the elements of the array are all ConstantInt's. |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1110 | bool ConstantArray::isString() const { |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1111 | // Check the element type for i8... |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1112 | if (getType()->getElementType() != Type::Int8Ty) |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1113 | return false; |
| 1114 | // Check the elements to make sure they are all integers, not constant |
| 1115 | // expressions. |
| 1116 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1117 | if (!isa<ConstantInt>(getOperand(i))) |
| 1118 | return false; |
| 1119 | return true; |
| 1120 | } |
| 1121 | |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1122 | /// isCString - This method returns true if the array is a string (see |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1123 | /// isString) and it ends in a null byte \\0 and does not contains any other |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1124 | /// null bytes except its terminator. |
Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 1125 | bool ConstantArray::isCString() const { |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1126 | // Check the element type for i8... |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1127 | if (getType()->getElementType() != Type::Int8Ty) |
Evan Cheng | abf6345 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1128 | return false; |
Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 1129 | |
Evan Cheng | abf6345 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1130 | // Last element must be a null. |
Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 1131 | if (!getOperand(getNumOperands()-1)->isNullValue()) |
Evan Cheng | abf6345 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1132 | return false; |
| 1133 | // Other elements must be non-null integers. |
| 1134 | for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { |
| 1135 | if (!isa<ConstantInt>(getOperand(i))) |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1136 | return false; |
Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 1137 | if (getOperand(i)->isNullValue()) |
Evan Cheng | abf6345 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
Evan Cheng | 22c7030 | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 1140 | return true; |
| 1141 | } |
| 1142 | |
| 1143 | |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1144 | /// getAsString - If the sub-element type of this array is i8 |
| 1145 | /// then this method converts the array to an std::string and returns it. |
| 1146 | /// Otherwise, it asserts out. |
| 1147 | /// |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1148 | std::string ConstantArray::getAsString() const { |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1149 | assert(isString() && "Not a string!"); |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1150 | std::string Result; |
Owen Anderson | 45e3958 | 2008-06-24 21:58:29 +0000 | [diff] [blame] | 1151 | Result.reserve(getNumOperands()); |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 1152 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Owen Anderson | e4f6ee5 | 2008-06-25 01:05:05 +0000 | [diff] [blame] | 1153 | Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1154 | return Result; |
| 1155 | } |
| 1156 | |
| 1157 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1158 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1159 | // |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1161 | namespace llvm { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1165 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1166 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1167 | void ConstantStruct::destroyConstant() { |
Owen Anderson | 1f0ba8c | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1168 | // Implicitly locked. |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 1169 | getType()->getContext().pImpl->StructConstants.remove(this); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1170 | destroyConstantImpl(); |
| 1171 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1172 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1173 | // destroyConstant - Remove the constant from the constant table... |
| 1174 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1175 | void ConstantVector::destroyConstant() { |
Owen Anderson | 1f0ba8c | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1176 | // Implicitly locked. |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1177 | getType()->getContext().pImpl->VectorConstants.remove(this); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1178 | destroyConstantImpl(); |
| 1179 | } |
| 1180 | |
Dan Gohman | fa73ea2 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 1181 | /// This function will return true iff every element in this vector constant |
Jim Laskey | fa30182 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 1182 | /// is set to all ones. |
| 1183 | /// @returns true iff this constant's emements are all set to all ones. |
| 1184 | /// @brief Determine if the value is all ones. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1185 | bool ConstantVector::isAllOnesValue() const { |
Jim Laskey | fa30182 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 1186 | // Check out first element. |
| 1187 | const Constant *Elt = getOperand(0); |
| 1188 | const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); |
| 1189 | if (!CI || !CI->isAllOnesValue()) return false; |
| 1190 | // Then make sure all remaining elements point to the same value. |
| 1191 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { |
| 1192 | if (getOperand(I) != Elt) return false; |
| 1193 | } |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
Dan Gohman | 3b7cf0a | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1197 | /// getSplatValue - If this is a splat constant, where all of the |
| 1198 | /// elements have the same value, return that value. Otherwise return null. |
| 1199 | Constant *ConstantVector::getSplatValue() { |
| 1200 | // Check out first element. |
| 1201 | Constant *Elt = getOperand(0); |
| 1202 | // Then make sure all remaining elements point to the same value. |
| 1203 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) |
| 1204 | if (getOperand(I) != Elt) return 0; |
| 1205 | return Elt; |
| 1206 | } |
| 1207 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1208 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1209 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1210 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1211 | namespace llvm { |
| 1212 | // ConstantPointerNull does not take extra "value" argument... |
| 1213 | template<class ValType> |
| 1214 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 1215 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 1216 | return new ConstantPointerNull(Ty); |
| 1217 | } |
| 1218 | }; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1220 | template<> |
| 1221 | struct ConvertConstantType<ConstantPointerNull, PointerType> { |
| 1222 | static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { |
| 1223 | // Make everyone now use a constant of the new type... |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1224 | Constant *New = ConstantPointerNull::get(NewTy); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1225 | assert(New != OldC && "Didn't replace constant??"); |
| 1226 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1227 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1228 | } |
| 1229 | }; |
| 1230 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1232 | static ManagedStatic<ValueMap<char, PointerType, |
| 1233 | ConstantPointerNull> > NullPtrConstants; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1235 | static char getValType(ConstantPointerNull *) { |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1240 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1241 | // Implicitly locked. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1242 | return NullPtrConstants->getOrCreate(Ty, 0); |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1245 | // destroyConstant - Remove the constant from the constant table... |
| 1246 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1247 | void ConstantPointerNull::destroyConstant() { |
Owen Anderson | 1f0ba8c | 2009-06-19 18:34:09 +0000 | [diff] [blame] | 1248 | // Implicitly locked. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1249 | NullPtrConstants->remove(this); |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1250 | destroyConstantImpl(); |
| 1251 | } |
| 1252 | |
| 1253 | |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1254 | //---- UndefValue::get() implementation... |
| 1255 | // |
| 1256 | |
| 1257 | namespace llvm { |
| 1258 | // UndefValue does not take extra "value" argument... |
| 1259 | template<class ValType> |
| 1260 | struct ConstantCreator<UndefValue, Type, ValType> { |
| 1261 | static UndefValue *create(const Type *Ty, const ValType &V) { |
| 1262 | return new UndefValue(Ty); |
| 1263 | } |
| 1264 | }; |
| 1265 | |
| 1266 | template<> |
| 1267 | struct ConvertConstantType<UndefValue, Type> { |
| 1268 | static void convert(UndefValue *OldC, const Type *NewTy) { |
| 1269 | // Make everyone now use a constant of the new type. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1270 | Constant *New = UndefValue::get(NewTy); |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1271 | assert(New != OldC && "Didn't replace constant??"); |
| 1272 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1273 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1274 | } |
| 1275 | }; |
| 1276 | } |
| 1277 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1278 | static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants; |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1279 | |
| 1280 | static char getValType(UndefValue *) { |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1285 | UndefValue *UndefValue::get(const Type *Ty) { |
| 1286 | // Implicitly locked. |
| 1287 | return UndefValueConstants->getOrCreate(Ty, 0); |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | // destroyConstant - Remove the constant from the constant table. |
| 1291 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1292 | void UndefValue::destroyConstant() { |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1293 | // Implicitly locked. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1294 | UndefValueConstants->remove(this); |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1295 | destroyConstantImpl(); |
| 1296 | } |
| 1297 | |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1298 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1299 | // |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1300 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1301 | namespace { |
| 1302 | |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1303 | struct ExprMapKeyType { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1304 | typedef SmallVector<unsigned, 4> IndexList; |
| 1305 | |
| 1306 | ExprMapKeyType(unsigned opc, |
| 1307 | const std::vector<Constant*> &ops, |
| 1308 | unsigned short pred = 0, |
| 1309 | const IndexList &inds = IndexList()) |
| 1310 | : opcode(opc), predicate(pred), operands(ops), indices(inds) {} |
Reid Spencer | 8d5a6ae | 2006-12-04 18:38:05 +0000 | [diff] [blame] | 1311 | uint16_t opcode; |
| 1312 | uint16_t predicate; |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1313 | std::vector<Constant*> operands; |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1314 | IndexList indices; |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1315 | bool operator==(const ExprMapKeyType& that) const { |
| 1316 | return this->opcode == that.opcode && |
| 1317 | this->predicate == that.predicate && |
Bill Wendling | 9a20afd | 2008-10-26 00:19:56 +0000 | [diff] [blame] | 1318 | this->operands == that.operands && |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1319 | this->indices == that.indices; |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1320 | } |
| 1321 | bool operator<(const ExprMapKeyType & that) const { |
| 1322 | return this->opcode < that.opcode || |
| 1323 | (this->opcode == that.opcode && this->predicate < that.predicate) || |
| 1324 | (this->opcode == that.opcode && this->predicate == that.predicate && |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1325 | this->operands < that.operands) || |
| 1326 | (this->opcode == that.opcode && this->predicate == that.predicate && |
| 1327 | this->operands == that.operands && this->indices < that.indices); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | bool operator!=(const ExprMapKeyType& that) const { |
| 1331 | return !(*this == that); |
| 1332 | } |
| 1333 | }; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1334 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1337 | namespace llvm { |
| 1338 | template<> |
| 1339 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 1340 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V, |
| 1341 | unsigned short pred = 0) { |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1342 | if (Instruction::isCast(V.opcode)) |
| 1343 | return new UnaryConstantExpr(V.opcode, V.operands[0], Ty); |
| 1344 | if ((V.opcode >= Instruction::BinaryOpsBegin && |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1345 | V.opcode < Instruction::BinaryOpsEnd)) |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1346 | return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]); |
| 1347 | if (V.opcode == Instruction::Select) |
| 1348 | return new SelectConstantExpr(V.operands[0], V.operands[1], |
| 1349 | V.operands[2]); |
| 1350 | if (V.opcode == Instruction::ExtractElement) |
| 1351 | return new ExtractElementConstantExpr(V.operands[0], V.operands[1]); |
| 1352 | if (V.opcode == Instruction::InsertElement) |
| 1353 | return new InsertElementConstantExpr(V.operands[0], V.operands[1], |
| 1354 | V.operands[2]); |
| 1355 | if (V.opcode == Instruction::ShuffleVector) |
| 1356 | return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1], |
| 1357 | V.operands[2]); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1358 | if (V.opcode == Instruction::InsertValue) |
| 1359 | return new InsertValueConstantExpr(V.operands[0], V.operands[1], |
| 1360 | V.indices, Ty); |
| 1361 | if (V.opcode == Instruction::ExtractValue) |
| 1362 | return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1363 | if (V.opcode == Instruction::GetElementPtr) { |
| 1364 | std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end()); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1365 | return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1366 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1367 | |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1368 | // The compare instructions are weird. We have to encode the predicate |
| 1369 | // value and it is combined with the instruction opcode by multiplying |
| 1370 | // the opcode by one hundred. We must decode this to get the predicate. |
| 1371 | if (V.opcode == Instruction::ICmp) |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1372 | return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate, |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1373 | V.operands[0], V.operands[1]); |
| 1374 | if (V.opcode == Instruction::FCmp) |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1375 | return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate, |
| 1376 | V.operands[0], V.operands[1]); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1377 | llvm_unreachable("Invalid ConstantExpr!"); |
Jeff Cohen | 6ebe235 | 2006-12-15 21:47:01 +0000 | [diff] [blame] | 1378 | return 0; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1379 | } |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1380 | }; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1382 | template<> |
| 1383 | struct ConvertConstantType<ConstantExpr, Type> { |
| 1384 | static void convert(ConstantExpr *OldC, const Type *NewTy) { |
| 1385 | Constant *New; |
| 1386 | switch (OldC->getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1387 | case Instruction::Trunc: |
| 1388 | case Instruction::ZExt: |
| 1389 | case Instruction::SExt: |
| 1390 | case Instruction::FPTrunc: |
| 1391 | case Instruction::FPExt: |
| 1392 | case Instruction::UIToFP: |
| 1393 | case Instruction::SIToFP: |
| 1394 | case Instruction::FPToUI: |
| 1395 | case Instruction::FPToSI: |
| 1396 | case Instruction::PtrToInt: |
| 1397 | case Instruction::IntToPtr: |
| 1398 | case Instruction::BitCast: |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1399 | New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0), |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1400 | NewTy); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1401 | break; |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1402 | case Instruction::Select: |
| 1403 | New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), |
| 1404 | OldC->getOperand(1), |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1405 | OldC->getOperand(2)); |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1406 | break; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1407 | default: |
| 1408 | assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1409 | OldC->getOpcode() < Instruction::BinaryOpsEnd); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1410 | New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1411 | OldC->getOperand(1)); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | case Instruction::GetElementPtr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1414 | // Make everyone now use a constant of the new type... |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1415 | std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1416 | New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1417 | &Idx[0], Idx.size()); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1421 | assert(New != OldC && "Didn't replace constant??"); |
| 1422 | OldC->uncheckedReplaceAllUsesWith(New); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1423 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1424 | } |
| 1425 | }; |
| 1426 | } // end namespace llvm |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1427 | |
| 1428 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1429 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1430 | std::vector<Constant*> Operands; |
| 1431 | Operands.reserve(CE->getNumOperands()); |
| 1432 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1433 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1434 | return ExprMapKeyType(CE->getOpcode(), Operands, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1435 | CE->isCompare() ? CE->getPredicate() : 0, |
| 1436 | CE->hasIndices() ? |
| 1437 | CE->getIndices() : SmallVector<unsigned, 4>()); |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 1440 | static ManagedStatic<ValueMap<ExprMapKeyType, Type, |
| 1441 | ConstantExpr> > ExprConstants; |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1442 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1443 | /// This is a utility function to handle folding of casts and lookup of the |
Duncan Sands | 66a1a05 | 2008-03-30 19:38:55 +0000 | [diff] [blame] | 1444 | /// cast in the ExprConstants map. It is used by the various get* methods below. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1445 | static inline Constant *getFoldedCast( |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1446 | Instruction::CastOps opc, Constant *C, const Type *Ty) { |
Chris Lattner | 9eacf8a | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1447 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1448 | // Fold a few common cases |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1449 | if (Constant *FC = ConstantFoldCastInstruction(Ty->getContext(), opc, C, Ty)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1450 | return FC; |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1451 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1452 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1453 | std::vector<Constant*> argVec(1, C); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1454 | ExprMapKeyType Key(opc, argVec); |
Owen Anderson | 3e456ab | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1455 | |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1456 | // Implicitly locked. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1457 | return ExprConstants->getOrCreate(Ty, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1458 | } |
Reid Spencer | 7858b33 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1459 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1460 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1461 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1462 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1463 | assert(C && Ty && "Null arguments to getCast"); |
| 1464 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1465 | |
| 1466 | switch (opc) { |
| 1467 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1468 | llvm_unreachable("Invalid cast opcode"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1469 | break; |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1470 | case Instruction::Trunc: return getTrunc(C, Ty); |
| 1471 | case Instruction::ZExt: return getZExt(C, Ty); |
| 1472 | case Instruction::SExt: return getSExt(C, Ty); |
| 1473 | case Instruction::FPTrunc: return getFPTrunc(C, Ty); |
| 1474 | case Instruction::FPExt: return getFPExtend(C, Ty); |
| 1475 | case Instruction::UIToFP: return getUIToFP(C, Ty); |
| 1476 | case Instruction::SIToFP: return getSIToFP(C, Ty); |
| 1477 | case Instruction::FPToUI: return getFPToUI(C, Ty); |
| 1478 | case Instruction::FPToSI: return getFPToSI(C, Ty); |
| 1479 | case Instruction::PtrToInt: return getPtrToInt(C, Ty); |
| 1480 | case Instruction::IntToPtr: return getIntToPtr(C, Ty); |
| 1481 | case Instruction::BitCast: return getBitCast(C, Ty); |
Chris Lattner | f5ac6c2 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1482 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1483 | return 0; |
Reid Spencer | 7858b33 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1486 | Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1487 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1488 | return getCast(Instruction::BitCast, C, Ty); |
| 1489 | return getCast(Instruction::ZExt, C, Ty); |
| 1490 | } |
| 1491 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1492 | Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1493 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1494 | return getCast(Instruction::BitCast, C, Ty); |
| 1495 | return getCast(Instruction::SExt, C, Ty); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1499 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1500 | return getCast(Instruction::BitCast, C, Ty); |
| 1501 | return getCast(Instruction::Trunc, C, Ty); |
| 1502 | } |
| 1503 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1504 | Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) { |
Reid Spencer | c0459fb | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1505 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1506 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast"); |
Reid Spencer | c0459fb | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1508 | if (Ty->isInteger()) |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1509 | return getCast(Instruction::PtrToInt, S, Ty); |
| 1510 | return getCast(Instruction::BitCast, S, Ty); |
Reid Spencer | c0459fb | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1513 | Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty, |
| 1514 | bool isSigned) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1515 | assert(C->getType()->isIntOrIntVector() && |
| 1516 | Ty->isIntOrIntVector() && "Invalid cast"); |
| 1517 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1518 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1519 | Instruction::CastOps opcode = |
| 1520 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1521 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1522 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1523 | return getCast(opcode, C, Ty); |
| 1524 | } |
| 1525 | |
| 1526 | Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1527 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1528 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1529 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1530 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | f25212a | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1531 | if (SrcBits == DstBits) |
| 1532 | return C; // Avoid a useless cast |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1533 | Instruction::CastOps opcode = |
Reid Spencer | f25212a | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1534 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1535 | return getCast(opcode, C, Ty); |
| 1536 | } |
| 1537 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1538 | Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1539 | #ifndef NDEBUG |
| 1540 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1541 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1542 | #endif |
| 1543 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1544 | assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer"); |
| 1545 | assert(Ty->isIntOrIntVector() && "Trunc produces only integral"); |
| 1546 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1547 | "SrcTy must be larger than DestTy for Trunc!"); |
| 1548 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1549 | return getFoldedCast(Instruction::Trunc, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1552 | Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1553 | #ifndef NDEBUG |
| 1554 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1555 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1556 | #endif |
| 1557 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1558 | assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral"); |
| 1559 | assert(Ty->isIntOrIntVector() && "SExt produces only integer"); |
| 1560 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1561 | "SrcTy must be smaller than DestTy for SExt!"); |
| 1562 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1563 | return getFoldedCast(Instruction::SExt, C, Ty); |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1566 | Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1567 | #ifndef NDEBUG |
| 1568 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1569 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1570 | #endif |
| 1571 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1572 | assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral"); |
| 1573 | assert(Ty->isIntOrIntVector() && "ZExt produces only integer"); |
| 1574 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1575 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 1576 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1577 | return getFoldedCast(Instruction::ZExt, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1580 | Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1581 | #ifndef NDEBUG |
| 1582 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1583 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1584 | #endif |
| 1585 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1586 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
| 1587 | C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1588 | "This is an illegal floating point truncation!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1589 | return getFoldedCast(Instruction::FPTrunc, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1592 | Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1593 | #ifndef NDEBUG |
| 1594 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1595 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1596 | #endif |
| 1597 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1598 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
| 1599 | C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1600 | "This is an illegal floating point extension!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1601 | return getFoldedCast(Instruction::FPExt, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1604 | Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1605 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1606 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1607 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1608 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1609 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1610 | assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && |
| 1611 | "This is an illegal uint to floating point cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1612 | return getFoldedCast(Instruction::UIToFP, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1615 | Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1616 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1617 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1618 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1619 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1620 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1621 | assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1622 | "This is an illegal sint to floating point cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1623 | return getFoldedCast(Instruction::SIToFP, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1626 | Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1627 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1628 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1629 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1630 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1631 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1632 | assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && |
| 1633 | "This is an illegal floating point to uint cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1634 | return getFoldedCast(Instruction::FPToUI, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1637 | Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1638 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1639 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1640 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1641 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1642 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
| 1643 | assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() && |
| 1644 | "This is an illegal floating point to sint cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1645 | return getFoldedCast(Instruction::FPToSI, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1648 | Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1649 | assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer"); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1650 | assert(DstTy->isInteger() && "PtrToInt destination must be integral"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1651 | return getFoldedCast(Instruction::PtrToInt, C, DstTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1654 | Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1655 | assert(C->getType()->isInteger() && "IntToPtr source must be integral"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1656 | assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1657 | return getFoldedCast(Instruction::IntToPtr, C, DstTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1660 | Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1661 | // BitCast implies a no-op cast of type only. No bits change. However, you |
| 1662 | // can't cast pointers to anything but pointers. |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1663 | #ifndef NDEBUG |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1664 | const Type *SrcTy = C->getType(); |
| 1665 | assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) && |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1666 | "BitCast cannot cast pointer to non-pointer and vice versa"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1667 | |
| 1668 | // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr |
| 1669 | // or nonptr->ptr). For all the other types, the cast is okay if source and |
| 1670 | // destination bit widths are identical. |
| 1671 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1672 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1673 | #endif |
Chris Lattner | cf1bb08 | 2009-03-08 04:06:26 +0000 | [diff] [blame] | 1674 | assert(SrcBitSize == DstBitSize && "BitCast requires types of same width"); |
Chris Lattner | 8c7f24a | 2009-03-21 06:55:54 +0000 | [diff] [blame] | 1675 | |
| 1676 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 1677 | // speedily. |
| 1678 | if (C->getType() == DstTy) return C; |
| 1679 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1680 | return getFoldedCast(Instruction::BitCast, C, DstTy); |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1683 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1684 | Constant *C1, Constant *C2) { |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1685 | // Check the operands for consistency first |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1686 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 1687 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1688 | "Invalid opcode in binary constant expression"); |
| 1689 | assert(C1->getType() == C2->getType() && |
| 1690 | "Operand types in binary constant expression should match"); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1691 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1692 | if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty) |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1693 | if (Constant *FC = ConstantFoldBinaryInstruction(ReqTy->getContext(), |
| 1694 | Opcode, C1, C2)) |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1695 | return FC; // Fold a few common cases... |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1697 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Reid Spencer | 67263fe | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1698 | ExprMapKeyType Key(Opcode, argVec); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1699 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1700 | // Implicitly locked. |
| 1701 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1704 | Constant *ConstantExpr::getCompareTy(unsigned short predicate, |
Nate Begeman | ff795a8 | 2008-07-25 17:56:27 +0000 | [diff] [blame] | 1705 | Constant *C1, Constant *C2) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1706 | switch (predicate) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1707 | default: llvm_unreachable("Invalid CmpInst predicate"); |
Nate Begeman | b5557ab | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 1708 | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
| 1709 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
| 1710 | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
| 1711 | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
| 1712 | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
| 1713 | case CmpInst::FCMP_TRUE: |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1714 | return getFCmp(predicate, C1, C2); |
| 1715 | |
Nate Begeman | b5557ab | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 1716 | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
| 1717 | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
| 1718 | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
| 1719 | case CmpInst::ICMP_SLE: |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1720 | return getICmp(predicate, C1, C2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1721 | } |
Reid Spencer | 67263fe | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1724 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1725 | // API compatibility: Adjust integer opcodes to floating-point opcodes. |
| 1726 | if (C1->getType()->isFPOrFPVector()) { |
| 1727 | if (Opcode == Instruction::Add) Opcode = Instruction::FAdd; |
| 1728 | else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub; |
| 1729 | else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul; |
| 1730 | } |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1731 | #ifndef NDEBUG |
| 1732 | switch (Opcode) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1733 | case Instruction::Add: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1734 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1735 | case Instruction::Mul: |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1736 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1737 | assert(C1->getType()->isIntOrIntVector() && |
| 1738 | "Tried to create an integer operation on a non-integer type!"); |
| 1739 | break; |
| 1740 | case Instruction::FAdd: |
| 1741 | case Instruction::FSub: |
| 1742 | case Instruction::FMul: |
| 1743 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1744 | assert(C1->getType()->isFPOrFPVector() && |
| 1745 | "Tried to create a floating-point operation on a " |
| 1746 | "non-floating-point type!"); |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1747 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1748 | case Instruction::UDiv: |
| 1749 | case Instruction::SDiv: |
| 1750 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1751 | assert(C1->getType()->isIntOrIntVector() && |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1752 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1753 | break; |
| 1754 | case Instruction::FDiv: |
| 1755 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1756 | assert(C1->getType()->isFPOrFPVector() && |
| 1757 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1758 | break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1759 | case Instruction::URem: |
| 1760 | case Instruction::SRem: |
| 1761 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1762 | assert(C1->getType()->isIntOrIntVector() && |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1763 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1764 | break; |
| 1765 | case Instruction::FRem: |
| 1766 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1767 | assert(C1->getType()->isFPOrFPVector() && |
| 1768 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1769 | break; |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1770 | case Instruction::And: |
| 1771 | case Instruction::Or: |
| 1772 | case Instruction::Xor: |
| 1773 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1774 | assert(C1->getType()->isIntOrIntVector() && |
Misha Brukman | 1bae291 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1775 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1776 | break; |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1777 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1778 | case Instruction::LShr: |
| 1779 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1780 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Dan Gohman | c131793 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 1781 | assert(C1->getType()->isIntOrIntVector() && |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1782 | "Tried to create a shift operation on a non-integer type!"); |
| 1783 | break; |
| 1784 | default: |
| 1785 | break; |
| 1786 | } |
| 1787 | #endif |
| 1788 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1789 | return getTy(C1->getType(), Opcode, C1, C2); |
Reid Spencer | 67263fe | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1792 | Constant* ConstantExpr::getSizeOf(const Type* Ty) { |
| 1793 | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
| 1794 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1795 | Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1); |
| 1796 | Constant *GEP = getGetElementPtr( |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 1797 | Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1798 | return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty); |
| 1799 | } |
| 1800 | |
| 1801 | Constant* ConstantExpr::getAlignOf(const Type* Ty) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1802 | // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1 |
| 1803 | const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 1804 | Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo()); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1805 | Constant *Zero = ConstantInt::get(Type::Int32Ty, 0); |
| 1806 | Constant *One = ConstantInt::get(Type::Int32Ty, 1); |
| 1807 | Constant *Indices[2] = { Zero, One }; |
| 1808 | Constant *GEP = getGetElementPtr(NullPtr, Indices, 2); |
| 1809 | return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty); |
| 1810 | } |
| 1811 | |
| 1812 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1813 | Constant *ConstantExpr::getCompare(unsigned short pred, |
Reid Spencer | 67263fe | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1814 | Constant *C1, Constant *C2) { |
| 1815 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1816 | return getCompareTy(pred, C1, C2); |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1819 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1820 | Constant *V1, Constant *V2) { |
Chris Lattner | 9ace0cd | 2008-12-29 00:16:12 +0000 | [diff] [blame] | 1821 | assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (ReqTy == V1->getType()) |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1824 | if (Constant *SC = ConstantFoldSelectInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1825 | ReqTy->getContext(), C, V1, V2)) |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1826 | return SC; // Fold common cases |
| 1827 | |
| 1828 | std::vector<Constant*> argVec(3, C); |
| 1829 | argVec[1] = V1; |
| 1830 | argVec[2] = V2; |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1831 | ExprMapKeyType Key(Instruction::Select, argVec); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1832 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1833 | // Implicitly locked. |
| 1834 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1837 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1838 | Value* const *Idxs, |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1839 | unsigned NumIdx) { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1840 | assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, |
| 1841 | Idxs+NumIdx) == |
| 1842 | cast<PointerType>(ReqTy)->getElementType() && |
| 1843 | "GEP indices invalid!"); |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1844 | |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1845 | if (Constant *FC = ConstantFoldGetElementPtr( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1846 | ReqTy->getContext(), C, (Constant**)Idxs, NumIdx)) |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1847 | return FC; // Fold a few common cases... |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1849 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1850 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1851 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1852 | std::vector<Constant*> ArgVec; |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1853 | ArgVec.reserve(NumIdx+1); |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1854 | ArgVec.push_back(C); |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1855 | for (unsigned i = 0; i != NumIdx; ++i) |
| 1856 | ArgVec.push_back(cast<Constant>(Idxs[i])); |
| 1857 | const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1858 | |
| 1859 | // Implicitly locked. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1860 | return ExprConstants->getOrCreate(ReqTy, Key); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1863 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1864 | unsigned NumIdx) { |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1865 | // Get the result type of the getelementptr! |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1866 | const Type *Ty = |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1867 | GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1868 | assert(Ty && "GEP indices invalid!"); |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1869 | unsigned As = cast<PointerType>(C->getType())->getAddressSpace(); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1870 | return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx); |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1873 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs, |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1874 | unsigned NumIdx) { |
| 1875 | return getGetElementPtr(C, (Value* const *)Idxs, NumIdx); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1878 | |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1879 | Constant * |
| 1880 | ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) { |
| 1881 | assert(LHS->getType() == RHS->getType()); |
| 1882 | assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1883 | pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); |
| 1884 | |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1885 | if (Constant *FC = ConstantFoldCompareInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1886 | LHS->getContext(), pred, LHS, RHS)) |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1887 | return FC; // Fold a few common cases... |
| 1888 | |
| 1889 | // Look up the constant in the table first to ensure uniqueness |
| 1890 | std::vector<Constant*> ArgVec; |
| 1891 | ArgVec.push_back(LHS); |
| 1892 | ArgVec.push_back(RHS); |
Reid Spencer | 4fa021a | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1893 | // Get the key type with both the opcode and predicate |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1894 | const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1895 | |
| 1896 | // Implicitly locked. |
| 1897 | return ExprConstants->getOrCreate(Type::Int1Ty, Key); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | Constant * |
| 1901 | ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) { |
| 1902 | assert(LHS->getType() == RHS->getType()); |
| 1903 | assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); |
| 1904 | |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1905 | if (Constant *FC = ConstantFoldCompareInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1906 | LHS->getContext(), pred, LHS, RHS)) |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1907 | return FC; // Fold a few common cases... |
| 1908 | |
| 1909 | // Look up the constant in the table first to ensure uniqueness |
| 1910 | std::vector<Constant*> ArgVec; |
| 1911 | ArgVec.push_back(LHS); |
| 1912 | ArgVec.push_back(RHS); |
Reid Spencer | 4fa021a | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1913 | // Get the key type with both the opcode and predicate |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1914 | const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1915 | |
| 1916 | // Implicitly locked. |
| 1917 | return ExprConstants->getOrCreate(Type::Int1Ty, Key); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1920 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 1921 | Constant *Idx) { |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1922 | if (Constant *FC = ConstantFoldExtractElementInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1923 | ReqTy->getContext(), Val, Idx)) |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1924 | return FC; // Fold a few common cases... |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1925 | // Look up the constant in the table first to ensure uniqueness |
| 1926 | std::vector<Constant*> ArgVec(1, Val); |
| 1927 | ArgVec.push_back(Idx); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1928 | const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1929 | |
| 1930 | // Implicitly locked. |
| 1931 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1935 | assert(isa<VectorType>(Val->getType()) && |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1936 | "Tried to create extractelement operation on non-vector type!"); |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1937 | assert(Idx->getType() == Type::Int32Ty && |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1938 | "Extractelement index must be i32 type!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1939 | return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(), |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1940 | Val, Idx); |
| 1941 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1942 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1943 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 1944 | Constant *Elt, Constant *Idx) { |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1945 | if (Constant *FC = ConstantFoldInsertElementInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1946 | ReqTy->getContext(), Val, Elt, Idx)) |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1947 | return FC; // Fold a few common cases... |
| 1948 | // Look up the constant in the table first to ensure uniqueness |
| 1949 | std::vector<Constant*> ArgVec(1, Val); |
| 1950 | ArgVec.push_back(Elt); |
| 1951 | ArgVec.push_back(Idx); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1952 | const ExprMapKeyType Key(Instruction::InsertElement,ArgVec); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Implicitly locked. |
| 1955 | return ExprConstants->getOrCreate(ReqTy, Key); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1959 | Constant *Idx) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1960 | assert(isa<VectorType>(Val->getType()) && |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1961 | "Tried to create insertelement operation on non-vector type!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1962 | assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1963 | && "Insertelement types must match!"); |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1964 | assert(Idx->getType() == Type::Int32Ty && |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1965 | "Insertelement index must be i32 type!"); |
Gordon Henriksen | 699609c | 2008-08-30 15:41:51 +0000 | [diff] [blame] | 1966 | return getInsertElementTy(Val->getType(), Val, Elt, Idx); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1969 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 1970 | Constant *V2, Constant *Mask) { |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1971 | if (Constant *FC = ConstantFoldShuffleVectorInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1972 | ReqTy->getContext(), V1, V2, Mask)) |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1973 | return FC; // Fold a few common cases... |
| 1974 | // Look up the constant in the table first to ensure uniqueness |
| 1975 | std::vector<Constant*> ArgVec(1, V1); |
| 1976 | ArgVec.push_back(V2); |
| 1977 | ArgVec.push_back(Mask); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1978 | const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Implicitly locked. |
| 1981 | return ExprConstants->getOrCreate(ReqTy, Key); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1985 | Constant *Mask) { |
| 1986 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1987 | "Invalid shuffle vector constant expr operands!"); |
Nate Begeman | 0f123cf | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1988 | |
| 1989 | unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements(); |
| 1990 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
| 1991 | const Type *ShufTy = VectorType::get(EltTy, NElts); |
| 1992 | return getShuffleVectorTy(ShufTy, V1, V2, Mask); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1995 | Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, |
| 1996 | Constant *Val, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1997 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1998 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 1999 | Idxs+NumIdx) == Val->getType() && |
| 2000 | "insertvalue indices invalid!"); |
| 2001 | assert(Agg->getType() == ReqTy && |
| 2002 | "insertvalue type invalid!"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2003 | assert(Agg->getType()->isFirstClassType() && |
| 2004 | "Non-first-class type for constant InsertValue expression"); |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2005 | Constant *FC = ConstantFoldInsertValueInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 2006 | ReqTy->getContext(), Agg, Val, Idxs, NumIdx); |
Dan Gohman | e089160 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 2007 | assert(FC && "InsertValue constant expr couldn't be folded!"); |
| 2008 | return FC; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2012 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2013 | assert(Agg->getType()->isFirstClassType() && |
| 2014 | "Tried to create insertelement operation on non-first-class type!"); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2015 | |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2016 | const Type *ReqTy = Agg->getType(); |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2017 | #ifndef NDEBUG |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2018 | const Type *ValTy = |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2019 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 2020 | #endif |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2021 | assert(ValTy == Val->getType() && "insertvalue indices invalid!"); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2022 | return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx); |
| 2023 | } |
| 2024 | |
| 2025 | Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2026 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2027 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 2028 | Idxs+NumIdx) == ReqTy && |
| 2029 | "extractvalue indices invalid!"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2030 | assert(Agg->getType()->isFirstClassType() && |
| 2031 | "Non-first-class type for constant extractvalue expression"); |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2032 | Constant *FC = ConstantFoldExtractValueInstruction( |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 2033 | ReqTy->getContext(), Agg, Idxs, NumIdx); |
Dan Gohman | e089160 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 2034 | assert(FC && "ExtractValue constant expr couldn't be folded!"); |
| 2035 | return FC; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | Constant *ConstantExpr::getExtractValue(Constant *Agg, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2039 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2040 | assert(Agg->getType()->isFirstClassType() && |
| 2041 | "Tried to create extractelement operation on non-first-class type!"); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2042 | |
| 2043 | const Type *ReqTy = |
| 2044 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
| 2045 | assert(ReqTy && "extractvalue indices invalid!"); |
| 2046 | return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx); |
| 2047 | } |
| 2048 | |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2049 | Constant* ConstantExpr::getNeg(Constant* C) { |
| 2050 | // API compatibility: Adjust integer opcodes to floating-point opcodes. |
| 2051 | if (C->getType()->isFPOrFPVector()) |
| 2052 | return getFNeg(C); |
| 2053 | assert(C->getType()->isIntOrIntVector() && |
| 2054 | "Cannot NEG a nonintegral value!"); |
| 2055 | return get(Instruction::Sub, |
| 2056 | ConstantFP::getZeroValueForNegation(C->getType()), |
| 2057 | C); |
| 2058 | } |
| 2059 | |
| 2060 | Constant* ConstantExpr::getFNeg(Constant* C) { |
| 2061 | assert(C->getType()->isFPOrFPVector() && |
| 2062 | "Cannot FNEG a non-floating-point value!"); |
| 2063 | return get(Instruction::FSub, |
| 2064 | ConstantFP::getZeroValueForNegation(C->getType()), |
| 2065 | C); |
| 2066 | } |
| 2067 | |
| 2068 | Constant* ConstantExpr::getNot(Constant* C) { |
| 2069 | assert(C->getType()->isIntOrIntVector() && |
| 2070 | "Cannot NOT a nonintegral value!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame^] | 2071 | return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType())); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) { |
| 2075 | return get(Instruction::Add, C1, C2); |
| 2076 | } |
| 2077 | |
| 2078 | Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) { |
| 2079 | return get(Instruction::FAdd, C1, C2); |
| 2080 | } |
| 2081 | |
| 2082 | Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) { |
| 2083 | return get(Instruction::Sub, C1, C2); |
| 2084 | } |
| 2085 | |
| 2086 | Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) { |
| 2087 | return get(Instruction::FSub, C1, C2); |
| 2088 | } |
| 2089 | |
| 2090 | Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) { |
| 2091 | return get(Instruction::Mul, C1, C2); |
| 2092 | } |
| 2093 | |
| 2094 | Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) { |
| 2095 | return get(Instruction::FMul, C1, C2); |
| 2096 | } |
| 2097 | |
| 2098 | Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) { |
| 2099 | return get(Instruction::UDiv, C1, C2); |
| 2100 | } |
| 2101 | |
| 2102 | Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) { |
| 2103 | return get(Instruction::SDiv, C1, C2); |
| 2104 | } |
| 2105 | |
| 2106 | Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) { |
| 2107 | return get(Instruction::FDiv, C1, C2); |
| 2108 | } |
| 2109 | |
| 2110 | Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) { |
| 2111 | return get(Instruction::URem, C1, C2); |
| 2112 | } |
| 2113 | |
| 2114 | Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) { |
| 2115 | return get(Instruction::SRem, C1, C2); |
| 2116 | } |
| 2117 | |
| 2118 | Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) { |
| 2119 | return get(Instruction::FRem, C1, C2); |
| 2120 | } |
| 2121 | |
| 2122 | Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) { |
| 2123 | return get(Instruction::And, C1, C2); |
| 2124 | } |
| 2125 | |
| 2126 | Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) { |
| 2127 | return get(Instruction::Or, C1, C2); |
| 2128 | } |
| 2129 | |
| 2130 | Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) { |
| 2131 | return get(Instruction::Xor, C1, C2); |
| 2132 | } |
| 2133 | |
| 2134 | Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) { |
| 2135 | return get(Instruction::Shl, C1, C2); |
| 2136 | } |
| 2137 | |
| 2138 | Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) { |
| 2139 | return get(Instruction::LShr, C1, C2); |
| 2140 | } |
| 2141 | |
| 2142 | Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) { |
| 2143 | return get(Instruction::AShr, C1, C2); |
| 2144 | } |
| 2145 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2146 | // destroyConstant - Remove the constant from the constant table... |
| 2147 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2148 | void ConstantExpr::destroyConstant() { |
| 2149 | // Implicitly locked. |
| 2150 | ExprConstants->remove(this); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2151 | destroyConstantImpl(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 2154 | const char *ConstantExpr::getOpcodeName() const { |
| 2155 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2156 | } |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 2157 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2158 | //===----------------------------------------------------------------------===// |
| 2159 | // replaceUsesOfWithOnConstant implementations |
| 2160 | |
Chris Lattner | 5498405 | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2161 | /// replaceUsesOfWithOnConstant - Update this constant array to change uses of |
| 2162 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 2163 | /// etc. |
| 2164 | /// |
| 2165 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 2166 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
| 2167 | /// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that |
| 2168 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 2169 | /// work, but would be really slow because it would have to unique each updated |
| 2170 | /// array instance. |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2171 | |
| 2172 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 2173 | std::vector<Constant*> Elements; |
| 2174 | Elements.reserve(CA->getNumOperands()); |
| 2175 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 2176 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 2177 | return Elements; |
| 2178 | } |
| 2179 | |
| 2180 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2181 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2182 | Use *U) { |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2183 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2184 | Constant *ToC = cast<Constant>(To); |
| 2185 | |
| 2186 | LLVMContext &Context = getType()->getContext(); |
| 2187 | LLVMContextImpl *pImpl = Context.pImpl; |
| 2188 | |
| 2189 | std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup; |
| 2190 | Lookup.first.first = getType(); |
| 2191 | Lookup.second = this; |
| 2192 | |
| 2193 | std::vector<Constant*> &Values = Lookup.first.second; |
| 2194 | Values.reserve(getNumOperands()); // Build replacement array. |
| 2195 | |
| 2196 | // Fill values with the modified operands of the constant array. Also, |
| 2197 | // compute whether this turns into an all-zeros array. |
| 2198 | bool isAllZeros = false; |
| 2199 | unsigned NumUpdated = 0; |
| 2200 | if (!ToC->isNullValue()) { |
| 2201 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2202 | Constant *Val = cast<Constant>(O->get()); |
| 2203 | if (Val == From) { |
| 2204 | Val = ToC; |
| 2205 | ++NumUpdated; |
| 2206 | } |
| 2207 | Values.push_back(Val); |
| 2208 | } |
| 2209 | } else { |
| 2210 | isAllZeros = true; |
| 2211 | for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) { |
| 2212 | Constant *Val = cast<Constant>(O->get()); |
| 2213 | if (Val == From) { |
| 2214 | Val = ToC; |
| 2215 | ++NumUpdated; |
| 2216 | } |
| 2217 | Values.push_back(Val); |
| 2218 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | Constant *Replacement = 0; |
| 2223 | if (isAllZeros) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2224 | Replacement = ConstantAggregateZero::get(getType()); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2225 | } else { |
| 2226 | // Check to see if we have this array type already. |
| 2227 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 2228 | bool Exists; |
| 2229 | LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I = |
| 2230 | pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists); |
| 2231 | |
| 2232 | if (Exists) { |
| 2233 | Replacement = I->second; |
| 2234 | } else { |
| 2235 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2236 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 2237 | // old with the new, then deleting the old... just update the current one |
| 2238 | // in place! |
| 2239 | pImpl->ArrayConstants.MoveConstantToNewSlot(this, I); |
| 2240 | |
| 2241 | // Update to the new value. Optimize for the case when we have a single |
| 2242 | // operand that we're changing, but handle bulk updates efficiently. |
| 2243 | if (NumUpdated == 1) { |
| 2244 | unsigned OperandToUpdate = U - OperandList; |
| 2245 | assert(getOperand(OperandToUpdate) == From && |
| 2246 | "ReplaceAllUsesWith broken!"); |
| 2247 | setOperand(OperandToUpdate, ToC); |
| 2248 | } else { |
| 2249 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 2250 | if (getOperand(i) == From) |
| 2251 | setOperand(i, ToC); |
| 2252 | } |
| 2253 | return; |
| 2254 | } |
| 2255 | } |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2256 | |
| 2257 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2258 | assert(Replacement != this && "I didn't contain From!"); |
| 2259 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2260 | // Everyone using this now uses the replacement. |
| 2261 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Delete the old constant! |
| 2264 | destroyConstant(); |
| 2265 | } |
| 2266 | |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2267 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 2268 | std::vector<Constant*> Elements; |
| 2269 | Elements.reserve(CS->getNumOperands()); |
| 2270 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 2271 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 2272 | return Elements; |
| 2273 | } |
| 2274 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2275 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2276 | Use *U) { |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2277 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2278 | Constant *ToC = cast<Constant>(To); |
| 2279 | |
| 2280 | unsigned OperandToUpdate = U-OperandList; |
| 2281 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 2282 | |
| 2283 | std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup; |
| 2284 | Lookup.first.first = getType(); |
| 2285 | Lookup.second = this; |
| 2286 | std::vector<Constant*> &Values = Lookup.first.second; |
| 2287 | Values.reserve(getNumOperands()); // Build replacement struct. |
| 2288 | |
| 2289 | |
| 2290 | // Fill values with the modified operands of the constant struct. Also, |
| 2291 | // compute whether this turns into an all-zeros struct. |
| 2292 | bool isAllZeros = false; |
| 2293 | if (!ToC->isNullValue()) { |
| 2294 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) |
| 2295 | Values.push_back(cast<Constant>(O->get())); |
| 2296 | } else { |
| 2297 | isAllZeros = true; |
| 2298 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2299 | Constant *Val = cast<Constant>(O->get()); |
| 2300 | Values.push_back(Val); |
| 2301 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 2302 | } |
| 2303 | } |
| 2304 | Values[OperandToUpdate] = ToC; |
| 2305 | |
| 2306 | LLVMContext &Context = getType()->getContext(); |
| 2307 | LLVMContextImpl *pImpl = Context.pImpl; |
| 2308 | |
| 2309 | Constant *Replacement = 0; |
| 2310 | if (isAllZeros) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2311 | Replacement = ConstantAggregateZero::get(getType()); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2312 | } else { |
| 2313 | // Check to see if we have this array type already. |
| 2314 | sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); |
| 2315 | bool Exists; |
| 2316 | LLVMContextImpl::StructConstantsTy::MapTy::iterator I = |
| 2317 | pImpl->StructConstants.InsertOrGetItem(Lookup, Exists); |
| 2318 | |
| 2319 | if (Exists) { |
| 2320 | Replacement = I->second; |
| 2321 | } else { |
| 2322 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2323 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 2324 | // old with the new, then deleting the old... just update the current one |
| 2325 | // in place! |
| 2326 | pImpl->StructConstants.MoveConstantToNewSlot(this, I); |
| 2327 | |
| 2328 | // Update to the new value. |
| 2329 | setOperand(OperandToUpdate, ToC); |
| 2330 | return; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | assert(Replacement != this && "I didn't contain From!"); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2336 | // Everyone using this now uses the replacement. |
| 2337 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2338 | |
| 2339 | // Delete the old constant! |
| 2340 | destroyConstant(); |
| 2341 | } |
| 2342 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2343 | static std::vector<Constant*> getValType(ConstantVector *CP) { |
| 2344 | std::vector<Constant*> Elements; |
| 2345 | Elements.reserve(CP->getNumOperands()); |
| 2346 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 2347 | Elements.push_back(CP->getOperand(i)); |
| 2348 | return Elements; |
| 2349 | } |
| 2350 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2351 | void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2352 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2353 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2354 | |
| 2355 | std::vector<Constant*> Values; |
| 2356 | Values.reserve(getNumOperands()); // Build replacement array... |
| 2357 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2358 | Constant *Val = getOperand(i); |
| 2359 | if (Val == From) Val = cast<Constant>(To); |
| 2360 | Values.push_back(Val); |
| 2361 | } |
| 2362 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2363 | Constant *Replacement = get(getType(), Values); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2364 | assert(Replacement != this && "I didn't contain From!"); |
| 2365 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2366 | // Everyone using this now uses the replacement. |
| 2367 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2368 | |
| 2369 | // Delete the old constant! |
| 2370 | destroyConstant(); |
| 2371 | } |
| 2372 | |
| 2373 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2374 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2375 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 2376 | Constant *To = cast<Constant>(ToV); |
| 2377 | |
| 2378 | Constant *Replacement = 0; |
| 2379 | if (getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2380 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2381 | Constant *Pointer = getOperand(0); |
| 2382 | Indices.reserve(getNumOperands()-1); |
| 2383 | if (Pointer == From) Pointer = To; |
| 2384 | |
| 2385 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 2386 | Constant *Val = getOperand(i); |
| 2387 | if (Val == From) Val = To; |
| 2388 | Indices.push_back(Val); |
| 2389 | } |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2390 | Replacement = ConstantExpr::getGetElementPtr(Pointer, |
| 2391 | &Indices[0], Indices.size()); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2392 | } else if (getOpcode() == Instruction::ExtractValue) { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2393 | Constant *Agg = getOperand(0); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2394 | if (Agg == From) Agg = To; |
| 2395 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2396 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2397 | Replacement = ConstantExpr::getExtractValue(Agg, |
| 2398 | &Indices[0], Indices.size()); |
| 2399 | } else if (getOpcode() == Instruction::InsertValue) { |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2400 | Constant *Agg = getOperand(0); |
| 2401 | Constant *Val = getOperand(1); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2402 | if (Agg == From) Agg = To; |
| 2403 | if (Val == From) Val = To; |
| 2404 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2405 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2406 | Replacement = ConstantExpr::getInsertValue(Agg, Val, |
| 2407 | &Indices[0], Indices.size()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2408 | } else if (isCast()) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2409 | assert(getOperand(0) == From && "Cast only has one use!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2410 | Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2411 | } else if (getOpcode() == Instruction::Select) { |
| 2412 | Constant *C1 = getOperand(0); |
| 2413 | Constant *C2 = getOperand(1); |
| 2414 | Constant *C3 = getOperand(2); |
| 2415 | if (C1 == From) C1 = To; |
| 2416 | if (C2 == From) C2 = To; |
| 2417 | if (C3 == From) C3 = To; |
| 2418 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2419 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 2420 | Constant *C1 = getOperand(0); |
| 2421 | Constant *C2 = getOperand(1); |
| 2422 | if (C1 == From) C1 = To; |
| 2423 | if (C2 == From) C2 = To; |
| 2424 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | 42b5580 | 2006-04-08 05:09:48 +0000 | [diff] [blame] | 2425 | } else if (getOpcode() == Instruction::InsertElement) { |
| 2426 | Constant *C1 = getOperand(0); |
| 2427 | Constant *C2 = getOperand(1); |
| 2428 | Constant *C3 = getOperand(1); |
| 2429 | if (C1 == From) C1 = To; |
| 2430 | if (C2 == From) C2 = To; |
| 2431 | if (C3 == From) C3 = To; |
| 2432 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 2433 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 2434 | Constant *C1 = getOperand(0); |
| 2435 | Constant *C2 = getOperand(1); |
| 2436 | Constant *C3 = getOperand(2); |
| 2437 | if (C1 == From) C1 = To; |
| 2438 | if (C2 == From) C2 = To; |
| 2439 | if (C3 == From) C3 = To; |
| 2440 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2441 | } else if (isCompare()) { |
| 2442 | Constant *C1 = getOperand(0); |
| 2443 | Constant *C2 = getOperand(1); |
| 2444 | if (C1 == From) C1 = To; |
| 2445 | if (C2 == From) C2 = To; |
| 2446 | if (getOpcode() == Instruction::ICmp) |
| 2447 | Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2); |
Chris Lattner | bbedb0e | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2448 | else { |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2449 | assert(getOpcode() == Instruction::FCmp); |
| 2450 | Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2); |
Chris Lattner | bbedb0e | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2451 | } |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2452 | } else if (getNumOperands() == 2) { |
| 2453 | Constant *C1 = getOperand(0); |
| 2454 | Constant *C2 = getOperand(1); |
| 2455 | if (C1 == From) C1 = To; |
| 2456 | if (C2 == From) C2 = To; |
| 2457 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 2458 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2459 | llvm_unreachable("Unknown ConstantExpr type!"); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2460 | return; |
| 2461 | } |
| 2462 | |
| 2463 | assert(Replacement != this && "I didn't contain From!"); |
| 2464 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2465 | // Everyone using this now uses the replacement. |
| 2466 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2467 | |
| 2468 | // Delete the old constant! |
| 2469 | destroyConstant(); |
Matthijs Kooijman | 10b9de6 | 2008-07-03 07:46:41 +0000 | [diff] [blame] | 2470 | } |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 2471 | |