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