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 | eb59ca9 | 2011-02-07 20:03:14 +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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Constants.h" |
Chris Lattner | 92f6fea | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 15 | #include "ConstantFold.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/FoldingSet.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/GlobalValue.h" |
| 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/IR/Operator.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 8a94bf1 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 37f077a | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Talin | 41ee4e5 | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 36 | #include <cstdarg> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 40 | // Constant Class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 43 | void Constant::anchor() { } |
| 44 | |
Chris Lattner | b447387 | 2011-07-15 05:58:04 +0000 | [diff] [blame] | 45 | bool Constant::isNegativeZeroValue() const { |
| 46 | // Floating point values have an explicit -0.0 value. |
| 47 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 48 | return CFP->isZero() && CFP->isNegative(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 49 | |
David Tweed | ec7eb55 | 2013-03-18 11:54:44 +0000 | [diff] [blame^] | 50 | // Equivalent for a vector of -0.0's. |
| 51 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 52 | if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue())) |
| 53 | if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative()) |
| 54 | return true; |
| 55 | |
| 56 | // However, vectors of zeroes which are floating point represent +0.0's. |
| 57 | if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this)) |
| 58 | if (const VectorType *VT = dyn_cast<VectorType>(CAZ->getType())) |
| 59 | if (VT->getElementType()->isFloatingPointTy()) |
| 60 | // As it's a CAZ, we know it's the zero bit-pattern (ie, +0.0) in each element. |
| 61 | return false; |
| 62 | |
Chris Lattner | b447387 | 2011-07-15 05:58:04 +0000 | [diff] [blame] | 63 | // Otherwise, just use +0.0. |
| 64 | return isNullValue(); |
| 65 | } |
| 66 | |
Shuxin Yang | c3d6de2 | 2013-01-09 00:53:25 +0000 | [diff] [blame] | 67 | // Return true iff this constant is positive zero (floating point), negative |
| 68 | // zero (floating point), or a null value. |
Shuxin Yang | 935e35d | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 69 | bool Constant::isZeroValue() const { |
| 70 | // Floating point values have an explicit -0.0 value. |
| 71 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 72 | return CFP->isZero(); |
| 73 | |
| 74 | // Otherwise, just use +0.0. |
| 75 | return isNullValue(); |
| 76 | } |
| 77 | |
Chris Lattner | 032c6eb | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 78 | bool Constant::isNullValue() const { |
| 79 | // 0 is null. |
| 80 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 81 | return CI->isZero(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 032c6eb | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 83 | // +0.0 is null. |
| 84 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 85 | return CFP->isZero() && !CFP->isNegative(); |
| 86 | |
| 87 | // constant zero is zero for aggregates and cpnull is null for pointers. |
| 88 | return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this); |
| 89 | } |
| 90 | |
Nadav Rotem | 4c7c0f2 | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 91 | bool Constant::isAllOnesValue() const { |
| 92 | // Check for -1 integers |
| 93 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 94 | return CI->isMinusOne(); |
| 95 | |
| 96 | // Check for FP which are bitcasted from -1 integers |
| 97 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 98 | return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue(); |
| 99 | |
Benjamin Kramer | b518cae | 2011-11-14 19:12:20 +0000 | [diff] [blame] | 100 | // Check for constant vectors which are splats of -1 values. |
Nadav Rotem | 4c7c0f2 | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 101 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
Benjamin Kramer | b518cae | 2011-11-14 19:12:20 +0000 | [diff] [blame] | 102 | if (Constant *Splat = CV->getSplatValue()) |
| 103 | return Splat->isAllOnesValue(); |
Nadav Rotem | 4c7c0f2 | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 104 | |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 105 | // Check for constant vectors which are splats of -1 values. |
| 106 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 107 | if (Constant *Splat = CV->getSplatValue()) |
| 108 | return Splat->isAllOnesValue(); |
| 109 | |
Nadav Rotem | 4c7c0f2 | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 110 | return false; |
| 111 | } |
Benjamin Kramer | b518cae | 2011-11-14 19:12:20 +0000 | [diff] [blame] | 112 | |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 113 | // Constructor to create a '0' constant of arbitrary type... |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 114 | Constant *Constant::getNullValue(Type *Ty) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 115 | switch (Ty->getTypeID()) { |
| 116 | case Type::IntegerTyID: |
| 117 | return ConstantInt::get(Ty, 0); |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 118 | case Type::HalfTyID: |
| 119 | return ConstantFP::get(Ty->getContext(), |
| 120 | APFloat::getZero(APFloat::IEEEhalf)); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 121 | case Type::FloatTyID: |
Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 122 | return ConstantFP::get(Ty->getContext(), |
| 123 | APFloat::getZero(APFloat::IEEEsingle)); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 124 | case Type::DoubleTyID: |
Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 125 | return ConstantFP::get(Ty->getContext(), |
| 126 | APFloat::getZero(APFloat::IEEEdouble)); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 127 | case Type::X86_FP80TyID: |
Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 128 | return ConstantFP::get(Ty->getContext(), |
| 129 | APFloat::getZero(APFloat::x87DoubleExtended)); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 130 | case Type::FP128TyID: |
| 131 | return ConstantFP::get(Ty->getContext(), |
Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 132 | APFloat::getZero(APFloat::IEEEquad)); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 133 | case Type::PPC_FP128TyID: |
Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 134 | return ConstantFP::get(Ty->getContext(), |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 135 | APFloat(APFloat::PPCDoubleDouble, |
| 136 | APInt::getNullValue(128))); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 137 | case Type::PointerTyID: |
| 138 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 139 | case Type::StructTyID: |
| 140 | case Type::ArrayTyID: |
| 141 | case Type::VectorTyID: |
| 142 | return ConstantAggregateZero::get(Ty); |
| 143 | default: |
| 144 | // Function, Label, or Opaque type? |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 145 | llvm_unreachable("Cannot create a null constant of that type!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 149 | Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) { |
| 150 | Type *ScalarTy = Ty->getScalarType(); |
Dan Gohman | 43ee5f7 | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 151 | |
| 152 | // Create the base integer constant. |
| 153 | Constant *C = ConstantInt::get(Ty->getContext(), V); |
| 154 | |
| 155 | // Convert an integer to a pointer, if necessary. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 156 | if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy)) |
Dan Gohman | 43ee5f7 | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 157 | C = ConstantExpr::getIntToPtr(C, PTy); |
| 158 | |
| 159 | // Broadcast a scalar to a vector, if necessary. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 160 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 161 | C = ConstantVector::getSplat(VTy->getNumElements(), C); |
Dan Gohman | 43ee5f7 | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 162 | |
| 163 | return C; |
| 164 | } |
| 165 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 166 | Constant *Constant::getAllOnesValue(Type *Ty) { |
| 167 | if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 168 | return ConstantInt::get(Ty->getContext(), |
| 169 | APInt::getAllOnesValue(ITy->getBitWidth())); |
Nadav Rotem | 093399c | 2011-02-17 21:22:27 +0000 | [diff] [blame] | 170 | |
| 171 | if (Ty->isFloatingPointTy()) { |
| 172 | APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(), |
| 173 | !Ty->isPPC_FP128Ty()); |
| 174 | return ConstantFP::get(Ty->getContext(), FL); |
| 175 | } |
| 176 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 177 | VectorType *VTy = cast<VectorType>(Ty); |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 178 | return ConstantVector::getSplat(VTy->getNumElements(), |
| 179 | getAllOnesValue(VTy->getElementType())); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 182 | /// getAggregateElement - For aggregates (struct/array/vector) return the |
| 183 | /// constant that corresponds to the specified element if possible, or null if |
| 184 | /// not. This can return null if the element index is a ConstantExpr, or if |
| 185 | /// 'this' is a constant expr. |
| 186 | Constant *Constant::getAggregateElement(unsigned Elt) const { |
| 187 | if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(this)) |
| 188 | return Elt < CS->getNumOperands() ? CS->getOperand(Elt) : 0; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 190 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(this)) |
| 191 | return Elt < CA->getNumOperands() ? CA->getOperand(Elt) : 0; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 193 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
| 194 | return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : 0; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 196 | if (const ConstantAggregateZero *CAZ =dyn_cast<ConstantAggregateZero>(this)) |
| 197 | return CAZ->getElementValue(Elt); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 199 | if (const UndefValue *UV = dyn_cast<UndefValue>(this)) |
| 200 | return UV->getElementValue(Elt); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 202 | if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this)) |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 203 | return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) : 0; |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | Constant *Constant::getAggregateElement(Constant *Elt) const { |
| 208 | assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer"); |
| 209 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) |
| 210 | return getAggregateElement(CI->getZExtValue()); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 215 | void Constant::destroyConstantImpl() { |
| 216 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 217 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | ef6a6a6 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 218 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 219 | // but they don't know that. Because we only find out when the CPV is |
| 220 | // 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] | 221 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 222 | // |
| 223 | while (!use_empty()) { |
| 224 | Value *V = use_back(); |
| 225 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 37f077a | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 226 | if (!isa<Constant>(V)) { |
David Greene | d2e63b7 | 2010-01-05 01:29:19 +0000 | [diff] [blame] | 227 | dbgs() << "While deleting: " << *this |
Chris Lattner | 37f077a | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 228 | << "\n\nUse still stuck around after Def is destroyed: " |
| 229 | << *V << "\n\n"; |
| 230 | } |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 231 | #endif |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 232 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 233 | cast<Constant>(V)->destroyConstant(); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 234 | |
| 235 | // The constant should remove itself from our use list... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 236 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Value has no outstanding references it is safe to delete it now... |
| 240 | delete this; |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 241 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 243 | /// canTrap - Return true if evaluation of this constant could trap. This is |
| 244 | /// true for things like constant expressions that could divide by zero. |
| 245 | bool Constant::canTrap() const { |
| 246 | assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); |
| 247 | // The only thing that could possibly trap are constant exprs. |
| 248 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(this); |
| 249 | if (!CE) return false; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 250 | |
| 251 | // ConstantExpr traps if any operands can trap. |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 252 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 253 | if (CE->getOperand(i)->canTrap()) |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 254 | return true; |
| 255 | |
| 256 | // Otherwise, only specific operations can trap. |
| 257 | switch (CE->getOpcode()) { |
| 258 | default: |
| 259 | return false; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 260 | case Instruction::UDiv: |
| 261 | case Instruction::SDiv: |
| 262 | case Instruction::FDiv: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 263 | case Instruction::URem: |
| 264 | case Instruction::SRem: |
| 265 | case Instruction::FRem: |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 266 | // Div and rem can trap if the RHS is not known to be non-zero. |
Chris Lattner | 0eeb913 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 267 | if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue()) |
Chris Lattner | 35b89fa | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 268 | return true; |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | |
Hans Wennborg | 1839858 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 273 | /// isThreadDependent - Return true if the value can vary between threads. |
| 274 | bool Constant::isThreadDependent() const { |
| 275 | SmallPtrSet<const Constant*, 64> Visited; |
| 276 | SmallVector<const Constant*, 64> WorkList; |
| 277 | WorkList.push_back(this); |
| 278 | Visited.insert(this); |
| 279 | |
| 280 | while (!WorkList.empty()) { |
| 281 | const Constant *C = WorkList.pop_back_val(); |
| 282 | |
| 283 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| 284 | if (GV->isThreadLocal()) |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) { |
Hans Wennborg | fbeb956 | 2012-11-16 10:33:25 +0000 | [diff] [blame] | 289 | const Constant *D = dyn_cast<Constant>(C->getOperand(I)); |
| 290 | if (!D) |
| 291 | continue; |
Hans Wennborg | 1839858 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 292 | if (Visited.insert(D)) |
| 293 | WorkList.push_back(D); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return false; |
| 298 | } |
| 299 | |
Chris Lattner | 4a7642e | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 300 | /// isConstantUsed - Return true if the constant has users other than constant |
| 301 | /// exprs and other dangling things. |
| 302 | bool Constant::isConstantUsed() const { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 303 | for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) { |
Chris Lattner | 4a7642e | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 304 | const Constant *UC = dyn_cast<Constant>(*UI); |
| 305 | if (UC == 0 || isa<GlobalValue>(UC)) |
| 306 | return true; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 4a7642e | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 308 | if (UC->isConstantUsed()) |
| 309 | return true; |
| 310 | } |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 315 | |
| 316 | /// getRelocationInfo - This method classifies the entry according to |
| 317 | /// whether or not it may generate a relocation entry. This must be |
| 318 | /// conservative, so if it might codegen to a relocatable entry, it should say |
| 319 | /// so. The return values are: |
| 320 | /// |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 321 | /// NoRelocation: This constant pool entry is guaranteed to never have a |
| 322 | /// relocation applied to it (because it holds a simple constant like |
| 323 | /// '4'). |
| 324 | /// LocalRelocation: This entry has relocations, but the entries are |
| 325 | /// guaranteed to be resolvable by the static linker, so the dynamic |
| 326 | /// linker will never see them. |
| 327 | /// GlobalRelocations: This entry may have arbitrary relocations. |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 328 | /// |
Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 329 | /// FIXME: This really should not be in IR. |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 330 | Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { |
| 331 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 332 | if (GV->hasLocalLinkage() || GV->hasHiddenVisibility()) |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 333 | return LocalRelocation; // Local to this file/library. |
| 334 | return GlobalRelocations; // Global reference. |
Anton Korobeynikov | ab267a2 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 335 | } |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 5d81bef | 2009-10-28 04:12:16 +0000 | [diff] [blame] | 337 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(this)) |
| 338 | return BA->getFunction()->getRelocationInfo(); |
| 339 | |
Chris Lattner | 5099b31 | 2010-01-03 18:09:40 +0000 | [diff] [blame] | 340 | // While raw uses of blockaddress need to be relocated, differences between |
| 341 | // two of them don't when they are for labels in the same function. This is a |
| 342 | // common idiom when creating a table for the indirect goto extension, so we |
| 343 | // handle it efficiently here. |
| 344 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) |
| 345 | if (CE->getOpcode() == Instruction::Sub) { |
| 346 | ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0)); |
| 347 | ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1)); |
| 348 | if (LHS && RHS && |
| 349 | LHS->getOpcode() == Instruction::PtrToInt && |
| 350 | RHS->getOpcode() == Instruction::PtrToInt && |
| 351 | isa<BlockAddress>(LHS->getOperand(0)) && |
| 352 | isa<BlockAddress>(RHS->getOperand(0)) && |
| 353 | cast<BlockAddress>(LHS->getOperand(0))->getFunction() == |
| 354 | cast<BlockAddress>(RHS->getOperand(0))->getFunction()) |
| 355 | return NoRelocation; |
| 356 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 083a1e0 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 358 | PossibleRelocationsTy Result = NoRelocation; |
Evan Cheng | afe1581 | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 359 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 0eeb913 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 360 | Result = std::max(Result, |
| 361 | cast<Constant>(getOperand(i))->getRelocationInfo()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 363 | return Result; |
Evan Cheng | afe1581 | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Chris Lattner | 13fb0db | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 366 | /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove |
| 367 | /// it. This involves recursively eliminating any dead users of the |
| 368 | /// constantexpr. |
| 369 | static bool removeDeadUsersOfConstant(const Constant *C) { |
| 370 | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 13fb0db | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 372 | while (!C->use_empty()) { |
| 373 | const Constant *User = dyn_cast<Constant>(C->use_back()); |
| 374 | if (!User) return false; // Non-constant usage; |
| 375 | if (!removeDeadUsersOfConstant(User)) |
| 376 | return false; // Constant wasn't dead |
| 377 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 13fb0db | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 379 | const_cast<Constant*>(C)->destroyConstant(); |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /// removeDeadConstantUsers - If there are any dead constant users dangling |
| 385 | /// off of this constant, remove them. This method is useful for clients |
| 386 | /// that want to check to see if a global is unused, but don't want to deal |
| 387 | /// with potentially dead constants hanging off of the globals. |
| 388 | void Constant::removeDeadConstantUsers() const { |
| 389 | Value::const_use_iterator I = use_begin(), E = use_end(); |
| 390 | Value::const_use_iterator LastNonDeadUser = E; |
| 391 | while (I != E) { |
| 392 | const Constant *User = dyn_cast<Constant>(*I); |
| 393 | if (User == 0) { |
| 394 | LastNonDeadUser = I; |
| 395 | ++I; |
| 396 | continue; |
| 397 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 13fb0db | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 399 | if (!removeDeadUsersOfConstant(User)) { |
| 400 | // If the constant wasn't dead, remember that this was the last live use |
| 401 | // and move on to the next constant. |
| 402 | LastNonDeadUser = I; |
| 403 | ++I; |
| 404 | continue; |
| 405 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 13fb0db | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 407 | // If the constant was dead, then the iterator is invalidated. |
| 408 | if (LastNonDeadUser == E) { |
| 409 | I = use_begin(); |
| 410 | if (I == E) break; |
| 411 | } else { |
| 412 | I = LastNonDeadUser; |
| 413 | ++I; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
Chris Lattner | 8638144 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 419 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 420 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 421 | // ConstantInt |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 422 | //===----------------------------------------------------------------------===// |
| 423 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 424 | void ConstantInt::anchor() { } |
| 425 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 426 | ConstantInt::ConstantInt(IntegerType *Ty, const APInt& V) |
Chris Lattner | eb41bdd | 2007-02-20 05:55:46 +0000 | [diff] [blame] | 427 | : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { |
Reid Spencer | 532d0ce | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 428 | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 431 | ConstantInt *ConstantInt::getTrue(LLVMContext &Context) { |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 432 | LLVMContextImpl *pImpl = Context.pImpl; |
Benjamin Kramer | f601d6d | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 433 | if (!pImpl->TheTrueVal) |
| 434 | pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1); |
| 435 | return pImpl->TheTrueVal; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 438 | ConstantInt *ConstantInt::getFalse(LLVMContext &Context) { |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 439 | LLVMContextImpl *pImpl = Context.pImpl; |
Benjamin Kramer | f601d6d | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 440 | if (!pImpl->TheFalseVal) |
| 441 | pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0); |
| 442 | return pImpl->TheFalseVal; |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 445 | Constant *ConstantInt::getTrue(Type *Ty) { |
| 446 | VectorType *VTy = dyn_cast<VectorType>(Ty); |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 447 | if (!VTy) { |
| 448 | assert(Ty->isIntegerTy(1) && "True must be i1 or vector of i1."); |
| 449 | return ConstantInt::getTrue(Ty->getContext()); |
| 450 | } |
| 451 | assert(VTy->getElementType()->isIntegerTy(1) && |
| 452 | "True must be vector of i1 or i1."); |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 453 | return ConstantVector::getSplat(VTy->getNumElements(), |
| 454 | ConstantInt::getTrue(Ty->getContext())); |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 457 | Constant *ConstantInt::getFalse(Type *Ty) { |
| 458 | VectorType *VTy = dyn_cast<VectorType>(Ty); |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 459 | if (!VTy) { |
| 460 | assert(Ty->isIntegerTy(1) && "False must be i1 or vector of i1."); |
| 461 | return ConstantInt::getFalse(Ty->getContext()); |
| 462 | } |
| 463 | assert(VTy->getElementType()->isIntegerTy(1) && |
| 464 | "False must be vector of i1 or i1."); |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 465 | return ConstantVector::getSplat(VTy->getNumElements(), |
| 466 | ConstantInt::getFalse(Ty->getContext())); |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 469 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 470 | // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap |
| 471 | // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the |
| 472 | // operator== and operator!= to ensure that the DenseMap doesn't attempt to |
| 473 | // compare APInt's of different widths, which would violate an APInt class |
| 474 | // invariant which generates an assertion. |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 475 | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 476 | // Get the corresponding integer type for the bit width of the value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 477 | IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 478 | // get an existing value or the insertion position |
| 479 | DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 480 | ConstantInt *&Slot = Context.pImpl->IntConstants[Key]; |
Owen Anderson | 59d5aac | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 481 | if (!Slot) Slot = new ConstantInt(ITy, V); |
| 482 | return Slot; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 485 | Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 486 | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 487 | |
| 488 | // For vectors, broadcast the value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 489 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 490 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 491 | |
| 492 | return C; |
| 493 | } |
| 494 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 495 | ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 496 | bool isSigned) { |
| 497 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); |
| 498 | } |
| 499 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 500 | ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 501 | return get(Ty, V, true); |
| 502 | } |
| 503 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 504 | Constant *ConstantInt::getSigned(Type *Ty, int64_t V) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 505 | return get(Ty, V, true); |
| 506 | } |
| 507 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 508 | Constant *ConstantInt::get(Type *Ty, const APInt& V) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 509 | ConstantInt *C = get(Ty->getContext(), V); |
| 510 | assert(C->getType() == Ty->getScalarType() && |
| 511 | "ConstantInt type doesn't match the type implied by its value!"); |
| 512 | |
| 513 | // For vectors, broadcast the value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 514 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 515 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 516 | |
| 517 | return C; |
| 518 | } |
| 519 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 520 | ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, |
Erick Tryzelaar | 0e81f66 | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 521 | uint8_t radix) { |
| 522 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix)); |
| 523 | } |
| 524 | |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 525 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 526 | // ConstantFP |
Chris Lattner | 6b6f6ba | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 527 | //===----------------------------------------------------------------------===// |
| 528 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 529 | static const fltSemantics *TypeToFloatSemantics(Type *Ty) { |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 530 | if (Ty->isHalfTy()) |
| 531 | return &APFloat::IEEEhalf; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 532 | if (Ty->isFloatTy()) |
Rafael Espindola | 87d1f47 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 533 | return &APFloat::IEEEsingle; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 534 | if (Ty->isDoubleTy()) |
Rafael Espindola | 87d1f47 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 535 | return &APFloat::IEEEdouble; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 536 | if (Ty->isX86_FP80Ty()) |
Rafael Espindola | 87d1f47 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 537 | return &APFloat::x87DoubleExtended; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 538 | else if (Ty->isFP128Ty()) |
Rafael Espindola | 87d1f47 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 539 | return &APFloat::IEEEquad; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 540 | |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 541 | assert(Ty->isPPC_FP128Ty() && "Unknown FP format"); |
Rafael Espindola | 87d1f47 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 542 | return &APFloat::PPCDoubleDouble; |
| 543 | } |
| 544 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 545 | void ConstantFP::anchor() { } |
| 546 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 547 | /// get() - This returns a constant fp for the specified value in the |
| 548 | /// specified type. This should only be used for simple constant values like |
| 549 | /// 2.0/1.0 etc, that are known-valid both as double and as the target format. |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 550 | Constant *ConstantFP::get(Type *Ty, double V) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 551 | LLVMContext &Context = Ty->getContext(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 552 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 553 | APFloat FV(V); |
| 554 | bool ignored; |
| 555 | FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), |
| 556 | APFloat::rmNearestTiesToEven, &ignored); |
| 557 | Constant *C = get(Context, FV); |
| 558 | |
| 559 | // For vectors, broadcast the value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 560 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 561 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 562 | |
| 563 | return C; |
| 564 | } |
| 565 | |
Erick Tryzelaar | 0e81f66 | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 566 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 567 | Constant *ConstantFP::get(Type *Ty, StringRef Str) { |
Erick Tryzelaar | 0e81f66 | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 568 | LLVMContext &Context = Ty->getContext(); |
| 569 | |
| 570 | APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str); |
| 571 | Constant *C = get(Context, FV); |
| 572 | |
| 573 | // For vectors, broadcast the value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 574 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 575 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Erick Tryzelaar | 0e81f66 | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 576 | |
| 577 | return C; |
| 578 | } |
| 579 | |
| 580 | |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 581 | ConstantFP *ConstantFP::getNegativeZero(Type *Ty) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 582 | LLVMContext &Context = Ty->getContext(); |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 583 | APFloat apf = cast<ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 584 | apf.changeSign(); |
| 585 | return get(Context, apf); |
| 586 | } |
| 587 | |
| 588 | |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 589 | Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { |
| 590 | Type *ScalarTy = Ty->getScalarType(); |
| 591 | if (ScalarTy->isFloatingPointTy()) { |
| 592 | Constant *C = getNegativeZero(ScalarTy); |
| 593 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 594 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
| 595 | return C; |
| 596 | } |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 597 | |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 598 | return Constant::getNullValue(Ty); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
| 602 | // ConstantFP accessors. |
| 603 | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
| 604 | DenseMapAPFloatKeyInfo::KeyTy Key(V); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 605 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 606 | LLVMContextImpl* pImpl = Context.pImpl; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 607 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 608 | ConstantFP *&Slot = pImpl->FPConstants[Key]; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 609 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 610 | if (!Slot) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 611 | Type *Ty; |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 612 | if (&V.getSemantics() == &APFloat::IEEEhalf) |
| 613 | Ty = Type::getHalfTy(Context); |
| 614 | else if (&V.getSemantics() == &APFloat::IEEEsingle) |
Owen Anderson | 59d5aac | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 615 | Ty = Type::getFloatTy(Context); |
| 616 | else if (&V.getSemantics() == &APFloat::IEEEdouble) |
| 617 | Ty = Type::getDoubleTy(Context); |
| 618 | else if (&V.getSemantics() == &APFloat::x87DoubleExtended) |
| 619 | Ty = Type::getX86_FP80Ty(Context); |
| 620 | else if (&V.getSemantics() == &APFloat::IEEEquad) |
| 621 | Ty = Type::getFP128Ty(Context); |
| 622 | else { |
| 623 | assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && |
| 624 | "Unknown FP format"); |
| 625 | Ty = Type::getPPC_FP128Ty(Context); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 626 | } |
Owen Anderson | 59d5aac | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 627 | Slot = new ConstantFP(Ty, V); |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 628 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 629 | |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 630 | return Slot; |
| 631 | } |
| 632 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 633 | ConstantFP *ConstantFP::getInfinity(Type *Ty, bool Negative) { |
Dan Gohman | f344f7f | 2009-09-25 23:00:48 +0000 | [diff] [blame] | 634 | const fltSemantics &Semantics = *TypeToFloatSemantics(Ty); |
| 635 | return ConstantFP::get(Ty->getContext(), |
| 636 | APFloat::getInf(Semantics, Negative)); |
| 637 | } |
| 638 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 639 | ConstantFP::ConstantFP(Type *Ty, const APFloat& V) |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 640 | : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { |
Chris Lattner | 288e78f | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 641 | assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && |
| 642 | "FP type Mismatch"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Chris Lattner | 032c6eb | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 645 | bool ConstantFP::isExactlyValue(const APFloat &V) const { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 646 | return Val.bitwiseIsEqual(V); |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 649 | //===----------------------------------------------------------------------===// |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 650 | // ConstantAggregateZero Implementation |
| 651 | //===----------------------------------------------------------------------===// |
| 652 | |
| 653 | /// getSequentialElement - If this CAZ has array or vector type, return a zero |
| 654 | /// with the right element type. |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 655 | Constant *ConstantAggregateZero::getSequentialElement() const { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 656 | return Constant::getNullValue(getType()->getSequentialElementType()); |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /// getStructElement - If this CAZ has struct type, return a zero with the |
| 660 | /// right element type for the specified element. |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 661 | Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 662 | return Constant::getNullValue(getType()->getStructElementType(Elt)); |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /// getElementValue - Return a zero of the right value for the specified GEP |
| 666 | /// index if we can, otherwise return null (e.g. if C is a ConstantExpr). |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 667 | Constant *ConstantAggregateZero::getElementValue(Constant *C) const { |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 668 | if (isa<SequentialType>(getType())) |
| 669 | return getSequentialElement(); |
| 670 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
| 671 | } |
| 672 | |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 673 | /// getElementValue - Return a zero of the right value for the specified GEP |
| 674 | /// index. |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 675 | Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 676 | if (isa<SequentialType>(getType())) |
| 677 | return getSequentialElement(); |
| 678 | return getStructElement(Idx); |
| 679 | } |
| 680 | |
| 681 | |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 682 | //===----------------------------------------------------------------------===// |
| 683 | // UndefValue Implementation |
| 684 | //===----------------------------------------------------------------------===// |
| 685 | |
| 686 | /// getSequentialElement - If this undef has array or vector type, return an |
| 687 | /// undef with the right element type. |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 688 | UndefValue *UndefValue::getSequentialElement() const { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 689 | return UndefValue::get(getType()->getSequentialElementType()); |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /// getStructElement - If this undef has struct type, return a zero with the |
| 693 | /// right element type for the specified element. |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 694 | UndefValue *UndefValue::getStructElement(unsigned Elt) const { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 695 | return UndefValue::get(getType()->getStructElementType(Elt)); |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /// getElementValue - Return an undef of the right value for the specified GEP |
| 699 | /// index if we can, otherwise return null (e.g. if C is a ConstantExpr). |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 700 | UndefValue *UndefValue::getElementValue(Constant *C) const { |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 701 | if (isa<SequentialType>(getType())) |
| 702 | return getSequentialElement(); |
| 703 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
| 704 | } |
| 705 | |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 706 | /// getElementValue - Return an undef of the right value for the specified GEP |
| 707 | /// index. |
Chris Lattner | 3d5ed22 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 708 | UndefValue *UndefValue::getElementValue(unsigned Idx) const { |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 709 | if (isa<SequentialType>(getType())) |
| 710 | return getSequentialElement(); |
| 711 | return getStructElement(Idx); |
| 712 | } |
| 713 | |
| 714 | |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 715 | |
| 716 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 717 | // ConstantXXX Classes |
| 718 | //===----------------------------------------------------------------------===// |
| 719 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 720 | template <typename ItTy, typename EltTy> |
| 721 | static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) { |
| 722 | for (; Start != End; ++Start) |
| 723 | if (*Start != Elt) |
| 724 | return false; |
| 725 | return true; |
| 726 | } |
Chris Lattner | 9b4ee0c | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 727 | |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 728 | ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 729 | : Constant(T, ConstantArrayVal, |
| 730 | OperandTraits<ConstantArray>::op_end(this) - V.size(), |
| 731 | V.size()) { |
Alkis Evlogimenos | e0de1d6 | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 732 | assert(V.size() == T->getNumElements() && |
| 733 | "Invalid initializer vector for constant array"); |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 734 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 735 | assert(V[i]->getType() == T->getElementType() && |
Alkis Evlogimenos | cad90ad | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 736 | "Initializer for array element doesn't match array element type!"); |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 737 | std::copy(V.begin(), V.end(), op_begin()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 740 | Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 741 | // Empty arrays are canonicalized to ConstantAggregateZero. |
| 742 | if (V.empty()) |
| 743 | return ConstantAggregateZero::get(Ty); |
| 744 | |
Jeffrey Yasskin | 1fb613c | 2009-09-30 21:08:08 +0000 | [diff] [blame] | 745 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 746 | assert(V[i]->getType() == Ty->getElementType() && |
| 747 | "Wrong type in array element initializer"); |
| 748 | } |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 749 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 751 | // If this is an all-zero array, return a ConstantAggregateZero object. If |
| 752 | // all undef, return an UndefValue, if "all simple", then return a |
| 753 | // ConstantDataArray. |
| 754 | Constant *C = V[0]; |
| 755 | if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C)) |
| 756 | return UndefValue::get(Ty); |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 757 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 758 | if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C)) |
| 759 | return ConstantAggregateZero::get(Ty); |
| 760 | |
| 761 | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
| 762 | // the element type is compatible with ConstantDataVector. If so, use it. |
| 763 | if (ConstantDataSequential::isElementTypeCompatible(C->getType())) { |
| 764 | // We speculatively build the elements here even if it turns out that there |
| 765 | // is a constantexpr or something else weird in the array, since it is so |
| 766 | // uncommon for that to happen. |
| 767 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
| 768 | if (CI->getType()->isIntegerTy(8)) { |
| 769 | SmallVector<uint8_t, 16> Elts; |
| 770 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 771 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 772 | Elts.push_back(CI->getZExtValue()); |
| 773 | else |
| 774 | break; |
| 775 | if (Elts.size() == V.size()) |
| 776 | return ConstantDataArray::get(C->getContext(), Elts); |
| 777 | } else if (CI->getType()->isIntegerTy(16)) { |
| 778 | SmallVector<uint16_t, 16> Elts; |
| 779 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 780 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 781 | Elts.push_back(CI->getZExtValue()); |
| 782 | else |
| 783 | break; |
| 784 | if (Elts.size() == V.size()) |
| 785 | return ConstantDataArray::get(C->getContext(), Elts); |
| 786 | } else if (CI->getType()->isIntegerTy(32)) { |
| 787 | SmallVector<uint32_t, 16> Elts; |
| 788 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 789 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 790 | Elts.push_back(CI->getZExtValue()); |
| 791 | else |
| 792 | break; |
| 793 | if (Elts.size() == V.size()) |
| 794 | return ConstantDataArray::get(C->getContext(), Elts); |
| 795 | } else if (CI->getType()->isIntegerTy(64)) { |
| 796 | SmallVector<uint64_t, 16> Elts; |
| 797 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 798 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 799 | Elts.push_back(CI->getZExtValue()); |
| 800 | else |
| 801 | break; |
| 802 | if (Elts.size() == V.size()) |
| 803 | return ConstantDataArray::get(C->getContext(), Elts); |
| 804 | } |
| 805 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 806 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 807 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 808 | if (CFP->getType()->isFloatTy()) { |
| 809 | SmallVector<float, 16> Elts; |
| 810 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 811 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) |
| 812 | Elts.push_back(CFP->getValueAPF().convertToFloat()); |
| 813 | else |
| 814 | break; |
| 815 | if (Elts.size() == V.size()) |
| 816 | return ConstantDataArray::get(C->getContext(), Elts); |
| 817 | } else if (CFP->getType()->isDoubleTy()) { |
| 818 | SmallVector<double, 16> Elts; |
| 819 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 820 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) |
| 821 | Elts.push_back(CFP->getValueAPF().convertToDouble()); |
| 822 | else |
| 823 | break; |
| 824 | if (Elts.size() == V.size()) |
| 825 | return ConstantDataArray::get(C->getContext(), Elts); |
| 826 | } |
| 827 | } |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 828 | } |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 830 | // Otherwise, we really do want to create a ConstantArray. |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 831 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 834 | /// getTypeForElements - Return an anonymous struct type to use for a constant |
| 835 | /// with the specified set of elements. The list must not be empty. |
| 836 | StructType *ConstantStruct::getTypeForElements(LLVMContext &Context, |
| 837 | ArrayRef<Constant*> V, |
| 838 | bool Packed) { |
Bill Wendling | a7a3f04 | 2012-02-07 01:27:51 +0000 | [diff] [blame] | 839 | unsigned VecSize = V.size(); |
| 840 | SmallVector<Type*, 16> EltTypes(VecSize); |
| 841 | for (unsigned i = 0; i != VecSize; ++i) |
| 842 | EltTypes[i] = V[i]->getType(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 843 | |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 844 | return StructType::get(Context, EltTypes, Packed); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V, |
| 849 | bool Packed) { |
| 850 | assert(!V.empty() && |
| 851 | "ConstantStruct::getTypeForElements cannot be called on empty list"); |
| 852 | return getTypeForElements(V[0]->getContext(), V, Packed); |
| 853 | } |
| 854 | |
| 855 | |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 856 | ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 857 | : Constant(T, ConstantStructVal, |
| 858 | OperandTraits<ConstantStruct>::op_end(this) - V.size(), |
| 859 | V.size()) { |
Chris Lattner | f4ef8db | 2011-08-07 04:18:48 +0000 | [diff] [blame] | 860 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 861 | "Invalid initializer vector for constant structure"); |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 862 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 863 | assert((T->isOpaque() || V[i]->getType() == T->getElementType(i)) && |
Chris Lattner | b843889 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 864 | "Initializer for struct element doesn't match struct element type!"); |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 865 | std::copy(V.begin(), V.end(), op_begin()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 868 | // ConstantStruct accessors. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 869 | Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 870 | assert((ST->isOpaque() || ST->getNumElements() == V.size()) && |
| 871 | "Incorrect # elements specified to ConstantStruct::get"); |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 872 | |
| 873 | // Create a ConstantAggregateZero value if all elements are zeros. |
| 874 | bool isZero = true; |
| 875 | bool isUndef = false; |
| 876 | |
| 877 | if (!V.empty()) { |
| 878 | isUndef = isa<UndefValue>(V[0]); |
| 879 | isZero = V[0]->isNullValue(); |
| 880 | if (isUndef || isZero) { |
| 881 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 882 | if (!V[i]->isNullValue()) |
| 883 | isZero = false; |
| 884 | if (!isa<UndefValue>(V[i])) |
| 885 | isUndef = false; |
| 886 | } |
| 887 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 888 | } |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 889 | if (isZero) |
| 890 | return ConstantAggregateZero::get(ST); |
| 891 | if (isUndef) |
| 892 | return UndefValue::get(ST); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 893 | |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 894 | return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Chris Lattner | f4ef8db | 2011-08-07 04:18:48 +0000 | [diff] [blame] | 897 | Constant *ConstantStruct::get(StructType *T, ...) { |
Talin | 41ee4e5 | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 898 | va_list ap; |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 899 | SmallVector<Constant*, 8> Values; |
| 900 | va_start(ap, T); |
| 901 | while (Constant *Val = va_arg(ap, llvm::Constant*)) |
Talin | 41ee4e5 | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 902 | Values.push_back(Val); |
Talin | bdcd766 | 2011-03-01 18:00:49 +0000 | [diff] [blame] | 903 | va_end(ap); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 904 | return get(T, Values); |
Talin | 41ee4e5 | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 907 | ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 908 | : Constant(T, ConstantVectorVal, |
| 909 | OperandTraits<ConstantVector>::op_end(this) - V.size(), |
| 910 | V.size()) { |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 911 | for (size_t i = 0, e = V.size(); i != e; i++) |
| 912 | assert(V[i]->getType() == T->getElementType() && |
Dan Gohman | fa73ea2 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 913 | "Initializer for vector element doesn't match vector element type!"); |
Jay Foad | 166579e | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 914 | std::copy(V.begin(), V.end(), op_begin()); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 917 | // ConstantVector accessors. |
Jay Foad | a0c1384 | 2011-06-22 09:10:19 +0000 | [diff] [blame] | 918 | Constant *ConstantVector::get(ArrayRef<Constant*> V) { |
Jay Foad | 9afc527 | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 919 | assert(!V.empty() && "Vectors can't be empty"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 920 | VectorType *T = VectorType::get(V.front()->getType(), V.size()); |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 921 | LLVMContextImpl *pImpl = T->getContext().pImpl; |
Jay Foad | 9afc527 | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 922 | |
Chris Lattner | 2ca5c86 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 923 | // If this is an all-undef or all-zero vector, return a |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 924 | // ConstantAggregateZero or UndefValue. |
| 925 | Constant *C = V[0]; |
| 926 | bool isZero = C->isNullValue(); |
| 927 | bool isUndef = isa<UndefValue>(C); |
| 928 | |
| 929 | if (isZero || isUndef) { |
| 930 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 931 | if (V[i] != C) { |
| 932 | isZero = isUndef = false; |
| 933 | break; |
| 934 | } |
| 935 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 936 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 937 | if (isZero) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 938 | return ConstantAggregateZero::get(T); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 939 | if (isUndef) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 940 | return UndefValue::get(T); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 942 | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
| 943 | // the element type is compatible with ConstantDataVector. If so, use it. |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 944 | if (ConstantDataSequential::isElementTypeCompatible(C->getType())) { |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 945 | // We speculatively build the elements here even if it turns out that there |
| 946 | // is a constantexpr or something else weird in the array, since it is so |
| 947 | // uncommon for that to happen. |
| 948 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
| 949 | if (CI->getType()->isIntegerTy(8)) { |
| 950 | SmallVector<uint8_t, 16> Elts; |
| 951 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 952 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 953 | Elts.push_back(CI->getZExtValue()); |
| 954 | else |
| 955 | break; |
| 956 | if (Elts.size() == V.size()) |
| 957 | return ConstantDataVector::get(C->getContext(), Elts); |
| 958 | } else if (CI->getType()->isIntegerTy(16)) { |
| 959 | SmallVector<uint16_t, 16> Elts; |
| 960 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 961 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 962 | Elts.push_back(CI->getZExtValue()); |
| 963 | else |
| 964 | break; |
| 965 | if (Elts.size() == V.size()) |
| 966 | return ConstantDataVector::get(C->getContext(), Elts); |
| 967 | } else if (CI->getType()->isIntegerTy(32)) { |
| 968 | SmallVector<uint32_t, 16> Elts; |
| 969 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 970 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 971 | Elts.push_back(CI->getZExtValue()); |
| 972 | else |
| 973 | break; |
| 974 | if (Elts.size() == V.size()) |
| 975 | return ConstantDataVector::get(C->getContext(), Elts); |
| 976 | } else if (CI->getType()->isIntegerTy(64)) { |
| 977 | SmallVector<uint64_t, 16> Elts; |
| 978 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 979 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V[i])) |
| 980 | Elts.push_back(CI->getZExtValue()); |
| 981 | else |
| 982 | break; |
| 983 | if (Elts.size() == V.size()) |
| 984 | return ConstantDataVector::get(C->getContext(), Elts); |
| 985 | } |
| 986 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 987 | |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 988 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 989 | if (CFP->getType()->isFloatTy()) { |
| 990 | SmallVector<float, 16> Elts; |
| 991 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 992 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) |
| 993 | Elts.push_back(CFP->getValueAPF().convertToFloat()); |
| 994 | else |
| 995 | break; |
| 996 | if (Elts.size() == V.size()) |
| 997 | return ConstantDataVector::get(C->getContext(), Elts); |
| 998 | } else if (CFP->getType()->isDoubleTy()) { |
| 999 | SmallVector<double, 16> Elts; |
| 1000 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1001 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V[i])) |
| 1002 | Elts.push_back(CFP->getValueAPF().convertToDouble()); |
| 1003 | else |
| 1004 | break; |
| 1005 | if (Elts.size() == V.size()) |
| 1006 | return ConstantDataVector::get(C->getContext(), Elts); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 1011 | // Otherwise, the element type isn't compatible with ConstantDataVector, or |
| 1012 | // the operand list constants a ConstantExpr or something else strange. |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1013 | return pImpl->VectorConstants.getOrCreate(T, V); |
| 1014 | } |
| 1015 | |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 1016 | Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) { |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 1017 | // If this splat is compatible with ConstantDataVector, use it instead of |
| 1018 | // ConstantVector. |
| 1019 | if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) && |
| 1020 | ConstantDataSequential::isElementTypeCompatible(V->getType())) |
| 1021 | return ConstantDataVector::getSplat(NumElts, V); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 1023 | SmallVector<Constant*, 32> Elts(NumElts, V); |
| 1024 | return get(Elts); |
| 1025 | } |
| 1026 | |
| 1027 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1028 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 1029 | // can't be inline because we don't want to #include Instruction.h into |
| 1030 | // Constant.h |
| 1031 | bool ConstantExpr::isCast() const { |
| 1032 | return Instruction::isCast(getOpcode()); |
| 1033 | } |
| 1034 | |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1035 | bool ConstantExpr::isCompare() const { |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1036 | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Dan Gohman | e6992f7 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1039 | bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const { |
| 1040 | if (getOpcode() != Instruction::GetElementPtr) return false; |
| 1041 | |
| 1042 | gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this); |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 1043 | User::const_op_iterator OI = llvm::next(this->op_begin()); |
Dan Gohman | e6992f7 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Skip the first index, as it has no static limit. |
| 1046 | ++GEPI; |
| 1047 | ++OI; |
| 1048 | |
| 1049 | // The remaining indices must be compile-time known integers within the |
| 1050 | // bounds of the corresponding notional static array types. |
| 1051 | for (; GEPI != E; ++GEPI, ++OI) { |
| 1052 | ConstantInt *CI = dyn_cast<ConstantInt>(*OI); |
| 1053 | if (!CI) return false; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1054 | if (ArrayType *ATy = dyn_cast<ArrayType>(*GEPI)) |
Dan Gohman | e6992f7 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1055 | if (CI->getValue().getActiveBits() > 64 || |
| 1056 | CI->getZExtValue() >= ATy->getNumElements()) |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | // All the indices checked out. |
| 1061 | return true; |
| 1062 | } |
| 1063 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1064 | bool ConstantExpr::hasIndices() const { |
| 1065 | return getOpcode() == Instruction::ExtractValue || |
| 1066 | getOpcode() == Instruction::InsertValue; |
| 1067 | } |
| 1068 | |
Jay Foad | d30aa5a | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 1069 | ArrayRef<unsigned> ConstantExpr::getIndices() const { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1070 | if (const ExtractValueConstantExpr *EVCE = |
| 1071 | dyn_cast<ExtractValueConstantExpr>(this)) |
| 1072 | return EVCE->Indices; |
Dan Gohman | 1a20357 | 2008-06-23 16:39:44 +0000 | [diff] [blame] | 1073 | |
| 1074 | return cast<InsertValueConstantExpr>(this)->Indices; |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 1077 | unsigned ConstantExpr::getPredicate() const { |
Chris Lattner | 3e19473 | 2011-07-17 06:01:30 +0000 | [diff] [blame] | 1078 | assert(isCompare()); |
Chris Lattner | b7daa84 | 2007-10-18 16:26:24 +0000 | [diff] [blame] | 1079 | return ((const CompareConstantExpr*)this)->predicate; |
Reid Spencer | 728b6db | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 1080 | } |
Chris Lattner | f4ba6c7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 1081 | |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 1082 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 1083 | /// one, but with the specified operand set to the specified value. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1084 | Constant * |
| 1085 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 1086 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 1087 | "Replacing operand with value of different type!"); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1088 | if (getOperand(OpNo) == Op) |
| 1089 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 1a8def6 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1090 | |
| 1091 | SmallVector<Constant*, 8> NewOps; |
| 1092 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1093 | NewOps.push_back(i == OpNo ? Op : getOperand(i)); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1094 | |
Chris Lattner | 1a8def6 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1095 | return getWithOperands(NewOps); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /// getWithOperands - This returns the current constant expression with the |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1099 | /// operands replaced with the specified values. The specified array must |
| 1100 | /// have the same number of operands as our current one. |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1101 | Constant *ConstantExpr:: |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1102 | getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { |
Jay Foad | b81e457 | 2011-04-13 13:46:01 +0000 | [diff] [blame] | 1103 | assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1104 | bool AnyChange = Ty != getType(); |
| 1105 | for (unsigned i = 0; i != Ops.size(); ++i) |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1106 | AnyChange |= Ops[i] != getOperand(i); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1108 | if (!AnyChange) // No operands changed, return self. |
| 1109 | return const_cast<ConstantExpr*>(this); |
| 1110 | |
| 1111 | switch (getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1112 | case Instruction::Trunc: |
| 1113 | case Instruction::ZExt: |
| 1114 | case Instruction::SExt: |
| 1115 | case Instruction::FPTrunc: |
| 1116 | case Instruction::FPExt: |
| 1117 | case Instruction::UIToFP: |
| 1118 | case Instruction::SIToFP: |
| 1119 | case Instruction::FPToUI: |
| 1120 | case Instruction::FPToSI: |
| 1121 | case Instruction::PtrToInt: |
| 1122 | case Instruction::IntToPtr: |
| 1123 | case Instruction::BitCast: |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1124 | return ConstantExpr::getCast(getOpcode(), Ops[0], Ty); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1125 | case Instruction::Select: |
| 1126 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 1127 | case Instruction::InsertElement: |
| 1128 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 1129 | case Instruction::ExtractElement: |
| 1130 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
Chris Lattner | 1a8def6 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1131 | case Instruction::InsertValue: |
| 1132 | return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices()); |
| 1133 | case Instruction::ExtractValue: |
| 1134 | return ConstantExpr::getExtractValue(Ops[0], getIndices()); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1135 | case Instruction::ShuffleVector: |
| 1136 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | f9021ff | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 1137 | case Instruction::GetElementPtr: |
Chris Lattner | 1a8def6 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1138 | return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1), |
| 1139 | cast<GEPOperator>(this)->isInBounds()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1140 | case Instruction::ICmp: |
| 1141 | case Instruction::FCmp: |
| 1142 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); |
Chris Lattner | b88a7fb | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1143 | default: |
| 1144 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 1145 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData); |
Chris Lattner | 1fe8f6b | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1149 | |
| 1150 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1151 | // isValueValidForType implementations |
| 1152 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1153 | bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1154 | unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay |
| 1155 | if (Ty->isIntegerTy(1)) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1156 | return Val == 0 || Val == 1; |
Reid Spencer | 554cec6 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 1157 | if (NumBits >= 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1158 | return true; // always true, has to fit in largest type |
| 1159 | uint64_t Max = (1ll << NumBits) - 1; |
| 1160 | return Val <= Max; |
Reid Spencer | 9b11d51 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1163 | bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1164 | unsigned NumBits = Ty->getIntegerBitWidth(); |
| 1165 | if (Ty->isIntegerTy(1)) |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1166 | return Val == 0 || Val == 1 || Val == -1; |
Reid Spencer | 554cec6 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 1167 | if (NumBits >= 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1168 | return true; // always true, has to fit in largest type |
| 1169 | int64_t Min = -(1ll << (NumBits-1)); |
| 1170 | int64_t Max = (1ll << (NumBits-1)) - 1; |
| 1171 | return (Val >= Min && Val <= Max); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1174 | bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1175 | // convert modifies in place, so make a copy. |
| 1176 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1177 | bool losesInfo; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 1178 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1179 | default: |
| 1180 | return false; // These can't be represented as floating point! |
| 1181 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1182 | // FIXME rounding mode needs to be more flexible |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1183 | case Type::HalfTyID: { |
| 1184 | if (&Val2.getSemantics() == &APFloat::IEEEhalf) |
| 1185 | return true; |
| 1186 | Val2.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1187 | return !losesInfo; |
| 1188 | } |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1189 | case Type::FloatTyID: { |
| 1190 | if (&Val2.getSemantics() == &APFloat::IEEEsingle) |
| 1191 | return true; |
| 1192 | Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1193 | return !losesInfo; |
| 1194 | } |
| 1195 | case Type::DoubleTyID: { |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1196 | if (&Val2.getSemantics() == &APFloat::IEEEhalf || |
| 1197 | &Val2.getSemantics() == &APFloat::IEEEsingle || |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1198 | &Val2.getSemantics() == &APFloat::IEEEdouble) |
| 1199 | return true; |
| 1200 | Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1201 | return !losesInfo; |
| 1202 | } |
Dale Johannesen | ebbc95d | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1203 | case Type::X86_FP80TyID: |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1204 | return &Val2.getSemantics() == &APFloat::IEEEhalf || |
| 1205 | &Val2.getSemantics() == &APFloat::IEEEsingle || |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1206 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1207 | &Val2.getSemantics() == &APFloat::x87DoubleExtended; |
Dale Johannesen | ebbc95d | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1208 | case Type::FP128TyID: |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1209 | return &Val2.getSemantics() == &APFloat::IEEEhalf || |
| 1210 | &Val2.getSemantics() == &APFloat::IEEEsingle || |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1211 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1212 | &Val2.getSemantics() == &APFloat::IEEEquad; |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 1213 | case Type::PPC_FP128TyID: |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1214 | return &Val2.getSemantics() == &APFloat::IEEEhalf || |
| 1215 | &Val2.getSemantics() == &APFloat::IEEEsingle || |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 1216 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 1217 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1218 | } |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 1219 | } |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1222 | //===----------------------------------------------------------------------===// |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1223 | // Factory Function Implementation |
| 1224 | |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1225 | ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { |
Chris Lattner | 61c70e9 | 2010-08-28 04:09:24 +0000 | [diff] [blame] | 1226 | assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1227 | "Cannot create an aggregate zero of non-aggregate type!"); |
| 1228 | |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1229 | ConstantAggregateZero *&Entry = Ty->getContext().pImpl->CAZConstants[Ty]; |
| 1230 | if (Entry == 0) |
| 1231 | Entry = new ConstantAggregateZero(Ty); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1233 | return Entry; |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 1236 | /// destroyConstant - Remove the constant from the constant table. |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1237 | /// |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1238 | void ConstantAggregateZero::destroyConstant() { |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1239 | getContext().pImpl->CAZConstants.erase(getType()); |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1240 | destroyConstantImpl(); |
| 1241 | } |
| 1242 | |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1243 | /// destroyConstant - Remove the constant from the constant table... |
| 1244 | /// |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1245 | void ConstantArray::destroyConstant() { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1246 | getType()->getContext().pImpl->ArrayConstants.remove(this); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1247 | destroyConstantImpl(); |
| 1248 | } |
| 1249 | |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1250 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1251 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1252 | // |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1253 | |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1254 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1255 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1256 | void ConstantStruct::destroyConstant() { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1257 | getType()->getContext().pImpl->StructConstants.remove(this); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1258 | destroyConstantImpl(); |
| 1259 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1260 | |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1261 | // destroyConstant - Remove the constant from the constant table... |
| 1262 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1263 | void ConstantVector::destroyConstant() { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1264 | getType()->getContext().pImpl->VectorConstants.remove(this); |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1265 | destroyConstantImpl(); |
| 1266 | } |
| 1267 | |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1268 | /// getSplatValue - If this is a splat vector constant, meaning that all of |
| 1269 | /// the elements have the same value, return that value. Otherwise return 0. |
| 1270 | Constant *Constant::getSplatValue() const { |
| 1271 | assert(this->getType()->isVectorTy() && "Only valid for vectors!"); |
| 1272 | if (isa<ConstantAggregateZero>(this)) |
| 1273 | return getNullValue(this->getType()->getVectorElementType()); |
| 1274 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 1275 | return CV->getSplatValue(); |
| 1276 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
| 1277 | return CV->getSplatValue(); |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Dan Gohman | 3b7cf0a | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1281 | /// getSplatValue - If this is a splat constant, where all of the |
| 1282 | /// elements have the same value, return that value. Otherwise return null. |
Duncan Sands | 7681c6d | 2011-02-01 08:39:12 +0000 | [diff] [blame] | 1283 | Constant *ConstantVector::getSplatValue() const { |
Dan Gohman | 3b7cf0a | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1284 | // Check out first element. |
| 1285 | Constant *Elt = getOperand(0); |
| 1286 | // Then make sure all remaining elements point to the same value. |
| 1287 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) |
Chris Lattner | 3e19473 | 2011-07-17 06:01:30 +0000 | [diff] [blame] | 1288 | if (getOperand(I) != Elt) |
| 1289 | return 0; |
Dan Gohman | 3b7cf0a | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1290 | return Elt; |
| 1291 | } |
| 1292 | |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1293 | /// If C is a constant integer then return its value, otherwise C must be a |
| 1294 | /// vector of constant integers, all equal, and the common value is returned. |
| 1295 | const APInt &Constant::getUniqueInteger() const { |
| 1296 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 1297 | return CI->getValue(); |
| 1298 | assert(this->getSplatValue() && "Doesn't contain a unique integer!"); |
| 1299 | const Constant *C = this->getAggregateElement(0U); |
| 1300 | assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!"); |
| 1301 | return cast<ConstantInt>(C)->getValue(); |
| 1302 | } |
| 1303 | |
| 1304 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1305 | //---- ConstantPointerNull::get() implementation. |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1306 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1308 | ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1309 | ConstantPointerNull *&Entry = Ty->getContext().pImpl->CPNConstants[Ty]; |
| 1310 | if (Entry == 0) |
| 1311 | Entry = new ConstantPointerNull(Ty); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1313 | return Entry; |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1316 | // destroyConstant - Remove the constant from the constant table... |
| 1317 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1318 | void ConstantPointerNull::destroyConstant() { |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1319 | getContext().pImpl->CPNConstants.erase(getType()); |
| 1320 | // Free the constant and any dangling references to it. |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1321 | destroyConstantImpl(); |
| 1322 | } |
| 1323 | |
| 1324 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1325 | //---- UndefValue::get() implementation. |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1326 | // |
| 1327 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1328 | UndefValue *UndefValue::get(Type *Ty) { |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1329 | UndefValue *&Entry = Ty->getContext().pImpl->UVConstants[Ty]; |
| 1330 | if (Entry == 0) |
| 1331 | Entry = new UndefValue(Ty); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1333 | return Entry; |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | // destroyConstant - Remove the constant from the constant table. |
| 1337 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1338 | void UndefValue::destroyConstant() { |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1339 | // Free the constant and any dangling references to it. |
| 1340 | getContext().pImpl->UVConstants.erase(getType()); |
Chris Lattner | b9f1859 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1341 | destroyConstantImpl(); |
| 1342 | } |
| 1343 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1344 | //---- BlockAddress::get() implementation. |
| 1345 | // |
| 1346 | |
| 1347 | BlockAddress *BlockAddress::get(BasicBlock *BB) { |
| 1348 | assert(BB->getParent() != 0 && "Block must have a parent"); |
| 1349 | return get(BB->getParent(), BB); |
| 1350 | } |
| 1351 | |
| 1352 | BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) { |
| 1353 | BlockAddress *&BA = |
| 1354 | F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)]; |
| 1355 | if (BA == 0) |
| 1356 | BA = new BlockAddress(F, BB); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1358 | assert(BA->getFunction() == F && "Basic block moved between functions"); |
| 1359 | return BA; |
| 1360 | } |
| 1361 | |
| 1362 | BlockAddress::BlockAddress(Function *F, BasicBlock *BB) |
| 1363 | : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal, |
| 1364 | &Op<0>(), 2) { |
Chris Lattner | d0ec235 | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1365 | setOperand(0, F); |
| 1366 | setOperand(1, BB); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 1367 | BB->AdjustBlockAddressRefCount(1); |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | // destroyConstant - Remove the constant from the constant table. |
| 1372 | // |
| 1373 | void BlockAddress::destroyConstant() { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1374 | getFunction()->getType()->getContext().pImpl |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1375 | ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock())); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 1376 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1377 | destroyConstantImpl(); |
| 1378 | } |
| 1379 | |
| 1380 | void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { |
| 1381 | // This could be replacing either the Basic Block or the Function. In either |
| 1382 | // case, we have to remove the map entry. |
| 1383 | Function *NewF = getFunction(); |
| 1384 | BasicBlock *NewBB = getBasicBlock(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1386 | if (U == &Op<0>()) |
| 1387 | NewF = cast<Function>(To); |
| 1388 | else |
| 1389 | NewBB = cast<BasicBlock>(To); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1391 | // See if the 'new' entry already exists, if not, just update this in place |
| 1392 | // and return early. |
| 1393 | BlockAddress *&NewBA = |
| 1394 | getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)]; |
| 1395 | if (NewBA == 0) { |
Chris Lattner | d0ec235 | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1396 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1398 | // Remove the old entry, this can't cause the map to rehash (just a |
| 1399 | // tombstone will get added). |
| 1400 | getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(), |
| 1401 | getBasicBlock())); |
| 1402 | NewBA = this; |
Chris Lattner | d0ec235 | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1403 | setOperand(0, NewF); |
| 1404 | setOperand(1, NewBB); |
| 1405 | getBasicBlock()->AdjustBlockAddressRefCount(1); |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1406 | return; |
| 1407 | } |
| 1408 | |
| 1409 | // Otherwise, I do need to replace this with an existing value. |
| 1410 | assert(NewBA != this && "I didn't contain From!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1412 | // Everyone using this now uses the replacement. |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 1413 | replaceAllUsesWith(NewBA); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1414 | |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1415 | destroyConstant(); |
| 1416 | } |
| 1417 | |
| 1418 | //---- ConstantExpr::get() implementations. |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1419 | // |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1420 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1421 | /// 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] | 1422 | /// 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] | 1423 | static inline Constant *getFoldedCast( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1424 | Instruction::CastOps opc, Constant *C, Type *Ty) { |
Chris Lattner | 9eacf8a | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1425 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1426 | // Fold a few common cases |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1427 | if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1428 | return FC; |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1429 | |
Owen Anderson | d03eecd | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1430 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 1431 | |
Nadav Rotem | 82e905a | 2013-03-07 01:30:40 +0000 | [diff] [blame] | 1432 | // Look up the constant in the table first to ensure uniqueness. |
Nadav Rotem | 55d8f6d | 2013-03-07 01:38:04 +0000 | [diff] [blame] | 1433 | ExprMapKeyType Key(opc, C); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1434 | |
Owen Anderson | d03eecd | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1435 | return pImpl->ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1436 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1437 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1438 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1439 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1440 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1441 | assert(C && Ty && "Null arguments to getCast"); |
Chris Lattner | 0b68a00 | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1442 | assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1443 | |
| 1444 | switch (opc) { |
Chris Lattner | 0b68a00 | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1445 | default: |
| 1446 | llvm_unreachable("Invalid cast opcode"); |
Chris Lattner | 0b68a00 | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1447 | case Instruction::Trunc: return getTrunc(C, Ty); |
| 1448 | case Instruction::ZExt: return getZExt(C, Ty); |
| 1449 | case Instruction::SExt: return getSExt(C, Ty); |
| 1450 | case Instruction::FPTrunc: return getFPTrunc(C, Ty); |
| 1451 | case Instruction::FPExt: return getFPExtend(C, Ty); |
| 1452 | case Instruction::UIToFP: return getUIToFP(C, Ty); |
| 1453 | case Instruction::SIToFP: return getSIToFP(C, Ty); |
| 1454 | case Instruction::FPToUI: return getFPToUI(C, Ty); |
| 1455 | case Instruction::FPToSI: return getFPToSI(C, Ty); |
| 1456 | case Instruction::PtrToInt: return getPtrToInt(C, Ty); |
| 1457 | case Instruction::IntToPtr: return getIntToPtr(C, Ty); |
| 1458 | case Instruction::BitCast: return getBitCast(C, Ty); |
Chris Lattner | f5ac6c2 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1459 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1460 | } |
Reid Spencer | 7858b33 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1461 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1462 | Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1463 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1464 | return getBitCast(C, Ty); |
| 1465 | return getZExt(C, Ty); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1468 | Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1469 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1470 | return getBitCast(C, Ty); |
| 1471 | return getSExt(C, Ty); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1474 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1475 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1476 | return getBitCast(C, Ty); |
| 1477 | return getTrunc(C, Ty); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1480 | Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { |
Evgeniy Stepanov | 655578f | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 1481 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 1482 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 1483 | "Invalid cast"); |
Reid Spencer | c0459fb | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1484 | |
Evgeniy Stepanov | 655578f | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 1485 | if (Ty->isIntOrIntVectorTy()) |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1486 | return getPtrToInt(S, Ty); |
| 1487 | return getBitCast(S, Ty); |
Reid Spencer | c0459fb | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1490 | Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1491 | bool isSigned) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1492 | assert(C->getType()->isIntOrIntVectorTy() && |
| 1493 | Ty->isIntOrIntVectorTy() && "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1494 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1495 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1496 | Instruction::CastOps opcode = |
| 1497 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1498 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1499 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1500 | return getCast(opcode, C, Ty); |
| 1501 | } |
| 1502 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1503 | Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1504 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1505 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1506 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1507 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | f25212a | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1508 | if (SrcBits == DstBits) |
| 1509 | return C; // Avoid a useless cast |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1510 | Instruction::CastOps opcode = |
Jay Foad | 9afc527 | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 1511 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); |
Reid Spencer | 84f3eab | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1512 | return getCast(opcode, C, Ty); |
| 1513 | } |
| 1514 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1515 | Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1516 | #ifndef NDEBUG |
| 1517 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1518 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1519 | #endif |
| 1520 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1521 | assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer"); |
| 1522 | assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1523 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1524 | "SrcTy must be larger than DestTy for Trunc!"); |
| 1525 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1526 | return getFoldedCast(Instruction::Trunc, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1529 | Constant *ConstantExpr::getSExt(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1530 | #ifndef NDEBUG |
| 1531 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1532 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1533 | #endif |
| 1534 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1535 | assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral"); |
| 1536 | assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1537 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1538 | "SrcTy must be smaller than DestTy for SExt!"); |
| 1539 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1540 | return getFoldedCast(Instruction::SExt, C, Ty); |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1543 | Constant *ConstantExpr::getZExt(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1544 | #ifndef NDEBUG |
| 1545 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1546 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1547 | #endif |
| 1548 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1549 | assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral"); |
| 1550 | assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1551 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1552 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 1553 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1554 | return getFoldedCast(Instruction::ZExt, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1557 | Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1558 | #ifndef NDEBUG |
| 1559 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1560 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1561 | #endif |
| 1562 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1563 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1564 | C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1565 | "This is an illegal floating point truncation!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1566 | return getFoldedCast(Instruction::FPTrunc, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1569 | Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1570 | #ifndef NDEBUG |
| 1571 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1572 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1573 | #endif |
| 1574 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1575 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1576 | C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1577 | "This is an illegal floating point extension!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1578 | return getFoldedCast(Instruction::FPExt, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1581 | Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1582 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1583 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1584 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1585 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1586 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1587 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1588 | "This is an illegal uint to floating point cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1589 | return getFoldedCast(Instruction::UIToFP, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1592 | Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1593 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1594 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1595 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1596 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1597 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1598 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1599 | "This is an illegal sint to floating point cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1600 | return getFoldedCast(Instruction::SIToFP, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1603 | Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1604 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1605 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1606 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1607 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1608 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1609 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1610 | "This is an illegal floating point to uint cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1611 | return getFoldedCast(Instruction::FPToUI, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1614 | Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty) { |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1615 | #ifndef NDEBUG |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1616 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1617 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | b6dc935 | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1618 | #endif |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1619 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1620 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1621 | "This is an illegal floating point to sint cast!"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1622 | return getFoldedCast(Instruction::FPToSI, C, Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1625 | Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) { |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1626 | assert(C->getType()->getScalarType()->isPointerTy() && |
| 1627 | "PtrToInt source must be pointer or pointer vector"); |
| 1628 | assert(DstTy->getScalarType()->isIntegerTy() && |
| 1629 | "PtrToInt destination must be integer or integer vector"); |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1630 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
Nick Lewycky | 1486ae6 | 2012-01-25 03:20:12 +0000 | [diff] [blame] | 1631 | if (isa<VectorType>(C->getType())) |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1632 | assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1633 | "Invalid cast between a different number of vector elements"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1634 | return getFoldedCast(Instruction::PtrToInt, C, DstTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1637 | Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) { |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1638 | assert(C->getType()->getScalarType()->isIntegerTy() && |
| 1639 | "IntToPtr source must be integer or integer vector"); |
| 1640 | assert(DstTy->getScalarType()->isPointerTy() && |
| 1641 | "IntToPtr destination must be a pointer or pointer vector"); |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1642 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
Nick Lewycky | 1486ae6 | 2012-01-25 03:20:12 +0000 | [diff] [blame] | 1643 | if (isa<VectorType>(C->getType())) |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1644 | assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1645 | "Invalid cast between a different number of vector elements"); |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1646 | return getFoldedCast(Instruction::IntToPtr, C, DstTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1649 | Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy) { |
Chris Lattner | 0b68a00 | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1650 | assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) && |
| 1651 | "Invalid constantexpr bitcast!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1652 | |
Chris Lattner | 8c7f24a | 2009-03-21 06:55:54 +0000 | [diff] [blame] | 1653 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 1654 | // speedily. |
| 1655 | if (C->getType() == DstTy) return C; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1656 | |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1657 | return getFoldedCast(Instruction::BitCast, C, DstTy); |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1660 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, |
| 1661 | unsigned Flags) { |
| 1662 | // Check the operands for consistency first. |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1663 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 1664 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1665 | "Invalid opcode in binary constant expression"); |
| 1666 | assert(C1->getType() == C2->getType() && |
| 1667 | "Operand types in binary constant expression should match"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1669 | #ifndef NDEBUG |
| 1670 | switch (Opcode) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1671 | case Instruction::Add: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1672 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1673 | case Instruction::Mul: |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1674 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1675 | assert(C1->getType()->isIntOrIntVectorTy() && |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1676 | "Tried to create an integer operation on a non-integer type!"); |
| 1677 | break; |
| 1678 | case Instruction::FAdd: |
| 1679 | case Instruction::FSub: |
| 1680 | case Instruction::FMul: |
| 1681 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1682 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1683 | "Tried to create a floating-point operation on a " |
| 1684 | "non-floating-point type!"); |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1685 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1686 | case Instruction::UDiv: |
| 1687 | case Instruction::SDiv: |
| 1688 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1689 | assert(C1->getType()->isIntOrIntVectorTy() && |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1690 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1691 | break; |
| 1692 | case Instruction::FDiv: |
| 1693 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1694 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1695 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1696 | break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1697 | case Instruction::URem: |
| 1698 | case Instruction::SRem: |
| 1699 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1700 | assert(C1->getType()->isIntOrIntVectorTy() && |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1701 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1702 | break; |
| 1703 | case Instruction::FRem: |
| 1704 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1705 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1706 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1707 | break; |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1708 | case Instruction::And: |
| 1709 | case Instruction::Or: |
| 1710 | case Instruction::Xor: |
| 1711 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1712 | assert(C1->getType()->isIntOrIntVectorTy() && |
Misha Brukman | 1bae291 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1713 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1714 | break; |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1715 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1716 | case Instruction::LShr: |
| 1717 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1718 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1719 | assert(C1->getType()->isIntOrIntVectorTy() && |
Chris Lattner | 91b362b | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1720 | "Tried to create a shift operation on a non-integer type!"); |
| 1721 | break; |
| 1722 | default: |
| 1723 | break; |
| 1724 | } |
| 1725 | #endif |
| 1726 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1727 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 1728 | return FC; // Fold a few common cases. |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1729 | |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1730 | Constant *ArgVec[] = { C1, C2 }; |
| 1731 | ExprMapKeyType Key(Opcode, ArgVec, 0, Flags); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1732 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1733 | LLVMContextImpl *pImpl = C1->getContext().pImpl; |
| 1734 | return pImpl->ExprConstants.getOrCreate(C1->getType(), Key); |
Reid Spencer | 67263fe | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1737 | Constant *ConstantExpr::getSizeOf(Type* Ty) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1738 | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
| 1739 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1740 | Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1741 | Constant *GEP = getGetElementPtr( |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1742 | Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1743 | return getPtrToInt(GEP, |
| 1744 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1747 | Constant *ConstantExpr::getAlignOf(Type* Ty) { |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 1748 | // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1 |
Dan Gohman | e2574d3 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1749 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1750 | Type *AligningTy = |
Chris Lattner | b231866 | 2011-06-18 22:48:56 +0000 | [diff] [blame] | 1751 | StructType::get(Type::getInt1Ty(Ty->getContext()), Ty, NULL); |
Micah Villmow | b8bce92 | 2012-10-24 17:25:11 +0000 | [diff] [blame] | 1752 | Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo()); |
Dan Gohman | 06ed3e7 | 2010-01-28 02:43:22 +0000 | [diff] [blame] | 1753 | Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1754 | Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1755 | Constant *Indices[2] = { Zero, One }; |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1756 | Constant *GEP = getGetElementPtr(NullPtr, Indices); |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1757 | return getPtrToInt(GEP, |
| 1758 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1761 | Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) { |
Dan Gohman | 2544a1d | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1762 | return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()), |
| 1763 | FieldNo)); |
| 1764 | } |
| 1765 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1766 | Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) { |
Dan Gohman | 3778f21 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1767 | // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo |
| 1768 | // Note that a non-inbounds gep is used, as null isn't within any object. |
| 1769 | Constant *GEPIdx[] = { |
Dan Gohman | 2544a1d | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1770 | ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0), |
| 1771 | FieldNo |
Dan Gohman | 3778f21 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1772 | }; |
| 1773 | Constant *GEP = getGetElementPtr( |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1774 | Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); |
Dan Gohman | 3b49063 | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1775 | return getPtrToInt(GEP, |
| 1776 | Type::getInt64Ty(Ty->getContext())); |
Dan Gohman | 3778f21 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1777 | } |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1778 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1779 | Constant *ConstantExpr::getCompare(unsigned short Predicate, |
| 1780 | Constant *C1, Constant *C2) { |
Reid Spencer | 67263fe | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1781 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1782 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1783 | switch (Predicate) { |
| 1784 | default: llvm_unreachable("Invalid CmpInst predicate"); |
| 1785 | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
| 1786 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
| 1787 | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
| 1788 | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
| 1789 | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
| 1790 | case CmpInst::FCMP_TRUE: |
| 1791 | return getFCmp(Predicate, C1, C2); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1792 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1793 | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
| 1794 | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
| 1795 | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
| 1796 | case CmpInst::ICMP_SLE: |
| 1797 | return getICmp(Predicate, C1, C2); |
| 1798 | } |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1801 | Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) { |
Chris Lattner | 9ace0cd | 2008-12-29 00:16:12 +0000 | [diff] [blame] | 1802 | assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1804 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
| 1805 | return SC; // Fold common cases |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1806 | |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1807 | Constant *ArgVec[] = { C, V1, V2 }; |
| 1808 | ExprMapKeyType Key(Instruction::Select, ArgVec); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1810 | LLVMContextImpl *pImpl = C->getContext().pImpl; |
| 1811 | return pImpl->ExprConstants.getOrCreate(V1->getType(), Key); |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1814 | Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs, |
| 1815 | bool InBounds) { |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1816 | assert(C->getType()->isPtrOrPtrVectorTy() && |
| 1817 | "Non-pointer type for constant GetElementPtr expression"); |
| 1818 | |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1819 | if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs)) |
Chris Lattner | 1f78d51 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1820 | return FC; // Fold a few common cases. |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1822 | // Get the result type of the getelementptr! |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1823 | Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), Idxs); |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1824 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1825 | unsigned AS = C->getType()->getPointerAddressSpace(); |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1826 | Type *ReqTy = Ty->getPointerTo(AS); |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1827 | if (VectorType *VecTy = dyn_cast<VectorType>(C->getType())) |
| 1828 | ReqTy = VectorType::get(ReqTy, VecTy->getNumElements()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1829 | |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1830 | // Look up the constant in the table first to ensure uniqueness |
| 1831 | std::vector<Constant*> ArgVec; |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1832 | ArgVec.reserve(1 + Idxs.size()); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1833 | ArgVec.push_back(C); |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1834 | for (unsigned i = 0, e = Idxs.size(); i != e; ++i) { |
| 1835 | assert(Idxs[i]->getType()->isVectorTy() == ReqTy->isVectorTy() && |
| 1836 | "getelementptr index type missmatch"); |
| 1837 | assert((!Idxs[i]->getType()->isVectorTy() || |
| 1838 | ReqTy->getVectorNumElements() == |
| 1839 | Idxs[i]->getType()->getVectorNumElements()) && |
| 1840 | "getelementptr index type missmatch"); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1841 | ArgVec.push_back(cast<Constant>(Idxs[i])); |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1842 | } |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1843 | const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0, |
Chris Lattner | 1f78d51 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1844 | InBounds ? GEPOperator::IsInBounds : 0); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1846 | LLVMContextImpl *pImpl = C->getContext().pImpl; |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1847 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
| 1848 | } |
| 1849 | |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1850 | Constant * |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1851 | ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) { |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1852 | assert(LHS->getType() == RHS->getType()); |
| 1853 | assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1854 | pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); |
| 1855 | |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1856 | if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1857 | return FC; // Fold a few common cases... |
| 1858 | |
| 1859 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1860 | Constant *ArgVec[] = { LHS, RHS }; |
Reid Spencer | 4fa021a | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1861 | // Get the key type with both the opcode and predicate |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1862 | const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); |
Owen Anderson | 31c36f0 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1863 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1864 | Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
| 1865 | if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1866 | ResultTy = VectorType::get(ResultTy, VT->getNumElements()); |
| 1867 | |
Owen Anderson | d03eecd | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1868 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1869 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | Constant * |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1873 | ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) { |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1874 | assert(LHS->getType() == RHS->getType()); |
| 1875 | assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); |
| 1876 | |
Chris Lattner | b29d596 | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1877 | if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1878 | return FC; // Fold a few common cases... |
| 1879 | |
| 1880 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1881 | Constant *ArgVec[] = { LHS, RHS }; |
Reid Spencer | 4fa021a | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1882 | // Get the key type with both the opcode and predicate |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1883 | const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1885 | Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
| 1886 | if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1887 | ResultTy = VectorType::get(ResultTy, VT->getNumElements()); |
| 1888 | |
Owen Anderson | d03eecd | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1889 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
Nick Lewycky | 401f325 | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1890 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
Reid Spencer | 077d0eb | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1893 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1894 | assert(Val->getType()->isVectorTy() && |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1895 | "Tried to create extractelement operation on non-vector type!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1896 | assert(Idx->getType()->isIntegerTy(32) && |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1897 | "Extractelement index must be i32 type!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1899 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
Chris Lattner | 83738a2 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 1900 | return FC; // Fold a few common cases. |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1901 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1902 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1903 | Constant *ArgVec[] = { Val, Idx }; |
| 1904 | const ExprMapKeyType Key(Instruction::ExtractElement, ArgVec); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1906 | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1907 | Type *ReqTy = Val->getType()->getVectorElementType(); |
Owen Anderson | d03eecd | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1908 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1912 | Constant *Idx) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1913 | assert(Val->getType()->isVectorTy() && |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1914 | "Tried to create insertelement operation on non-vector type!"); |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1915 | assert(Elt->getType() == Val->getType()->getVectorElementType() && |
| 1916 | "Insertelement types must match!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1917 | assert(Idx->getType()->isIntegerTy(32) && |
Reid Spencer | 3d10b0b | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1918 | "Insertelement index must be i32 type!"); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1920 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
| 1921 | return FC; // Fold a few common cases. |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1922 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1923 | Constant *ArgVec[] = { Val, Elt, Idx }; |
| 1924 | const ExprMapKeyType Key(Instruction::InsertElement, ArgVec); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1925 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1926 | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
| 1927 | return pImpl->ExprConstants.getOrCreate(Val->getType(), Key); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1931 | Constant *Mask) { |
| 1932 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1933 | "Invalid shuffle vector constant expr operands!"); |
Nate Begeman | 0f123cf | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1935 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
| 1936 | return FC; // Fold a few common cases. |
| 1937 | |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1938 | unsigned NElts = Mask->getType()->getVectorNumElements(); |
| 1939 | Type *EltTy = V1->getType()->getVectorElementType(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1940 | Type *ShufTy = VectorType::get(EltTy, NElts); |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1941 | |
| 1942 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 4e4cc7d | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1943 | Constant *ArgVec[] = { V1, V2, Mask }; |
| 1944 | const ExprMapKeyType Key(Instruction::ShuffleVector, ArgVec); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1945 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1946 | LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; |
| 1947 | return pImpl->ExprConstants.getOrCreate(ShufTy, Key); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1950 | Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1951 | ArrayRef<unsigned> Idxs) { |
| 1952 | assert(ExtractValueInst::getIndexedType(Agg->getType(), |
| 1953 | Idxs) == Val->getType() && |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1954 | "insertvalue indices invalid!"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1955 | assert(Agg->getType()->isFirstClassType() && |
Chris Lattner | 4e47aad | 2011-07-12 05:26:21 +0000 | [diff] [blame] | 1956 | "Non-first-class type for constant insertvalue expression"); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1957 | Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs); |
Chris Lattner | 4e47aad | 2011-07-12 05:26:21 +0000 | [diff] [blame] | 1958 | assert(FC && "insertvalue constant expr couldn't be folded!"); |
Dan Gohman | e089160 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 1959 | return FC; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1962 | Constant *ConstantExpr::getExtractValue(Constant *Agg, |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1963 | ArrayRef<unsigned> Idxs) { |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1964 | assert(Agg->getType()->isFirstClassType() && |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1965 | "Tried to create extractelement operation on non-first-class type!"); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1967 | Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs); |
Chandler Carruth | dc770fc | 2011-07-10 09:45:35 +0000 | [diff] [blame] | 1968 | (void)ReqTy; |
Chris Lattner | eaf7980 | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1969 | assert(ReqTy && "extractvalue indices invalid!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1970 | |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1971 | assert(Agg->getType()->isFirstClassType() && |
| 1972 | "Non-first-class type for constant extractvalue expression"); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1973 | Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs); |
Dan Gohman | e089160 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 1974 | assert(FC && "ExtractValue constant expr couldn't be folded!"); |
| 1975 | return FC; |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1978 | Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1979 | assert(C->getType()->isIntOrIntVectorTy() && |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1980 | "Cannot NEG a nonintegral value!"); |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1981 | return getSub(ConstantFP::getZeroValueForNegation(C->getType()), |
| 1982 | C, HasNUW, HasNSW); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1985 | Constant *ConstantExpr::getFNeg(Constant *C) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1986 | assert(C->getType()->isFPOrFPVectorTy() && |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1987 | "Cannot FNEG a non-floating-point value!"); |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1988 | return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1991 | Constant *ConstantExpr::getNot(Constant *C) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1992 | assert(C->getType()->isIntOrIntVectorTy() && |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1993 | "Cannot NOT a nonintegral value!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1994 | return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType())); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1997 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2, |
| 1998 | bool HasNUW, bool HasNSW) { |
| 1999 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2000 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2001 | return get(Instruction::Add, C1, C2, Flags); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2004 | Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2005 | return get(Instruction::FAdd, C1, C2); |
| 2006 | } |
| 2007 | |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2008 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2, |
| 2009 | bool HasNUW, bool HasNSW) { |
| 2010 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2011 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2012 | return get(Instruction::Sub, C1, C2, Flags); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2015 | Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2016 | return get(Instruction::FSub, C1, C2); |
| 2017 | } |
| 2018 | |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2019 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2, |
| 2020 | bool HasNUW, bool HasNSW) { |
| 2021 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2022 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2023 | return get(Instruction::Mul, C1, C2, Flags); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2026 | Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2027 | return get(Instruction::FMul, C1, C2); |
| 2028 | } |
| 2029 | |
Chris Lattner | 74f5c5a | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2030 | Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) { |
| 2031 | return get(Instruction::UDiv, C1, C2, |
| 2032 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Chris Lattner | 74f5c5a | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2035 | Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) { |
| 2036 | return get(Instruction::SDiv, C1, C2, |
| 2037 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2040 | Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2041 | return get(Instruction::FDiv, C1, C2); |
| 2042 | } |
| 2043 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2044 | Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2045 | return get(Instruction::URem, C1, C2); |
| 2046 | } |
| 2047 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2048 | Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2049 | return get(Instruction::SRem, C1, C2); |
| 2050 | } |
| 2051 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2052 | Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2053 | return get(Instruction::FRem, C1, C2); |
| 2054 | } |
| 2055 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2056 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2057 | return get(Instruction::And, C1, C2); |
| 2058 | } |
| 2059 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2060 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2061 | return get(Instruction::Or, C1, C2); |
| 2062 | } |
| 2063 | |
Chris Lattner | f067d58 | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2064 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2065 | return get(Instruction::Xor, C1, C2); |
| 2066 | } |
| 2067 | |
Chris Lattner | 81baf14 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2068 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2, |
| 2069 | bool HasNUW, bool HasNSW) { |
| 2070 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2071 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2072 | return get(Instruction::Shl, C1, C2, Flags); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Chris Lattner | 74f5c5a | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2075 | Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) { |
| 2076 | return get(Instruction::LShr, C1, C2, |
| 2077 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Chris Lattner | 74f5c5a | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2080 | Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) { |
| 2081 | return get(Instruction::AShr, C1, C2, |
| 2082 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Duncan Sands | c038a78 | 2012-06-12 14:33:56 +0000 | [diff] [blame] | 2085 | /// getBinOpIdentity - Return the identity for the given binary operation, |
| 2086 | /// i.e. a constant C such that X op C = X and C op X = X for every X. It |
Duncan Sands | ee5a094 | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2087 | /// returns null if the operator doesn't have an identity. |
Duncan Sands | c038a78 | 2012-06-12 14:33:56 +0000 | [diff] [blame] | 2088 | Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) { |
| 2089 | switch (Opcode) { |
| 2090 | default: |
Duncan Sands | ee5a094 | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2091 | // Doesn't have an identity. |
| 2092 | return 0; |
| 2093 | |
Duncan Sands | c038a78 | 2012-06-12 14:33:56 +0000 | [diff] [blame] | 2094 | case Instruction::Add: |
| 2095 | case Instruction::Or: |
| 2096 | case Instruction::Xor: |
| 2097 | return Constant::getNullValue(Ty); |
| 2098 | |
| 2099 | case Instruction::Mul: |
| 2100 | return ConstantInt::get(Ty, 1); |
| 2101 | |
| 2102 | case Instruction::And: |
| 2103 | return Constant::getAllOnesValue(Ty); |
| 2104 | } |
| 2105 | } |
| 2106 | |
Duncan Sands | ee5a094 | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2107 | /// getBinOpAbsorber - Return the absorbing element for the given binary |
| 2108 | /// operation, i.e. a constant C such that X op C = C and C op X = C for |
| 2109 | /// every X. For example, this returns zero for integer multiplication. |
| 2110 | /// It returns null if the operator doesn't have an absorbing element. |
| 2111 | Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) { |
| 2112 | switch (Opcode) { |
| 2113 | default: |
| 2114 | // Doesn't have an absorber. |
| 2115 | return 0; |
| 2116 | |
| 2117 | case Instruction::Or: |
| 2118 | return Constant::getAllOnesValue(Ty); |
| 2119 | |
| 2120 | case Instruction::And: |
| 2121 | case Instruction::Mul: |
| 2122 | return Constant::getNullValue(Ty); |
| 2123 | } |
| 2124 | } |
| 2125 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2126 | // destroyConstant - Remove the constant from the constant table... |
| 2127 | // |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2128 | void ConstantExpr::destroyConstant() { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2129 | getType()->getContext().pImpl->ExprConstants.remove(this); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 2130 | destroyConstantImpl(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 2133 | const char *ConstantExpr::getOpcodeName() const { |
| 2134 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2135 | } |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 2137 | |
| 2138 | |
| 2139 | GetElementPtrConstantExpr:: |
Chris Lattner | a7c6988 | 2012-01-26 20:40:56 +0000 | [diff] [blame] | 2140 | GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2141 | Type *DestTy) |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 2142 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 2143 | OperandTraits<GetElementPtrConstantExpr>::op_end(this) |
| 2144 | - (IdxList.size()+1), IdxList.size()+1) { |
| 2145 | OperandList[0] = C; |
| 2146 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 2147 | OperandList[i+1] = IdxList[i]; |
| 2148 | } |
| 2149 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2150 | //===----------------------------------------------------------------------===// |
| 2151 | // ConstantData* implementations |
| 2152 | |
| 2153 | void ConstantDataArray::anchor() {} |
| 2154 | void ConstantDataVector::anchor() {} |
| 2155 | |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2156 | /// getElementType - Return the element type of the array/vector. |
| 2157 | Type *ConstantDataSequential::getElementType() const { |
| 2158 | return getType()->getElementType(); |
| 2159 | } |
| 2160 | |
Chris Lattner | 9e631da | 2012-01-24 09:31:43 +0000 | [diff] [blame] | 2161 | StringRef ConstantDataSequential::getRawDataValues() const { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2162 | return StringRef(DataElements, getNumElements()*getElementByteSize()); |
Chris Lattner | 9e631da | 2012-01-24 09:31:43 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 2165 | /// isElementTypeCompatible - Return true if a ConstantDataSequential can be |
| 2166 | /// formed with a vector or array of the specified element type. |
| 2167 | /// ConstantDataArray only works with normal float and int types that are |
| 2168 | /// stored densely in memory, not with things like i42 or x86_f80. |
| 2169 | bool ConstantDataSequential::isElementTypeCompatible(const Type *Ty) { |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2170 | if (Ty->isFloatTy() || Ty->isDoubleTy()) return true; |
| 2171 | if (const IntegerType *IT = dyn_cast<IntegerType>(Ty)) { |
| 2172 | switch (IT->getBitWidth()) { |
| 2173 | case 8: |
| 2174 | case 16: |
| 2175 | case 32: |
| 2176 | case 64: |
| 2177 | return true; |
| 2178 | default: break; |
| 2179 | } |
| 2180 | } |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2184 | /// getNumElements - Return the number of elements in the array or vector. |
| 2185 | unsigned ConstantDataSequential::getNumElements() const { |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2186 | if (ArrayType *AT = dyn_cast<ArrayType>(getType())) |
| 2187 | return AT->getNumElements(); |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2188 | return getType()->getVectorNumElements(); |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2192 | /// getElementByteSize - Return the size in bytes of the elements in the data. |
| 2193 | uint64_t ConstantDataSequential::getElementByteSize() const { |
| 2194 | return getElementType()->getPrimitiveSizeInBits()/8; |
| 2195 | } |
| 2196 | |
| 2197 | /// getElementPointer - Return the start of the specified element. |
| 2198 | const char *ConstantDataSequential::getElementPointer(unsigned Elt) const { |
Chris Lattner | 1ee0ecf | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2199 | assert(Elt < getNumElements() && "Invalid Elt"); |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2200 | return DataElements+Elt*getElementByteSize(); |
| 2201 | } |
| 2202 | |
| 2203 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2204 | /// isAllZeros - return true if the array is empty or all zeros. |
| 2205 | static bool isAllZeros(StringRef Arr) { |
| 2206 | for (StringRef::iterator I = Arr.begin(), E = Arr.end(); I != E; ++I) |
| 2207 | if (*I != 0) |
| 2208 | return false; |
| 2209 | return true; |
| 2210 | } |
Chris Lattner | ff2b7f3 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 2211 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2212 | /// getImpl - This is the underlying implementation of all of the |
| 2213 | /// ConstantDataSequential::get methods. They all thunk down to here, providing |
Chris Lattner | 8cf27ef | 2012-01-30 18:19:30 +0000 | [diff] [blame] | 2214 | /// the correct element type. We take the bytes in as a StringRef because |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2215 | /// we *want* an underlying "char*" to avoid TBAA type punning violations. |
| 2216 | Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2217 | assert(isElementTypeCompatible(Ty->getSequentialElementType())); |
Chris Lattner | 29cc6cb | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 2218 | // If the elements are all zero or there are no elements, return a CAZ, which |
| 2219 | // is more dense and canonical. |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2220 | if (isAllZeros(Elements)) |
| 2221 | return ConstantAggregateZero::get(Ty); |
| 2222 | |
| 2223 | // Do a lookup to see if we have already formed one of these. |
| 2224 | StringMap<ConstantDataSequential*>::MapEntryTy &Slot = |
| 2225 | Ty->getContext().pImpl->CDSConstants.GetOrCreateValue(Elements); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2226 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2227 | // The bucket can point to a linked list of different CDS's that have the same |
| 2228 | // body but different types. For example, 0,0,0,1 could be a 4 element array |
| 2229 | // of i8, or a 1-element array of i32. They'll both end up in the same |
| 2230 | /// StringMap bucket, linked up by their Next pointers. Walk the list. |
| 2231 | ConstantDataSequential **Entry = &Slot.getValue(); |
| 2232 | for (ConstantDataSequential *Node = *Entry; Node != 0; |
| 2233 | Entry = &Node->Next, Node = *Entry) |
| 2234 | if (Node->getType() == Ty) |
| 2235 | return Node; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2237 | // Okay, we didn't get a hit. Create a node of the right class, link it in, |
| 2238 | // and return it. |
| 2239 | if (isa<ArrayType>(Ty)) |
| 2240 | return *Entry = new ConstantDataArray(Ty, Slot.getKeyData()); |
| 2241 | |
| 2242 | assert(isa<VectorType>(Ty)); |
| 2243 | return *Entry = new ConstantDataVector(Ty, Slot.getKeyData()); |
| 2244 | } |
| 2245 | |
| 2246 | void ConstantDataSequential::destroyConstant() { |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2247 | // Remove the constant from the StringMap. |
| 2248 | StringMap<ConstantDataSequential*> &CDSConstants = |
| 2249 | getType()->getContext().pImpl->CDSConstants; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2250 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2251 | StringMap<ConstantDataSequential*>::iterator Slot = |
Chris Lattner | 9e631da | 2012-01-24 09:31:43 +0000 | [diff] [blame] | 2252 | CDSConstants.find(getRawDataValues()); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2253 | |
| 2254 | assert(Slot != CDSConstants.end() && "CDS not found in uniquing table"); |
| 2255 | |
| 2256 | ConstantDataSequential **Entry = &Slot->getValue(); |
| 2257 | |
| 2258 | // Remove the entry from the hash table. |
| 2259 | if ((*Entry)->Next == 0) { |
| 2260 | // If there is only one value in the bucket (common case) it must be this |
| 2261 | // entry, and removing the entry should remove the bucket completely. |
| 2262 | assert((*Entry) == this && "Hash mismatch in ConstantDataSequential"); |
| 2263 | getContext().pImpl->CDSConstants.erase(Slot); |
| 2264 | } else { |
| 2265 | // Otherwise, there are multiple entries linked off the bucket, unlink the |
| 2266 | // node we care about but keep the bucket around. |
| 2267 | for (ConstantDataSequential *Node = *Entry; ; |
| 2268 | Entry = &Node->Next, Node = *Entry) { |
| 2269 | assert(Node && "Didn't find entry in its uniquing hash table!"); |
| 2270 | // If we found our entry, unlink it from the list and we're done. |
| 2271 | if (Node == this) { |
| 2272 | *Entry = Node->Next; |
| 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2278 | // If we were part of a list, make sure that we don't delete the list that is |
| 2279 | // still owned by the uniquing map. |
| 2280 | Next = 0; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2281 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2282 | // Finally, actually delete it. |
| 2283 | destroyConstantImpl(); |
| 2284 | } |
| 2285 | |
| 2286 | /// get() constructors - Return a constant with array type with an element |
| 2287 | /// count and element type matching the ArrayRef passed in. Note that this |
| 2288 | /// can return a ConstantAggregateZero object. |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2289 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) { |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2290 | Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2291 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2292 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2293 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2294 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2295 | Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2296 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2297 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2298 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2299 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2300 | Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2301 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2302 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2303 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2304 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2305 | Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2306 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2307 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2308 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2309 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) { |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2310 | Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2311 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2312 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2313 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2314 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) { |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2315 | Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2316 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2317 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2320 | /// getString - This method constructs a CDS and initializes it with a text |
| 2321 | /// string. The default behavior (AddNull==true) causes a null terminator to |
| 2322 | /// be placed at the end of the array (increasing the length of the string by |
| 2323 | /// one more than the StringRef would normally indicate. Pass AddNull=false |
| 2324 | /// to disable this behavior. |
| 2325 | Constant *ConstantDataArray::getString(LLVMContext &Context, |
| 2326 | StringRef Str, bool AddNull) { |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2327 | if (!AddNull) { |
| 2328 | const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data()); |
| 2329 | return get(Context, ArrayRef<uint8_t>(const_cast<uint8_t *>(Data), |
| 2330 | Str.size())); |
| 2331 | } |
| 2332 | |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2333 | SmallVector<uint8_t, 64> ElementVals; |
| 2334 | ElementVals.append(Str.begin(), Str.end()); |
| 2335 | ElementVals.push_back(0); |
| 2336 | return get(Context, ElementVals); |
| 2337 | } |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2338 | |
| 2339 | /// get() constructors - Return a constant with vector type with an element |
| 2340 | /// count and element type matching the ArrayRef passed in. Note that this |
| 2341 | /// can return a ConstantAggregateZero object. |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2342 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2343 | Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2344 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2345 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2346 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2347 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2348 | Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2349 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2350 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2351 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2352 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2353 | Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2354 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2355 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2356 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2357 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2358 | Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2359 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2360 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2361 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2362 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) { |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2363 | Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2364 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2365 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2366 | } |
Chris Lattner | 3210060 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2367 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) { |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2368 | Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size()); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2369 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2370 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty); |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 2373 | Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { |
| 2374 | assert(isElementTypeCompatible(V->getType()) && |
| 2375 | "Element type not compatible with ConstantData"); |
| 2376 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 2377 | if (CI->getType()->isIntegerTy(8)) { |
| 2378 | SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2379 | return get(V->getContext(), Elts); |
| 2380 | } |
| 2381 | if (CI->getType()->isIntegerTy(16)) { |
| 2382 | SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2383 | return get(V->getContext(), Elts); |
| 2384 | } |
| 2385 | if (CI->getType()->isIntegerTy(32)) { |
| 2386 | SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2387 | return get(V->getContext(), Elts); |
| 2388 | } |
| 2389 | assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type"); |
| 2390 | SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2391 | return get(V->getContext(), Elts); |
| 2392 | } |
| 2393 | |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2394 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 2395 | if (CFP->getType()->isFloatTy()) { |
| 2396 | SmallVector<float, 16> Elts(NumElts, CFP->getValueAPF().convertToFloat()); |
| 2397 | return get(V->getContext(), Elts); |
| 2398 | } |
| 2399 | if (CFP->getType()->isDoubleTy()) { |
| 2400 | SmallVector<double, 16> Elts(NumElts, |
| 2401 | CFP->getValueAPF().convertToDouble()); |
| 2402 | return get(V->getContext(), Elts); |
| 2403 | } |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 2404 | } |
Chris Lattner | 36c744f | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2405 | return ConstantVector::getSplat(NumElts, V); |
Chris Lattner | 3c2c954 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2409 | /// getElementAsInteger - If this is a sequential container of integers (of |
| 2410 | /// any size), return the specified element in the low bits of a uint64_t. |
| 2411 | uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { |
| 2412 | assert(isa<IntegerType>(getElementType()) && |
| 2413 | "Accessor can only be used when element is an integer"); |
| 2414 | const char *EltPtr = getElementPointer(Elt); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2415 | |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2416 | // The data is stored in host byte order, make sure to cast back to the right |
| 2417 | // type to load with the right endianness. |
Chris Lattner | 230cdab | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2418 | switch (getElementType()->getIntegerBitWidth()) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2419 | default: llvm_unreachable("Invalid bitwidth for CDS"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2420 | case 8: |
| 2421 | return *const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(EltPtr)); |
| 2422 | case 16: |
| 2423 | return *const_cast<uint16_t *>(reinterpret_cast<const uint16_t *>(EltPtr)); |
| 2424 | case 32: |
| 2425 | return *const_cast<uint32_t *>(reinterpret_cast<const uint32_t *>(EltPtr)); |
| 2426 | case 64: |
| 2427 | return *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(EltPtr)); |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | /// getElementAsAPFloat - If this is a sequential container of floating point |
| 2432 | /// type, return the specified element as an APFloat. |
| 2433 | APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { |
| 2434 | const char *EltPtr = getElementPointer(Elt); |
| 2435 | |
| 2436 | switch (getElementType()->getTypeID()) { |
Nick Lewycky | 1486ae6 | 2012-01-25 03:20:12 +0000 | [diff] [blame] | 2437 | default: |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2438 | llvm_unreachable("Accessor can only be used when element is float/double!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2439 | case Type::FloatTyID: { |
| 2440 | const float *FloatPrt = reinterpret_cast<const float *>(EltPtr); |
| 2441 | return APFloat(*const_cast<float *>(FloatPrt)); |
| 2442 | } |
| 2443 | case Type::DoubleTyID: { |
| 2444 | const double *DoublePtr = reinterpret_cast<const double *>(EltPtr); |
| 2445 | return APFloat(*const_cast<double *>(DoublePtr)); |
| 2446 | } |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | /// getElementAsFloat - If this is an sequential container of floats, return |
| 2451 | /// the specified element as a float. |
| 2452 | float ConstantDataSequential::getElementAsFloat(unsigned Elt) const { |
| 2453 | assert(getElementType()->isFloatTy() && |
| 2454 | "Accessor can only be used when element is a 'float'"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2455 | const float *EltPtr = reinterpret_cast<const float *>(getElementPointer(Elt)); |
| 2456 | return *const_cast<float *>(EltPtr); |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | /// getElementAsDouble - If this is an sequential container of doubles, return |
| 2460 | /// the specified element as a float. |
| 2461 | double ConstantDataSequential::getElementAsDouble(unsigned Elt) const { |
| 2462 | assert(getElementType()->isDoubleTy() && |
| 2463 | "Accessor can only be used when element is a 'float'"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2464 | const double *EltPtr = |
| 2465 | reinterpret_cast<const double *>(getElementPointer(Elt)); |
| 2466 | return *const_cast<double *>(EltPtr); |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | /// getElementAsConstant - Return a Constant for a specified index's element. |
| 2470 | /// Note that this has to compute a new constant to return, so it isn't as |
| 2471 | /// efficient as getElementAsInteger/Float/Double. |
| 2472 | Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const { |
| 2473 | if (getElementType()->isFloatTy() || getElementType()->isDoubleTy()) |
| 2474 | return ConstantFP::get(getContext(), getElementAsAPFloat(Elt)); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2475 | |
Chris Lattner | 45bb5c5 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2476 | return ConstantInt::get(getElementType(), getElementAsInteger(Elt)); |
| 2477 | } |
| 2478 | |
Chris Lattner | 6233907 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2479 | /// isString - This method returns true if this is an array of i8. |
| 2480 | bool ConstantDataSequential::isString() const { |
| 2481 | return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(8); |
| 2482 | } |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2483 | |
Chris Lattner | 6233907 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2484 | /// isCString - This method returns true if the array "isString", ends with a |
| 2485 | /// nul byte, and does not contains any other nul bytes. |
| 2486 | bool ConstantDataSequential::isCString() const { |
| 2487 | if (!isString()) |
| 2488 | return false; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2489 | |
Chris Lattner | 6233907 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2490 | StringRef Str = getAsString(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | 6233907 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2492 | // The last value must be nul. |
| 2493 | if (Str.back() != 0) return false; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2494 | |
Chris Lattner | 6233907 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2495 | // Other elements must be non-nul. |
| 2496 | return Str.drop_back().find(0) == StringRef::npos; |
| 2497 | } |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2498 | |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2499 | /// getSplatValue - If this is a splat constant, meaning that all of the |
| 2500 | /// elements have the same value, return that value. Otherwise return NULL. |
| 2501 | Constant *ConstantDataVector::getSplatValue() const { |
| 2502 | const char *Base = getRawDataValues().data(); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2503 | |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2504 | // Compare elements 1+ to the 0'th element. |
| 2505 | unsigned EltSize = getElementByteSize(); |
| 2506 | for (unsigned i = 1, e = getNumElements(); i != e; ++i) |
| 2507 | if (memcmp(Base, Base+i*EltSize, EltSize)) |
| 2508 | return 0; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2509 | |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2510 | // If they're all the same, return the 0th one as a representative. |
| 2511 | return getElementAsConstant(0); |
| 2512 | } |
Chris Lattner | 04e3b1e | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2514 | //===----------------------------------------------------------------------===// |
| 2515 | // replaceUsesOfWithOnConstant implementations |
| 2516 | |
Chris Lattner | 5498405 | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2517 | /// replaceUsesOfWithOnConstant - Update this constant array to change uses of |
| 2518 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 2519 | /// etc. |
| 2520 | /// |
| 2521 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 2522 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
| 2523 | /// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that |
| 2524 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 2525 | /// work, but would be really slow because it would have to unique each updated |
| 2526 | /// array instance. |
Chris Lattner | 2ee11ec | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 2527 | /// |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2528 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2529 | Use *U) { |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2530 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2531 | Constant *ToC = cast<Constant>(To); |
| 2532 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2533 | LLVMContextImpl *pImpl = getType()->getContext().pImpl; |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2534 | |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2535 | SmallVector<Constant*, 8> Values; |
| 2536 | LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup; |
| 2537 | Lookup.first = cast<ArrayType>(getType()); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2538 | Values.reserve(getNumOperands()); // Build replacement array. |
| 2539 | |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2540 | // Fill values with the modified operands of the constant array. Also, |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2541 | // compute whether this turns into an all-zeros array. |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2542 | unsigned NumUpdated = 0; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2543 | |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2544 | // Keep track of whether all the values in the array are "ToC". |
| 2545 | bool AllSame = true; |
| 2546 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2547 | Constant *Val = cast<Constant>(O->get()); |
| 2548 | if (Val == From) { |
| 2549 | Val = ToC; |
| 2550 | ++NumUpdated; |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2551 | } |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2552 | Values.push_back(Val); |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2553 | AllSame &= Val == ToC; |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2554 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2555 | |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2556 | Constant *Replacement = 0; |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2557 | if (AllSame && ToC->isNullValue()) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2558 | Replacement = ConstantAggregateZero::get(getType()); |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2559 | } else if (AllSame && isa<UndefValue>(ToC)) { |
| 2560 | Replacement = UndefValue::get(getType()); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2561 | } else { |
| 2562 | // Check to see if we have this array type already. |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2563 | Lookup.second = makeArrayRef(Values); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2564 | LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I = |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2565 | pImpl->ArrayConstants.find(Lookup); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2566 | |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2567 | if (I != pImpl->ArrayConstants.map_end()) { |
| 2568 | Replacement = I->first; |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2569 | } else { |
| 2570 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2571 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 2572 | // old with the new, then deleting the old... just update the current one |
| 2573 | // in place! |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2574 | pImpl->ArrayConstants.remove(this); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2575 | |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2576 | // Update to the new value. Optimize for the case when we have a single |
| 2577 | // operand that we're changing, but handle bulk updates efficiently. |
| 2578 | if (NumUpdated == 1) { |
| 2579 | unsigned OperandToUpdate = U - OperandList; |
| 2580 | assert(getOperand(OperandToUpdate) == From && |
| 2581 | "ReplaceAllUsesWith broken!"); |
| 2582 | setOperand(OperandToUpdate, ToC); |
| 2583 | } else { |
| 2584 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 2585 | if (getOperand(i) == From) |
| 2586 | setOperand(i, ToC); |
| 2587 | } |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2588 | pImpl->ArrayConstants.insert(this); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2589 | return; |
| 2590 | } |
| 2591 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | cea141f | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2593 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2594 | assert(Replacement != this && "I didn't contain From!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2596 | // Everyone using this now uses the replacement. |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 2597 | replaceAllUsesWith(Replacement); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2599 | // Delete the old constant! |
| 2600 | destroyConstant(); |
| 2601 | } |
| 2602 | |
| 2603 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2604 | Use *U) { |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2605 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2606 | Constant *ToC = cast<Constant>(To); |
| 2607 | |
| 2608 | unsigned OperandToUpdate = U-OperandList; |
| 2609 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 2610 | |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2611 | SmallVector<Constant*, 8> Values; |
| 2612 | LLVMContextImpl::StructConstantsTy::LookupKey Lookup; |
| 2613 | Lookup.first = cast<StructType>(getType()); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2614 | Values.reserve(getNumOperands()); // Build replacement struct. |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2615 | |
| 2616 | // Fill values with the modified operands of the constant struct. Also, |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2617 | // compute whether this turns into an all-zeros struct. |
| 2618 | bool isAllZeros = false; |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2619 | bool isAllUndef = false; |
| 2620 | if (ToC->isNullValue()) { |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2621 | isAllZeros = true; |
| 2622 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2623 | Constant *Val = cast<Constant>(O->get()); |
| 2624 | Values.push_back(Val); |
| 2625 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 2626 | } |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2627 | } else if (isa<UndefValue>(ToC)) { |
| 2628 | isAllUndef = true; |
| 2629 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2630 | Constant *Val = cast<Constant>(O->get()); |
| 2631 | Values.push_back(Val); |
| 2632 | if (isAllUndef) isAllUndef = isa<UndefValue>(Val); |
| 2633 | } |
| 2634 | } else { |
| 2635 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) |
| 2636 | Values.push_back(cast<Constant>(O->get())); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2637 | } |
| 2638 | Values[OperandToUpdate] = ToC; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2640 | LLVMContextImpl *pImpl = getContext().pImpl; |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2641 | |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2642 | Constant *Replacement = 0; |
| 2643 | if (isAllZeros) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2644 | Replacement = ConstantAggregateZero::get(getType()); |
Chris Lattner | e150e2d | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2645 | } else if (isAllUndef) { |
| 2646 | Replacement = UndefValue::get(getType()); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2647 | } else { |
Chris Lattner | 93604b6 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2648 | // Check to see if we have this struct type already. |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2649 | Lookup.second = makeArrayRef(Values); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2650 | LLVMContextImpl::StructConstantsTy::MapTy::iterator I = |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2651 | pImpl->StructConstants.find(Lookup); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2652 | |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2653 | if (I != pImpl->StructConstants.map_end()) { |
| 2654 | Replacement = I->first; |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2655 | } else { |
| 2656 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2657 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 2658 | // old with the new, then deleting the old... just update the current one |
| 2659 | // in place! |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2660 | pImpl->StructConstants.remove(this); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2661 | |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2662 | // Update to the new value. |
| 2663 | setOperand(OperandToUpdate, ToC); |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2664 | pImpl->StructConstants.insert(this); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2665 | return; |
| 2666 | } |
| 2667 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2668 | |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2669 | assert(Replacement != this && "I didn't contain From!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2671 | // Everyone using this now uses the replacement. |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 2672 | replaceAllUsesWith(Replacement); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2674 | // Delete the old constant! |
| 2675 | destroyConstant(); |
| 2676 | } |
| 2677 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2678 | void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2679 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2680 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2681 | |
Chris Lattner | a7c6988 | 2012-01-26 20:40:56 +0000 | [diff] [blame] | 2682 | SmallVector<Constant*, 8> Values; |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2683 | Values.reserve(getNumOperands()); // Build replacement array... |
| 2684 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2685 | Constant *Val = getOperand(i); |
| 2686 | if (Val == From) Val = cast<Constant>(To); |
| 2687 | Values.push_back(Val); |
| 2688 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2689 | |
Jay Foad | a0c1384 | 2011-06-22 09:10:19 +0000 | [diff] [blame] | 2690 | Constant *Replacement = get(Values); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2691 | assert(Replacement != this && "I didn't contain From!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2693 | // Everyone using this now uses the replacement. |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 2694 | replaceAllUsesWith(Replacement); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2695 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2696 | // Delete the old constant! |
| 2697 | destroyConstant(); |
| 2698 | } |
| 2699 | |
| 2700 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2701 | Use *U) { |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2702 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 2703 | Constant *To = cast<Constant>(ToV); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 1a8def6 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 2705 | SmallVector<Constant*, 8> NewOps; |
| 2706 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2707 | Constant *Op = getOperand(i); |
| 2708 | NewOps.push_back(Op == From ? To : Op); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2709 | } |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | 1a8def6 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 2711 | Constant *Replacement = getWithOperands(NewOps); |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2712 | assert(Replacement != this && "I didn't contain From!"); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2713 | |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2714 | // Everyone using this now uses the replacement. |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 2715 | replaceAllUsesWith(Replacement); |
Galina Kistanova | a46517e | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | 5cbade9 | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2717 | // Delete the old constant! |
| 2718 | destroyConstant(); |
Matthijs Kooijman | 10b9de6 | 2008-07-03 07:46:41 +0000 | [diff] [blame] | 2719 | } |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 2720 | |
| 2721 | Instruction *ConstantExpr::getAsInstruction() { |
| 2722 | SmallVector<Value*,4> ValueOperands; |
| 2723 | for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) |
| 2724 | ValueOperands.push_back(cast<Value>(I)); |
| 2725 | |
| 2726 | ArrayRef<Value*> Ops(ValueOperands); |
| 2727 | |
| 2728 | switch (getOpcode()) { |
| 2729 | case Instruction::Trunc: |
| 2730 | case Instruction::ZExt: |
| 2731 | case Instruction::SExt: |
| 2732 | case Instruction::FPTrunc: |
| 2733 | case Instruction::FPExt: |
| 2734 | case Instruction::UIToFP: |
| 2735 | case Instruction::SIToFP: |
| 2736 | case Instruction::FPToUI: |
| 2737 | case Instruction::FPToSI: |
| 2738 | case Instruction::PtrToInt: |
| 2739 | case Instruction::IntToPtr: |
| 2740 | case Instruction::BitCast: |
| 2741 | return CastInst::Create((Instruction::CastOps)getOpcode(), |
| 2742 | Ops[0], getType()); |
| 2743 | case Instruction::Select: |
| 2744 | return SelectInst::Create(Ops[0], Ops[1], Ops[2]); |
| 2745 | case Instruction::InsertElement: |
| 2746 | return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]); |
| 2747 | case Instruction::ExtractElement: |
| 2748 | return ExtractElementInst::Create(Ops[0], Ops[1]); |
| 2749 | case Instruction::InsertValue: |
| 2750 | return InsertValueInst::Create(Ops[0], Ops[1], getIndices()); |
| 2751 | case Instruction::ExtractValue: |
| 2752 | return ExtractValueInst::Create(Ops[0], getIndices()); |
| 2753 | case Instruction::ShuffleVector: |
| 2754 | return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]); |
| 2755 | |
| 2756 | case Instruction::GetElementPtr: |
| 2757 | if (cast<GEPOperator>(this)->isInBounds()) |
| 2758 | return GetElementPtrInst::CreateInBounds(Ops[0], Ops.slice(1)); |
| 2759 | else |
| 2760 | return GetElementPtrInst::Create(Ops[0], Ops.slice(1)); |
| 2761 | |
| 2762 | case Instruction::ICmp: |
| 2763 | case Instruction::FCmp: |
| 2764 | return CmpInst::Create((Instruction::OtherOps)getOpcode(), |
| 2765 | getPredicate(), Ops[0], Ops[1]); |
| 2766 | |
| 2767 | default: |
| 2768 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 2769 | BinaryOperator *BO = |
| 2770 | BinaryOperator::Create((Instruction::BinaryOps)getOpcode(), |
| 2771 | Ops[0], Ops[1]); |
| 2772 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 2773 | BO->setHasNoUnsignedWrap(SubclassOptionalData & |
| 2774 | OverflowingBinaryOperator::NoUnsignedWrap); |
| 2775 | BO->setHasNoSignedWrap(SubclassOptionalData & |
| 2776 | OverflowingBinaryOperator::NoSignedWrap); |
| 2777 | } |
| 2778 | if (isa<PossiblyExactOperator>(BO)) |
| 2779 | BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact); |
| 2780 | return BO; |
| 2781 | } |
| 2782 | } |