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