blob: 065ebcd5dc9da6452974e4caf5c9d6e7133b656b [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)
Devang Patele059ba6e2009-07-23 01:07:34 +00001330 : MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001331 for (unsigned i = 0; i != NumVals; ++i)
Devang Patele059ba6e2009-07-23 01:07:34 +00001332 Node.push_back(WeakVH(Vals[i]));
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
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001340//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001341//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001342
Dan Gohmand78c4002008-05-13 00:00:25 +00001343namespace {
1344
Reid Spenceree3c9912006-12-04 05:19:50 +00001345struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001346 typedef SmallVector<unsigned, 4> IndexList;
1347
1348 ExprMapKeyType(unsigned opc,
1349 const std::vector<Constant*> &ops,
1350 unsigned short pred = 0,
1351 const IndexList &inds = IndexList())
1352 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001353 uint16_t opcode;
1354 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001355 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001356 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001357 bool operator==(const ExprMapKeyType& that) const {
1358 return this->opcode == that.opcode &&
1359 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001360 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001361 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001362 }
1363 bool operator<(const ExprMapKeyType & that) const {
1364 return this->opcode < that.opcode ||
1365 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1366 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001367 this->operands < that.operands) ||
1368 (this->opcode == that.opcode && this->predicate == that.predicate &&
1369 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001370 }
1371
1372 bool operator!=(const ExprMapKeyType& that) const {
1373 return !(*this == that);
1374 }
1375};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001376
Dan Gohmand78c4002008-05-13 00:00:25 +00001377}
1378
Chris Lattner189d19f2003-11-21 20:23:48 +00001379namespace llvm {
1380 template<>
1381 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001382 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1383 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001384 if (Instruction::isCast(V.opcode))
1385 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1386 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001387 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001388 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1389 if (V.opcode == Instruction::Select)
1390 return new SelectConstantExpr(V.operands[0], V.operands[1],
1391 V.operands[2]);
1392 if (V.opcode == Instruction::ExtractElement)
1393 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1394 if (V.opcode == Instruction::InsertElement)
1395 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1396 V.operands[2]);
1397 if (V.opcode == Instruction::ShuffleVector)
1398 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1399 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001400 if (V.opcode == Instruction::InsertValue)
1401 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1402 V.indices, Ty);
1403 if (V.opcode == Instruction::ExtractValue)
1404 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001405 if (V.opcode == Instruction::GetElementPtr) {
1406 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001407 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001408 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001409
Reid Spenceree3c9912006-12-04 05:19:50 +00001410 // The compare instructions are weird. We have to encode the predicate
1411 // value and it is combined with the instruction opcode by multiplying
1412 // the opcode by one hundred. We must decode this to get the predicate.
1413 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001414 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001415 V.operands[0], V.operands[1]);
1416 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001417 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1418 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001419 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001420 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001421 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001422 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001423
Chris Lattner189d19f2003-11-21 20:23:48 +00001424 template<>
1425 struct ConvertConstantType<ConstantExpr, Type> {
1426 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1427 Constant *New;
1428 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001429 case Instruction::Trunc:
1430 case Instruction::ZExt:
1431 case Instruction::SExt:
1432 case Instruction::FPTrunc:
1433 case Instruction::FPExt:
1434 case Instruction::UIToFP:
1435 case Instruction::SIToFP:
1436 case Instruction::FPToUI:
1437 case Instruction::FPToSI:
1438 case Instruction::PtrToInt:
1439 case Instruction::IntToPtr:
1440 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001441 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001442 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001443 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001444 case Instruction::Select:
1445 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1446 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001447 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001448 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001449 default:
1450 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001451 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001452 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001453 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001454 break;
1455 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001456 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001457 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001458 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001459 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001460 break;
1461 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001462
Chris Lattner189d19f2003-11-21 20:23:48 +00001463 assert(New != OldC && "Didn't replace constant??");
1464 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001465 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001466 }
1467 };
1468} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001469
1470
Chris Lattner3e650af2004-08-04 04:48:01 +00001471static ExprMapKeyType getValType(ConstantExpr *CE) {
1472 std::vector<Constant*> Operands;
1473 Operands.reserve(CE->getNumOperands());
1474 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1475 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001476 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001477 CE->isCompare() ? CE->getPredicate() : 0,
1478 CE->hasIndices() ?
1479 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001480}
1481
Chris Lattner69edc982006-09-28 00:35:06 +00001482static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1483 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001484
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001485/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001486/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001487static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001488 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001489 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001490 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001491 if (Constant *FC =
1492 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001493 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001494
Vikram S. Adve4c485332002-07-15 18:19:33 +00001495 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001496 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001497 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001498
Owen Anderson61794042009-06-17 20:10:08 +00001499 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001500 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001501}
Reid Spencerf37dc652006-12-05 19:14:13 +00001502
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001503Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001504 Instruction::CastOps opc = Instruction::CastOps(oc);
1505 assert(Instruction::isCast(opc) && "opcode out of range");
1506 assert(C && Ty && "Null arguments to getCast");
1507 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1508
1509 switch (opc) {
1510 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001511 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001512 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001513 case Instruction::Trunc: return getTrunc(C, Ty);
1514 case Instruction::ZExt: return getZExt(C, Ty);
1515 case Instruction::SExt: return getSExt(C, Ty);
1516 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1517 case Instruction::FPExt: return getFPExtend(C, Ty);
1518 case Instruction::UIToFP: return getUIToFP(C, Ty);
1519 case Instruction::SIToFP: return getSIToFP(C, Ty);
1520 case Instruction::FPToUI: return getFPToUI(C, Ty);
1521 case Instruction::FPToSI: return getFPToSI(C, Ty);
1522 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1523 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1524 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001525 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001526 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001527}
1528
Reid Spencer5c140882006-12-04 20:17:56 +00001529Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001530 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001531 return getCast(Instruction::BitCast, C, Ty);
1532 return getCast(Instruction::ZExt, C, Ty);
1533}
1534
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001535Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001536 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001537 return getCast(Instruction::BitCast, C, Ty);
1538 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001539}
1540
1541Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001542 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001543 return getCast(Instruction::BitCast, C, Ty);
1544 return getCast(Instruction::Trunc, C, Ty);
1545}
1546
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001547Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001548 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001549 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001550
Chris Lattner03c49532007-01-15 02:27:26 +00001551 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001552 return getCast(Instruction::PtrToInt, S, Ty);
1553 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001554}
1555
Reid Spencer56521c42006-12-12 00:51:07 +00001556Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1557 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001558 assert(C->getType()->isIntOrIntVector() &&
1559 Ty->isIntOrIntVector() && "Invalid cast");
1560 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1561 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001562 Instruction::CastOps opcode =
1563 (SrcBits == DstBits ? Instruction::BitCast :
1564 (SrcBits > DstBits ? Instruction::Trunc :
1565 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1566 return getCast(opcode, C, Ty);
1567}
1568
1569Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001570 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001571 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001572 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1573 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001574 if (SrcBits == DstBits)
1575 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001576 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001577 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001578 return getCast(opcode, C, Ty);
1579}
1580
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001581Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001582#ifndef NDEBUG
1583 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1584 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1585#endif
1586 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1587 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1588 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1589 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001590 "SrcTy must be larger than DestTy for Trunc!");
1591
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001592 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001593}
1594
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001595Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001596#ifndef NDEBUG
1597 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1598 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1599#endif
1600 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1601 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1602 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1603 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001604 "SrcTy must be smaller than DestTy for SExt!");
1605
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001606 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001607}
1608
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001609Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001610#ifndef NDEBUG
1611 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1612 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1613#endif
1614 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1615 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1616 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1617 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001618 "SrcTy must be smaller than DestTy for ZExt!");
1619
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001620 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001621}
1622
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001623Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001624#ifndef NDEBUG
1625 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1626 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1627#endif
1628 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1629 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1630 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001631 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001632 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001633}
1634
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001635Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001636#ifndef NDEBUG
1637 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1638 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1639#endif
1640 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1641 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1642 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001643 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001644 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001645}
1646
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001647Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001648#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001649 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1650 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001651#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001652 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1653 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1654 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001655 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001656}
1657
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001658Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001659#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001660 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1661 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001662#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001663 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1664 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001665 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001666 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001667}
1668
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001669Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001670#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001671 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1672 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001673#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001674 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1675 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1676 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001677 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001678}
1679
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001680Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001681#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001682 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1683 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001684#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001685 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1686 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1687 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001688 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001689}
1690
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001691Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001692 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001693 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001694 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001695}
1696
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001697Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001698 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001699 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001700 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001701}
1702
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001703Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001704 // BitCast implies a no-op cast of type only. No bits change. However, you
1705 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001706#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001707 const Type *SrcTy = C->getType();
1708 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001709 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001710
1711 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1712 // or nonptr->ptr). For all the other types, the cast is okay if source and
1713 // destination bit widths are identical.
1714 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1715 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001716#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001717 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001718
1719 // It is common to ask for a bitcast of a value to its own type, handle this
1720 // speedily.
1721 if (C->getType() == DstTy) return C;
1722
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001723 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001724}
1725
Chris Lattnerb50d1352003-10-05 00:17:43 +00001726Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001727 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001728 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001729 assert(Opcode >= Instruction::BinaryOpsBegin &&
1730 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001731 "Invalid opcode in binary constant expression");
1732 assert(C1->getType() == C2->getType() &&
1733 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001734
Reid Spencer542964f2007-01-11 18:21:29 +00001735 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001736 if (Constant *FC = ConstantFoldBinaryInstruction(
1737 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001738 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001739
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001740 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001741 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001742
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001743 // Implicitly locked.
1744 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001745}
1746
Reid Spencer266e42b2006-12-23 06:05:41 +00001747Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001748 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001749 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001750 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001751 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1752 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1753 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1754 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1755 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1756 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001757 return getFCmp(predicate, C1, C2);
1758
Nate Begemanc96e2e42008-07-25 17:35:37 +00001759 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1760 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1761 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1762 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001763 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001764 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001765}
1766
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001767Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001768 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1769 if (C1->getType()->isFPOrFPVector()) {
1770 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1771 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1772 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1773 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001774#ifndef NDEBUG
1775 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001776 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001777 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001778 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001779 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001780 assert(C1->getType()->isIntOrIntVector() &&
1781 "Tried to create an integer operation on a non-integer type!");
1782 break;
1783 case Instruction::FAdd:
1784 case Instruction::FSub:
1785 case Instruction::FMul:
1786 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1787 assert(C1->getType()->isFPOrFPVector() &&
1788 "Tried to create a floating-point operation on a "
1789 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001790 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001791 case Instruction::UDiv:
1792 case Instruction::SDiv:
1793 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001794 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001795 "Tried to create an arithmetic operation on a non-arithmetic type!");
1796 break;
1797 case Instruction::FDiv:
1798 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001799 assert(C1->getType()->isFPOrFPVector() &&
1800 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001801 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001802 case Instruction::URem:
1803 case Instruction::SRem:
1804 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001805 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001806 "Tried to create an arithmetic operation on a non-arithmetic type!");
1807 break;
1808 case Instruction::FRem:
1809 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001810 assert(C1->getType()->isFPOrFPVector() &&
1811 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001812 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001813 case Instruction::And:
1814 case Instruction::Or:
1815 case Instruction::Xor:
1816 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001817 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001818 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001819 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001820 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001821 case Instruction::LShr:
1822 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001823 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001824 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001825 "Tried to create a shift operation on a non-integer type!");
1826 break;
1827 default:
1828 break;
1829 }
1830#endif
1831
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001832 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001833}
1834
Reid Spencer266e42b2006-12-23 06:05:41 +00001835Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001836 Constant *C1, Constant *C2) {
1837 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001838 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001839}
1840
Chris Lattner6e415c02004-03-12 05:54:04 +00001841Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001842 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001843 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001844
1845 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001846 if (Constant *SC = ConstantFoldSelectInstruction(
1847 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001848 return SC; // Fold common cases
1849
1850 std::vector<Constant*> argVec(3, C);
1851 argVec[1] = V1;
1852 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001853 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001854
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001855 // Implicitly locked.
1856 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001857}
1858
Chris Lattnerb50d1352003-10-05 00:17:43 +00001859Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001860 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001861 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001862 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1863 Idxs+NumIdx) ==
1864 cast<PointerType>(ReqTy)->getElementType() &&
1865 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001866
Owen Anderson53a52212009-07-13 04:09:18 +00001867 if (Constant *FC = ConstantFoldGetElementPtr(
1868 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001869 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001870
Chris Lattnerb50d1352003-10-05 00:17:43 +00001871 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001872 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001873 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001874 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001875 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001876 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001877 for (unsigned i = 0; i != NumIdx; ++i)
1878 ArgVec.push_back(cast<Constant>(Idxs[i]));
1879 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001880
1881 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001882 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001883}
1884
Chris Lattner302116a2007-01-31 04:40:28 +00001885Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001886 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001887 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001888 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001889 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001890 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001891 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001892 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001893}
1894
Chris Lattner302116a2007-01-31 04:40:28 +00001895Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001896 unsigned NumIdx) {
1897 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001898}
1899
Chris Lattner302116a2007-01-31 04:40:28 +00001900
Reid Spenceree3c9912006-12-04 05:19:50 +00001901Constant *
1902ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1903 assert(LHS->getType() == RHS->getType());
1904 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1905 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1906
Owen Anderson53a52212009-07-13 04:09:18 +00001907 if (Constant *FC = ConstantFoldCompareInstruction(
1908 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001909 return FC; // Fold a few common cases...
1910
1911 // Look up the constant in the table first to ensure uniqueness
1912 std::vector<Constant*> ArgVec;
1913 ArgVec.push_back(LHS);
1914 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001915 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001916 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001917
1918 // Implicitly locked.
1919 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001920}
1921
1922Constant *
1923ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1924 assert(LHS->getType() == RHS->getType());
1925 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1926
Owen Anderson53a52212009-07-13 04:09:18 +00001927 if (Constant *FC = ConstantFoldCompareInstruction(
1928 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001929 return FC; // Fold a few common cases...
1930
1931 // Look up the constant in the table first to ensure uniqueness
1932 std::vector<Constant*> ArgVec;
1933 ArgVec.push_back(LHS);
1934 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001935 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001936 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001937
1938 // Implicitly locked.
1939 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001940}
1941
Robert Bocchino23004482006-01-10 19:05:34 +00001942Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1943 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001944 if (Constant *FC = ConstantFoldExtractElementInstruction(
1945 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001946 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001947 // Look up the constant in the table first to ensure uniqueness
1948 std::vector<Constant*> ArgVec(1, Val);
1949 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001950 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001951
1952 // Implicitly locked.
1953 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001954}
1955
1956Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001957 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001958 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001959 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001960 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001961 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001962 Val, Idx);
1963}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001964
Robert Bocchinoca27f032006-01-17 20:07:22 +00001965Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1966 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001967 if (Constant *FC = ConstantFoldInsertElementInstruction(
1968 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001969 return FC; // Fold a few common cases...
1970 // Look up the constant in the table first to ensure uniqueness
1971 std::vector<Constant*> ArgVec(1, Val);
1972 ArgVec.push_back(Elt);
1973 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001974 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001975
1976 // Implicitly locked.
1977 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001978}
1979
1980Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1981 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001982 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001983 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001984 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001985 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001986 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001987 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001988 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001989}
1990
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001991Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1992 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001993 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1994 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001995 return FC; // Fold a few common cases...
1996 // Look up the constant in the table first to ensure uniqueness
1997 std::vector<Constant*> ArgVec(1, V1);
1998 ArgVec.push_back(V2);
1999 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002000 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002001
2002 // Implicitly locked.
2003 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002004}
2005
2006Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2007 Constant *Mask) {
2008 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2009 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002010
2011 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2012 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2013 const Type *ShufTy = VectorType::get(EltTy, NElts);
2014 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002015}
2016
Dan Gohman12fce772008-05-15 19:50:34 +00002017Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2018 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002019 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002020 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2021 Idxs+NumIdx) == Val->getType() &&
2022 "insertvalue indices invalid!");
2023 assert(Agg->getType() == ReqTy &&
2024 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002025 assert(Agg->getType()->isFirstClassType() &&
2026 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002027 Constant *FC = ConstantFoldInsertValueInstruction(
2028 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002029 assert(FC && "InsertValue constant expr couldn't be folded!");
2030 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002031}
2032
2033Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002034 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002035 assert(Agg->getType()->isFirstClassType() &&
2036 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002037
Dan Gohman0752bff2008-05-23 00:36:11 +00002038 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002039#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002040 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002041 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002042#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002043 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002044 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2045}
2046
2047Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002048 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002049 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2050 Idxs+NumIdx) == ReqTy &&
2051 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002052 assert(Agg->getType()->isFirstClassType() &&
2053 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002054 Constant *FC = ConstantFoldExtractValueInstruction(
2055 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002056 assert(FC && "ExtractValue constant expr couldn't be folded!");
2057 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002058}
2059
2060Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002061 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002062 assert(Agg->getType()->isFirstClassType() &&
2063 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002064
2065 const Type *ReqTy =
2066 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2067 assert(ReqTy && "extractvalue indices invalid!");
2068 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2069}
2070
Vikram S. Adve4c485332002-07-15 18:19:33 +00002071// destroyConstant - Remove the constant from the constant table...
2072//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002073void ConstantExpr::destroyConstant() {
2074 // Implicitly locked.
2075 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002076 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002077}
2078
Chris Lattner3cd8c562002-07-30 18:54:25 +00002079const char *ConstantExpr::getOpcodeName() const {
2080 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002081}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002082
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002083//===----------------------------------------------------------------------===//
2084// replaceUsesOfWithOnConstant implementations
2085
Chris Lattner913849b2007-08-21 00:55:23 +00002086/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2087/// 'From' to be uses of 'To'. This must update the uniquing data structures
2088/// etc.
2089///
2090/// Note that we intentionally replace all uses of From with To here. Consider
2091/// a large array that uses 'From' 1000 times. By handling this case all here,
2092/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2093/// single invocation handles all 1000 uses. Handling them one at a time would
2094/// work, but would be really slow because it would have to unique each updated
2095/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002096void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002097 Use *U) {
Owen Anderson3d344922009-07-21 20:55:28 +00002098 Constant *Replacement =
2099 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
2100
2101 if (!Replacement) return;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002102
2103 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002104 assert(Replacement != this && "I didn't contain From!");
2105
Chris Lattner7a1450d2005-10-04 18:13:04 +00002106 // Everyone using this now uses the replacement.
2107 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002108
2109 // Delete the old constant!
2110 destroyConstant();
2111}
2112
2113void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002114 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002115 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002116 Constant *ToC = cast<Constant>(To);
2117
Chris Lattnerdff59112005-10-04 18:47:09 +00002118 unsigned OperandToUpdate = U-OperandList;
2119 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2120
Jim Laskeyc03caef2006-07-17 17:38:29 +00002121 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002122 Lookup.first.first = getType();
2123 Lookup.second = this;
2124 std::vector<Constant*> &Values = Lookup.first.second;
2125 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002126
Chris Lattnerdff59112005-10-04 18:47:09 +00002127
Chris Lattner8760ec72005-10-04 01:17:50 +00002128 // Fill values with the modified operands of the constant struct. Also,
2129 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002130 bool isAllZeros = false;
2131 if (!ToC->isNullValue()) {
2132 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2133 Values.push_back(cast<Constant>(O->get()));
2134 } else {
2135 isAllZeros = true;
2136 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2137 Constant *Val = cast<Constant>(O->get());
2138 Values.push_back(Val);
2139 if (isAllZeros) isAllZeros = Val->isNullValue();
2140 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002141 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002142 Values[OperandToUpdate] = ToC;
2143
Chris Lattner8760ec72005-10-04 01:17:50 +00002144 Constant *Replacement = 0;
2145 if (isAllZeros) {
Owen Anderson39ede7b2009-07-21 20:13:12 +00002146 Replacement = getType()->getContext().getConstantAggregateZero(getType());
Chris Lattner8760ec72005-10-04 01:17:50 +00002147 } else {
2148 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002149 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002150 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002151 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002152 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002153
2154 if (Exists) {
2155 Replacement = I->second;
2156 } else {
2157 // Okay, the new shape doesn't exist in the system yet. Instead of
2158 // creating a new constant struct, inserting it, replaceallusesof'ing the
2159 // old with the new, then deleting the old... just update the current one
2160 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002161 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002162
Chris Lattnerdff59112005-10-04 18:47:09 +00002163 // Update to the new value.
2164 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002165 return;
2166 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002167 }
2168
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002169 assert(Replacement != this && "I didn't contain From!");
2170
Chris Lattner7a1450d2005-10-04 18:13:04 +00002171 // Everyone using this now uses the replacement.
2172 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002173
2174 // Delete the old constant!
2175 destroyConstant();
2176}
2177
Reid Spencerd84d35b2007-02-15 02:26:10 +00002178void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002179 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002180 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2181
2182 std::vector<Constant*> Values;
2183 Values.reserve(getNumOperands()); // Build replacement array...
2184 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2185 Constant *Val = getOperand(i);
2186 if (Val == From) Val = cast<Constant>(To);
2187 Values.push_back(Val);
2188 }
2189
Reid Spencerd84d35b2007-02-15 02:26:10 +00002190 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002191 assert(Replacement != this && "I didn't contain From!");
2192
Chris Lattner7a1450d2005-10-04 18:13:04 +00002193 // Everyone using this now uses the replacement.
2194 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002195
2196 // Delete the old constant!
2197 destroyConstant();
2198}
2199
2200void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002201 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002202 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2203 Constant *To = cast<Constant>(ToV);
2204
2205 Constant *Replacement = 0;
2206 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002207 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002208 Constant *Pointer = getOperand(0);
2209 Indices.reserve(getNumOperands()-1);
2210 if (Pointer == From) Pointer = To;
2211
2212 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2213 Constant *Val = getOperand(i);
2214 if (Val == From) Val = To;
2215 Indices.push_back(Val);
2216 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002217 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2218 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002219 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002220 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002221 if (Agg == From) Agg = To;
2222
Dan Gohman1ecaf452008-05-31 00:58:22 +00002223 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002224 Replacement = ConstantExpr::getExtractValue(Agg,
2225 &Indices[0], Indices.size());
2226 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002227 Constant *Agg = getOperand(0);
2228 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002229 if (Agg == From) Agg = To;
2230 if (Val == From) Val = To;
2231
Dan Gohman1ecaf452008-05-31 00:58:22 +00002232 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002233 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2234 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002236 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002238 } else if (getOpcode() == Instruction::Select) {
2239 Constant *C1 = getOperand(0);
2240 Constant *C2 = getOperand(1);
2241 Constant *C3 = getOperand(2);
2242 if (C1 == From) C1 = To;
2243 if (C2 == From) C2 = To;
2244 if (C3 == From) C3 = To;
2245 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002246 } else if (getOpcode() == Instruction::ExtractElement) {
2247 Constant *C1 = getOperand(0);
2248 Constant *C2 = getOperand(1);
2249 if (C1 == From) C1 = To;
2250 if (C2 == From) C2 = To;
2251 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002252 } else if (getOpcode() == Instruction::InsertElement) {
2253 Constant *C1 = getOperand(0);
2254 Constant *C2 = getOperand(1);
2255 Constant *C3 = getOperand(1);
2256 if (C1 == From) C1 = To;
2257 if (C2 == From) C2 = To;
2258 if (C3 == From) C3 = To;
2259 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2260 } else if (getOpcode() == Instruction::ShuffleVector) {
2261 Constant *C1 = getOperand(0);
2262 Constant *C2 = getOperand(1);
2263 Constant *C3 = getOperand(2);
2264 if (C1 == From) C1 = To;
2265 if (C2 == From) C2 = To;
2266 if (C3 == From) C3 = To;
2267 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002268 } else if (isCompare()) {
2269 Constant *C1 = getOperand(0);
2270 Constant *C2 = getOperand(1);
2271 if (C1 == From) C1 = To;
2272 if (C2 == From) C2 = To;
2273 if (getOpcode() == Instruction::ICmp)
2274 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002275 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002276 assert(getOpcode() == Instruction::FCmp);
2277 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002278 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002279 } else if (getNumOperands() == 2) {
2280 Constant *C1 = getOperand(0);
2281 Constant *C2 = getOperand(1);
2282 if (C1 == From) C1 = To;
2283 if (C2 == From) C2 = To;
2284 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2285 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002286 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002287 return;
2288 }
2289
2290 assert(Replacement != this && "I didn't contain From!");
2291
Chris Lattner7a1450d2005-10-04 18:13:04 +00002292 // Everyone using this now uses the replacement.
2293 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002294
2295 // Delete the old constant!
2296 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002297}
Nick Lewycky49f89192009-04-04 07:22:01 +00002298
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002299void MDNode::replaceElement(Value *From, Value *To) {
2300 SmallVector<Value*, 4> Values;
2301 Values.reserve(getNumElements()); // Build replacement array...
2302 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2303 Value *Val = getElement(i);
2304 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002305 Values.push_back(Val);
2306 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002307
Owen Anderson4118dde2009-07-16 23:44:30 +00002308 MDNode *Replacement =
2309 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002310 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002311
Nick Lewycky49f89192009-04-04 07:22:01 +00002312 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewycky49f89192009-04-04 07:22:01 +00002313}