blob: f8ae2bd79f484ae04e915f19f1bb25e112379f09 [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 Lattner72e39582007-06-15 06:10:53 +0000131Constant *Constant::getAllOnesValue(const Type *Ty) {
132 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
133 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
134 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
135}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136
137// Static constructor to create an integral constant with all bits set
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000138ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000139 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000140 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000141 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000142}
143
Dan Gohman30978072007-05-24 14:36:04 +0000144/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000145/// has all its bits set to true.
146/// @brief Get the all ones value
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000147ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000148 std::vector<Constant*> Elts;
149 Elts.resize(Ty->getNumElements(),
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000150 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000151 assert(Elts[0] && "Not a vector integer type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000152 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000153}
154
155
Chris Lattner2105d662008-07-10 00:28:11 +0000156/// getVectorElements - This method, which is only valid on constant of vector
157/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000158/// This handles breaking down a vector undef into undef elements, etc. For
159/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000160void Constant::getVectorElements(LLVMContext &Context,
161 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000162 assert(isa<VectorType>(getType()) && "Not a vector constant!");
163
164 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
165 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
166 Elts.push_back(CV->getOperand(i));
167 return;
168 }
169
170 const VectorType *VT = cast<VectorType>(getType());
171 if (isa<ConstantAggregateZero>(this)) {
172 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000173 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000174 return;
175 }
176
Chris Lattnerc5098a22008-07-14 05:10:41 +0000177 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000178 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000179 return;
180 }
181
182 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000183}
184
185
186
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000188// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189//===----------------------------------------------------------------------===//
190
Reid Spencerb31bffe2007-02-26 23:54:03 +0000191ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000192 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000193 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194}
195
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000196ConstantInt *ConstantInt::TheTrueVal = 0;
197ConstantInt *ConstantInt::TheFalseVal = 0;
198
199namespace llvm {
200 void CleanupTrueFalse(void *) {
201 ConstantInt::ResetTrueFalse();
202 }
203}
204
205static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
206
207ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
208 assert(TheTrueVal == 0 && TheFalseVal == 0);
209 TheTrueVal = get(Type::Int1Ty, 1);
210 TheFalseVal = get(Type::Int1Ty, 0);
211
212 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
213 TrueFalseCleanup.Register();
214
215 return WhichOne ? TheTrueVal : TheFalseVal;
216}
217
218
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000219namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000220 struct DenseMapAPIntKeyInfo {
221 struct KeyTy {
222 APInt val;
223 const Type* type;
224 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
225 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
226 bool operator==(const KeyTy& that) const {
227 return type == that.type && this->val == that.val;
228 }
229 bool operator!=(const KeyTy& that) const {
230 return !this->operator==(that);
231 }
232 };
233 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
234 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000235 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000236 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000237 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000238 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000239 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
240 return LHS == RHS;
241 }
Dale Johannesena719a602007-08-24 00:56:33 +0000242 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000243 };
244}
245
246
Reid Spencerb31bffe2007-02-26 23:54:03 +0000247typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
248 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000249static ManagedStatic<IntMapTy> IntConstants;
250
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000251ConstantInt *ConstantInt::get(const IntegerType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000252 uint64_t V, bool isSigned) {
253 return get(APInt(Ty->getBitWidth(), V, isSigned));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000254}
255
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000256Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000257 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
258
259 // For vectors, broadcast the value.
260 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
261 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000262 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000263
264 return C;
Reid Spencerb31bffe2007-02-26 23:54:03 +0000265}
266
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000267// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000268// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000269// operator== and operator!= to ensure that the DenseMap doesn't attempt to
270// compare APInt's of different widths, which would violate an APInt class
271// invariant which generates an assertion.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000272ConstantInt *ConstantInt::get(const APInt& V) {
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000273 // Get the corresponding integer type for the bit width of the value.
274 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000275 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000276 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000277
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000278 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000279 ConstantInt *&Slot = (*IntConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000280 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000281
Owen Andersond830eb82009-06-18 19:10:19 +0000282 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000283 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000284 ConstantInt *&NewSlot = (*IntConstants)[Key];
285 if (!Slot) {
286 NewSlot = new ConstantInt(ITy, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000287 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000288
289 return NewSlot;
290 } else {
291 return Slot;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000292 }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000293}
294
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000295Constant *ConstantInt::get(const Type *Ty, const APInt &V) {
296 ConstantInt *C = ConstantInt::get(V);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000297 assert(C->getType() == Ty->getScalarType() &&
298 "ConstantInt type doesn't match the type implied by its value!");
299
300 // For vectors, broadcast the value.
301 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
302 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000303 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000304
305 return C;
306}
307
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000308//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000309// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000310//===----------------------------------------------------------------------===//
311
Chris Lattner98bd9392008-04-09 06:38:30 +0000312static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
313 if (Ty == Type::FloatTy)
314 return &APFloat::IEEEsingle;
315 if (Ty == Type::DoubleTy)
316 return &APFloat::IEEEdouble;
317 if (Ty == Type::X86_FP80Ty)
318 return &APFloat::x87DoubleExtended;
319 else if (Ty == Type::FP128Ty)
320 return &APFloat::IEEEquad;
321
322 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
323 return &APFloat::PPCDoubleDouble;
324}
325
Dale Johannesend246b2c2007-08-30 00:23:21 +0000326ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
327 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000328 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
329 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000330}
331
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000332bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000333 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000334}
335
Dale Johannesend246b2c2007-08-30 00:23:21 +0000336bool ConstantFP::isExactlyValue(const APFloat& V) const {
337 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000338}
339
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000340namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000341 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000342 struct KeyTy {
343 APFloat val;
344 KeyTy(const APFloat& V) : val(V){}
345 KeyTy(const KeyTy& that) : val(that.val) {}
346 bool operator==(const KeyTy& that) const {
347 return this->val.bitwiseIsEqual(that.val);
348 }
349 bool operator!=(const KeyTy& that) const {
350 return !this->operator==(that);
351 }
352 };
353 static inline KeyTy getEmptyKey() {
354 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000355 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000356 static inline KeyTy getTombstoneKey() {
357 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000358 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000359 static unsigned getHashValue(const KeyTy &Key) {
360 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000361 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000362 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
363 return LHS == RHS;
364 }
Dale Johannesena719a602007-08-24 00:56:33 +0000365 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000366 };
367}
368
369//---- ConstantFP::get() implementation...
370//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000371typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000372 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000373
Dale Johannesena719a602007-08-24 00:56:33 +0000374static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000375
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000376ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000377 DenseMapAPFloatKeyInfo::KeyTy Key(V);
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000378
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000379 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000380 ConstantFP *&Slot = (*FPConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000381 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000382
Owen Andersond830eb82009-06-18 19:10:19 +0000383 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000384 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000385 ConstantFP *&NewSlot = (*FPConstants)[Key];
386 if (!NewSlot) {
Owen Andersond830eb82009-06-18 19:10:19 +0000387 const Type *Ty;
388 if (&V.getSemantics() == &APFloat::IEEEsingle)
389 Ty = Type::FloatTy;
390 else if (&V.getSemantics() == &APFloat::IEEEdouble)
391 Ty = Type::DoubleTy;
392 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
393 Ty = Type::X86_FP80Ty;
394 else if (&V.getSemantics() == &APFloat::IEEEquad)
395 Ty = Type::FP128Ty;
396 else {
397 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
398 "Unknown FP format");
399 Ty = Type::PPC_FP128Ty;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000400 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000401 NewSlot = new ConstantFP(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000402 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000403
404 return NewSlot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000405 }
Owen Andersond830eb82009-06-18 19:10:19 +0000406
407 return Slot;
Dale Johannesend246b2c2007-08-30 00:23:21 +0000408}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000409
Chris Lattner98bd9392008-04-09 06:38:30 +0000410/// get() - This returns a constant fp for the specified value in the
411/// specified type. This should only be used for simple constant values like
412/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000413Constant *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000414 APFloat FV(V);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000415 bool ignored;
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000416 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
417 APFloat::rmNearestTiesToEven, &ignored);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000418 Constant *C = get(FV);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000419
420 // For vectors, broadcast the value.
421 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
422 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000423 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000424
425 return C;
Chris Lattner98bd9392008-04-09 06:38:30 +0000426}
427
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000428//===----------------------------------------------------------------------===//
429// ConstantXXX Classes
430//===----------------------------------------------------------------------===//
431
432
Chris Lattner3462ae32001-12-03 22:26:30 +0000433ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000434 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000435 : Constant(T, ConstantArrayVal,
436 OperandTraits<ConstantArray>::op_end(this) - V.size(),
437 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000438 assert(V.size() == T->getNumElements() &&
439 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000440 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000441 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
442 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000443 Constant *C = *I;
444 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000445 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000446 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000447 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000448 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 }
450}
451
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000452
Chris Lattner3462ae32001-12-03 22:26:30 +0000453ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000454 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000455 : Constant(T, ConstantStructVal,
456 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
457 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000458 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000459 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000460 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000461 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
462 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000463 Constant *C = *I;
464 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000465 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000466 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000467 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000468 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000469 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000470 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000471 }
472}
473
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000474
Reid Spencerd84d35b2007-02-15 02:26:10 +0000475ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000476 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000477 : Constant(T, ConstantVectorVal,
478 OperandTraits<ConstantVector>::op_end(this) - V.size(),
479 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000480 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000481 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
482 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000483 Constant *C = *I;
484 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000485 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000486 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000487 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000488 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000489 }
490}
491
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000492
Gabor Greiff6caff662008-05-10 08:32:32 +0000493namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000494// We declare several classes private to this file, so use an anonymous
495// namespace
496namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000497
Gordon Henriksen14a55692007-12-10 02:14:30 +0000498/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
499/// behind the scenes to implement unary constant exprs.
500class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000501 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000502public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000503 // allocate space for exactly one operand
504 void *operator new(size_t s) {
505 return User::operator new(s, 1);
506 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000507 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000508 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
509 Op<0>() = C;
510 }
511 /// Transparently provide more efficient getOperand methods.
512 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000513};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000514
Gordon Henriksen14a55692007-12-10 02:14:30 +0000515/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
516/// behind the scenes to implement binary constant exprs.
517class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000518 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000519public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000520 // allocate space for exactly two operands
521 void *operator new(size_t s) {
522 return User::operator new(s, 2);
523 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000524 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000525 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000526 Op<0>() = C1;
527 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000528 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000529 /// Transparently provide more efficient getOperand methods.
530 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000531};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000532
Gordon Henriksen14a55692007-12-10 02:14:30 +0000533/// SelectConstantExpr - This class is private to Constants.cpp, and is used
534/// behind the scenes to implement select constant exprs.
535class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000536 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000537public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000538 // allocate space for exactly three operands
539 void *operator new(size_t s) {
540 return User::operator new(s, 3);
541 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000542 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000543 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000544 Op<0>() = C1;
545 Op<1>() = C2;
546 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000547 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 /// Transparently provide more efficient getOperand methods.
549 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000550};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000551
Gordon Henriksen14a55692007-12-10 02:14:30 +0000552/// ExtractElementConstantExpr - This class is private to
553/// Constants.cpp, and is used behind the scenes to implement
554/// extractelement constant exprs.
555class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000556 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000557public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000558 // allocate space for exactly two operands
559 void *operator new(size_t s) {
560 return User::operator new(s, 2);
561 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000562 ExtractElementConstantExpr(Constant *C1, Constant *C2)
563 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000564 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000565 Op<0>() = C1;
566 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000567 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000568 /// Transparently provide more efficient getOperand methods.
569 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000570};
Robert Bocchino23004482006-01-10 19:05:34 +0000571
Gordon Henriksen14a55692007-12-10 02:14:30 +0000572/// InsertElementConstantExpr - This class is private to
573/// Constants.cpp, and is used behind the scenes to implement
574/// insertelement constant exprs.
575class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000576 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000577public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000578 // allocate space for exactly three operands
579 void *operator new(size_t s) {
580 return User::operator new(s, 3);
581 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000582 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
583 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000584 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000585 Op<0>() = C1;
586 Op<1>() = C2;
587 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000588 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000589 /// Transparently provide more efficient getOperand methods.
590 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000591};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000592
Gordon Henriksen14a55692007-12-10 02:14:30 +0000593/// ShuffleVectorConstantExpr - This class is private to
594/// Constants.cpp, and is used behind the scenes to implement
595/// shufflevector constant exprs.
596class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000597 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000598public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000599 // allocate space for exactly three operands
600 void *operator new(size_t s) {
601 return User::operator new(s, 3);
602 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000603 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000604 : ConstantExpr(VectorType::get(
605 cast<VectorType>(C1->getType())->getElementType(),
606 cast<VectorType>(C3->getType())->getNumElements()),
607 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000608 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000609 Op<0>() = C1;
610 Op<1>() = C2;
611 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000612 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000613 /// Transparently provide more efficient getOperand methods.
614 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000615};
616
Dan Gohman12fce772008-05-15 19:50:34 +0000617/// ExtractValueConstantExpr - This class is private to
618/// Constants.cpp, and is used behind the scenes to implement
619/// extractvalue constant exprs.
620class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000621 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000622public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000623 // allocate space for exactly one operand
624 void *operator new(size_t s) {
625 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000626 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000627 ExtractValueConstantExpr(Constant *Agg,
628 const SmallVector<unsigned, 4> &IdxList,
629 const Type *DestTy)
630 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
631 Indices(IdxList) {
632 Op<0>() = Agg;
633 }
634
Dan Gohman7bb04502008-05-31 19:09:08 +0000635 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000636 const SmallVector<unsigned, 4> Indices;
637
Dan Gohman12fce772008-05-15 19:50:34 +0000638 /// Transparently provide more efficient getOperand methods.
639 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
640};
641
642/// InsertValueConstantExpr - This class is private to
643/// Constants.cpp, and is used behind the scenes to implement
644/// insertvalue constant exprs.
645class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000646 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000647public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000648 // allocate space for exactly one operand
649 void *operator new(size_t s) {
650 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000651 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000652 InsertValueConstantExpr(Constant *Agg, Constant *Val,
653 const SmallVector<unsigned, 4> &IdxList,
654 const Type *DestTy)
655 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
656 Indices(IdxList) {
657 Op<0>() = Agg;
658 Op<1>() = Val;
659 }
660
Dan Gohman7bb04502008-05-31 19:09:08 +0000661 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000662 const SmallVector<unsigned, 4> Indices;
663
Dan Gohman12fce772008-05-15 19:50:34 +0000664 /// Transparently provide more efficient getOperand methods.
665 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
666};
667
668
Gordon Henriksen14a55692007-12-10 02:14:30 +0000669/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
670/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000671class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000672 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000673 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000674public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000675 static GetElementPtrConstantExpr *Create(Constant *C,
676 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000677 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000678 return new(IdxList.size() + 1)
679 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000680 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000681 /// Transparently provide more efficient getOperand methods.
682 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000683};
684
685// CompareConstantExpr - This class is private to Constants.cpp, and is used
686// behind the scenes to implement ICmp and FCmp constant expressions. This is
687// needed in order to store the predicate value for these instructions.
688struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000689 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
690 // allocate space for exactly two operands
691 void *operator new(size_t s) {
692 return User::operator new(s, 2);
693 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000694 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000695 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
696 unsigned short pred, Constant* LHS, Constant* RHS)
697 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000698 Op<0>() = LHS;
699 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000700 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000701 /// Transparently provide more efficient getOperand methods.
702 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000703};
704
705} // end anonymous namespace
706
Gabor Greiff6caff662008-05-10 08:32:32 +0000707template <>
708struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
709};
710DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
711
712template <>
713struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
714};
715DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
716
717template <>
718struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
719};
720DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
721
722template <>
723struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
724};
725DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
726
727template <>
728struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
729};
730DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
731
732template <>
733struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
734};
735DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
736
Dan Gohman12fce772008-05-15 19:50:34 +0000737template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000738struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000739};
Dan Gohman12fce772008-05-15 19:50:34 +0000740DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
741
742template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000743struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000744};
Dan Gohman12fce772008-05-15 19:50:34 +0000745DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
746
Gabor Greiff6caff662008-05-10 08:32:32 +0000747template <>
748struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
749};
750
751GetElementPtrConstantExpr::GetElementPtrConstantExpr
752 (Constant *C,
753 const std::vector<Constant*> &IdxList,
754 const Type *DestTy)
755 : ConstantExpr(DestTy, Instruction::GetElementPtr,
756 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
757 - (IdxList.size()+1),
758 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000759 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000760 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000761 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000762}
763
764DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
765
766
767template <>
768struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
769};
770DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
771
772
773} // End llvm namespace
774
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000775
776// Utility function for determining if a ConstantExpr is a CastOp or not. This
777// can't be inline because we don't want to #include Instruction.h into
778// Constant.h
779bool ConstantExpr::isCast() const {
780 return Instruction::isCast(getOpcode());
781}
782
Reid Spenceree3c9912006-12-04 05:19:50 +0000783bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000784 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000785}
786
Dan Gohman1ecaf452008-05-31 00:58:22 +0000787bool ConstantExpr::hasIndices() const {
788 return getOpcode() == Instruction::ExtractValue ||
789 getOpcode() == Instruction::InsertValue;
790}
791
792const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
793 if (const ExtractValueConstantExpr *EVCE =
794 dyn_cast<ExtractValueConstantExpr>(this))
795 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000796
797 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000798}
799
Chris Lattner817175f2004-03-29 02:37:53 +0000800Constant *ConstantExpr::getNot(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000801 assert(C->getType()->isIntOrIntVector() &&
802 "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000803 return get(Instruction::Xor, C,
Dale Johannesen47a5ef32008-08-21 21:20:09 +0000804 Constant::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000805}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000806Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
807 return get(Instruction::Add, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000808}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000809Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
810 return get(Instruction::FAdd, C1, C2);
Dan Gohmana5b96452009-06-04 22:49:04 +0000811}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000812Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
813 return get(Instruction::Sub, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000814}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000815Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
816 return get(Instruction::FSub, C1, C2);
Dan Gohmana5b96452009-06-04 22:49:04 +0000817}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000818Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
819 return get(Instruction::Mul, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000820}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000821Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
822 return get(Instruction::FMul, C1, C2);
Dan Gohmana5b96452009-06-04 22:49:04 +0000823}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000824Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
825 return get(Instruction::UDiv, C1, C2);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000826}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000827Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
828 return get(Instruction::SDiv, C1, C2);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000829}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000830Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
831 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000832}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000833Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
834 return get(Instruction::URem, C1, C2);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000835}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000836Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
837 return get(Instruction::SRem, C1, C2);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000838}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000839Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
840 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000841}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000842Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
843 return get(Instruction::And, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000844}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000845Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
846 return get(Instruction::Or, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000847}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000848Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
849 return get(Instruction::Xor, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000850}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000851unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000852 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000853 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000854 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000855}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000856Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
857 return get(Instruction::Shl, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000858}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000859Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
860 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000861}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000862Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
863 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000864}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000865
Chris Lattner7c1018a2006-07-14 19:37:40 +0000866/// getWithOperandReplaced - Return a constant expression identical to this
867/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000868Constant *
869ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000870 assert(OpNo < getNumOperands() && "Operand num is out of range!");
871 assert(Op->getType() == getOperand(OpNo)->getType() &&
872 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000873 if (getOperand(OpNo) == Op)
874 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000875
Chris Lattner227816342006-07-14 22:20:01 +0000876 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000877 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000878 case Instruction::Trunc:
879 case Instruction::ZExt:
880 case Instruction::SExt:
881 case Instruction::FPTrunc:
882 case Instruction::FPExt:
883 case Instruction::UIToFP:
884 case Instruction::SIToFP:
885 case Instruction::FPToUI:
886 case Instruction::FPToSI:
887 case Instruction::PtrToInt:
888 case Instruction::IntToPtr:
889 case Instruction::BitCast:
890 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000891 case Instruction::Select:
892 Op0 = (OpNo == 0) ? Op : getOperand(0);
893 Op1 = (OpNo == 1) ? Op : getOperand(1);
894 Op2 = (OpNo == 2) ? Op : getOperand(2);
895 return ConstantExpr::getSelect(Op0, Op1, Op2);
896 case Instruction::InsertElement:
897 Op0 = (OpNo == 0) ? Op : getOperand(0);
898 Op1 = (OpNo == 1) ? Op : getOperand(1);
899 Op2 = (OpNo == 2) ? Op : getOperand(2);
900 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
901 case Instruction::ExtractElement:
902 Op0 = (OpNo == 0) ? Op : getOperand(0);
903 Op1 = (OpNo == 1) ? Op : getOperand(1);
904 return ConstantExpr::getExtractElement(Op0, Op1);
905 case Instruction::ShuffleVector:
906 Op0 = (OpNo == 0) ? Op : getOperand(0);
907 Op1 = (OpNo == 1) ? Op : getOperand(1);
908 Op2 = (OpNo == 2) ? Op : getOperand(2);
909 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000910 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000911 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000912 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000913 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000914 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000915 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000916 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000917 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000918 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000919 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000920 default:
921 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000922 Op0 = (OpNo == 0) ? Op : getOperand(0);
923 Op1 = (OpNo == 1) ? Op : getOperand(1);
924 return ConstantExpr::get(getOpcode(), Op0, Op1);
925 }
926}
927
928/// getWithOperands - This returns the current constant expression with the
929/// operands replaced with the specified values. The specified operands must
930/// match count and type with the existing ones.
931Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000932getWithOperands(Constant* const *Ops, unsigned NumOps) const {
933 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000934 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000935 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000936 assert(Ops[i]->getType() == getOperand(i)->getType() &&
937 "Operand type mismatch!");
938 AnyChange |= Ops[i] != getOperand(i);
939 }
940 if (!AnyChange) // No operands changed, return self.
941 return const_cast<ConstantExpr*>(this);
942
943 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000944 case Instruction::Trunc:
945 case Instruction::ZExt:
946 case Instruction::SExt:
947 case Instruction::FPTrunc:
948 case Instruction::FPExt:
949 case Instruction::UIToFP:
950 case Instruction::SIToFP:
951 case Instruction::FPToUI:
952 case Instruction::FPToSI:
953 case Instruction::PtrToInt:
954 case Instruction::IntToPtr:
955 case Instruction::BitCast:
956 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000957 case Instruction::Select:
958 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
959 case Instruction::InsertElement:
960 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
961 case Instruction::ExtractElement:
962 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
963 case Instruction::ShuffleVector:
964 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000965 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000966 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000967 case Instruction::ICmp:
968 case Instruction::FCmp:
969 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000970 default:
971 assert(getNumOperands() == 2 && "Must be binary operator?");
972 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000973 }
974}
975
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976
977//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000978// isValueValidForType implementations
979
Reid Spencere7334722006-12-19 01:28:19 +0000980bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000981 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000982 if (Ty == Type::Int1Ty)
983 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000984 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000985 return true; // always true, has to fit in largest type
986 uint64_t Max = (1ll << NumBits) - 1;
987 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000988}
989
Reid Spencere0fc4df2006-10-20 07:07:24 +0000990bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000991 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000992 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000993 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000994 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000995 return true; // always true, has to fit in largest type
996 int64_t Min = -(1ll << (NumBits-1));
997 int64_t Max = (1ll << (NumBits-1)) - 1;
998 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000999}
1000
Dale Johannesend246b2c2007-08-30 00:23:21 +00001001bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1002 // convert modifies in place, so make a copy.
1003 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001004 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001005 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001006 default:
1007 return false; // These can't be represented as floating point!
1008
Dale Johannesend246b2c2007-08-30 00:23:21 +00001009 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001010 case Type::FloatTyID: {
1011 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1012 return true;
1013 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1014 return !losesInfo;
1015 }
1016 case Type::DoubleTyID: {
1017 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1018 &Val2.getSemantics() == &APFloat::IEEEdouble)
1019 return true;
1020 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1021 return !losesInfo;
1022 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001023 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001024 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1025 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1026 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001027 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001028 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1029 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1030 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001031 case Type::PPC_FP128TyID:
1032 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1033 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1034 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001035 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001036}
Chris Lattner9655e542001-07-20 19:16:02 +00001037
Chris Lattner49d855c2001-09-07 16:46:31 +00001038//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001039// Factory Function Implementation
1040
Gabor Greiff6caff662008-05-10 08:32:32 +00001041
1042// The number of operands for each ConstantCreator::create method is
1043// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001044// ConstantCreator - A class that is used to create constants by
1045// ValueMap*. This class should be partially specialized if there is
1046// something strange that needs to be done to interface to the ctor for the
1047// constant.
1048//
Chris Lattner189d19f2003-11-21 20:23:48 +00001049namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001050 template<class ValType>
1051 struct ConstantTraits;
1052
1053 template<typename T, typename Alloc>
1054 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1055 static unsigned uses(const std::vector<T, Alloc>& v) {
1056 return v.size();
1057 }
1058 };
1059
Chris Lattner189d19f2003-11-21 20:23:48 +00001060 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001061 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001062 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001064 }
1065 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001066
Chris Lattner189d19f2003-11-21 20:23:48 +00001067 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001068 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001069 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwin69208f02009-07-12 07:15:17 +00001070 LLVM_UNREACHABLE("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +00001071 }
1072 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001073
Chris Lattner935aa922005-10-04 17:48:46 +00001074 template<class ValType, class TypeClass, class ConstantClass,
1075 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001076 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001077 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001078 typedef std::pair<const Type*, ValType> MapKey;
1079 typedef std::map<MapKey, Constant *> MapTy;
1080 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1081 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001082 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001083 /// Map - This is the main map from the element descriptor to the Constants.
1084 /// This is the primary way we avoid creating two of the same shape
1085 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001086 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001087
1088 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1089 /// from the constants to their element in Map. This is important for
1090 /// removal of constants from the array, which would otherwise have to scan
1091 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001092 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001093
Jim Laskeyc03caef2006-07-17 17:38:29 +00001094 /// AbstractTypeMap - Map for abstract type constants.
1095 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001096 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001097
1098 /// ValueMapLock - Mutex for this map.
1099 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +00001100
Chris Lattner98fa07b2003-05-23 20:03:32 +00001101 public:
Owen Anderson61794042009-06-17 20:10:08 +00001102 // NOTE: This function is not locked. It is the caller's responsibility
1103 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001104 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001105
1106 /// InsertOrGetItem - Return an iterator for the specified element.
1107 /// If the element exists in the map, the returned iterator points to the
1108 /// entry and Exists=true. If not, the iterator points to the newly
1109 /// inserted entry and returns Exists=false. Newly inserted entries have
1110 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +00001111 /// NOTE: This function is not locked. It is the caller's responsibility
1112 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001113 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1114 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001115 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001116 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001117 Exists = !IP.second;
1118 return IP.first;
1119 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001120
Chris Lattner935aa922005-10-04 17:48:46 +00001121private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001122 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001123 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001124 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001125 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1126 IMI->second->second == CP &&
1127 "InverseMap corrupt!");
1128 return IMI->second;
1129 }
1130
Jim Laskeyc03caef2006-07-17 17:38:29 +00001131 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001132 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1133 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001134 if (I == Map.end() || I->second != CP) {
1135 // FIXME: This should not use a linear scan. If this gets to be a
1136 // performance problem, someone should look at this.
1137 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1138 /* empty */;
1139 }
Chris Lattner935aa922005-10-04 17:48:46 +00001140 return I;
1141 }
Owen Andersonf89c38c2009-06-17 20:43:39 +00001142
1143 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
1144 typename MapTy::iterator I) {
1145 ConstantClass* Result =
1146 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1147
1148 assert(Result->getType() == Ty && "Type specified is not correct!");
1149 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1150
1151 if (HasLargeKey) // Remember the reverse mapping if needed.
1152 InverseMap.insert(std::make_pair(Result, I));
1153
1154 // If the type of the constant is abstract, make sure that an entry
1155 // exists for it in the AbstractTypeMap.
1156 if (Ty->isAbstract()) {
1157 typename AbstractTypeMapTy::iterator TI =
1158 AbstractTypeMap.find(Ty);
1159
1160 if (TI == AbstractTypeMap.end()) {
1161 // Add ourselves to the ATU list of the type.
1162 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1163
1164 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1165 }
1166 }
1167
1168 return Result;
1169 }
Chris Lattner935aa922005-10-04 17:48:46 +00001170public:
1171
Chris Lattnerb64419a2005-10-03 22:51:37 +00001172 /// getOrCreate - Return the specified constant from the map, creating it if
1173 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001174 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001175 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +00001176 MapKey Lookup(Ty, V);
1177 ConstantClass* Result = 0;
1178
1179 typename MapTy::iterator I = Map.find(Lookup);
1180 // Is it in the map?
1181 if (I != Map.end())
1182 Result = static_cast<ConstantClass *>(I->second);
1183
1184 if (!Result) {
1185 // If no preexisting value, create one now...
1186 Result = Create(Ty, V, I);
1187 }
1188
1189 return Result;
1190 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001191
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001192 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001193 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001194 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001195 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001196 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001197
Chris Lattner935aa922005-10-04 17:48:46 +00001198 if (HasLargeKey) // Remember the reverse mapping if needed.
1199 InverseMap.erase(CP);
1200
Chris Lattnerb50d1352003-10-05 00:17:43 +00001201 // Now that we found the entry, make sure this isn't the entry that
1202 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001203 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001204 if (Ty->isAbstract()) {
1205 assert(AbstractTypeMap.count(Ty) &&
1206 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001207 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001208 if (ATMEntryIt == I) {
1209 // Yes, we are removing the representative entry for this type.
1210 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001211 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001212
Chris Lattnerb50d1352003-10-05 00:17:43 +00001213 // First check the entry before this one...
1214 if (TmpIt != Map.begin()) {
1215 --TmpIt;
1216 if (TmpIt->first.first != Ty) // Not the same type, move back...
1217 ++TmpIt;
1218 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001219
Chris Lattnerb50d1352003-10-05 00:17:43 +00001220 // If we didn't find the same type, try to move forward...
1221 if (TmpIt == ATMEntryIt) {
1222 ++TmpIt;
1223 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1224 --TmpIt; // No entry afterwards with the same type
1225 }
1226
1227 // If there is another entry in the map of the same abstract type,
1228 // update the AbstractTypeMap entry now.
1229 if (TmpIt != ATMEntryIt) {
1230 ATMEntryIt = TmpIt;
1231 } else {
1232 // Otherwise, we are removing the last instance of this type
1233 // from the table. Remove from the ATM, and from user list.
1234 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1235 AbstractTypeMap.erase(Ty);
1236 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001237 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001238 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001239
Chris Lattnerb50d1352003-10-05 00:17:43 +00001240 Map.erase(I);
1241 }
1242
Chris Lattner3b793c62005-10-04 21:35:50 +00001243
1244 /// MoveConstantToNewSlot - If we are about to change C to be the element
1245 /// specified by I, update our internal data structures to reflect this
1246 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +00001247 /// NOTE: This function is not locked. It is the responsibility of the
1248 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001249 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001250 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001251 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001252 assert(OldI != Map.end() && "Constant not found in constant table!");
1253 assert(OldI->second == C && "Didn't find correct element?");
1254
1255 // If this constant is the representative element for its abstract type,
1256 // update the AbstractTypeMap so that the representative element is I.
1257 if (C->getType()->isAbstract()) {
1258 typename AbstractTypeMapTy::iterator ATI =
1259 AbstractTypeMap.find(C->getType());
1260 assert(ATI != AbstractTypeMap.end() &&
1261 "Abstract type not in AbstractTypeMap?");
1262 if (ATI->second == OldI)
1263 ATI->second = I;
1264 }
1265
1266 // Remove the old entry from the map.
1267 Map.erase(OldI);
1268
1269 // Update the inverse map so that we know that this constant is now
1270 // located at descriptor I.
1271 if (HasLargeKey) {
1272 assert(I->second == C && "Bad inversemap entry!");
1273 InverseMap[C] = I;
1274 }
1275 }
1276
Chris Lattnerb50d1352003-10-05 00:17:43 +00001277 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001278 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001279 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001280 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001281
1282 assert(I != AbstractTypeMap.end() &&
1283 "Abstract type not in AbstractTypeMap?");
1284
1285 // Convert a constant at a time until the last one is gone. The last one
1286 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1287 // eliminated eventually.
1288 do {
1289 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001290 TypeClass>::convert(
1291 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001292 cast<TypeClass>(NewTy));
1293
Jim Laskeyc03caef2006-07-17 17:38:29 +00001294 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001295 } while (I != AbstractTypeMap.end());
1296 }
1297
1298 // If the type became concrete without being refined to any other existing
1299 // type, we just remove ourselves from the ATU list.
1300 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001301 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001302 }
1303
1304 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001305 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001306 }
1307 };
1308}
1309
Chris Lattnera84df0a22006-09-28 23:36:21 +00001310
Chris Lattner28173502007-02-20 06:11:36 +00001311
Chris Lattner9fba3da2004-02-15 05:53:04 +00001312//---- ConstantAggregateZero::get() implementation...
1313//
1314namespace llvm {
1315 // ConstantAggregateZero does not take extra "value" argument...
1316 template<class ValType>
1317 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1318 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1319 return new ConstantAggregateZero(Ty);
1320 }
1321 };
1322
1323 template<>
1324 struct ConvertConstantType<ConstantAggregateZero, Type> {
1325 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1326 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001327 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001328 assert(New != OldC && "Didn't replace constant??");
1329 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001330 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001331 }
1332 };
1333}
1334
Chris Lattner69edc982006-09-28 00:35:06 +00001335static ManagedStatic<ValueMap<char, Type,
1336 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001337
Chris Lattner3e650af2004-08-04 04:48:01 +00001338static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1339
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001340ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001341 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001342 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001343
1344 // Implicitly locked.
1345 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001346}
1347
Dan Gohman92b551b2009-03-03 02:55:14 +00001348/// destroyConstant - Remove the constant from the constant table...
1349///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001350void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001351 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001352 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001353 destroyConstantImpl();
1354}
1355
Chris Lattner3462ae32001-12-03 22:26:30 +00001356//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001357//
Chris Lattner189d19f2003-11-21 20:23:48 +00001358namespace llvm {
1359 template<>
1360 struct ConvertConstantType<ConstantArray, ArrayType> {
1361 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1362 // Make everyone now use a constant of the new type...
1363 std::vector<Constant*> C;
1364 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1365 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001366 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001367 assert(New != OldC && "Didn't replace constant??");
1368 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001369 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001370 }
1371 };
1372}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001373
Chris Lattner3e650af2004-08-04 04:48:01 +00001374static std::vector<Constant*> getValType(ConstantArray *CA) {
1375 std::vector<Constant*> Elements;
1376 Elements.reserve(CA->getNumOperands());
1377 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1378 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1379 return Elements;
1380}
1381
Chris Lattnerb64419a2005-10-03 22:51:37 +00001382typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001383 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001384static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001385
Chris Lattner015e8212004-02-15 04:14:47 +00001386Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001387 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001388 // If this is an all-zero array, return a ConstantAggregateZero object
1389 if (!V.empty()) {
1390 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001391 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001392 // Implicitly locked.
1393 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001394 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001395 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001396 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001397 // Implicitly locked.
1398 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001399 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001400 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001401
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001402 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001403}
1404
Dan Gohman92b551b2009-03-03 02:55:14 +00001405/// destroyConstant - Remove the constant from the constant table...
1406///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001407void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001408 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001409 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001410 destroyConstantImpl();
1411}
1412
Reid Spencer6f614532006-05-30 08:23:18 +00001413/// ConstantArray::get(const string&) - Return an array that is initialized to
1414/// contain the specified string. If length is zero then a null terminator is
1415/// added to the specified string so that it may be used in a natural way.
1416/// Otherwise, the length parameter specifies how much of the string to use
1417/// and it won't be null terminated.
1418///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001419Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001420 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001421 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001422 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001423
1424 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001425 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001426 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001427 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001428
Reid Spencer8d9336d2006-12-31 05:26:44 +00001429 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001430 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001431}
1432
Reid Spencer2546b762007-01-26 07:37:34 +00001433/// isString - This method returns true if the array is an array of i8, and
1434/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001435bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001436 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001437 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001438 return false;
1439 // Check the elements to make sure they are all integers, not constant
1440 // expressions.
1441 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1442 if (!isa<ConstantInt>(getOperand(i)))
1443 return false;
1444 return true;
1445}
1446
Evan Cheng3763c5b2006-10-26 19:15:05 +00001447/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001448/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001449/// null bytes except its terminator.
Owen Anderson53a52212009-07-13 04:09:18 +00001450bool ConstantArray::isCString(LLVMContext &Context) const {
Reid Spencer2546b762007-01-26 07:37:34 +00001451 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001452 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001453 return false;
Owen Anderson53a52212009-07-13 04:09:18 +00001454 Constant *Zero = Context.getNullValue(getOperand(0)->getType());
Evan Chenge974da62006-10-26 21:48:03 +00001455 // Last element must be a null.
1456 if (getOperand(getNumOperands()-1) != Zero)
1457 return false;
1458 // Other elements must be non-null integers.
1459 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1460 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001461 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001462 if (getOperand(i) == Zero)
1463 return false;
1464 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001465 return true;
1466}
1467
1468
Dan Gohman92b551b2009-03-03 02:55:14 +00001469/// getAsString - If the sub-element type of this array is i8
1470/// then this method converts the array to an std::string and returns it.
1471/// Otherwise, it asserts out.
1472///
Chris Lattner81fabb02002-08-26 17:53:56 +00001473std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001474 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001475 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001476 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001477 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001478 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001479 return Result;
1480}
1481
1482
Chris Lattner3462ae32001-12-03 22:26:30 +00001483//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001484//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001485
Chris Lattner189d19f2003-11-21 20:23:48 +00001486namespace llvm {
1487 template<>
1488 struct ConvertConstantType<ConstantStruct, StructType> {
1489 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1490 // Make everyone now use a constant of the new type...
1491 std::vector<Constant*> C;
1492 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1493 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001494 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001495 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001496
Chris Lattner189d19f2003-11-21 20:23:48 +00001497 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001498 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001499 }
1500 };
1501}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001502
Chris Lattner8760ec72005-10-04 01:17:50 +00001503typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001504 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001505static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001506
Chris Lattner3e650af2004-08-04 04:48:01 +00001507static std::vector<Constant*> getValType(ConstantStruct *CS) {
1508 std::vector<Constant*> Elements;
1509 Elements.reserve(CS->getNumOperands());
1510 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1511 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1512 return Elements;
1513}
1514
Chris Lattner015e8212004-02-15 04:14:47 +00001515Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001516 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001517 // Create a ConstantAggregateZero value if all elements are zeros...
1518 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001519 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001520 // Implicitly locked.
1521 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001522
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001523 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001524}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001525
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001526Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001527 std::vector<const Type*> StructEls;
1528 StructEls.reserve(V.size());
1529 for (unsigned i = 0, e = V.size(); i != e; ++i)
1530 StructEls.push_back(V[i]->getType());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001531 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001532}
1533
Chris Lattnerd7a73302001-10-13 06:57:33 +00001534// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001535//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001536void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001537 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001538 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001539 destroyConstantImpl();
1540}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001541
Reid Spencerd84d35b2007-02-15 02:26:10 +00001542//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001543//
1544namespace llvm {
1545 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001546 struct ConvertConstantType<ConstantVector, VectorType> {
1547 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001548 // Make everyone now use a constant of the new type...
1549 std::vector<Constant*> C;
1550 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1551 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001552 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001553 assert(New != OldC && "Didn't replace constant??");
1554 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001555 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001556 }
1557 };
1558}
1559
Reid Spencerd84d35b2007-02-15 02:26:10 +00001560static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001561 std::vector<Constant*> Elements;
1562 Elements.reserve(CP->getNumOperands());
1563 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1564 Elements.push_back(CP->getOperand(i));
1565 return Elements;
1566}
1567
Reid Spencerd84d35b2007-02-15 02:26:10 +00001568static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001569 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001570
Reid Spencerd84d35b2007-02-15 02:26:10 +00001571Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001572 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001573 assert(!V.empty() && "Vectors can't be empty");
1574 // If this is an all-undef or alll-zero vector, return a
1575 // ConstantAggregateZero or UndefValue.
1576 Constant *C = V[0];
1577 bool isZero = C->isNullValue();
1578 bool isUndef = isa<UndefValue>(C);
1579
1580 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001581 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001582 if (V[i] != C) {
1583 isZero = isUndef = false;
1584 break;
1585 }
Brian Gaeke02209042004-08-20 06:00:58 +00001586 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001587
1588 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001589 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001590 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001591 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001592
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001593 // Implicitly locked.
1594 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001595}
1596
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001597Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001598 assert(!V.empty() && "Cannot infer type if V is empty");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001599 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001600}
1601
1602// destroyConstant - Remove the constant from the constant table...
1603//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001604void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001605 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001606 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001607 destroyConstantImpl();
1608}
1609
Dan Gohman30978072007-05-24 14:36:04 +00001610/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001611/// is set to all ones.
1612/// @returns true iff this constant's emements are all set to all ones.
1613/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001614bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001615 // Check out first element.
1616 const Constant *Elt = getOperand(0);
1617 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1618 if (!CI || !CI->isAllOnesValue()) return false;
1619 // Then make sure all remaining elements point to the same value.
1620 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1621 if (getOperand(I) != Elt) return false;
1622 }
1623 return true;
1624}
1625
Dan Gohman07159202007-10-17 17:51:30 +00001626/// getSplatValue - If this is a splat constant, where all of the
1627/// elements have the same value, return that value. Otherwise return null.
1628Constant *ConstantVector::getSplatValue() {
1629 // Check out first element.
1630 Constant *Elt = getOperand(0);
1631 // Then make sure all remaining elements point to the same value.
1632 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1633 if (getOperand(I) != Elt) return 0;
1634 return Elt;
1635}
1636
Chris Lattner3462ae32001-12-03 22:26:30 +00001637//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001638//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001639
Chris Lattner189d19f2003-11-21 20:23:48 +00001640namespace llvm {
1641 // ConstantPointerNull does not take extra "value" argument...
1642 template<class ValType>
1643 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1644 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1645 return new ConstantPointerNull(Ty);
1646 }
1647 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001648
Chris Lattner189d19f2003-11-21 20:23:48 +00001649 template<>
1650 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1651 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1652 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001653 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001654 assert(New != OldC && "Didn't replace constant??");
1655 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001656 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001657 }
1658 };
1659}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001660
Chris Lattner69edc982006-09-28 00:35:06 +00001661static ManagedStatic<ValueMap<char, PointerType,
1662 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001663
Chris Lattner3e650af2004-08-04 04:48:01 +00001664static char getValType(ConstantPointerNull *) {
1665 return 0;
1666}
1667
1668
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001669ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001670 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001671 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001672}
1673
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001674// destroyConstant - Remove the constant from the constant table...
1675//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001676void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001677 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001678 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001679 destroyConstantImpl();
1680}
1681
1682
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001683//---- UndefValue::get() implementation...
1684//
1685
1686namespace llvm {
1687 // UndefValue does not take extra "value" argument...
1688 template<class ValType>
1689 struct ConstantCreator<UndefValue, Type, ValType> {
1690 static UndefValue *create(const Type *Ty, const ValType &V) {
1691 return new UndefValue(Ty);
1692 }
1693 };
1694
1695 template<>
1696 struct ConvertConstantType<UndefValue, Type> {
1697 static void convert(UndefValue *OldC, const Type *NewTy) {
1698 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001699 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001700 assert(New != OldC && "Didn't replace constant??");
1701 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001702 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001703 }
1704 };
1705}
1706
Chris Lattner69edc982006-09-28 00:35:06 +00001707static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001708
1709static char getValType(UndefValue *) {
1710 return 0;
1711}
1712
1713
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001714UndefValue *UndefValue::get(const Type *Ty) {
1715 // Implicitly locked.
1716 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001717}
1718
1719// destroyConstant - Remove the constant from the constant table.
1720//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001721void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001722 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001723 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001724 destroyConstantImpl();
1725}
1726
Nick Lewycky49f89192009-04-04 07:22:01 +00001727//---- MDString::get() implementation
1728//
1729
1730MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001731 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001732 StrBegin(begin), StrEnd(end) {}
1733
1734static ManagedStatic<StringMap<MDString*> > MDStringCache;
1735
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001736MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001737 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001738 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1739 StrBegin, StrEnd);
1740 MDString *&S = Entry.getValue();
1741 if (!S) S = new MDString(Entry.getKeyData(),
1742 Entry.getKeyData() + Entry.getKeyLength());
Owen Anderson65c5cd72009-06-17 20:34:43 +00001743
Owen Andersond830eb82009-06-18 19:10:19 +00001744 return S;
Nick Lewycky49f89192009-04-04 07:22:01 +00001745}
1746
Devang Patel4c563162009-06-24 22:42:39 +00001747MDString *MDString::get(const std::string &Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001748 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Devang Patel4c563162009-06-24 22:42:39 +00001749 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1750 Str.data(), Str.data() + Str.size());
1751 MDString *&S = Entry.getValue();
1752 if (!S) S = new MDString(Entry.getKeyData(),
1753 Entry.getKeyData() + Entry.getKeyLength());
1754
1755 return S;
1756}
1757
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001758void MDString::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001759 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001760 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
Nick Lewycky49f89192009-04-04 07:22:01 +00001761 destroyConstantImpl();
1762}
1763
1764//---- MDNode::get() implementation
1765//
1766
1767static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1768
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001769MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001770 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001771 for (unsigned i = 0; i != NumVals; ++i)
1772 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001773}
1774
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001775void MDNode::Profile(FoldingSetNodeID &ID) const {
1776 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001777 ID.AddPointer(*I);
1778}
1779
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001780MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001781 FoldingSetNodeID ID;
1782 for (unsigned i = 0; i != NumVals; ++i)
1783 ID.AddPointer(Vals[i]);
1784
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001785 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001786 void *InsertPoint;
1787 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001788 ConstantsLock->reader_release();
Owen Andersond830eb82009-06-18 19:10:19 +00001789
1790 if (!N) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001791 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001792 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1793 if (!N) {
Owen Andersond830eb82009-06-18 19:10:19 +00001794 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1795 N = new(0) MDNode(Vals, NumVals);
1796 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001797 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001798 }
Owen Andersond830eb82009-06-18 19:10:19 +00001799 return N;
Nick Lewycky49f89192009-04-04 07:22:01 +00001800}
1801
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001802void MDNode::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001803 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001804 MDNodeSet->RemoveNode(this);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001805
Nick Lewycky49f89192009-04-04 07:22:01 +00001806 destroyConstantImpl();
1807}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001808
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001809//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001810//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001811
Dan Gohmand78c4002008-05-13 00:00:25 +00001812namespace {
1813
Reid Spenceree3c9912006-12-04 05:19:50 +00001814struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001815 typedef SmallVector<unsigned, 4> IndexList;
1816
1817 ExprMapKeyType(unsigned opc,
1818 const std::vector<Constant*> &ops,
1819 unsigned short pred = 0,
1820 const IndexList &inds = IndexList())
1821 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001822 uint16_t opcode;
1823 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001824 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001825 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001826 bool operator==(const ExprMapKeyType& that) const {
1827 return this->opcode == that.opcode &&
1828 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001829 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001830 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001831 }
1832 bool operator<(const ExprMapKeyType & that) const {
1833 return this->opcode < that.opcode ||
1834 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1835 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001836 this->operands < that.operands) ||
1837 (this->opcode == that.opcode && this->predicate == that.predicate &&
1838 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001839 }
1840
1841 bool operator!=(const ExprMapKeyType& that) const {
1842 return !(*this == that);
1843 }
1844};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001845
Dan Gohmand78c4002008-05-13 00:00:25 +00001846}
1847
Chris Lattner189d19f2003-11-21 20:23:48 +00001848namespace llvm {
1849 template<>
1850 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001851 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1852 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001853 if (Instruction::isCast(V.opcode))
1854 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1855 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001856 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001857 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1858 if (V.opcode == Instruction::Select)
1859 return new SelectConstantExpr(V.operands[0], V.operands[1],
1860 V.operands[2]);
1861 if (V.opcode == Instruction::ExtractElement)
1862 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1863 if (V.opcode == Instruction::InsertElement)
1864 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1865 V.operands[2]);
1866 if (V.opcode == Instruction::ShuffleVector)
1867 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1868 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001869 if (V.opcode == Instruction::InsertValue)
1870 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1871 V.indices, Ty);
1872 if (V.opcode == Instruction::ExtractValue)
1873 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001874 if (V.opcode == Instruction::GetElementPtr) {
1875 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001876 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001877 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001878
Reid Spenceree3c9912006-12-04 05:19:50 +00001879 // The compare instructions are weird. We have to encode the predicate
1880 // value and it is combined with the instruction opcode by multiplying
1881 // the opcode by one hundred. We must decode this to get the predicate.
1882 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001883 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001884 V.operands[0], V.operands[1]);
1885 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001886 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1887 V.operands[0], V.operands[1]);
Torok Edwin56d06592009-07-11 20:10:48 +00001888 LLVM_UNREACHABLE("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001889 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001890 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001891 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001892
Chris Lattner189d19f2003-11-21 20:23:48 +00001893 template<>
1894 struct ConvertConstantType<ConstantExpr, Type> {
1895 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1896 Constant *New;
1897 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 case Instruction::Trunc:
1899 case Instruction::ZExt:
1900 case Instruction::SExt:
1901 case Instruction::FPTrunc:
1902 case Instruction::FPExt:
1903 case Instruction::UIToFP:
1904 case Instruction::SIToFP:
1905 case Instruction::FPToUI:
1906 case Instruction::FPToSI:
1907 case Instruction::PtrToInt:
1908 case Instruction::IntToPtr:
1909 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001910 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001911 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001912 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001913 case Instruction::Select:
1914 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1915 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001916 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001917 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001918 default:
1919 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001920 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001921 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001922 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001923 break;
1924 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001925 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001926 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001927 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001928 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001929 break;
1930 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001931
Chris Lattner189d19f2003-11-21 20:23:48 +00001932 assert(New != OldC && "Didn't replace constant??");
1933 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001934 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001935 }
1936 };
1937} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001938
1939
Chris Lattner3e650af2004-08-04 04:48:01 +00001940static ExprMapKeyType getValType(ConstantExpr *CE) {
1941 std::vector<Constant*> Operands;
1942 Operands.reserve(CE->getNumOperands());
1943 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1944 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001945 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001946 CE->isCompare() ? CE->getPredicate() : 0,
1947 CE->hasIndices() ?
1948 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001949}
1950
Chris Lattner69edc982006-09-28 00:35:06 +00001951static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1952 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001953
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001954/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001955/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001957 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001958 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001960 if (Constant *FC =
1961 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001962 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001963
Vikram S. Adve4c485332002-07-15 18:19:33 +00001964 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001965 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001966 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001967
Owen Anderson61794042009-06-17 20:10:08 +00001968 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001969 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001970}
Reid Spencerf37dc652006-12-05 19:14:13 +00001971
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001972Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973 Instruction::CastOps opc = Instruction::CastOps(oc);
1974 assert(Instruction::isCast(opc) && "opcode out of range");
1975 assert(C && Ty && "Null arguments to getCast");
1976 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1977
1978 switch (opc) {
1979 default:
Torok Edwin56d06592009-07-11 20:10:48 +00001980 LLVM_UNREACHABLE("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001982 case Instruction::Trunc: return getTrunc(C, Ty);
1983 case Instruction::ZExt: return getZExt(C, Ty);
1984 case Instruction::SExt: return getSExt(C, Ty);
1985 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1986 case Instruction::FPExt: return getFPExtend(C, Ty);
1987 case Instruction::UIToFP: return getUIToFP(C, Ty);
1988 case Instruction::SIToFP: return getSIToFP(C, Ty);
1989 case Instruction::FPToUI: return getFPToUI(C, Ty);
1990 case Instruction::FPToSI: return getFPToSI(C, Ty);
1991 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1992 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1993 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001994 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001996}
1997
Reid Spencer5c140882006-12-04 20:17:56 +00001998Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001999 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002000 return getCast(Instruction::BitCast, C, Ty);
2001 return getCast(Instruction::ZExt, C, Ty);
2002}
2003
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002004Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002005 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002006 return getCast(Instruction::BitCast, C, Ty);
2007 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00002008}
2009
2010Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002011 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002012 return getCast(Instruction::BitCast, C, Ty);
2013 return getCast(Instruction::Trunc, C, Ty);
2014}
2015
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002016Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00002017 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002018 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00002019
Chris Lattner03c49532007-01-15 02:27:26 +00002020 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002021 return getCast(Instruction::PtrToInt, S, Ty);
2022 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00002023}
2024
Reid Spencer56521c42006-12-12 00:51:07 +00002025Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
2026 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002027 assert(C->getType()->isIntOrIntVector() &&
2028 Ty->isIntOrIntVector() && "Invalid cast");
2029 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2030 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00002031 Instruction::CastOps opcode =
2032 (SrcBits == DstBits ? Instruction::BitCast :
2033 (SrcBits > DstBits ? Instruction::Trunc :
2034 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2035 return getCast(opcode, C, Ty);
2036}
2037
2038Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002039 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00002040 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002041 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2042 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00002043 if (SrcBits == DstBits)
2044 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00002045 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00002046 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00002047 return getCast(opcode, C, Ty);
2048}
2049
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002050Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002051#ifndef NDEBUG
2052 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2053 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2054#endif
2055 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2056 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
2057 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
2058 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 "SrcTy must be larger than DestTy for Trunc!");
2060
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002061 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062}
2063
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002064Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002065#ifndef NDEBUG
2066 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2067 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2068#endif
2069 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2070 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
2071 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
2072 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 "SrcTy must be smaller than DestTy for SExt!");
2074
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002075 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00002076}
2077
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002078Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002079#ifndef NDEBUG
2080 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2081 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2082#endif
2083 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2084 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
2085 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
2086 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002087 "SrcTy must be smaller than DestTy for ZExt!");
2088
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002089 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090}
2091
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002092Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002093#ifndef NDEBUG
2094 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2095 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2096#endif
2097 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2098 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2099 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002101 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102}
2103
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002104Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002105#ifndef NDEBUG
2106 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2107 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2108#endif
2109 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2110 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2111 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002113 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002114}
2115
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002116Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002117#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002118 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2119 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002120#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002121 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2122 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
2123 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002124 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002125}
2126
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002127Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002128#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002129 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2130 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002131#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002132 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2133 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002134 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002135 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002136}
2137
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002138Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002139#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002140 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2141 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002142#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002143 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2144 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2145 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002146 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002147}
2148
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002149Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002150#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002151 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2152 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002153#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002154 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2155 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2156 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002157 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158}
2159
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002160Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00002162 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002163 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164}
2165
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002166Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00002167 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002169 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170}
2171
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002172Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 // BitCast implies a no-op cast of type only. No bits change. However, you
2174 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002175#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 const Type *SrcTy = C->getType();
2177 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002178 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179
2180 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2181 // or nonptr->ptr). For all the other types, the cast is okay if source and
2182 // destination bit widths are identical.
2183 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2184 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002185#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002186 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002187
2188 // It is common to ask for a bitcast of a value to its own type, handle this
2189 // speedily.
2190 if (C->getType() == DstTy) return C;
2191
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002192 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002193}
2194
Chris Lattnerb50d1352003-10-05 00:17:43 +00002195Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002196 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002197 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002198 assert(Opcode >= Instruction::BinaryOpsBegin &&
2199 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002200 "Invalid opcode in binary constant expression");
2201 assert(C1->getType() == C2->getType() &&
2202 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002203
Reid Spencer542964f2007-01-11 18:21:29 +00002204 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00002205 if (Constant *FC = ConstantFoldBinaryInstruction(
2206 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00002207 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002208
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002209 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002210 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002211
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002212 // Implicitly locked.
2213 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002214}
2215
Reid Spencer266e42b2006-12-23 06:05:41 +00002216Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002217 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002218 switch (predicate) {
Torok Edwin56d06592009-07-11 20:10:48 +00002219 default: LLVM_UNREACHABLE("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002220 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2221 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2222 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2223 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2224 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2225 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002226 return getFCmp(predicate, C1, C2);
2227
Nate Begemanc96e2e42008-07-25 17:35:37 +00002228 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2229 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2230 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2231 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002232 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002233 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002234}
2235
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002236Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002237 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2238 if (C1->getType()->isFPOrFPVector()) {
2239 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2240 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2241 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2242 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002243#ifndef NDEBUG
2244 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002245 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002246 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002247 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002248 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002249 assert(C1->getType()->isIntOrIntVector() &&
2250 "Tried to create an integer operation on a non-integer type!");
2251 break;
2252 case Instruction::FAdd:
2253 case Instruction::FSub:
2254 case Instruction::FMul:
2255 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2256 assert(C1->getType()->isFPOrFPVector() &&
2257 "Tried to create a floating-point operation on a "
2258 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002259 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002260 case Instruction::UDiv:
2261 case Instruction::SDiv:
2262 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002263 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002264 "Tried to create an arithmetic operation on a non-arithmetic type!");
2265 break;
2266 case Instruction::FDiv:
2267 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002268 assert(C1->getType()->isFPOrFPVector() &&
2269 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002270 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002271 case Instruction::URem:
2272 case Instruction::SRem:
2273 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002274 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002275 "Tried to create an arithmetic operation on a non-arithmetic type!");
2276 break;
2277 case Instruction::FRem:
2278 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002279 assert(C1->getType()->isFPOrFPVector() &&
2280 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002281 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002282 case Instruction::And:
2283 case Instruction::Or:
2284 case Instruction::Xor:
2285 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002286 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002287 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002288 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002289 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002290 case Instruction::LShr:
2291 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002292 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002293 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002294 "Tried to create a shift operation on a non-integer type!");
2295 break;
2296 default:
2297 break;
2298 }
2299#endif
2300
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002301 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002302}
2303
Reid Spencer266e42b2006-12-23 06:05:41 +00002304Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002305 Constant *C1, Constant *C2) {
2306 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002307 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002308}
2309
Chris Lattner6e415c02004-03-12 05:54:04 +00002310Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002311 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002312 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002313
2314 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00002315 if (Constant *SC = ConstantFoldSelectInstruction(
2316 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00002317 return SC; // Fold common cases
2318
2319 std::vector<Constant*> argVec(3, C);
2320 argVec[1] = V1;
2321 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002322 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002323
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002324 // Implicitly locked.
2325 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002326}
2327
Chris Lattnerb50d1352003-10-05 00:17:43 +00002328Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002329 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002330 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002331 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2332 Idxs+NumIdx) ==
2333 cast<PointerType>(ReqTy)->getElementType() &&
2334 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002335
Owen Anderson53a52212009-07-13 04:09:18 +00002336 if (Constant *FC = ConstantFoldGetElementPtr(
2337 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002338 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002339
Chris Lattnerb50d1352003-10-05 00:17:43 +00002340 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002341 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002342 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002343 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002344 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002345 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002346 for (unsigned i = 0; i != NumIdx; ++i)
2347 ArgVec.push_back(cast<Constant>(Idxs[i]));
2348 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002349
2350 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002351 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002352}
2353
Chris Lattner302116a2007-01-31 04:40:28 +00002354Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002355 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002356 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002357 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002358 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002359 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002360 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002361 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002362}
2363
Chris Lattner302116a2007-01-31 04:40:28 +00002364Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002365 unsigned NumIdx) {
2366 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002367}
2368
Chris Lattner302116a2007-01-31 04:40:28 +00002369
Reid Spenceree3c9912006-12-04 05:19:50 +00002370Constant *
2371ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2372 assert(LHS->getType() == RHS->getType());
2373 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2374 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2375
Owen Anderson53a52212009-07-13 04:09:18 +00002376 if (Constant *FC = ConstantFoldCompareInstruction(
2377 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002378 return FC; // Fold a few common cases...
2379
2380 // Look up the constant in the table first to ensure uniqueness
2381 std::vector<Constant*> ArgVec;
2382 ArgVec.push_back(LHS);
2383 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002384 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002385 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002386
2387 // Implicitly locked.
2388 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002389}
2390
2391Constant *
2392ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2393 assert(LHS->getType() == RHS->getType());
2394 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2395
Owen Anderson53a52212009-07-13 04:09:18 +00002396 if (Constant *FC = ConstantFoldCompareInstruction(
2397 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002398 return FC; // Fold a few common cases...
2399
2400 // Look up the constant in the table first to ensure uniqueness
2401 std::vector<Constant*> ArgVec;
2402 ArgVec.push_back(LHS);
2403 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002404 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002405 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002406
2407 // Implicitly locked.
2408 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002409}
2410
Robert Bocchino23004482006-01-10 19:05:34 +00002411Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2412 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002413 if (Constant *FC = ConstantFoldExtractElementInstruction(
2414 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002415 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002416 // Look up the constant in the table first to ensure uniqueness
2417 std::vector<Constant*> ArgVec(1, Val);
2418 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002419 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002420
2421 // Implicitly locked.
2422 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002423}
2424
2425Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002426 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002427 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002428 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002429 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002430 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002431 Val, Idx);
2432}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002433
Robert Bocchinoca27f032006-01-17 20:07:22 +00002434Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2435 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002436 if (Constant *FC = ConstantFoldInsertElementInstruction(
2437 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002438 return FC; // Fold a few common cases...
2439 // Look up the constant in the table first to ensure uniqueness
2440 std::vector<Constant*> ArgVec(1, Val);
2441 ArgVec.push_back(Elt);
2442 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002443 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002444
2445 // Implicitly locked.
2446 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002447}
2448
2449Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2450 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002451 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002452 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002453 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002454 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002455 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002456 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002457 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002458}
2459
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002460Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2461 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002462 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2463 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002464 return FC; // Fold a few common cases...
2465 // Look up the constant in the table first to ensure uniqueness
2466 std::vector<Constant*> ArgVec(1, V1);
2467 ArgVec.push_back(V2);
2468 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002469 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002470
2471 // Implicitly locked.
2472 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002473}
2474
2475Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2476 Constant *Mask) {
2477 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2478 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002479
2480 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2481 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2482 const Type *ShufTy = VectorType::get(EltTy, NElts);
2483 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002484}
2485
Dan Gohman12fce772008-05-15 19:50:34 +00002486Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2487 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002488 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002489 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2490 Idxs+NumIdx) == Val->getType() &&
2491 "insertvalue indices invalid!");
2492 assert(Agg->getType() == ReqTy &&
2493 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002494 assert(Agg->getType()->isFirstClassType() &&
2495 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002496 Constant *FC = ConstantFoldInsertValueInstruction(
2497 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002498 assert(FC && "InsertValue constant expr couldn't be folded!");
2499 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002500}
2501
2502Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002503 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002504 assert(Agg->getType()->isFirstClassType() &&
2505 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002506
Dan Gohman0752bff2008-05-23 00:36:11 +00002507 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002508#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002509 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002510 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002511#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002512 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002513 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2514}
2515
2516Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002517 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002518 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2519 Idxs+NumIdx) == ReqTy &&
2520 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002521 assert(Agg->getType()->isFirstClassType() &&
2522 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002523 Constant *FC = ConstantFoldExtractValueInstruction(
2524 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002525 assert(FC && "ExtractValue constant expr couldn't be folded!");
2526 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002527}
2528
2529Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002530 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002531 assert(Agg->getType()->isFirstClassType() &&
2532 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002533
2534 const Type *ReqTy =
2535 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2536 assert(ReqTy && "extractvalue indices invalid!");
2537 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2538}
2539
Vikram S. Adve4c485332002-07-15 18:19:33 +00002540// destroyConstant - Remove the constant from the constant table...
2541//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002542void ConstantExpr::destroyConstant() {
2543 // Implicitly locked.
2544 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002545 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002546}
2547
Chris Lattner3cd8c562002-07-30 18:54:25 +00002548const char *ConstantExpr::getOpcodeName() const {
2549 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002550}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002551
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002552//===----------------------------------------------------------------------===//
2553// replaceUsesOfWithOnConstant implementations
2554
Chris Lattner913849b2007-08-21 00:55:23 +00002555/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2556/// 'From' to be uses of 'To'. This must update the uniquing data structures
2557/// etc.
2558///
2559/// Note that we intentionally replace all uses of From with To here. Consider
2560/// a large array that uses 'From' 1000 times. By handling this case all here,
2561/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2562/// single invocation handles all 1000 uses. Handling them one at a time would
2563/// work, but would be really slow because it would have to unique each updated
2564/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002565void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002566 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002567 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002568 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002569
Jim Laskeyc03caef2006-07-17 17:38:29 +00002570 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002571 Lookup.first.first = getType();
2572 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002573
Chris Lattnerb64419a2005-10-03 22:51:37 +00002574 std::vector<Constant*> &Values = Lookup.first.second;
2575 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002576
Chris Lattner8760ec72005-10-04 01:17:50 +00002577 // Fill values with the modified operands of the constant array. Also,
2578 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002579 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002580 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002581 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002582 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2583 Constant *Val = cast<Constant>(O->get());
2584 if (Val == From) {
2585 Val = ToC;
2586 ++NumUpdated;
2587 }
2588 Values.push_back(Val);
2589 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002590 } else {
2591 isAllZeros = true;
2592 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2593 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002594 if (Val == From) {
2595 Val = ToC;
2596 ++NumUpdated;
2597 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002598 Values.push_back(Val);
2599 if (isAllZeros) isAllZeros = Val->isNullValue();
2600 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002601 }
2602
Chris Lattnerb64419a2005-10-03 22:51:37 +00002603 Constant *Replacement = 0;
2604 if (isAllZeros) {
2605 Replacement = ConstantAggregateZero::get(getType());
2606 } else {
2607 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002608 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002609 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002610 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002611 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002612
2613 if (Exists) {
2614 Replacement = I->second;
2615 } else {
2616 // Okay, the new shape doesn't exist in the system yet. Instead of
2617 // creating a new constant array, inserting it, replaceallusesof'ing the
2618 // old with the new, then deleting the old... just update the current one
2619 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002620 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002621
Chris Lattner913849b2007-08-21 00:55:23 +00002622 // Update to the new value. Optimize for the case when we have a single
2623 // operand that we're changing, but handle bulk updates efficiently.
2624 if (NumUpdated == 1) {
2625 unsigned OperandToUpdate = U-OperandList;
2626 assert(getOperand(OperandToUpdate) == From &&
2627 "ReplaceAllUsesWith broken!");
2628 setOperand(OperandToUpdate, ToC);
2629 } else {
2630 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2631 if (getOperand(i) == From)
2632 setOperand(i, ToC);
2633 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002634 return;
2635 }
2636 }
2637
2638 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002639 assert(Replacement != this && "I didn't contain From!");
2640
Chris Lattner7a1450d2005-10-04 18:13:04 +00002641 // Everyone using this now uses the replacement.
2642 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002643
2644 // Delete the old constant!
2645 destroyConstant();
2646}
2647
2648void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002649 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002650 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002651 Constant *ToC = cast<Constant>(To);
2652
Chris Lattnerdff59112005-10-04 18:47:09 +00002653 unsigned OperandToUpdate = U-OperandList;
2654 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2655
Jim Laskeyc03caef2006-07-17 17:38:29 +00002656 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002657 Lookup.first.first = getType();
2658 Lookup.second = this;
2659 std::vector<Constant*> &Values = Lookup.first.second;
2660 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002661
Chris Lattnerdff59112005-10-04 18:47:09 +00002662
Chris Lattner8760ec72005-10-04 01:17:50 +00002663 // Fill values with the modified operands of the constant struct. Also,
2664 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002665 bool isAllZeros = false;
2666 if (!ToC->isNullValue()) {
2667 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2668 Values.push_back(cast<Constant>(O->get()));
2669 } else {
2670 isAllZeros = true;
2671 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2672 Constant *Val = cast<Constant>(O->get());
2673 Values.push_back(Val);
2674 if (isAllZeros) isAllZeros = Val->isNullValue();
2675 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002676 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002677 Values[OperandToUpdate] = ToC;
2678
Chris Lattner8760ec72005-10-04 01:17:50 +00002679 Constant *Replacement = 0;
2680 if (isAllZeros) {
2681 Replacement = ConstantAggregateZero::get(getType());
2682 } else {
2683 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002684 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002685 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002686 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002687 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002688
2689 if (Exists) {
2690 Replacement = I->second;
2691 } else {
2692 // Okay, the new shape doesn't exist in the system yet. Instead of
2693 // creating a new constant struct, inserting it, replaceallusesof'ing the
2694 // old with the new, then deleting the old... just update the current one
2695 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002696 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002697
Chris Lattnerdff59112005-10-04 18:47:09 +00002698 // Update to the new value.
2699 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002700 return;
2701 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002702 }
2703
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002704 assert(Replacement != this && "I didn't contain From!");
2705
Chris Lattner7a1450d2005-10-04 18:13:04 +00002706 // Everyone using this now uses the replacement.
2707 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002708
2709 // Delete the old constant!
2710 destroyConstant();
2711}
2712
Reid Spencerd84d35b2007-02-15 02:26:10 +00002713void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002714 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002715 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2716
2717 std::vector<Constant*> Values;
2718 Values.reserve(getNumOperands()); // Build replacement array...
2719 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2720 Constant *Val = getOperand(i);
2721 if (Val == From) Val = cast<Constant>(To);
2722 Values.push_back(Val);
2723 }
2724
Reid Spencerd84d35b2007-02-15 02:26:10 +00002725 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002726 assert(Replacement != this && "I didn't contain From!");
2727
Chris Lattner7a1450d2005-10-04 18:13:04 +00002728 // Everyone using this now uses the replacement.
2729 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002730
2731 // Delete the old constant!
2732 destroyConstant();
2733}
2734
2735void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002736 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002737 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2738 Constant *To = cast<Constant>(ToV);
2739
2740 Constant *Replacement = 0;
2741 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002742 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002743 Constant *Pointer = getOperand(0);
2744 Indices.reserve(getNumOperands()-1);
2745 if (Pointer == From) Pointer = To;
2746
2747 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2748 Constant *Val = getOperand(i);
2749 if (Val == From) Val = To;
2750 Indices.push_back(Val);
2751 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002752 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2753 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002754 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002755 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002756 if (Agg == From) Agg = To;
2757
Dan Gohman1ecaf452008-05-31 00:58:22 +00002758 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002759 Replacement = ConstantExpr::getExtractValue(Agg,
2760 &Indices[0], Indices.size());
2761 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002762 Constant *Agg = getOperand(0);
2763 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002764 if (Agg == From) Agg = To;
2765 if (Val == From) Val = To;
2766
Dan Gohman1ecaf452008-05-31 00:58:22 +00002767 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002768 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2769 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002770 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002771 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002772 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002773 } else if (getOpcode() == Instruction::Select) {
2774 Constant *C1 = getOperand(0);
2775 Constant *C2 = getOperand(1);
2776 Constant *C3 = getOperand(2);
2777 if (C1 == From) C1 = To;
2778 if (C2 == From) C2 = To;
2779 if (C3 == From) C3 = To;
2780 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002781 } else if (getOpcode() == Instruction::ExtractElement) {
2782 Constant *C1 = getOperand(0);
2783 Constant *C2 = getOperand(1);
2784 if (C1 == From) C1 = To;
2785 if (C2 == From) C2 = To;
2786 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002787 } else if (getOpcode() == Instruction::InsertElement) {
2788 Constant *C1 = getOperand(0);
2789 Constant *C2 = getOperand(1);
2790 Constant *C3 = getOperand(1);
2791 if (C1 == From) C1 = To;
2792 if (C2 == From) C2 = To;
2793 if (C3 == From) C3 = To;
2794 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2795 } else if (getOpcode() == Instruction::ShuffleVector) {
2796 Constant *C1 = getOperand(0);
2797 Constant *C2 = getOperand(1);
2798 Constant *C3 = getOperand(2);
2799 if (C1 == From) C1 = To;
2800 if (C2 == From) C2 = To;
2801 if (C3 == From) C3 = To;
2802 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002803 } else if (isCompare()) {
2804 Constant *C1 = getOperand(0);
2805 Constant *C2 = getOperand(1);
2806 if (C1 == From) C1 = To;
2807 if (C2 == From) C2 = To;
2808 if (getOpcode() == Instruction::ICmp)
2809 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002810 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002811 assert(getOpcode() == Instruction::FCmp);
2812 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002813 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002814 } else if (getNumOperands() == 2) {
2815 Constant *C1 = getOperand(0);
2816 Constant *C2 = getOperand(1);
2817 if (C1 == From) C1 = To;
2818 if (C2 == From) C2 = To;
2819 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2820 } else {
Torok Edwin56d06592009-07-11 20:10:48 +00002821 LLVM_UNREACHABLE("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002822 return;
2823 }
2824
2825 assert(Replacement != this && "I didn't contain From!");
2826
Chris Lattner7a1450d2005-10-04 18:13:04 +00002827 // Everyone using this now uses the replacement.
2828 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002829
2830 // Delete the old constant!
2831 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002832}
Nick Lewycky49f89192009-04-04 07:22:01 +00002833
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002834void MDNode::replaceElement(Value *From, Value *To) {
2835 SmallVector<Value*, 4> Values;
2836 Values.reserve(getNumElements()); // Build replacement array...
2837 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2838 Value *Val = getElement(i);
2839 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002840 Values.push_back(Val);
2841 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002842
2843 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002844 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002845
Nick Lewycky49f89192009-04-04 07:22:01 +00002846 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002847
Nick Lewycky49f89192009-04-04 07:22:01 +00002848 destroyConstant();
2849}