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