blob: 9f544a720598f03149275e9276f73e64ff461242 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000021#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000027#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000028#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000029#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000030#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000031#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000034#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000035#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Chris Lattner2f7c9632001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000039// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
41
Owen Andersond830eb82009-06-18 19:10:19 +000042// Becomes a no-op when multithreading is disabled.
43ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000044
Chris Lattner3462ae32001-12-03 22:26:30 +000045void Constant::destroyConstantImpl() {
46 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000047 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000048 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-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 Lattner3462ae32001-12-03 22:26:30 +000051 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000052 //
53 while (!use_empty()) {
54 Value *V = use_back();
55#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000056 if (!isa<Constant>(V))
Bill Wendling6a462f12006-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 Lattnerd7a73302001-10-13 06:57:33 +000060#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000061 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000062 Constant *CV = cast<Constant>(V);
63 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000064
65 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000066 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 }
68
69 // Value has no outstanding references it is safe to delete it now...
70 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000071}
Chris Lattner2f7c9632001-06-06 20:29:01 +000072
Chris Lattner23dd1f62006-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 Spencer7e80b0b2006-10-26 06:15:43 +000090 case Instruction::UDiv:
91 case Instruction::SDiv:
92 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000093 case Instruction::URem:
94 case Instruction::SRem:
95 case Instruction::FRem:
Chris Lattner23dd1f62006-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 Korobeynikov7437b592009-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 Korobeynikov255a3cb2009-03-30 15:28:21 +0000120
121 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000122 }
123
Evan Chengf9e003b2007-03-08 00:59:12 +0000124 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000125 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000126 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000127
Evan Chengf9e003b2007-03-08 00:59:12 +0000128 return false;
129}
130
Chris Lattner2105d662008-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 Lattnerc5098a22008-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 Anderson53a52212009-07-13 04:09:18 +0000135void Constant::getVectorElements(LLVMContext &Context,
136 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-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 Anderson53a52212009-07-13 04:09:18 +0000148 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000149 return;
150 }
151
Chris Lattnerc5098a22008-07-14 05:10:41 +0000152 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000153 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000154 return;
155 }
156
157 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000158}
159
160
161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000163// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164//===----------------------------------------------------------------------===//
165
Reid Spencerb31bffe2007-02-26 23:54:03 +0000166ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000167 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000168 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169}
170
Chris Lattnera80bf0b2007-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 Andersonb6b25302009-07-14 23:09:55 +0000184 TheTrueVal = getGlobalContext().getConstantInt(Type::Int1Ty, 1);
185 TheFalseVal = getGlobalContext().getConstantInt(Type::Int1Ty, 0);
Chris Lattnera80bf0b2007-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
193
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000194namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000195 struct DenseMapAPIntKeyInfo {
196 struct KeyTy {
197 APInt val;
198 const Type* type;
199 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
200 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
201 bool operator==(const KeyTy& that) const {
202 return type == that.type && this->val == that.val;
203 }
204 bool operator!=(const KeyTy& that) const {
205 return !this->operator==(that);
206 }
207 };
208 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
209 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000210 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000211 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000212 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000213 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000214 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
215 return LHS == RHS;
216 }
Dale Johannesena719a602007-08-24 00:56:33 +0000217 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000218 };
219}
220
221
Reid Spencerb31bffe2007-02-26 23:54:03 +0000222typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
223 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000224static ManagedStatic<IntMapTy> IntConstants;
225
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000226// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000227// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000228// operator== and operator!= to ensure that the DenseMap doesn't attempt to
229// compare APInt's of different widths, which would violate an APInt class
230// invariant which generates an assertion.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000231ConstantInt *ConstantInt::get(const APInt& V) {
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000232 // Get the corresponding integer type for the bit width of the value.
233 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000234 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000235 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000236
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000237 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000238 ConstantInt *&Slot = (*IntConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000239 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000240
Owen Andersond830eb82009-06-18 19:10:19 +0000241 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000242 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000243 ConstantInt *&NewSlot = (*IntConstants)[Key];
244 if (!Slot) {
245 NewSlot = new ConstantInt(ITy, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000246 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000247
248 return NewSlot;
249 } else {
250 return Slot;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000251 }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000252}
253
254//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000255// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000256//===----------------------------------------------------------------------===//
257
Dale Johannesend246b2c2007-08-30 00:23:21 +0000258ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
259 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000260 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
261 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000262}
263
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000264bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000265 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000266}
267
Dale Johannesend246b2c2007-08-30 00:23:21 +0000268bool ConstantFP::isExactlyValue(const APFloat& V) const {
269 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000270}
271
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000272namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000273 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000274 struct KeyTy {
275 APFloat val;
276 KeyTy(const APFloat& V) : val(V){}
277 KeyTy(const KeyTy& that) : val(that.val) {}
278 bool operator==(const KeyTy& that) const {
279 return this->val.bitwiseIsEqual(that.val);
280 }
281 bool operator!=(const KeyTy& that) const {
282 return !this->operator==(that);
283 }
284 };
285 static inline KeyTy getEmptyKey() {
286 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000287 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000288 static inline KeyTy getTombstoneKey() {
289 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000290 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000291 static unsigned getHashValue(const KeyTy &Key) {
292 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000293 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000294 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
295 return LHS == RHS;
296 }
Dale Johannesena719a602007-08-24 00:56:33 +0000297 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000298 };
299}
300
301//---- ConstantFP::get() implementation...
302//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000303typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000304 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000305
Dale Johannesena719a602007-08-24 00:56:33 +0000306static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000307
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000308ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000309 DenseMapAPFloatKeyInfo::KeyTy Key(V);
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000310
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000311 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000312 ConstantFP *&Slot = (*FPConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000313 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000314
Owen Andersond830eb82009-06-18 19:10:19 +0000315 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000316 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000317 ConstantFP *&NewSlot = (*FPConstants)[Key];
318 if (!NewSlot) {
Owen Andersond830eb82009-06-18 19:10:19 +0000319 const Type *Ty;
320 if (&V.getSemantics() == &APFloat::IEEEsingle)
321 Ty = Type::FloatTy;
322 else if (&V.getSemantics() == &APFloat::IEEEdouble)
323 Ty = Type::DoubleTy;
324 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
325 Ty = Type::X86_FP80Ty;
326 else if (&V.getSemantics() == &APFloat::IEEEquad)
327 Ty = Type::FP128Ty;
328 else {
329 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
330 "Unknown FP format");
331 Ty = Type::PPC_FP128Ty;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000332 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000333 NewSlot = new ConstantFP(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000334 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000335
336 return NewSlot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000337 }
Owen Andersond830eb82009-06-18 19:10:19 +0000338
339 return Slot;
Dale Johannesend246b2c2007-08-30 00:23:21 +0000340}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000341
342//===----------------------------------------------------------------------===//
343// ConstantXXX Classes
344//===----------------------------------------------------------------------===//
345
346
Chris Lattner3462ae32001-12-03 22:26:30 +0000347ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000348 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000349 : Constant(T, ConstantArrayVal,
350 OperandTraits<ConstantArray>::op_end(this) - V.size(),
351 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000352 assert(V.size() == T->getNumElements() &&
353 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000354 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000355 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
356 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000357 Constant *C = *I;
358 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000359 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000360 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000361 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000362 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363 }
364}
365
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000366
Chris Lattner3462ae32001-12-03 22:26:30 +0000367ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000368 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000369 : Constant(T, ConstantStructVal,
370 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
371 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000372 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000373 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000374 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000375 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
376 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000377 Constant *C = *I;
378 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000379 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000380 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000381 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000382 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000383 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000384 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385 }
386}
387
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000388
Reid Spencerd84d35b2007-02-15 02:26:10 +0000389ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000390 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000391 : Constant(T, ConstantVectorVal,
392 OperandTraits<ConstantVector>::op_end(this) - V.size(),
393 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000394 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000395 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
396 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000397 Constant *C = *I;
398 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000399 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000400 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000401 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000402 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000403 }
404}
405
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000406
Gabor Greiff6caff662008-05-10 08:32:32 +0000407namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000408// We declare several classes private to this file, so use an anonymous
409// namespace
410namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000411
Gordon Henriksen14a55692007-12-10 02:14:30 +0000412/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
413/// behind the scenes to implement unary constant exprs.
414class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000415 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000416public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000417 // allocate space for exactly one operand
418 void *operator new(size_t s) {
419 return User::operator new(s, 1);
420 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000421 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000422 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
423 Op<0>() = C;
424 }
425 /// Transparently provide more efficient getOperand methods.
426 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000427};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000428
Gordon Henriksen14a55692007-12-10 02:14:30 +0000429/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
430/// behind the scenes to implement binary constant exprs.
431class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000432 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000433public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000434 // allocate space for exactly two operands
435 void *operator new(size_t s) {
436 return User::operator new(s, 2);
437 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000438 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000439 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000440 Op<0>() = C1;
441 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000442 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000443 /// Transparently provide more efficient getOperand methods.
444 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000445};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000446
Gordon Henriksen14a55692007-12-10 02:14:30 +0000447/// SelectConstantExpr - This class is private to Constants.cpp, and is used
448/// behind the scenes to implement select constant exprs.
449class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000450 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000451public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000452 // allocate space for exactly three operands
453 void *operator new(size_t s) {
454 return User::operator new(s, 3);
455 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000456 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000457 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000458 Op<0>() = C1;
459 Op<1>() = C2;
460 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000461 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000462 /// Transparently provide more efficient getOperand methods.
463 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000464};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000465
Gordon Henriksen14a55692007-12-10 02:14:30 +0000466/// ExtractElementConstantExpr - This class is private to
467/// Constants.cpp, and is used behind the scenes to implement
468/// extractelement constant exprs.
469class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000470 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000471public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000472 // allocate space for exactly two operands
473 void *operator new(size_t s) {
474 return User::operator new(s, 2);
475 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000476 ExtractElementConstantExpr(Constant *C1, Constant *C2)
477 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000478 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000479 Op<0>() = C1;
480 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000481 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000482 /// Transparently provide more efficient getOperand methods.
483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000484};
Robert Bocchino23004482006-01-10 19:05:34 +0000485
Gordon Henriksen14a55692007-12-10 02:14:30 +0000486/// InsertElementConstantExpr - This class is private to
487/// Constants.cpp, and is used behind the scenes to implement
488/// insertelement constant exprs.
489class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000490 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000491public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000492 // allocate space for exactly three operands
493 void *operator new(size_t s) {
494 return User::operator new(s, 3);
495 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000496 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
497 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000498 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000499 Op<0>() = C1;
500 Op<1>() = C2;
501 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000502 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000503 /// Transparently provide more efficient getOperand methods.
504 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000505};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000506
Gordon Henriksen14a55692007-12-10 02:14:30 +0000507/// ShuffleVectorConstantExpr - This class is private to
508/// Constants.cpp, and is used behind the scenes to implement
509/// shufflevector constant exprs.
510class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000511 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000512public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000513 // allocate space for exactly three operands
514 void *operator new(size_t s) {
515 return User::operator new(s, 3);
516 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000517 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000518 : ConstantExpr(VectorType::get(
519 cast<VectorType>(C1->getType())->getElementType(),
520 cast<VectorType>(C3->getType())->getNumElements()),
521 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000522 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000523 Op<0>() = C1;
524 Op<1>() = C2;
525 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000526 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000527 /// Transparently provide more efficient getOperand methods.
528 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000529};
530
Dan Gohman12fce772008-05-15 19:50:34 +0000531/// ExtractValueConstantExpr - This class is private to
532/// Constants.cpp, and is used behind the scenes to implement
533/// extractvalue constant exprs.
534class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000535 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000536public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000537 // allocate space for exactly one operand
538 void *operator new(size_t s) {
539 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000540 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000541 ExtractValueConstantExpr(Constant *Agg,
542 const SmallVector<unsigned, 4> &IdxList,
543 const Type *DestTy)
544 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
545 Indices(IdxList) {
546 Op<0>() = Agg;
547 }
548
Dan Gohman7bb04502008-05-31 19:09:08 +0000549 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000550 const SmallVector<unsigned, 4> Indices;
551
Dan Gohman12fce772008-05-15 19:50:34 +0000552 /// Transparently provide more efficient getOperand methods.
553 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
554};
555
556/// InsertValueConstantExpr - This class is private to
557/// Constants.cpp, and is used behind the scenes to implement
558/// insertvalue constant exprs.
559class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000560 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000561public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000562 // allocate space for exactly one operand
563 void *operator new(size_t s) {
564 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000565 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000566 InsertValueConstantExpr(Constant *Agg, Constant *Val,
567 const SmallVector<unsigned, 4> &IdxList,
568 const Type *DestTy)
569 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
570 Indices(IdxList) {
571 Op<0>() = Agg;
572 Op<1>() = Val;
573 }
574
Dan Gohman7bb04502008-05-31 19:09:08 +0000575 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000576 const SmallVector<unsigned, 4> Indices;
577
Dan Gohman12fce772008-05-15 19:50:34 +0000578 /// Transparently provide more efficient getOperand methods.
579 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
580};
581
582
Gordon Henriksen14a55692007-12-10 02:14:30 +0000583/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
584/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000585class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000586 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000587 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000588public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000589 static GetElementPtrConstantExpr *Create(Constant *C,
590 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000591 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000592 return new(IdxList.size() + 1)
593 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000594 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000595 /// Transparently provide more efficient getOperand methods.
596 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000597};
598
599// CompareConstantExpr - This class is private to Constants.cpp, and is used
600// behind the scenes to implement ICmp and FCmp constant expressions. This is
601// needed in order to store the predicate value for these instructions.
602struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000603 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
604 // allocate space for exactly two operands
605 void *operator new(size_t s) {
606 return User::operator new(s, 2);
607 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000608 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000609 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
610 unsigned short pred, Constant* LHS, Constant* RHS)
611 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000612 Op<0>() = LHS;
613 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000614 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000615 /// Transparently provide more efficient getOperand methods.
616 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000617};
618
619} // end anonymous namespace
620
Gabor Greiff6caff662008-05-10 08:32:32 +0000621template <>
622struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
623};
624DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
625
626template <>
627struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
628};
629DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
630
631template <>
632struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
633};
634DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
635
636template <>
637struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
638};
639DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
640
641template <>
642struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
643};
644DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
645
646template <>
647struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
648};
649DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
650
Dan Gohman12fce772008-05-15 19:50:34 +0000651template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000652struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000653};
Dan Gohman12fce772008-05-15 19:50:34 +0000654DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
655
656template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000657struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000658};
Dan Gohman12fce772008-05-15 19:50:34 +0000659DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
660
Gabor Greiff6caff662008-05-10 08:32:32 +0000661template <>
662struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
663};
664
665GetElementPtrConstantExpr::GetElementPtrConstantExpr
666 (Constant *C,
667 const std::vector<Constant*> &IdxList,
668 const Type *DestTy)
669 : ConstantExpr(DestTy, Instruction::GetElementPtr,
670 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
671 - (IdxList.size()+1),
672 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000673 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000674 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000675 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000676}
677
678DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
679
680
681template <>
682struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
683};
684DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
685
686
687} // End llvm namespace
688
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000689
690// Utility function for determining if a ConstantExpr is a CastOp or not. This
691// can't be inline because we don't want to #include Instruction.h into
692// Constant.h
693bool ConstantExpr::isCast() const {
694 return Instruction::isCast(getOpcode());
695}
696
Reid Spenceree3c9912006-12-04 05:19:50 +0000697bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000698 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000699}
700
Dan Gohman1ecaf452008-05-31 00:58:22 +0000701bool ConstantExpr::hasIndices() const {
702 return getOpcode() == Instruction::ExtractValue ||
703 getOpcode() == Instruction::InsertValue;
704}
705
706const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
707 if (const ExtractValueConstantExpr *EVCE =
708 dyn_cast<ExtractValueConstantExpr>(this))
709 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000710
711 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000712}
713
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000714unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000715 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000716 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000717 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000718}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000719
Chris Lattner7c1018a2006-07-14 19:37:40 +0000720/// getWithOperandReplaced - Return a constant expression identical to this
721/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000722Constant *
723ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000724 assert(OpNo < getNumOperands() && "Operand num is out of range!");
725 assert(Op->getType() == getOperand(OpNo)->getType() &&
726 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000727 if (getOperand(OpNo) == Op)
728 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000729
Chris Lattner227816342006-07-14 22:20:01 +0000730 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000731 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000732 case Instruction::Trunc:
733 case Instruction::ZExt:
734 case Instruction::SExt:
735 case Instruction::FPTrunc:
736 case Instruction::FPExt:
737 case Instruction::UIToFP:
738 case Instruction::SIToFP:
739 case Instruction::FPToUI:
740 case Instruction::FPToSI:
741 case Instruction::PtrToInt:
742 case Instruction::IntToPtr:
743 case Instruction::BitCast:
744 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000745 case Instruction::Select:
746 Op0 = (OpNo == 0) ? Op : getOperand(0);
747 Op1 = (OpNo == 1) ? Op : getOperand(1);
748 Op2 = (OpNo == 2) ? Op : getOperand(2);
749 return ConstantExpr::getSelect(Op0, Op1, Op2);
750 case Instruction::InsertElement:
751 Op0 = (OpNo == 0) ? Op : getOperand(0);
752 Op1 = (OpNo == 1) ? Op : getOperand(1);
753 Op2 = (OpNo == 2) ? Op : getOperand(2);
754 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
755 case Instruction::ExtractElement:
756 Op0 = (OpNo == 0) ? Op : getOperand(0);
757 Op1 = (OpNo == 1) ? Op : getOperand(1);
758 return ConstantExpr::getExtractElement(Op0, Op1);
759 case Instruction::ShuffleVector:
760 Op0 = (OpNo == 0) ? Op : getOperand(0);
761 Op1 = (OpNo == 1) ? Op : getOperand(1);
762 Op2 = (OpNo == 2) ? Op : getOperand(2);
763 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000764 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000765 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000766 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000767 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000768 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000769 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000770 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000771 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000772 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000773 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000774 default:
775 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000776 Op0 = (OpNo == 0) ? Op : getOperand(0);
777 Op1 = (OpNo == 1) ? Op : getOperand(1);
778 return ConstantExpr::get(getOpcode(), Op0, Op1);
779 }
780}
781
782/// getWithOperands - This returns the current constant expression with the
783/// operands replaced with the specified values. The specified operands must
784/// match count and type with the existing ones.
785Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000786getWithOperands(Constant* const *Ops, unsigned NumOps) const {
787 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000788 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000789 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000790 assert(Ops[i]->getType() == getOperand(i)->getType() &&
791 "Operand type mismatch!");
792 AnyChange |= Ops[i] != getOperand(i);
793 }
794 if (!AnyChange) // No operands changed, return self.
795 return const_cast<ConstantExpr*>(this);
796
797 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000798 case Instruction::Trunc:
799 case Instruction::ZExt:
800 case Instruction::SExt:
801 case Instruction::FPTrunc:
802 case Instruction::FPExt:
803 case Instruction::UIToFP:
804 case Instruction::SIToFP:
805 case Instruction::FPToUI:
806 case Instruction::FPToSI:
807 case Instruction::PtrToInt:
808 case Instruction::IntToPtr:
809 case Instruction::BitCast:
810 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000811 case Instruction::Select:
812 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
813 case Instruction::InsertElement:
814 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
815 case Instruction::ExtractElement:
816 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
817 case Instruction::ShuffleVector:
818 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000819 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000820 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000821 case Instruction::ICmp:
822 case Instruction::FCmp:
823 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000824 default:
825 assert(getNumOperands() == 2 && "Must be binary operator?");
826 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000827 }
828}
829
Chris Lattner2f7c9632001-06-06 20:29:01 +0000830
831//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000832// isValueValidForType implementations
833
Reid Spencere7334722006-12-19 01:28:19 +0000834bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000835 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000836 if (Ty == Type::Int1Ty)
837 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000838 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000839 return true; // always true, has to fit in largest type
840 uint64_t Max = (1ll << NumBits) - 1;
841 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000842}
843
Reid Spencere0fc4df2006-10-20 07:07:24 +0000844bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000845 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000846 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000847 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000848 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000849 return true; // always true, has to fit in largest type
850 int64_t Min = -(1ll << (NumBits-1));
851 int64_t Max = (1ll << (NumBits-1)) - 1;
852 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000853}
854
Dale Johannesend246b2c2007-08-30 00:23:21 +0000855bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
856 // convert modifies in place, so make a copy.
857 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000858 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000859 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000860 default:
861 return false; // These can't be represented as floating point!
862
Dale Johannesend246b2c2007-08-30 00:23:21 +0000863 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000864 case Type::FloatTyID: {
865 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
866 return true;
867 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
868 return !losesInfo;
869 }
870 case Type::DoubleTyID: {
871 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
872 &Val2.getSemantics() == &APFloat::IEEEdouble)
873 return true;
874 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
875 return !losesInfo;
876 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000877 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000878 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
879 &Val2.getSemantics() == &APFloat::IEEEdouble ||
880 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000881 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000882 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
883 &Val2.getSemantics() == &APFloat::IEEEdouble ||
884 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000885 case Type::PPC_FP128TyID:
886 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
887 &Val2.getSemantics() == &APFloat::IEEEdouble ||
888 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000889 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000890}
Chris Lattner9655e542001-07-20 19:16:02 +0000891
Chris Lattner49d855c2001-09-07 16:46:31 +0000892//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000893// Factory Function Implementation
894
Gabor Greiff6caff662008-05-10 08:32:32 +0000895
896// The number of operands for each ConstantCreator::create method is
897// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000898// ConstantCreator - A class that is used to create constants by
899// ValueMap*. This class should be partially specialized if there is
900// something strange that needs to be done to interface to the ctor for the
901// constant.
902//
Chris Lattner189d19f2003-11-21 20:23:48 +0000903namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000904 template<class ValType>
905 struct ConstantTraits;
906
907 template<typename T, typename Alloc>
908 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
909 static unsigned uses(const std::vector<T, Alloc>& v) {
910 return v.size();
911 }
912 };
913
Chris Lattner189d19f2003-11-21 20:23:48 +0000914 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000915 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000916 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000917 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000918 }
919 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000920
Chris Lattner189d19f2003-11-21 20:23:48 +0000921 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000922 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000923 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000924 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000925 }
926 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000927
Chris Lattner935aa922005-10-04 17:48:46 +0000928 template<class ValType, class TypeClass, class ConstantClass,
929 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000930 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000931 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000932 typedef std::pair<const Type*, ValType> MapKey;
933 typedef std::map<MapKey, Constant *> MapTy;
934 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
935 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000936 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000937 /// Map - This is the main map from the element descriptor to the Constants.
938 /// This is the primary way we avoid creating two of the same shape
939 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000940 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000941
942 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
943 /// from the constants to their element in Map. This is important for
944 /// removal of constants from the array, which would otherwise have to scan
945 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000946 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000947
Jim Laskeyc03caef2006-07-17 17:38:29 +0000948 /// AbstractTypeMap - Map for abstract type constants.
949 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000950 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000951
952 /// ValueMapLock - Mutex for this map.
953 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000954
Chris Lattner98fa07b2003-05-23 20:03:32 +0000955 public:
Owen Anderson61794042009-06-17 20:10:08 +0000956 // NOTE: This function is not locked. It is the caller's responsibility
957 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000958 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000959
960 /// InsertOrGetItem - Return an iterator for the specified element.
961 /// If the element exists in the map, the returned iterator points to the
962 /// entry and Exists=true. If not, the iterator points to the newly
963 /// inserted entry and returns Exists=false. Newly inserted entries have
964 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000965 /// NOTE: This function is not locked. It is the caller's responsibility
966 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000967 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
968 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000969 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000970 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000971 Exists = !IP.second;
972 return IP.first;
973 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000974
Chris Lattner935aa922005-10-04 17:48:46 +0000975private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000976 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000977 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000978 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000979 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
980 IMI->second->second == CP &&
981 "InverseMap corrupt!");
982 return IMI->second;
983 }
984
Jim Laskeyc03caef2006-07-17 17:38:29 +0000985 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000986 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
987 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000988 if (I == Map.end() || I->second != CP) {
989 // FIXME: This should not use a linear scan. If this gets to be a
990 // performance problem, someone should look at this.
991 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
992 /* empty */;
993 }
Chris Lattner935aa922005-10-04 17:48:46 +0000994 return I;
995 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000996
997 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
998 typename MapTy::iterator I) {
999 ConstantClass* Result =
1000 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1001
1002 assert(Result->getType() == Ty && "Type specified is not correct!");
1003 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1004
1005 if (HasLargeKey) // Remember the reverse mapping if needed.
1006 InverseMap.insert(std::make_pair(Result, I));
1007
1008 // If the type of the constant is abstract, make sure that an entry
1009 // exists for it in the AbstractTypeMap.
1010 if (Ty->isAbstract()) {
1011 typename AbstractTypeMapTy::iterator TI =
1012 AbstractTypeMap.find(Ty);
1013
1014 if (TI == AbstractTypeMap.end()) {
1015 // Add ourselves to the ATU list of the type.
1016 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1017
1018 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1019 }
1020 }
1021
1022 return Result;
1023 }
Chris Lattner935aa922005-10-04 17:48:46 +00001024public:
1025
Chris Lattnerb64419a2005-10-03 22:51:37 +00001026 /// getOrCreate - Return the specified constant from the map, creating it if
1027 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001028 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001029 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +00001030 MapKey Lookup(Ty, V);
1031 ConstantClass* Result = 0;
1032
1033 typename MapTy::iterator I = Map.find(Lookup);
1034 // Is it in the map?
1035 if (I != Map.end())
1036 Result = static_cast<ConstantClass *>(I->second);
1037
1038 if (!Result) {
1039 // If no preexisting value, create one now...
1040 Result = Create(Ty, V, I);
1041 }
1042
1043 return Result;
1044 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001045
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001046 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001047 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001048 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001049 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001050 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001051
Chris Lattner935aa922005-10-04 17:48:46 +00001052 if (HasLargeKey) // Remember the reverse mapping if needed.
1053 InverseMap.erase(CP);
1054
Chris Lattnerb50d1352003-10-05 00:17:43 +00001055 // Now that we found the entry, make sure this isn't the entry that
1056 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001057 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001058 if (Ty->isAbstract()) {
1059 assert(AbstractTypeMap.count(Ty) &&
1060 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001061 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001062 if (ATMEntryIt == I) {
1063 // Yes, we are removing the representative entry for this type.
1064 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001065 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001066
Chris Lattnerb50d1352003-10-05 00:17:43 +00001067 // First check the entry before this one...
1068 if (TmpIt != Map.begin()) {
1069 --TmpIt;
1070 if (TmpIt->first.first != Ty) // Not the same type, move back...
1071 ++TmpIt;
1072 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001073
Chris Lattnerb50d1352003-10-05 00:17:43 +00001074 // If we didn't find the same type, try to move forward...
1075 if (TmpIt == ATMEntryIt) {
1076 ++TmpIt;
1077 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1078 --TmpIt; // No entry afterwards with the same type
1079 }
1080
1081 // If there is another entry in the map of the same abstract type,
1082 // update the AbstractTypeMap entry now.
1083 if (TmpIt != ATMEntryIt) {
1084 ATMEntryIt = TmpIt;
1085 } else {
1086 // Otherwise, we are removing the last instance of this type
1087 // from the table. Remove from the ATM, and from user list.
1088 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1089 AbstractTypeMap.erase(Ty);
1090 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001091 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001092 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001093
Chris Lattnerb50d1352003-10-05 00:17:43 +00001094 Map.erase(I);
1095 }
1096
Chris Lattner3b793c62005-10-04 21:35:50 +00001097
1098 /// MoveConstantToNewSlot - If we are about to change C to be the element
1099 /// specified by I, update our internal data structures to reflect this
1100 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +00001101 /// NOTE: This function is not locked. It is the responsibility of the
1102 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001103 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001104 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001105 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001106 assert(OldI != Map.end() && "Constant not found in constant table!");
1107 assert(OldI->second == C && "Didn't find correct element?");
1108
1109 // If this constant is the representative element for its abstract type,
1110 // update the AbstractTypeMap so that the representative element is I.
1111 if (C->getType()->isAbstract()) {
1112 typename AbstractTypeMapTy::iterator ATI =
1113 AbstractTypeMap.find(C->getType());
1114 assert(ATI != AbstractTypeMap.end() &&
1115 "Abstract type not in AbstractTypeMap?");
1116 if (ATI->second == OldI)
1117 ATI->second = I;
1118 }
1119
1120 // Remove the old entry from the map.
1121 Map.erase(OldI);
1122
1123 // Update the inverse map so that we know that this constant is now
1124 // located at descriptor I.
1125 if (HasLargeKey) {
1126 assert(I->second == C && "Bad inversemap entry!");
1127 InverseMap[C] = I;
1128 }
1129 }
1130
Chris Lattnerb50d1352003-10-05 00:17:43 +00001131 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001132 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001133 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001134 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001135
1136 assert(I != AbstractTypeMap.end() &&
1137 "Abstract type not in AbstractTypeMap?");
1138
1139 // Convert a constant at a time until the last one is gone. The last one
1140 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1141 // eliminated eventually.
1142 do {
1143 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001144 TypeClass>::convert(
1145 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001146 cast<TypeClass>(NewTy));
1147
Jim Laskeyc03caef2006-07-17 17:38:29 +00001148 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001149 } while (I != AbstractTypeMap.end());
1150 }
1151
1152 // If the type became concrete without being refined to any other existing
1153 // type, we just remove ourselves from the ATU list.
1154 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001155 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001156 }
1157
1158 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001159 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001160 }
1161 };
1162}
1163
Chris Lattnera84df0a22006-09-28 23:36:21 +00001164
Chris Lattner28173502007-02-20 06:11:36 +00001165
Chris Lattner9fba3da2004-02-15 05:53:04 +00001166//---- ConstantAggregateZero::get() implementation...
1167//
1168namespace llvm {
1169 // ConstantAggregateZero does not take extra "value" argument...
1170 template<class ValType>
1171 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1172 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1173 return new ConstantAggregateZero(Ty);
1174 }
1175 };
1176
1177 template<>
1178 struct ConvertConstantType<ConstantAggregateZero, Type> {
1179 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1180 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001181 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001182 assert(New != OldC && "Didn't replace constant??");
1183 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001184 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001185 }
1186 };
1187}
1188
Chris Lattner69edc982006-09-28 00:35:06 +00001189static ManagedStatic<ValueMap<char, Type,
1190 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001191
Chris Lattner3e650af2004-08-04 04:48:01 +00001192static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1193
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001194ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001195 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001196 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001197
1198 // Implicitly locked.
1199 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001200}
1201
Dan Gohman92b551b2009-03-03 02:55:14 +00001202/// destroyConstant - Remove the constant from the constant table...
1203///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001204void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001205 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001206 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001207 destroyConstantImpl();
1208}
1209
Chris Lattner3462ae32001-12-03 22:26:30 +00001210//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001211//
Chris Lattner189d19f2003-11-21 20:23:48 +00001212namespace llvm {
1213 template<>
1214 struct ConvertConstantType<ConstantArray, ArrayType> {
1215 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1216 // Make everyone now use a constant of the new type...
1217 std::vector<Constant*> C;
1218 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1219 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001220 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001221 assert(New != OldC && "Didn't replace constant??");
1222 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001223 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001224 }
1225 };
1226}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001227
Chris Lattner3e650af2004-08-04 04:48:01 +00001228static std::vector<Constant*> getValType(ConstantArray *CA) {
1229 std::vector<Constant*> Elements;
1230 Elements.reserve(CA->getNumOperands());
1231 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1232 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1233 return Elements;
1234}
1235
Chris Lattnerb64419a2005-10-03 22:51:37 +00001236typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001237 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001238static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001239
Chris Lattner015e8212004-02-15 04:14:47 +00001240Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001241 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001242 // If this is an all-zero array, return a ConstantAggregateZero object
1243 if (!V.empty()) {
1244 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001245 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001246 // Implicitly locked.
1247 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001248 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001249 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001250 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001251 // Implicitly locked.
1252 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001253 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001254 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001255
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001256 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001257}
1258
Dan Gohman92b551b2009-03-03 02:55:14 +00001259/// destroyConstant - Remove the constant from the constant table...
1260///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001261void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001262 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001263 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001264 destroyConstantImpl();
1265}
1266
Reid Spencer2546b762007-01-26 07:37:34 +00001267/// isString - This method returns true if the array is an array of i8, and
1268/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001269bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001270 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001271 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001272 return false;
1273 // Check the elements to make sure they are all integers, not constant
1274 // expressions.
1275 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1276 if (!isa<ConstantInt>(getOperand(i)))
1277 return false;
1278 return true;
1279}
1280
Evan Cheng3763c5b2006-10-26 19:15:05 +00001281/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001282/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001283/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001284bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001285 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001286 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001287 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001288
Evan Chenge974da62006-10-26 21:48:03 +00001289 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001290 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001291 return false;
1292 // Other elements must be non-null integers.
1293 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1294 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001295 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001296 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001297 return false;
1298 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001299 return true;
1300}
1301
1302
Dan Gohman92b551b2009-03-03 02:55:14 +00001303/// getAsString - If the sub-element type of this array is i8
1304/// then this method converts the array to an std::string and returns it.
1305/// Otherwise, it asserts out.
1306///
Chris Lattner81fabb02002-08-26 17:53:56 +00001307std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001308 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001309 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001310 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001311 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001312 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001313 return Result;
1314}
1315
1316
Chris Lattner3462ae32001-12-03 22:26:30 +00001317//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001318//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001319
Chris Lattner189d19f2003-11-21 20:23:48 +00001320namespace llvm {
1321 template<>
1322 struct ConvertConstantType<ConstantStruct, StructType> {
1323 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1324 // Make everyone now use a constant of the new type...
1325 std::vector<Constant*> C;
1326 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1327 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001328 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001329 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001330
Chris Lattner189d19f2003-11-21 20:23:48 +00001331 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001332 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001333 }
1334 };
1335}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001336
Chris Lattner8760ec72005-10-04 01:17:50 +00001337typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001338 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001339static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001340
Chris Lattner3e650af2004-08-04 04:48:01 +00001341static std::vector<Constant*> getValType(ConstantStruct *CS) {
1342 std::vector<Constant*> Elements;
1343 Elements.reserve(CS->getNumOperands());
1344 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1345 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1346 return Elements;
1347}
1348
Chris Lattner015e8212004-02-15 04:14:47 +00001349Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001350 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001351 // Create a ConstantAggregateZero value if all elements are zeros...
1352 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001353 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001354 // Implicitly locked.
1355 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001356
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001357 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001358}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001359
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001360Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001361 std::vector<const Type*> StructEls;
1362 StructEls.reserve(V.size());
1363 for (unsigned i = 0, e = V.size(); i != e; ++i)
1364 StructEls.push_back(V[i]->getType());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001365 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001366}
1367
Chris Lattnerd7a73302001-10-13 06:57:33 +00001368// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001369//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001370void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001371 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001372 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001373 destroyConstantImpl();
1374}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001375
Reid Spencerd84d35b2007-02-15 02:26:10 +00001376//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001377//
1378namespace llvm {
1379 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001380 struct ConvertConstantType<ConstantVector, VectorType> {
1381 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001382 // Make everyone now use a constant of the new type...
1383 std::vector<Constant*> C;
1384 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1385 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001386 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001387 assert(New != OldC && "Didn't replace constant??");
1388 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001389 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001390 }
1391 };
1392}
1393
Reid Spencerd84d35b2007-02-15 02:26:10 +00001394static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001395 std::vector<Constant*> Elements;
1396 Elements.reserve(CP->getNumOperands());
1397 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1398 Elements.push_back(CP->getOperand(i));
1399 return Elements;
1400}
1401
Reid Spencerd84d35b2007-02-15 02:26:10 +00001402static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001403 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001404
Reid Spencerd84d35b2007-02-15 02:26:10 +00001405Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001406 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001407 assert(!V.empty() && "Vectors can't be empty");
1408 // If this is an all-undef or alll-zero vector, return a
1409 // ConstantAggregateZero or UndefValue.
1410 Constant *C = V[0];
1411 bool isZero = C->isNullValue();
1412 bool isUndef = isa<UndefValue>(C);
1413
1414 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001415 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001416 if (V[i] != C) {
1417 isZero = isUndef = false;
1418 break;
1419 }
Brian Gaeke02209042004-08-20 06:00:58 +00001420 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001421
1422 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001423 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001424 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001425 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001426
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001427 // Implicitly locked.
1428 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001429}
1430
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001431Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001432 assert(!V.empty() && "Cannot infer type if V is empty");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001433 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001434}
1435
1436// destroyConstant - Remove the constant from the constant table...
1437//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001438void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001439 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001440 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001441 destroyConstantImpl();
1442}
1443
Dan Gohman30978072007-05-24 14:36:04 +00001444/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001445/// is set to all ones.
1446/// @returns true iff this constant's emements are all set to all ones.
1447/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001448bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001449 // Check out first element.
1450 const Constant *Elt = getOperand(0);
1451 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1452 if (!CI || !CI->isAllOnesValue()) return false;
1453 // Then make sure all remaining elements point to the same value.
1454 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1455 if (getOperand(I) != Elt) return false;
1456 }
1457 return true;
1458}
1459
Dan Gohman07159202007-10-17 17:51:30 +00001460/// getSplatValue - If this is a splat constant, where all of the
1461/// elements have the same value, return that value. Otherwise return null.
1462Constant *ConstantVector::getSplatValue() {
1463 // Check out first element.
1464 Constant *Elt = getOperand(0);
1465 // Then make sure all remaining elements point to the same value.
1466 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1467 if (getOperand(I) != Elt) return 0;
1468 return Elt;
1469}
1470
Chris Lattner3462ae32001-12-03 22:26:30 +00001471//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001472//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001473
Chris Lattner189d19f2003-11-21 20:23:48 +00001474namespace llvm {
1475 // ConstantPointerNull does not take extra "value" argument...
1476 template<class ValType>
1477 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1478 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1479 return new ConstantPointerNull(Ty);
1480 }
1481 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001482
Chris Lattner189d19f2003-11-21 20:23:48 +00001483 template<>
1484 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1485 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1486 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001487 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001488 assert(New != OldC && "Didn't replace constant??");
1489 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001490 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001491 }
1492 };
1493}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001494
Chris Lattner69edc982006-09-28 00:35:06 +00001495static ManagedStatic<ValueMap<char, PointerType,
1496 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001497
Chris Lattner3e650af2004-08-04 04:48:01 +00001498static char getValType(ConstantPointerNull *) {
1499 return 0;
1500}
1501
1502
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001503ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001504 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001505 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001506}
1507
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001508// destroyConstant - Remove the constant from the constant table...
1509//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001510void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001511 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001512 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001513 destroyConstantImpl();
1514}
1515
1516
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001517//---- UndefValue::get() implementation...
1518//
1519
1520namespace llvm {
1521 // UndefValue does not take extra "value" argument...
1522 template<class ValType>
1523 struct ConstantCreator<UndefValue, Type, ValType> {
1524 static UndefValue *create(const Type *Ty, const ValType &V) {
1525 return new UndefValue(Ty);
1526 }
1527 };
1528
1529 template<>
1530 struct ConvertConstantType<UndefValue, Type> {
1531 static void convert(UndefValue *OldC, const Type *NewTy) {
1532 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001533 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001534 assert(New != OldC && "Didn't replace constant??");
1535 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001536 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001537 }
1538 };
1539}
1540
Chris Lattner69edc982006-09-28 00:35:06 +00001541static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001542
1543static char getValType(UndefValue *) {
1544 return 0;
1545}
1546
1547
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001548UndefValue *UndefValue::get(const Type *Ty) {
1549 // Implicitly locked.
1550 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001551}
1552
1553// destroyConstant - Remove the constant from the constant table.
1554//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001555void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001556 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001557 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001558 destroyConstantImpl();
1559}
1560
Nick Lewycky49f89192009-04-04 07:22:01 +00001561//---- MDString::get() implementation
1562//
1563
1564MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001565 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001566 StrBegin(begin), StrEnd(end) {}
1567
1568static ManagedStatic<StringMap<MDString*> > MDStringCache;
1569
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001570MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001571 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001572 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1573 StrBegin, StrEnd);
1574 MDString *&S = Entry.getValue();
1575 if (!S) S = new MDString(Entry.getKeyData(),
1576 Entry.getKeyData() + Entry.getKeyLength());
Owen Anderson65c5cd72009-06-17 20:34:43 +00001577
Owen Andersond830eb82009-06-18 19:10:19 +00001578 return S;
Nick Lewycky49f89192009-04-04 07:22:01 +00001579}
1580
Devang Patel4c563162009-06-24 22:42:39 +00001581MDString *MDString::get(const std::string &Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001582 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Devang Patel4c563162009-06-24 22:42:39 +00001583 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1584 Str.data(), Str.data() + Str.size());
1585 MDString *&S = Entry.getValue();
1586 if (!S) S = new MDString(Entry.getKeyData(),
1587 Entry.getKeyData() + Entry.getKeyLength());
1588
1589 return S;
1590}
1591
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001592void MDString::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001593 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001594 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
Nick Lewycky49f89192009-04-04 07:22:01 +00001595 destroyConstantImpl();
1596}
1597
1598//---- MDNode::get() implementation
1599//
1600
1601static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1602
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001603MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001604 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001605 for (unsigned i = 0; i != NumVals; ++i)
1606 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001607}
1608
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001609void MDNode::Profile(FoldingSetNodeID &ID) const {
1610 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001611 ID.AddPointer(*I);
1612}
1613
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001614MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001615 FoldingSetNodeID ID;
1616 for (unsigned i = 0; i != NumVals; ++i)
1617 ID.AddPointer(Vals[i]);
1618
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001619 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001620 void *InsertPoint;
1621 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001622 ConstantsLock->reader_release();
Owen Andersond830eb82009-06-18 19:10:19 +00001623
1624 if (!N) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001625 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001626 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1627 if (!N) {
Owen Andersond830eb82009-06-18 19:10:19 +00001628 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1629 N = new(0) MDNode(Vals, NumVals);
1630 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001631 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001632 }
Owen Andersond830eb82009-06-18 19:10:19 +00001633 return N;
Nick Lewycky49f89192009-04-04 07:22:01 +00001634}
1635
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001636void MDNode::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001637 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001638 MDNodeSet->RemoveNode(this);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001639
Nick Lewycky49f89192009-04-04 07:22:01 +00001640 destroyConstantImpl();
1641}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001642
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001643//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001644//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001645
Dan Gohmand78c4002008-05-13 00:00:25 +00001646namespace {
1647
Reid Spenceree3c9912006-12-04 05:19:50 +00001648struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001649 typedef SmallVector<unsigned, 4> IndexList;
1650
1651 ExprMapKeyType(unsigned opc,
1652 const std::vector<Constant*> &ops,
1653 unsigned short pred = 0,
1654 const IndexList &inds = IndexList())
1655 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001656 uint16_t opcode;
1657 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001658 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001659 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001660 bool operator==(const ExprMapKeyType& that) const {
1661 return this->opcode == that.opcode &&
1662 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001663 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001664 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001665 }
1666 bool operator<(const ExprMapKeyType & that) const {
1667 return this->opcode < that.opcode ||
1668 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1669 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001670 this->operands < that.operands) ||
1671 (this->opcode == that.opcode && this->predicate == that.predicate &&
1672 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001673 }
1674
1675 bool operator!=(const ExprMapKeyType& that) const {
1676 return !(*this == that);
1677 }
1678};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001679
Dan Gohmand78c4002008-05-13 00:00:25 +00001680}
1681
Chris Lattner189d19f2003-11-21 20:23:48 +00001682namespace llvm {
1683 template<>
1684 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001685 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1686 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001687 if (Instruction::isCast(V.opcode))
1688 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1689 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001690 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001691 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1692 if (V.opcode == Instruction::Select)
1693 return new SelectConstantExpr(V.operands[0], V.operands[1],
1694 V.operands[2]);
1695 if (V.opcode == Instruction::ExtractElement)
1696 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1697 if (V.opcode == Instruction::InsertElement)
1698 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1699 V.operands[2]);
1700 if (V.opcode == Instruction::ShuffleVector)
1701 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1702 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001703 if (V.opcode == Instruction::InsertValue)
1704 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1705 V.indices, Ty);
1706 if (V.opcode == Instruction::ExtractValue)
1707 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001708 if (V.opcode == Instruction::GetElementPtr) {
1709 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001710 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001711 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001712
Reid Spenceree3c9912006-12-04 05:19:50 +00001713 // The compare instructions are weird. We have to encode the predicate
1714 // value and it is combined with the instruction opcode by multiplying
1715 // the opcode by one hundred. We must decode this to get the predicate.
1716 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001717 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001718 V.operands[0], V.operands[1]);
1719 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001720 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1721 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001722 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001723 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001724 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001725 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001726
Chris Lattner189d19f2003-11-21 20:23:48 +00001727 template<>
1728 struct ConvertConstantType<ConstantExpr, Type> {
1729 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1730 Constant *New;
1731 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001732 case Instruction::Trunc:
1733 case Instruction::ZExt:
1734 case Instruction::SExt:
1735 case Instruction::FPTrunc:
1736 case Instruction::FPExt:
1737 case Instruction::UIToFP:
1738 case Instruction::SIToFP:
1739 case Instruction::FPToUI:
1740 case Instruction::FPToSI:
1741 case Instruction::PtrToInt:
1742 case Instruction::IntToPtr:
1743 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001744 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001745 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001746 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001747 case Instruction::Select:
1748 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1749 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001750 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001751 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001752 default:
1753 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001754 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001755 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001756 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001757 break;
1758 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001759 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001760 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001761 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001762 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001763 break;
1764 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001765
Chris Lattner189d19f2003-11-21 20:23:48 +00001766 assert(New != OldC && "Didn't replace constant??");
1767 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001768 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001769 }
1770 };
1771} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001772
1773
Chris Lattner3e650af2004-08-04 04:48:01 +00001774static ExprMapKeyType getValType(ConstantExpr *CE) {
1775 std::vector<Constant*> Operands;
1776 Operands.reserve(CE->getNumOperands());
1777 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1778 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001779 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001780 CE->isCompare() ? CE->getPredicate() : 0,
1781 CE->hasIndices() ?
1782 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001783}
1784
Chris Lattner69edc982006-09-28 00:35:06 +00001785static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1786 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001787
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001788/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001789/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001790static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001791 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001792 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001794 if (Constant *FC =
1795 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001796 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001797
Vikram S. Adve4c485332002-07-15 18:19:33 +00001798 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001799 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001800 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001801
Owen Anderson61794042009-06-17 20:10:08 +00001802 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001803 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001804}
Reid Spencerf37dc652006-12-05 19:14:13 +00001805
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001806Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001807 Instruction::CastOps opc = Instruction::CastOps(oc);
1808 assert(Instruction::isCast(opc) && "opcode out of range");
1809 assert(C && Ty && "Null arguments to getCast");
1810 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1811
1812 switch (opc) {
1813 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001814 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001815 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001816 case Instruction::Trunc: return getTrunc(C, Ty);
1817 case Instruction::ZExt: return getZExt(C, Ty);
1818 case Instruction::SExt: return getSExt(C, Ty);
1819 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1820 case Instruction::FPExt: return getFPExtend(C, Ty);
1821 case Instruction::UIToFP: return getUIToFP(C, Ty);
1822 case Instruction::SIToFP: return getSIToFP(C, Ty);
1823 case Instruction::FPToUI: return getFPToUI(C, Ty);
1824 case Instruction::FPToSI: return getFPToSI(C, Ty);
1825 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1826 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1827 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001828 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001829 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001830}
1831
Reid Spencer5c140882006-12-04 20:17:56 +00001832Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001833 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001834 return getCast(Instruction::BitCast, C, Ty);
1835 return getCast(Instruction::ZExt, C, Ty);
1836}
1837
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001838Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001839 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001840 return getCast(Instruction::BitCast, C, Ty);
1841 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001842}
1843
1844Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001845 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001846 return getCast(Instruction::BitCast, C, Ty);
1847 return getCast(Instruction::Trunc, C, Ty);
1848}
1849
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001850Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001851 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001852 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001853
Chris Lattner03c49532007-01-15 02:27:26 +00001854 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001855 return getCast(Instruction::PtrToInt, S, Ty);
1856 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001857}
1858
Reid Spencer56521c42006-12-12 00:51:07 +00001859Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1860 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001861 assert(C->getType()->isIntOrIntVector() &&
1862 Ty->isIntOrIntVector() && "Invalid cast");
1863 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1864 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001865 Instruction::CastOps opcode =
1866 (SrcBits == DstBits ? Instruction::BitCast :
1867 (SrcBits > DstBits ? Instruction::Trunc :
1868 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1869 return getCast(opcode, C, Ty);
1870}
1871
1872Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001873 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001874 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001875 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1876 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001877 if (SrcBits == DstBits)
1878 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001879 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001880 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001881 return getCast(opcode, C, Ty);
1882}
1883
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001884Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001885#ifndef NDEBUG
1886 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1887 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1888#endif
1889 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1890 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1891 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1892 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893 "SrcTy must be larger than DestTy for Trunc!");
1894
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001895 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001896}
1897
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001898Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001899#ifndef NDEBUG
1900 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1901 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1902#endif
1903 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1904 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1905 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1906 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001907 "SrcTy must be smaller than DestTy for SExt!");
1908
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001909 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001910}
1911
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001912Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001913#ifndef NDEBUG
1914 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1915 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1916#endif
1917 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1918 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1919 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1920 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001921 "SrcTy must be smaller than DestTy for ZExt!");
1922
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001923 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924}
1925
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001926Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001927#ifndef NDEBUG
1928 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1929 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1930#endif
1931 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1932 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1933 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001934 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001935 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936}
1937
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001938Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001939#ifndef NDEBUG
1940 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1941 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1942#endif
1943 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1944 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1945 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001947 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948}
1949
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001950Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001951#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001952 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1953 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001954#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001955 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1956 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1957 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001958 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959}
1960
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001961Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001962#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001963 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1964 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001965#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001966 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1967 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001968 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001969 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970}
1971
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001972Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001973#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001974 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1975 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001976#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001977 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1978 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1979 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001980 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981}
1982
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001983Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001984#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001985 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1986 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001987#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001988 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1989 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1990 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001991 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001992}
1993
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001994Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001996 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001997 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998}
1999
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002000Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00002001 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002002 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002003 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004}
2005
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002006Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007 // BitCast implies a no-op cast of type only. No bits change. However, you
2008 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002009#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 const Type *SrcTy = C->getType();
2011 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002012 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013
2014 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2015 // or nonptr->ptr). For all the other types, the cast is okay if source and
2016 // destination bit widths are identical.
2017 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2018 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002019#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002020 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002021
2022 // It is common to ask for a bitcast of a value to its own type, handle this
2023 // speedily.
2024 if (C->getType() == DstTy) return C;
2025
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002026 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002027}
2028
Chris Lattnerb50d1352003-10-05 00:17:43 +00002029Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002030 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002031 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002032 assert(Opcode >= Instruction::BinaryOpsBegin &&
2033 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002034 "Invalid opcode in binary constant expression");
2035 assert(C1->getType() == C2->getType() &&
2036 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002037
Reid Spencer542964f2007-01-11 18:21:29 +00002038 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00002039 if (Constant *FC = ConstantFoldBinaryInstruction(
2040 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00002041 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002042
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002043 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002044 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002045
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002046 // Implicitly locked.
2047 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002048}
2049
Reid Spencer266e42b2006-12-23 06:05:41 +00002050Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002051 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002052 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002053 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002054 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2055 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2056 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2057 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2058 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2059 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002060 return getFCmp(predicate, C1, C2);
2061
Nate Begemanc96e2e42008-07-25 17:35:37 +00002062 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2063 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2064 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2065 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002066 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002067 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002068}
2069
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002070Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002071 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2072 if (C1->getType()->isFPOrFPVector()) {
2073 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2074 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2075 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2076 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002077#ifndef NDEBUG
2078 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002079 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002080 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002081 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002082 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002083 assert(C1->getType()->isIntOrIntVector() &&
2084 "Tried to create an integer operation on a non-integer type!");
2085 break;
2086 case Instruction::FAdd:
2087 case Instruction::FSub:
2088 case Instruction::FMul:
2089 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2090 assert(C1->getType()->isFPOrFPVector() &&
2091 "Tried to create a floating-point operation on a "
2092 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002093 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002094 case Instruction::UDiv:
2095 case Instruction::SDiv:
2096 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002097 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002098 "Tried to create an arithmetic operation on a non-arithmetic type!");
2099 break;
2100 case Instruction::FDiv:
2101 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002102 assert(C1->getType()->isFPOrFPVector() &&
2103 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002104 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002105 case Instruction::URem:
2106 case Instruction::SRem:
2107 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002108 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002109 "Tried to create an arithmetic operation on a non-arithmetic type!");
2110 break;
2111 case Instruction::FRem:
2112 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002113 assert(C1->getType()->isFPOrFPVector() &&
2114 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002115 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002116 case Instruction::And:
2117 case Instruction::Or:
2118 case Instruction::Xor:
2119 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002120 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002121 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002122 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002123 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002124 case Instruction::LShr:
2125 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002126 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002127 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002128 "Tried to create a shift operation on a non-integer type!");
2129 break;
2130 default:
2131 break;
2132 }
2133#endif
2134
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002135 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002136}
2137
Reid Spencer266e42b2006-12-23 06:05:41 +00002138Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002139 Constant *C1, Constant *C2) {
2140 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002141 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002142}
2143
Chris Lattner6e415c02004-03-12 05:54:04 +00002144Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002145 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002146 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002147
2148 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00002149 if (Constant *SC = ConstantFoldSelectInstruction(
2150 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00002151 return SC; // Fold common cases
2152
2153 std::vector<Constant*> argVec(3, C);
2154 argVec[1] = V1;
2155 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002156 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002157
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002158 // Implicitly locked.
2159 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002160}
2161
Chris Lattnerb50d1352003-10-05 00:17:43 +00002162Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002163 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002164 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002165 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2166 Idxs+NumIdx) ==
2167 cast<PointerType>(ReqTy)->getElementType() &&
2168 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002169
Owen Anderson53a52212009-07-13 04:09:18 +00002170 if (Constant *FC = ConstantFoldGetElementPtr(
2171 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002172 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002173
Chris Lattnerb50d1352003-10-05 00:17:43 +00002174 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002175 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002176 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002177 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002178 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002179 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002180 for (unsigned i = 0; i != NumIdx; ++i)
2181 ArgVec.push_back(cast<Constant>(Idxs[i]));
2182 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002183
2184 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002185 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002186}
2187
Chris Lattner302116a2007-01-31 04:40:28 +00002188Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002189 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002190 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002191 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002192 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002193 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002194 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002195 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002196}
2197
Chris Lattner302116a2007-01-31 04:40:28 +00002198Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002199 unsigned NumIdx) {
2200 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002201}
2202
Chris Lattner302116a2007-01-31 04:40:28 +00002203
Reid Spenceree3c9912006-12-04 05:19:50 +00002204Constant *
2205ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2206 assert(LHS->getType() == RHS->getType());
2207 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2208 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2209
Owen Anderson53a52212009-07-13 04:09:18 +00002210 if (Constant *FC = ConstantFoldCompareInstruction(
2211 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002212 return FC; // Fold a few common cases...
2213
2214 // Look up the constant in the table first to ensure uniqueness
2215 std::vector<Constant*> ArgVec;
2216 ArgVec.push_back(LHS);
2217 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002218 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002219 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002220
2221 // Implicitly locked.
2222 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002223}
2224
2225Constant *
2226ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2227 assert(LHS->getType() == RHS->getType());
2228 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2229
Owen Anderson53a52212009-07-13 04:09:18 +00002230 if (Constant *FC = ConstantFoldCompareInstruction(
2231 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002232 return FC; // Fold a few common cases...
2233
2234 // Look up the constant in the table first to ensure uniqueness
2235 std::vector<Constant*> ArgVec;
2236 ArgVec.push_back(LHS);
2237 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002238 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002239 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002240
2241 // Implicitly locked.
2242 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002243}
2244
Robert Bocchino23004482006-01-10 19:05:34 +00002245Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2246 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002247 if (Constant *FC = ConstantFoldExtractElementInstruction(
2248 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002249 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002250 // Look up the constant in the table first to ensure uniqueness
2251 std::vector<Constant*> ArgVec(1, Val);
2252 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002253 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002254
2255 // Implicitly locked.
2256 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002257}
2258
2259Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002260 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002261 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002262 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002263 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002264 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002265 Val, Idx);
2266}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002267
Robert Bocchinoca27f032006-01-17 20:07:22 +00002268Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2269 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002270 if (Constant *FC = ConstantFoldInsertElementInstruction(
2271 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002272 return FC; // Fold a few common cases...
2273 // Look up the constant in the table first to ensure uniqueness
2274 std::vector<Constant*> ArgVec(1, Val);
2275 ArgVec.push_back(Elt);
2276 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002277 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002278
2279 // Implicitly locked.
2280 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002281}
2282
2283Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2284 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002285 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002286 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002287 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002288 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002289 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002290 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002291 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002292}
2293
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002294Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2295 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002296 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2297 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002298 return FC; // Fold a few common cases...
2299 // Look up the constant in the table first to ensure uniqueness
2300 std::vector<Constant*> ArgVec(1, V1);
2301 ArgVec.push_back(V2);
2302 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002303 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002304
2305 // Implicitly locked.
2306 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002307}
2308
2309Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2310 Constant *Mask) {
2311 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2312 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002313
2314 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2315 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2316 const Type *ShufTy = VectorType::get(EltTy, NElts);
2317 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002318}
2319
Dan Gohman12fce772008-05-15 19:50:34 +00002320Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2321 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002322 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002323 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2324 Idxs+NumIdx) == Val->getType() &&
2325 "insertvalue indices invalid!");
2326 assert(Agg->getType() == ReqTy &&
2327 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002328 assert(Agg->getType()->isFirstClassType() &&
2329 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002330 Constant *FC = ConstantFoldInsertValueInstruction(
2331 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002332 assert(FC && "InsertValue constant expr couldn't be folded!");
2333 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002334}
2335
2336Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002337 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002338 assert(Agg->getType()->isFirstClassType() &&
2339 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002340
Dan Gohman0752bff2008-05-23 00:36:11 +00002341 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002342#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002343 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002344 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002345#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002346 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002347 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2348}
2349
2350Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002351 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002352 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2353 Idxs+NumIdx) == ReqTy &&
2354 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002355 assert(Agg->getType()->isFirstClassType() &&
2356 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002357 Constant *FC = ConstantFoldExtractValueInstruction(
2358 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002359 assert(FC && "ExtractValue constant expr couldn't be folded!");
2360 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002361}
2362
2363Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002364 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002365 assert(Agg->getType()->isFirstClassType() &&
2366 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002367
2368 const Type *ReqTy =
2369 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2370 assert(ReqTy && "extractvalue indices invalid!");
2371 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2372}
2373
Vikram S. Adve4c485332002-07-15 18:19:33 +00002374// destroyConstant - Remove the constant from the constant table...
2375//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002376void ConstantExpr::destroyConstant() {
2377 // Implicitly locked.
2378 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002379 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002380}
2381
Chris Lattner3cd8c562002-07-30 18:54:25 +00002382const char *ConstantExpr::getOpcodeName() const {
2383 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002384}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002385
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002386//===----------------------------------------------------------------------===//
2387// replaceUsesOfWithOnConstant implementations
2388
Chris Lattner913849b2007-08-21 00:55:23 +00002389/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2390/// 'From' to be uses of 'To'. This must update the uniquing data structures
2391/// etc.
2392///
2393/// Note that we intentionally replace all uses of From with To here. Consider
2394/// a large array that uses 'From' 1000 times. By handling this case all here,
2395/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2396/// single invocation handles all 1000 uses. Handling them one at a time would
2397/// work, but would be really slow because it would have to unique each updated
2398/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002399void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002400 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002401 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002402 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002403
Jim Laskeyc03caef2006-07-17 17:38:29 +00002404 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002405 Lookup.first.first = getType();
2406 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002407
Chris Lattnerb64419a2005-10-03 22:51:37 +00002408 std::vector<Constant*> &Values = Lookup.first.second;
2409 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002410
Chris Lattner8760ec72005-10-04 01:17:50 +00002411 // Fill values with the modified operands of the constant array. Also,
2412 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002413 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002414 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002415 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002416 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2417 Constant *Val = cast<Constant>(O->get());
2418 if (Val == From) {
2419 Val = ToC;
2420 ++NumUpdated;
2421 }
2422 Values.push_back(Val);
2423 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002424 } else {
2425 isAllZeros = true;
2426 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2427 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002428 if (Val == From) {
2429 Val = ToC;
2430 ++NumUpdated;
2431 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002432 Values.push_back(Val);
2433 if (isAllZeros) isAllZeros = Val->isNullValue();
2434 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002435 }
2436
Chris Lattnerb64419a2005-10-03 22:51:37 +00002437 Constant *Replacement = 0;
2438 if (isAllZeros) {
2439 Replacement = ConstantAggregateZero::get(getType());
2440 } else {
2441 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002442 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002443 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002444 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002445 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002446
2447 if (Exists) {
2448 Replacement = I->second;
2449 } else {
2450 // Okay, the new shape doesn't exist in the system yet. Instead of
2451 // creating a new constant array, inserting it, replaceallusesof'ing the
2452 // old with the new, then deleting the old... just update the current one
2453 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002454 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002455
Chris Lattner913849b2007-08-21 00:55:23 +00002456 // Update to the new value. Optimize for the case when we have a single
2457 // operand that we're changing, but handle bulk updates efficiently.
2458 if (NumUpdated == 1) {
2459 unsigned OperandToUpdate = U-OperandList;
2460 assert(getOperand(OperandToUpdate) == From &&
2461 "ReplaceAllUsesWith broken!");
2462 setOperand(OperandToUpdate, ToC);
2463 } else {
2464 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2465 if (getOperand(i) == From)
2466 setOperand(i, ToC);
2467 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002468 return;
2469 }
2470 }
2471
2472 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002473 assert(Replacement != this && "I didn't contain From!");
2474
Chris Lattner7a1450d2005-10-04 18:13:04 +00002475 // Everyone using this now uses the replacement.
2476 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002477
2478 // Delete the old constant!
2479 destroyConstant();
2480}
2481
2482void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002483 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002484 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002485 Constant *ToC = cast<Constant>(To);
2486
Chris Lattnerdff59112005-10-04 18:47:09 +00002487 unsigned OperandToUpdate = U-OperandList;
2488 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2489
Jim Laskeyc03caef2006-07-17 17:38:29 +00002490 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002491 Lookup.first.first = getType();
2492 Lookup.second = this;
2493 std::vector<Constant*> &Values = Lookup.first.second;
2494 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002495
Chris Lattnerdff59112005-10-04 18:47:09 +00002496
Chris Lattner8760ec72005-10-04 01:17:50 +00002497 // Fill values with the modified operands of the constant struct. Also,
2498 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002499 bool isAllZeros = false;
2500 if (!ToC->isNullValue()) {
2501 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2502 Values.push_back(cast<Constant>(O->get()));
2503 } else {
2504 isAllZeros = true;
2505 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2506 Constant *Val = cast<Constant>(O->get());
2507 Values.push_back(Val);
2508 if (isAllZeros) isAllZeros = Val->isNullValue();
2509 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002510 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002511 Values[OperandToUpdate] = ToC;
2512
Chris Lattner8760ec72005-10-04 01:17:50 +00002513 Constant *Replacement = 0;
2514 if (isAllZeros) {
2515 Replacement = ConstantAggregateZero::get(getType());
2516 } else {
2517 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002518 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002519 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002520 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002521 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002522
2523 if (Exists) {
2524 Replacement = I->second;
2525 } else {
2526 // Okay, the new shape doesn't exist in the system yet. Instead of
2527 // creating a new constant struct, inserting it, replaceallusesof'ing the
2528 // old with the new, then deleting the old... just update the current one
2529 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002530 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002531
Chris Lattnerdff59112005-10-04 18:47:09 +00002532 // Update to the new value.
2533 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002534 return;
2535 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002536 }
2537
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002538 assert(Replacement != this && "I didn't contain From!");
2539
Chris Lattner7a1450d2005-10-04 18:13:04 +00002540 // Everyone using this now uses the replacement.
2541 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002542
2543 // Delete the old constant!
2544 destroyConstant();
2545}
2546
Reid Spencerd84d35b2007-02-15 02:26:10 +00002547void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002548 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002549 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2550
2551 std::vector<Constant*> Values;
2552 Values.reserve(getNumOperands()); // Build replacement array...
2553 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2554 Constant *Val = getOperand(i);
2555 if (Val == From) Val = cast<Constant>(To);
2556 Values.push_back(Val);
2557 }
2558
Reid Spencerd84d35b2007-02-15 02:26:10 +00002559 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002560 assert(Replacement != this && "I didn't contain From!");
2561
Chris Lattner7a1450d2005-10-04 18:13:04 +00002562 // Everyone using this now uses the replacement.
2563 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002564
2565 // Delete the old constant!
2566 destroyConstant();
2567}
2568
2569void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002570 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002571 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2572 Constant *To = cast<Constant>(ToV);
2573
2574 Constant *Replacement = 0;
2575 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002576 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002577 Constant *Pointer = getOperand(0);
2578 Indices.reserve(getNumOperands()-1);
2579 if (Pointer == From) Pointer = To;
2580
2581 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2582 Constant *Val = getOperand(i);
2583 if (Val == From) Val = To;
2584 Indices.push_back(Val);
2585 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002586 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2587 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002588 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002589 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002590 if (Agg == From) Agg = To;
2591
Dan Gohman1ecaf452008-05-31 00:58:22 +00002592 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002593 Replacement = ConstantExpr::getExtractValue(Agg,
2594 &Indices[0], Indices.size());
2595 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002596 Constant *Agg = getOperand(0);
2597 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002598 if (Agg == From) Agg = To;
2599 if (Val == From) Val = To;
2600
Dan Gohman1ecaf452008-05-31 00:58:22 +00002601 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002602 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2603 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002605 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002607 } else if (getOpcode() == Instruction::Select) {
2608 Constant *C1 = getOperand(0);
2609 Constant *C2 = getOperand(1);
2610 Constant *C3 = getOperand(2);
2611 if (C1 == From) C1 = To;
2612 if (C2 == From) C2 = To;
2613 if (C3 == From) C3 = To;
2614 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002615 } else if (getOpcode() == Instruction::ExtractElement) {
2616 Constant *C1 = getOperand(0);
2617 Constant *C2 = getOperand(1);
2618 if (C1 == From) C1 = To;
2619 if (C2 == From) C2 = To;
2620 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002621 } else if (getOpcode() == Instruction::InsertElement) {
2622 Constant *C1 = getOperand(0);
2623 Constant *C2 = getOperand(1);
2624 Constant *C3 = getOperand(1);
2625 if (C1 == From) C1 = To;
2626 if (C2 == From) C2 = To;
2627 if (C3 == From) C3 = To;
2628 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2629 } else if (getOpcode() == Instruction::ShuffleVector) {
2630 Constant *C1 = getOperand(0);
2631 Constant *C2 = getOperand(1);
2632 Constant *C3 = getOperand(2);
2633 if (C1 == From) C1 = To;
2634 if (C2 == From) C2 = To;
2635 if (C3 == From) C3 = To;
2636 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002637 } else if (isCompare()) {
2638 Constant *C1 = getOperand(0);
2639 Constant *C2 = getOperand(1);
2640 if (C1 == From) C1 = To;
2641 if (C2 == From) C2 = To;
2642 if (getOpcode() == Instruction::ICmp)
2643 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002644 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002645 assert(getOpcode() == Instruction::FCmp);
2646 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002647 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002648 } else if (getNumOperands() == 2) {
2649 Constant *C1 = getOperand(0);
2650 Constant *C2 = getOperand(1);
2651 if (C1 == From) C1 = To;
2652 if (C2 == From) C2 = To;
2653 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2654 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002655 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002656 return;
2657 }
2658
2659 assert(Replacement != this && "I didn't contain From!");
2660
Chris Lattner7a1450d2005-10-04 18:13:04 +00002661 // Everyone using this now uses the replacement.
2662 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002663
2664 // Delete the old constant!
2665 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002666}
Nick Lewycky49f89192009-04-04 07:22:01 +00002667
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002668void MDNode::replaceElement(Value *From, Value *To) {
2669 SmallVector<Value*, 4> Values;
2670 Values.reserve(getNumElements()); // Build replacement array...
2671 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2672 Value *Val = getElement(i);
2673 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002674 Values.push_back(Val);
2675 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002676
2677 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002678 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002679
Nick Lewycky49f89192009-04-04 07:22:01 +00002680 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002681
Nick Lewycky49f89192009-04-04 07:22:01 +00002682 destroyConstant();
2683}