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