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