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