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