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