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