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