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