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