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