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