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