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