blob: cf01a9feef89eabd623d5fcb368ebfd0786e17a0 [file] [log] [blame]
Chris Lattner9bc02a42003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner92f6fea2007-02-27 03:05:06 +000015#include "ConstantFold.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1c9c8e62004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Nick Lewyckycb337992009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000020#include "llvm/Module.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000021#include "llvm/ADT/FoldingSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/StringExtras.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000023#include "llvm/ADT/StringMap.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000025#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner8a94bf12006-09-28 00:35:06 +000027#include "llvm/Support/ManagedStatic.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000028#include "llvm/Support/MathExtras.h"
Owen Anderson04fb7c32009-06-20 00:24:58 +000029#include "llvm/System/Mutex.h"
Owen Anderson3e456ab2009-06-17 18:40:29 +000030#include "llvm/System/RWMutex.h"
Owen Andersone3cd5ca2009-06-18 16:54:52 +000031#include "llvm/System/Threading.h"
Chris Lattner6b6f6ba2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerf9021ff2007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner00950542001-06-06 20:29:01 +000034#include <algorithm>
Reid Spenceref9b9a72007-02-05 20:47:22 +000035#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000039// Constant Class
Chris Lattner00950542001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
41
Owen Andersonee6aefc2009-06-18 19:10:19 +000042// Becomes a no-op when multithreading is disabled.
43ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson3e456ab2009-06-17 18:40:29 +000044
Chris Lattnere9bb2df2001-12-03 22:26:30 +000045void Constant::destroyConstantImpl() {
46 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000047 // references to the constant by other constants in the constant pool. These
Misha Brukmanef6a6a62003-08-21 22:14:26 +000048 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000049 // but they don't know that. Because we only find out when the CPV is
50 // deleted, we must now notify all of our users (that should only be
Chris Lattnere9bb2df2001-12-03 22:26:30 +000051 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000052 //
53 while (!use_empty()) {
54 Value *V = use_back();
55#ifndef NDEBUG // Only in -g mode...
Chris Lattner6183b922002-07-18 00:14:50 +000056 if (!isa<Constant>(V))
Bill Wendling2e3def12006-11-17 08:03:48 +000057 DOUT << "While deleting: " << *this
58 << "\n\nUse still stuck around after Def is destroyed: "
59 << *V << "\n\n";
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000060#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000061 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1c9c8e62004-07-17 23:48:33 +000062 Constant *CV = cast<Constant>(V);
63 CV->destroyConstant();
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000064
65 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000066 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000067 }
68
69 // Value has no outstanding references it is safe to delete it now...
70 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +000071}
Chris Lattner00950542001-06-06 20:29:01 +000072
Chris Lattner35b89fa2006-10-20 00:27:06 +000073/// canTrap - Return true if evaluation of this constant could trap. This is
74/// true for things like constant expressions that could divide by zero.
75bool Constant::canTrap() const {
76 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
77 // The only thing that could possibly trap are constant exprs.
78 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
79 if (!CE) return false;
80
81 // ConstantExpr traps if any operands can trap.
82 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
83 if (getOperand(i)->canTrap())
84 return true;
85
86 // Otherwise, only specific operations can trap.
87 switch (CE->getOpcode()) {
88 default:
89 return false;
Reid Spencer1628cec2006-10-26 06:15:43 +000090 case Instruction::UDiv:
91 case Instruction::SDiv:
92 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +000093 case Instruction::URem:
94 case Instruction::SRem:
95 case Instruction::FRem:
Chris Lattner35b89fa2006-10-20 00:27:06 +000096 // Div and rem can trap if the RHS is not known to be non-zero.
97 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
98 return true;
99 return false;
100 }
101}
102
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000103/// ContainsRelocations - Return true if the constant value contains relocations
104/// which cannot be resolved at compile time. Kind argument is used to filter
105/// only 'interesting' sorts of relocations.
106bool Constant::ContainsRelocations(unsigned Kind) const {
107 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
108 bool isLocal = GV->hasLocalLinkage();
109 if ((Kind & Reloc::Local) && isLocal) {
110 // Global has local linkage and 'local' kind of relocations are
111 // requested
112 return true;
113 }
114
115 if ((Kind & Reloc::Global) && !isLocal) {
116 // Global has non-local linkage and 'global' kind of relocations are
117 // requested
118 return true;
119 }
Anton Korobeynikov48738b92009-03-30 15:28:21 +0000120
121 return false;
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000122 }
123
Evan Chengafe15812007-03-08 00:59:12 +0000124 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovfd653072009-03-30 15:28:00 +0000125 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengafe15812007-03-08 00:59:12 +0000126 return true;
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000127
Evan Chengafe15812007-03-08 00:59:12 +0000128 return false;
129}
130
Chris Lattner86381442008-07-10 00:28:11 +0000131/// getVectorElements - This method, which is only valid on constant of vector
132/// type, returns the elements of the vector in the specified smallvector.
Chris Lattner071aade2008-07-14 05:10:41 +0000133/// This handles breaking down a vector undef into undef elements, etc. For
134/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson0a5372e2009-07-13 04:09:18 +0000135void Constant::getVectorElements(LLVMContext &Context,
136 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner86381442008-07-10 00:28:11 +0000137 assert(isa<VectorType>(getType()) && "Not a vector constant!");
138
139 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
140 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
141 Elts.push_back(CV->getOperand(i));
142 return;
143 }
144
145 const VectorType *VT = cast<VectorType>(getType());
146 if (isa<ConstantAggregateZero>(this)) {
147 Elts.assign(VT->getNumElements(),
Owen Anderson0a5372e2009-07-13 04:09:18 +0000148 Context.getNullValue(VT->getElementType()));
Chris Lattner86381442008-07-10 00:28:11 +0000149 return;
150 }
151
Chris Lattner071aade2008-07-14 05:10:41 +0000152 if (isa<UndefValue>(this)) {
Owen Anderson0a5372e2009-07-13 04:09:18 +0000153 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattner071aade2008-07-14 05:10:41 +0000154 return;
155 }
156
157 // Unknown type, must be constant expr etc.
Chris Lattner86381442008-07-10 00:28:11 +0000158}
159
160
161
Chris Lattner00950542001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000163// ConstantInt
Chris Lattner00950542001-06-06 20:29:01 +0000164//===----------------------------------------------------------------------===//
165
Reid Spencer532d0ce2007-02-26 23:54:03 +0000166ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattnereb41bdd2007-02-20 05:55:46 +0000167 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencer532d0ce2007-02-26 23:54:03 +0000168 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner00950542001-06-06 20:29:01 +0000169}
170
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000171ConstantInt *ConstantInt::TheTrueVal = 0;
172ConstantInt *ConstantInt::TheFalseVal = 0;
173
174namespace llvm {
175 void CleanupTrueFalse(void *) {
176 ConstantInt::ResetTrueFalse();
177 }
178}
179
180static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
181
182ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
183 assert(TheTrueVal == 0 && TheFalseVal == 0);
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000184 TheTrueVal = getGlobalContext().getConstantInt(Type::Int1Ty, 1);
185 TheFalseVal = getGlobalContext().getConstantInt(Type::Int1Ty, 0);
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000186
187 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
188 TrueFalseCleanup.Register();
189
190 return WhichOne ? TheTrueVal : TheFalseVal;
191}
192
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000193//===----------------------------------------------------------------------===//
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000194// ConstantFP
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000195//===----------------------------------------------------------------------===//
196
Rafael Espindola87d1f472009-07-15 17:40:42 +0000197static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
198 if (Ty == Type::FloatTy)
199 return &APFloat::IEEEsingle;
200 if (Ty == Type::DoubleTy)
201 return &APFloat::IEEEdouble;
202 if (Ty == Type::X86_FP80Ty)
203 return &APFloat::x87DoubleExtended;
204 else if (Ty == Type::FP128Ty)
205 return &APFloat::IEEEquad;
206
207 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
208 return &APFloat::PPCDoubleDouble;
209}
210
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000211ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
212 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner288e78f2008-04-09 06:38:30 +0000213 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
214 "FP type Mismatch");
Chris Lattner00950542001-06-06 20:29:01 +0000215}
216
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000217bool ConstantFP::isNullValue() const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000218 return Val.isZero() && !Val.isNegative();
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000219}
220
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000221bool ConstantFP::isExactlyValue(const APFloat& V) const {
222 return Val.bitwiseIsEqual(V);
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000223}
224
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000225//===----------------------------------------------------------------------===//
226// ConstantXXX Classes
227//===----------------------------------------------------------------------===//
228
229
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000230ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000231 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000232 : Constant(T, ConstantArrayVal,
233 OperandTraits<ConstantArray>::op_end(this) - V.size(),
234 V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000235 assert(V.size() == T->getNumElements() &&
236 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000237 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000238 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
239 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000240 Constant *C = *I;
241 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000242 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000243 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000244 "Initializer for array element doesn't match array element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000245 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000246 }
247}
248
Chris Lattnere4671472005-01-29 00:34:39 +0000249
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000250ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000251 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000252 : Constant(T, ConstantStructVal,
253 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
254 V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000255 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000256 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000257 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000258 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
259 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000260 Constant *C = *I;
261 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000262 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner71abaab2005-10-07 05:23:36 +0000263 C->getType()->isAbstract()) &&
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000264 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner71abaab2005-10-07 05:23:36 +0000265 C->getType()->getTypeID())) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000266 "Initializer for struct element doesn't match struct element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000267 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000268 }
269}
270
Chris Lattnere4671472005-01-29 00:34:39 +0000271
Reid Spencer9d6565a2007-02-15 02:26:10 +0000272ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000273 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000274 : Constant(T, ConstantVectorVal,
275 OperandTraits<ConstantVector>::op_end(this) - V.size(),
276 V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000277 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000278 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
279 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000280 Constant *C = *I;
281 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000282 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000283 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000284 "Initializer for vector element doesn't match vector element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000285 *OL = C;
Brian Gaeke715c90b2004-08-20 06:00:58 +0000286 }
287}
288
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000289
Gabor Greifefe65362008-05-10 08:32:32 +0000290namespace llvm {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000291// We declare several classes private to this file, so use an anonymous
292// namespace
293namespace {
Reid Spencer728b6db2006-12-03 05:48:19 +0000294
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000295/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
296/// behind the scenes to implement unary constant exprs.
297class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000298 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000299public:
Gabor Greif051a9502008-04-06 20:25:17 +0000300 // allocate space for exactly one operand
301 void *operator new(size_t s) {
302 return User::operator new(s, 1);
303 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000304 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greifefe65362008-05-10 08:32:32 +0000305 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
306 Op<0>() = C;
307 }
308 /// Transparently provide more efficient getOperand methods.
309 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000310};
Reid Spencer728b6db2006-12-03 05:48:19 +0000311
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000312/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
313/// behind the scenes to implement binary constant exprs.
314class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000315 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000316public:
Gabor Greif051a9502008-04-06 20:25:17 +0000317 // allocate space for exactly two operands
318 void *operator new(size_t s) {
319 return User::operator new(s, 2);
320 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000321 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greifefe65362008-05-10 08:32:32 +0000322 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000323 Op<0>() = C1;
324 Op<1>() = C2;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000325 }
Gabor Greifefe65362008-05-10 08:32:32 +0000326 /// Transparently provide more efficient getOperand methods.
327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000328};
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000329
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000330/// SelectConstantExpr - This class is private to Constants.cpp, and is used
331/// behind the scenes to implement select constant exprs.
332class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000333 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000334public:
Gabor Greif051a9502008-04-06 20:25:17 +0000335 // allocate space for exactly three operands
336 void *operator new(size_t s) {
337 return User::operator new(s, 3);
338 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000339 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greifefe65362008-05-10 08:32:32 +0000340 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000341 Op<0>() = C1;
342 Op<1>() = C2;
343 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000344 }
Gabor Greifefe65362008-05-10 08:32:32 +0000345 /// Transparently provide more efficient getOperand methods.
346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000347};
Chris Lattnere4671472005-01-29 00:34:39 +0000348
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000349/// ExtractElementConstantExpr - This class is private to
350/// Constants.cpp, and is used behind the scenes to implement
351/// extractelement constant exprs.
352class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000353 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000354public:
Gabor Greif051a9502008-04-06 20:25:17 +0000355 // allocate space for exactly two operands
356 void *operator new(size_t s) {
357 return User::operator new(s, 2);
358 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000359 ExtractElementConstantExpr(Constant *C1, Constant *C2)
360 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +0000361 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000362 Op<0>() = C1;
363 Op<1>() = C2;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000364 }
Gabor Greifefe65362008-05-10 08:32:32 +0000365 /// Transparently provide more efficient getOperand methods.
366 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000367};
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000368
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000369/// InsertElementConstantExpr - This class is private to
370/// Constants.cpp, and is used behind the scenes to implement
371/// insertelement constant exprs.
372class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000373 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000374public:
Gabor Greif051a9502008-04-06 20:25:17 +0000375 // allocate space for exactly three operands
376 void *operator new(size_t s) {
377 return User::operator new(s, 3);
378 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000379 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
380 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greifefe65362008-05-10 08:32:32 +0000381 &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000382 Op<0>() = C1;
383 Op<1>() = C2;
384 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000385 }
Gabor Greifefe65362008-05-10 08:32:32 +0000386 /// Transparently provide more efficient getOperand methods.
387 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000388};
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000389
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000390/// ShuffleVectorConstantExpr - This class is private to
391/// Constants.cpp, and is used behind the scenes to implement
392/// shufflevector constant exprs.
393class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000394 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000395public:
Gabor Greif051a9502008-04-06 20:25:17 +0000396 // allocate space for exactly three operands
397 void *operator new(size_t s) {
398 return User::operator new(s, 3);
399 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000400 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman0f123cf2009-02-12 21:28:33 +0000401 : ConstantExpr(VectorType::get(
402 cast<VectorType>(C1->getType())->getElementType(),
403 cast<VectorType>(C3->getType())->getNumElements()),
404 Instruction::ShuffleVector,
Gabor Greifefe65362008-05-10 08:32:32 +0000405 &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000406 Op<0>() = C1;
407 Op<1>() = C2;
408 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000409 }
Gabor Greifefe65362008-05-10 08:32:32 +0000410 /// Transparently provide more efficient getOperand methods.
411 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000412};
413
Dan Gohman041e2eb2008-05-15 19:50:34 +0000414/// ExtractValueConstantExpr - This class is private to
415/// Constants.cpp, and is used behind the scenes to implement
416/// extractvalue constant exprs.
417class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000418 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman041e2eb2008-05-15 19:50:34 +0000419public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000420 // allocate space for exactly one operand
421 void *operator new(size_t s) {
422 return User::operator new(s, 1);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000423 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000424 ExtractValueConstantExpr(Constant *Agg,
425 const SmallVector<unsigned, 4> &IdxList,
426 const Type *DestTy)
427 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
428 Indices(IdxList) {
429 Op<0>() = Agg;
430 }
431
Dan Gohman35651cd2008-05-31 19:09:08 +0000432 /// Indices - These identify which value to extract.
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000433 const SmallVector<unsigned, 4> Indices;
434
Dan Gohman041e2eb2008-05-15 19:50:34 +0000435 /// Transparently provide more efficient getOperand methods.
436 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
437};
438
439/// InsertValueConstantExpr - This class is private to
440/// Constants.cpp, and is used behind the scenes to implement
441/// insertvalue constant exprs.
442class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000443 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman041e2eb2008-05-15 19:50:34 +0000444public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000445 // allocate space for exactly one operand
446 void *operator new(size_t s) {
447 return User::operator new(s, 2);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000448 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000449 InsertValueConstantExpr(Constant *Agg, Constant *Val,
450 const SmallVector<unsigned, 4> &IdxList,
451 const Type *DestTy)
452 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
453 Indices(IdxList) {
454 Op<0>() = Agg;
455 Op<1>() = Val;
456 }
457
Dan Gohman35651cd2008-05-31 19:09:08 +0000458 /// Indices - These identify the position for the insertion.
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000459 const SmallVector<unsigned, 4> Indices;
460
Dan Gohman041e2eb2008-05-15 19:50:34 +0000461 /// Transparently provide more efficient getOperand methods.
462 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
463};
464
465
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000466/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
467/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greif051a9502008-04-06 20:25:17 +0000468class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000469 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000470 const Type *DestTy);
Gabor Greif051a9502008-04-06 20:25:17 +0000471public:
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000472 static GetElementPtrConstantExpr *Create(Constant *C,
473 const std::vector<Constant*>&IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000474 const Type *DestTy) {
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000475 return new(IdxList.size() + 1)
476 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greif051a9502008-04-06 20:25:17 +0000477 }
Gabor Greifefe65362008-05-10 08:32:32 +0000478 /// Transparently provide more efficient getOperand methods.
479 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000480};
481
482// CompareConstantExpr - This class is private to Constants.cpp, and is used
483// behind the scenes to implement ICmp and FCmp constant expressions. This is
484// needed in order to store the predicate value for these instructions.
485struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000486 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
487 // allocate space for exactly two operands
488 void *operator new(size_t s) {
489 return User::operator new(s, 2);
490 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000491 unsigned short predicate;
Nate Begemanac80ade2008-05-12 19:01:56 +0000492 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
493 unsigned short pred, Constant* LHS, Constant* RHS)
494 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000495 Op<0>() = LHS;
496 Op<1>() = RHS;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000497 }
Gabor Greifefe65362008-05-10 08:32:32 +0000498 /// Transparently provide more efficient getOperand methods.
499 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000500};
501
502} // end anonymous namespace
503
Gabor Greifefe65362008-05-10 08:32:32 +0000504template <>
505struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
506};
507DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
508
509template <>
510struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
511};
512DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
513
514template <>
515struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
516};
517DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
518
519template <>
520struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
521};
522DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
523
524template <>
525struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
526};
527DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
528
529template <>
530struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
531};
532DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
533
Dan Gohman041e2eb2008-05-15 19:50:34 +0000534template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000535struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000536};
Dan Gohman041e2eb2008-05-15 19:50:34 +0000537DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
538
539template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000540struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000541};
Dan Gohman041e2eb2008-05-15 19:50:34 +0000542DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
543
Gabor Greifefe65362008-05-10 08:32:32 +0000544template <>
545struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
546};
547
548GetElementPtrConstantExpr::GetElementPtrConstantExpr
549 (Constant *C,
550 const std::vector<Constant*> &IdxList,
551 const Type *DestTy)
552 : ConstantExpr(DestTy, Instruction::GetElementPtr,
553 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
554 - (IdxList.size()+1),
555 IdxList.size()+1) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000556 OperandList[0] = C;
Gabor Greifefe65362008-05-10 08:32:32 +0000557 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif6c80c382008-05-26 21:33:52 +0000558 OperandList[i+1] = IdxList[i];
Gabor Greifefe65362008-05-10 08:32:32 +0000559}
560
561DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
562
563
564template <>
565struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
566};
567DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
568
569
570} // End llvm namespace
571
Reid Spencer3da59db2006-11-27 01:05:10 +0000572
573// Utility function for determining if a ConstantExpr is a CastOp or not. This
574// can't be inline because we don't want to #include Instruction.h into
575// Constant.h
576bool ConstantExpr::isCast() const {
577 return Instruction::isCast(getOpcode());
578}
579
Reid Spencer077d0eb2006-12-04 05:19:50 +0000580bool ConstantExpr::isCompare() const {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000581 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spencer077d0eb2006-12-04 05:19:50 +0000582}
583
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000584bool ConstantExpr::hasIndices() const {
585 return getOpcode() == Instruction::ExtractValue ||
586 getOpcode() == Instruction::InsertValue;
587}
588
589const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
590 if (const ExtractValueConstantExpr *EVCE =
591 dyn_cast<ExtractValueConstantExpr>(this))
592 return EVCE->Indices;
Dan Gohman1a203572008-06-23 16:39:44 +0000593
594 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000595}
596
Reid Spencer728b6db2006-12-03 05:48:19 +0000597unsigned ConstantExpr::getPredicate() const {
Nate Begemanac80ade2008-05-12 19:01:56 +0000598 assert(getOpcode() == Instruction::FCmp ||
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000599 getOpcode() == Instruction::ICmp);
Chris Lattnerb7daa842007-10-18 16:26:24 +0000600 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer728b6db2006-12-03 05:48:19 +0000601}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000602
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000603/// getWithOperandReplaced - Return a constant expression identical to this
604/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000605Constant *
606ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000607 assert(OpNo < getNumOperands() && "Operand num is out of range!");
608 assert(Op->getType() == getOperand(OpNo)->getType() &&
609 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000610 if (getOperand(OpNo) == Op)
611 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000612
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000613 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000614 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000615 case Instruction::Trunc:
616 case Instruction::ZExt:
617 case Instruction::SExt:
618 case Instruction::FPTrunc:
619 case Instruction::FPExt:
620 case Instruction::UIToFP:
621 case Instruction::SIToFP:
622 case Instruction::FPToUI:
623 case Instruction::FPToSI:
624 case Instruction::PtrToInt:
625 case Instruction::IntToPtr:
626 case Instruction::BitCast:
627 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000628 case Instruction::Select:
629 Op0 = (OpNo == 0) ? Op : getOperand(0);
630 Op1 = (OpNo == 1) ? Op : getOperand(1);
631 Op2 = (OpNo == 2) ? Op : getOperand(2);
632 return ConstantExpr::getSelect(Op0, Op1, Op2);
633 case Instruction::InsertElement:
634 Op0 = (OpNo == 0) ? Op : getOperand(0);
635 Op1 = (OpNo == 1) ? Op : getOperand(1);
636 Op2 = (OpNo == 2) ? Op : getOperand(2);
637 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
638 case Instruction::ExtractElement:
639 Op0 = (OpNo == 0) ? Op : getOperand(0);
640 Op1 = (OpNo == 1) ? Op : getOperand(1);
641 return ConstantExpr::getExtractElement(Op0, Op1);
642 case Instruction::ShuffleVector:
643 Op0 = (OpNo == 0) ? Op : getOperand(0);
644 Op1 = (OpNo == 1) ? Op : getOperand(1);
645 Op2 = (OpNo == 2) ? Op : getOperand(2);
646 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000647 case Instruction::GetElementPtr: {
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000648 SmallVector<Constant*, 8> Ops;
Dan Gohman041e2eb2008-05-15 19:50:34 +0000649 Ops.resize(getNumOperands()-1);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000650 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman041e2eb2008-05-15 19:50:34 +0000651 Ops[i-1] = getOperand(i);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000652 if (OpNo == 0)
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000653 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000654 Ops[OpNo-1] = Op;
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000655 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000656 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000657 default:
658 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000659 Op0 = (OpNo == 0) ? Op : getOperand(0);
660 Op1 = (OpNo == 1) ? Op : getOperand(1);
661 return ConstantExpr::get(getOpcode(), Op0, Op1);
662 }
663}
664
665/// getWithOperands - This returns the current constant expression with the
666/// operands replaced with the specified values. The specified operands must
667/// match count and type with the existing ones.
668Constant *ConstantExpr::
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000669getWithOperands(Constant* const *Ops, unsigned NumOps) const {
670 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000671 bool AnyChange = false;
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000672 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000673 assert(Ops[i]->getType() == getOperand(i)->getType() &&
674 "Operand type mismatch!");
675 AnyChange |= Ops[i] != getOperand(i);
676 }
677 if (!AnyChange) // No operands changed, return self.
678 return const_cast<ConstantExpr*>(this);
679
680 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000681 case Instruction::Trunc:
682 case Instruction::ZExt:
683 case Instruction::SExt:
684 case Instruction::FPTrunc:
685 case Instruction::FPExt:
686 case Instruction::UIToFP:
687 case Instruction::SIToFP:
688 case Instruction::FPToUI:
689 case Instruction::FPToSI:
690 case Instruction::PtrToInt:
691 case Instruction::IntToPtr:
692 case Instruction::BitCast:
693 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000694 case Instruction::Select:
695 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
696 case Instruction::InsertElement:
697 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
698 case Instruction::ExtractElement:
699 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
700 case Instruction::ShuffleVector:
701 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000702 case Instruction::GetElementPtr:
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000703 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000704 case Instruction::ICmp:
705 case Instruction::FCmp:
706 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000707 default:
708 assert(getNumOperands() == 2 && "Must be binary operator?");
709 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000710 }
711}
712
Chris Lattner00950542001-06-06 20:29:01 +0000713
714//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000715// isValueValidForType implementations
716
Reid Spencer9b11d512006-12-19 01:28:19 +0000717bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000718 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencera54b7cb2007-01-12 07:05:14 +0000719 if (Ty == Type::Int1Ty)
720 return Val == 0 || Val == 1;
Reid Spencer554cec62007-02-05 23:47:56 +0000721 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000722 return true; // always true, has to fit in largest type
723 uint64_t Max = (1ll << NumBits) - 1;
724 return Val <= Max;
Reid Spencer9b11d512006-12-19 01:28:19 +0000725}
726
Reid Spencerb83eb642006-10-20 07:07:24 +0000727bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000728 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencera54b7cb2007-01-12 07:05:14 +0000729 if (Ty == Type::Int1Ty)
Reid Spencerc1030572007-01-19 21:13:56 +0000730 return Val == 0 || Val == 1 || Val == -1;
Reid Spencer554cec62007-02-05 23:47:56 +0000731 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000732 return true; // always true, has to fit in largest type
733 int64_t Min = -(1ll << (NumBits-1));
734 int64_t Max = (1ll << (NumBits-1)) - 1;
735 return (Val >= Min && Val <= Max);
Chris Lattner00950542001-06-06 20:29:01 +0000736}
737
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000738bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
739 // convert modifies in place, so make a copy.
740 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +0000741 bool losesInfo;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000742 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000743 default:
744 return false; // These can't be represented as floating point!
745
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000746 // FIXME rounding mode needs to be more flexible
Dale Johannesen23a98552008-10-09 23:00:39 +0000747 case Type::FloatTyID: {
748 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
749 return true;
750 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
751 return !losesInfo;
752 }
753 case Type::DoubleTyID: {
754 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
755 &Val2.getSemantics() == &APFloat::IEEEdouble)
756 return true;
757 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
758 return !losesInfo;
759 }
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000760 case Type::X86_FP80TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000761 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
762 &Val2.getSemantics() == &APFloat::IEEEdouble ||
763 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000764 case Type::FP128TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000765 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
766 &Val2.getSemantics() == &APFloat::IEEEdouble ||
767 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000768 case Type::PPC_FP128TyID:
769 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
770 &Val2.getSemantics() == &APFloat::IEEEdouble ||
771 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner00950542001-06-06 20:29:01 +0000772 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000773}
Chris Lattner37bf6302001-07-20 19:16:02 +0000774
Chris Lattner531daef2001-09-07 16:46:31 +0000775//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +0000776// Factory Function Implementation
777
Gabor Greifefe65362008-05-10 08:32:32 +0000778
779// The number of operands for each ConstantCreator::create method is
780// determined by the ConstantTraits template.
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000781// ConstantCreator - A class that is used to create constants by
782// ValueMap*. This class should be partially specialized if there is
783// something strange that needs to be done to interface to the ctor for the
784// constant.
785//
Chris Lattner31f84992003-11-21 20:23:48 +0000786namespace llvm {
Gabor Greifefe65362008-05-10 08:32:32 +0000787 template<class ValType>
788 struct ConstantTraits;
789
790 template<typename T, typename Alloc>
791 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
792 static unsigned uses(const std::vector<T, Alloc>& v) {
793 return v.size();
794 }
795 };
796
Chris Lattner31f84992003-11-21 20:23:48 +0000797 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattnerf190d382006-06-28 21:38:54 +0000798 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner31f84992003-11-21 20:23:48 +0000799 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greifefe65362008-05-10 08:32:32 +0000800 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner31f84992003-11-21 20:23:48 +0000801 }
802 };
Misha Brukmanfd939082005-04-21 23:48:37 +0000803
Chris Lattner31f84992003-11-21 20:23:48 +0000804 template<class ConstantClass, class TypeClass>
Chris Lattnerf190d382006-06-28 21:38:54 +0000805 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner31f84992003-11-21 20:23:48 +0000806 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000807 llvm_unreachable("This type cannot be converted!");
Chris Lattner31f84992003-11-21 20:23:48 +0000808 }
809 };
Chris Lattnered468e372003-10-05 00:17:43 +0000810
Chris Lattnera55b30a2005-10-04 17:48:46 +0000811 template<class ValType, class TypeClass, class ConstantClass,
812 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattnerf190d382006-06-28 21:38:54 +0000813 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnercea141f2005-10-03 22:51:37 +0000814 public:
Jim Laskeyede5aa42006-07-17 17:38:29 +0000815 typedef std::pair<const Type*, ValType> MapKey;
816 typedef std::map<MapKey, Constant *> MapTy;
817 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
818 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnercea141f2005-10-03 22:51:37 +0000819 private:
Chris Lattnerd7a3fc62005-10-04 16:52:46 +0000820 /// Map - This is the main map from the element descriptor to the Constants.
821 /// This is the primary way we avoid creating two of the same shape
822 /// constant.
Chris Lattnered468e372003-10-05 00:17:43 +0000823 MapTy Map;
Chris Lattnera55b30a2005-10-04 17:48:46 +0000824
825 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
826 /// from the constants to their element in Map. This is important for
827 /// removal of constants from the array, which would otherwise have to scan
828 /// through the map with very large keys.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000829 InverseMapTy InverseMap;
Chris Lattnered468e372003-10-05 00:17:43 +0000830
Jim Laskeyede5aa42006-07-17 17:38:29 +0000831 /// AbstractTypeMap - Map for abstract type constants.
832 ///
Chris Lattnered468e372003-10-05 00:17:43 +0000833 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson04fb7c32009-06-20 00:24:58 +0000834
835 /// ValueMapLock - Mutex for this map.
836 sys::SmartMutex<true> ValueMapLock;
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000837
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000838 public:
Owen Anderson31c36f02009-06-17 20:10:08 +0000839 // NOTE: This function is not locked. It is the caller's responsibility
840 // to enforce proper synchronization.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000841 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnercea141f2005-10-03 22:51:37 +0000842
843 /// InsertOrGetItem - Return an iterator for the specified element.
844 /// If the element exists in the map, the returned iterator points to the
845 /// entry and Exists=true. If not, the iterator points to the newly
846 /// inserted entry and returns Exists=false. Newly inserted entries have
847 /// I->second == 0, and should be filled in.
Owen Anderson31c36f02009-06-17 20:10:08 +0000848 /// NOTE: This function is not locked. It is the caller's responsibility
849 // to enforce proper synchronization.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000850 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
851 &InsertVal,
Chris Lattnercea141f2005-10-03 22:51:37 +0000852 bool &Exists) {
Jim Laskeyede5aa42006-07-17 17:38:29 +0000853 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnercea141f2005-10-03 22:51:37 +0000854 Exists = !IP.second;
855 return IP.first;
856 }
Chris Lattnerd7a3fc62005-10-04 16:52:46 +0000857
Chris Lattnera55b30a2005-10-04 17:48:46 +0000858private:
Jim Laskeyede5aa42006-07-17 17:38:29 +0000859 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattnera55b30a2005-10-04 17:48:46 +0000860 if (HasLargeKey) {
Jim Laskeyede5aa42006-07-17 17:38:29 +0000861 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattnera55b30a2005-10-04 17:48:46 +0000862 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
863 IMI->second->second == CP &&
864 "InverseMap corrupt!");
865 return IMI->second;
866 }
867
Jim Laskeyede5aa42006-07-17 17:38:29 +0000868 typename MapTy::iterator I =
Dan Gohman430b8a22008-08-05 14:45:15 +0000869 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
870 getValType(CP)));
Chris Lattnerd7a3fc62005-10-04 16:52:46 +0000871 if (I == Map.end() || I->second != CP) {
872 // FIXME: This should not use a linear scan. If this gets to be a
873 // performance problem, someone should look at this.
874 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
875 /* empty */;
876 }
Chris Lattnera55b30a2005-10-04 17:48:46 +0000877 return I;
878 }
Owen Anderson32a25562009-06-17 20:43:39 +0000879
880 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
881 typename MapTy::iterator I) {
882 ConstantClass* Result =
883 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
884
885 assert(Result->getType() == Ty && "Type specified is not correct!");
886 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
887
888 if (HasLargeKey) // Remember the reverse mapping if needed.
889 InverseMap.insert(std::make_pair(Result, I));
890
891 // If the type of the constant is abstract, make sure that an entry
892 // exists for it in the AbstractTypeMap.
893 if (Ty->isAbstract()) {
894 typename AbstractTypeMapTy::iterator TI =
895 AbstractTypeMap.find(Ty);
896
897 if (TI == AbstractTypeMap.end()) {
898 // Add ourselves to the ATU list of the type.
899 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
900
901 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
902 }
903 }
904
905 return Result;
906 }
Chris Lattnera55b30a2005-10-04 17:48:46 +0000907public:
908
Chris Lattnercea141f2005-10-03 22:51:37 +0000909 /// getOrCreate - Return the specified constant from the map, creating it if
910 /// necessary.
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000911 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000912 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Anderson430444b2009-06-19 23:16:19 +0000913 MapKey Lookup(Ty, V);
914 ConstantClass* Result = 0;
915
916 typename MapTy::iterator I = Map.find(Lookup);
917 // Is it in the map?
918 if (I != Map.end())
919 Result = static_cast<ConstantClass *>(I->second);
920
921 if (!Result) {
922 // If no preexisting value, create one now...
923 Result = Create(Ty, V, I);
924 }
925
926 return Result;
927 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000928
Owen Anderson04fb7c32009-06-20 00:24:58 +0000929 void remove(ConstantClass *CP) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000930 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyede5aa42006-07-17 17:38:29 +0000931 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnered468e372003-10-05 00:17:43 +0000932 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner6823c9f2004-08-04 04:48:01 +0000933 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnered468e372003-10-05 00:17:43 +0000934
Chris Lattnera55b30a2005-10-04 17:48:46 +0000935 if (HasLargeKey) // Remember the reverse mapping if needed.
936 InverseMap.erase(CP);
937
Chris Lattnered468e372003-10-05 00:17:43 +0000938 // Now that we found the entry, make sure this isn't the entry that
939 // the AbstractTypeMap points to.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000940 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnered468e372003-10-05 00:17:43 +0000941 if (Ty->isAbstract()) {
942 assert(AbstractTypeMap.count(Ty) &&
943 "Abstract type not in AbstractTypeMap?");
Jim Laskeyede5aa42006-07-17 17:38:29 +0000944 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnered468e372003-10-05 00:17:43 +0000945 if (ATMEntryIt == I) {
946 // Yes, we are removing the representative entry for this type.
947 // See if there are any other entries of the same type.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000948 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanfd939082005-04-21 23:48:37 +0000949
Chris Lattnered468e372003-10-05 00:17:43 +0000950 // First check the entry before this one...
951 if (TmpIt != Map.begin()) {
952 --TmpIt;
953 if (TmpIt->first.first != Ty) // Not the same type, move back...
954 ++TmpIt;
955 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000956
Chris Lattnered468e372003-10-05 00:17:43 +0000957 // If we didn't find the same type, try to move forward...
958 if (TmpIt == ATMEntryIt) {
959 ++TmpIt;
960 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
961 --TmpIt; // No entry afterwards with the same type
962 }
963
964 // If there is another entry in the map of the same abstract type,
965 // update the AbstractTypeMap entry now.
966 if (TmpIt != ATMEntryIt) {
967 ATMEntryIt = TmpIt;
968 } else {
969 // Otherwise, we are removing the last instance of this type
970 // from the table. Remove from the ATM, and from user list.
971 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
972 AbstractTypeMap.erase(Ty);
973 }
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000974 }
Chris Lattnered468e372003-10-05 00:17:43 +0000975 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000976
Chris Lattnered468e372003-10-05 00:17:43 +0000977 Map.erase(I);
978 }
979
Chris Lattnera1e3f542005-10-04 21:35:50 +0000980
981 /// MoveConstantToNewSlot - If we are about to change C to be the element
982 /// specified by I, update our internal data structures to reflect this
983 /// fact.
Owen Anderson31c36f02009-06-17 20:10:08 +0000984 /// NOTE: This function is not locked. It is the responsibility of the
985 /// caller to enforce proper synchronization if using this method.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000986 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattnera1e3f542005-10-04 21:35:50 +0000987 // First, remove the old location of the specified constant in the map.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000988 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattnera1e3f542005-10-04 21:35:50 +0000989 assert(OldI != Map.end() && "Constant not found in constant table!");
990 assert(OldI->second == C && "Didn't find correct element?");
991
992 // If this constant is the representative element for its abstract type,
993 // update the AbstractTypeMap so that the representative element is I.
994 if (C->getType()->isAbstract()) {
995 typename AbstractTypeMapTy::iterator ATI =
996 AbstractTypeMap.find(C->getType());
997 assert(ATI != AbstractTypeMap.end() &&
998 "Abstract type not in AbstractTypeMap?");
999 if (ATI->second == OldI)
1000 ATI->second = I;
1001 }
1002
1003 // Remove the old entry from the map.
1004 Map.erase(OldI);
1005
1006 // Update the inverse map so that we know that this constant is now
1007 // located at descriptor I.
1008 if (HasLargeKey) {
1009 assert(I->second == C && "Bad inversemap entry!");
1010 InverseMap[C] = I;
1011 }
1012 }
1013
Chris Lattnered468e372003-10-05 00:17:43 +00001014 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +00001015 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanfd939082005-04-21 23:48:37 +00001016 typename AbstractTypeMapTy::iterator I =
Jim Laskeyede5aa42006-07-17 17:38:29 +00001017 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnered468e372003-10-05 00:17:43 +00001018
1019 assert(I != AbstractTypeMap.end() &&
1020 "Abstract type not in AbstractTypeMap?");
1021
1022 // Convert a constant at a time until the last one is gone. The last one
1023 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1024 // eliminated eventually.
1025 do {
1026 ConvertConstantType<ConstantClass,
Jim Laskeyede5aa42006-07-17 17:38:29 +00001027 TypeClass>::convert(
1028 static_cast<ConstantClass *>(I->second->second),
Chris Lattnered468e372003-10-05 00:17:43 +00001029 cast<TypeClass>(NewTy));
1030
Jim Laskeyede5aa42006-07-17 17:38:29 +00001031 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnered468e372003-10-05 00:17:43 +00001032 } while (I != AbstractTypeMap.end());
1033 }
1034
1035 // If the type became concrete without being refined to any other existing
1036 // type, we just remove ourselves from the ATU list.
1037 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersonee6aefc2009-06-18 19:10:19 +00001038 AbsTy->removeAbstractTypeUser(this);
Chris Lattnered468e372003-10-05 00:17:43 +00001039 }
1040
1041 void dump() const {
Bill Wendling2e3def12006-11-17 08:03:48 +00001042 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001043 }
1044 };
1045}
1046
Chris Lattner003cbf32006-09-28 23:36:21 +00001047
Chris Lattnerd1afbd02007-02-20 06:11:36 +00001048
Chris Lattner40bbeb52004-02-15 05:53:04 +00001049//---- ConstantAggregateZero::get() implementation...
1050//
1051namespace llvm {
1052 // ConstantAggregateZero does not take extra "value" argument...
1053 template<class ValType>
1054 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1055 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1056 return new ConstantAggregateZero(Ty);
1057 }
1058 };
1059
1060 template<>
1061 struct ConvertConstantType<ConstantAggregateZero, Type> {
1062 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1063 // Make everyone now use a constant of the new type...
Owen Anderson04fb7c32009-06-20 00:24:58 +00001064 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001065 assert(New != OldC && "Didn't replace constant??");
1066 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001067 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner40bbeb52004-02-15 05:53:04 +00001068 }
1069 };
1070}
1071
Chris Lattner8a94bf12006-09-28 00:35:06 +00001072static ManagedStatic<ValueMap<char, Type,
1073 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner40bbeb52004-02-15 05:53:04 +00001074
Chris Lattner6823c9f2004-08-04 04:48:01 +00001075static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1076
Owen Anderson04fb7c32009-06-20 00:24:58 +00001077ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001078 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattner31d1a2c2006-06-10 04:16:23 +00001079 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001080
1081 // Implicitly locked.
1082 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001083}
1084
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001085/// destroyConstant - Remove the constant from the constant table...
1086///
Owen Anderson04fb7c32009-06-20 00:24:58 +00001087void ConstantAggregateZero::destroyConstant() {
Owen Anderson31c36f02009-06-17 20:10:08 +00001088 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001089 AggZeroConstants->remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001090 destroyConstantImpl();
1091}
1092
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001093//---- ConstantArray::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001094//
Chris Lattner31f84992003-11-21 20:23:48 +00001095namespace llvm {
1096 template<>
1097 struct ConvertConstantType<ConstantArray, ArrayType> {
1098 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1099 // Make everyone now use a constant of the new type...
1100 std::vector<Constant*> C;
1101 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1102 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson04fb7c32009-06-20 00:24:58 +00001103 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner31f84992003-11-21 20:23:48 +00001104 assert(New != OldC && "Didn't replace constant??");
1105 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001106 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001107 }
1108 };
1109}
Chris Lattnered468e372003-10-05 00:17:43 +00001110
Chris Lattner6823c9f2004-08-04 04:48:01 +00001111static std::vector<Constant*> getValType(ConstantArray *CA) {
1112 std::vector<Constant*> Elements;
1113 Elements.reserve(CA->getNumOperands());
1114 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1115 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1116 return Elements;
1117}
1118
Chris Lattnercea141f2005-10-03 22:51:37 +00001119typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattnera55b30a2005-10-04 17:48:46 +00001120 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner8a94bf12006-09-28 00:35:06 +00001121static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner531daef2001-09-07 16:46:31 +00001122
Chris Lattnerca705fa2004-02-15 04:14:47 +00001123Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001124 const std::vector<Constant*> &V) {
Chris Lattner40bbeb52004-02-15 05:53:04 +00001125 // If this is an all-zero array, return a ConstantAggregateZero object
1126 if (!V.empty()) {
1127 Constant *C = V[0];
Owen Anderson3e456ab2009-06-17 18:40:29 +00001128 if (!C->isNullValue()) {
Owen Anderson04fb7c32009-06-20 00:24:58 +00001129 // Implicitly locked.
1130 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001131 }
Chris Lattner40bbeb52004-02-15 05:53:04 +00001132 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson3e456ab2009-06-17 18:40:29 +00001133 if (V[i] != C) {
Owen Anderson04fb7c32009-06-20 00:24:58 +00001134 // Implicitly locked.
1135 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001136 }
Chris Lattner40bbeb52004-02-15 05:53:04 +00001137 }
Owen Anderson3e456ab2009-06-17 18:40:29 +00001138
Owen Anderson04fb7c32009-06-20 00:24:58 +00001139 return ConstantAggregateZero::get(Ty);
Chris Lattner531daef2001-09-07 16:46:31 +00001140}
1141
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001142/// destroyConstant - Remove the constant from the constant table...
1143///
Owen Anderson04fb7c32009-06-20 00:24:58 +00001144void ConstantArray::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001145 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001146 ArrayConstants->remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001147 destroyConstantImpl();
1148}
1149
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001150/// isString - This method returns true if the array is an array of i8, and
1151/// if the elements of the array are all ConstantInt's.
Chris Lattner13cfdea2004-01-14 17:06:38 +00001152bool ConstantArray::isString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001153 // Check the element type for i8...
Reid Spencer79e21d32006-12-31 05:26:44 +00001154 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattner13cfdea2004-01-14 17:06:38 +00001155 return false;
1156 // Check the elements to make sure they are all integers, not constant
1157 // expressions.
1158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1159 if (!isa<ConstantInt>(getOperand(i)))
1160 return false;
1161 return true;
1162}
1163
Evan Cheng22c70302006-10-26 19:15:05 +00001164/// isCString - This method returns true if the array is a string (see
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001165/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng22c70302006-10-26 19:15:05 +00001166/// null bytes except its terminator.
Owen Anderson1ca29d32009-07-13 21:27:19 +00001167bool ConstantArray::isCString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001168 // Check the element type for i8...
Reid Spencer79e21d32006-12-31 05:26:44 +00001169 if (getType()->getElementType() != Type::Int8Ty)
Evan Chengabf63452006-10-26 21:48:03 +00001170 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +00001171
Evan Chengabf63452006-10-26 21:48:03 +00001172 // Last element must be a null.
Owen Anderson1ca29d32009-07-13 21:27:19 +00001173 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +00001174 return false;
1175 // Other elements must be non-null integers.
1176 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1177 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +00001178 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +00001179 if (getOperand(i)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +00001180 return false;
1181 }
Evan Cheng22c70302006-10-26 19:15:05 +00001182 return true;
1183}
1184
1185
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001186/// getAsString - If the sub-element type of this array is i8
1187/// then this method converts the array to an std::string and returns it.
1188/// Otherwise, it asserts out.
1189///
Chris Lattner93aeea32002-08-26 17:53:56 +00001190std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +00001191 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +00001192 std::string Result;
Owen Anderson45e39582008-06-24 21:58:29 +00001193 Result.reserve(getNumOperands());
Chris Lattnerc07736a2003-07-23 15:22:26 +00001194 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersone4f6ee52008-06-25 01:05:05 +00001195 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner93aeea32002-08-26 17:53:56 +00001196 return Result;
1197}
1198
1199
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001200//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001201//
Chris Lattnered468e372003-10-05 00:17:43 +00001202
Chris Lattner31f84992003-11-21 20:23:48 +00001203namespace llvm {
1204 template<>
1205 struct ConvertConstantType<ConstantStruct, StructType> {
1206 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1207 // Make everyone now use a constant of the new type...
1208 std::vector<Constant*> C;
1209 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1210 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson04fb7c32009-06-20 00:24:58 +00001211 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner31f84992003-11-21 20:23:48 +00001212 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanfd939082005-04-21 23:48:37 +00001213
Chris Lattner31f84992003-11-21 20:23:48 +00001214 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001215 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001216 }
1217 };
1218}
Chris Lattnered468e372003-10-05 00:17:43 +00001219
Chris Lattnerc182a882005-10-04 01:17:50 +00001220typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattnera55b30a2005-10-04 17:48:46 +00001221 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8a94bf12006-09-28 00:35:06 +00001222static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner531daef2001-09-07 16:46:31 +00001223
Chris Lattner6823c9f2004-08-04 04:48:01 +00001224static std::vector<Constant*> getValType(ConstantStruct *CS) {
1225 std::vector<Constant*> Elements;
1226 Elements.reserve(CS->getNumOperands());
1227 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1228 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1229 return Elements;
1230}
1231
Chris Lattnerca705fa2004-02-15 04:14:47 +00001232Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001233 const std::vector<Constant*> &V) {
Chris Lattner40bbeb52004-02-15 05:53:04 +00001234 // Create a ConstantAggregateZero value if all elements are zeros...
1235 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson31c36f02009-06-17 20:10:08 +00001236 if (!V[i]->isNullValue())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001237 // Implicitly locked.
1238 return StructConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001239
Owen Anderson04fb7c32009-06-20 00:24:58 +00001240 return ConstantAggregateZero::get(Ty);
Chris Lattner531daef2001-09-07 16:46:31 +00001241}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001242
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001243// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001244//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001245void ConstantStruct::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001246 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001247 StructConstants->remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001248 destroyConstantImpl();
1249}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001250
Reid Spencer9d6565a2007-02-15 02:26:10 +00001251//---- ConstantVector::get() implementation...
Brian Gaeke715c90b2004-08-20 06:00:58 +00001252//
1253namespace llvm {
1254 template<>
Reid Spencer9d6565a2007-02-15 02:26:10 +00001255 struct ConvertConstantType<ConstantVector, VectorType> {
1256 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001257 // Make everyone now use a constant of the new type...
1258 std::vector<Constant*> C;
1259 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1260 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson04fb7c32009-06-20 00:24:58 +00001261 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001262 assert(New != OldC && "Didn't replace constant??");
1263 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001264 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke715c90b2004-08-20 06:00:58 +00001265 }
1266 };
1267}
1268
Reid Spencer9d6565a2007-02-15 02:26:10 +00001269static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001270 std::vector<Constant*> Elements;
1271 Elements.reserve(CP->getNumOperands());
1272 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1273 Elements.push_back(CP->getOperand(i));
1274 return Elements;
1275}
1276
Reid Spencer9d6565a2007-02-15 02:26:10 +00001277static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencerac9dcb92007-02-15 03:39:18 +00001278 ConstantVector> > VectorConstants;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001279
Reid Spencer9d6565a2007-02-15 02:26:10 +00001280Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001281 const std::vector<Constant*> &V) {
Chris Lattner7cc9a4b2008-07-10 00:44:03 +00001282 assert(!V.empty() && "Vectors can't be empty");
1283 // If this is an all-undef or alll-zero vector, return a
1284 // ConstantAggregateZero or UndefValue.
1285 Constant *C = V[0];
1286 bool isZero = C->isNullValue();
1287 bool isUndef = isa<UndefValue>(C);
1288
1289 if (isZero || isUndef) {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001290 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattner7cc9a4b2008-07-10 00:44:03 +00001291 if (V[i] != C) {
1292 isZero = isUndef = false;
1293 break;
1294 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001295 }
Chris Lattner7cc9a4b2008-07-10 00:44:03 +00001296
1297 if (isZero)
Owen Anderson04fb7c32009-06-20 00:24:58 +00001298 return ConstantAggregateZero::get(Ty);
Chris Lattner7cc9a4b2008-07-10 00:44:03 +00001299 if (isUndef)
Owen Anderson04fb7c32009-06-20 00:24:58 +00001300 return UndefValue::get(Ty);
Owen Anderson31c36f02009-06-17 20:10:08 +00001301
Owen Anderson04fb7c32009-06-20 00:24:58 +00001302 // Implicitly locked.
1303 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001304}
1305
Brian Gaeke715c90b2004-08-20 06:00:58 +00001306// destroyConstant - Remove the constant from the constant table...
1307//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001308void ConstantVector::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001309 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001310 VectorConstants->remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001311 destroyConstantImpl();
1312}
1313
Dan Gohmanfa73ea22007-05-24 14:36:04 +00001314/// This function will return true iff every element in this vector constant
Jim Laskeyfa301822007-01-12 22:39:14 +00001315/// is set to all ones.
1316/// @returns true iff this constant's emements are all set to all ones.
1317/// @brief Determine if the value is all ones.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001318bool ConstantVector::isAllOnesValue() const {
Jim Laskeyfa301822007-01-12 22:39:14 +00001319 // Check out first element.
1320 const Constant *Elt = getOperand(0);
1321 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1322 if (!CI || !CI->isAllOnesValue()) return false;
1323 // Then make sure all remaining elements point to the same value.
1324 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1325 if (getOperand(I) != Elt) return false;
1326 }
1327 return true;
1328}
1329
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001330/// getSplatValue - If this is a splat constant, where all of the
1331/// elements have the same value, return that value. Otherwise return null.
1332Constant *ConstantVector::getSplatValue() {
1333 // Check out first element.
1334 Constant *Elt = getOperand(0);
1335 // Then make sure all remaining elements point to the same value.
1336 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1337 if (getOperand(I) != Elt) return 0;
1338 return Elt;
1339}
1340
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001341//---- ConstantPointerNull::get() implementation...
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001342//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001343
Chris Lattner31f84992003-11-21 20:23:48 +00001344namespace llvm {
1345 // ConstantPointerNull does not take extra "value" argument...
1346 template<class ValType>
1347 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1348 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1349 return new ConstantPointerNull(Ty);
1350 }
1351 };
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001352
Chris Lattner31f84992003-11-21 20:23:48 +00001353 template<>
1354 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1355 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1356 // Make everyone now use a constant of the new type...
Owen Anderson04fb7c32009-06-20 00:24:58 +00001357 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001358 assert(New != OldC && "Didn't replace constant??");
1359 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001360 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001361 }
1362 };
1363}
Chris Lattnered468e372003-10-05 00:17:43 +00001364
Chris Lattner8a94bf12006-09-28 00:35:06 +00001365static ManagedStatic<ValueMap<char, PointerType,
1366 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001367
Chris Lattner6823c9f2004-08-04 04:48:01 +00001368static char getValType(ConstantPointerNull *) {
1369 return 0;
1370}
1371
1372
Owen Anderson04fb7c32009-06-20 00:24:58 +00001373ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson31c36f02009-06-17 20:10:08 +00001374 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001375 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001376}
1377
Chris Lattner41661fd2002-08-18 00:40:04 +00001378// destroyConstant - Remove the constant from the constant table...
1379//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001380void ConstantPointerNull::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001381 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001382 NullPtrConstants->remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001383 destroyConstantImpl();
1384}
1385
1386
Chris Lattnerb9f18592004-10-16 18:07:16 +00001387//---- UndefValue::get() implementation...
1388//
1389
1390namespace llvm {
1391 // UndefValue does not take extra "value" argument...
1392 template<class ValType>
1393 struct ConstantCreator<UndefValue, Type, ValType> {
1394 static UndefValue *create(const Type *Ty, const ValType &V) {
1395 return new UndefValue(Ty);
1396 }
1397 };
1398
1399 template<>
1400 struct ConvertConstantType<UndefValue, Type> {
1401 static void convert(UndefValue *OldC, const Type *NewTy) {
1402 // Make everyone now use a constant of the new type.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001403 Constant *New = UndefValue::get(NewTy);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001404 assert(New != OldC && "Didn't replace constant??");
1405 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001406 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerb9f18592004-10-16 18:07:16 +00001407 }
1408 };
1409}
1410
Chris Lattner8a94bf12006-09-28 00:35:06 +00001411static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerb9f18592004-10-16 18:07:16 +00001412
1413static char getValType(UndefValue *) {
1414 return 0;
1415}
1416
1417
Owen Anderson04fb7c32009-06-20 00:24:58 +00001418UndefValue *UndefValue::get(const Type *Ty) {
1419 // Implicitly locked.
1420 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001421}
1422
1423// destroyConstant - Remove the constant from the constant table.
1424//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001425void UndefValue::destroyConstant() {
Owen Anderson31c36f02009-06-17 20:10:08 +00001426 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001427 UndefValueConstants->remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001428 destroyConstantImpl();
1429}
1430
Nick Lewycky21cc4462009-04-04 07:22:01 +00001431//---- MDString::get() implementation
1432//
1433
1434MDString::MDString(const char *begin, const char *end)
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001435 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky21cc4462009-04-04 07:22:01 +00001436 StrBegin(begin), StrEnd(end) {}
1437
Owen Anderson04fb7c32009-06-20 00:24:58 +00001438void MDString::destroyConstant() {
Owen Andersonaad3fb72009-07-16 22:11:26 +00001439 getType()->getContext().erase(this);
Nick Lewycky21cc4462009-04-04 07:22:01 +00001440 destroyConstantImpl();
1441}
1442
1443//---- MDNode::get() implementation
1444//
1445
1446static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1447
Nick Lewyckycb337992009-05-10 20:57:05 +00001448MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001449 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckycb337992009-05-10 20:57:05 +00001450 for (unsigned i = 0; i != NumVals; ++i)
1451 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky21cc4462009-04-04 07:22:01 +00001452}
1453
Nick Lewyckycb337992009-05-10 20:57:05 +00001454void MDNode::Profile(FoldingSetNodeID &ID) const {
1455 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky21cc4462009-04-04 07:22:01 +00001456 ID.AddPointer(*I);
1457}
1458
Owen Anderson04fb7c32009-06-20 00:24:58 +00001459MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky21cc4462009-04-04 07:22:01 +00001460 FoldingSetNodeID ID;
1461 for (unsigned i = 0; i != NumVals; ++i)
1462 ID.AddPointer(Vals[i]);
1463
Owen Anderson04fb7c32009-06-20 00:24:58 +00001464 ConstantsLock->reader_acquire();
Owen Andersonee6aefc2009-06-18 19:10:19 +00001465 void *InsertPoint;
1466 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001467 ConstantsLock->reader_release();
Owen Andersonee6aefc2009-06-18 19:10:19 +00001468
1469 if (!N) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +00001470 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001471 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1472 if (!N) {
Owen Andersonee6aefc2009-06-18 19:10:19 +00001473 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1474 N = new(0) MDNode(Vals, NumVals);
1475 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001476 }
Owen Anderson3e456ab2009-06-17 18:40:29 +00001477 }
Owen Andersonee6aefc2009-06-18 19:10:19 +00001478 return N;
Nick Lewycky21cc4462009-04-04 07:22:01 +00001479}
1480
Owen Anderson04fb7c32009-06-20 00:24:58 +00001481void MDNode::destroyConstant() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +00001482 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersonee6aefc2009-06-18 19:10:19 +00001483 MDNodeSet->RemoveNode(this);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001484
Nick Lewycky21cc4462009-04-04 07:22:01 +00001485 destroyConstantImpl();
1486}
Chris Lattnerb9f18592004-10-16 18:07:16 +00001487
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001488//---- ConstantExpr::get() implementations...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001489//
Reid Spencer79e21d32006-12-31 05:26:44 +00001490
Dan Gohman844731a2008-05-13 00:00:25 +00001491namespace {
1492
Reid Spencer077d0eb2006-12-04 05:19:50 +00001493struct ExprMapKeyType {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001494 typedef SmallVector<unsigned, 4> IndexList;
1495
1496 ExprMapKeyType(unsigned opc,
1497 const std::vector<Constant*> &ops,
1498 unsigned short pred = 0,
1499 const IndexList &inds = IndexList())
1500 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencer8d5a6ae2006-12-04 18:38:05 +00001501 uint16_t opcode;
1502 uint16_t predicate;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001503 std::vector<Constant*> operands;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001504 IndexList indices;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001505 bool operator==(const ExprMapKeyType& that) const {
1506 return this->opcode == that.opcode &&
1507 this->predicate == that.predicate &&
Bill Wendling9a20afd2008-10-26 00:19:56 +00001508 this->operands == that.operands &&
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001509 this->indices == that.indices;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001510 }
1511 bool operator<(const ExprMapKeyType & that) const {
1512 return this->opcode < that.opcode ||
1513 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1514 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001515 this->operands < that.operands) ||
1516 (this->opcode == that.opcode && this->predicate == that.predicate &&
1517 this->operands == that.operands && this->indices < that.indices);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001518 }
1519
1520 bool operator!=(const ExprMapKeyType& that) const {
1521 return !(*this == that);
1522 }
1523};
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001524
Dan Gohman844731a2008-05-13 00:00:25 +00001525}
1526
Chris Lattner31f84992003-11-21 20:23:48 +00001527namespace llvm {
1528 template<>
1529 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer728b6db2006-12-03 05:48:19 +00001530 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1531 unsigned short pred = 0) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001532 if (Instruction::isCast(V.opcode))
1533 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1534 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer832254e2007-02-02 02:16:23 +00001535 V.opcode < Instruction::BinaryOpsEnd))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001536 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1537 if (V.opcode == Instruction::Select)
1538 return new SelectConstantExpr(V.operands[0], V.operands[1],
1539 V.operands[2]);
1540 if (V.opcode == Instruction::ExtractElement)
1541 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1542 if (V.opcode == Instruction::InsertElement)
1543 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1544 V.operands[2]);
1545 if (V.opcode == Instruction::ShuffleVector)
1546 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1547 V.operands[2]);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001548 if (V.opcode == Instruction::InsertValue)
1549 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1550 V.indices, Ty);
1551 if (V.opcode == Instruction::ExtractValue)
1552 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001553 if (V.opcode == Instruction::GetElementPtr) {
1554 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greif051a9502008-04-06 20:25:17 +00001555 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001556 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001557
Reid Spencer077d0eb2006-12-04 05:19:50 +00001558 // The compare instructions are weird. We have to encode the predicate
1559 // value and it is combined with the instruction opcode by multiplying
1560 // the opcode by one hundred. We must decode this to get the predicate.
1561 if (V.opcode == Instruction::ICmp)
Nate Begemanac80ade2008-05-12 19:01:56 +00001562 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spencer077d0eb2006-12-04 05:19:50 +00001563 V.operands[0], V.operands[1]);
1564 if (V.opcode == Instruction::FCmp)
Nate Begemanac80ade2008-05-12 19:01:56 +00001565 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1566 V.operands[0], V.operands[1]);
Torok Edwinc23197a2009-07-14 16:55:14 +00001567 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen6ebe2352006-12-15 21:47:01 +00001568 return 0;
Chris Lattnered468e372003-10-05 00:17:43 +00001569 }
Chris Lattner31f84992003-11-21 20:23:48 +00001570 };
Chris Lattnered468e372003-10-05 00:17:43 +00001571
Chris Lattner31f84992003-11-21 20:23:48 +00001572 template<>
1573 struct ConvertConstantType<ConstantExpr, Type> {
1574 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1575 Constant *New;
1576 switch (OldC->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001577 case Instruction::Trunc:
1578 case Instruction::ZExt:
1579 case Instruction::SExt:
1580 case Instruction::FPTrunc:
1581 case Instruction::FPExt:
1582 case Instruction::UIToFP:
1583 case Instruction::SIToFP:
1584 case Instruction::FPToUI:
1585 case Instruction::FPToSI:
1586 case Instruction::PtrToInt:
1587 case Instruction::IntToPtr:
1588 case Instruction::BitCast:
Reid Spencerd977d862006-12-12 23:36:14 +00001589 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001590 NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001591 break;
Chris Lattner08a45cc2004-03-12 05:54:04 +00001592 case Instruction::Select:
1593 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1594 OldC->getOperand(1),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001595 OldC->getOperand(2));
Chris Lattner08a45cc2004-03-12 05:54:04 +00001596 break;
Chris Lattner31f84992003-11-21 20:23:48 +00001597 default:
1598 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001599 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner31f84992003-11-21 20:23:48 +00001600 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001601 OldC->getOperand(1));
Chris Lattner31f84992003-11-21 20:23:48 +00001602 break;
1603 case Instruction::GetElementPtr:
Misha Brukmanfd939082005-04-21 23:48:37 +00001604 // Make everyone now use a constant of the new type...
Chris Lattner7fa6e662004-10-11 22:52:25 +00001605 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001606 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001607 &Idx[0], Idx.size());
Chris Lattner31f84992003-11-21 20:23:48 +00001608 break;
1609 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001610
Chris Lattner31f84992003-11-21 20:23:48 +00001611 assert(New != OldC && "Didn't replace constant??");
1612 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001613 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001614 }
1615 };
1616} // end namespace llvm
Chris Lattnered468e372003-10-05 00:17:43 +00001617
1618
Chris Lattner6823c9f2004-08-04 04:48:01 +00001619static ExprMapKeyType getValType(ConstantExpr *CE) {
1620 std::vector<Constant*> Operands;
1621 Operands.reserve(CE->getNumOperands());
1622 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1623 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spencer077d0eb2006-12-04 05:19:50 +00001624 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001625 CE->isCompare() ? CE->getPredicate() : 0,
1626 CE->hasIndices() ?
1627 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner6823c9f2004-08-04 04:48:01 +00001628}
1629
Chris Lattner8a94bf12006-09-28 00:35:06 +00001630static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1631 ConstantExpr> > ExprConstants;
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001632
Reid Spencer3da59db2006-11-27 01:05:10 +00001633/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands66a1a052008-03-30 19:38:55 +00001634/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer3da59db2006-11-27 01:05:10 +00001635static inline Constant *getFoldedCast(
Owen Anderson04fb7c32009-06-20 00:24:58 +00001636 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001637 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001638 // Fold a few common cases
Owen Anderson0a5372e2009-07-13 04:09:18 +00001639 if (Constant *FC =
1640 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +00001641 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001642
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001643 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001644 std::vector<Constant*> argVec(1, C);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001645 ExprMapKeyType Key(opc, argVec);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001646
Owen Anderson31c36f02009-06-17 20:10:08 +00001647 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001648 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001649}
Reid Spencer7858b332006-12-05 19:14:13 +00001650
Owen Anderson04fb7c32009-06-20 00:24:58 +00001651Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001652 Instruction::CastOps opc = Instruction::CastOps(oc);
1653 assert(Instruction::isCast(opc) && "opcode out of range");
1654 assert(C && Ty && "Null arguments to getCast");
1655 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1656
1657 switch (opc) {
1658 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001659 llvm_unreachable("Invalid cast opcode");
Reid Spencer3da59db2006-11-27 01:05:10 +00001660 break;
Owen Anderson04fb7c32009-06-20 00:24:58 +00001661 case Instruction::Trunc: return getTrunc(C, Ty);
1662 case Instruction::ZExt: return getZExt(C, Ty);
1663 case Instruction::SExt: return getSExt(C, Ty);
1664 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1665 case Instruction::FPExt: return getFPExtend(C, Ty);
1666 case Instruction::UIToFP: return getUIToFP(C, Ty);
1667 case Instruction::SIToFP: return getSIToFP(C, Ty);
1668 case Instruction::FPToUI: return getFPToUI(C, Ty);
1669 case Instruction::FPToSI: return getFPToSI(C, Ty);
1670 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1671 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1672 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001673 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001674 return 0;
Reid Spencer7858b332006-12-05 19:14:13 +00001675}
1676
Reid Spencer848414e2006-12-04 20:17:56 +00001677Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001678 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001679 return getCast(Instruction::BitCast, C, Ty);
1680 return getCast(Instruction::ZExt, C, Ty);
1681}
1682
Owen Anderson04fb7c32009-06-20 00:24:58 +00001683Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001684 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001685 return getCast(Instruction::BitCast, C, Ty);
1686 return getCast(Instruction::SExt, C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001687}
1688
1689Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001690 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001691 return getCast(Instruction::BitCast, C, Ty);
1692 return getCast(Instruction::Trunc, C, Ty);
1693}
1694
Owen Anderson04fb7c32009-06-20 00:24:58 +00001695Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerc0459fb2006-12-05 03:25:26 +00001696 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner42a75512007-01-15 02:27:26 +00001697 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerc0459fb2006-12-05 03:25:26 +00001698
Chris Lattner42a75512007-01-15 02:27:26 +00001699 if (Ty->isInteger())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001700 return getCast(Instruction::PtrToInt, S, Ty);
1701 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerc0459fb2006-12-05 03:25:26 +00001702}
1703
Reid Spencer84f3eab2006-12-12 00:51:07 +00001704Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1705 bool isSigned) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001706 assert(C->getType()->isIntOrIntVector() &&
1707 Ty->isIntOrIntVector() && "Invalid cast");
1708 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1709 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer84f3eab2006-12-12 00:51:07 +00001710 Instruction::CastOps opcode =
1711 (SrcBits == DstBits ? Instruction::BitCast :
1712 (SrcBits > DstBits ? Instruction::Trunc :
1713 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1714 return getCast(opcode, C, Ty);
1715}
1716
1717Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001718 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer84f3eab2006-12-12 00:51:07 +00001719 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001720 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1721 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerf25212a2006-12-12 05:38:50 +00001722 if (SrcBits == DstBits)
1723 return C; // Avoid a useless cast
Reid Spencer84f3eab2006-12-12 00:51:07 +00001724 Instruction::CastOps opcode =
Reid Spencerf25212a2006-12-12 05:38:50 +00001725 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer84f3eab2006-12-12 00:51:07 +00001726 return getCast(opcode, C, Ty);
1727}
1728
Owen Anderson04fb7c32009-06-20 00:24:58 +00001729Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001730#ifndef NDEBUG
1731 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1732 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1733#endif
1734 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1735 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1736 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1737 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001738 "SrcTy must be larger than DestTy for Trunc!");
1739
Owen Anderson04fb7c32009-06-20 00:24:58 +00001740 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001741}
1742
Owen Anderson04fb7c32009-06-20 00:24:58 +00001743Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001744#ifndef NDEBUG
1745 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1746 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1747#endif
1748 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1749 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1750 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1751 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001752 "SrcTy must be smaller than DestTy for SExt!");
1753
Owen Anderson04fb7c32009-06-20 00:24:58 +00001754 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001755}
1756
Owen Anderson04fb7c32009-06-20 00:24:58 +00001757Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001758#ifndef NDEBUG
1759 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1760 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1761#endif
1762 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1763 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1764 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1765 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001766 "SrcTy must be smaller than DestTy for ZExt!");
1767
Owen Anderson04fb7c32009-06-20 00:24:58 +00001768 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001769}
1770
Owen Anderson04fb7c32009-06-20 00:24:58 +00001771Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001772#ifndef NDEBUG
1773 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1774 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1775#endif
1776 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1777 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1778 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001779 "This is an illegal floating point truncation!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001780 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001781}
1782
Owen Anderson04fb7c32009-06-20 00:24:58 +00001783Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001784#ifndef NDEBUG
1785 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1786 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1787#endif
1788 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1789 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1790 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001791 "This is an illegal floating point extension!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001792 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001793}
1794
Owen Anderson04fb7c32009-06-20 00:24:58 +00001795Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001796#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001797 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1798 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001799#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001800 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1801 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1802 "This is an illegal uint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001803 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001804}
1805
Owen Anderson04fb7c32009-06-20 00:24:58 +00001806Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001807#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001808 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1809 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001810#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001811 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1812 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00001813 "This is an illegal sint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001814 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001815}
1816
Owen Anderson04fb7c32009-06-20 00:24:58 +00001817Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001818#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001819 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1820 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001821#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001822 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1823 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1824 "This is an illegal floating point to uint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001825 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001826}
1827
Owen Anderson04fb7c32009-06-20 00:24:58 +00001828Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001829#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001830 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1831 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001832#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001833 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1834 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1835 "This is an illegal floating point to sint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001836 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001837}
1838
Owen Anderson04fb7c32009-06-20 00:24:58 +00001839Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001840 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner42a75512007-01-15 02:27:26 +00001841 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001842 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001843}
1844
Owen Anderson04fb7c32009-06-20 00:24:58 +00001845Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001846 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer3da59db2006-11-27 01:05:10 +00001847 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001848 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001849}
1850
Owen Anderson04fb7c32009-06-20 00:24:58 +00001851Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001852 // BitCast implies a no-op cast of type only. No bits change. However, you
1853 // can't cast pointers to anything but pointers.
Devang Patelb6dc9352008-11-03 23:20:04 +00001854#ifndef NDEBUG
Reid Spencer3da59db2006-11-27 01:05:10 +00001855 const Type *SrcTy = C->getType();
1856 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer848414e2006-12-04 20:17:56 +00001857 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer3da59db2006-11-27 01:05:10 +00001858
1859 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1860 // or nonptr->ptr). For all the other types, the cast is okay if source and
1861 // destination bit widths are identical.
1862 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1863 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Patelb6dc9352008-11-03 23:20:04 +00001864#endif
Chris Lattnercf1bb082009-03-08 04:06:26 +00001865 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattner8c7f24a2009-03-21 06:55:54 +00001866
1867 // It is common to ask for a bitcast of a value to its own type, handle this
1868 // speedily.
1869 if (C->getType() == DstTy) return C;
1870
Owen Anderson04fb7c32009-06-20 00:24:58 +00001871 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001872}
1873
Chris Lattnered468e372003-10-05 00:17:43 +00001874Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001875 Constant *C1, Constant *C2) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001876 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001877 assert(Opcode >= Instruction::BinaryOpsBegin &&
1878 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001879 "Invalid opcode in binary constant expression");
1880 assert(C1->getType() == C2->getType() &&
1881 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001882
Reid Spencer4fe16d62007-01-11 18:21:29 +00001883 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson0a5372e2009-07-13 04:09:18 +00001884 if (Constant *FC = ConstantFoldBinaryInstruction(
1885 getGlobalContext(), Opcode, C1, C2))
Chris Lattnered468e372003-10-05 00:17:43 +00001886 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001887
Chris Lattner9bc02a42003-05-13 21:37:02 +00001888 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencer67263fe2006-12-04 21:35:24 +00001889 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001890
Owen Anderson04fb7c32009-06-20 00:24:58 +00001891 // Implicitly locked.
1892 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001893}
1894
Reid Spencere4d87aa2006-12-23 06:05:41 +00001895Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begemanff795a82008-07-25 17:56:27 +00001896 Constant *C1, Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001897 switch (predicate) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001898 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanb5557ab2008-07-25 17:35:37 +00001899 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1900 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1901 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1902 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1903 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1904 case CmpInst::FCMP_TRUE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001905 return getFCmp(predicate, C1, C2);
1906
Nate Begemanb5557ab2008-07-25 17:35:37 +00001907 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1908 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1909 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1910 case CmpInst::ICMP_SLE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001911 return getICmp(predicate, C1, C2);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001912 }
Reid Spencer67263fe2006-12-04 21:35:24 +00001913}
1914
Owen Anderson04fb7c32009-06-20 00:24:58 +00001915Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001916 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1917 if (C1->getType()->isFPOrFPVector()) {
1918 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1919 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1920 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1921 }
Chris Lattner91b362b2004-08-17 17:28:46 +00001922#ifndef NDEBUG
1923 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001924 case Instruction::Add:
Reid Spencer0a783f72006-11-02 01:53:59 +00001925 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001926 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001927 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001928 assert(C1->getType()->isIntOrIntVector() &&
1929 "Tried to create an integer operation on a non-integer type!");
1930 break;
1931 case Instruction::FAdd:
1932 case Instruction::FSub:
1933 case Instruction::FMul:
1934 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1935 assert(C1->getType()->isFPOrFPVector() &&
1936 "Tried to create a floating-point operation on a "
1937 "non-floating-point type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001938 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001939 case Instruction::UDiv:
1940 case Instruction::SDiv:
1941 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001942 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001943 "Tried to create an arithmetic operation on a non-arithmetic type!");
1944 break;
1945 case Instruction::FDiv:
1946 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001947 assert(C1->getType()->isFPOrFPVector() &&
1948 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001949 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001950 case Instruction::URem:
1951 case Instruction::SRem:
1952 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001953 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001954 "Tried to create an arithmetic operation on a non-arithmetic type!");
1955 break;
1956 case Instruction::FRem:
1957 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001958 assert(C1->getType()->isFPOrFPVector() &&
1959 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer0a783f72006-11-02 01:53:59 +00001960 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001961 case Instruction::And:
1962 case Instruction::Or:
1963 case Instruction::Xor:
1964 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001965 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001966 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001967 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001968 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001969 case Instruction::LShr:
1970 case Instruction::AShr:
Reid Spencer832254e2007-02-02 02:16:23 +00001971 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanc1317932009-03-14 17:09:17 +00001972 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001973 "Tried to create a shift operation on a non-integer type!");
1974 break;
1975 default:
1976 break;
1977 }
1978#endif
1979
Owen Anderson04fb7c32009-06-20 00:24:58 +00001980 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencer67263fe2006-12-04 21:35:24 +00001981}
1982
Reid Spencere4d87aa2006-12-23 06:05:41 +00001983Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencer67263fe2006-12-04 21:35:24 +00001984 Constant *C1, Constant *C2) {
1985 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001986 return getCompareTy(pred, C1, C2);
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001987}
1988
Chris Lattner08a45cc2004-03-12 05:54:04 +00001989Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001990 Constant *V1, Constant *V2) {
Chris Lattner9ace0cd2008-12-29 00:16:12 +00001991 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner08a45cc2004-03-12 05:54:04 +00001992
1993 if (ReqTy == V1->getType())
Owen Anderson0a5372e2009-07-13 04:09:18 +00001994 if (Constant *SC = ConstantFoldSelectInstruction(
1995 getGlobalContext(), C, V1, V2))
Chris Lattner08a45cc2004-03-12 05:54:04 +00001996 return SC; // Fold common cases
1997
1998 std::vector<Constant*> argVec(3, C);
1999 argVec[1] = V1;
2000 argVec[2] = V2;
Reid Spencer077d0eb2006-12-04 05:19:50 +00002001 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00002002
Owen Anderson04fb7c32009-06-20 00:24:58 +00002003 // Implicitly locked.
2004 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00002005}
2006
Chris Lattnered468e372003-10-05 00:17:43 +00002007Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002008 Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00002009 unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002010 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2011 Idxs+NumIdx) ==
2012 cast<PointerType>(ReqTy)->getElementType() &&
2013 "GEP indices invalid!");
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00002014
Owen Anderson0a5372e2009-07-13 04:09:18 +00002015 if (Constant *FC = ConstantFoldGetElementPtr(
2016 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattnerd628f6a2003-04-17 19:24:48 +00002017 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00002018
Chris Lattnered468e372003-10-05 00:17:43 +00002019 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00002020 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002021 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00002022 std::vector<Constant*> ArgVec;
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002023 ArgVec.reserve(NumIdx+1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00002024 ArgVec.push_back(C);
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002025 for (unsigned i = 0; i != NumIdx; ++i)
2026 ArgVec.push_back(cast<Constant>(Idxs[i]));
2027 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00002028
2029 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00002030 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002031}
2032
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002033Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00002034 unsigned NumIdx) {
Chris Lattnered468e372003-10-05 00:17:43 +00002035 // Get the result type of the getelementptr!
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002036 const Type *Ty =
Dan Gohman041e2eb2008-05-15 19:50:34 +00002037 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00002038 assert(Ty && "GEP indices invalid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00002039 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson04fb7c32009-06-20 00:24:58 +00002040 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner7fa6e662004-10-11 22:52:25 +00002041}
2042
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002043Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00002044 unsigned NumIdx) {
2045 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00002046}
2047
Chris Lattner2b9a5da2007-01-31 04:40:28 +00002048
Reid Spencer077d0eb2006-12-04 05:19:50 +00002049Constant *
2050ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2051 assert(LHS->getType() == RHS->getType());
2052 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2053 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2054
Owen Anderson0a5372e2009-07-13 04:09:18 +00002055 if (Constant *FC = ConstantFoldCompareInstruction(
2056 getGlobalContext(),pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00002057 return FC; // Fold a few common cases...
2058
2059 // Look up the constant in the table first to ensure uniqueness
2060 std::vector<Constant*> ArgVec;
2061 ArgVec.push_back(LHS);
2062 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00002063 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00002064 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00002065
2066 // Implicitly locked.
2067 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002068}
2069
2070Constant *
2071ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2072 assert(LHS->getType() == RHS->getType());
2073 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2074
Owen Anderson0a5372e2009-07-13 04:09:18 +00002075 if (Constant *FC = ConstantFoldCompareInstruction(
2076 getGlobalContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00002077 return FC; // Fold a few common cases...
2078
2079 // Look up the constant in the table first to ensure uniqueness
2080 std::vector<Constant*> ArgVec;
2081 ArgVec.push_back(LHS);
2082 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00002083 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00002084 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00002085
2086 // Implicitly locked.
2087 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002088}
2089
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002090Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2091 Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00002092 if (Constant *FC = ConstantFoldExtractElementInstruction(
2093 getGlobalContext(), Val, Idx))
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00002094 return FC; // Fold a few common cases...
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002095 // Look up the constant in the table first to ensure uniqueness
2096 std::vector<Constant*> ArgVec(1, Val);
2097 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002098 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00002099
2100 // Implicitly locked.
2101 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002102}
2103
2104Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002105 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00002106 "Tried to create extractelement operation on non-vector type!");
Reid Spencer79e21d32006-12-31 05:26:44 +00002107 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00002108 "Extractelement index must be i32 type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002109 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002110 Val, Idx);
2111}
Chris Lattnered468e372003-10-05 00:17:43 +00002112
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00002113Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2114 Constant *Elt, Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00002115 if (Constant *FC = ConstantFoldInsertElementInstruction(
2116 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00002117 return FC; // Fold a few common cases...
2118 // Look up the constant in the table first to ensure uniqueness
2119 std::vector<Constant*> ArgVec(1, Val);
2120 ArgVec.push_back(Elt);
2121 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002122 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00002123
2124 // Implicitly locked.
2125 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00002126}
2127
2128Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2129 Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002130 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00002131 "Tried to create insertelement operation on non-vector type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002132 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00002133 && "Insertelement types must match!");
Reid Spencer79e21d32006-12-31 05:26:44 +00002134 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00002135 "Insertelement index must be i32 type!");
Gordon Henriksen699609c2008-08-30 15:41:51 +00002136 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00002137}
2138
Chris Lattner00f10232006-04-08 01:18:18 +00002139Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2140 Constant *V2, Constant *Mask) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00002141 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2142 getGlobalContext(), V1, V2, Mask))
Chris Lattner00f10232006-04-08 01:18:18 +00002143 return FC; // Fold a few common cases...
2144 // Look up the constant in the table first to ensure uniqueness
2145 std::vector<Constant*> ArgVec(1, V1);
2146 ArgVec.push_back(V2);
2147 ArgVec.push_back(Mask);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002148 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00002149
2150 // Implicitly locked.
2151 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00002152}
2153
2154Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2155 Constant *Mask) {
2156 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2157 "Invalid shuffle vector constant expr operands!");
Nate Begeman0f123cf2009-02-12 21:28:33 +00002158
2159 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2160 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2161 const Type *ShufTy = VectorType::get(EltTy, NElts);
2162 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattner00f10232006-04-08 01:18:18 +00002163}
2164
Dan Gohman041e2eb2008-05-15 19:50:34 +00002165Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2166 Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002167 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002168 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2169 Idxs+NumIdx) == Val->getType() &&
2170 "insertvalue indices invalid!");
2171 assert(Agg->getType() == ReqTy &&
2172 "insertvalue type invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00002173 assert(Agg->getType()->isFirstClassType() &&
2174 "Non-first-class type for constant InsertValue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00002175 Constant *FC = ConstantFoldInsertValueInstruction(
2176 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00002177 assert(FC && "InsertValue constant expr couldn't be folded!");
2178 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00002179}
2180
2181Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002182 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00002183 assert(Agg->getType()->isFirstClassType() &&
2184 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002185
Dan Gohmane4569942008-05-23 00:36:11 +00002186 const Type *ReqTy = Agg->getType();
Devang Patelb6dc9352008-11-03 23:20:04 +00002187#ifndef NDEBUG
Dan Gohmane4569942008-05-23 00:36:11 +00002188 const Type *ValTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00002189 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Patelb6dc9352008-11-03 23:20:04 +00002190#endif
Dan Gohmane4569942008-05-23 00:36:11 +00002191 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002192 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2193}
2194
2195Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002196 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002197 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2198 Idxs+NumIdx) == ReqTy &&
2199 "extractvalue indices invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00002200 assert(Agg->getType()->isFirstClassType() &&
2201 "Non-first-class type for constant extractvalue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00002202 Constant *FC = ConstantFoldExtractValueInstruction(
2203 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00002204 assert(FC && "ExtractValue constant expr couldn't be folded!");
2205 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00002206}
2207
2208Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002209 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00002210 assert(Agg->getType()->isFirstClassType() &&
2211 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002212
2213 const Type *ReqTy =
2214 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2215 assert(ReqTy && "extractvalue indices invalid!");
2216 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2217}
2218
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002219// destroyConstant - Remove the constant from the constant table...
2220//
Owen Anderson04fb7c32009-06-20 00:24:58 +00002221void ConstantExpr::destroyConstant() {
2222 // Implicitly locked.
2223 ExprConstants->remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002224 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00002225}
2226
Chris Lattnerc188eeb2002-07-30 18:54:25 +00002227const char *ConstantExpr::getOpcodeName() const {
2228 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00002229}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00002230
Chris Lattner5cbade92005-10-03 21:58:36 +00002231//===----------------------------------------------------------------------===//
2232// replaceUsesOfWithOnConstant implementations
2233
Chris Lattner54984052007-08-21 00:55:23 +00002234/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2235/// 'From' to be uses of 'To'. This must update the uniquing data structures
2236/// etc.
2237///
2238/// Note that we intentionally replace all uses of From with To here. Consider
2239/// a large array that uses 'From' 1000 times. By handling this case all here,
2240/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2241/// single invocation handles all 1000 uses. Handling them one at a time would
2242/// work, but would be really slow because it would have to unique each updated
2243/// array instance.
Chris Lattner5cbade92005-10-03 21:58:36 +00002244void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002245 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002246 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattnerc182a882005-10-04 01:17:50 +00002247 Constant *ToC = cast<Constant>(To);
Chris Lattner23ec01f2005-10-04 18:47:09 +00002248
Jim Laskeyede5aa42006-07-17 17:38:29 +00002249 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnercea141f2005-10-03 22:51:37 +00002250 Lookup.first.first = getType();
2251 Lookup.second = this;
Chris Lattner23ec01f2005-10-04 18:47:09 +00002252
Chris Lattnercea141f2005-10-03 22:51:37 +00002253 std::vector<Constant*> &Values = Lookup.first.second;
2254 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattner23ec01f2005-10-04 18:47:09 +00002255
Chris Lattnerc182a882005-10-04 01:17:50 +00002256 // Fill values with the modified operands of the constant array. Also,
2257 // compute whether this turns into an all-zeros array.
Chris Lattner23ec01f2005-10-04 18:47:09 +00002258 bool isAllZeros = false;
Chris Lattner54984052007-08-21 00:55:23 +00002259 unsigned NumUpdated = 0;
Chris Lattner23ec01f2005-10-04 18:47:09 +00002260 if (!ToC->isNullValue()) {
Chris Lattner54984052007-08-21 00:55:23 +00002261 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2262 Constant *Val = cast<Constant>(O->get());
2263 if (Val == From) {
2264 Val = ToC;
2265 ++NumUpdated;
2266 }
2267 Values.push_back(Val);
2268 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00002269 } else {
2270 isAllZeros = true;
2271 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2272 Constant *Val = cast<Constant>(O->get());
Chris Lattner54984052007-08-21 00:55:23 +00002273 if (Val == From) {
2274 Val = ToC;
2275 ++NumUpdated;
2276 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00002277 Values.push_back(Val);
2278 if (isAllZeros) isAllZeros = Val->isNullValue();
2279 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002280 }
2281
Chris Lattnercea141f2005-10-03 22:51:37 +00002282 Constant *Replacement = 0;
2283 if (isAllZeros) {
2284 Replacement = ConstantAggregateZero::get(getType());
2285 } else {
2286 // Check to see if we have this array type already.
Owen Andersona9d1f2c2009-07-07 18:33:04 +00002287 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnercea141f2005-10-03 22:51:37 +00002288 bool Exists;
Jim Laskeyede5aa42006-07-17 17:38:29 +00002289 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner8a94bf12006-09-28 00:35:06 +00002290 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnercea141f2005-10-03 22:51:37 +00002291
2292 if (Exists) {
2293 Replacement = I->second;
2294 } else {
2295 // Okay, the new shape doesn't exist in the system yet. Instead of
2296 // creating a new constant array, inserting it, replaceallusesof'ing the
2297 // old with the new, then deleting the old... just update the current one
2298 // in place!
Chris Lattner8a94bf12006-09-28 00:35:06 +00002299 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnercea141f2005-10-03 22:51:37 +00002300
Chris Lattner54984052007-08-21 00:55:23 +00002301 // Update to the new value. Optimize for the case when we have a single
2302 // operand that we're changing, but handle bulk updates efficiently.
2303 if (NumUpdated == 1) {
2304 unsigned OperandToUpdate = U-OperandList;
2305 assert(getOperand(OperandToUpdate) == From &&
2306 "ReplaceAllUsesWith broken!");
2307 setOperand(OperandToUpdate, ToC);
2308 } else {
2309 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2310 if (getOperand(i) == From)
2311 setOperand(i, ToC);
2312 }
Chris Lattnercea141f2005-10-03 22:51:37 +00002313 return;
2314 }
2315 }
2316
2317 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00002318 assert(Replacement != this && "I didn't contain From!");
2319
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002320 // Everyone using this now uses the replacement.
2321 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002322
2323 // Delete the old constant!
2324 destroyConstant();
2325}
2326
2327void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002328 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002329 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattnerc182a882005-10-04 01:17:50 +00002330 Constant *ToC = cast<Constant>(To);
2331
Chris Lattner23ec01f2005-10-04 18:47:09 +00002332 unsigned OperandToUpdate = U-OperandList;
2333 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2334
Jim Laskeyede5aa42006-07-17 17:38:29 +00002335 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerc182a882005-10-04 01:17:50 +00002336 Lookup.first.first = getType();
2337 Lookup.second = this;
2338 std::vector<Constant*> &Values = Lookup.first.second;
2339 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattner5cbade92005-10-03 21:58:36 +00002340
Chris Lattner23ec01f2005-10-04 18:47:09 +00002341
Chris Lattnerc182a882005-10-04 01:17:50 +00002342 // Fill values with the modified operands of the constant struct. Also,
2343 // compute whether this turns into an all-zeros struct.
Chris Lattner23ec01f2005-10-04 18:47:09 +00002344 bool isAllZeros = false;
2345 if (!ToC->isNullValue()) {
2346 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2347 Values.push_back(cast<Constant>(O->get()));
2348 } else {
2349 isAllZeros = true;
2350 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2351 Constant *Val = cast<Constant>(O->get());
2352 Values.push_back(Val);
2353 if (isAllZeros) isAllZeros = Val->isNullValue();
2354 }
Chris Lattnerc182a882005-10-04 01:17:50 +00002355 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00002356 Values[OperandToUpdate] = ToC;
2357
Chris Lattnerc182a882005-10-04 01:17:50 +00002358 Constant *Replacement = 0;
2359 if (isAllZeros) {
2360 Replacement = ConstantAggregateZero::get(getType());
2361 } else {
2362 // Check to see if we have this array type already.
Owen Andersona9d1f2c2009-07-07 18:33:04 +00002363 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerc182a882005-10-04 01:17:50 +00002364 bool Exists;
Jim Laskeyede5aa42006-07-17 17:38:29 +00002365 StructConstantsTy::MapTy::iterator I =
Chris Lattner8a94bf12006-09-28 00:35:06 +00002366 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerc182a882005-10-04 01:17:50 +00002367
2368 if (Exists) {
2369 Replacement = I->second;
2370 } else {
2371 // Okay, the new shape doesn't exist in the system yet. Instead of
2372 // creating a new constant struct, inserting it, replaceallusesof'ing the
2373 // old with the new, then deleting the old... just update the current one
2374 // in place!
Chris Lattner8a94bf12006-09-28 00:35:06 +00002375 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerc182a882005-10-04 01:17:50 +00002376
Chris Lattner23ec01f2005-10-04 18:47:09 +00002377 // Update to the new value.
2378 setOperand(OperandToUpdate, ToC);
Chris Lattnerc182a882005-10-04 01:17:50 +00002379 return;
2380 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002381 }
2382
Chris Lattner5cbade92005-10-03 21:58:36 +00002383 assert(Replacement != this && "I didn't contain From!");
2384
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002385 // Everyone using this now uses the replacement.
2386 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002387
2388 // Delete the old constant!
2389 destroyConstant();
2390}
2391
Reid Spencer9d6565a2007-02-15 02:26:10 +00002392void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002393 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002394 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2395
2396 std::vector<Constant*> Values;
2397 Values.reserve(getNumOperands()); // Build replacement array...
2398 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2399 Constant *Val = getOperand(i);
2400 if (Val == From) Val = cast<Constant>(To);
2401 Values.push_back(Val);
2402 }
2403
Reid Spencer9d6565a2007-02-15 02:26:10 +00002404 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattner5cbade92005-10-03 21:58:36 +00002405 assert(Replacement != this && "I didn't contain From!");
2406
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002407 // Everyone using this now uses the replacement.
2408 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002409
2410 // Delete the old constant!
2411 destroyConstant();
2412}
2413
2414void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002415 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002416 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2417 Constant *To = cast<Constant>(ToV);
2418
2419 Constant *Replacement = 0;
2420 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002421 SmallVector<Constant*, 8> Indices;
Chris Lattner5cbade92005-10-03 21:58:36 +00002422 Constant *Pointer = getOperand(0);
2423 Indices.reserve(getNumOperands()-1);
2424 if (Pointer == From) Pointer = To;
2425
2426 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2427 Constant *Val = getOperand(i);
2428 if (Val == From) Val = To;
2429 Indices.push_back(Val);
2430 }
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002431 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2432 &Indices[0], Indices.size());
Dan Gohman041e2eb2008-05-15 19:50:34 +00002433 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002434 Constant *Agg = getOperand(0);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002435 if (Agg == From) Agg = To;
2436
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002437 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002438 Replacement = ConstantExpr::getExtractValue(Agg,
2439 &Indices[0], Indices.size());
2440 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002441 Constant *Agg = getOperand(0);
2442 Constant *Val = getOperand(1);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002443 if (Agg == From) Agg = To;
2444 if (Val == From) Val = To;
2445
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002446 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002447 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2448 &Indices[0], Indices.size());
Reid Spencer3da59db2006-11-27 01:05:10 +00002449 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002450 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002451 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002452 } else if (getOpcode() == Instruction::Select) {
2453 Constant *C1 = getOperand(0);
2454 Constant *C2 = getOperand(1);
2455 Constant *C3 = getOperand(2);
2456 if (C1 == From) C1 = To;
2457 if (C2 == From) C2 = To;
2458 if (C3 == From) C3 = To;
2459 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002460 } else if (getOpcode() == Instruction::ExtractElement) {
2461 Constant *C1 = getOperand(0);
2462 Constant *C2 = getOperand(1);
2463 if (C1 == From) C1 = To;
2464 if (C2 == From) C2 = To;
2465 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002466 } else if (getOpcode() == Instruction::InsertElement) {
2467 Constant *C1 = getOperand(0);
2468 Constant *C2 = getOperand(1);
2469 Constant *C3 = getOperand(1);
2470 if (C1 == From) C1 = To;
2471 if (C2 == From) C2 = To;
2472 if (C3 == From) C3 = To;
2473 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2474 } else if (getOpcode() == Instruction::ShuffleVector) {
2475 Constant *C1 = getOperand(0);
2476 Constant *C2 = getOperand(1);
2477 Constant *C3 = getOperand(2);
2478 if (C1 == From) C1 = To;
2479 if (C2 == From) C2 = To;
2480 if (C3 == From) C3 = To;
2481 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002482 } else if (isCompare()) {
2483 Constant *C1 = getOperand(0);
2484 Constant *C2 = getOperand(1);
2485 if (C1 == From) C1 = To;
2486 if (C2 == From) C2 = To;
2487 if (getOpcode() == Instruction::ICmp)
2488 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002489 else {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002490 assert(getOpcode() == Instruction::FCmp);
2491 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002492 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002493 } else if (getNumOperands() == 2) {
2494 Constant *C1 = getOperand(0);
2495 Constant *C2 = getOperand(1);
2496 if (C1 == From) C1 = To;
2497 if (C2 == From) C2 = To;
2498 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2499 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002500 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002501 return;
2502 }
2503
2504 assert(Replacement != this && "I didn't contain From!");
2505
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002506 // Everyone using this now uses the replacement.
2507 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002508
2509 // Delete the old constant!
2510 destroyConstant();
Matthijs Kooijman10b9de62008-07-03 07:46:41 +00002511}
Nick Lewycky21cc4462009-04-04 07:22:01 +00002512
Nick Lewyckycb337992009-05-10 20:57:05 +00002513void MDNode::replaceElement(Value *From, Value *To) {
2514 SmallVector<Value*, 4> Values;
2515 Values.reserve(getNumElements()); // Build replacement array...
2516 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2517 Value *Val = getElement(i);
2518 if (Val == From) Val = To;
Nick Lewycky21cc4462009-04-04 07:22:01 +00002519 Values.push_back(Val);
2520 }
Nick Lewyckycb337992009-05-10 20:57:05 +00002521
2522 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky21cc4462009-04-04 07:22:01 +00002523 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckycb337992009-05-10 20:57:05 +00002524
Nick Lewycky21cc4462009-04-04 07:22:01 +00002525 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckycb337992009-05-10 20:57:05 +00002526
Nick Lewycky21cc4462009-04-04 07:22:01 +00002527 destroyConstant();
2528}