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