blob: db405927ff5956150a904840775a8086faf4a45b [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
Chris Lattner4565ef52009-07-22 00:05:44 +0000104
105/// getRelocationInfo - This method classifies the entry according to
106/// whether or not it may generate a relocation entry. This must be
107/// conservative, so if it might codegen to a relocatable entry, it should say
108/// so. The return values are:
109///
110/// 0: This constant pool entry is guaranteed to never have a relocation
111/// applied to it (because it holds a simple constant like '4').
112/// 1: This entry has relocations, but the entries are guaranteed to be
113/// resolvable by the static linker, so the dynamic linker will never see
114/// them.
115/// 2: This entry may have arbitrary relocations.
116///
117/// FIXME: This really should not be in VMCore.
118unsigned Constant::getRelocationInfo() const {
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000119 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000120 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
121 return 1; // Local to this file/library.
122 return 2; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000123 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000124
125 unsigned Result = 0;
Evan Chengf9e003b2007-03-08 00:59:12 +0000126 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner4565ef52009-07-22 00:05:44 +0000127 Result = std::max(Result, getOperand(i)->getRelocationInfo());
128
129 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000130}
131
Chris Lattner4565ef52009-07-22 00:05:44 +0000132
Chris Lattner2105d662008-07-10 00:28:11 +0000133/// getVectorElements - This method, which is only valid on constant of vector
134/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000135/// This handles breaking down a vector undef into undef elements, etc. For
136/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000137void Constant::getVectorElements(LLVMContext &Context,
138 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000139 assert(isa<VectorType>(getType()) && "Not a vector constant!");
140
141 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
142 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
143 Elts.push_back(CV->getOperand(i));
144 return;
145 }
146
147 const VectorType *VT = cast<VectorType>(getType());
148 if (isa<ConstantAggregateZero>(this)) {
149 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000150 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000151 return;
152 }
153
Chris Lattnerc5098a22008-07-14 05:10:41 +0000154 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000155 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000156 return;
157 }
158
159 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000160}
161
162
163
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000165// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166//===----------------------------------------------------------------------===//
167
Reid Spencerb31bffe2007-02-26 23:54:03 +0000168ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000169 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000170 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171}
172
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000173//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000174// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000175//===----------------------------------------------------------------------===//
176
Daniel Dunbare974dc32009-07-17 18:33:52 +0000177#ifndef NDEBUG
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000178static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
179 if (Ty == Type::FloatTy)
180 return &APFloat::IEEEsingle;
181 if (Ty == Type::DoubleTy)
182 return &APFloat::IEEEdouble;
183 if (Ty == Type::X86_FP80Ty)
184 return &APFloat::x87DoubleExtended;
185 else if (Ty == Type::FP128Ty)
186 return &APFloat::IEEEquad;
187
188 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
189 return &APFloat::PPCDoubleDouble;
190}
Daniel Dunbare974dc32009-07-17 18:33:52 +0000191#endif
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000192
Dale Johannesend246b2c2007-08-30 00:23:21 +0000193ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
194 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000195 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
196 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197}
198
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000199bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000200 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000201}
202
Dale Johannesend246b2c2007-08-30 00:23:21 +0000203bool ConstantFP::isExactlyValue(const APFloat& V) const {
204 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000205}
206
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000207//===----------------------------------------------------------------------===//
208// ConstantXXX Classes
209//===----------------------------------------------------------------------===//
210
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000213 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000214 : Constant(T, ConstantArrayVal,
215 OperandTraits<ConstantArray>::op_end(this) - V.size(),
216 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000217 assert(V.size() == T->getNumElements() &&
218 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000219 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000220 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
221 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000222 Constant *C = *I;
223 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000224 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000225 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000226 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000227 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 }
229}
230
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000231
Chris Lattner3462ae32001-12-03 22:26:30 +0000232ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000233 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000234 : Constant(T, ConstantStructVal,
235 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
236 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000237 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000238 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000239 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000240 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
241 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000242 Constant *C = *I;
243 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000244 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000245 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000246 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000247 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000248 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000249 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250 }
251}
252
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000253
Reid Spencerd84d35b2007-02-15 02:26:10 +0000254ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000255 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000256 : Constant(T, ConstantVectorVal,
257 OperandTraits<ConstantVector>::op_end(this) - V.size(),
258 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000259 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000260 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
261 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000262 Constant *C = *I;
263 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000264 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000265 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000266 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000267 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000268 }
269}
270
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000271
Gabor Greiff6caff662008-05-10 08:32:32 +0000272namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000273// We declare several classes private to this file, so use an anonymous
274// namespace
275namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000276
Gordon Henriksen14a55692007-12-10 02:14:30 +0000277/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
278/// behind the scenes to implement unary constant exprs.
279class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000280 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000281public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000282 // allocate space for exactly one operand
283 void *operator new(size_t s) {
284 return User::operator new(s, 1);
285 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000286 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000287 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
288 Op<0>() = C;
289 }
290 /// Transparently provide more efficient getOperand methods.
291 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000292};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000293
Gordon Henriksen14a55692007-12-10 02:14:30 +0000294/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
295/// behind the scenes to implement binary constant exprs.
296class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000297 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000298public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000299 // allocate space for exactly two operands
300 void *operator new(size_t s) {
301 return User::operator new(s, 2);
302 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000303 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000304 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000305 Op<0>() = C1;
306 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000307 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000308 /// Transparently provide more efficient getOperand methods.
309 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000310};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000311
Gordon Henriksen14a55692007-12-10 02:14:30 +0000312/// SelectConstantExpr - This class is private to Constants.cpp, and is used
313/// behind the scenes to implement select constant exprs.
314class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000315 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000316public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000317 // allocate space for exactly three operands
318 void *operator new(size_t s) {
319 return User::operator new(s, 3);
320 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000321 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000322 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000323 Op<0>() = C1;
324 Op<1>() = C2;
325 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000326 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000327 /// Transparently provide more efficient getOperand methods.
328 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000329};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000330
Gordon Henriksen14a55692007-12-10 02:14:30 +0000331/// ExtractElementConstantExpr - This class is private to
332/// Constants.cpp, and is used behind the scenes to implement
333/// extractelement constant exprs.
334class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000335 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000336public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000337 // allocate space for exactly two operands
338 void *operator new(size_t s) {
339 return User::operator new(s, 2);
340 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000341 ExtractElementConstantExpr(Constant *C1, Constant *C2)
342 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000343 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000344 Op<0>() = C1;
345 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000346 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000347 /// Transparently provide more efficient getOperand methods.
348 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000349};
Robert Bocchino23004482006-01-10 19:05:34 +0000350
Gordon Henriksen14a55692007-12-10 02:14:30 +0000351/// InsertElementConstantExpr - This class is private to
352/// Constants.cpp, and is used behind the scenes to implement
353/// insertelement constant exprs.
354class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000355 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000356public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000357 // allocate space for exactly three operands
358 void *operator new(size_t s) {
359 return User::operator new(s, 3);
360 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000361 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
362 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000363 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000364 Op<0>() = C1;
365 Op<1>() = C2;
366 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000367 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000368 /// Transparently provide more efficient getOperand methods.
369 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000370};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000371
Gordon Henriksen14a55692007-12-10 02:14:30 +0000372/// ShuffleVectorConstantExpr - This class is private to
373/// Constants.cpp, and is used behind the scenes to implement
374/// shufflevector constant exprs.
375class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000376 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000377public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000378 // allocate space for exactly three operands
379 void *operator new(size_t s) {
380 return User::operator new(s, 3);
381 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000382 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000383 : ConstantExpr(VectorType::get(
384 cast<VectorType>(C1->getType())->getElementType(),
385 cast<VectorType>(C3->getType())->getNumElements()),
386 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000387 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000388 Op<0>() = C1;
389 Op<1>() = C2;
390 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000391 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000392 /// Transparently provide more efficient getOperand methods.
393 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000394};
395
Dan Gohman12fce772008-05-15 19:50:34 +0000396/// ExtractValueConstantExpr - This class is private to
397/// Constants.cpp, and is used behind the scenes to implement
398/// extractvalue constant exprs.
399class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000400 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000401public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000402 // allocate space for exactly one operand
403 void *operator new(size_t s) {
404 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000405 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000406 ExtractValueConstantExpr(Constant *Agg,
407 const SmallVector<unsigned, 4> &IdxList,
408 const Type *DestTy)
409 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
410 Indices(IdxList) {
411 Op<0>() = Agg;
412 }
413
Dan Gohman7bb04502008-05-31 19:09:08 +0000414 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000415 const SmallVector<unsigned, 4> Indices;
416
Dan Gohman12fce772008-05-15 19:50:34 +0000417 /// Transparently provide more efficient getOperand methods.
418 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
419};
420
421/// InsertValueConstantExpr - This class is private to
422/// Constants.cpp, and is used behind the scenes to implement
423/// insertvalue constant exprs.
424class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000425 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000426public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000427 // allocate space for exactly one operand
428 void *operator new(size_t s) {
429 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000430 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000431 InsertValueConstantExpr(Constant *Agg, Constant *Val,
432 const SmallVector<unsigned, 4> &IdxList,
433 const Type *DestTy)
434 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
435 Indices(IdxList) {
436 Op<0>() = Agg;
437 Op<1>() = Val;
438 }
439
Dan Gohman7bb04502008-05-31 19:09:08 +0000440 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000441 const SmallVector<unsigned, 4> Indices;
442
Dan Gohman12fce772008-05-15 19:50:34 +0000443 /// Transparently provide more efficient getOperand methods.
444 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
445};
446
447
Gordon Henriksen14a55692007-12-10 02:14:30 +0000448/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
449/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000450class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000451 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000452 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000453public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000454 static GetElementPtrConstantExpr *Create(Constant *C,
455 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000456 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000457 return
458 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000459 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000460 /// Transparently provide more efficient getOperand methods.
461 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000462};
463
464// CompareConstantExpr - This class is private to Constants.cpp, and is used
465// behind the scenes to implement ICmp and FCmp constant expressions. This is
466// needed in order to store the predicate value for these instructions.
467struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000468 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
469 // allocate space for exactly two operands
470 void *operator new(size_t s) {
471 return User::operator new(s, 2);
472 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000473 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000474 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
475 unsigned short pred, Constant* LHS, Constant* RHS)
476 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000477 Op<0>() = LHS;
478 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000479 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000480 /// Transparently provide more efficient getOperand methods.
481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000482};
483
484} // end anonymous namespace
485
Gabor Greiff6caff662008-05-10 08:32:32 +0000486template <>
487struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
488};
489DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
490
491template <>
492struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
493};
494DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
495
496template <>
497struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
498};
499DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
500
501template <>
502struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
503};
504DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
505
506template <>
507struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
508};
509DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
510
511template <>
512struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
513};
514DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
515
Dan Gohman12fce772008-05-15 19:50:34 +0000516template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000517struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000518};
Dan Gohman12fce772008-05-15 19:50:34 +0000519DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
520
521template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000522struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000523};
Dan Gohman12fce772008-05-15 19:50:34 +0000524DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
525
Gabor Greiff6caff662008-05-10 08:32:32 +0000526template <>
527struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
528};
529
530GetElementPtrConstantExpr::GetElementPtrConstantExpr
531 (Constant *C,
532 const std::vector<Constant*> &IdxList,
533 const Type *DestTy)
534 : ConstantExpr(DestTy, Instruction::GetElementPtr,
535 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
536 - (IdxList.size()+1),
537 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000538 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000539 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000540 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000541}
542
543DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
544
545
546template <>
547struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
548};
549DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
550
551
552} // End llvm namespace
553
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000554
555// Utility function for determining if a ConstantExpr is a CastOp or not. This
556// can't be inline because we don't want to #include Instruction.h into
557// Constant.h
558bool ConstantExpr::isCast() const {
559 return Instruction::isCast(getOpcode());
560}
561
Reid Spenceree3c9912006-12-04 05:19:50 +0000562bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000563 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000564}
565
Dan Gohman1ecaf452008-05-31 00:58:22 +0000566bool ConstantExpr::hasIndices() const {
567 return getOpcode() == Instruction::ExtractValue ||
568 getOpcode() == Instruction::InsertValue;
569}
570
571const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
572 if (const ExtractValueConstantExpr *EVCE =
573 dyn_cast<ExtractValueConstantExpr>(this))
574 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000575
576 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000577}
578
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000579unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000580 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000581 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000582 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000583}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000584
Chris Lattner7c1018a2006-07-14 19:37:40 +0000585/// getWithOperandReplaced - Return a constant expression identical to this
586/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000587Constant *
588ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000589 assert(OpNo < getNumOperands() && "Operand num is out of range!");
590 assert(Op->getType() == getOperand(OpNo)->getType() &&
591 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000592 if (getOperand(OpNo) == Op)
593 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000594
Chris Lattner227816342006-07-14 22:20:01 +0000595 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000596 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000597 case Instruction::Trunc:
598 case Instruction::ZExt:
599 case Instruction::SExt:
600 case Instruction::FPTrunc:
601 case Instruction::FPExt:
602 case Instruction::UIToFP:
603 case Instruction::SIToFP:
604 case Instruction::FPToUI:
605 case Instruction::FPToSI:
606 case Instruction::PtrToInt:
607 case Instruction::IntToPtr:
608 case Instruction::BitCast:
609 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000610 case Instruction::Select:
611 Op0 = (OpNo == 0) ? Op : getOperand(0);
612 Op1 = (OpNo == 1) ? Op : getOperand(1);
613 Op2 = (OpNo == 2) ? Op : getOperand(2);
614 return ConstantExpr::getSelect(Op0, Op1, Op2);
615 case Instruction::InsertElement:
616 Op0 = (OpNo == 0) ? Op : getOperand(0);
617 Op1 = (OpNo == 1) ? Op : getOperand(1);
618 Op2 = (OpNo == 2) ? Op : getOperand(2);
619 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
620 case Instruction::ExtractElement:
621 Op0 = (OpNo == 0) ? Op : getOperand(0);
622 Op1 = (OpNo == 1) ? Op : getOperand(1);
623 return ConstantExpr::getExtractElement(Op0, Op1);
624 case Instruction::ShuffleVector:
625 Op0 = (OpNo == 0) ? Op : getOperand(0);
626 Op1 = (OpNo == 1) ? Op : getOperand(1);
627 Op2 = (OpNo == 2) ? Op : getOperand(2);
628 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000629 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000630 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000631 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000632 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000633 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000634 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000635 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000636 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000637 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000638 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000639 default:
640 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000641 Op0 = (OpNo == 0) ? Op : getOperand(0);
642 Op1 = (OpNo == 1) ? Op : getOperand(1);
643 return ConstantExpr::get(getOpcode(), Op0, Op1);
644 }
645}
646
647/// getWithOperands - This returns the current constant expression with the
648/// operands replaced with the specified values. The specified operands must
649/// match count and type with the existing ones.
650Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000651getWithOperands(Constant* const *Ops, unsigned NumOps) const {
652 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000653 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000654 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000655 assert(Ops[i]->getType() == getOperand(i)->getType() &&
656 "Operand type mismatch!");
657 AnyChange |= Ops[i] != getOperand(i);
658 }
659 if (!AnyChange) // No operands changed, return self.
660 return const_cast<ConstantExpr*>(this);
661
662 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000663 case Instruction::Trunc:
664 case Instruction::ZExt:
665 case Instruction::SExt:
666 case Instruction::FPTrunc:
667 case Instruction::FPExt:
668 case Instruction::UIToFP:
669 case Instruction::SIToFP:
670 case Instruction::FPToUI:
671 case Instruction::FPToSI:
672 case Instruction::PtrToInt:
673 case Instruction::IntToPtr:
674 case Instruction::BitCast:
675 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000676 case Instruction::Select:
677 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
678 case Instruction::InsertElement:
679 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
680 case Instruction::ExtractElement:
681 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
682 case Instruction::ShuffleVector:
683 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000684 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000685 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000686 case Instruction::ICmp:
687 case Instruction::FCmp:
688 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000689 default:
690 assert(getNumOperands() == 2 && "Must be binary operator?");
691 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000692 }
693}
694
Chris Lattner2f7c9632001-06-06 20:29:01 +0000695
696//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000697// isValueValidForType implementations
698
Reid Spencere7334722006-12-19 01:28:19 +0000699bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000700 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000701 if (Ty == Type::Int1Ty)
702 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000703 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000704 return true; // always true, has to fit in largest type
705 uint64_t Max = (1ll << NumBits) - 1;
706 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000707}
708
Reid Spencere0fc4df2006-10-20 07:07:24 +0000709bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000710 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000711 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000712 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000713 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000714 return true; // always true, has to fit in largest type
715 int64_t Min = -(1ll << (NumBits-1));
716 int64_t Max = (1ll << (NumBits-1)) - 1;
717 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718}
719
Dale Johannesend246b2c2007-08-30 00:23:21 +0000720bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
721 // convert modifies in place, so make a copy.
722 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000723 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000724 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000725 default:
726 return false; // These can't be represented as floating point!
727
Dale Johannesend246b2c2007-08-30 00:23:21 +0000728 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000729 case Type::FloatTyID: {
730 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
731 return true;
732 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
733 return !losesInfo;
734 }
735 case Type::DoubleTyID: {
736 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
737 &Val2.getSemantics() == &APFloat::IEEEdouble)
738 return true;
739 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
740 return !losesInfo;
741 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000742 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000743 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
744 &Val2.getSemantics() == &APFloat::IEEEdouble ||
745 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000746 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000747 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
748 &Val2.getSemantics() == &APFloat::IEEEdouble ||
749 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000750 case Type::PPC_FP128TyID:
751 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
752 &Val2.getSemantics() == &APFloat::IEEEdouble ||
753 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000754 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000755}
Chris Lattner9655e542001-07-20 19:16:02 +0000756
Chris Lattner49d855c2001-09-07 16:46:31 +0000757//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000758// Factory Function Implementation
759
Gabor Greiff6caff662008-05-10 08:32:32 +0000760
761// The number of operands for each ConstantCreator::create method is
762// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000763// ConstantCreator - A class that is used to create constants by
764// ValueMap*. This class should be partially specialized if there is
765// something strange that needs to be done to interface to the ctor for the
766// constant.
767//
Chris Lattner189d19f2003-11-21 20:23:48 +0000768namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000769 template<class ValType>
770 struct ConstantTraits;
771
772 template<typename T, typename Alloc>
773 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
774 static unsigned uses(const std::vector<T, Alloc>& v) {
775 return v.size();
776 }
777 };
778
Chris Lattner189d19f2003-11-21 20:23:48 +0000779 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000780 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000781 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000782 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000783 }
784 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000785
Chris Lattner189d19f2003-11-21 20:23:48 +0000786 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000787 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000788 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000789 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000790 }
791 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000792
Chris Lattner935aa922005-10-04 17:48:46 +0000793 template<class ValType, class TypeClass, class ConstantClass,
794 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000795 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000796 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000797 typedef std::pair<const Type*, ValType> MapKey;
798 typedef std::map<MapKey, Constant *> MapTy;
799 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
800 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000801 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000802 /// Map - This is the main map from the element descriptor to the Constants.
803 /// This is the primary way we avoid creating two of the same shape
804 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000805 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000806
807 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
808 /// from the constants to their element in Map. This is important for
809 /// removal of constants from the array, which would otherwise have to scan
810 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000811 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000812
Jim Laskeyc03caef2006-07-17 17:38:29 +0000813 /// AbstractTypeMap - Map for abstract type constants.
814 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000815 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000816
817 /// ValueMapLock - Mutex for this map.
818 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000819
Chris Lattner98fa07b2003-05-23 20:03:32 +0000820 public:
Owen Anderson61794042009-06-17 20:10:08 +0000821 // NOTE: This function is not locked. It is the caller's responsibility
822 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000823 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000824
825 /// InsertOrGetItem - Return an iterator for the specified element.
826 /// If the element exists in the map, the returned iterator points to the
827 /// entry and Exists=true. If not, the iterator points to the newly
828 /// inserted entry and returns Exists=false. Newly inserted entries have
829 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000830 /// NOTE: This function is not locked. It is the caller's responsibility
831 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000832 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
833 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000834 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000835 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000836 Exists = !IP.second;
837 return IP.first;
838 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000839
Chris Lattner935aa922005-10-04 17:48:46 +0000840private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000841 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000842 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000843 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000844 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
845 IMI->second->second == CP &&
846 "InverseMap corrupt!");
847 return IMI->second;
848 }
849
Jim Laskeyc03caef2006-07-17 17:38:29 +0000850 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000851 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
852 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000853 if (I == Map.end() || I->second != CP) {
854 // FIXME: This should not use a linear scan. If this gets to be a
855 // performance problem, someone should look at this.
856 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
857 /* empty */;
858 }
Chris Lattner935aa922005-10-04 17:48:46 +0000859 return I;
860 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000861
862 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
863 typename MapTy::iterator I) {
864 ConstantClass* Result =
865 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
866
867 assert(Result->getType() == Ty && "Type specified is not correct!");
868 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
869
870 if (HasLargeKey) // Remember the reverse mapping if needed.
871 InverseMap.insert(std::make_pair(Result, I));
872
873 // If the type of the constant is abstract, make sure that an entry
874 // exists for it in the AbstractTypeMap.
875 if (Ty->isAbstract()) {
876 typename AbstractTypeMapTy::iterator TI =
877 AbstractTypeMap.find(Ty);
878
879 if (TI == AbstractTypeMap.end()) {
880 // Add ourselves to the ATU list of the type.
881 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
882
883 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
884 }
885 }
886
887 return Result;
888 }
Chris Lattner935aa922005-10-04 17:48:46 +0000889public:
890
Chris Lattnerb64419a2005-10-03 22:51:37 +0000891 /// getOrCreate - Return the specified constant from the map, creating it if
892 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000893 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000894 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +0000895 MapKey Lookup(Ty, V);
896 ConstantClass* Result = 0;
897
898 typename MapTy::iterator I = Map.find(Lookup);
899 // Is it in the map?
900 if (I != Map.end())
901 Result = static_cast<ConstantClass *>(I->second);
902
903 if (!Result) {
904 // If no preexisting value, create one now...
905 Result = Create(Ty, V, I);
906 }
907
908 return Result;
909 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000910
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000911 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000912 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000913 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000914 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000915 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000916
Chris Lattner935aa922005-10-04 17:48:46 +0000917 if (HasLargeKey) // Remember the reverse mapping if needed.
918 InverseMap.erase(CP);
919
Chris Lattnerb50d1352003-10-05 00:17:43 +0000920 // Now that we found the entry, make sure this isn't the entry that
921 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000922 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000923 if (Ty->isAbstract()) {
924 assert(AbstractTypeMap.count(Ty) &&
925 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000926 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000927 if (ATMEntryIt == I) {
928 // Yes, we are removing the representative entry for this type.
929 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000930 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000931
Chris Lattnerb50d1352003-10-05 00:17:43 +0000932 // First check the entry before this one...
933 if (TmpIt != Map.begin()) {
934 --TmpIt;
935 if (TmpIt->first.first != Ty) // Not the same type, move back...
936 ++TmpIt;
937 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000938
Chris Lattnerb50d1352003-10-05 00:17:43 +0000939 // If we didn't find the same type, try to move forward...
940 if (TmpIt == ATMEntryIt) {
941 ++TmpIt;
942 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
943 --TmpIt; // No entry afterwards with the same type
944 }
945
946 // If there is another entry in the map of the same abstract type,
947 // update the AbstractTypeMap entry now.
948 if (TmpIt != ATMEntryIt) {
949 ATMEntryIt = TmpIt;
950 } else {
951 // Otherwise, we are removing the last instance of this type
952 // from the table. Remove from the ATM, and from user list.
953 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
954 AbstractTypeMap.erase(Ty);
955 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000956 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000957 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000958
Chris Lattnerb50d1352003-10-05 00:17:43 +0000959 Map.erase(I);
960 }
961
Chris Lattner3b793c62005-10-04 21:35:50 +0000962
963 /// MoveConstantToNewSlot - If we are about to change C to be the element
964 /// specified by I, update our internal data structures to reflect this
965 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +0000966 /// NOTE: This function is not locked. It is the responsibility of the
967 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000968 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000969 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000970 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000971 assert(OldI != Map.end() && "Constant not found in constant table!");
972 assert(OldI->second == C && "Didn't find correct element?");
973
974 // If this constant is the representative element for its abstract type,
975 // update the AbstractTypeMap so that the representative element is I.
976 if (C->getType()->isAbstract()) {
977 typename AbstractTypeMapTy::iterator ATI =
978 AbstractTypeMap.find(C->getType());
979 assert(ATI != AbstractTypeMap.end() &&
980 "Abstract type not in AbstractTypeMap?");
981 if (ATI->second == OldI)
982 ATI->second = I;
983 }
984
985 // Remove the old entry from the map.
986 Map.erase(OldI);
987
988 // Update the inverse map so that we know that this constant is now
989 // located at descriptor I.
990 if (HasLargeKey) {
991 assert(I->second == C && "Bad inversemap entry!");
992 InverseMap[C] = I;
993 }
994 }
995
Chris Lattnerb50d1352003-10-05 00:17:43 +0000996 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000997 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000998 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000999 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001000
1001 assert(I != AbstractTypeMap.end() &&
1002 "Abstract type not in AbstractTypeMap?");
1003
1004 // Convert a constant at a time until the last one is gone. The last one
1005 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1006 // eliminated eventually.
1007 do {
1008 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001009 TypeClass>::convert(
1010 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001011 cast<TypeClass>(NewTy));
1012
Jim Laskeyc03caef2006-07-17 17:38:29 +00001013 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001014 } while (I != AbstractTypeMap.end());
1015 }
1016
1017 // If the type became concrete without being refined to any other existing
1018 // type, we just remove ourselves from the ATU list.
1019 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001020 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001021 }
1022
1023 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001024 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001025 }
1026 };
1027}
1028
Dan Gohman92b551b2009-03-03 02:55:14 +00001029/// destroyConstant - Remove the constant from the constant table...
1030///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001031void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001032 // Implicitly locked.
Owen Anderson39ede7b2009-07-21 20:13:12 +00001033 getType()->getContext().erase(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001034 destroyConstantImpl();
1035}
1036
Dan Gohman92b551b2009-03-03 02:55:14 +00001037/// destroyConstant - Remove the constant from the constant table...
1038///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001039void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001040 // Implicitly locked.
Owen Anderson3d344922009-07-21 20:55:28 +00001041 getType()->getContext().erase(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001042 destroyConstantImpl();
1043}
1044
Reid Spencer2546b762007-01-26 07:37:34 +00001045/// isString - This method returns true if the array is an array of i8, and
1046/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001047bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001048 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001049 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001050 return false;
1051 // Check the elements to make sure they are all integers, not constant
1052 // expressions.
1053 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1054 if (!isa<ConstantInt>(getOperand(i)))
1055 return false;
1056 return true;
1057}
1058
Evan Cheng3763c5b2006-10-26 19:15:05 +00001059/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001060/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001061/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001062bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001063 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001064 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001065 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001066
Evan Chenge974da62006-10-26 21:48:03 +00001067 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001068 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001069 return false;
1070 // Other elements must be non-null integers.
1071 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1072 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001073 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001074 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001075 return false;
1076 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001077 return true;
1078}
1079
1080
Dan Gohman92b551b2009-03-03 02:55:14 +00001081/// getAsString - If the sub-element type of this array is i8
1082/// then this method converts the array to an std::string and returns it.
1083/// Otherwise, it asserts out.
1084///
Chris Lattner81fabb02002-08-26 17:53:56 +00001085std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001086 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001087 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001088 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001089 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001090 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001091 return Result;
1092}
1093
1094
Chris Lattner3462ae32001-12-03 22:26:30 +00001095//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001096//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001097
Chris Lattner189d19f2003-11-21 20:23:48 +00001098namespace llvm {
1099 template<>
1100 struct ConvertConstantType<ConstantStruct, StructType> {
1101 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1102 // Make everyone now use a constant of the new type...
1103 std::vector<Constant*> C;
1104 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1105 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001106 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001107 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001108
Chris Lattner189d19f2003-11-21 20:23:48 +00001109 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001110 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001111 }
1112 };
1113}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001114
Chris Lattner8760ec72005-10-04 01:17:50 +00001115typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001116 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001117static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001118
Chris Lattner3e650af2004-08-04 04:48:01 +00001119static std::vector<Constant*> getValType(ConstantStruct *CS) {
1120 std::vector<Constant*> Elements;
1121 Elements.reserve(CS->getNumOperands());
1122 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1123 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1124 return Elements;
1125}
1126
Chris Lattner015e8212004-02-15 04:14:47 +00001127Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001128 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001129 // Create a ConstantAggregateZero value if all elements are zeros...
1130 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001131 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001132 // Implicitly locked.
1133 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001134
Owen Anderson39ede7b2009-07-21 20:13:12 +00001135 return Ty->getContext().getConstantAggregateZero(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001136}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001137
Chris Lattnerd7a73302001-10-13 06:57:33 +00001138// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001139//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001140void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001141 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001142 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001143 destroyConstantImpl();
1144}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001145
Reid Spencerd84d35b2007-02-15 02:26:10 +00001146//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001147//
1148namespace llvm {
1149 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001150 struct ConvertConstantType<ConstantVector, VectorType> {
1151 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001152 // Make everyone now use a constant of the new type...
1153 std::vector<Constant*> C;
1154 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1155 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001156 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001157 assert(New != OldC && "Didn't replace constant??");
1158 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001159 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001160 }
1161 };
1162}
1163
Reid Spencerd84d35b2007-02-15 02:26:10 +00001164static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001165 std::vector<Constant*> Elements;
1166 Elements.reserve(CP->getNumOperands());
1167 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1168 Elements.push_back(CP->getOperand(i));
1169 return Elements;
1170}
1171
Reid Spencerd84d35b2007-02-15 02:26:10 +00001172static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001173 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001174
Reid Spencerd84d35b2007-02-15 02:26:10 +00001175Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001176 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001177 assert(!V.empty() && "Vectors can't be empty");
1178 // If this is an all-undef or alll-zero vector, return a
1179 // ConstantAggregateZero or UndefValue.
1180 Constant *C = V[0];
1181 bool isZero = C->isNullValue();
1182 bool isUndef = isa<UndefValue>(C);
1183
1184 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001185 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001186 if (V[i] != C) {
1187 isZero = isUndef = false;
1188 break;
1189 }
Brian Gaeke02209042004-08-20 06:00:58 +00001190 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001191
1192 if (isZero)
Owen Anderson39ede7b2009-07-21 20:13:12 +00001193 return Ty->getContext().getConstantAggregateZero(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001194 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001195 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001196
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001197 // Implicitly locked.
1198 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001199}
1200
Brian Gaeke02209042004-08-20 06:00:58 +00001201// destroyConstant - Remove the constant from the constant table...
1202//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001203void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001204 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001205 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001206 destroyConstantImpl();
1207}
1208
Dan Gohman30978072007-05-24 14:36:04 +00001209/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001210/// is set to all ones.
1211/// @returns true iff this constant's emements are all set to all ones.
1212/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001213bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001214 // Check out first element.
1215 const Constant *Elt = getOperand(0);
1216 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1217 if (!CI || !CI->isAllOnesValue()) return false;
1218 // Then make sure all remaining elements point to the same value.
1219 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1220 if (getOperand(I) != Elt) return false;
1221 }
1222 return true;
1223}
1224
Dan Gohman07159202007-10-17 17:51:30 +00001225/// getSplatValue - If this is a splat constant, where all of the
1226/// elements have the same value, return that value. Otherwise return null.
1227Constant *ConstantVector::getSplatValue() {
1228 // Check out first element.
1229 Constant *Elt = getOperand(0);
1230 // Then make sure all remaining elements point to the same value.
1231 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1232 if (getOperand(I) != Elt) return 0;
1233 return Elt;
1234}
1235
Chris Lattner3462ae32001-12-03 22:26:30 +00001236//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001237//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001238
Chris Lattner189d19f2003-11-21 20:23:48 +00001239namespace llvm {
1240 // ConstantPointerNull does not take extra "value" argument...
1241 template<class ValType>
1242 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1243 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1244 return new ConstantPointerNull(Ty);
1245 }
1246 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001247
Chris Lattner189d19f2003-11-21 20:23:48 +00001248 template<>
1249 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1250 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1251 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001252 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001253 assert(New != OldC && "Didn't replace constant??");
1254 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001255 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001256 }
1257 };
1258}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001259
Chris Lattner69edc982006-09-28 00:35:06 +00001260static ManagedStatic<ValueMap<char, PointerType,
1261 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001262
Chris Lattner3e650af2004-08-04 04:48:01 +00001263static char getValType(ConstantPointerNull *) {
1264 return 0;
1265}
1266
1267
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001268ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001269 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001270 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001271}
1272
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001273// destroyConstant - Remove the constant from the constant table...
1274//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001275void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001276 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001277 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001278 destroyConstantImpl();
1279}
1280
1281
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001282//---- UndefValue::get() implementation...
1283//
1284
1285namespace llvm {
1286 // UndefValue does not take extra "value" argument...
1287 template<class ValType>
1288 struct ConstantCreator<UndefValue, Type, ValType> {
1289 static UndefValue *create(const Type *Ty, const ValType &V) {
1290 return new UndefValue(Ty);
1291 }
1292 };
1293
1294 template<>
1295 struct ConvertConstantType<UndefValue, Type> {
1296 static void convert(UndefValue *OldC, const Type *NewTy) {
1297 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001298 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001299 assert(New != OldC && "Didn't replace constant??");
1300 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001301 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001302 }
1303 };
1304}
1305
Chris Lattner69edc982006-09-28 00:35:06 +00001306static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001307
1308static char getValType(UndefValue *) {
1309 return 0;
1310}
1311
1312
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001313UndefValue *UndefValue::get(const Type *Ty) {
1314 // Implicitly locked.
1315 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001316}
1317
1318// destroyConstant - Remove the constant from the constant table.
1319//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001320void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001321 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001322 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001323 destroyConstantImpl();
1324}
1325
Nick Lewycky49f89192009-04-04 07:22:01 +00001326//---- MDNode::get() implementation
1327//
1328
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001329MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001330 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001331 for (unsigned i = 0; i != NumVals; ++i)
1332 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001333}
1334
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001335void MDNode::Profile(FoldingSetNodeID &ID) const {
1336 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001337 ID.AddPointer(*I);
1338}
1339
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001340void MDNode::destroyConstant() {
Owen Anderson4118dde2009-07-16 23:44:30 +00001341 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001342 destroyConstantImpl();
1343}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001344
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001345//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001346//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001347
Dan Gohmand78c4002008-05-13 00:00:25 +00001348namespace {
1349
Reid Spenceree3c9912006-12-04 05:19:50 +00001350struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001351 typedef SmallVector<unsigned, 4> IndexList;
1352
1353 ExprMapKeyType(unsigned opc,
1354 const std::vector<Constant*> &ops,
1355 unsigned short pred = 0,
1356 const IndexList &inds = IndexList())
1357 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001358 uint16_t opcode;
1359 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001360 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001361 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001362 bool operator==(const ExprMapKeyType& that) const {
1363 return this->opcode == that.opcode &&
1364 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001365 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001366 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001367 }
1368 bool operator<(const ExprMapKeyType & that) const {
1369 return this->opcode < that.opcode ||
1370 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1371 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001372 this->operands < that.operands) ||
1373 (this->opcode == that.opcode && this->predicate == that.predicate &&
1374 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001375 }
1376
1377 bool operator!=(const ExprMapKeyType& that) const {
1378 return !(*this == that);
1379 }
1380};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001381
Dan Gohmand78c4002008-05-13 00:00:25 +00001382}
1383
Chris Lattner189d19f2003-11-21 20:23:48 +00001384namespace llvm {
1385 template<>
1386 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001387 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1388 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001389 if (Instruction::isCast(V.opcode))
1390 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1391 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001392 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001393 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1394 if (V.opcode == Instruction::Select)
1395 return new SelectConstantExpr(V.operands[0], V.operands[1],
1396 V.operands[2]);
1397 if (V.opcode == Instruction::ExtractElement)
1398 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1399 if (V.opcode == Instruction::InsertElement)
1400 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1401 V.operands[2]);
1402 if (V.opcode == Instruction::ShuffleVector)
1403 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1404 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001405 if (V.opcode == Instruction::InsertValue)
1406 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1407 V.indices, Ty);
1408 if (V.opcode == Instruction::ExtractValue)
1409 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001410 if (V.opcode == Instruction::GetElementPtr) {
1411 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001412 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001413 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001414
Reid Spenceree3c9912006-12-04 05:19:50 +00001415 // The compare instructions are weird. We have to encode the predicate
1416 // value and it is combined with the instruction opcode by multiplying
1417 // the opcode by one hundred. We must decode this to get the predicate.
1418 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001419 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001420 V.operands[0], V.operands[1]);
1421 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001422 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1423 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001424 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001425 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001426 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001427 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001428
Chris Lattner189d19f2003-11-21 20:23:48 +00001429 template<>
1430 struct ConvertConstantType<ConstantExpr, Type> {
1431 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1432 Constant *New;
1433 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001434 case Instruction::Trunc:
1435 case Instruction::ZExt:
1436 case Instruction::SExt:
1437 case Instruction::FPTrunc:
1438 case Instruction::FPExt:
1439 case Instruction::UIToFP:
1440 case Instruction::SIToFP:
1441 case Instruction::FPToUI:
1442 case Instruction::FPToSI:
1443 case Instruction::PtrToInt:
1444 case Instruction::IntToPtr:
1445 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001446 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001447 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001448 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001449 case Instruction::Select:
1450 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1451 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001452 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001453 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001454 default:
1455 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001456 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001457 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001458 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001459 break;
1460 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001461 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001462 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001463 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001464 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001465 break;
1466 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001467
Chris Lattner189d19f2003-11-21 20:23:48 +00001468 assert(New != OldC && "Didn't replace constant??");
1469 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001470 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001471 }
1472 };
1473} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001474
1475
Chris Lattner3e650af2004-08-04 04:48:01 +00001476static ExprMapKeyType getValType(ConstantExpr *CE) {
1477 std::vector<Constant*> Operands;
1478 Operands.reserve(CE->getNumOperands());
1479 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1480 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001481 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001482 CE->isCompare() ? CE->getPredicate() : 0,
1483 CE->hasIndices() ?
1484 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001485}
1486
Chris Lattner69edc982006-09-28 00:35:06 +00001487static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1488 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001489
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001490/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001491/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001492static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001493 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001494 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001495 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001496 if (Constant *FC =
1497 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001498 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001499
Vikram S. Adve4c485332002-07-15 18:19:33 +00001500 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001501 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001502 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001503
Owen Anderson61794042009-06-17 20:10:08 +00001504 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001505 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001506}
Reid Spencerf37dc652006-12-05 19:14:13 +00001507
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001508Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001509 Instruction::CastOps opc = Instruction::CastOps(oc);
1510 assert(Instruction::isCast(opc) && "opcode out of range");
1511 assert(C && Ty && "Null arguments to getCast");
1512 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1513
1514 switch (opc) {
1515 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001516 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001517 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001518 case Instruction::Trunc: return getTrunc(C, Ty);
1519 case Instruction::ZExt: return getZExt(C, Ty);
1520 case Instruction::SExt: return getSExt(C, Ty);
1521 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1522 case Instruction::FPExt: return getFPExtend(C, Ty);
1523 case Instruction::UIToFP: return getUIToFP(C, Ty);
1524 case Instruction::SIToFP: return getSIToFP(C, Ty);
1525 case Instruction::FPToUI: return getFPToUI(C, Ty);
1526 case Instruction::FPToSI: return getFPToSI(C, Ty);
1527 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1528 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1529 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001530 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001531 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001532}
1533
Reid Spencer5c140882006-12-04 20:17:56 +00001534Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001535 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001536 return getCast(Instruction::BitCast, C, Ty);
1537 return getCast(Instruction::ZExt, C, Ty);
1538}
1539
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001540Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001541 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001542 return getCast(Instruction::BitCast, C, Ty);
1543 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001544}
1545
1546Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001547 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001548 return getCast(Instruction::BitCast, C, Ty);
1549 return getCast(Instruction::Trunc, C, Ty);
1550}
1551
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001552Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001553 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001554 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001555
Chris Lattner03c49532007-01-15 02:27:26 +00001556 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001557 return getCast(Instruction::PtrToInt, S, Ty);
1558 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001559}
1560
Reid Spencer56521c42006-12-12 00:51:07 +00001561Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1562 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001563 assert(C->getType()->isIntOrIntVector() &&
1564 Ty->isIntOrIntVector() && "Invalid cast");
1565 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1566 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001567 Instruction::CastOps opcode =
1568 (SrcBits == DstBits ? Instruction::BitCast :
1569 (SrcBits > DstBits ? Instruction::Trunc :
1570 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1571 return getCast(opcode, C, Ty);
1572}
1573
1574Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001575 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001576 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001577 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1578 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001579 if (SrcBits == DstBits)
1580 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001581 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001582 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001583 return getCast(opcode, C, Ty);
1584}
1585
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001586Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001587#ifndef NDEBUG
1588 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1589 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1590#endif
1591 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1592 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1593 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1594 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001595 "SrcTy must be larger than DestTy for Trunc!");
1596
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001597 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001598}
1599
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001600Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001601#ifndef NDEBUG
1602 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1603 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1604#endif
1605 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1606 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1607 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1608 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609 "SrcTy must be smaller than DestTy for SExt!");
1610
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001611 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001612}
1613
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001614Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001615#ifndef NDEBUG
1616 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1617 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1618#endif
1619 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1620 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1621 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1622 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001623 "SrcTy must be smaller than DestTy for ZExt!");
1624
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001625 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001626}
1627
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001628Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001629#ifndef NDEBUG
1630 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1631 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1632#endif
1633 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1634 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1635 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001636 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001637 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001638}
1639
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001640Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001641#ifndef NDEBUG
1642 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1643 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1644#endif
1645 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1646 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1647 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001648 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001649 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001650}
1651
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001652Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001653#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001654 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1655 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001656#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001657 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1658 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1659 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001660 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001661}
1662
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001663Constant *ConstantExpr::getSIToFP(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() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001670 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001671 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001672}
1673
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001674Constant *ConstantExpr::getFPToUI(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()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1681 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001682 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001683}
1684
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001685Constant *ConstantExpr::getFPToSI(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 sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001693 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001694}
1695
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001696Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001697 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001698 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001699 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001700}
1701
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001702Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001703 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001704 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001705 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001706}
1707
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001708Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001709 // BitCast implies a no-op cast of type only. No bits change. However, you
1710 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001711#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001712 const Type *SrcTy = C->getType();
1713 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001714 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715
1716 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1717 // or nonptr->ptr). For all the other types, the cast is okay if source and
1718 // destination bit widths are identical.
1719 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1720 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001721#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001722 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001723
1724 // It is common to ask for a bitcast of a value to its own type, handle this
1725 // speedily.
1726 if (C->getType() == DstTy) return C;
1727
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001728 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001729}
1730
Chris Lattnerb50d1352003-10-05 00:17:43 +00001731Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001732 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001733 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001734 assert(Opcode >= Instruction::BinaryOpsBegin &&
1735 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001736 "Invalid opcode in binary constant expression");
1737 assert(C1->getType() == C2->getType() &&
1738 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001739
Reid Spencer542964f2007-01-11 18:21:29 +00001740 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001741 if (Constant *FC = ConstantFoldBinaryInstruction(
1742 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001743 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001744
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001745 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001746 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001747
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001748 // Implicitly locked.
1749 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001750}
1751
Reid Spencer266e42b2006-12-23 06:05:41 +00001752Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001753 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001754 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001755 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001756 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1757 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1758 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1759 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1760 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1761 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001762 return getFCmp(predicate, C1, C2);
1763
Nate Begemanc96e2e42008-07-25 17:35:37 +00001764 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1765 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1766 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1767 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001768 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001769 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001770}
1771
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001772Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001773 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1774 if (C1->getType()->isFPOrFPVector()) {
1775 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1776 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1777 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1778 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001779#ifndef NDEBUG
1780 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001781 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001782 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001783 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001784 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001785 assert(C1->getType()->isIntOrIntVector() &&
1786 "Tried to create an integer operation on a non-integer type!");
1787 break;
1788 case Instruction::FAdd:
1789 case Instruction::FSub:
1790 case Instruction::FMul:
1791 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1792 assert(C1->getType()->isFPOrFPVector() &&
1793 "Tried to create a floating-point operation on a "
1794 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001795 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001796 case Instruction::UDiv:
1797 case Instruction::SDiv:
1798 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001799 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001800 "Tried to create an arithmetic operation on a non-arithmetic type!");
1801 break;
1802 case Instruction::FDiv:
1803 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001804 assert(C1->getType()->isFPOrFPVector() &&
1805 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001806 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001807 case Instruction::URem:
1808 case Instruction::SRem:
1809 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001810 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001811 "Tried to create an arithmetic operation on a non-arithmetic type!");
1812 break;
1813 case Instruction::FRem:
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 Spencer7eb55b32006-11-02 01:53:59 +00001817 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001818 case Instruction::And:
1819 case Instruction::Or:
1820 case Instruction::Xor:
1821 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001822 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001823 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001824 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001825 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001826 case Instruction::LShr:
1827 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001828 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001829 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001830 "Tried to create a shift operation on a non-integer type!");
1831 break;
1832 default:
1833 break;
1834 }
1835#endif
1836
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001837 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001838}
1839
Reid Spencer266e42b2006-12-23 06:05:41 +00001840Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001841 Constant *C1, Constant *C2) {
1842 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001843 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001844}
1845
Chris Lattner6e415c02004-03-12 05:54:04 +00001846Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001847 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001848 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001849
1850 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001851 if (Constant *SC = ConstantFoldSelectInstruction(
1852 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001853 return SC; // Fold common cases
1854
1855 std::vector<Constant*> argVec(3, C);
1856 argVec[1] = V1;
1857 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001858 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001859
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001860 // Implicitly locked.
1861 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001862}
1863
Chris Lattnerb50d1352003-10-05 00:17:43 +00001864Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001865 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001866 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001867 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1868 Idxs+NumIdx) ==
1869 cast<PointerType>(ReqTy)->getElementType() &&
1870 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001871
Owen Anderson53a52212009-07-13 04:09:18 +00001872 if (Constant *FC = ConstantFoldGetElementPtr(
1873 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001874 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001875
Chris Lattnerb50d1352003-10-05 00:17:43 +00001876 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001877 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001878 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001879 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001880 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001881 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001882 for (unsigned i = 0; i != NumIdx; ++i)
1883 ArgVec.push_back(cast<Constant>(Idxs[i]));
1884 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001885
1886 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001887 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001888}
1889
Chris Lattner302116a2007-01-31 04:40:28 +00001890Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001891 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001892 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001893 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001894 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001895 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001896 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001897 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001898}
1899
Chris Lattner302116a2007-01-31 04:40:28 +00001900Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001901 unsigned NumIdx) {
1902 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001903}
1904
Chris Lattner302116a2007-01-31 04:40:28 +00001905
Reid Spenceree3c9912006-12-04 05:19:50 +00001906Constant *
1907ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1908 assert(LHS->getType() == RHS->getType());
1909 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1910 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1911
Owen Anderson53a52212009-07-13 04:09:18 +00001912 if (Constant *FC = ConstantFoldCompareInstruction(
1913 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001914 return FC; // Fold a few common cases...
1915
1916 // Look up the constant in the table first to ensure uniqueness
1917 std::vector<Constant*> ArgVec;
1918 ArgVec.push_back(LHS);
1919 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001920 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001921 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001922
1923 // Implicitly locked.
1924 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001925}
1926
1927Constant *
1928ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1929 assert(LHS->getType() == RHS->getType());
1930 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1931
Owen Anderson53a52212009-07-13 04:09:18 +00001932 if (Constant *FC = ConstantFoldCompareInstruction(
1933 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001934 return FC; // Fold a few common cases...
1935
1936 // Look up the constant in the table first to ensure uniqueness
1937 std::vector<Constant*> ArgVec;
1938 ArgVec.push_back(LHS);
1939 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001940 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001941 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001942
1943 // Implicitly locked.
1944 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001945}
1946
Robert Bocchino23004482006-01-10 19:05:34 +00001947Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1948 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001949 if (Constant *FC = ConstantFoldExtractElementInstruction(
1950 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001951 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001952 // Look up the constant in the table first to ensure uniqueness
1953 std::vector<Constant*> ArgVec(1, Val);
1954 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001955 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001956
1957 // Implicitly locked.
1958 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001959}
1960
1961Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001962 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001963 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001964 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001965 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001966 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001967 Val, Idx);
1968}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001969
Robert Bocchinoca27f032006-01-17 20:07:22 +00001970Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1971 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001972 if (Constant *FC = ConstantFoldInsertElementInstruction(
1973 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001974 return FC; // Fold a few common cases...
1975 // Look up the constant in the table first to ensure uniqueness
1976 std::vector<Constant*> ArgVec(1, Val);
1977 ArgVec.push_back(Elt);
1978 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001979 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001980
1981 // Implicitly locked.
1982 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001983}
1984
1985Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1986 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001987 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001988 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001989 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001990 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001991 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001992 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001993 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001994}
1995
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001996Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1997 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001998 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1999 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002000 return FC; // Fold a few common cases...
2001 // Look up the constant in the table first to ensure uniqueness
2002 std::vector<Constant*> ArgVec(1, V1);
2003 ArgVec.push_back(V2);
2004 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002005 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002006
2007 // Implicitly locked.
2008 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002009}
2010
2011Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2012 Constant *Mask) {
2013 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2014 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002015
2016 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2017 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2018 const Type *ShufTy = VectorType::get(EltTy, NElts);
2019 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002020}
2021
Dan Gohman12fce772008-05-15 19:50:34 +00002022Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2023 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002024 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002025 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2026 Idxs+NumIdx) == Val->getType() &&
2027 "insertvalue indices invalid!");
2028 assert(Agg->getType() == ReqTy &&
2029 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002030 assert(Agg->getType()->isFirstClassType() &&
2031 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002032 Constant *FC = ConstantFoldInsertValueInstruction(
2033 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002034 assert(FC && "InsertValue constant expr couldn't be folded!");
2035 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002036}
2037
2038Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002039 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002040 assert(Agg->getType()->isFirstClassType() &&
2041 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002042
Dan Gohman0752bff2008-05-23 00:36:11 +00002043 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002044#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002045 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002046 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002047#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002048 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002049 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2050}
2051
2052Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002053 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002054 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2055 Idxs+NumIdx) == ReqTy &&
2056 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002057 assert(Agg->getType()->isFirstClassType() &&
2058 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002059 Constant *FC = ConstantFoldExtractValueInstruction(
2060 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002061 assert(FC && "ExtractValue constant expr couldn't be folded!");
2062 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002063}
2064
2065Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002066 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002067 assert(Agg->getType()->isFirstClassType() &&
2068 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002069
2070 const Type *ReqTy =
2071 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2072 assert(ReqTy && "extractvalue indices invalid!");
2073 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2074}
2075
Vikram S. Adve4c485332002-07-15 18:19:33 +00002076// destroyConstant - Remove the constant from the constant table...
2077//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002078void ConstantExpr::destroyConstant() {
2079 // Implicitly locked.
2080 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002081 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002082}
2083
Chris Lattner3cd8c562002-07-30 18:54:25 +00002084const char *ConstantExpr::getOpcodeName() const {
2085 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002086}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002087
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002088//===----------------------------------------------------------------------===//
2089// replaceUsesOfWithOnConstant implementations
2090
Chris Lattner913849b2007-08-21 00:55:23 +00002091/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2092/// 'From' to be uses of 'To'. This must update the uniquing data structures
2093/// etc.
2094///
2095/// Note that we intentionally replace all uses of From with To here. Consider
2096/// a large array that uses 'From' 1000 times. By handling this case all here,
2097/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2098/// single invocation handles all 1000 uses. Handling them one at a time would
2099/// work, but would be really slow because it would have to unique each updated
2100/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002101void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002102 Use *U) {
Owen Anderson3d344922009-07-21 20:55:28 +00002103 Constant *Replacement =
2104 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
2105
2106 if (!Replacement) return;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002107
2108 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002109 assert(Replacement != this && "I didn't contain From!");
2110
Chris Lattner7a1450d2005-10-04 18:13:04 +00002111 // Everyone using this now uses the replacement.
2112 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002113
2114 // Delete the old constant!
2115 destroyConstant();
2116}
2117
2118void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002119 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002120 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002121 Constant *ToC = cast<Constant>(To);
2122
Chris Lattnerdff59112005-10-04 18:47:09 +00002123 unsigned OperandToUpdate = U-OperandList;
2124 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2125
Jim Laskeyc03caef2006-07-17 17:38:29 +00002126 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002127 Lookup.first.first = getType();
2128 Lookup.second = this;
2129 std::vector<Constant*> &Values = Lookup.first.second;
2130 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002131
Chris Lattnerdff59112005-10-04 18:47:09 +00002132
Chris Lattner8760ec72005-10-04 01:17:50 +00002133 // Fill values with the modified operands of the constant struct. Also,
2134 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002135 bool isAllZeros = false;
2136 if (!ToC->isNullValue()) {
2137 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2138 Values.push_back(cast<Constant>(O->get()));
2139 } else {
2140 isAllZeros = true;
2141 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2142 Constant *Val = cast<Constant>(O->get());
2143 Values.push_back(Val);
2144 if (isAllZeros) isAllZeros = Val->isNullValue();
2145 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002146 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002147 Values[OperandToUpdate] = ToC;
2148
Chris Lattner8760ec72005-10-04 01:17:50 +00002149 Constant *Replacement = 0;
2150 if (isAllZeros) {
Owen Anderson39ede7b2009-07-21 20:13:12 +00002151 Replacement = getType()->getContext().getConstantAggregateZero(getType());
Chris Lattner8760ec72005-10-04 01:17:50 +00002152 } else {
2153 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002154 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002155 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002156 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002157 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002158
2159 if (Exists) {
2160 Replacement = I->second;
2161 } else {
2162 // Okay, the new shape doesn't exist in the system yet. Instead of
2163 // creating a new constant struct, inserting it, replaceallusesof'ing the
2164 // old with the new, then deleting the old... just update the current one
2165 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002166 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002167
Chris Lattnerdff59112005-10-04 18:47:09 +00002168 // Update to the new value.
2169 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002170 return;
2171 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002172 }
2173
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002174 assert(Replacement != this && "I didn't contain From!");
2175
Chris Lattner7a1450d2005-10-04 18:13:04 +00002176 // Everyone using this now uses the replacement.
2177 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002178
2179 // Delete the old constant!
2180 destroyConstant();
2181}
2182
Reid Spencerd84d35b2007-02-15 02:26:10 +00002183void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002184 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002185 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2186
2187 std::vector<Constant*> Values;
2188 Values.reserve(getNumOperands()); // Build replacement array...
2189 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2190 Constant *Val = getOperand(i);
2191 if (Val == From) Val = cast<Constant>(To);
2192 Values.push_back(Val);
2193 }
2194
Reid Spencerd84d35b2007-02-15 02:26:10 +00002195 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002196 assert(Replacement != this && "I didn't contain From!");
2197
Chris Lattner7a1450d2005-10-04 18:13:04 +00002198 // Everyone using this now uses the replacement.
2199 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002200
2201 // Delete the old constant!
2202 destroyConstant();
2203}
2204
2205void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002206 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002207 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2208 Constant *To = cast<Constant>(ToV);
2209
2210 Constant *Replacement = 0;
2211 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002212 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002213 Constant *Pointer = getOperand(0);
2214 Indices.reserve(getNumOperands()-1);
2215 if (Pointer == From) Pointer = To;
2216
2217 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2218 Constant *Val = getOperand(i);
2219 if (Val == From) Val = To;
2220 Indices.push_back(Val);
2221 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002222 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2223 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002224 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002225 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002226 if (Agg == From) Agg = To;
2227
Dan Gohman1ecaf452008-05-31 00:58:22 +00002228 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002229 Replacement = ConstantExpr::getExtractValue(Agg,
2230 &Indices[0], Indices.size());
2231 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002232 Constant *Agg = getOperand(0);
2233 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002234 if (Agg == From) Agg = To;
2235 if (Val == From) Val = To;
2236
Dan Gohman1ecaf452008-05-31 00:58:22 +00002237 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002238 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2239 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002241 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002243 } else if (getOpcode() == Instruction::Select) {
2244 Constant *C1 = getOperand(0);
2245 Constant *C2 = getOperand(1);
2246 Constant *C3 = getOperand(2);
2247 if (C1 == From) C1 = To;
2248 if (C2 == From) C2 = To;
2249 if (C3 == From) C3 = To;
2250 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002251 } else if (getOpcode() == Instruction::ExtractElement) {
2252 Constant *C1 = getOperand(0);
2253 Constant *C2 = getOperand(1);
2254 if (C1 == From) C1 = To;
2255 if (C2 == From) C2 = To;
2256 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002257 } else if (getOpcode() == Instruction::InsertElement) {
2258 Constant *C1 = getOperand(0);
2259 Constant *C2 = getOperand(1);
2260 Constant *C3 = getOperand(1);
2261 if (C1 == From) C1 = To;
2262 if (C2 == From) C2 = To;
2263 if (C3 == From) C3 = To;
2264 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2265 } else if (getOpcode() == Instruction::ShuffleVector) {
2266 Constant *C1 = getOperand(0);
2267 Constant *C2 = getOperand(1);
2268 Constant *C3 = getOperand(2);
2269 if (C1 == From) C1 = To;
2270 if (C2 == From) C2 = To;
2271 if (C3 == From) C3 = To;
2272 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002273 } else if (isCompare()) {
2274 Constant *C1 = getOperand(0);
2275 Constant *C2 = getOperand(1);
2276 if (C1 == From) C1 = To;
2277 if (C2 == From) C2 = To;
2278 if (getOpcode() == Instruction::ICmp)
2279 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002280 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002281 assert(getOpcode() == Instruction::FCmp);
2282 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002283 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002284 } else if (getNumOperands() == 2) {
2285 Constant *C1 = getOperand(0);
2286 Constant *C2 = getOperand(1);
2287 if (C1 == From) C1 = To;
2288 if (C2 == From) C2 = To;
2289 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2290 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002291 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002292 return;
2293 }
2294
2295 assert(Replacement != this && "I didn't contain From!");
2296
Chris Lattner7a1450d2005-10-04 18:13:04 +00002297 // Everyone using this now uses the replacement.
2298 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002299
2300 // Delete the old constant!
2301 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002302}
Nick Lewycky49f89192009-04-04 07:22:01 +00002303
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002304void MDNode::replaceElement(Value *From, Value *To) {
2305 SmallVector<Value*, 4> Values;
2306 Values.reserve(getNumElements()); // Build replacement array...
2307 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2308 Value *Val = getElement(i);
2309 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002310 Values.push_back(Val);
2311 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002312
Owen Anderson4118dde2009-07-16 23:44:30 +00002313 MDNode *Replacement =
2314 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002315 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002316
Nick Lewycky49f89192009-04-04 07:22:01 +00002317 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002318
Nick Lewycky49f89192009-04-04 07:22:01 +00002319 destroyConstant();
2320}