Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | e17322b | 2011-02-07 20:03:14 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chris Lattner | 33e93b8 | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 16 | #include "ConstantFold.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalValue.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Dan Gohman | 7d82e13 | 2009-07-18 01:49:22 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/FoldingSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 31 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 35 | #include <map> |
Talin | 3a0a30d | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 36 | #include <cstdarg> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 40 | // Constant Class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 43 | // Constructor to create a '0' constant of arbitrary type... |
Chris Lattner | 74eb5d7 | 2009-10-30 22:33:29 +0000 | [diff] [blame] | 44 | Constant *Constant::getNullValue(const Type *Ty) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 45 | switch (Ty->getTypeID()) { |
| 46 | case Type::IntegerTyID: |
| 47 | return ConstantInt::get(Ty, 0); |
| 48 | case Type::FloatTyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 49 | return ConstantFP::get(Ty->getContext(), |
| 50 | APFloat::getZero(APFloat::IEEEsingle)); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 51 | case Type::DoubleTyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 52 | return ConstantFP::get(Ty->getContext(), |
| 53 | APFloat::getZero(APFloat::IEEEdouble)); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 54 | case Type::X86_FP80TyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 55 | return ConstantFP::get(Ty->getContext(), |
| 56 | APFloat::getZero(APFloat::x87DoubleExtended)); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 57 | case Type::FP128TyID: |
| 58 | return ConstantFP::get(Ty->getContext(), |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 59 | APFloat::getZero(APFloat::IEEEquad)); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 60 | case Type::PPC_FP128TyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 61 | return ConstantFP::get(Ty->getContext(), |
Benjamin Kramer | 6f88fcb | 2010-12-04 14:43:08 +0000 | [diff] [blame] | 62 | APFloat(APInt::getNullValue(128))); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 63 | case Type::PointerTyID: |
| 64 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 65 | case Type::StructTyID: |
| 66 | case Type::ArrayTyID: |
| 67 | case Type::VectorTyID: |
| 68 | return ConstantAggregateZero::get(Ty); |
| 69 | default: |
| 70 | // Function, Label, or Opaque type? |
| 71 | assert(!"Cannot create a null constant of that type!"); |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 76 | Constant *Constant::getIntegerValue(const Type *Ty, const APInt &V) { |
Dan Gohman | f011f5a | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 77 | const Type *ScalarTy = Ty->getScalarType(); |
| 78 | |
| 79 | // Create the base integer constant. |
| 80 | Constant *C = ConstantInt::get(Ty->getContext(), V); |
| 81 | |
| 82 | // Convert an integer to a pointer, if necessary. |
| 83 | if (const PointerType *PTy = dyn_cast<PointerType>(ScalarTy)) |
| 84 | C = ConstantExpr::getIntToPtr(C, PTy); |
| 85 | |
| 86 | // Broadcast a scalar to a vector, if necessary. |
| 87 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 88 | C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); |
| 89 | |
| 90 | return C; |
| 91 | } |
| 92 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 93 | Constant *Constant::getAllOnesValue(const Type *Ty) { |
Chris Lattner | 74eb5d7 | 2009-10-30 22:33:29 +0000 | [diff] [blame] | 94 | if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 95 | return ConstantInt::get(Ty->getContext(), |
| 96 | APInt::getAllOnesValue(ITy->getBitWidth())); |
Nadav Rotem | 7cc6d12 | 2011-02-17 21:22:27 +0000 | [diff] [blame] | 97 | |
| 98 | if (Ty->isFloatingPointTy()) { |
| 99 | APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(), |
| 100 | !Ty->isPPC_FP128Ty()); |
| 101 | return ConstantFP::get(Ty->getContext(), FL); |
| 102 | } |
| 103 | |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 104 | SmallVector<Constant*, 16> Elts; |
Chris Lattner | 74eb5d7 | 2009-10-30 22:33:29 +0000 | [diff] [blame] | 105 | const VectorType *VTy = cast<VectorType>(Ty); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 106 | Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); |
| 107 | assert(Elts[0] && "Not a vector integer type!"); |
| 108 | return cast<ConstantVector>(ConstantVector::get(Elts)); |
| 109 | } |
| 110 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 111 | void Constant::destroyConstantImpl() { |
| 112 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 113 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 114 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 115 | // but they don't know that. Because we only find out when the CPV is |
| 116 | // deleted, we must now notify all of our users (that should only be |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 117 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 118 | // |
| 119 | while (!use_empty()) { |
| 120 | Value *V = use_back(); |
| 121 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 122 | if (!isa<Constant>(V)) { |
David Greene | 1e27a13 | 2010-01-05 01:29:19 +0000 | [diff] [blame] | 123 | dbgs() << "While deleting: " << *this |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 124 | << "\n\nUse still stuck around after Def is destroyed: " |
| 125 | << *V << "\n\n"; |
| 126 | } |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 127 | #endif |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 128 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 129 | Constant *CV = cast<Constant>(V); |
| 130 | CV->destroyConstant(); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 131 | |
| 132 | // The constant should remove itself from our use list... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 133 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Value has no outstanding references it is safe to delete it now... |
| 137 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 138 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 140 | /// canTrap - Return true if evaluation of this constant could trap. This is |
| 141 | /// true for things like constant expressions that could divide by zero. |
| 142 | bool Constant::canTrap() const { |
| 143 | assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); |
| 144 | // The only thing that could possibly trap are constant exprs. |
| 145 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(this); |
| 146 | if (!CE) return false; |
| 147 | |
| 148 | // ConstantExpr traps if any operands can trap. |
| 149 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 150 | if (CE->getOperand(i)->canTrap()) |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 151 | return true; |
| 152 | |
| 153 | // Otherwise, only specific operations can trap. |
| 154 | switch (CE->getOpcode()) { |
| 155 | default: |
| 156 | return false; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 157 | case Instruction::UDiv: |
| 158 | case Instruction::SDiv: |
| 159 | case Instruction::FDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 160 | case Instruction::URem: |
| 161 | case Instruction::SRem: |
| 162 | case Instruction::FRem: |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 163 | // Div and rem can trap if the RHS is not known to be non-zero. |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 164 | if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue()) |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 165 | return true; |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
Chris Lattner | 253bc77 | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 170 | /// isConstantUsed - Return true if the constant has users other than constant |
| 171 | /// exprs and other dangling things. |
| 172 | bool Constant::isConstantUsed() const { |
Gabor Greif | c78d720 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 173 | for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) { |
Chris Lattner | 253bc77 | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 174 | const Constant *UC = dyn_cast<Constant>(*UI); |
| 175 | if (UC == 0 || isa<GlobalValue>(UC)) |
| 176 | return true; |
| 177 | |
| 178 | if (UC->isConstantUsed()) |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 185 | |
| 186 | /// getRelocationInfo - This method classifies the entry according to |
| 187 | /// whether or not it may generate a relocation entry. This must be |
| 188 | /// conservative, so if it might codegen to a relocatable entry, it should say |
| 189 | /// so. The return values are: |
| 190 | /// |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 191 | /// NoRelocation: This constant pool entry is guaranteed to never have a |
| 192 | /// relocation applied to it (because it holds a simple constant like |
| 193 | /// '4'). |
| 194 | /// LocalRelocation: This entry has relocations, but the entries are |
| 195 | /// guaranteed to be resolvable by the static linker, so the dynamic |
| 196 | /// linker will never see them. |
| 197 | /// GlobalRelocations: This entry may have arbitrary relocations. |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 198 | /// |
| 199 | /// FIXME: This really should not be in VMCore. |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 200 | Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { |
| 201 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 202 | if (GV->hasLocalLinkage() || GV->hasHiddenVisibility()) |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 203 | return LocalRelocation; // Local to this file/library. |
| 204 | return GlobalRelocations; // Global reference. |
Anton Korobeynikov | 7437b59 | 2009-03-29 17:13:18 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 2cb85b4 | 2009-10-28 04:12:16 +0000 | [diff] [blame] | 207 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(this)) |
| 208 | return BA->getFunction()->getRelocationInfo(); |
| 209 | |
Chris Lattner | a7cfc43 | 2010-01-03 18:09:40 +0000 | [diff] [blame] | 210 | // While raw uses of blockaddress need to be relocated, differences between |
| 211 | // two of them don't when they are for labels in the same function. This is a |
| 212 | // common idiom when creating a table for the indirect goto extension, so we |
| 213 | // handle it efficiently here. |
| 214 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) |
| 215 | if (CE->getOpcode() == Instruction::Sub) { |
| 216 | ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0)); |
| 217 | ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1)); |
| 218 | if (LHS && RHS && |
| 219 | LHS->getOpcode() == Instruction::PtrToInt && |
| 220 | RHS->getOpcode() == Instruction::PtrToInt && |
| 221 | isa<BlockAddress>(LHS->getOperand(0)) && |
| 222 | isa<BlockAddress>(RHS->getOperand(0)) && |
| 223 | cast<BlockAddress>(LHS->getOperand(0))->getFunction() == |
| 224 | cast<BlockAddress>(RHS->getOperand(0))->getFunction()) |
| 225 | return NoRelocation; |
| 226 | } |
| 227 | |
Chris Lattner | 5cd4dd3 | 2009-07-24 03:27:21 +0000 | [diff] [blame] | 228 | PossibleRelocationsTy Result = NoRelocation; |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 229 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 230 | Result = std::max(Result, |
| 231 | cast<Constant>(getOperand(i))->getRelocationInfo()); |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 232 | |
| 233 | return Result; |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 237 | /// getVectorElements - This method, which is only valid on constant of vector |
| 238 | /// type, returns the elements of the vector in the specified smallvector. |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 239 | /// This handles breaking down a vector undef into undef elements, etc. For |
| 240 | /// constant exprs and other cases we can't handle, we return an empty vector. |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 241 | void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 242 | assert(getType()->isVectorTy() && "Not a vector constant!"); |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 243 | |
| 244 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) { |
| 245 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) |
| 246 | Elts.push_back(CV->getOperand(i)); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | const VectorType *VT = cast<VectorType>(getType()); |
| 251 | if (isa<ConstantAggregateZero>(this)) { |
| 252 | Elts.assign(VT->getNumElements(), |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 253 | Constant::getNullValue(VT->getElementType())); |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 257 | if (isa<UndefValue>(this)) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 258 | Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType())); |
Chris Lattner | c5098a2 | 2008-07-14 05:10:41 +0000 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
| 262 | // Unknown type, must be constant expr etc. |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 266 | /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove |
| 267 | /// it. This involves recursively eliminating any dead users of the |
| 268 | /// constantexpr. |
| 269 | static bool removeDeadUsersOfConstant(const Constant *C) { |
| 270 | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
| 271 | |
| 272 | while (!C->use_empty()) { |
| 273 | const Constant *User = dyn_cast<Constant>(C->use_back()); |
| 274 | if (!User) return false; // Non-constant usage; |
| 275 | if (!removeDeadUsersOfConstant(User)) |
| 276 | return false; // Constant wasn't dead |
| 277 | } |
| 278 | |
| 279 | const_cast<Constant*>(C)->destroyConstant(); |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /// removeDeadConstantUsers - If there are any dead constant users dangling |
| 285 | /// off of this constant, remove them. This method is useful for clients |
| 286 | /// that want to check to see if a global is unused, but don't want to deal |
| 287 | /// with potentially dead constants hanging off of the globals. |
| 288 | void Constant::removeDeadConstantUsers() const { |
| 289 | Value::const_use_iterator I = use_begin(), E = use_end(); |
| 290 | Value::const_use_iterator LastNonDeadUser = E; |
| 291 | while (I != E) { |
| 292 | const Constant *User = dyn_cast<Constant>(*I); |
| 293 | if (User == 0) { |
| 294 | LastNonDeadUser = I; |
| 295 | ++I; |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | if (!removeDeadUsersOfConstant(User)) { |
| 300 | // If the constant wasn't dead, remember that this was the last live use |
| 301 | // and move on to the next constant. |
| 302 | LastNonDeadUser = I; |
| 303 | ++I; |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | // If the constant was dead, then the iterator is invalidated. |
| 308 | if (LastNonDeadUser == E) { |
| 309 | I = use_begin(); |
| 310 | if (I == E) break; |
| 311 | } else { |
| 312 | I = LastNonDeadUser; |
| 313 | ++I; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 320 | //===----------------------------------------------------------------------===// |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 321 | // ConstantInt |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 322 | //===----------------------------------------------------------------------===// |
| 323 | |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 324 | ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V) |
Chris Lattner | 5db2f47 | 2007-02-20 05:55:46 +0000 | [diff] [blame] | 325 | : Constant(Ty, ConstantIntVal, 0, 0), Val(V) { |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 326 | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 329 | ConstantInt* ConstantInt::getTrue(LLVMContext &Context) { |
| 330 | LLVMContextImpl *pImpl = Context.pImpl; |
Benjamin Kramer | ddd1b7b | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 331 | if (!pImpl->TheTrueVal) |
| 332 | pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1); |
| 333 | return pImpl->TheTrueVal; |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | ConstantInt* ConstantInt::getFalse(LLVMContext &Context) { |
| 337 | LLVMContextImpl *pImpl = Context.pImpl; |
Benjamin Kramer | ddd1b7b | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 338 | if (!pImpl->TheFalseVal) |
| 339 | pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0); |
| 340 | return pImpl->TheFalseVal; |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 344 | // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap |
| 345 | // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the |
| 346 | // operator== and operator!= to ensure that the DenseMap doesn't attempt to |
| 347 | // compare APInt's of different widths, which would violate an APInt class |
| 348 | // invariant which generates an assertion. |
| 349 | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) { |
| 350 | // Get the corresponding integer type for the bit width of the value. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 351 | const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 352 | // get an existing value or the insertion position |
| 353 | DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 354 | ConstantInt *&Slot = Context.pImpl->IntConstants[Key]; |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 355 | if (!Slot) Slot = new ConstantInt(ITy, V); |
| 356 | return Slot; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 359 | Constant *ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 360 | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), |
| 361 | V, isSigned); |
| 362 | |
| 363 | // For vectors, broadcast the value. |
| 364 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 365 | return ConstantVector::get(SmallVector<Constant*, |
| 366 | 16>(VTy->getNumElements(), C)); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 367 | |
| 368 | return C; |
| 369 | } |
| 370 | |
| 371 | ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V, |
| 372 | bool isSigned) { |
| 373 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); |
| 374 | } |
| 375 | |
| 376 | ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) { |
| 377 | return get(Ty, V, true); |
| 378 | } |
| 379 | |
| 380 | Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) { |
| 381 | return get(Ty, V, true); |
| 382 | } |
| 383 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 384 | Constant *ConstantInt::get(const Type* Ty, const APInt& V) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 385 | ConstantInt *C = get(Ty->getContext(), V); |
| 386 | assert(C->getType() == Ty->getScalarType() && |
| 387 | "ConstantInt type doesn't match the type implied by its value!"); |
| 388 | |
| 389 | // For vectors, broadcast the value. |
| 390 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 391 | return ConstantVector::get( |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 392 | SmallVector<Constant *, 16>(VTy->getNumElements(), C)); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 393 | |
| 394 | return C; |
| 395 | } |
| 396 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 397 | ConstantInt* ConstantInt::get(const IntegerType* Ty, StringRef Str, |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 398 | uint8_t radix) { |
| 399 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix)); |
| 400 | } |
| 401 | |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 402 | //===----------------------------------------------------------------------===// |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 403 | // ConstantFP |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 404 | //===----------------------------------------------------------------------===// |
| 405 | |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 406 | static const fltSemantics *TypeToFloatSemantics(const Type *Ty) { |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 407 | if (Ty->isFloatTy()) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 408 | return &APFloat::IEEEsingle; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 409 | if (Ty->isDoubleTy()) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 410 | return &APFloat::IEEEdouble; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 411 | if (Ty->isX86_FP80Ty()) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 412 | return &APFloat::x87DoubleExtended; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 413 | else if (Ty->isFP128Ty()) |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 414 | return &APFloat::IEEEquad; |
| 415 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 416 | assert(Ty->isPPC_FP128Ty() && "Unknown FP format"); |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 417 | return &APFloat::PPCDoubleDouble; |
| 418 | } |
| 419 | |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 420 | /// get() - This returns a constant fp for the specified value in the |
| 421 | /// specified type. This should only be used for simple constant values like |
| 422 | /// 2.0/1.0 etc, that are known-valid both as double and as the target format. |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 423 | Constant *ConstantFP::get(const Type* Ty, double V) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 424 | LLVMContext &Context = Ty->getContext(); |
| 425 | |
| 426 | APFloat FV(V); |
| 427 | bool ignored; |
| 428 | FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), |
| 429 | APFloat::rmNearestTiesToEven, &ignored); |
| 430 | Constant *C = get(Context, FV); |
| 431 | |
| 432 | // For vectors, broadcast the value. |
| 433 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 434 | return ConstantVector::get( |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 435 | SmallVector<Constant *, 16>(VTy->getNumElements(), C)); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 436 | |
| 437 | return C; |
| 438 | } |
| 439 | |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 440 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 441 | Constant *ConstantFP::get(const Type* Ty, StringRef Str) { |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 442 | LLVMContext &Context = Ty->getContext(); |
| 443 | |
| 444 | APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str); |
| 445 | Constant *C = get(Context, FV); |
| 446 | |
| 447 | // For vectors, broadcast the value. |
| 448 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 449 | return ConstantVector::get( |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 450 | SmallVector<Constant *, 16>(VTy->getNumElements(), C)); |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 451 | |
| 452 | return C; |
| 453 | } |
| 454 | |
| 455 | |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 456 | ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) { |
| 457 | LLVMContext &Context = Ty->getContext(); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 458 | APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF(); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 459 | apf.changeSign(); |
| 460 | return get(Context, apf); |
| 461 | } |
| 462 | |
| 463 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 464 | Constant *ConstantFP::getZeroValueForNegation(const Type* Ty) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 465 | if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 466 | if (PTy->getElementType()->isFloatingPointTy()) { |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 467 | SmallVector<Constant*, 16> zeros(PTy->getNumElements(), |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 468 | getNegativeZero(PTy->getElementType())); |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 469 | return ConstantVector::get(zeros); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 472 | if (Ty->isFloatingPointTy()) |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 473 | return getNegativeZero(Ty); |
| 474 | |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 475 | return Constant::getNullValue(Ty); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | |
| 479 | // ConstantFP accessors. |
| 480 | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
| 481 | DenseMapAPFloatKeyInfo::KeyTy Key(V); |
| 482 | |
| 483 | LLVMContextImpl* pImpl = Context.pImpl; |
| 484 | |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 485 | ConstantFP *&Slot = pImpl->FPConstants[Key]; |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 486 | |
| 487 | if (!Slot) { |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 488 | const Type *Ty; |
| 489 | if (&V.getSemantics() == &APFloat::IEEEsingle) |
| 490 | Ty = Type::getFloatTy(Context); |
| 491 | else if (&V.getSemantics() == &APFloat::IEEEdouble) |
| 492 | Ty = Type::getDoubleTy(Context); |
| 493 | else if (&V.getSemantics() == &APFloat::x87DoubleExtended) |
| 494 | Ty = Type::getX86_FP80Ty(Context); |
| 495 | else if (&V.getSemantics() == &APFloat::IEEEquad) |
| 496 | Ty = Type::getFP128Ty(Context); |
| 497 | else { |
| 498 | assert(&V.getSemantics() == &APFloat::PPCDoubleDouble && |
| 499 | "Unknown FP format"); |
| 500 | Ty = Type::getPPC_FP128Ty(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 501 | } |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 502 | Slot = new ConstantFP(Ty, V); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return Slot; |
| 506 | } |
| 507 | |
Dan Gohman | 394468d | 2009-09-25 23:40:21 +0000 | [diff] [blame] | 508 | ConstantFP *ConstantFP::getInfinity(const Type *Ty, bool Negative) { |
Dan Gohman | feb5021 | 2009-09-25 23:00:48 +0000 | [diff] [blame] | 509 | const fltSemantics &Semantics = *TypeToFloatSemantics(Ty); |
| 510 | return ConstantFP::get(Ty->getContext(), |
| 511 | APFloat::getInf(Semantics, Negative)); |
| 512 | } |
| 513 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 514 | ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) |
| 515 | : Constant(Ty, ConstantFPVal, 0, 0), Val(V) { |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 516 | assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && |
| 517 | "FP type Mismatch"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 520 | bool ConstantFP::isNullValue() const { |
Dale Johannesen | a719a60 | 2007-08-24 00:56:33 +0000 | [diff] [blame] | 521 | return Val.isZero() && !Val.isNegative(); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 524 | bool ConstantFP::isExactlyValue(const APFloat& V) const { |
| 525 | return Val.bitwiseIsEqual(V); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 528 | //===----------------------------------------------------------------------===// |
| 529 | // ConstantXXX Classes |
| 530 | //===----------------------------------------------------------------------===// |
| 531 | |
| 532 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 533 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 534 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 535 | : Constant(T, ConstantArrayVal, |
| 536 | OperandTraits<ConstantArray>::op_end(this) - V.size(), |
| 537 | V.size()) { |
Alkis Evlogimenos | 0507ffe | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 538 | assert(V.size() == T->getNumElements() && |
| 539 | "Invalid initializer vector for constant array"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 540 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 541 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 542 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 543 | Constant *C = *I; |
Nick Lewycky | ae4617c | 2009-10-03 19:30:43 +0000 | [diff] [blame] | 544 | assert(C->getType() == T->getElementType() && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 545 | "Initializer for array element doesn't match array element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 546 | *OL = C; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 550 | Constant *ConstantArray::get(const ArrayType *Ty, |
| 551 | const std::vector<Constant*> &V) { |
Jeffrey Yasskin | 8ce67f8 | 2009-09-30 21:08:08 +0000 | [diff] [blame] | 552 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 553 | assert(V[i]->getType() == Ty->getElementType() && |
| 554 | "Wrong type in array element initializer"); |
| 555 | } |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 556 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 557 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 558 | if (!V.empty()) { |
| 559 | Constant *C = V[0]; |
Chris Lattner | 09660c9 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 560 | if (!C->isNullValue()) |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 561 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
Chris Lattner | 09660c9 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 562 | |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 563 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
Chris Lattner | 09660c9 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 564 | if (V[i] != C) |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 565 | return pImpl->ArrayConstants.getOrCreate(Ty, V); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 568 | return ConstantAggregateZero::get(Ty); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 572 | Constant *ConstantArray::get(const ArrayType* T, Constant *const* Vals, |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 573 | unsigned NumVals) { |
| 574 | // FIXME: make this the primary ctor method. |
| 575 | return get(T, std::vector<Constant*>(Vals, Vals+NumVals)); |
| 576 | } |
| 577 | |
| 578 | /// ConstantArray::get(const string&) - Return an array that is initialized to |
| 579 | /// contain the specified string. If length is zero then a null terminator is |
| 580 | /// added to the specified string so that it may be used in a natural way. |
| 581 | /// Otherwise, the length parameter specifies how much of the string to use |
| 582 | /// and it won't be null terminated. |
| 583 | /// |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 584 | Constant *ConstantArray::get(LLVMContext &Context, StringRef Str, |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 585 | bool AddNull) { |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 586 | std::vector<Constant*> ElementVals; |
Benjamin Kramer | 3d4af4e | 2010-08-01 11:43:26 +0000 | [diff] [blame] | 587 | ElementVals.reserve(Str.size() + size_t(AddNull)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 588 | for (unsigned i = 0; i < Str.size(); ++i) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 589 | ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i])); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 590 | |
| 591 | // Add a null terminator to the string... |
| 592 | if (AddNull) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 593 | ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 596 | ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size()); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 597 | return get(ATy, ElementVals); |
| 598 | } |
| 599 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 600 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 601 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 602 | : Constant(T, ConstantStructVal, |
| 603 | OperandTraits<ConstantStruct>::op_end(this) - V.size(), |
| 604 | V.size()) { |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 605 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 606 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 607 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 608 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 609 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 610 | Constant *C = *I; |
Nick Lewycky | ae4617c | 2009-10-03 19:30:43 +0000 | [diff] [blame] | 611 | assert(C->getType() == T->getElementType(I-V.begin()) && |
Chris Lattner | 93c8f14 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 612 | "Initializer for struct element doesn't match struct element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 613 | *OL = C; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 617 | // ConstantStruct accessors. |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 618 | Constant *ConstantStruct::get(const StructType* T, |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 619 | const std::vector<Constant*>& V) { |
| 620 | LLVMContextImpl* pImpl = T->getContext().pImpl; |
| 621 | |
| 622 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 623 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 624 | if (!V[i]->isNullValue()) |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 625 | return pImpl->StructConstants.getOrCreate(T, V); |
| 626 | |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 627 | return ConstantAggregateZero::get(T); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 630 | Constant *ConstantStruct::get(LLVMContext &Context, |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 631 | const std::vector<Constant*>& V, bool packed) { |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 632 | std::vector<const Type*> StructEls; |
| 633 | StructEls.reserve(V.size()); |
| 634 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 635 | StructEls.push_back(V[i]->getType()); |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 636 | return get(StructType::get(Context, StructEls, packed), V); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 639 | Constant *ConstantStruct::get(LLVMContext &Context, |
| 640 | Constant *const *Vals, unsigned NumVals, |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 641 | bool Packed) { |
| 642 | // FIXME: make this the primary ctor method. |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 643 | return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 645 | |
Talin | 3a0a30d | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 646 | Constant* ConstantStruct::get(LLVMContext &Context, bool Packed, |
| 647 | Constant * Val, ...) { |
| 648 | va_list ap; |
| 649 | std::vector<Constant*> Values; |
| 650 | va_start(ap, Val); |
| 651 | while (Val) { |
| 652 | Values.push_back(Val); |
| 653 | Val = va_arg(ap, llvm::Constant*); |
| 654 | } |
Talin | de422be | 2011-03-01 18:00:49 +0000 | [diff] [blame^] | 655 | va_end(ap); |
Talin | 3a0a30d | 2011-02-28 23:53:27 +0000 | [diff] [blame] | 656 | return get(Context, Values, Packed); |
| 657 | } |
| 658 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 659 | ConstantVector::ConstantVector(const VectorType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 660 | const std::vector<Constant*> &V) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 661 | : Constant(T, ConstantVectorVal, |
| 662 | OperandTraits<ConstantVector>::op_end(this) - V.size(), |
| 663 | V.size()) { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 664 | Use *OL = OperandList; |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 665 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 666 | I != E; ++I, ++OL) { |
| 667 | Constant *C = *I; |
| 668 | assert(C->getType() == T->getElementType() && |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 669 | "Initializer for vector element doesn't match vector element type!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 670 | *OL = C; |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 674 | // ConstantVector accessors. |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 675 | Constant *ConstantVector::get(const VectorType *T, |
| 676 | const std::vector<Constant*> &V) { |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 677 | assert(!V.empty() && "Vectors can't be empty"); |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 678 | LLVMContextImpl *pImpl = T->getContext().pImpl; |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 680 | // If this is an all-undef or all-zero vector, return a |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 681 | // ConstantAggregateZero or UndefValue. |
| 682 | Constant *C = V[0]; |
| 683 | bool isZero = C->isNullValue(); |
| 684 | bool isUndef = isa<UndefValue>(C); |
| 685 | |
| 686 | if (isZero || isUndef) { |
| 687 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 688 | if (V[i] != C) { |
| 689 | isZero = isUndef = false; |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if (isZero) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 695 | return ConstantAggregateZero::get(T); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 696 | if (isUndef) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 697 | return UndefValue::get(T); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 698 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 699 | return pImpl->VectorConstants.getOrCreate(T, V); |
| 700 | } |
| 701 | |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 702 | Constant *ConstantVector::get(ArrayRef<Constant*> V) { |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 703 | // FIXME: make this the primary ctor method. |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 704 | assert(!V.empty() && "Vectors cannot be empty"); |
| 705 | return get(VectorType::get(V.front()->getType(), V.size()), V.vec()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 708 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 709 | // can't be inline because we don't want to #include Instruction.h into |
| 710 | // Constant.h |
| 711 | bool ConstantExpr::isCast() const { |
| 712 | return Instruction::isCast(getOpcode()); |
| 713 | } |
| 714 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 715 | bool ConstantExpr::isCompare() const { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 716 | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 719 | bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const { |
| 720 | if (getOpcode() != Instruction::GetElementPtr) return false; |
| 721 | |
| 722 | gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this); |
Oscar Fuentes | 40b31ad | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 723 | User::const_op_iterator OI = llvm::next(this->op_begin()); |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 724 | |
| 725 | // Skip the first index, as it has no static limit. |
| 726 | ++GEPI; |
| 727 | ++OI; |
| 728 | |
| 729 | // The remaining indices must be compile-time known integers within the |
| 730 | // bounds of the corresponding notional static array types. |
| 731 | for (; GEPI != E; ++GEPI, ++OI) { |
| 732 | ConstantInt *CI = dyn_cast<ConstantInt>(*OI); |
| 733 | if (!CI) return false; |
| 734 | if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI)) |
| 735 | if (CI->getValue().getActiveBits() > 64 || |
| 736 | CI->getZExtValue() >= ATy->getNumElements()) |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | // All the indices checked out. |
| 741 | return true; |
| 742 | } |
| 743 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 744 | bool ConstantExpr::hasIndices() const { |
| 745 | return getOpcode() == Instruction::ExtractValue || |
| 746 | getOpcode() == Instruction::InsertValue; |
| 747 | } |
| 748 | |
| 749 | const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const { |
| 750 | if (const ExtractValueConstantExpr *EVCE = |
| 751 | dyn_cast<ExtractValueConstantExpr>(this)) |
| 752 | return EVCE->Indices; |
Dan Gohman | a469bdb | 2008-06-23 16:39:44 +0000 | [diff] [blame] | 753 | |
| 754 | return cast<InsertValueConstantExpr>(this)->Indices; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 757 | unsigned ConstantExpr::getPredicate() const { |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 758 | assert(getOpcode() == Instruction::FCmp || |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 759 | getOpcode() == Instruction::ICmp); |
Chris Lattner | ef65009 | 2007-10-18 16:26:24 +0000 | [diff] [blame] | 760 | return ((const CompareConstantExpr*)this)->predicate; |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 761 | } |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 762 | |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 763 | /// getWithOperandReplaced - Return a constant expression identical to this |
| 764 | /// one, but with the specified operand set to the specified value. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 765 | Constant * |
| 766 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 767 | assert(OpNo < getNumOperands() && "Operand num is out of range!"); |
| 768 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 769 | "Replacing operand with value of different type!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 770 | if (getOperand(OpNo) == Op) |
| 771 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 773 | Constant *Op0, *Op1, *Op2; |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 774 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 775 | case Instruction::Trunc: |
| 776 | case Instruction::ZExt: |
| 777 | case Instruction::SExt: |
| 778 | case Instruction::FPTrunc: |
| 779 | case Instruction::FPExt: |
| 780 | case Instruction::UIToFP: |
| 781 | case Instruction::SIToFP: |
| 782 | case Instruction::FPToUI: |
| 783 | case Instruction::FPToSI: |
| 784 | case Instruction::PtrToInt: |
| 785 | case Instruction::IntToPtr: |
| 786 | case Instruction::BitCast: |
| 787 | return ConstantExpr::getCast(getOpcode(), Op, getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 788 | case Instruction::Select: |
| 789 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 790 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 791 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 792 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 793 | case Instruction::InsertElement: |
| 794 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 795 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 796 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 797 | return ConstantExpr::getInsertElement(Op0, Op1, Op2); |
| 798 | case Instruction::ExtractElement: |
| 799 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 800 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 801 | return ConstantExpr::getExtractElement(Op0, Op1); |
| 802 | case Instruction::ShuffleVector: |
| 803 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 804 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
| 805 | Op2 = (OpNo == 2) ? Op : getOperand(2); |
| 806 | return ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 807 | case Instruction::GetElementPtr: { |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 808 | SmallVector<Constant*, 8> Ops; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 809 | Ops.resize(getNumOperands()-1); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 810 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 811 | Ops[i-1] = getOperand(i); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 812 | if (OpNo == 0) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 813 | return cast<GEPOperator>(this)->isInBounds() ? |
| 814 | ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) : |
| 815 | ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size()); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 816 | Ops[OpNo-1] = Op; |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 817 | return cast<GEPOperator>(this)->isInBounds() ? |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 818 | ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()): |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 819 | ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size()); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 820 | } |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 821 | default: |
| 822 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 823 | Op0 = (OpNo == 0) ? Op : getOperand(0); |
| 824 | Op1 = (OpNo == 1) ? Op : getOperand(1); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 825 | return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | /// getWithOperands - This returns the current constant expression with the |
| 830 | /// operands replaced with the specified values. The specified operands must |
| 831 | /// match count and type with the existing ones. |
| 832 | Constant *ConstantExpr:: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 833 | getWithOperands(Constant *const *Ops, unsigned NumOps) const { |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 834 | assert(NumOps == getNumOperands() && "Operand count mismatch!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 835 | bool AnyChange = false; |
Chris Lattner | b078e28 | 2008-08-20 22:27:40 +0000 | [diff] [blame] | 836 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 837 | assert(Ops[i]->getType() == getOperand(i)->getType() && |
| 838 | "Operand type mismatch!"); |
| 839 | AnyChange |= Ops[i] != getOperand(i); |
| 840 | } |
| 841 | if (!AnyChange) // No operands changed, return self. |
| 842 | return const_cast<ConstantExpr*>(this); |
| 843 | |
| 844 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 845 | case Instruction::Trunc: |
| 846 | case Instruction::ZExt: |
| 847 | case Instruction::SExt: |
| 848 | case Instruction::FPTrunc: |
| 849 | case Instruction::FPExt: |
| 850 | case Instruction::UIToFP: |
| 851 | case Instruction::SIToFP: |
| 852 | case Instruction::FPToUI: |
| 853 | case Instruction::FPToSI: |
| 854 | case Instruction::PtrToInt: |
| 855 | case Instruction::IntToPtr: |
| 856 | case Instruction::BitCast: |
| 857 | return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 858 | case Instruction::Select: |
| 859 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 860 | case Instruction::InsertElement: |
| 861 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 862 | case Instruction::ExtractElement: |
| 863 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 864 | case Instruction::ShuffleVector: |
| 865 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 866 | case Instruction::GetElementPtr: |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 867 | return cast<GEPOperator>(this)->isInBounds() ? |
| 868 | ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], NumOps-1) : |
| 869 | ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 870 | case Instruction::ICmp: |
| 871 | case Instruction::FCmp: |
| 872 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 873 | default: |
| 874 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 875 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 879 | |
| 880 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 881 | // isValueValidForType implementations |
| 882 | |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 883 | bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 884 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 885 | if (Ty == Type::getInt1Ty(Ty->getContext())) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 886 | return Val == 0 || Val == 1; |
Reid Spencer | d7a00d7 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 887 | if (NumBits >= 64) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 888 | return true; // always true, has to fit in largest type |
| 889 | uint64_t Max = (1ll << NumBits) - 1; |
| 890 | return Val <= Max; |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 893 | bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 894 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 895 | if (Ty == Type::getInt1Ty(Ty->getContext())) |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 896 | return Val == 0 || Val == 1 || Val == -1; |
Reid Spencer | d7a00d7 | 2007-02-05 23:47:56 +0000 | [diff] [blame] | 897 | if (NumBits >= 64) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 898 | return true; // always true, has to fit in largest type |
| 899 | int64_t Min = -(1ll << (NumBits-1)); |
| 900 | int64_t Max = (1ll << (NumBits-1)) - 1; |
| 901 | return (Val >= Min && Val <= Max); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 904 | bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { |
| 905 | // convert modifies in place, so make a copy. |
| 906 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 907 | bool losesInfo; |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 908 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 909 | default: |
| 910 | return false; // These can't be represented as floating point! |
| 911 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 912 | // FIXME rounding mode needs to be more flexible |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 913 | case Type::FloatTyID: { |
| 914 | if (&Val2.getSemantics() == &APFloat::IEEEsingle) |
| 915 | return true; |
| 916 | Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); |
| 917 | return !losesInfo; |
| 918 | } |
| 919 | case Type::DoubleTyID: { |
| 920 | if (&Val2.getSemantics() == &APFloat::IEEEsingle || |
| 921 | &Val2.getSemantics() == &APFloat::IEEEdouble) |
| 922 | return true; |
| 923 | Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); |
| 924 | return !losesInfo; |
| 925 | } |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 926 | case Type::X86_FP80TyID: |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 927 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 928 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 929 | &Val2.getSemantics() == &APFloat::x87DoubleExtended; |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 930 | case Type::FP128TyID: |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 931 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 932 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 933 | &Val2.getSemantics() == &APFloat::IEEEquad; |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 934 | case Type::PPC_FP128TyID: |
| 935 | return &Val2.getSemantics() == &APFloat::IEEEsingle || |
| 936 | &Val2.getSemantics() == &APFloat::IEEEdouble || |
| 937 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 938 | } |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 939 | } |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 941 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 942 | // Factory Function Implementation |
| 943 | |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 944 | ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) { |
Chris Lattner | 13ee795 | 2010-08-28 04:09:24 +0000 | [diff] [blame] | 945 | assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 946 | "Cannot create an aggregate zero of non-aggregate type!"); |
| 947 | |
| 948 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 949 | return pImpl->AggZeroConstants.getOrCreate(Ty, 0); |
| 950 | } |
| 951 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 952 | /// destroyConstant - Remove the constant from the constant table... |
| 953 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 954 | void ConstantAggregateZero::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 955 | getRawType()->getContext().pImpl->AggZeroConstants.remove(this); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 956 | destroyConstantImpl(); |
| 957 | } |
| 958 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 959 | /// destroyConstant - Remove the constant from the constant table... |
| 960 | /// |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 961 | void ConstantArray::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 962 | getRawType()->getContext().pImpl->ArrayConstants.remove(this); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 963 | destroyConstantImpl(); |
| 964 | } |
| 965 | |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 966 | /// isString - This method returns true if the array is an array of i8, and |
| 967 | /// if the elements of the array are all ConstantInt's. |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 968 | bool ConstantArray::isString() const { |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 969 | // Check the element type for i8... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 970 | if (!getType()->getElementType()->isIntegerTy(8)) |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 971 | return false; |
| 972 | // Check the elements to make sure they are all integers, not constant |
| 973 | // expressions. |
| 974 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 975 | if (!isa<ConstantInt>(getOperand(i))) |
| 976 | return false; |
| 977 | return true; |
| 978 | } |
| 979 | |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 980 | /// isCString - This method returns true if the array is a string (see |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 981 | /// isString) and it ends in a null byte \\0 and does not contains any other |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 982 | /// null bytes except its terminator. |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 983 | bool ConstantArray::isCString() const { |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 984 | // Check the element type for i8... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 985 | if (!getType()->getElementType()->isIntegerTy(8)) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 986 | return false; |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 987 | |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 988 | // Last element must be a null. |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 989 | if (!getOperand(getNumOperands()-1)->isNullValue()) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 990 | return false; |
| 991 | // Other elements must be non-null integers. |
| 992 | for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) { |
| 993 | if (!isa<ConstantInt>(getOperand(i))) |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 994 | return false; |
Owen Anderson | e4dcecd | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 995 | if (getOperand(i)->isNullValue()) |
Evan Cheng | e974da6 | 2006-10-26 21:48:03 +0000 | [diff] [blame] | 996 | return false; |
| 997 | } |
Evan Cheng | 3763c5b | 2006-10-26 19:15:05 +0000 | [diff] [blame] | 998 | return true; |
| 999 | } |
| 1000 | |
| 1001 | |
Dan Gohman | 92b551b | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 1002 | /// getAsString - If the sub-element type of this array is i8 |
| 1003 | /// then this method converts the array to an std::string and returns it. |
| 1004 | /// Otherwise, it asserts out. |
| 1005 | /// |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1006 | std::string ConstantArray::getAsString() const { |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 1007 | assert(isString() && "Not a string!"); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1008 | std::string Result; |
Owen Anderson | 79c69bc | 2008-06-24 21:58:29 +0000 | [diff] [blame] | 1009 | Result.reserve(getNumOperands()); |
Chris Lattner | 6077c31 | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 1010 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Owen Anderson | ee9c30d | 2008-06-25 01:05:05 +0000 | [diff] [blame] | 1011 | Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1012 | return Result; |
| 1013 | } |
| 1014 | |
| 1015 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1016 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1017 | // |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1019 | namespace llvm { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1021 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1023 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1024 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1025 | void ConstantStruct::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1026 | getRawType()->getContext().pImpl->StructConstants.remove(this); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1027 | destroyConstantImpl(); |
| 1028 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1029 | |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1030 | // destroyConstant - Remove the constant from the constant table... |
| 1031 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1032 | void ConstantVector::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1033 | getRawType()->getContext().pImpl->VectorConstants.remove(this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1034 | destroyConstantImpl(); |
| 1035 | } |
| 1036 | |
Dan Gohman | 3097807 | 2007-05-24 14:36:04 +0000 | [diff] [blame] | 1037 | /// This function will return true iff every element in this vector constant |
Jim Laskey | f047882 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 1038 | /// is set to all ones. |
| 1039 | /// @returns true iff this constant's emements are all set to all ones. |
| 1040 | /// @brief Determine if the value is all ones. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1041 | bool ConstantVector::isAllOnesValue() const { |
Jim Laskey | f047882 | 2007-01-12 22:39:14 +0000 | [diff] [blame] | 1042 | // Check out first element. |
| 1043 | const Constant *Elt = getOperand(0); |
| 1044 | const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); |
| 1045 | if (!CI || !CI->isAllOnesValue()) return false; |
| 1046 | // Then make sure all remaining elements point to the same value. |
| 1047 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { |
| 1048 | if (getOperand(I) != Elt) return false; |
| 1049 | } |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
Dan Gohman | 0715920 | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1053 | /// getSplatValue - If this is a splat constant, where all of the |
| 1054 | /// elements have the same value, return that value. Otherwise return null. |
Duncan Sands | cf0ff03 | 2011-02-01 08:39:12 +0000 | [diff] [blame] | 1055 | Constant *ConstantVector::getSplatValue() const { |
Dan Gohman | 0715920 | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1056 | // Check out first element. |
| 1057 | Constant *Elt = getOperand(0); |
| 1058 | // Then make sure all remaining elements point to the same value. |
| 1059 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) |
| 1060 | if (getOperand(I) != Elt) return 0; |
| 1061 | return Elt; |
| 1062 | } |
| 1063 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1064 | //---- ConstantPointerNull::get() implementation. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1065 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1066 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1067 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 1068 | return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1071 | // destroyConstant - Remove the constant from the constant table... |
| 1072 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1073 | void ConstantPointerNull::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1074 | getRawType()->getContext().pImpl->NullPtrConstants.remove(this); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1075 | destroyConstantImpl(); |
| 1076 | } |
| 1077 | |
| 1078 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1079 | //---- UndefValue::get() implementation. |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1080 | // |
| 1081 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1082 | UndefValue *UndefValue::get(const Type *Ty) { |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 1083 | return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | // destroyConstant - Remove the constant from the constant table. |
| 1087 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1088 | void UndefValue::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1089 | getRawType()->getContext().pImpl->UndefValueConstants.remove(this); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1090 | destroyConstantImpl(); |
| 1091 | } |
| 1092 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1093 | //---- BlockAddress::get() implementation. |
| 1094 | // |
| 1095 | |
| 1096 | BlockAddress *BlockAddress::get(BasicBlock *BB) { |
| 1097 | assert(BB->getParent() != 0 && "Block must have a parent"); |
| 1098 | return get(BB->getParent(), BB); |
| 1099 | } |
| 1100 | |
| 1101 | BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) { |
| 1102 | BlockAddress *&BA = |
| 1103 | F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)]; |
| 1104 | if (BA == 0) |
| 1105 | BA = new BlockAddress(F, BB); |
| 1106 | |
| 1107 | assert(BA->getFunction() == F && "Basic block moved between functions"); |
| 1108 | return BA; |
| 1109 | } |
| 1110 | |
| 1111 | BlockAddress::BlockAddress(Function *F, BasicBlock *BB) |
| 1112 | : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal, |
| 1113 | &Op<0>(), 2) { |
Chris Lattner | c559a9f | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1114 | setOperand(0, F); |
| 1115 | setOperand(1, BB); |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 1116 | BB->AdjustBlockAddressRefCount(1); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | // destroyConstant - Remove the constant from the constant table. |
| 1121 | // |
| 1122 | void BlockAddress::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1123 | getFunction()->getRawType()->getContext().pImpl |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1124 | ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock())); |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 1125 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1126 | destroyConstantImpl(); |
| 1127 | } |
| 1128 | |
| 1129 | void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { |
| 1130 | // This could be replacing either the Basic Block or the Function. In either |
| 1131 | // case, we have to remove the map entry. |
| 1132 | Function *NewF = getFunction(); |
| 1133 | BasicBlock *NewBB = getBasicBlock(); |
| 1134 | |
| 1135 | if (U == &Op<0>()) |
| 1136 | NewF = cast<Function>(To); |
| 1137 | else |
| 1138 | NewBB = cast<BasicBlock>(To); |
| 1139 | |
| 1140 | // See if the 'new' entry already exists, if not, just update this in place |
| 1141 | // and return early. |
| 1142 | BlockAddress *&NewBA = |
| 1143 | getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)]; |
| 1144 | if (NewBA == 0) { |
Chris Lattner | c559a9f | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1145 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
| 1146 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1147 | // Remove the old entry, this can't cause the map to rehash (just a |
| 1148 | // tombstone will get added). |
| 1149 | getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(), |
| 1150 | getBasicBlock())); |
| 1151 | NewBA = this; |
Chris Lattner | c559a9f | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1152 | setOperand(0, NewF); |
| 1153 | setOperand(1, NewBB); |
| 1154 | getBasicBlock()->AdjustBlockAddressRefCount(1); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | // Otherwise, I do need to replace this with an existing value. |
| 1159 | assert(NewBA != this && "I didn't contain From!"); |
| 1160 | |
| 1161 | // Everyone using this now uses the replacement. |
| 1162 | uncheckedReplaceAllUsesWith(NewBA); |
| 1163 | |
| 1164 | destroyConstant(); |
| 1165 | } |
| 1166 | |
| 1167 | //---- ConstantExpr::get() implementations. |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1168 | // |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1169 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1170 | /// This is a utility function to handle folding of casts and lookup of the |
Duncan Sands | 7d6c8ae | 2008-03-30 19:38:55 +0000 | [diff] [blame] | 1171 | /// cast in the ExprConstants map. It is used by the various get* methods below. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1172 | static inline Constant *getFoldedCast( |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1173 | Instruction::CastOps opc, Constant *C, const Type *Ty) { |
Chris Lattner | 815ae2b | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1174 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1175 | // Fold a few common cases |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1176 | if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1177 | return FC; |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1178 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1179 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 1180 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1181 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1182 | std::vector<Constant*> argVec(1, C); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1183 | ExprMapKeyType Key(opc, argVec); |
Owen Anderson | 2d7231d | 2009-06-17 18:40:29 +0000 | [diff] [blame] | 1184 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1185 | return pImpl->ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1186 | } |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1187 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1188 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1189 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1190 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1191 | assert(C && Ty && "Null arguments to getCast"); |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1192 | assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1193 | |
| 1194 | switch (opc) { |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1195 | default: |
| 1196 | llvm_unreachable("Invalid cast opcode"); |
| 1197 | break; |
| 1198 | case Instruction::Trunc: return getTrunc(C, Ty); |
| 1199 | case Instruction::ZExt: return getZExt(C, Ty); |
| 1200 | case Instruction::SExt: return getSExt(C, Ty); |
| 1201 | case Instruction::FPTrunc: return getFPTrunc(C, Ty); |
| 1202 | case Instruction::FPExt: return getFPExtend(C, Ty); |
| 1203 | case Instruction::UIToFP: return getUIToFP(C, Ty); |
| 1204 | case Instruction::SIToFP: return getSIToFP(C, Ty); |
| 1205 | case Instruction::FPToUI: return getFPToUI(C, Ty); |
| 1206 | case Instruction::FPToSI: return getFPToSI(C, Ty); |
| 1207 | case Instruction::PtrToInt: return getPtrToInt(C, Ty); |
| 1208 | case Instruction::IntToPtr: return getIntToPtr(C, Ty); |
| 1209 | case Instruction::BitCast: return getBitCast(C, Ty); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1210 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1211 | return 0; |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1214 | Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1215 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1216 | return getBitCast(C, Ty); |
| 1217 | return getZExt(C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1220 | Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1221 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1222 | return getBitCast(C, Ty); |
| 1223 | return getSExt(C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1227 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1228 | return getBitCast(C, Ty); |
| 1229 | return getTrunc(C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1232 | Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1233 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 1234 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast"); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1235 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1236 | if (Ty->isIntegerTy()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1237 | return getPtrToInt(S, Ty); |
| 1238 | return getBitCast(S, Ty); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1241 | Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty, |
| 1242 | bool isSigned) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1243 | assert(C->getType()->isIntOrIntVectorTy() && |
| 1244 | Ty->isIntOrIntVectorTy() && "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1245 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1246 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1247 | Instruction::CastOps opcode = |
| 1248 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1249 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1250 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1251 | return getCast(opcode, C, Ty); |
| 1252 | } |
| 1253 | |
| 1254 | Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1255 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1256 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1257 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1258 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | ca104e8 | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1259 | if (SrcBits == DstBits) |
| 1260 | return C; // Avoid a useless cast |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1261 | Instruction::CastOps opcode = |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 1262 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1263 | return getCast(opcode, C, Ty); |
| 1264 | } |
| 1265 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1266 | Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1267 | #ifndef NDEBUG |
| 1268 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1269 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1270 | #endif |
| 1271 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1272 | assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer"); |
| 1273 | assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1274 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1275 | "SrcTy must be larger than DestTy for Trunc!"); |
| 1276 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1277 | return getFoldedCast(Instruction::Trunc, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1280 | Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1281 | #ifndef NDEBUG |
| 1282 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1283 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1284 | #endif |
| 1285 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1286 | assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral"); |
| 1287 | assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1288 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1289 | "SrcTy must be smaller than DestTy for SExt!"); |
| 1290 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1291 | return getFoldedCast(Instruction::SExt, C, Ty); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1294 | Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1295 | #ifndef NDEBUG |
| 1296 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1297 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1298 | #endif |
| 1299 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1300 | assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral"); |
| 1301 | assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1302 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1303 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 1304 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1305 | return getFoldedCast(Instruction::ZExt, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1308 | Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1309 | #ifndef NDEBUG |
| 1310 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1311 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1312 | #endif |
| 1313 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1314 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1315 | C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1316 | "This is an illegal floating point truncation!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1317 | return getFoldedCast(Instruction::FPTrunc, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1320 | Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1321 | #ifndef NDEBUG |
| 1322 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1323 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1324 | #endif |
| 1325 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1326 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1327 | C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1328 | "This is an illegal floating point extension!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1329 | return getFoldedCast(Instruction::FPExt, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1332 | Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1333 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1334 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1335 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1336 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1337 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1338 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1339 | "This is an illegal uint to floating point cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1340 | return getFoldedCast(Instruction::UIToFP, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1343 | Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1344 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1345 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1346 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1347 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1348 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1349 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1350 | "This is an illegal sint to floating point cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1351 | return getFoldedCast(Instruction::SIToFP, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1354 | Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1355 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1356 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1357 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1358 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1359 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1360 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1361 | "This is an illegal floating point to uint cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1362 | return getFoldedCast(Instruction::FPToUI, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1365 | Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1366 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1367 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1368 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1369 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1370 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1371 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1372 | "This is an illegal floating point to sint cast!"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1373 | return getFoldedCast(Instruction::FPToSI, C, Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1376 | Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1377 | assert(C->getType()->isPointerTy() && "PtrToInt source must be pointer"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1378 | assert(DstTy->isIntegerTy() && "PtrToInt destination must be integral"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1379 | return getFoldedCast(Instruction::PtrToInt, C, DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1382 | Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1383 | assert(C->getType()->isIntegerTy() && "IntToPtr source must be integral"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1384 | assert(DstTy->isPointerTy() && "IntToPtr destination must be a pointer"); |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1385 | return getFoldedCast(Instruction::IntToPtr, C, DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1388 | Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1389 | assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) && |
| 1390 | "Invalid constantexpr bitcast!"); |
Chris Lattner | cbeda87 | 2009-03-21 06:55:54 +0000 | [diff] [blame] | 1391 | |
| 1392 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 1393 | // speedily. |
| 1394 | if (C->getType() == DstTy) return C; |
| 1395 | |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1396 | return getFoldedCast(Instruction::BitCast, C, DstTy); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1399 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1400 | Constant *C1, Constant *C2, |
| 1401 | unsigned Flags) { |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1402 | // Check the operands for consistency first |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1403 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 1404 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1405 | "Invalid opcode in binary constant expression"); |
| 1406 | assert(C1->getType() == C2->getType() && |
| 1407 | "Operand types in binary constant expression should match"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1408 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1409 | if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext())) |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1410 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1411 | return FC; // Fold a few common cases... |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1413 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1414 | ExprMapKeyType Key(Opcode, argVec, 0, Flags); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1415 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1416 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1417 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1420 | Constant *ConstantExpr::getCompareTy(unsigned short predicate, |
Nate Begeman | 098cc6f | 2008-07-25 17:56:27 +0000 | [diff] [blame] | 1421 | Constant *C1, Constant *C2) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1422 | switch (predicate) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1423 | default: llvm_unreachable("Invalid CmpInst predicate"); |
Nate Begeman | c96e2e4 | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 1424 | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
| 1425 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
| 1426 | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
| 1427 | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
| 1428 | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
| 1429 | case CmpInst::FCMP_TRUE: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1430 | return getFCmp(predicate, C1, C2); |
| 1431 | |
Nate Begeman | c96e2e4 | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 1432 | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
| 1433 | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
| 1434 | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
| 1435 | case CmpInst::ICMP_SLE: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1436 | return getICmp(predicate, C1, C2); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1437 | } |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1440 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, |
| 1441 | unsigned Flags) { |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1442 | #ifndef NDEBUG |
| 1443 | switch (Opcode) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1444 | case Instruction::Add: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1445 | case Instruction::Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1446 | case Instruction::Mul: |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1447 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1448 | assert(C1->getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1449 | "Tried to create an integer operation on a non-integer type!"); |
| 1450 | break; |
| 1451 | case Instruction::FAdd: |
| 1452 | case Instruction::FSub: |
| 1453 | case Instruction::FMul: |
| 1454 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1455 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1456 | "Tried to create a floating-point operation on a " |
| 1457 | "non-floating-point type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1458 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1459 | case Instruction::UDiv: |
| 1460 | case Instruction::SDiv: |
| 1461 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1462 | assert(C1->getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1463 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1464 | break; |
| 1465 | case Instruction::FDiv: |
| 1466 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1467 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1468 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1469 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1470 | case Instruction::URem: |
| 1471 | case Instruction::SRem: |
| 1472 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1473 | assert(C1->getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1474 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1475 | break; |
| 1476 | case Instruction::FRem: |
| 1477 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1478 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1479 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1480 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1481 | case Instruction::And: |
| 1482 | case Instruction::Or: |
| 1483 | case Instruction::Xor: |
| 1484 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1485 | assert(C1->getType()->isIntOrIntVectorTy() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1486 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1487 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1488 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1489 | case Instruction::LShr: |
| 1490 | case Instruction::AShr: |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1491 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1492 | assert(C1->getType()->isIntOrIntVectorTy() && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1493 | "Tried to create a shift operation on a non-integer type!"); |
| 1494 | break; |
| 1495 | default: |
| 1496 | break; |
| 1497 | } |
| 1498 | #endif |
| 1499 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1500 | return getTy(C1->getType(), Opcode, C1, C2, Flags); |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1503 | Constant *ConstantExpr::getSizeOf(const Type* Ty) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1504 | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
| 1505 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1506 | Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1507 | Constant *GEP = getGetElementPtr( |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1508 | Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1); |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1509 | return getPtrToInt(GEP, |
| 1510 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1513 | Constant *ConstantExpr::getAlignOf(const Type* Ty) { |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 1514 | // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1 |
Dan Gohman | 50c09d0 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1515 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1516 | const Type *AligningTy = StructType::get(Ty->getContext(), |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 1517 | Type::getInt1Ty(Ty->getContext()), Ty, NULL); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1518 | Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo()); |
Dan Gohman | a9be739 | 2010-01-28 02:43:22 +0000 | [diff] [blame] | 1519 | Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1520 | Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1521 | Constant *Indices[2] = { Zero, One }; |
| 1522 | Constant *GEP = getGetElementPtr(NullPtr, Indices, 2); |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1523 | return getPtrToInt(GEP, |
| 1524 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1527 | Constant *ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) { |
Dan Gohman | ede94e6 | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1528 | return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()), |
| 1529 | FieldNo)); |
| 1530 | } |
| 1531 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1532 | Constant *ConstantExpr::getOffsetOf(const Type* Ty, Constant *FieldNo) { |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1533 | // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo |
| 1534 | // Note that a non-inbounds gep is used, as null isn't within any object. |
| 1535 | Constant *GEPIdx[] = { |
Dan Gohman | ede94e6 | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1536 | ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0), |
| 1537 | FieldNo |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1538 | }; |
| 1539 | Constant *GEP = getGetElementPtr( |
Dan Gohman | ede94e6 | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1540 | Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2); |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1541 | return getPtrToInt(GEP, |
| 1542 | Type::getInt64Ty(Ty->getContext())); |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1543 | } |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1544 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1545 | Constant *ConstantExpr::getCompare(unsigned short pred, |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1546 | Constant *C1, Constant *C2) { |
| 1547 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1548 | return getCompareTy(pred, C1, C2); |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1551 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1552 | Constant *V1, Constant *V2) { |
Chris Lattner | 4163213 | 2008-12-29 00:16:12 +0000 | [diff] [blame] | 1553 | assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1554 | |
| 1555 | if (ReqTy == V1->getType()) |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1556 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1557 | return SC; // Fold common cases |
| 1558 | |
| 1559 | std::vector<Constant*> argVec(3, C); |
| 1560 | argVec[1] = V1; |
| 1561 | argVec[2] = V2; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1562 | ExprMapKeyType Key(Instruction::Select, argVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1563 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1564 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1565 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1568 | template<typename IndexTy> |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1569 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1570 | IndexTy const *Idxs, |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1571 | unsigned NumIdx, bool InBounds) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1572 | assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, |
| 1573 | Idxs+NumIdx) == |
| 1574 | cast<PointerType>(ReqTy)->getElementType() && |
| 1575 | "GEP indices invalid!"); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1577 | if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs, NumIdx)) |
| 1578 | return FC; // Fold a few common cases. |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1579 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1580 | assert(C->getType()->isPointerTy() && |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1581 | "Non-pointer type for constant GetElementPtr expression"); |
| 1582 | // Look up the constant in the table first to ensure uniqueness |
| 1583 | std::vector<Constant*> ArgVec; |
| 1584 | ArgVec.reserve(NumIdx+1); |
| 1585 | ArgVec.push_back(C); |
| 1586 | for (unsigned i = 0; i != NumIdx; ++i) |
| 1587 | ArgVec.push_back(cast<Constant>(Idxs[i])); |
| 1588 | const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0, |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1589 | InBounds ? GEPOperator::IsInBounds : 0); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1590 | |
| 1591 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1592 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
| 1593 | } |
| 1594 | |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1595 | template<typename IndexTy> |
| 1596 | Constant *ConstantExpr::getGetElementPtrImpl(Constant *C, IndexTy const *Idxs, |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1597 | unsigned NumIdx, bool InBounds) { |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1598 | // Get the result type of the getelementptr! |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1599 | const Type *Ty = |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1600 | GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1601 | assert(Ty && "GEP indices invalid!"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1602 | unsigned As = cast<PointerType>(C->getType())->getAddressSpace(); |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1603 | return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx,InBounds); |
Dan Gohman | e4ca02d | 2009-09-03 23:34:49 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1606 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs, |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1607 | unsigned NumIdx, bool InBounds) { |
| 1608 | return getGetElementPtrImpl(C, Idxs, NumIdx, InBounds); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant *const *Idxs, |
Chris Lattner | 94c8d29 | 2011-02-11 05:34:33 +0000 | [diff] [blame] | 1612 | unsigned NumIdx, bool InBounds) { |
| 1613 | return getGetElementPtrImpl(C, Idxs, NumIdx, InBounds); |
Dan Gohman | e4ca02d | 2009-09-03 23:34:49 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1616 | Constant * |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1617 | ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) { |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1618 | assert(LHS->getType() == RHS->getType()); |
| 1619 | assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1620 | pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); |
| 1621 | |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1622 | if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1623 | return FC; // Fold a few common cases... |
| 1624 | |
| 1625 | // Look up the constant in the table first to ensure uniqueness |
| 1626 | std::vector<Constant*> ArgVec; |
| 1627 | ArgVec.push_back(LHS); |
| 1628 | ArgVec.push_back(RHS); |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1629 | // Get the key type with both the opcode and predicate |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1630 | const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1631 | |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1632 | const Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
| 1633 | if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
| 1634 | ResultTy = VectorType::get(ResultTy, VT->getNumElements()); |
| 1635 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1636 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1637 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | Constant * |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1641 | ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) { |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1642 | assert(LHS->getType() == RHS->getType()); |
| 1643 | assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); |
| 1644 | |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1645 | if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1646 | return FC; // Fold a few common cases... |
| 1647 | |
| 1648 | // Look up the constant in the table first to ensure uniqueness |
| 1649 | std::vector<Constant*> ArgVec; |
| 1650 | ArgVec.push_back(LHS); |
| 1651 | ArgVec.push_back(RHS); |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1652 | // Get the key type with both the opcode and predicate |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1653 | const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1654 | |
| 1655 | const Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
| 1656 | if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
| 1657 | ResultTy = VectorType::get(ResultTy, VT->getNumElements()); |
| 1658 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1659 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1660 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1663 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 1664 | Constant *Idx) { |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1665 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
Chris Lattner | 09660c9 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 1666 | return FC; // Fold a few common cases. |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1667 | // Look up the constant in the table first to ensure uniqueness |
| 1668 | std::vector<Constant*> ArgVec(1, Val); |
| 1669 | ArgVec.push_back(Idx); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1670 | const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1671 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1672 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1673 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1677 | assert(Val->getType()->isVectorTy() && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1678 | "Tried to create extractelement operation on non-vector type!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1679 | assert(Idx->getType()->isIntegerTy(32) && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1680 | "Extractelement index must be i32 type!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1681 | return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(), |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1682 | Val, Idx); |
| 1683 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1684 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1685 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 1686 | Constant *Elt, Constant *Idx) { |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1687 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
Chris Lattner | 09660c9 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 1688 | return FC; // Fold a few common cases. |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1689 | // Look up the constant in the table first to ensure uniqueness |
| 1690 | std::vector<Constant*> ArgVec(1, Val); |
| 1691 | ArgVec.push_back(Elt); |
| 1692 | ArgVec.push_back(Idx); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1693 | const ExprMapKeyType Key(Instruction::InsertElement,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1694 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1695 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1696 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1700 | Constant *Idx) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1701 | assert(Val->getType()->isVectorTy() && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1702 | "Tried to create insertelement operation on non-vector type!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1703 | assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1704 | && "Insertelement types must match!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1705 | assert(Idx->getType()->isIntegerTy(32) && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1706 | "Insertelement index must be i32 type!"); |
Gordon Henriksen | b52d1ed | 2008-08-30 15:41:51 +0000 | [diff] [blame] | 1707 | return getInsertElementTy(Val->getType(), Val, Elt, Idx); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1710 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 1711 | Constant *V2, Constant *Mask) { |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1712 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1713 | return FC; // Fold a few common cases... |
| 1714 | // Look up the constant in the table first to ensure uniqueness |
| 1715 | std::vector<Constant*> ArgVec(1, V1); |
| 1716 | ArgVec.push_back(V2); |
| 1717 | ArgVec.push_back(Mask); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1718 | const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1719 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1720 | LLVMContextImpl *pImpl = ReqTy->getContext().pImpl; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1721 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1725 | Constant *Mask) { |
| 1726 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1727 | "Invalid shuffle vector constant expr operands!"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 1728 | |
| 1729 | unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements(); |
| 1730 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
| 1731 | const Type *ShufTy = VectorType::get(EltTy, NElts); |
| 1732 | return getShuffleVectorTy(ShufTy, V1, V2, Mask); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1735 | Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, |
| 1736 | Constant *Val, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1737 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1738 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 1739 | Idxs+NumIdx) == Val->getType() && |
| 1740 | "insertvalue indices invalid!"); |
| 1741 | assert(Agg->getType() == ReqTy && |
| 1742 | "insertvalue type invalid!"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1743 | assert(Agg->getType()->isFirstClassType() && |
| 1744 | "Non-first-class type for constant InsertValue expression"); |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1745 | Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx); |
Dan Gohman | d5d24f6 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 1746 | assert(FC && "InsertValue constant expr couldn't be folded!"); |
| 1747 | return FC; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1751 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1752 | assert(Agg->getType()->isFirstClassType() && |
| 1753 | "Tried to create insertelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1754 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1755 | const Type *ReqTy = Agg->getType(); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1756 | #ifndef NDEBUG |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1757 | const Type *ValTy = |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1758 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1759 | #endif |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1760 | assert(ValTy == Val->getType() && "insertvalue indices invalid!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1761 | return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx); |
| 1762 | } |
| 1763 | |
| 1764 | Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1765 | const unsigned *Idxs, unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1766 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs, |
| 1767 | Idxs+NumIdx) == ReqTy && |
| 1768 | "extractvalue indices invalid!"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1769 | assert(Agg->getType()->isFirstClassType() && |
| 1770 | "Non-first-class type for constant extractvalue expression"); |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1771 | Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx); |
Dan Gohman | d5d24f6 | 2008-07-21 23:30:30 +0000 | [diff] [blame] | 1772 | assert(FC && "ExtractValue constant expr couldn't be folded!"); |
| 1773 | return FC; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | Constant *ConstantExpr::getExtractValue(Constant *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1777 | const unsigned *IdxList, unsigned NumIdx) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1778 | assert(Agg->getType()->isFirstClassType() && |
| 1779 | "Tried to create extractelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1780 | |
| 1781 | const Type *ReqTy = |
| 1782 | ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); |
| 1783 | assert(ReqTy && "extractvalue indices invalid!"); |
| 1784 | return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx); |
| 1785 | } |
| 1786 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1787 | Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1788 | assert(C->getType()->isIntOrIntVectorTy() && |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1789 | "Cannot NEG a nonintegral value!"); |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1790 | return getSub(ConstantFP::getZeroValueForNegation(C->getType()), |
| 1791 | C, HasNUW, HasNSW); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1794 | Constant *ConstantExpr::getFNeg(Constant *C) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1795 | assert(C->getType()->isFPOrFPVectorTy() && |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1796 | "Cannot FNEG a non-floating-point value!"); |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1797 | return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1800 | Constant *ConstantExpr::getNot(Constant *C) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1801 | assert(C->getType()->isIntOrIntVectorTy() && |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1802 | "Cannot NOT a nonintegral value!"); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1803 | return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1806 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2, |
| 1807 | bool HasNUW, bool HasNSW) { |
| 1808 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 1809 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 1810 | return get(Instruction::Add, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1813 | Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1814 | return get(Instruction::FAdd, C1, C2); |
| 1815 | } |
| 1816 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1817 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2, |
| 1818 | bool HasNUW, bool HasNSW) { |
| 1819 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 1820 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 1821 | return get(Instruction::Sub, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1824 | Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1825 | return get(Instruction::FSub, C1, C2); |
| 1826 | } |
| 1827 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1828 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2, |
| 1829 | bool HasNUW, bool HasNSW) { |
| 1830 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 1831 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 1832 | return get(Instruction::Mul, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1835 | Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1836 | return get(Instruction::FMul, C1, C2); |
| 1837 | } |
| 1838 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 1839 | Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) { |
| 1840 | return get(Instruction::UDiv, C1, C2, |
| 1841 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 1844 | Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) { |
| 1845 | return get(Instruction::SDiv, C1, C2, |
| 1846 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1849 | Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1850 | return get(Instruction::FDiv, C1, C2); |
| 1851 | } |
| 1852 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1853 | Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1854 | return get(Instruction::URem, C1, C2); |
| 1855 | } |
| 1856 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1857 | Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1858 | return get(Instruction::SRem, C1, C2); |
| 1859 | } |
| 1860 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1861 | Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1862 | return get(Instruction::FRem, C1, C2); |
| 1863 | } |
| 1864 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1865 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1866 | return get(Instruction::And, C1, C2); |
| 1867 | } |
| 1868 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1869 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1870 | return get(Instruction::Or, C1, C2); |
| 1871 | } |
| 1872 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 1873 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1874 | return get(Instruction::Xor, C1, C2); |
| 1875 | } |
| 1876 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 1877 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2, |
| 1878 | bool HasNUW, bool HasNSW) { |
| 1879 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 1880 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 1881 | return get(Instruction::Shl, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 1884 | Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) { |
| 1885 | return get(Instruction::LShr, C1, C2, |
| 1886 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 1889 | Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) { |
| 1890 | return get(Instruction::AShr, C1, C2, |
| 1891 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1894 | // destroyConstant - Remove the constant from the constant table... |
| 1895 | // |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1896 | void ConstantExpr::destroyConstant() { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1897 | getRawType()->getContext().pImpl->ExprConstants.remove(this); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1898 | destroyConstantImpl(); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1901 | const char *ConstantExpr::getOpcodeName() const { |
| 1902 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1903 | } |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 1904 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 1905 | |
| 1906 | |
| 1907 | GetElementPtrConstantExpr:: |
| 1908 | GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
| 1909 | const Type *DestTy) |
| 1910 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 1911 | OperandTraits<GetElementPtrConstantExpr>::op_end(this) |
| 1912 | - (IdxList.size()+1), IdxList.size()+1) { |
| 1913 | OperandList[0] = C; |
| 1914 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 1915 | OperandList[i+1] = IdxList[i]; |
| 1916 | } |
| 1917 | |
| 1918 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1919 | //===----------------------------------------------------------------------===// |
| 1920 | // replaceUsesOfWithOnConstant implementations |
| 1921 | |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 1922 | /// replaceUsesOfWithOnConstant - Update this constant array to change uses of |
| 1923 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 1924 | /// etc. |
| 1925 | /// |
| 1926 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 1927 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
| 1928 | /// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that |
| 1929 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 1930 | /// work, but would be really slow because it would have to unique each updated |
| 1931 | /// array instance. |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1932 | /// |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1933 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1934 | Use *U) { |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1935 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1936 | Constant *ToC = cast<Constant>(To); |
| 1937 | |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1938 | LLVMContextImpl *pImpl = getRawType()->getContext().pImpl; |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1939 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 1940 | std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup; |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1941 | Lookup.first.first = cast<ArrayType>(getRawType()); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1942 | Lookup.second = this; |
| 1943 | |
| 1944 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1945 | Values.reserve(getNumOperands()); // Build replacement array. |
| 1946 | |
| 1947 | // Fill values with the modified operands of the constant array. Also, |
| 1948 | // compute whether this turns into an all-zeros array. |
| 1949 | bool isAllZeros = false; |
| 1950 | unsigned NumUpdated = 0; |
| 1951 | if (!ToC->isNullValue()) { |
| 1952 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1953 | Constant *Val = cast<Constant>(O->get()); |
| 1954 | if (Val == From) { |
| 1955 | Val = ToC; |
| 1956 | ++NumUpdated; |
| 1957 | } |
| 1958 | Values.push_back(Val); |
| 1959 | } |
| 1960 | } else { |
| 1961 | isAllZeros = true; |
| 1962 | for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) { |
| 1963 | Constant *Val = cast<Constant>(O->get()); |
| 1964 | if (Val == From) { |
| 1965 | Val = ToC; |
| 1966 | ++NumUpdated; |
| 1967 | } |
| 1968 | Values.push_back(Val); |
| 1969 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | Constant *Replacement = 0; |
| 1974 | if (isAllZeros) { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 1975 | Replacement = ConstantAggregateZero::get(getRawType()); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1976 | } else { |
| 1977 | // Check to see if we have this array type already. |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1978 | bool Exists; |
| 1979 | LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I = |
| 1980 | pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists); |
| 1981 | |
| 1982 | if (Exists) { |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 1983 | Replacement = I->second; |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1984 | } else { |
| 1985 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1986 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 1987 | // old with the new, then deleting the old... just update the current one |
| 1988 | // in place! |
| 1989 | pImpl->ArrayConstants.MoveConstantToNewSlot(this, I); |
| 1990 | |
| 1991 | // Update to the new value. Optimize for the case when we have a single |
| 1992 | // operand that we're changing, but handle bulk updates efficiently. |
| 1993 | if (NumUpdated == 1) { |
| 1994 | unsigned OperandToUpdate = U - OperandList; |
| 1995 | assert(getOperand(OperandToUpdate) == From && |
| 1996 | "ReplaceAllUsesWith broken!"); |
| 1997 | setOperand(OperandToUpdate, ToC); |
| 1998 | } else { |
| 1999 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 2000 | if (getOperand(i) == From) |
| 2001 | setOperand(i, ToC); |
| 2002 | } |
| 2003 | return; |
| 2004 | } |
| 2005 | } |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2008 | assert(Replacement != this && "I didn't contain From!"); |
| 2009 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2010 | // Everyone using this now uses the replacement. |
| 2011 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Delete the old constant! |
| 2014 | destroyConstant(); |
| 2015 | } |
| 2016 | |
| 2017 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2018 | Use *U) { |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2019 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2020 | Constant *ToC = cast<Constant>(To); |
| 2021 | |
| 2022 | unsigned OperandToUpdate = U-OperandList; |
| 2023 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 2024 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 2025 | std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup; |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2026 | Lookup.first.first = cast<StructType>(getRawType()); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2027 | Lookup.second = this; |
| 2028 | std::vector<Constant*> &Values = Lookup.first.second; |
| 2029 | Values.reserve(getNumOperands()); // Build replacement struct. |
| 2030 | |
| 2031 | |
| 2032 | // Fill values with the modified operands of the constant struct. Also, |
| 2033 | // compute whether this turns into an all-zeros struct. |
| 2034 | bool isAllZeros = false; |
| 2035 | if (!ToC->isNullValue()) { |
| 2036 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) |
| 2037 | Values.push_back(cast<Constant>(O->get())); |
| 2038 | } else { |
| 2039 | isAllZeros = true; |
| 2040 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2041 | Constant *Val = cast<Constant>(O->get()); |
| 2042 | Values.push_back(Val); |
| 2043 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 2044 | } |
| 2045 | } |
| 2046 | Values[OperandToUpdate] = ToC; |
| 2047 | |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2048 | LLVMContextImpl *pImpl = getRawType()->getContext().pImpl; |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2049 | |
| 2050 | Constant *Replacement = 0; |
| 2051 | if (isAllZeros) { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2052 | Replacement = ConstantAggregateZero::get(getRawType()); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2053 | } else { |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2054 | // Check to see if we have this struct type already. |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2055 | bool Exists; |
| 2056 | LLVMContextImpl::StructConstantsTy::MapTy::iterator I = |
| 2057 | pImpl->StructConstants.InsertOrGetItem(Lookup, Exists); |
| 2058 | |
| 2059 | if (Exists) { |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 2060 | Replacement = I->second; |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2061 | } else { |
| 2062 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 2063 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 2064 | // old with the new, then deleting the old... just update the current one |
| 2065 | // in place! |
| 2066 | pImpl->StructConstants.MoveConstantToNewSlot(this, I); |
| 2067 | |
| 2068 | // Update to the new value. |
| 2069 | setOperand(OperandToUpdate, ToC); |
| 2070 | return; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | assert(Replacement != this && "I didn't contain From!"); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2076 | // Everyone using this now uses the replacement. |
| 2077 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2078 | |
| 2079 | // Delete the old constant! |
| 2080 | destroyConstant(); |
| 2081 | } |
| 2082 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2083 | void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2084 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2085 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2086 | |
| 2087 | std::vector<Constant*> Values; |
| 2088 | Values.reserve(getNumOperands()); // Build replacement array... |
| 2089 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2090 | Constant *Val = getOperand(i); |
| 2091 | if (Val == From) Val = cast<Constant>(To); |
| 2092 | Values.push_back(Val); |
| 2093 | } |
| 2094 | |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2095 | Constant *Replacement = get(cast<VectorType>(getRawType()), Values); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2096 | assert(Replacement != this && "I didn't contain From!"); |
| 2097 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2098 | // Everyone using this now uses the replacement. |
| 2099 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2100 | |
| 2101 | // Delete the old constant! |
| 2102 | destroyConstant(); |
| 2103 | } |
| 2104 | |
| 2105 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2106 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2107 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 2108 | Constant *To = cast<Constant>(ToV); |
| 2109 | |
| 2110 | Constant *Replacement = 0; |
| 2111 | if (getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2112 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2113 | Constant *Pointer = getOperand(0); |
| 2114 | Indices.reserve(getNumOperands()-1); |
| 2115 | if (Pointer == From) Pointer = To; |
| 2116 | |
| 2117 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 2118 | Constant *Val = getOperand(i); |
| 2119 | if (Val == From) Val = To; |
| 2120 | Indices.push_back(Val); |
| 2121 | } |
Chris Lattner | b5d7030 | 2007-02-19 20:01:23 +0000 | [diff] [blame] | 2122 | Replacement = ConstantExpr::getGetElementPtr(Pointer, |
Chris Lattner | 603af18 | 2011-02-11 05:37:21 +0000 | [diff] [blame] | 2123 | &Indices[0], Indices.size(), |
| 2124 | cast<GEPOperator>(this)->isInBounds()); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2125 | } else if (getOpcode() == Instruction::ExtractValue) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2126 | Constant *Agg = getOperand(0); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2127 | if (Agg == From) Agg = To; |
| 2128 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2129 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2130 | Replacement = ConstantExpr::getExtractValue(Agg, |
| 2131 | &Indices[0], Indices.size()); |
| 2132 | } else if (getOpcode() == Instruction::InsertValue) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2133 | Constant *Agg = getOperand(0); |
| 2134 | Constant *Val = getOperand(1); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2135 | if (Agg == From) Agg = To; |
| 2136 | if (Val == From) Val = To; |
| 2137 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2138 | const SmallVector<unsigned, 4> &Indices = getIndices(); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2139 | Replacement = ConstantExpr::getInsertValue(Agg, Val, |
| 2140 | &Indices[0], Indices.size()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2141 | } else if (isCast()) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2142 | assert(getOperand(0) == From && "Cast only has one use!"); |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 2143 | Replacement = ConstantExpr::getCast(getOpcode(), To, getRawType()); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2144 | } else if (getOpcode() == Instruction::Select) { |
| 2145 | Constant *C1 = getOperand(0); |
| 2146 | Constant *C2 = getOperand(1); |
| 2147 | Constant *C3 = getOperand(2); |
| 2148 | if (C1 == From) C1 = To; |
| 2149 | if (C2 == From) C2 = To; |
| 2150 | if (C3 == From) C3 = To; |
| 2151 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 2152 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 2153 | Constant *C1 = getOperand(0); |
| 2154 | Constant *C2 = getOperand(1); |
| 2155 | if (C1 == From) C1 = To; |
| 2156 | if (C2 == From) C2 = To; |
| 2157 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | a93b4b5 | 2006-04-08 05:09:48 +0000 | [diff] [blame] | 2158 | } else if (getOpcode() == Instruction::InsertElement) { |
| 2159 | Constant *C1 = getOperand(0); |
| 2160 | Constant *C2 = getOperand(1); |
| 2161 | Constant *C3 = getOperand(1); |
| 2162 | if (C1 == From) C1 = To; |
| 2163 | if (C2 == From) C2 = To; |
| 2164 | if (C3 == From) C3 = To; |
| 2165 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 2166 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 2167 | Constant *C1 = getOperand(0); |
| 2168 | Constant *C2 = getOperand(1); |
| 2169 | Constant *C3 = getOperand(2); |
| 2170 | if (C1 == From) C1 = To; |
| 2171 | if (C2 == From) C2 = To; |
| 2172 | if (C3 == From) C3 = To; |
| 2173 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 2174 | } else if (isCompare()) { |
| 2175 | Constant *C1 = getOperand(0); |
| 2176 | Constant *C2 = getOperand(1); |
| 2177 | if (C1 == From) C1 = To; |
| 2178 | if (C2 == From) C2 = To; |
| 2179 | if (getOpcode() == Instruction::ICmp) |
| 2180 | Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2); |
Chris Lattner | eab4926 | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2181 | else { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2182 | assert(getOpcode() == Instruction::FCmp); |
| 2183 | Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2); |
Chris Lattner | eab4926 | 2008-07-14 05:17:31 +0000 | [diff] [blame] | 2184 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2185 | } else if (getNumOperands() == 2) { |
| 2186 | Constant *C1 = getOperand(0); |
| 2187 | Constant *C2 = getOperand(1); |
| 2188 | if (C1 == From) C1 = To; |
| 2189 | if (C2 == From) C2 = To; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2190 | Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2191 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2192 | llvm_unreachable("Unknown ConstantExpr type!"); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2193 | return; |
| 2194 | } |
| 2195 | |
| 2196 | assert(Replacement != this && "I didn't contain From!"); |
| 2197 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 2198 | // Everyone using this now uses the replacement. |
| 2199 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2200 | |
| 2201 | // Delete the old constant! |
| 2202 | destroyConstant(); |
Matthijs Kooijman | ba5d7ef | 2008-07-03 07:46:41 +0000 | [diff] [blame] | 2203 | } |