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