blob: e64b0c4ff50b71c48925e3d662bd1d1021f2e855 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000030#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000031#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000032#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000033#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000034#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000035#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000036#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000040// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Andersond830eb82009-06-18 19:10:19 +000043// Becomes a no-op when multithreading is disabled.
44ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000045
Chris Lattner3462ae32001-12-03 22:26:30 +000046void Constant::destroyConstantImpl() {
47 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000049 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000050 // but they don't know that. Because we only find out when the CPV is
51 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000052 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000053 //
54 while (!use_empty()) {
55 Value *V = use_back();
56#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000057 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000058 DOUT << "While deleting: " << *this
59 << "\n\nUse still stuck around after Def is destroyed: "
60 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000061#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000062 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000063 Constant *CV = cast<Constant>(V);
64 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000065
66 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000067 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000068 }
69
70 // Value has no outstanding references it is safe to delete it now...
71 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000072}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattner23dd1f62006-10-20 00:27:06 +000074/// canTrap - Return true if evaluation of this constant could trap. This is
75/// true for things like constant expressions that could divide by zero.
76bool Constant::canTrap() const {
77 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
78 // The only thing that could possibly trap are constant exprs.
79 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
80 if (!CE) return false;
81
82 // ConstantExpr traps if any operands can trap.
83 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
84 if (getOperand(i)->canTrap())
85 return true;
86
87 // Otherwise, only specific operations can trap.
88 switch (CE->getOpcode()) {
89 default:
90 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000091 case Instruction::UDiv:
92 case Instruction::SDiv:
93 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000094 case Instruction::URem:
95 case Instruction::SRem:
96 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000097 // Div and rem can trap if the RHS is not known to be non-zero.
98 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
99 return true;
100 return false;
101 }
102}
103
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000104/// ContainsRelocations - Return true if the constant value contains relocations
105/// which cannot be resolved at compile time. Kind argument is used to filter
106/// only 'interesting' sorts of relocations.
107bool Constant::ContainsRelocations(unsigned Kind) const {
108 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
109 bool isLocal = GV->hasLocalLinkage();
110 if ((Kind & Reloc::Local) && isLocal) {
111 // Global has local linkage and 'local' kind of relocations are
112 // requested
113 return true;
114 }
115
116 if ((Kind & Reloc::Global) && !isLocal) {
117 // Global has non-local linkage and 'global' kind of relocations are
118 // requested
119 return true;
120 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000121
122 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000123 }
124
Evan Chengf9e003b2007-03-08 00:59:12 +0000125 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000126 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000127 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000128
Evan Chengf9e003b2007-03-08 00:59:12 +0000129 return false;
130}
131
Chris Lattner2105d662008-07-10 00:28:11 +0000132/// getVectorElements - This method, which is only valid on constant of vector
133/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000134/// This handles breaking down a vector undef into undef elements, etc. For
135/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000136void Constant::getVectorElements(LLVMContext &Context,
137 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000138 assert(isa<VectorType>(getType()) && "Not a vector constant!");
139
140 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
141 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
142 Elts.push_back(CV->getOperand(i));
143 return;
144 }
145
146 const VectorType *VT = cast<VectorType>(getType());
147 if (isa<ConstantAggregateZero>(this)) {
148 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000149 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000150 return;
151 }
152
Chris Lattnerc5098a22008-07-14 05:10:41 +0000153 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000154 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000155 return;
156 }
157
158 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000159}
160
161
162
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000164// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165//===----------------------------------------------------------------------===//
166
Reid Spencerb31bffe2007-02-26 23:54:03 +0000167ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000168 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000169 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170}
171
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000172//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000173// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000174//===----------------------------------------------------------------------===//
175
Daniel Dunbare974dc32009-07-17 18:33:52 +0000176#ifndef NDEBUG
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000177static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
178 if (Ty == Type::FloatTy)
179 return &APFloat::IEEEsingle;
180 if (Ty == Type::DoubleTy)
181 return &APFloat::IEEEdouble;
182 if (Ty == Type::X86_FP80Ty)
183 return &APFloat::x87DoubleExtended;
184 else if (Ty == Type::FP128Ty)
185 return &APFloat::IEEEquad;
186
187 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
188 return &APFloat::PPCDoubleDouble;
189}
Daniel Dunbare974dc32009-07-17 18:33:52 +0000190#endif
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000191
Dale Johannesend246b2c2007-08-30 00:23:21 +0000192ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
193 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000194 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
195 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196}
197
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000198bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000199 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000200}
201
Dale Johannesend246b2c2007-08-30 00:23:21 +0000202bool ConstantFP::isExactlyValue(const APFloat& V) const {
203 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000204}
205
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000206//===----------------------------------------------------------------------===//
207// ConstantXXX Classes
208//===----------------------------------------------------------------------===//
209
210
Chris Lattner3462ae32001-12-03 22:26:30 +0000211ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000212 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000213 : Constant(T, ConstantArrayVal,
214 OperandTraits<ConstantArray>::op_end(this) - V.size(),
215 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000216 assert(V.size() == T->getNumElements() &&
217 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000218 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000219 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
220 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000221 Constant *C = *I;
222 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000223 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000224 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000225 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000226 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 }
228}
229
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000230
Chris Lattner3462ae32001-12-03 22:26:30 +0000231ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000232 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000233 : Constant(T, ConstantStructVal,
234 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
235 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000236 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000237 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000238 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000239 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
240 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000241 Constant *C = *I;
242 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000243 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000244 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000245 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000246 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000247 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000248 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 }
250}
251
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252
Reid Spencerd84d35b2007-02-15 02:26:10 +0000253ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000254 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 : Constant(T, ConstantVectorVal,
256 OperandTraits<ConstantVector>::op_end(this) - V.size(),
257 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000258 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000259 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
260 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000261 Constant *C = *I;
262 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000263 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000264 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000265 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000266 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000267 }
268}
269
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000270
Gabor Greiff6caff662008-05-10 08:32:32 +0000271namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000272// We declare several classes private to this file, so use an anonymous
273// namespace
274namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000275
Gordon Henriksen14a55692007-12-10 02:14:30 +0000276/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
277/// behind the scenes to implement unary constant exprs.
278class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000279 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000280public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000281 // allocate space for exactly one operand
282 void *operator new(size_t s) {
283 return User::operator new(s, 1);
284 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000285 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000286 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
287 Op<0>() = C;
288 }
289 /// Transparently provide more efficient getOperand methods.
290 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000291};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000292
Gordon Henriksen14a55692007-12-10 02:14:30 +0000293/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
294/// behind the scenes to implement binary constant exprs.
295class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000296 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000297public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000298 // allocate space for exactly two operands
299 void *operator new(size_t s) {
300 return User::operator new(s, 2);
301 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000302 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000303 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000304 Op<0>() = C1;
305 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000306 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000307 /// Transparently provide more efficient getOperand methods.
308 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000309};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000310
Gordon Henriksen14a55692007-12-10 02:14:30 +0000311/// SelectConstantExpr - This class is private to Constants.cpp, and is used
312/// behind the scenes to implement select constant exprs.
313class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000314 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000315public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000316 // allocate space for exactly three operands
317 void *operator new(size_t s) {
318 return User::operator new(s, 3);
319 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000320 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000321 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000322 Op<0>() = C1;
323 Op<1>() = C2;
324 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000325 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000326 /// Transparently provide more efficient getOperand methods.
327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000328};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000329
Gordon Henriksen14a55692007-12-10 02:14:30 +0000330/// ExtractElementConstantExpr - This class is private to
331/// Constants.cpp, and is used behind the scenes to implement
332/// extractelement constant exprs.
333class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000334 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000335public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000336 // allocate space for exactly two operands
337 void *operator new(size_t s) {
338 return User::operator new(s, 2);
339 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000340 ExtractElementConstantExpr(Constant *C1, Constant *C2)
341 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000343 Op<0>() = C1;
344 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000345 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 /// Transparently provide more efficient getOperand methods.
347 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000348};
Robert Bocchino23004482006-01-10 19:05:34 +0000349
Gordon Henriksen14a55692007-12-10 02:14:30 +0000350/// InsertElementConstantExpr - This class is private to
351/// Constants.cpp, and is used behind the scenes to implement
352/// insertelement constant exprs.
353class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000354 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000355public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000356 // allocate space for exactly three operands
357 void *operator new(size_t s) {
358 return User::operator new(s, 3);
359 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000360 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
361 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000362 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000363 Op<0>() = C1;
364 Op<1>() = C2;
365 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000366 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000367 /// Transparently provide more efficient getOperand methods.
368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000369};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000370
Gordon Henriksen14a55692007-12-10 02:14:30 +0000371/// ShuffleVectorConstantExpr - This class is private to
372/// Constants.cpp, and is used behind the scenes to implement
373/// shufflevector constant exprs.
374class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000375 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000376public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000377 // allocate space for exactly three operands
378 void *operator new(size_t s) {
379 return User::operator new(s, 3);
380 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000381 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000382 : ConstantExpr(VectorType::get(
383 cast<VectorType>(C1->getType())->getElementType(),
384 cast<VectorType>(C3->getType())->getNumElements()),
385 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000386 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000387 Op<0>() = C1;
388 Op<1>() = C2;
389 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000390 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000391 /// Transparently provide more efficient getOperand methods.
392 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000393};
394
Dan Gohman12fce772008-05-15 19:50:34 +0000395/// ExtractValueConstantExpr - This class is private to
396/// Constants.cpp, and is used behind the scenes to implement
397/// extractvalue constant exprs.
398class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000399 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000400public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000401 // allocate space for exactly one operand
402 void *operator new(size_t s) {
403 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000404 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000405 ExtractValueConstantExpr(Constant *Agg,
406 const SmallVector<unsigned, 4> &IdxList,
407 const Type *DestTy)
408 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
409 Indices(IdxList) {
410 Op<0>() = Agg;
411 }
412
Dan Gohman7bb04502008-05-31 19:09:08 +0000413 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000414 const SmallVector<unsigned, 4> Indices;
415
Dan Gohman12fce772008-05-15 19:50:34 +0000416 /// Transparently provide more efficient getOperand methods.
417 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
418};
419
420/// InsertValueConstantExpr - This class is private to
421/// Constants.cpp, and is used behind the scenes to implement
422/// insertvalue constant exprs.
423class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000424 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000425public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000426 // allocate space for exactly one operand
427 void *operator new(size_t s) {
428 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000429 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000430 InsertValueConstantExpr(Constant *Agg, Constant *Val,
431 const SmallVector<unsigned, 4> &IdxList,
432 const Type *DestTy)
433 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
434 Indices(IdxList) {
435 Op<0>() = Agg;
436 Op<1>() = Val;
437 }
438
Dan Gohman7bb04502008-05-31 19:09:08 +0000439 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000440 const SmallVector<unsigned, 4> Indices;
441
Dan Gohman12fce772008-05-15 19:50:34 +0000442 /// Transparently provide more efficient getOperand methods.
443 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
444};
445
446
Gordon Henriksen14a55692007-12-10 02:14:30 +0000447/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
448/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000449class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000450 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000451 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000452public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000453 static GetElementPtrConstantExpr *Create(Constant *C,
454 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000455 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000456 return
457 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000458 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000459 /// Transparently provide more efficient getOperand methods.
460 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000461};
462
463// CompareConstantExpr - This class is private to Constants.cpp, and is used
464// behind the scenes to implement ICmp and FCmp constant expressions. This is
465// needed in order to store the predicate value for these instructions.
466struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000467 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
468 // allocate space for exactly two operands
469 void *operator new(size_t s) {
470 return User::operator new(s, 2);
471 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000473 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
474 unsigned short pred, Constant* LHS, Constant* RHS)
475 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000476 Op<0>() = LHS;
477 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000478 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000479 /// Transparently provide more efficient getOperand methods.
480 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000481};
482
483} // end anonymous namespace
484
Gabor Greiff6caff662008-05-10 08:32:32 +0000485template <>
486struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
487};
488DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
489
490template <>
491struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
492};
493DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
494
495template <>
496struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
497};
498DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
499
500template <>
501struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
502};
503DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
504
505template <>
506struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
507};
508DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
509
510template <>
511struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
512};
513DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
514
Dan Gohman12fce772008-05-15 19:50:34 +0000515template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000516struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000517};
Dan Gohman12fce772008-05-15 19:50:34 +0000518DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
519
520template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000521struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000522};
Dan Gohman12fce772008-05-15 19:50:34 +0000523DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
524
Gabor Greiff6caff662008-05-10 08:32:32 +0000525template <>
526struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
527};
528
529GetElementPtrConstantExpr::GetElementPtrConstantExpr
530 (Constant *C,
531 const std::vector<Constant*> &IdxList,
532 const Type *DestTy)
533 : ConstantExpr(DestTy, Instruction::GetElementPtr,
534 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
535 - (IdxList.size()+1),
536 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000537 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000538 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000539 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000540}
541
542DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
543
544
545template <>
546struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
547};
548DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
549
550
551} // End llvm namespace
552
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000553
554// Utility function for determining if a ConstantExpr is a CastOp or not. This
555// can't be inline because we don't want to #include Instruction.h into
556// Constant.h
557bool ConstantExpr::isCast() const {
558 return Instruction::isCast(getOpcode());
559}
560
Reid Spenceree3c9912006-12-04 05:19:50 +0000561bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000562 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000563}
564
Dan Gohman1ecaf452008-05-31 00:58:22 +0000565bool ConstantExpr::hasIndices() const {
566 return getOpcode() == Instruction::ExtractValue ||
567 getOpcode() == Instruction::InsertValue;
568}
569
570const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
571 if (const ExtractValueConstantExpr *EVCE =
572 dyn_cast<ExtractValueConstantExpr>(this))
573 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000574
575 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000576}
577
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000578unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000579 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000580 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000581 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000582}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000583
Chris Lattner7c1018a2006-07-14 19:37:40 +0000584/// getWithOperandReplaced - Return a constant expression identical to this
585/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000586Constant *
587ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000588 assert(OpNo < getNumOperands() && "Operand num is out of range!");
589 assert(Op->getType() == getOperand(OpNo)->getType() &&
590 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000591 if (getOperand(OpNo) == Op)
592 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000593
Chris Lattner227816342006-07-14 22:20:01 +0000594 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000595 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000596 case Instruction::Trunc:
597 case Instruction::ZExt:
598 case Instruction::SExt:
599 case Instruction::FPTrunc:
600 case Instruction::FPExt:
601 case Instruction::UIToFP:
602 case Instruction::SIToFP:
603 case Instruction::FPToUI:
604 case Instruction::FPToSI:
605 case Instruction::PtrToInt:
606 case Instruction::IntToPtr:
607 case Instruction::BitCast:
608 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000609 case Instruction::Select:
610 Op0 = (OpNo == 0) ? Op : getOperand(0);
611 Op1 = (OpNo == 1) ? Op : getOperand(1);
612 Op2 = (OpNo == 2) ? Op : getOperand(2);
613 return ConstantExpr::getSelect(Op0, Op1, Op2);
614 case Instruction::InsertElement:
615 Op0 = (OpNo == 0) ? Op : getOperand(0);
616 Op1 = (OpNo == 1) ? Op : getOperand(1);
617 Op2 = (OpNo == 2) ? Op : getOperand(2);
618 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
619 case Instruction::ExtractElement:
620 Op0 = (OpNo == 0) ? Op : getOperand(0);
621 Op1 = (OpNo == 1) ? Op : getOperand(1);
622 return ConstantExpr::getExtractElement(Op0, Op1);
623 case Instruction::ShuffleVector:
624 Op0 = (OpNo == 0) ? Op : getOperand(0);
625 Op1 = (OpNo == 1) ? Op : getOperand(1);
626 Op2 = (OpNo == 2) ? Op : getOperand(2);
627 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000628 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000629 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000630 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000631 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000632 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000633 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000634 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000635 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000636 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000637 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000638 default:
639 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000640 Op0 = (OpNo == 0) ? Op : getOperand(0);
641 Op1 = (OpNo == 1) ? Op : getOperand(1);
642 return ConstantExpr::get(getOpcode(), Op0, Op1);
643 }
644}
645
646/// getWithOperands - This returns the current constant expression with the
647/// operands replaced with the specified values. The specified operands must
648/// match count and type with the existing ones.
649Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000650getWithOperands(Constant* const *Ops, unsigned NumOps) const {
651 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000652 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000653 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000654 assert(Ops[i]->getType() == getOperand(i)->getType() &&
655 "Operand type mismatch!");
656 AnyChange |= Ops[i] != getOperand(i);
657 }
658 if (!AnyChange) // No operands changed, return self.
659 return const_cast<ConstantExpr*>(this);
660
661 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000662 case Instruction::Trunc:
663 case Instruction::ZExt:
664 case Instruction::SExt:
665 case Instruction::FPTrunc:
666 case Instruction::FPExt:
667 case Instruction::UIToFP:
668 case Instruction::SIToFP:
669 case Instruction::FPToUI:
670 case Instruction::FPToSI:
671 case Instruction::PtrToInt:
672 case Instruction::IntToPtr:
673 case Instruction::BitCast:
674 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000675 case Instruction::Select:
676 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
677 case Instruction::InsertElement:
678 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
679 case Instruction::ExtractElement:
680 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
681 case Instruction::ShuffleVector:
682 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000683 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000684 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000685 case Instruction::ICmp:
686 case Instruction::FCmp:
687 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000688 default:
689 assert(getNumOperands() == 2 && "Must be binary operator?");
690 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000691 }
692}
693
Chris Lattner2f7c9632001-06-06 20:29:01 +0000694
695//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000696// isValueValidForType implementations
697
Reid Spencere7334722006-12-19 01:28:19 +0000698bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000699 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000700 if (Ty == Type::Int1Ty)
701 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000702 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000703 return true; // always true, has to fit in largest type
704 uint64_t Max = (1ll << NumBits) - 1;
705 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000706}
707
Reid Spencere0fc4df2006-10-20 07:07:24 +0000708bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000709 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000710 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000711 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000712 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000713 return true; // always true, has to fit in largest type
714 int64_t Min = -(1ll << (NumBits-1));
715 int64_t Max = (1ll << (NumBits-1)) - 1;
716 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717}
718
Dale Johannesend246b2c2007-08-30 00:23:21 +0000719bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
720 // convert modifies in place, so make a copy.
721 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000722 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000723 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724 default:
725 return false; // These can't be represented as floating point!
726
Dale Johannesend246b2c2007-08-30 00:23:21 +0000727 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000728 case Type::FloatTyID: {
729 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
730 return true;
731 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
732 return !losesInfo;
733 }
734 case Type::DoubleTyID: {
735 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
736 &Val2.getSemantics() == &APFloat::IEEEdouble)
737 return true;
738 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
739 return !losesInfo;
740 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000741 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000742 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
743 &Val2.getSemantics() == &APFloat::IEEEdouble ||
744 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000745 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000746 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
747 &Val2.getSemantics() == &APFloat::IEEEdouble ||
748 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000749 case Type::PPC_FP128TyID:
750 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
751 &Val2.getSemantics() == &APFloat::IEEEdouble ||
752 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000753 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000754}
Chris Lattner9655e542001-07-20 19:16:02 +0000755
Chris Lattner49d855c2001-09-07 16:46:31 +0000756//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000757// Factory Function Implementation
758
Gabor Greiff6caff662008-05-10 08:32:32 +0000759
760// The number of operands for each ConstantCreator::create method is
761// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000762// ConstantCreator - A class that is used to create constants by
763// ValueMap*. This class should be partially specialized if there is
764// something strange that needs to be done to interface to the ctor for the
765// constant.
766//
Chris Lattner189d19f2003-11-21 20:23:48 +0000767namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000768 template<class ValType>
769 struct ConstantTraits;
770
771 template<typename T, typename Alloc>
772 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
773 static unsigned uses(const std::vector<T, Alloc>& v) {
774 return v.size();
775 }
776 };
777
Chris Lattner189d19f2003-11-21 20:23:48 +0000778 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000779 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000780 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000781 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000782 }
783 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000784
Chris Lattner189d19f2003-11-21 20:23:48 +0000785 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000786 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000787 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000788 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000789 }
790 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000791
Chris Lattner935aa922005-10-04 17:48:46 +0000792 template<class ValType, class TypeClass, class ConstantClass,
793 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000794 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000795 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000796 typedef std::pair<const Type*, ValType> MapKey;
797 typedef std::map<MapKey, Constant *> MapTy;
798 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
799 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000800 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000801 /// Map - This is the main map from the element descriptor to the Constants.
802 /// This is the primary way we avoid creating two of the same shape
803 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000804 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000805
806 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
807 /// from the constants to their element in Map. This is important for
808 /// removal of constants from the array, which would otherwise have to scan
809 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000810 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000811
Jim Laskeyc03caef2006-07-17 17:38:29 +0000812 /// AbstractTypeMap - Map for abstract type constants.
813 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000814 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000815
816 /// ValueMapLock - Mutex for this map.
817 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000818
Chris Lattner98fa07b2003-05-23 20:03:32 +0000819 public:
Owen Anderson61794042009-06-17 20:10:08 +0000820 // NOTE: This function is not locked. It is the caller's responsibility
821 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000822 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000823
824 /// InsertOrGetItem - Return an iterator for the specified element.
825 /// If the element exists in the map, the returned iterator points to the
826 /// entry and Exists=true. If not, the iterator points to the newly
827 /// inserted entry and returns Exists=false. Newly inserted entries have
828 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000829 /// NOTE: This function is not locked. It is the caller's responsibility
830 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000831 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
832 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000833 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000834 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000835 Exists = !IP.second;
836 return IP.first;
837 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000838
Chris Lattner935aa922005-10-04 17:48:46 +0000839private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000840 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000841 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000842 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000843 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
844 IMI->second->second == CP &&
845 "InverseMap corrupt!");
846 return IMI->second;
847 }
848
Jim Laskeyc03caef2006-07-17 17:38:29 +0000849 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000850 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
851 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000852 if (I == Map.end() || I->second != CP) {
853 // FIXME: This should not use a linear scan. If this gets to be a
854 // performance problem, someone should look at this.
855 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
856 /* empty */;
857 }
Chris Lattner935aa922005-10-04 17:48:46 +0000858 return I;
859 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000860
861 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
862 typename MapTy::iterator I) {
863 ConstantClass* Result =
864 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
865
866 assert(Result->getType() == Ty && "Type specified is not correct!");
867 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
868
869 if (HasLargeKey) // Remember the reverse mapping if needed.
870 InverseMap.insert(std::make_pair(Result, I));
871
872 // If the type of the constant is abstract, make sure that an entry
873 // exists for it in the AbstractTypeMap.
874 if (Ty->isAbstract()) {
875 typename AbstractTypeMapTy::iterator TI =
876 AbstractTypeMap.find(Ty);
877
878 if (TI == AbstractTypeMap.end()) {
879 // Add ourselves to the ATU list of the type.
880 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
881
882 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
883 }
884 }
885
886 return Result;
887 }
Chris Lattner935aa922005-10-04 17:48:46 +0000888public:
889
Chris Lattnerb64419a2005-10-03 22:51:37 +0000890 /// getOrCreate - Return the specified constant from the map, creating it if
891 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000892 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000893 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +0000894 MapKey Lookup(Ty, V);
895 ConstantClass* Result = 0;
896
897 typename MapTy::iterator I = Map.find(Lookup);
898 // Is it in the map?
899 if (I != Map.end())
900 Result = static_cast<ConstantClass *>(I->second);
901
902 if (!Result) {
903 // If no preexisting value, create one now...
904 Result = Create(Ty, V, I);
905 }
906
907 return Result;
908 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000909
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000910 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000911 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000912 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000913 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000914 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000915
Chris Lattner935aa922005-10-04 17:48:46 +0000916 if (HasLargeKey) // Remember the reverse mapping if needed.
917 InverseMap.erase(CP);
918
Chris Lattnerb50d1352003-10-05 00:17:43 +0000919 // Now that we found the entry, make sure this isn't the entry that
920 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000921 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000922 if (Ty->isAbstract()) {
923 assert(AbstractTypeMap.count(Ty) &&
924 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000925 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000926 if (ATMEntryIt == I) {
927 // Yes, we are removing the representative entry for this type.
928 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000929 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000930
Chris Lattnerb50d1352003-10-05 00:17:43 +0000931 // First check the entry before this one...
932 if (TmpIt != Map.begin()) {
933 --TmpIt;
934 if (TmpIt->first.first != Ty) // Not the same type, move back...
935 ++TmpIt;
936 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000937
Chris Lattnerb50d1352003-10-05 00:17:43 +0000938 // If we didn't find the same type, try to move forward...
939 if (TmpIt == ATMEntryIt) {
940 ++TmpIt;
941 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
942 --TmpIt; // No entry afterwards with the same type
943 }
944
945 // If there is another entry in the map of the same abstract type,
946 // update the AbstractTypeMap entry now.
947 if (TmpIt != ATMEntryIt) {
948 ATMEntryIt = TmpIt;
949 } else {
950 // Otherwise, we are removing the last instance of this type
951 // from the table. Remove from the ATM, and from user list.
952 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
953 AbstractTypeMap.erase(Ty);
954 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000955 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000956 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000957
Chris Lattnerb50d1352003-10-05 00:17:43 +0000958 Map.erase(I);
959 }
960
Chris Lattner3b793c62005-10-04 21:35:50 +0000961
962 /// MoveConstantToNewSlot - If we are about to change C to be the element
963 /// specified by I, update our internal data structures to reflect this
964 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +0000965 /// NOTE: This function is not locked. It is the responsibility of the
966 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000967 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000968 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000969 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000970 assert(OldI != Map.end() && "Constant not found in constant table!");
971 assert(OldI->second == C && "Didn't find correct element?");
972
973 // If this constant is the representative element for its abstract type,
974 // update the AbstractTypeMap so that the representative element is I.
975 if (C->getType()->isAbstract()) {
976 typename AbstractTypeMapTy::iterator ATI =
977 AbstractTypeMap.find(C->getType());
978 assert(ATI != AbstractTypeMap.end() &&
979 "Abstract type not in AbstractTypeMap?");
980 if (ATI->second == OldI)
981 ATI->second = I;
982 }
983
984 // Remove the old entry from the map.
985 Map.erase(OldI);
986
987 // Update the inverse map so that we know that this constant is now
988 // located at descriptor I.
989 if (HasLargeKey) {
990 assert(I->second == C && "Bad inversemap entry!");
991 InverseMap[C] = I;
992 }
993 }
994
Chris Lattnerb50d1352003-10-05 00:17:43 +0000995 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000996 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000997 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000998 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000999
1000 assert(I != AbstractTypeMap.end() &&
1001 "Abstract type not in AbstractTypeMap?");
1002
1003 // Convert a constant at a time until the last one is gone. The last one
1004 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1005 // eliminated eventually.
1006 do {
1007 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001008 TypeClass>::convert(
1009 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001010 cast<TypeClass>(NewTy));
1011
Jim Laskeyc03caef2006-07-17 17:38:29 +00001012 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001013 } while (I != AbstractTypeMap.end());
1014 }
1015
1016 // If the type became concrete without being refined to any other existing
1017 // type, we just remove ourselves from the ATU list.
1018 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001019 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001020 }
1021
1022 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001023 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001024 }
1025 };
1026}
1027
Dan Gohman92b551b2009-03-03 02:55:14 +00001028/// destroyConstant - Remove the constant from the constant table...
1029///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001030void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001031 // Implicitly locked.
Owen Anderson39ede7b2009-07-21 20:13:12 +00001032 getType()->getContext().erase(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001033 destroyConstantImpl();
1034}
1035
Dan Gohman92b551b2009-03-03 02:55:14 +00001036/// destroyConstant - Remove the constant from the constant table...
1037///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001038void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001039 // Implicitly locked.
Owen Anderson3d344922009-07-21 20:55:28 +00001040 getType()->getContext().erase(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001041 destroyConstantImpl();
1042}
1043
Reid Spencer2546b762007-01-26 07:37:34 +00001044/// isString - This method returns true if the array is an array of i8, and
1045/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001046bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001047 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001048 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001049 return false;
1050 // Check the elements to make sure they are all integers, not constant
1051 // expressions.
1052 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1053 if (!isa<ConstantInt>(getOperand(i)))
1054 return false;
1055 return true;
1056}
1057
Evan Cheng3763c5b2006-10-26 19:15:05 +00001058/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001059/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001060/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001061bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001062 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001063 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001064 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001065
Evan Chenge974da62006-10-26 21:48:03 +00001066 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001067 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001068 return false;
1069 // Other elements must be non-null integers.
1070 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1071 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001072 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001073 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001074 return false;
1075 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001076 return true;
1077}
1078
1079
Dan Gohman92b551b2009-03-03 02:55:14 +00001080/// getAsString - If the sub-element type of this array is i8
1081/// then this method converts the array to an std::string and returns it.
1082/// Otherwise, it asserts out.
1083///
Chris Lattner81fabb02002-08-26 17:53:56 +00001084std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001085 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001086 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001087 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001088 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001089 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001090 return Result;
1091}
1092
1093
Chris Lattner3462ae32001-12-03 22:26:30 +00001094//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001095//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001096
Chris Lattner189d19f2003-11-21 20:23:48 +00001097namespace llvm {
1098 template<>
1099 struct ConvertConstantType<ConstantStruct, StructType> {
1100 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1101 // Make everyone now use a constant of the new type...
1102 std::vector<Constant*> C;
1103 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1104 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001105 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001106 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001107
Chris Lattner189d19f2003-11-21 20:23:48 +00001108 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001109 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001110 }
1111 };
1112}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001113
Chris Lattner8760ec72005-10-04 01:17:50 +00001114typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001115 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001116static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001117
Chris Lattner3e650af2004-08-04 04:48:01 +00001118static std::vector<Constant*> getValType(ConstantStruct *CS) {
1119 std::vector<Constant*> Elements;
1120 Elements.reserve(CS->getNumOperands());
1121 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1122 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1123 return Elements;
1124}
1125
Chris Lattner015e8212004-02-15 04:14:47 +00001126Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001127 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001128 // Create a ConstantAggregateZero value if all elements are zeros...
1129 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001130 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001131 // Implicitly locked.
1132 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001133
Owen Anderson39ede7b2009-07-21 20:13:12 +00001134 return Ty->getContext().getConstantAggregateZero(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001135}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001136
Chris Lattnerd7a73302001-10-13 06:57:33 +00001137// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001138//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001139void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001140 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001141 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001142 destroyConstantImpl();
1143}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001144
Reid Spencerd84d35b2007-02-15 02:26:10 +00001145//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001146//
1147namespace llvm {
1148 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001149 struct ConvertConstantType<ConstantVector, VectorType> {
1150 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001151 // Make everyone now use a constant of the new type...
1152 std::vector<Constant*> C;
1153 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1154 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001155 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001156 assert(New != OldC && "Didn't replace constant??");
1157 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001158 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001159 }
1160 };
1161}
1162
Reid Spencerd84d35b2007-02-15 02:26:10 +00001163static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001164 std::vector<Constant*> Elements;
1165 Elements.reserve(CP->getNumOperands());
1166 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1167 Elements.push_back(CP->getOperand(i));
1168 return Elements;
1169}
1170
Reid Spencerd84d35b2007-02-15 02:26:10 +00001171static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001172 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001173
Reid Spencerd84d35b2007-02-15 02:26:10 +00001174Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001175 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001176 assert(!V.empty() && "Vectors can't be empty");
1177 // If this is an all-undef or alll-zero vector, return a
1178 // ConstantAggregateZero or UndefValue.
1179 Constant *C = V[0];
1180 bool isZero = C->isNullValue();
1181 bool isUndef = isa<UndefValue>(C);
1182
1183 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001184 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001185 if (V[i] != C) {
1186 isZero = isUndef = false;
1187 break;
1188 }
Brian Gaeke02209042004-08-20 06:00:58 +00001189 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001190
1191 if (isZero)
Owen Anderson39ede7b2009-07-21 20:13:12 +00001192 return Ty->getContext().getConstantAggregateZero(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001193 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001194 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001195
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001196 // Implicitly locked.
1197 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001198}
1199
Brian Gaeke02209042004-08-20 06:00:58 +00001200// destroyConstant - Remove the constant from the constant table...
1201//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001202void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001203 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001204 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001205 destroyConstantImpl();
1206}
1207
Dan Gohman30978072007-05-24 14:36:04 +00001208/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001209/// is set to all ones.
1210/// @returns true iff this constant's emements are all set to all ones.
1211/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001212bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001213 // Check out first element.
1214 const Constant *Elt = getOperand(0);
1215 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1216 if (!CI || !CI->isAllOnesValue()) return false;
1217 // Then make sure all remaining elements point to the same value.
1218 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1219 if (getOperand(I) != Elt) return false;
1220 }
1221 return true;
1222}
1223
Dan Gohman07159202007-10-17 17:51:30 +00001224/// getSplatValue - If this is a splat constant, where all of the
1225/// elements have the same value, return that value. Otherwise return null.
1226Constant *ConstantVector::getSplatValue() {
1227 // Check out first element.
1228 Constant *Elt = getOperand(0);
1229 // Then make sure all remaining elements point to the same value.
1230 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1231 if (getOperand(I) != Elt) return 0;
1232 return Elt;
1233}
1234
Chris Lattner3462ae32001-12-03 22:26:30 +00001235//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001236//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001237
Chris Lattner189d19f2003-11-21 20:23:48 +00001238namespace llvm {
1239 // ConstantPointerNull does not take extra "value" argument...
1240 template<class ValType>
1241 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1242 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1243 return new ConstantPointerNull(Ty);
1244 }
1245 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001246
Chris Lattner189d19f2003-11-21 20:23:48 +00001247 template<>
1248 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1249 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1250 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001251 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001252 assert(New != OldC && "Didn't replace constant??");
1253 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001254 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001255 }
1256 };
1257}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001258
Chris Lattner69edc982006-09-28 00:35:06 +00001259static ManagedStatic<ValueMap<char, PointerType,
1260 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001261
Chris Lattner3e650af2004-08-04 04:48:01 +00001262static char getValType(ConstantPointerNull *) {
1263 return 0;
1264}
1265
1266
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001267ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001268 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001269 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001270}
1271
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001272// destroyConstant - Remove the constant from the constant table...
1273//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001274void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001275 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001276 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001277 destroyConstantImpl();
1278}
1279
1280
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001281//---- UndefValue::get() implementation...
1282//
1283
1284namespace llvm {
1285 // UndefValue does not take extra "value" argument...
1286 template<class ValType>
1287 struct ConstantCreator<UndefValue, Type, ValType> {
1288 static UndefValue *create(const Type *Ty, const ValType &V) {
1289 return new UndefValue(Ty);
1290 }
1291 };
1292
1293 template<>
1294 struct ConvertConstantType<UndefValue, Type> {
1295 static void convert(UndefValue *OldC, const Type *NewTy) {
1296 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001297 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001298 assert(New != OldC && "Didn't replace constant??");
1299 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001300 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001301 }
1302 };
1303}
1304
Chris Lattner69edc982006-09-28 00:35:06 +00001305static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001306
1307static char getValType(UndefValue *) {
1308 return 0;
1309}
1310
1311
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001312UndefValue *UndefValue::get(const Type *Ty) {
1313 // Implicitly locked.
1314 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001315}
1316
1317// destroyConstant - Remove the constant from the constant table.
1318//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001319void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001320 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001321 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001322 destroyConstantImpl();
1323}
1324
Nick Lewycky49f89192009-04-04 07:22:01 +00001325//---- MDString::get() implementation
1326//
1327
1328MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001329 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001330 StrBegin(begin), StrEnd(end) {}
1331
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001332void MDString::destroyConstant() {
Owen Anderson69ab4162009-07-16 22:11:26 +00001333 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001334 destroyConstantImpl();
1335}
1336
1337//---- MDNode::get() implementation
1338//
1339
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001340MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001341 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001342 for (unsigned i = 0; i != NumVals; ++i)
1343 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001344}
1345
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001346void MDNode::Profile(FoldingSetNodeID &ID) const {
1347 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001348 ID.AddPointer(*I);
1349}
1350
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001351void MDNode::destroyConstant() {
Owen Anderson4118dde2009-07-16 23:44:30 +00001352 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001353 destroyConstantImpl();
1354}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001355
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001356//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001357//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001358
Dan Gohmand78c4002008-05-13 00:00:25 +00001359namespace {
1360
Reid Spenceree3c9912006-12-04 05:19:50 +00001361struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001362 typedef SmallVector<unsigned, 4> IndexList;
1363
1364 ExprMapKeyType(unsigned opc,
1365 const std::vector<Constant*> &ops,
1366 unsigned short pred = 0,
1367 const IndexList &inds = IndexList())
1368 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001369 uint16_t opcode;
1370 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001371 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001372 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001373 bool operator==(const ExprMapKeyType& that) const {
1374 return this->opcode == that.opcode &&
1375 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001376 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001377 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001378 }
1379 bool operator<(const ExprMapKeyType & that) const {
1380 return this->opcode < that.opcode ||
1381 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1382 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001383 this->operands < that.operands) ||
1384 (this->opcode == that.opcode && this->predicate == that.predicate &&
1385 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001386 }
1387
1388 bool operator!=(const ExprMapKeyType& that) const {
1389 return !(*this == that);
1390 }
1391};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001392
Dan Gohmand78c4002008-05-13 00:00:25 +00001393}
1394
Chris Lattner189d19f2003-11-21 20:23:48 +00001395namespace llvm {
1396 template<>
1397 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001398 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1399 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001400 if (Instruction::isCast(V.opcode))
1401 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1402 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001403 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001404 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1405 if (V.opcode == Instruction::Select)
1406 return new SelectConstantExpr(V.operands[0], V.operands[1],
1407 V.operands[2]);
1408 if (V.opcode == Instruction::ExtractElement)
1409 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1410 if (V.opcode == Instruction::InsertElement)
1411 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1412 V.operands[2]);
1413 if (V.opcode == Instruction::ShuffleVector)
1414 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1415 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001416 if (V.opcode == Instruction::InsertValue)
1417 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1418 V.indices, Ty);
1419 if (V.opcode == Instruction::ExtractValue)
1420 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001421 if (V.opcode == Instruction::GetElementPtr) {
1422 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001423 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001424 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001425
Reid Spenceree3c9912006-12-04 05:19:50 +00001426 // The compare instructions are weird. We have to encode the predicate
1427 // value and it is combined with the instruction opcode by multiplying
1428 // the opcode by one hundred. We must decode this to get the predicate.
1429 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001430 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001431 V.operands[0], V.operands[1]);
1432 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001433 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1434 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001435 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001436 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001437 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001438 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001439
Chris Lattner189d19f2003-11-21 20:23:48 +00001440 template<>
1441 struct ConvertConstantType<ConstantExpr, Type> {
1442 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1443 Constant *New;
1444 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001445 case Instruction::Trunc:
1446 case Instruction::ZExt:
1447 case Instruction::SExt:
1448 case Instruction::FPTrunc:
1449 case Instruction::FPExt:
1450 case Instruction::UIToFP:
1451 case Instruction::SIToFP:
1452 case Instruction::FPToUI:
1453 case Instruction::FPToSI:
1454 case Instruction::PtrToInt:
1455 case Instruction::IntToPtr:
1456 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001457 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001458 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001459 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001460 case Instruction::Select:
1461 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1462 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001463 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001464 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001465 default:
1466 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001467 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001468 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001469 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001470 break;
1471 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001472 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001473 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001474 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001475 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001476 break;
1477 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001478
Chris Lattner189d19f2003-11-21 20:23:48 +00001479 assert(New != OldC && "Didn't replace constant??");
1480 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001481 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001482 }
1483 };
1484} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001485
1486
Chris Lattner3e650af2004-08-04 04:48:01 +00001487static ExprMapKeyType getValType(ConstantExpr *CE) {
1488 std::vector<Constant*> Operands;
1489 Operands.reserve(CE->getNumOperands());
1490 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1491 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001492 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001493 CE->isCompare() ? CE->getPredicate() : 0,
1494 CE->hasIndices() ?
1495 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001496}
1497
Chris Lattner69edc982006-09-28 00:35:06 +00001498static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1499 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001500
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001501/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001502/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001503static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001504 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001505 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001507 if (Constant *FC =
1508 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001509 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001510
Vikram S. Adve4c485332002-07-15 18:19:33 +00001511 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001512 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001513 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001514
Owen Anderson61794042009-06-17 20:10:08 +00001515 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001516 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001517}
Reid Spencerf37dc652006-12-05 19:14:13 +00001518
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001519Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001520 Instruction::CastOps opc = Instruction::CastOps(oc);
1521 assert(Instruction::isCast(opc) && "opcode out of range");
1522 assert(C && Ty && "Null arguments to getCast");
1523 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1524
1525 switch (opc) {
1526 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001527 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001528 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001529 case Instruction::Trunc: return getTrunc(C, Ty);
1530 case Instruction::ZExt: return getZExt(C, Ty);
1531 case Instruction::SExt: return getSExt(C, Ty);
1532 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1533 case Instruction::FPExt: return getFPExtend(C, Ty);
1534 case Instruction::UIToFP: return getUIToFP(C, Ty);
1535 case Instruction::SIToFP: return getSIToFP(C, Ty);
1536 case Instruction::FPToUI: return getFPToUI(C, Ty);
1537 case Instruction::FPToSI: return getFPToSI(C, Ty);
1538 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1539 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1540 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001541 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001542 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001543}
1544
Reid Spencer5c140882006-12-04 20:17:56 +00001545Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001546 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001547 return getCast(Instruction::BitCast, C, Ty);
1548 return getCast(Instruction::ZExt, C, Ty);
1549}
1550
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001551Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001552 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001553 return getCast(Instruction::BitCast, C, Ty);
1554 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001555}
1556
1557Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001558 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001559 return getCast(Instruction::BitCast, C, Ty);
1560 return getCast(Instruction::Trunc, C, Ty);
1561}
1562
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001563Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001564 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001565 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001566
Chris Lattner03c49532007-01-15 02:27:26 +00001567 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001568 return getCast(Instruction::PtrToInt, S, Ty);
1569 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001570}
1571
Reid Spencer56521c42006-12-12 00:51:07 +00001572Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1573 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001574 assert(C->getType()->isIntOrIntVector() &&
1575 Ty->isIntOrIntVector() && "Invalid cast");
1576 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1577 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001578 Instruction::CastOps opcode =
1579 (SrcBits == DstBits ? Instruction::BitCast :
1580 (SrcBits > DstBits ? Instruction::Trunc :
1581 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1582 return getCast(opcode, C, Ty);
1583}
1584
1585Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001586 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001587 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001588 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1589 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001590 if (SrcBits == DstBits)
1591 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001592 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001593 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001594 return getCast(opcode, C, Ty);
1595}
1596
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001597Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001598#ifndef NDEBUG
1599 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1600 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1601#endif
1602 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1603 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1604 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1605 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001606 "SrcTy must be larger than DestTy for Trunc!");
1607
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001608 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609}
1610
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001611Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001612#ifndef NDEBUG
1613 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1614 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1615#endif
1616 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1617 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1618 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1619 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001620 "SrcTy must be smaller than DestTy for SExt!");
1621
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001622 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001623}
1624
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001625Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001626#ifndef NDEBUG
1627 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1628 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1629#endif
1630 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1631 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1632 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1633 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001634 "SrcTy must be smaller than DestTy for ZExt!");
1635
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001636 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001637}
1638
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001639Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001640#ifndef NDEBUG
1641 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1642 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1643#endif
1644 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1645 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1646 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001647 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001648 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001649}
1650
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001651Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001652#ifndef NDEBUG
1653 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1654 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1655#endif
1656 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1657 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1658 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001659 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001660 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001661}
1662
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001663Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001664#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001665 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1666 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001667#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001668 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1669 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1670 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001671 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001672}
1673
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001674Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001675#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001676 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1677 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001678#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001679 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1680 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001681 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001682 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001683}
1684
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001685Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001686#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001687 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1688 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001689#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001690 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1691 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1692 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001693 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001694}
1695
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001696Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001697#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001698 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1699 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001700#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001701 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1702 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1703 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001704 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001705}
1706
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001707Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001708 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001709 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001710 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001711}
1712
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001713Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001714 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001716 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001717}
1718
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001719Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001720 // BitCast implies a no-op cast of type only. No bits change. However, you
1721 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001722#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001723 const Type *SrcTy = C->getType();
1724 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001725 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001726
1727 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1728 // or nonptr->ptr). For all the other types, the cast is okay if source and
1729 // destination bit widths are identical.
1730 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1731 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001732#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001733 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001734
1735 // It is common to ask for a bitcast of a value to its own type, handle this
1736 // speedily.
1737 if (C->getType() == DstTy) return C;
1738
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001739 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001740}
1741
Chris Lattnerb50d1352003-10-05 00:17:43 +00001742Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001743 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001744 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001745 assert(Opcode >= Instruction::BinaryOpsBegin &&
1746 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001747 "Invalid opcode in binary constant expression");
1748 assert(C1->getType() == C2->getType() &&
1749 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001750
Reid Spencer542964f2007-01-11 18:21:29 +00001751 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001752 if (Constant *FC = ConstantFoldBinaryInstruction(
1753 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001754 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001755
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001756 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001757 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001758
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001759 // Implicitly locked.
1760 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001761}
1762
Reid Spencer266e42b2006-12-23 06:05:41 +00001763Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001764 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001765 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001766 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001767 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1768 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1769 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1770 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1771 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1772 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001773 return getFCmp(predicate, C1, C2);
1774
Nate Begemanc96e2e42008-07-25 17:35:37 +00001775 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1776 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1777 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1778 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001779 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001780 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001781}
1782
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001783Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001784 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1785 if (C1->getType()->isFPOrFPVector()) {
1786 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1787 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1788 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1789 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001790#ifndef NDEBUG
1791 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001793 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001794 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001795 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001796 assert(C1->getType()->isIntOrIntVector() &&
1797 "Tried to create an integer operation on a non-integer type!");
1798 break;
1799 case Instruction::FAdd:
1800 case Instruction::FSub:
1801 case Instruction::FMul:
1802 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1803 assert(C1->getType()->isFPOrFPVector() &&
1804 "Tried to create a floating-point operation on a "
1805 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001806 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001807 case Instruction::UDiv:
1808 case Instruction::SDiv:
1809 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001810 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001811 "Tried to create an arithmetic operation on a non-arithmetic type!");
1812 break;
1813 case Instruction::FDiv:
1814 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001815 assert(C1->getType()->isFPOrFPVector() &&
1816 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001817 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001818 case Instruction::URem:
1819 case Instruction::SRem:
1820 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001821 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001822 "Tried to create an arithmetic operation on a non-arithmetic type!");
1823 break;
1824 case Instruction::FRem:
1825 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001826 assert(C1->getType()->isFPOrFPVector() &&
1827 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001828 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001829 case Instruction::And:
1830 case Instruction::Or:
1831 case Instruction::Xor:
1832 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001833 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001834 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001835 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001836 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001837 case Instruction::LShr:
1838 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001839 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001840 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001841 "Tried to create a shift operation on a non-integer type!");
1842 break;
1843 default:
1844 break;
1845 }
1846#endif
1847
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001848 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001849}
1850
Reid Spencer266e42b2006-12-23 06:05:41 +00001851Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001852 Constant *C1, Constant *C2) {
1853 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001854 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001855}
1856
Chris Lattner6e415c02004-03-12 05:54:04 +00001857Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001858 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001859 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001860
1861 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001862 if (Constant *SC = ConstantFoldSelectInstruction(
1863 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001864 return SC; // Fold common cases
1865
1866 std::vector<Constant*> argVec(3, C);
1867 argVec[1] = V1;
1868 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001869 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001870
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001871 // Implicitly locked.
1872 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001873}
1874
Chris Lattnerb50d1352003-10-05 00:17:43 +00001875Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001876 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001877 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001878 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1879 Idxs+NumIdx) ==
1880 cast<PointerType>(ReqTy)->getElementType() &&
1881 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001882
Owen Anderson53a52212009-07-13 04:09:18 +00001883 if (Constant *FC = ConstantFoldGetElementPtr(
1884 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001885 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001886
Chris Lattnerb50d1352003-10-05 00:17:43 +00001887 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001888 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001889 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001890 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001891 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001892 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001893 for (unsigned i = 0; i != NumIdx; ++i)
1894 ArgVec.push_back(cast<Constant>(Idxs[i]));
1895 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001896
1897 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001898 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001899}
1900
Chris Lattner302116a2007-01-31 04:40:28 +00001901Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001902 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001903 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001904 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001905 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001906 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001907 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001908 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001909}
1910
Chris Lattner302116a2007-01-31 04:40:28 +00001911Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001912 unsigned NumIdx) {
1913 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001914}
1915
Chris Lattner302116a2007-01-31 04:40:28 +00001916
Reid Spenceree3c9912006-12-04 05:19:50 +00001917Constant *
1918ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1919 assert(LHS->getType() == RHS->getType());
1920 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1921 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1922
Owen Anderson53a52212009-07-13 04:09:18 +00001923 if (Constant *FC = ConstantFoldCompareInstruction(
1924 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001925 return FC; // Fold a few common cases...
1926
1927 // Look up the constant in the table first to ensure uniqueness
1928 std::vector<Constant*> ArgVec;
1929 ArgVec.push_back(LHS);
1930 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001931 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001932 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001933
1934 // Implicitly locked.
1935 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001936}
1937
1938Constant *
1939ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1940 assert(LHS->getType() == RHS->getType());
1941 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1942
Owen Anderson53a52212009-07-13 04:09:18 +00001943 if (Constant *FC = ConstantFoldCompareInstruction(
1944 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001945 return FC; // Fold a few common cases...
1946
1947 // Look up the constant in the table first to ensure uniqueness
1948 std::vector<Constant*> ArgVec;
1949 ArgVec.push_back(LHS);
1950 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001951 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001952 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001953
1954 // Implicitly locked.
1955 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001956}
1957
Robert Bocchino23004482006-01-10 19:05:34 +00001958Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1959 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001960 if (Constant *FC = ConstantFoldExtractElementInstruction(
1961 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001962 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001963 // Look up the constant in the table first to ensure uniqueness
1964 std::vector<Constant*> ArgVec(1, Val);
1965 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001966 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001967
1968 // Implicitly locked.
1969 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001970}
1971
1972Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001973 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001974 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001975 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001976 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001977 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001978 Val, Idx);
1979}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001980
Robert Bocchinoca27f032006-01-17 20:07:22 +00001981Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1982 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001983 if (Constant *FC = ConstantFoldInsertElementInstruction(
1984 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001985 return FC; // Fold a few common cases...
1986 // Look up the constant in the table first to ensure uniqueness
1987 std::vector<Constant*> ArgVec(1, Val);
1988 ArgVec.push_back(Elt);
1989 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001990 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001991
1992 // Implicitly locked.
1993 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001994}
1995
1996Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1997 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001998 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001999 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002000 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002001 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002002 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002003 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002004 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002005}
2006
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002007Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2008 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002009 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2010 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002011 return FC; // Fold a few common cases...
2012 // Look up the constant in the table first to ensure uniqueness
2013 std::vector<Constant*> ArgVec(1, V1);
2014 ArgVec.push_back(V2);
2015 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002016 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002017
2018 // Implicitly locked.
2019 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002020}
2021
2022Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2023 Constant *Mask) {
2024 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2025 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002026
2027 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2028 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2029 const Type *ShufTy = VectorType::get(EltTy, NElts);
2030 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002031}
2032
Dan Gohman12fce772008-05-15 19:50:34 +00002033Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2034 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002035 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002036 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2037 Idxs+NumIdx) == Val->getType() &&
2038 "insertvalue indices invalid!");
2039 assert(Agg->getType() == ReqTy &&
2040 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002041 assert(Agg->getType()->isFirstClassType() &&
2042 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002043 Constant *FC = ConstantFoldInsertValueInstruction(
2044 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002045 assert(FC && "InsertValue constant expr couldn't be folded!");
2046 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002047}
2048
2049Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002050 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002051 assert(Agg->getType()->isFirstClassType() &&
2052 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002053
Dan Gohman0752bff2008-05-23 00:36:11 +00002054 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002055#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002056 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002057 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002058#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002059 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002060 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2061}
2062
2063Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002064 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002065 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2066 Idxs+NumIdx) == ReqTy &&
2067 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002068 assert(Agg->getType()->isFirstClassType() &&
2069 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002070 Constant *FC = ConstantFoldExtractValueInstruction(
2071 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002072 assert(FC && "ExtractValue constant expr couldn't be folded!");
2073 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002074}
2075
2076Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002077 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002078 assert(Agg->getType()->isFirstClassType() &&
2079 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002080
2081 const Type *ReqTy =
2082 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2083 assert(ReqTy && "extractvalue indices invalid!");
2084 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2085}
2086
Vikram S. Adve4c485332002-07-15 18:19:33 +00002087// destroyConstant - Remove the constant from the constant table...
2088//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002089void ConstantExpr::destroyConstant() {
2090 // Implicitly locked.
2091 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002092 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002093}
2094
Chris Lattner3cd8c562002-07-30 18:54:25 +00002095const char *ConstantExpr::getOpcodeName() const {
2096 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002097}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002098
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002099//===----------------------------------------------------------------------===//
2100// replaceUsesOfWithOnConstant implementations
2101
Chris Lattner913849b2007-08-21 00:55:23 +00002102/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2103/// 'From' to be uses of 'To'. This must update the uniquing data structures
2104/// etc.
2105///
2106/// Note that we intentionally replace all uses of From with To here. Consider
2107/// a large array that uses 'From' 1000 times. By handling this case all here,
2108/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2109/// single invocation handles all 1000 uses. Handling them one at a time would
2110/// work, but would be really slow because it would have to unique each updated
2111/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002112void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002113 Use *U) {
Owen Anderson3d344922009-07-21 20:55:28 +00002114 Constant *Replacement =
2115 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
2116
2117 if (!Replacement) return;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002118
2119 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002120 assert(Replacement != this && "I didn't contain From!");
2121
Chris Lattner7a1450d2005-10-04 18:13:04 +00002122 // Everyone using this now uses the replacement.
2123 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002124
2125 // Delete the old constant!
2126 destroyConstant();
2127}
2128
2129void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002130 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002131 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002132 Constant *ToC = cast<Constant>(To);
2133
Chris Lattnerdff59112005-10-04 18:47:09 +00002134 unsigned OperandToUpdate = U-OperandList;
2135 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2136
Jim Laskeyc03caef2006-07-17 17:38:29 +00002137 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002138 Lookup.first.first = getType();
2139 Lookup.second = this;
2140 std::vector<Constant*> &Values = Lookup.first.second;
2141 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002142
Chris Lattnerdff59112005-10-04 18:47:09 +00002143
Chris Lattner8760ec72005-10-04 01:17:50 +00002144 // Fill values with the modified operands of the constant struct. Also,
2145 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002146 bool isAllZeros = false;
2147 if (!ToC->isNullValue()) {
2148 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2149 Values.push_back(cast<Constant>(O->get()));
2150 } else {
2151 isAllZeros = true;
2152 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2153 Constant *Val = cast<Constant>(O->get());
2154 Values.push_back(Val);
2155 if (isAllZeros) isAllZeros = Val->isNullValue();
2156 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002157 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002158 Values[OperandToUpdate] = ToC;
2159
Chris Lattner8760ec72005-10-04 01:17:50 +00002160 Constant *Replacement = 0;
2161 if (isAllZeros) {
Owen Anderson39ede7b2009-07-21 20:13:12 +00002162 Replacement = getType()->getContext().getConstantAggregateZero(getType());
Chris Lattner8760ec72005-10-04 01:17:50 +00002163 } else {
2164 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002165 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002166 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002167 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002168 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002169
2170 if (Exists) {
2171 Replacement = I->second;
2172 } else {
2173 // Okay, the new shape doesn't exist in the system yet. Instead of
2174 // creating a new constant struct, inserting it, replaceallusesof'ing the
2175 // old with the new, then deleting the old... just update the current one
2176 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002177 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002178
Chris Lattnerdff59112005-10-04 18:47:09 +00002179 // Update to the new value.
2180 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002181 return;
2182 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002183 }
2184
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002185 assert(Replacement != this && "I didn't contain From!");
2186
Chris Lattner7a1450d2005-10-04 18:13:04 +00002187 // Everyone using this now uses the replacement.
2188 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002189
2190 // Delete the old constant!
2191 destroyConstant();
2192}
2193
Reid Spencerd84d35b2007-02-15 02:26:10 +00002194void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002195 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002196 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2197
2198 std::vector<Constant*> Values;
2199 Values.reserve(getNumOperands()); // Build replacement array...
2200 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2201 Constant *Val = getOperand(i);
2202 if (Val == From) Val = cast<Constant>(To);
2203 Values.push_back(Val);
2204 }
2205
Reid Spencerd84d35b2007-02-15 02:26:10 +00002206 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002207 assert(Replacement != this && "I didn't contain From!");
2208
Chris Lattner7a1450d2005-10-04 18:13:04 +00002209 // Everyone using this now uses the replacement.
2210 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002211
2212 // Delete the old constant!
2213 destroyConstant();
2214}
2215
2216void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002217 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002218 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2219 Constant *To = cast<Constant>(ToV);
2220
2221 Constant *Replacement = 0;
2222 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002223 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002224 Constant *Pointer = getOperand(0);
2225 Indices.reserve(getNumOperands()-1);
2226 if (Pointer == From) Pointer = To;
2227
2228 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2229 Constant *Val = getOperand(i);
2230 if (Val == From) Val = To;
2231 Indices.push_back(Val);
2232 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002233 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2234 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002235 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002236 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002237 if (Agg == From) Agg = To;
2238
Dan Gohman1ecaf452008-05-31 00:58:22 +00002239 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002240 Replacement = ConstantExpr::getExtractValue(Agg,
2241 &Indices[0], Indices.size());
2242 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002243 Constant *Agg = getOperand(0);
2244 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002245 if (Agg == From) Agg = To;
2246 if (Val == From) Val = To;
2247
Dan Gohman1ecaf452008-05-31 00:58:22 +00002248 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002249 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2250 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002252 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002254 } else if (getOpcode() == Instruction::Select) {
2255 Constant *C1 = getOperand(0);
2256 Constant *C2 = getOperand(1);
2257 Constant *C3 = getOperand(2);
2258 if (C1 == From) C1 = To;
2259 if (C2 == From) C2 = To;
2260 if (C3 == From) C3 = To;
2261 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002262 } else if (getOpcode() == Instruction::ExtractElement) {
2263 Constant *C1 = getOperand(0);
2264 Constant *C2 = getOperand(1);
2265 if (C1 == From) C1 = To;
2266 if (C2 == From) C2 = To;
2267 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002268 } else if (getOpcode() == Instruction::InsertElement) {
2269 Constant *C1 = getOperand(0);
2270 Constant *C2 = getOperand(1);
2271 Constant *C3 = getOperand(1);
2272 if (C1 == From) C1 = To;
2273 if (C2 == From) C2 = To;
2274 if (C3 == From) C3 = To;
2275 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2276 } else if (getOpcode() == Instruction::ShuffleVector) {
2277 Constant *C1 = getOperand(0);
2278 Constant *C2 = getOperand(1);
2279 Constant *C3 = getOperand(2);
2280 if (C1 == From) C1 = To;
2281 if (C2 == From) C2 = To;
2282 if (C3 == From) C3 = To;
2283 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002284 } else if (isCompare()) {
2285 Constant *C1 = getOperand(0);
2286 Constant *C2 = getOperand(1);
2287 if (C1 == From) C1 = To;
2288 if (C2 == From) C2 = To;
2289 if (getOpcode() == Instruction::ICmp)
2290 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002291 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002292 assert(getOpcode() == Instruction::FCmp);
2293 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002294 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002295 } else if (getNumOperands() == 2) {
2296 Constant *C1 = getOperand(0);
2297 Constant *C2 = getOperand(1);
2298 if (C1 == From) C1 = To;
2299 if (C2 == From) C2 = To;
2300 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2301 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002302 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002303 return;
2304 }
2305
2306 assert(Replacement != this && "I didn't contain From!");
2307
Chris Lattner7a1450d2005-10-04 18:13:04 +00002308 // Everyone using this now uses the replacement.
2309 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002310
2311 // Delete the old constant!
2312 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002313}
Nick Lewycky49f89192009-04-04 07:22:01 +00002314
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002315void MDNode::replaceElement(Value *From, Value *To) {
2316 SmallVector<Value*, 4> Values;
2317 Values.reserve(getNumElements()); // Build replacement array...
2318 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2319 Value *Val = getElement(i);
2320 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002321 Values.push_back(Val);
2322 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002323
Owen Anderson4118dde2009-07-16 23:44:30 +00002324 MDNode *Replacement =
2325 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002326 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002327
Nick Lewycky49f89192009-04-04 07:22:01 +00002328 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002329
Nick Lewycky49f89192009-04-04 07:22:01 +00002330 destroyConstant();
2331}