blob: 38b30c281eee9527487b03a0ed71794dc014a62d [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 Lattnerb1585a92002-08-13 17:50:20 +0000131// Static constructor to create a '0' constant of arbitrary type...
Owen Andersonf92b4322009-06-26 20:21:18 +0000132static const uint64_t zero[2] = {0, 0};
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000133Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000134 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000135 case Type::IntegerTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000136 return ConstantInt::get(Ty, 0);
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000137 case Type::FloatTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000138 return ConstantFP::get(APFloat(APInt(32, 0)));
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000139 case Type::DoubleTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000140 return ConstantFP::get(APFloat(APInt(64, 0)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000141 case Type::X86_FP80TyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000142 return ConstantFP::get(APFloat(APInt(80, 2, zero)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000143 case Type::FP128TyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000144 return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
Dale Johannesen98d3a082007-09-14 22:26:36 +0000145 case Type::PPC_FP128TyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000146 return ConstantFP::get(APFloat(APInt(128, 2, zero)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147 case Type::PointerTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000148 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000149 case Type::StructTyID:
150 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000151 case Type::VectorTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000152 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000154 // Function, Label, or Opaque type?
155 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000156 return 0;
157 }
158}
159
Chris Lattner72e39582007-06-15 06:10:53 +0000160Constant *Constant::getAllOnesValue(const Type *Ty) {
161 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
162 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
163 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
164}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000165
166// Static constructor to create an integral constant with all bits set
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000167ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000168 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000169 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000170 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000171}
172
Dan Gohman30978072007-05-24 14:36:04 +0000173/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000174/// has all its bits set to true.
175/// @brief Get the all ones value
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000176ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000177 std::vector<Constant*> Elts;
178 Elts.resize(Ty->getNumElements(),
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000179 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000180 assert(Elts[0] && "Not a vector integer type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000181 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000182}
183
184
Chris Lattner2105d662008-07-10 00:28:11 +0000185/// getVectorElements - This method, which is only valid on constant of vector
186/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000187/// This handles breaking down a vector undef into undef elements, etc. For
188/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattner2105d662008-07-10 00:28:11 +0000189void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
190 assert(isa<VectorType>(getType()) && "Not a vector constant!");
191
192 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
193 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
194 Elts.push_back(CV->getOperand(i));
195 return;
196 }
197
198 const VectorType *VT = cast<VectorType>(getType());
199 if (isa<ConstantAggregateZero>(this)) {
200 Elts.assign(VT->getNumElements(),
201 Constant::getNullValue(VT->getElementType()));
202 return;
203 }
204
Chris Lattnerc5098a22008-07-14 05:10:41 +0000205 if (isa<UndefValue>(this)) {
206 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
207 return;
208 }
209
210 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000211}
212
213
214
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000216// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217//===----------------------------------------------------------------------===//
218
Reid Spencerb31bffe2007-02-26 23:54:03 +0000219ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000220 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000221 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222}
223
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000224ConstantInt *ConstantInt::TheTrueVal = 0;
225ConstantInt *ConstantInt::TheFalseVal = 0;
226
227namespace llvm {
228 void CleanupTrueFalse(void *) {
229 ConstantInt::ResetTrueFalse();
230 }
231}
232
233static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
234
235ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
236 assert(TheTrueVal == 0 && TheFalseVal == 0);
237 TheTrueVal = get(Type::Int1Ty, 1);
238 TheFalseVal = get(Type::Int1Ty, 0);
239
240 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
241 TrueFalseCleanup.Register();
242
243 return WhichOne ? TheTrueVal : TheFalseVal;
244}
245
246
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000247namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000248 struct DenseMapAPIntKeyInfo {
249 struct KeyTy {
250 APInt val;
251 const Type* type;
252 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
253 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
254 bool operator==(const KeyTy& that) const {
255 return type == that.type && this->val == that.val;
256 }
257 bool operator!=(const KeyTy& that) const {
258 return !this->operator==(that);
259 }
260 };
261 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
262 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000263 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000264 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000265 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000266 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000267 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
268 return LHS == RHS;
269 }
Dale Johannesena719a602007-08-24 00:56:33 +0000270 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000271 };
272}
273
274
Reid Spencerb31bffe2007-02-26 23:54:03 +0000275typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
276 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000277static ManagedStatic<IntMapTy> IntConstants;
278
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000279ConstantInt *ConstantInt::get(const IntegerType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000280 uint64_t V, bool isSigned) {
281 return get(APInt(Ty->getBitWidth(), V, isSigned));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000282}
283
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000284Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000285 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
286
287 // For vectors, broadcast the value.
288 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
289 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000290 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000291
292 return C;
Reid Spencerb31bffe2007-02-26 23:54:03 +0000293}
294
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000295// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000296// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000297// operator== and operator!= to ensure that the DenseMap doesn't attempt to
298// compare APInt's of different widths, which would violate an APInt class
299// invariant which generates an assertion.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000300ConstantInt *ConstantInt::get(const APInt& V) {
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000301 // Get the corresponding integer type for the bit width of the value.
302 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000303 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000304 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000305
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000306 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000307 ConstantInt *&Slot = (*IntConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000308 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000309
Owen Andersond830eb82009-06-18 19:10:19 +0000310 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000311 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000312 ConstantInt *&NewSlot = (*IntConstants)[Key];
313 if (!Slot) {
314 NewSlot = new ConstantInt(ITy, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000315 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000316
317 return NewSlot;
318 } else {
319 return Slot;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000320 }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000321}
322
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000323Constant *ConstantInt::get(const Type *Ty, const APInt &V) {
324 ConstantInt *C = ConstantInt::get(V);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000325 assert(C->getType() == Ty->getScalarType() &&
326 "ConstantInt type doesn't match the type implied by its value!");
327
328 // For vectors, broadcast the value.
329 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
330 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000331 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000332
333 return C;
334}
335
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000336//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000337// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000338//===----------------------------------------------------------------------===//
339
Chris Lattner98bd9392008-04-09 06:38:30 +0000340static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
341 if (Ty == Type::FloatTy)
342 return &APFloat::IEEEsingle;
343 if (Ty == Type::DoubleTy)
344 return &APFloat::IEEEdouble;
345 if (Ty == Type::X86_FP80Ty)
346 return &APFloat::x87DoubleExtended;
347 else if (Ty == Type::FP128Ty)
348 return &APFloat::IEEEquad;
349
350 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
351 return &APFloat::PPCDoubleDouble;
352}
353
Dale Johannesend246b2c2007-08-30 00:23:21 +0000354ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
355 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000356 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
357 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000358}
359
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000360bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000361 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000362}
363
Dale Johannesen98d3a082007-09-14 22:26:36 +0000364ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
365 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
366 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000367 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000368}
369
Dale Johannesend246b2c2007-08-30 00:23:21 +0000370bool ConstantFP::isExactlyValue(const APFloat& V) const {
371 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000372}
373
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000374namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000375 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000376 struct KeyTy {
377 APFloat val;
378 KeyTy(const APFloat& V) : val(V){}
379 KeyTy(const KeyTy& that) : val(that.val) {}
380 bool operator==(const KeyTy& that) const {
381 return this->val.bitwiseIsEqual(that.val);
382 }
383 bool operator!=(const KeyTy& that) const {
384 return !this->operator==(that);
385 }
386 };
387 static inline KeyTy getEmptyKey() {
388 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000389 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000390 static inline KeyTy getTombstoneKey() {
391 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000392 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000393 static unsigned getHashValue(const KeyTy &Key) {
394 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000395 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000396 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
397 return LHS == RHS;
398 }
Dale Johannesena719a602007-08-24 00:56:33 +0000399 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000400 };
401}
402
403//---- ConstantFP::get() implementation...
404//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000405typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000406 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000407
Dale Johannesena719a602007-08-24 00:56:33 +0000408static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000409
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000410ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000411 DenseMapAPFloatKeyInfo::KeyTy Key(V);
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000412
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000413 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000414 ConstantFP *&Slot = (*FPConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000415 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000416
Owen Andersond830eb82009-06-18 19:10:19 +0000417 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000418 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000419 ConstantFP *&NewSlot = (*FPConstants)[Key];
420 if (!NewSlot) {
Owen Andersond830eb82009-06-18 19:10:19 +0000421 const Type *Ty;
422 if (&V.getSemantics() == &APFloat::IEEEsingle)
423 Ty = Type::FloatTy;
424 else if (&V.getSemantics() == &APFloat::IEEEdouble)
425 Ty = Type::DoubleTy;
426 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
427 Ty = Type::X86_FP80Ty;
428 else if (&V.getSemantics() == &APFloat::IEEEquad)
429 Ty = Type::FP128Ty;
430 else {
431 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
432 "Unknown FP format");
433 Ty = Type::PPC_FP128Ty;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000434 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000435 NewSlot = new ConstantFP(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000436 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000437
438 return NewSlot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000439 }
Owen Andersond830eb82009-06-18 19:10:19 +0000440
441 return Slot;
Dale Johannesend246b2c2007-08-30 00:23:21 +0000442}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000443
Chris Lattner98bd9392008-04-09 06:38:30 +0000444/// get() - This returns a constant fp for the specified value in the
445/// specified type. This should only be used for simple constant values like
446/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000447Constant *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000448 APFloat FV(V);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000449 bool ignored;
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000450 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
451 APFloat::rmNearestTiesToEven, &ignored);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000452 Constant *C = get(FV);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000453
454 // For vectors, broadcast the value.
455 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
456 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000457 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000458
459 return C;
Chris Lattner98bd9392008-04-09 06:38:30 +0000460}
461
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000462//===----------------------------------------------------------------------===//
463// ConstantXXX Classes
464//===----------------------------------------------------------------------===//
465
466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000468 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000469 : Constant(T, ConstantArrayVal,
470 OperandTraits<ConstantArray>::op_end(this) - V.size(),
471 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000472 assert(V.size() == T->getNumElements() &&
473 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000474 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000475 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
476 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000477 Constant *C = *I;
478 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000479 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000480 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000481 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000482 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000483 }
484}
485
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000486
Chris Lattner3462ae32001-12-03 22:26:30 +0000487ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000488 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000489 : Constant(T, ConstantStructVal,
490 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
491 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000492 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000493 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000494 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000495 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
496 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000497 Constant *C = *I;
498 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000499 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000500 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000501 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000502 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000503 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000504 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000505 }
506}
507
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000508
Reid Spencerd84d35b2007-02-15 02:26:10 +0000509ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000510 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 : Constant(T, ConstantVectorVal,
512 OperandTraits<ConstantVector>::op_end(this) - V.size(),
513 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000514 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000515 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
516 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000517 Constant *C = *I;
518 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000519 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000520 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000521 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000522 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000523 }
524}
525
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000526
Gabor Greiff6caff662008-05-10 08:32:32 +0000527namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000528// We declare several classes private to this file, so use an anonymous
529// namespace
530namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000531
Gordon Henriksen14a55692007-12-10 02:14:30 +0000532/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
533/// behind the scenes to implement unary constant exprs.
534class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000535 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000536public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000537 // allocate space for exactly one operand
538 void *operator new(size_t s) {
539 return User::operator new(s, 1);
540 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000541 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000542 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
543 Op<0>() = C;
544 }
545 /// Transparently provide more efficient getOperand methods.
546 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000547};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000548
Gordon Henriksen14a55692007-12-10 02:14:30 +0000549/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
550/// behind the scenes to implement binary constant exprs.
551class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000552 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000553public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000554 // allocate space for exactly two operands
555 void *operator new(size_t s) {
556 return User::operator new(s, 2);
557 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000558 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000559 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000560 Op<0>() = C1;
561 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000562 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000563 /// Transparently provide more efficient getOperand methods.
564 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000565};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000566
Gordon Henriksen14a55692007-12-10 02:14:30 +0000567/// SelectConstantExpr - This class is private to Constants.cpp, and is used
568/// behind the scenes to implement select constant exprs.
569class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000570 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000571public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000572 // allocate space for exactly three operands
573 void *operator new(size_t s) {
574 return User::operator new(s, 3);
575 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000576 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000577 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000578 Op<0>() = C1;
579 Op<1>() = C2;
580 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000581 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000582 /// Transparently provide more efficient getOperand methods.
583 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000584};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000585
Gordon Henriksen14a55692007-12-10 02:14:30 +0000586/// ExtractElementConstantExpr - This class is private to
587/// Constants.cpp, and is used behind the scenes to implement
588/// extractelement constant exprs.
589class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000590 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000591public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000592 // allocate space for exactly two operands
593 void *operator new(size_t s) {
594 return User::operator new(s, 2);
595 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000596 ExtractElementConstantExpr(Constant *C1, Constant *C2)
597 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000598 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000599 Op<0>() = C1;
600 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000601 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000602 /// Transparently provide more efficient getOperand methods.
603 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000604};
Robert Bocchino23004482006-01-10 19:05:34 +0000605
Gordon Henriksen14a55692007-12-10 02:14:30 +0000606/// InsertElementConstantExpr - This class is private to
607/// Constants.cpp, and is used behind the scenes to implement
608/// insertelement constant exprs.
609class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000610 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000611public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000612 // allocate space for exactly three operands
613 void *operator new(size_t s) {
614 return User::operator new(s, 3);
615 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000616 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
617 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000618 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000619 Op<0>() = C1;
620 Op<1>() = C2;
621 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000622 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000623 /// Transparently provide more efficient getOperand methods.
624 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000625};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000626
Gordon Henriksen14a55692007-12-10 02:14:30 +0000627/// ShuffleVectorConstantExpr - This class is private to
628/// Constants.cpp, and is used behind the scenes to implement
629/// shufflevector constant exprs.
630class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000631 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000632public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000633 // allocate space for exactly three operands
634 void *operator new(size_t s) {
635 return User::operator new(s, 3);
636 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000637 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000638 : ConstantExpr(VectorType::get(
639 cast<VectorType>(C1->getType())->getElementType(),
640 cast<VectorType>(C3->getType())->getNumElements()),
641 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000642 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000643 Op<0>() = C1;
644 Op<1>() = C2;
645 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000646 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000647 /// Transparently provide more efficient getOperand methods.
648 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000649};
650
Dan Gohman12fce772008-05-15 19:50:34 +0000651/// ExtractValueConstantExpr - This class is private to
652/// Constants.cpp, and is used behind the scenes to implement
653/// extractvalue constant exprs.
654class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000655 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000656public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000657 // allocate space for exactly one operand
658 void *operator new(size_t s) {
659 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000660 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000661 ExtractValueConstantExpr(Constant *Agg,
662 const SmallVector<unsigned, 4> &IdxList,
663 const Type *DestTy)
664 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
665 Indices(IdxList) {
666 Op<0>() = Agg;
667 }
668
Dan Gohman7bb04502008-05-31 19:09:08 +0000669 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000670 const SmallVector<unsigned, 4> Indices;
671
Dan Gohman12fce772008-05-15 19:50:34 +0000672 /// Transparently provide more efficient getOperand methods.
673 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
674};
675
676/// InsertValueConstantExpr - This class is private to
677/// Constants.cpp, and is used behind the scenes to implement
678/// insertvalue constant exprs.
679class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000680 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000681public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000682 // allocate space for exactly one operand
683 void *operator new(size_t s) {
684 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000685 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000686 InsertValueConstantExpr(Constant *Agg, Constant *Val,
687 const SmallVector<unsigned, 4> &IdxList,
688 const Type *DestTy)
689 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
690 Indices(IdxList) {
691 Op<0>() = Agg;
692 Op<1>() = Val;
693 }
694
Dan Gohman7bb04502008-05-31 19:09:08 +0000695 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000696 const SmallVector<unsigned, 4> Indices;
697
Dan Gohman12fce772008-05-15 19:50:34 +0000698 /// Transparently provide more efficient getOperand methods.
699 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
700};
701
702
Gordon Henriksen14a55692007-12-10 02:14:30 +0000703/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
704/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000705class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000706 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000707 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000708public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000709 static GetElementPtrConstantExpr *Create(Constant *C,
710 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000711 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000712 return new(IdxList.size() + 1)
713 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000714 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000715 /// Transparently provide more efficient getOperand methods.
716 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000717};
718
719// CompareConstantExpr - This class is private to Constants.cpp, and is used
720// behind the scenes to implement ICmp and FCmp constant expressions. This is
721// needed in order to store the predicate value for these instructions.
722struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000723 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
724 // allocate space for exactly two operands
725 void *operator new(size_t s) {
726 return User::operator new(s, 2);
727 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000728 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000729 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
730 unsigned short pred, Constant* LHS, Constant* RHS)
731 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000732 Op<0>() = LHS;
733 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000734 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000735 /// Transparently provide more efficient getOperand methods.
736 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000737};
738
739} // end anonymous namespace
740
Gabor Greiff6caff662008-05-10 08:32:32 +0000741template <>
742struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
743};
744DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
745
746template <>
747struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
748};
749DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
750
751template <>
752struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
753};
754DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
755
756template <>
757struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
758};
759DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
760
761template <>
762struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
763};
764DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
765
766template <>
767struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
768};
769DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
770
Dan Gohman12fce772008-05-15 19:50:34 +0000771template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000772struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000773};
Dan Gohman12fce772008-05-15 19:50:34 +0000774DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
775
776template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000777struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000778};
Dan Gohman12fce772008-05-15 19:50:34 +0000779DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
780
Gabor Greiff6caff662008-05-10 08:32:32 +0000781template <>
782struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
783};
784
785GetElementPtrConstantExpr::GetElementPtrConstantExpr
786 (Constant *C,
787 const std::vector<Constant*> &IdxList,
788 const Type *DestTy)
789 : ConstantExpr(DestTy, Instruction::GetElementPtr,
790 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
791 - (IdxList.size()+1),
792 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000793 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000794 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000795 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000796}
797
798DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
799
800
801template <>
802struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
803};
804DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
805
806
807} // End llvm namespace
808
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000809
810// Utility function for determining if a ConstantExpr is a CastOp or not. This
811// can't be inline because we don't want to #include Instruction.h into
812// Constant.h
813bool ConstantExpr::isCast() const {
814 return Instruction::isCast(getOpcode());
815}
816
Reid Spenceree3c9912006-12-04 05:19:50 +0000817bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000818 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000819}
820
Dan Gohman1ecaf452008-05-31 00:58:22 +0000821bool ConstantExpr::hasIndices() const {
822 return getOpcode() == Instruction::ExtractValue ||
823 getOpcode() == Instruction::InsertValue;
824}
825
826const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
827 if (const ExtractValueConstantExpr *EVCE =
828 dyn_cast<ExtractValueConstantExpr>(this))
829 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000830
831 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000832}
833
Chris Lattner817175f2004-03-29 02:37:53 +0000834/// ConstantExpr::get* - Return some common constants without having to
835/// specify the full Instruction::OPCODE identifier.
836///
837Constant *ConstantExpr::getNeg(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000838 // API compatibility: Adjust integer opcodes to floating-point opcodes.
839 if (C->getType()->isFPOrFPVector())
840 return getFNeg(C);
841 assert(C->getType()->isIntOrIntVector() &&
842 "Cannot NEG a nonintegral value!");
Reid Spencer2eadb532007-01-21 00:29:26 +0000843 return get(Instruction::Sub,
844 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
845 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000846}
Dan Gohmana5b96452009-06-04 22:49:04 +0000847Constant *ConstantExpr::getFNeg(Constant *C) {
848 assert(C->getType()->isFPOrFPVector() &&
849 "Cannot FNEG a non-floating-point value!");
850 return get(Instruction::FSub,
851 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
852 C);
853}
Chris Lattner817175f2004-03-29 02:37:53 +0000854Constant *ConstantExpr::getNot(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000855 assert(C->getType()->isIntOrIntVector() &&
856 "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000857 return get(Instruction::Xor, C,
Dale Johannesen47a5ef32008-08-21 21:20:09 +0000858 Constant::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000859}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000860Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
861 return get(Instruction::Add, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000862}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000863Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
864 return get(Instruction::FAdd, C1, C2);
Dan Gohmana5b96452009-06-04 22:49:04 +0000865}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000866Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
867 return get(Instruction::Sub, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000868}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000869Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
870 return get(Instruction::FSub, C1, C2);
Dan Gohmana5b96452009-06-04 22:49:04 +0000871}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000872Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
873 return get(Instruction::Mul, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000874}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000875Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
876 return get(Instruction::FMul, C1, C2);
Dan Gohmana5b96452009-06-04 22:49:04 +0000877}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000878Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
879 return get(Instruction::UDiv, C1, C2);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000880}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000881Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
882 return get(Instruction::SDiv, C1, C2);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000883}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000884Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
885 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000886}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000887Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
888 return get(Instruction::URem, C1, C2);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000889}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000890Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
891 return get(Instruction::SRem, C1, C2);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000892}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000893Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
894 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000895}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000896Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
897 return get(Instruction::And, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000898}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000899Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
900 return get(Instruction::Or, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000901}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000902Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
903 return get(Instruction::Xor, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000904}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000905unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000906 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000907 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000908 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000909}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000910Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
911 return get(Instruction::Shl, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000912}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000913Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
914 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000915}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000916Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
917 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000918}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000919
Chris Lattner7c1018a2006-07-14 19:37:40 +0000920/// getWithOperandReplaced - Return a constant expression identical to this
921/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000922Constant *
923ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000924 assert(OpNo < getNumOperands() && "Operand num is out of range!");
925 assert(Op->getType() == getOperand(OpNo)->getType() &&
926 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000927 if (getOperand(OpNo) == Op)
928 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000929
Chris Lattner227816342006-07-14 22:20:01 +0000930 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000931 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000932 case Instruction::Trunc:
933 case Instruction::ZExt:
934 case Instruction::SExt:
935 case Instruction::FPTrunc:
936 case Instruction::FPExt:
937 case Instruction::UIToFP:
938 case Instruction::SIToFP:
939 case Instruction::FPToUI:
940 case Instruction::FPToSI:
941 case Instruction::PtrToInt:
942 case Instruction::IntToPtr:
943 case Instruction::BitCast:
944 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000945 case Instruction::Select:
946 Op0 = (OpNo == 0) ? Op : getOperand(0);
947 Op1 = (OpNo == 1) ? Op : getOperand(1);
948 Op2 = (OpNo == 2) ? Op : getOperand(2);
949 return ConstantExpr::getSelect(Op0, Op1, Op2);
950 case Instruction::InsertElement:
951 Op0 = (OpNo == 0) ? Op : getOperand(0);
952 Op1 = (OpNo == 1) ? Op : getOperand(1);
953 Op2 = (OpNo == 2) ? Op : getOperand(2);
954 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
955 case Instruction::ExtractElement:
956 Op0 = (OpNo == 0) ? Op : getOperand(0);
957 Op1 = (OpNo == 1) ? Op : getOperand(1);
958 return ConstantExpr::getExtractElement(Op0, Op1);
959 case Instruction::ShuffleVector:
960 Op0 = (OpNo == 0) ? Op : getOperand(0);
961 Op1 = (OpNo == 1) ? Op : getOperand(1);
962 Op2 = (OpNo == 2) ? Op : getOperand(2);
963 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000964 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000965 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000966 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000967 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000968 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000969 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000970 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000971 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000972 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000973 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000974 default:
975 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000976 Op0 = (OpNo == 0) ? Op : getOperand(0);
977 Op1 = (OpNo == 1) ? Op : getOperand(1);
978 return ConstantExpr::get(getOpcode(), Op0, Op1);
979 }
980}
981
982/// getWithOperands - This returns the current constant expression with the
983/// operands replaced with the specified values. The specified operands must
984/// match count and type with the existing ones.
985Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000986getWithOperands(Constant* const *Ops, unsigned NumOps) const {
987 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000988 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000989 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000990 assert(Ops[i]->getType() == getOperand(i)->getType() &&
991 "Operand type mismatch!");
992 AnyChange |= Ops[i] != getOperand(i);
993 }
994 if (!AnyChange) // No operands changed, return self.
995 return const_cast<ConstantExpr*>(this);
996
997 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000998 case Instruction::Trunc:
999 case Instruction::ZExt:
1000 case Instruction::SExt:
1001 case Instruction::FPTrunc:
1002 case Instruction::FPExt:
1003 case Instruction::UIToFP:
1004 case Instruction::SIToFP:
1005 case Instruction::FPToUI:
1006 case Instruction::FPToSI:
1007 case Instruction::PtrToInt:
1008 case Instruction::IntToPtr:
1009 case Instruction::BitCast:
1010 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +00001011 case Instruction::Select:
1012 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1013 case Instruction::InsertElement:
1014 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1015 case Instruction::ExtractElement:
1016 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1017 case Instruction::ShuffleVector:
1018 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001019 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +00001020 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +00001021 case Instruction::ICmp:
1022 case Instruction::FCmp:
1023 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +00001024 default:
1025 assert(getNumOperands() == 2 && "Must be binary operator?");
1026 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001027 }
1028}
1029
Chris Lattner2f7c9632001-06-06 20:29:01 +00001030
1031//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001032// isValueValidForType implementations
1033
Reid Spencere7334722006-12-19 01:28:19 +00001034bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001035 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001036 if (Ty == Type::Int1Ty)
1037 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001038 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001039 return true; // always true, has to fit in largest type
1040 uint64_t Max = (1ll << NumBits) - 1;
1041 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +00001042}
1043
Reid Spencere0fc4df2006-10-20 07:07:24 +00001044bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001045 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001046 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +00001047 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001048 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001049 return true; // always true, has to fit in largest type
1050 int64_t Min = -(1ll << (NumBits-1));
1051 int64_t Max = (1ll << (NumBits-1)) - 1;
1052 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001053}
1054
Dale Johannesend246b2c2007-08-30 00:23:21 +00001055bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1056 // convert modifies in place, so make a copy.
1057 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001058 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001059 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001060 default:
1061 return false; // These can't be represented as floating point!
1062
Dale Johannesend246b2c2007-08-30 00:23:21 +00001063 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001064 case Type::FloatTyID: {
1065 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1066 return true;
1067 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1068 return !losesInfo;
1069 }
1070 case Type::DoubleTyID: {
1071 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1072 &Val2.getSemantics() == &APFloat::IEEEdouble)
1073 return true;
1074 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1075 return !losesInfo;
1076 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001077 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001078 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1079 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1080 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001081 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001082 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1083 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1084 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001085 case Type::PPC_FP128TyID:
1086 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1087 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1088 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001089 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001090}
Chris Lattner9655e542001-07-20 19:16:02 +00001091
Chris Lattner49d855c2001-09-07 16:46:31 +00001092//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001093// Factory Function Implementation
1094
Gabor Greiff6caff662008-05-10 08:32:32 +00001095
1096// The number of operands for each ConstantCreator::create method is
1097// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001098// ConstantCreator - A class that is used to create constants by
1099// ValueMap*. This class should be partially specialized if there is
1100// something strange that needs to be done to interface to the ctor for the
1101// constant.
1102//
Chris Lattner189d19f2003-11-21 20:23:48 +00001103namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001104 template<class ValType>
1105 struct ConstantTraits;
1106
1107 template<typename T, typename Alloc>
1108 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1109 static unsigned uses(const std::vector<T, Alloc>& v) {
1110 return v.size();
1111 }
1112 };
1113
Chris Lattner189d19f2003-11-21 20:23:48 +00001114 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001115 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001116 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001117 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001118 }
1119 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001120
Chris Lattner189d19f2003-11-21 20:23:48 +00001121 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001122 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001123 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwin69208f02009-07-12 07:15:17 +00001124 LLVM_UNREACHABLE("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +00001125 }
1126 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001127
Chris Lattner935aa922005-10-04 17:48:46 +00001128 template<class ValType, class TypeClass, class ConstantClass,
1129 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001130 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001131 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001132 typedef std::pair<const Type*, ValType> MapKey;
1133 typedef std::map<MapKey, Constant *> MapTy;
1134 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1135 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001136 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001137 /// Map - This is the main map from the element descriptor to the Constants.
1138 /// This is the primary way we avoid creating two of the same shape
1139 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001140 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001141
1142 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1143 /// from the constants to their element in Map. This is important for
1144 /// removal of constants from the array, which would otherwise have to scan
1145 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001146 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001147
Jim Laskeyc03caef2006-07-17 17:38:29 +00001148 /// AbstractTypeMap - Map for abstract type constants.
1149 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001150 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001151
1152 /// ValueMapLock - Mutex for this map.
1153 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +00001154
Chris Lattner98fa07b2003-05-23 20:03:32 +00001155 public:
Owen Anderson61794042009-06-17 20:10:08 +00001156 // NOTE: This function is not locked. It is the caller's responsibility
1157 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001158 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001159
1160 /// InsertOrGetItem - Return an iterator for the specified element.
1161 /// If the element exists in the map, the returned iterator points to the
1162 /// entry and Exists=true. If not, the iterator points to the newly
1163 /// inserted entry and returns Exists=false. Newly inserted entries have
1164 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +00001165 /// NOTE: This function is not locked. It is the caller's responsibility
1166 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001167 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1168 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001169 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001170 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001171 Exists = !IP.second;
1172 return IP.first;
1173 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001174
Chris Lattner935aa922005-10-04 17:48:46 +00001175private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001176 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001177 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001178 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001179 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1180 IMI->second->second == CP &&
1181 "InverseMap corrupt!");
1182 return IMI->second;
1183 }
1184
Jim Laskeyc03caef2006-07-17 17:38:29 +00001185 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001186 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1187 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001188 if (I == Map.end() || I->second != CP) {
1189 // FIXME: This should not use a linear scan. If this gets to be a
1190 // performance problem, someone should look at this.
1191 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1192 /* empty */;
1193 }
Chris Lattner935aa922005-10-04 17:48:46 +00001194 return I;
1195 }
Owen Andersonf89c38c2009-06-17 20:43:39 +00001196
1197 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
1198 typename MapTy::iterator I) {
1199 ConstantClass* Result =
1200 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1201
1202 assert(Result->getType() == Ty && "Type specified is not correct!");
1203 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1204
1205 if (HasLargeKey) // Remember the reverse mapping if needed.
1206 InverseMap.insert(std::make_pair(Result, I));
1207
1208 // If the type of the constant is abstract, make sure that an entry
1209 // exists for it in the AbstractTypeMap.
1210 if (Ty->isAbstract()) {
1211 typename AbstractTypeMapTy::iterator TI =
1212 AbstractTypeMap.find(Ty);
1213
1214 if (TI == AbstractTypeMap.end()) {
1215 // Add ourselves to the ATU list of the type.
1216 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1217
1218 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1219 }
1220 }
1221
1222 return Result;
1223 }
Chris Lattner935aa922005-10-04 17:48:46 +00001224public:
1225
Chris Lattnerb64419a2005-10-03 22:51:37 +00001226 /// getOrCreate - Return the specified constant from the map, creating it if
1227 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001228 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001229 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +00001230 MapKey Lookup(Ty, V);
1231 ConstantClass* Result = 0;
1232
1233 typename MapTy::iterator I = Map.find(Lookup);
1234 // Is it in the map?
1235 if (I != Map.end())
1236 Result = static_cast<ConstantClass *>(I->second);
1237
1238 if (!Result) {
1239 // If no preexisting value, create one now...
1240 Result = Create(Ty, V, I);
1241 }
1242
1243 return Result;
1244 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001245
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001246 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001247 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001248 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001249 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001250 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001251
Chris Lattner935aa922005-10-04 17:48:46 +00001252 if (HasLargeKey) // Remember the reverse mapping if needed.
1253 InverseMap.erase(CP);
1254
Chris Lattnerb50d1352003-10-05 00:17:43 +00001255 // Now that we found the entry, make sure this isn't the entry that
1256 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001257 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001258 if (Ty->isAbstract()) {
1259 assert(AbstractTypeMap.count(Ty) &&
1260 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001261 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001262 if (ATMEntryIt == I) {
1263 // Yes, we are removing the representative entry for this type.
1264 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001265 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001266
Chris Lattnerb50d1352003-10-05 00:17:43 +00001267 // First check the entry before this one...
1268 if (TmpIt != Map.begin()) {
1269 --TmpIt;
1270 if (TmpIt->first.first != Ty) // Not the same type, move back...
1271 ++TmpIt;
1272 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001273
Chris Lattnerb50d1352003-10-05 00:17:43 +00001274 // If we didn't find the same type, try to move forward...
1275 if (TmpIt == ATMEntryIt) {
1276 ++TmpIt;
1277 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1278 --TmpIt; // No entry afterwards with the same type
1279 }
1280
1281 // If there is another entry in the map of the same abstract type,
1282 // update the AbstractTypeMap entry now.
1283 if (TmpIt != ATMEntryIt) {
1284 ATMEntryIt = TmpIt;
1285 } else {
1286 // Otherwise, we are removing the last instance of this type
1287 // from the table. Remove from the ATM, and from user list.
1288 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1289 AbstractTypeMap.erase(Ty);
1290 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001291 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001292 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001293
Chris Lattnerb50d1352003-10-05 00:17:43 +00001294 Map.erase(I);
1295 }
1296
Chris Lattner3b793c62005-10-04 21:35:50 +00001297
1298 /// MoveConstantToNewSlot - If we are about to change C to be the element
1299 /// specified by I, update our internal data structures to reflect this
1300 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +00001301 /// NOTE: This function is not locked. It is the responsibility of the
1302 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001303 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001304 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001305 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001306 assert(OldI != Map.end() && "Constant not found in constant table!");
1307 assert(OldI->second == C && "Didn't find correct element?");
1308
1309 // If this constant is the representative element for its abstract type,
1310 // update the AbstractTypeMap so that the representative element is I.
1311 if (C->getType()->isAbstract()) {
1312 typename AbstractTypeMapTy::iterator ATI =
1313 AbstractTypeMap.find(C->getType());
1314 assert(ATI != AbstractTypeMap.end() &&
1315 "Abstract type not in AbstractTypeMap?");
1316 if (ATI->second == OldI)
1317 ATI->second = I;
1318 }
1319
1320 // Remove the old entry from the map.
1321 Map.erase(OldI);
1322
1323 // Update the inverse map so that we know that this constant is now
1324 // located at descriptor I.
1325 if (HasLargeKey) {
1326 assert(I->second == C && "Bad inversemap entry!");
1327 InverseMap[C] = I;
1328 }
1329 }
1330
Chris Lattnerb50d1352003-10-05 00:17:43 +00001331 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001332 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001333 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001334 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001335
1336 assert(I != AbstractTypeMap.end() &&
1337 "Abstract type not in AbstractTypeMap?");
1338
1339 // Convert a constant at a time until the last one is gone. The last one
1340 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1341 // eliminated eventually.
1342 do {
1343 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001344 TypeClass>::convert(
1345 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001346 cast<TypeClass>(NewTy));
1347
Jim Laskeyc03caef2006-07-17 17:38:29 +00001348 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001349 } while (I != AbstractTypeMap.end());
1350 }
1351
1352 // If the type became concrete without being refined to any other existing
1353 // type, we just remove ourselves from the ATU list.
1354 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001355 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001356 }
1357
1358 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001359 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001360 }
1361 };
1362}
1363
Chris Lattnera84df0a22006-09-28 23:36:21 +00001364
Chris Lattner28173502007-02-20 06:11:36 +00001365
Chris Lattner9fba3da2004-02-15 05:53:04 +00001366//---- ConstantAggregateZero::get() implementation...
1367//
1368namespace llvm {
1369 // ConstantAggregateZero does not take extra "value" argument...
1370 template<class ValType>
1371 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1372 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1373 return new ConstantAggregateZero(Ty);
1374 }
1375 };
1376
1377 template<>
1378 struct ConvertConstantType<ConstantAggregateZero, Type> {
1379 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1380 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001381 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001382 assert(New != OldC && "Didn't replace constant??");
1383 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001384 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001385 }
1386 };
1387}
1388
Chris Lattner69edc982006-09-28 00:35:06 +00001389static ManagedStatic<ValueMap<char, Type,
1390 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001391
Chris Lattner3e650af2004-08-04 04:48:01 +00001392static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1393
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001394ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001395 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001396 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001397
1398 // Implicitly locked.
1399 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001400}
1401
Dan Gohman92b551b2009-03-03 02:55:14 +00001402/// destroyConstant - Remove the constant from the constant table...
1403///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001404void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001405 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001406 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001407 destroyConstantImpl();
1408}
1409
Chris Lattner3462ae32001-12-03 22:26:30 +00001410//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001411//
Chris Lattner189d19f2003-11-21 20:23:48 +00001412namespace llvm {
1413 template<>
1414 struct ConvertConstantType<ConstantArray, ArrayType> {
1415 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1416 // Make everyone now use a constant of the new type...
1417 std::vector<Constant*> C;
1418 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1419 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001420 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001421 assert(New != OldC && "Didn't replace constant??");
1422 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001423 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001424 }
1425 };
1426}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001427
Chris Lattner3e650af2004-08-04 04:48:01 +00001428static std::vector<Constant*> getValType(ConstantArray *CA) {
1429 std::vector<Constant*> Elements;
1430 Elements.reserve(CA->getNumOperands());
1431 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1432 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1433 return Elements;
1434}
1435
Chris Lattnerb64419a2005-10-03 22:51:37 +00001436typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001437 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001438static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001439
Chris Lattner015e8212004-02-15 04:14:47 +00001440Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001441 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001442 // If this is an all-zero array, return a ConstantAggregateZero object
1443 if (!V.empty()) {
1444 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001445 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001446 // Implicitly locked.
1447 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001448 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001449 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001450 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001451 // Implicitly locked.
1452 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001453 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001454 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001455
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001456 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001457}
1458
Dan Gohman92b551b2009-03-03 02:55:14 +00001459/// destroyConstant - Remove the constant from the constant table...
1460///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001461void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001462 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001463 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001464 destroyConstantImpl();
1465}
1466
Reid Spencer6f614532006-05-30 08:23:18 +00001467/// ConstantArray::get(const string&) - Return an array that is initialized to
1468/// contain the specified string. If length is zero then a null terminator is
1469/// added to the specified string so that it may be used in a natural way.
1470/// Otherwise, the length parameter specifies how much of the string to use
1471/// and it won't be null terminated.
1472///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001473Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001474 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001475 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001476 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001477
1478 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001479 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001480 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001481 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001482
Reid Spencer8d9336d2006-12-31 05:26:44 +00001483 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001484 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001485}
1486
Reid Spencer2546b762007-01-26 07:37:34 +00001487/// isString - This method returns true if the array is an array of i8, and
1488/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001489bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001490 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001491 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001492 return false;
1493 // Check the elements to make sure they are all integers, not constant
1494 // expressions.
1495 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1496 if (!isa<ConstantInt>(getOperand(i)))
1497 return false;
1498 return true;
1499}
1500
Evan Cheng3763c5b2006-10-26 19:15:05 +00001501/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001502/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001503/// null bytes except its terminator.
1504bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001505 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001506 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001507 return false;
1508 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1509 // Last element must be a null.
1510 if (getOperand(getNumOperands()-1) != Zero)
1511 return false;
1512 // Other elements must be non-null integers.
1513 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1514 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001515 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001516 if (getOperand(i) == Zero)
1517 return false;
1518 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001519 return true;
1520}
1521
1522
Dan Gohman92b551b2009-03-03 02:55:14 +00001523/// getAsString - If the sub-element type of this array is i8
1524/// then this method converts the array to an std::string and returns it.
1525/// Otherwise, it asserts out.
1526///
Chris Lattner81fabb02002-08-26 17:53:56 +00001527std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001528 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001529 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001530 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001531 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001532 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001533 return Result;
1534}
1535
1536
Chris Lattner3462ae32001-12-03 22:26:30 +00001537//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001538//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001539
Chris Lattner189d19f2003-11-21 20:23:48 +00001540namespace llvm {
1541 template<>
1542 struct ConvertConstantType<ConstantStruct, StructType> {
1543 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1544 // Make everyone now use a constant of the new type...
1545 std::vector<Constant*> C;
1546 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1547 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001548 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001549 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001550
Chris Lattner189d19f2003-11-21 20:23:48 +00001551 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001552 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001553 }
1554 };
1555}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001556
Chris Lattner8760ec72005-10-04 01:17:50 +00001557typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001558 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001559static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001560
Chris Lattner3e650af2004-08-04 04:48:01 +00001561static std::vector<Constant*> getValType(ConstantStruct *CS) {
1562 std::vector<Constant*> Elements;
1563 Elements.reserve(CS->getNumOperands());
1564 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1565 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1566 return Elements;
1567}
1568
Chris Lattner015e8212004-02-15 04:14:47 +00001569Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001570 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001571 // Create a ConstantAggregateZero value if all elements are zeros...
1572 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001573 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001574 // Implicitly locked.
1575 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001576
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001577 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001578}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001579
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001580Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001581 std::vector<const Type*> StructEls;
1582 StructEls.reserve(V.size());
1583 for (unsigned i = 0, e = V.size(); i != e; ++i)
1584 StructEls.push_back(V[i]->getType());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001585 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001586}
1587
Chris Lattnerd7a73302001-10-13 06:57:33 +00001588// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001589//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001590void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001591 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001592 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001593 destroyConstantImpl();
1594}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001595
Reid Spencerd84d35b2007-02-15 02:26:10 +00001596//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001597//
1598namespace llvm {
1599 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001600 struct ConvertConstantType<ConstantVector, VectorType> {
1601 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001602 // Make everyone now use a constant of the new type...
1603 std::vector<Constant*> C;
1604 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1605 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001606 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001607 assert(New != OldC && "Didn't replace constant??");
1608 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001609 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001610 }
1611 };
1612}
1613
Reid Spencerd84d35b2007-02-15 02:26:10 +00001614static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001615 std::vector<Constant*> Elements;
1616 Elements.reserve(CP->getNumOperands());
1617 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1618 Elements.push_back(CP->getOperand(i));
1619 return Elements;
1620}
1621
Reid Spencerd84d35b2007-02-15 02:26:10 +00001622static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001623 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001624
Reid Spencerd84d35b2007-02-15 02:26:10 +00001625Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001626 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001627 assert(!V.empty() && "Vectors can't be empty");
1628 // If this is an all-undef or alll-zero vector, return a
1629 // ConstantAggregateZero or UndefValue.
1630 Constant *C = V[0];
1631 bool isZero = C->isNullValue();
1632 bool isUndef = isa<UndefValue>(C);
1633
1634 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001635 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001636 if (V[i] != C) {
1637 isZero = isUndef = false;
1638 break;
1639 }
Brian Gaeke02209042004-08-20 06:00:58 +00001640 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001641
1642 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001643 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001644 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001645 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001646
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001647 // Implicitly locked.
1648 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001649}
1650
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001651Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001652 assert(!V.empty() && "Cannot infer type if V is empty");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001653 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001654}
1655
1656// destroyConstant - Remove the constant from the constant table...
1657//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001658void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001659 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001660 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001661 destroyConstantImpl();
1662}
1663
Dan Gohman30978072007-05-24 14:36:04 +00001664/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001665/// is set to all ones.
1666/// @returns true iff this constant's emements are all set to all ones.
1667/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001668bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001669 // Check out first element.
1670 const Constant *Elt = getOperand(0);
1671 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1672 if (!CI || !CI->isAllOnesValue()) return false;
1673 // Then make sure all remaining elements point to the same value.
1674 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1675 if (getOperand(I) != Elt) return false;
1676 }
1677 return true;
1678}
1679
Dan Gohman07159202007-10-17 17:51:30 +00001680/// getSplatValue - If this is a splat constant, where all of the
1681/// elements have the same value, return that value. Otherwise return null.
1682Constant *ConstantVector::getSplatValue() {
1683 // Check out first element.
1684 Constant *Elt = getOperand(0);
1685 // Then make sure all remaining elements point to the same value.
1686 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1687 if (getOperand(I) != Elt) return 0;
1688 return Elt;
1689}
1690
Chris Lattner3462ae32001-12-03 22:26:30 +00001691//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001692//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001693
Chris Lattner189d19f2003-11-21 20:23:48 +00001694namespace llvm {
1695 // ConstantPointerNull does not take extra "value" argument...
1696 template<class ValType>
1697 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1698 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1699 return new ConstantPointerNull(Ty);
1700 }
1701 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001702
Chris Lattner189d19f2003-11-21 20:23:48 +00001703 template<>
1704 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1705 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1706 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001707 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001708 assert(New != OldC && "Didn't replace constant??");
1709 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001710 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001711 }
1712 };
1713}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001714
Chris Lattner69edc982006-09-28 00:35:06 +00001715static ManagedStatic<ValueMap<char, PointerType,
1716 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001717
Chris Lattner3e650af2004-08-04 04:48:01 +00001718static char getValType(ConstantPointerNull *) {
1719 return 0;
1720}
1721
1722
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001723ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001724 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001725 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001726}
1727
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001728// destroyConstant - Remove the constant from the constant table...
1729//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001730void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001731 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001732 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001733 destroyConstantImpl();
1734}
1735
1736
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001737//---- UndefValue::get() implementation...
1738//
1739
1740namespace llvm {
1741 // UndefValue does not take extra "value" argument...
1742 template<class ValType>
1743 struct ConstantCreator<UndefValue, Type, ValType> {
1744 static UndefValue *create(const Type *Ty, const ValType &V) {
1745 return new UndefValue(Ty);
1746 }
1747 };
1748
1749 template<>
1750 struct ConvertConstantType<UndefValue, Type> {
1751 static void convert(UndefValue *OldC, const Type *NewTy) {
1752 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001753 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001754 assert(New != OldC && "Didn't replace constant??");
1755 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001756 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001757 }
1758 };
1759}
1760
Chris Lattner69edc982006-09-28 00:35:06 +00001761static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001762
1763static char getValType(UndefValue *) {
1764 return 0;
1765}
1766
1767
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001768UndefValue *UndefValue::get(const Type *Ty) {
1769 // Implicitly locked.
1770 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001771}
1772
1773// destroyConstant - Remove the constant from the constant table.
1774//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001775void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001776 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001777 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001778 destroyConstantImpl();
1779}
1780
Nick Lewycky49f89192009-04-04 07:22:01 +00001781//---- MDString::get() implementation
1782//
1783
1784MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001785 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001786 StrBegin(begin), StrEnd(end) {}
1787
1788static ManagedStatic<StringMap<MDString*> > MDStringCache;
1789
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001790MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001791 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001792 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1793 StrBegin, StrEnd);
1794 MDString *&S = Entry.getValue();
1795 if (!S) S = new MDString(Entry.getKeyData(),
1796 Entry.getKeyData() + Entry.getKeyLength());
Owen Anderson65c5cd72009-06-17 20:34:43 +00001797
Owen Andersond830eb82009-06-18 19:10:19 +00001798 return S;
Nick Lewycky49f89192009-04-04 07:22:01 +00001799}
1800
Devang Patel4c563162009-06-24 22:42:39 +00001801MDString *MDString::get(const std::string &Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001802 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Devang Patel4c563162009-06-24 22:42:39 +00001803 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1804 Str.data(), Str.data() + Str.size());
1805 MDString *&S = Entry.getValue();
1806 if (!S) S = new MDString(Entry.getKeyData(),
1807 Entry.getKeyData() + Entry.getKeyLength());
1808
1809 return S;
1810}
1811
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001812void MDString::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001813 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001814 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
Nick Lewycky49f89192009-04-04 07:22:01 +00001815 destroyConstantImpl();
1816}
1817
1818//---- MDNode::get() implementation
1819//
1820
1821static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1822
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001823MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001824 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001825 for (unsigned i = 0; i != NumVals; ++i)
1826 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001827}
1828
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001829void MDNode::Profile(FoldingSetNodeID &ID) const {
1830 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001831 ID.AddPointer(*I);
1832}
1833
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001834MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001835 FoldingSetNodeID ID;
1836 for (unsigned i = 0; i != NumVals; ++i)
1837 ID.AddPointer(Vals[i]);
1838
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001839 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001840 void *InsertPoint;
1841 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001842 ConstantsLock->reader_release();
Owen Andersond830eb82009-06-18 19:10:19 +00001843
1844 if (!N) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001845 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001846 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1847 if (!N) {
Owen Andersond830eb82009-06-18 19:10:19 +00001848 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1849 N = new(0) MDNode(Vals, NumVals);
1850 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001851 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001852 }
Owen Andersond830eb82009-06-18 19:10:19 +00001853 return N;
Nick Lewycky49f89192009-04-04 07:22:01 +00001854}
1855
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001856void MDNode::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001857 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001858 MDNodeSet->RemoveNode(this);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001859
Nick Lewycky49f89192009-04-04 07:22:01 +00001860 destroyConstantImpl();
1861}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001862
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001863//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001864//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001865
Dan Gohmand78c4002008-05-13 00:00:25 +00001866namespace {
1867
Reid Spenceree3c9912006-12-04 05:19:50 +00001868struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001869 typedef SmallVector<unsigned, 4> IndexList;
1870
1871 ExprMapKeyType(unsigned opc,
1872 const std::vector<Constant*> &ops,
1873 unsigned short pred = 0,
1874 const IndexList &inds = IndexList())
1875 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001876 uint16_t opcode;
1877 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001878 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001879 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001880 bool operator==(const ExprMapKeyType& that) const {
1881 return this->opcode == that.opcode &&
1882 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001883 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001884 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001885 }
1886 bool operator<(const ExprMapKeyType & that) const {
1887 return this->opcode < that.opcode ||
1888 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1889 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001890 this->operands < that.operands) ||
1891 (this->opcode == that.opcode && this->predicate == that.predicate &&
1892 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001893 }
1894
1895 bool operator!=(const ExprMapKeyType& that) const {
1896 return !(*this == that);
1897 }
1898};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001899
Dan Gohmand78c4002008-05-13 00:00:25 +00001900}
1901
Chris Lattner189d19f2003-11-21 20:23:48 +00001902namespace llvm {
1903 template<>
1904 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001905 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1906 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001907 if (Instruction::isCast(V.opcode))
1908 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1909 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001910 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001911 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1912 if (V.opcode == Instruction::Select)
1913 return new SelectConstantExpr(V.operands[0], V.operands[1],
1914 V.operands[2]);
1915 if (V.opcode == Instruction::ExtractElement)
1916 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1917 if (V.opcode == Instruction::InsertElement)
1918 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1919 V.operands[2]);
1920 if (V.opcode == Instruction::ShuffleVector)
1921 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1922 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001923 if (V.opcode == Instruction::InsertValue)
1924 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1925 V.indices, Ty);
1926 if (V.opcode == Instruction::ExtractValue)
1927 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001928 if (V.opcode == Instruction::GetElementPtr) {
1929 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001930 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001931 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001932
Reid Spenceree3c9912006-12-04 05:19:50 +00001933 // The compare instructions are weird. We have to encode the predicate
1934 // value and it is combined with the instruction opcode by multiplying
1935 // the opcode by one hundred. We must decode this to get the predicate.
1936 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001937 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001938 V.operands[0], V.operands[1]);
1939 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001940 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1941 V.operands[0], V.operands[1]);
Torok Edwin56d06592009-07-11 20:10:48 +00001942 LLVM_UNREACHABLE("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001943 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001944 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001945 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001946
Chris Lattner189d19f2003-11-21 20:23:48 +00001947 template<>
1948 struct ConvertConstantType<ConstantExpr, Type> {
1949 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1950 Constant *New;
1951 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001952 case Instruction::Trunc:
1953 case Instruction::ZExt:
1954 case Instruction::SExt:
1955 case Instruction::FPTrunc:
1956 case Instruction::FPExt:
1957 case Instruction::UIToFP:
1958 case Instruction::SIToFP:
1959 case Instruction::FPToUI:
1960 case Instruction::FPToSI:
1961 case Instruction::PtrToInt:
1962 case Instruction::IntToPtr:
1963 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001964 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001965 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001966 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001967 case Instruction::Select:
1968 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1969 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001970 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001971 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001972 default:
1973 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001974 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001975 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001976 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001977 break;
1978 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001979 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001980 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001981 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001982 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001983 break;
1984 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001985
Chris Lattner189d19f2003-11-21 20:23:48 +00001986 assert(New != OldC && "Didn't replace constant??");
1987 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001988 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001989 }
1990 };
1991} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001992
1993
Chris Lattner3e650af2004-08-04 04:48:01 +00001994static ExprMapKeyType getValType(ConstantExpr *CE) {
1995 std::vector<Constant*> Operands;
1996 Operands.reserve(CE->getNumOperands());
1997 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1998 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001999 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002000 CE->isCompare() ? CE->getPredicate() : 0,
2001 CE->hasIndices() ?
2002 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00002003}
2004
Chris Lattner69edc982006-09-28 00:35:06 +00002005static ManagedStatic<ValueMap<ExprMapKeyType, Type,
2006 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00002007
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002008/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00002009/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002011 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00002012 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 // Fold a few common cases
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002014 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00002016
Vikram S. Adve4c485332002-07-15 18:19:33 +00002017 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002018 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00002019 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00002020
Owen Anderson61794042009-06-17 20:10:08 +00002021 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002022 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002023}
Reid Spencerf37dc652006-12-05 19:14:13 +00002024
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002025Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 Instruction::CastOps opc = Instruction::CastOps(oc);
2027 assert(Instruction::isCast(opc) && "opcode out of range");
2028 assert(C && Ty && "Null arguments to getCast");
2029 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
2030
2031 switch (opc) {
2032 default:
Torok Edwin56d06592009-07-11 20:10:48 +00002033 LLVM_UNREACHABLE("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002034 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002035 case Instruction::Trunc: return getTrunc(C, Ty);
2036 case Instruction::ZExt: return getZExt(C, Ty);
2037 case Instruction::SExt: return getSExt(C, Ty);
2038 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
2039 case Instruction::FPExt: return getFPExtend(C, Ty);
2040 case Instruction::UIToFP: return getUIToFP(C, Ty);
2041 case Instruction::SIToFP: return getSIToFP(C, Ty);
2042 case Instruction::FPToUI: return getFPToUI(C, Ty);
2043 case Instruction::FPToSI: return getFPToSI(C, Ty);
2044 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
2045 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
2046 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00002047 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00002049}
2050
Reid Spencer5c140882006-12-04 20:17:56 +00002051Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002052 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002053 return getCast(Instruction::BitCast, C, Ty);
2054 return getCast(Instruction::ZExt, C, Ty);
2055}
2056
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002057Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002058 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002059 return getCast(Instruction::BitCast, C, Ty);
2060 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00002061}
2062
2063Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002064 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002065 return getCast(Instruction::BitCast, C, Ty);
2066 return getCast(Instruction::Trunc, C, Ty);
2067}
2068
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002069Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00002070 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002071 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00002072
Chris Lattner03c49532007-01-15 02:27:26 +00002073 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002074 return getCast(Instruction::PtrToInt, S, Ty);
2075 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00002076}
2077
Reid Spencer56521c42006-12-12 00:51:07 +00002078Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
2079 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002080 assert(C->getType()->isIntOrIntVector() &&
2081 Ty->isIntOrIntVector() && "Invalid cast");
2082 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2083 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00002084 Instruction::CastOps opcode =
2085 (SrcBits == DstBits ? Instruction::BitCast :
2086 (SrcBits > DstBits ? Instruction::Trunc :
2087 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2088 return getCast(opcode, C, Ty);
2089}
2090
2091Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002092 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00002093 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002094 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2095 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00002096 if (SrcBits == DstBits)
2097 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00002098 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00002099 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00002100 return getCast(opcode, C, Ty);
2101}
2102
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002103Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002104#ifndef NDEBUG
2105 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2106 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2107#endif
2108 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2109 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
2110 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
2111 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112 "SrcTy must be larger than DestTy for Trunc!");
2113
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002114 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115}
2116
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002117Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002118#ifndef NDEBUG
2119 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2120 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2121#endif
2122 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2123 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
2124 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
2125 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126 "SrcTy must be smaller than DestTy for SExt!");
2127
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002128 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00002129}
2130
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002131Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002132#ifndef NDEBUG
2133 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2134 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2135#endif
2136 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2137 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
2138 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
2139 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140 "SrcTy must be smaller than DestTy for ZExt!");
2141
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002142 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143}
2144
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002145Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002146#ifndef NDEBUG
2147 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2148 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2149#endif
2150 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2151 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2152 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002153 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002154 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002155}
2156
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002157Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002158#ifndef NDEBUG
2159 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2160 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2161#endif
2162 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2163 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2164 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002166 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002167}
2168
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002169Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002170#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002171 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2172 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002173#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002174 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2175 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
2176 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002177 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178}
2179
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002180Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002181#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002182 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2183 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002184#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002185 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2186 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002187 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002188 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189}
2190
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002191Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002192#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002193 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2194 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002195#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002196 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2197 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2198 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002199 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200}
2201
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002202Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002203#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002204 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2205 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002206#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002207 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2208 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2209 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002210 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211}
2212
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002213Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00002215 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002216 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002217}
2218
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002219Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00002220 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002221 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002222 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223}
2224
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002225Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 // BitCast implies a no-op cast of type only. No bits change. However, you
2227 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002228#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229 const Type *SrcTy = C->getType();
2230 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002231 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232
2233 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2234 // or nonptr->ptr). For all the other types, the cast is okay if source and
2235 // destination bit widths are identical.
2236 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2237 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002238#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002239 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002240
2241 // It is common to ask for a bitcast of a value to its own type, handle this
2242 // speedily.
2243 if (C->getType() == DstTy) return C;
2244
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002245 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002246}
2247
Duncan Sandsd334aca2009-05-21 15:52:21 +00002248Constant *ConstantExpr::getAlignOf(const Type *Ty) {
2249 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
2250 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
2251 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
2252 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
2253 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
2254 Constant *Indices[2] = { Zero, One };
2255 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
2256 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
2257}
2258
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00002259Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00002260 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00002261 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
2262 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00002263 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00002264 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00002265}
2266
Chris Lattnerb50d1352003-10-05 00:17:43 +00002267Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002268 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002269 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002270 assert(Opcode >= Instruction::BinaryOpsBegin &&
2271 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002272 "Invalid opcode in binary constant expression");
2273 assert(C1->getType() == C2->getType() &&
2274 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002275
Reid Spencer542964f2007-01-11 18:21:29 +00002276 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002277 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00002278 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002279
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002280 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002281 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002282
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002283 // Implicitly locked.
2284 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002285}
2286
Reid Spencer266e42b2006-12-23 06:05:41 +00002287Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002288 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002289 switch (predicate) {
Torok Edwin56d06592009-07-11 20:10:48 +00002290 default: LLVM_UNREACHABLE("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002291 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2292 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2293 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2294 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2295 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2296 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002297 return getFCmp(predicate, C1, C2);
2298
Nate Begemanc96e2e42008-07-25 17:35:37 +00002299 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2300 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2301 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2302 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002303 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002304 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002305}
2306
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002307Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002308 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2309 if (C1->getType()->isFPOrFPVector()) {
2310 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2311 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2312 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2313 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002314#ifndef NDEBUG
2315 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002316 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002317 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002318 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002319 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002320 assert(C1->getType()->isIntOrIntVector() &&
2321 "Tried to create an integer operation on a non-integer type!");
2322 break;
2323 case Instruction::FAdd:
2324 case Instruction::FSub:
2325 case Instruction::FMul:
2326 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2327 assert(C1->getType()->isFPOrFPVector() &&
2328 "Tried to create a floating-point operation on a "
2329 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002330 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002331 case Instruction::UDiv:
2332 case Instruction::SDiv:
2333 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002334 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002335 "Tried to create an arithmetic operation on a non-arithmetic type!");
2336 break;
2337 case Instruction::FDiv:
2338 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002339 assert(C1->getType()->isFPOrFPVector() &&
2340 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002341 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002342 case Instruction::URem:
2343 case Instruction::SRem:
2344 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002345 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002346 "Tried to create an arithmetic operation on a non-arithmetic type!");
2347 break;
2348 case Instruction::FRem:
2349 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002350 assert(C1->getType()->isFPOrFPVector() &&
2351 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002352 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002353 case Instruction::And:
2354 case Instruction::Or:
2355 case Instruction::Xor:
2356 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002357 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002358 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002359 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002360 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002361 case Instruction::LShr:
2362 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002363 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002364 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002365 "Tried to create a shift operation on a non-integer type!");
2366 break;
2367 default:
2368 break;
2369 }
2370#endif
2371
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002372 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002373}
2374
Reid Spencer266e42b2006-12-23 06:05:41 +00002375Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002376 Constant *C1, Constant *C2) {
2377 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002378 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002379}
2380
Chris Lattner6e415c02004-03-12 05:54:04 +00002381Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002382 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002383 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002384
2385 if (ReqTy == V1->getType())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002386 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00002387 return SC; // Fold common cases
2388
2389 std::vector<Constant*> argVec(3, C);
2390 argVec[1] = V1;
2391 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002392 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002393
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002394 // Implicitly locked.
2395 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002396}
2397
Chris Lattnerb50d1352003-10-05 00:17:43 +00002398Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002399 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002400 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002401 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2402 Idxs+NumIdx) ==
2403 cast<PointerType>(ReqTy)->getElementType() &&
2404 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002405
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002406 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002407 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002408
Chris Lattnerb50d1352003-10-05 00:17:43 +00002409 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002410 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002411 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002412 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002413 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002414 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002415 for (unsigned i = 0; i != NumIdx; ++i)
2416 ArgVec.push_back(cast<Constant>(Idxs[i]));
2417 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002418
2419 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002420 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002421}
2422
Chris Lattner302116a2007-01-31 04:40:28 +00002423Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002424 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002425 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002426 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002427 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002428 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002429 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002430 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002431}
2432
Chris Lattner302116a2007-01-31 04:40:28 +00002433Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002434 unsigned NumIdx) {
2435 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002436}
2437
Chris Lattner302116a2007-01-31 04:40:28 +00002438
Reid Spenceree3c9912006-12-04 05:19:50 +00002439Constant *
2440ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2441 assert(LHS->getType() == RHS->getType());
2442 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2443 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2444
Reid Spencer266e42b2006-12-23 06:05:41 +00002445 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002446 return FC; // Fold a few common cases...
2447
2448 // Look up the constant in the table first to ensure uniqueness
2449 std::vector<Constant*> ArgVec;
2450 ArgVec.push_back(LHS);
2451 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002452 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002453 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002454
2455 // Implicitly locked.
2456 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002457}
2458
2459Constant *
2460ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2461 assert(LHS->getType() == RHS->getType());
2462 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2463
Reid Spencer266e42b2006-12-23 06:05:41 +00002464 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002465 return FC; // Fold a few common cases...
2466
2467 // Look up the constant in the table first to ensure uniqueness
2468 std::vector<Constant*> ArgVec;
2469 ArgVec.push_back(LHS);
2470 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002471 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002472 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002473
2474 // Implicitly locked.
2475 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002476}
2477
Robert Bocchino23004482006-01-10 19:05:34 +00002478Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2479 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002480 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2481 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002482 // Look up the constant in the table first to ensure uniqueness
2483 std::vector<Constant*> ArgVec(1, Val);
2484 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002485 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002486
2487 // Implicitly locked.
2488 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002489}
2490
2491Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002492 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002493 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002494 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002495 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002496 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002497 Val, Idx);
2498}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002499
Robert Bocchinoca27f032006-01-17 20:07:22 +00002500Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2501 Constant *Elt, Constant *Idx) {
2502 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2503 return FC; // Fold a few common cases...
2504 // Look up the constant in the table first to ensure uniqueness
2505 std::vector<Constant*> ArgVec(1, Val);
2506 ArgVec.push_back(Elt);
2507 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002508 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002509
2510 // Implicitly locked.
2511 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002512}
2513
2514Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2515 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002516 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002517 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002518 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002519 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002520 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002521 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002522 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002523}
2524
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002525Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2526 Constant *V2, Constant *Mask) {
2527 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2528 return FC; // Fold a few common cases...
2529 // Look up the constant in the table first to ensure uniqueness
2530 std::vector<Constant*> ArgVec(1, V1);
2531 ArgVec.push_back(V2);
2532 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002533 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002534
2535 // Implicitly locked.
2536 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002537}
2538
2539Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2540 Constant *Mask) {
2541 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2542 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002543
2544 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2545 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2546 const Type *ShufTy = VectorType::get(EltTy, NElts);
2547 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002548}
2549
Dan Gohman12fce772008-05-15 19:50:34 +00002550Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2551 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002552 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002553 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2554 Idxs+NumIdx) == Val->getType() &&
2555 "insertvalue indices invalid!");
2556 assert(Agg->getType() == ReqTy &&
2557 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002558 assert(Agg->getType()->isFirstClassType() &&
2559 "Non-first-class type for constant InsertValue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002560 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
2561 assert(FC && "InsertValue constant expr couldn't be folded!");
2562 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002563}
2564
2565Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002566 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002567 assert(Agg->getType()->isFirstClassType() &&
2568 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002569
Dan Gohman0752bff2008-05-23 00:36:11 +00002570 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002571#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002572 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002573 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002574#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002575 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002576 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2577}
2578
2579Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002580 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002581 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2582 Idxs+NumIdx) == ReqTy &&
2583 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002584 assert(Agg->getType()->isFirstClassType() &&
2585 "Non-first-class type for constant extractvalue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002586 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
2587 assert(FC && "ExtractValue constant expr couldn't be folded!");
2588 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002589}
2590
2591Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002592 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002593 assert(Agg->getType()->isFirstClassType() &&
2594 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002595
2596 const Type *ReqTy =
2597 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2598 assert(ReqTy && "extractvalue indices invalid!");
2599 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2600}
2601
Reid Spencer2eadb532007-01-21 00:29:26 +00002602Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002603 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002604 if (PTy->getElementType()->isFloatingPoint()) {
2605 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002606 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002607 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002608 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002609
Dale Johannesen98d3a082007-09-14 22:26:36 +00002610 if (Ty->isFloatingPoint())
2611 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002612
2613 return Constant::getNullValue(Ty);
2614}
2615
Vikram S. Adve4c485332002-07-15 18:19:33 +00002616// destroyConstant - Remove the constant from the constant table...
2617//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002618void ConstantExpr::destroyConstant() {
2619 // Implicitly locked.
2620 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002621 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002622}
2623
Chris Lattner3cd8c562002-07-30 18:54:25 +00002624const char *ConstantExpr::getOpcodeName() const {
2625 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002626}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002627
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002628//===----------------------------------------------------------------------===//
2629// replaceUsesOfWithOnConstant implementations
2630
Chris Lattner913849b2007-08-21 00:55:23 +00002631/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2632/// 'From' to be uses of 'To'. This must update the uniquing data structures
2633/// etc.
2634///
2635/// Note that we intentionally replace all uses of From with To here. Consider
2636/// a large array that uses 'From' 1000 times. By handling this case all here,
2637/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2638/// single invocation handles all 1000 uses. Handling them one at a time would
2639/// work, but would be really slow because it would have to unique each updated
2640/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002641void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002642 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002643 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002644 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002645
Jim Laskeyc03caef2006-07-17 17:38:29 +00002646 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002647 Lookup.first.first = getType();
2648 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002649
Chris Lattnerb64419a2005-10-03 22:51:37 +00002650 std::vector<Constant*> &Values = Lookup.first.second;
2651 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002652
Chris Lattner8760ec72005-10-04 01:17:50 +00002653 // Fill values with the modified operands of the constant array. Also,
2654 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002655 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002656 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002657 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002658 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2659 Constant *Val = cast<Constant>(O->get());
2660 if (Val == From) {
2661 Val = ToC;
2662 ++NumUpdated;
2663 }
2664 Values.push_back(Val);
2665 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002666 } else {
2667 isAllZeros = true;
2668 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2669 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002670 if (Val == From) {
2671 Val = ToC;
2672 ++NumUpdated;
2673 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002674 Values.push_back(Val);
2675 if (isAllZeros) isAllZeros = Val->isNullValue();
2676 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002677 }
2678
Chris Lattnerb64419a2005-10-03 22:51:37 +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 Lattnerb64419a2005-10-03 22:51:37 +00002685 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002686 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002687 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +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 array, 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 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002697
Chris Lattner913849b2007-08-21 00:55:23 +00002698 // Update to the new value. Optimize for the case when we have a single
2699 // operand that we're changing, but handle bulk updates efficiently.
2700 if (NumUpdated == 1) {
2701 unsigned OperandToUpdate = U-OperandList;
2702 assert(getOperand(OperandToUpdate) == From &&
2703 "ReplaceAllUsesWith broken!");
2704 setOperand(OperandToUpdate, ToC);
2705 } else {
2706 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2707 if (getOperand(i) == From)
2708 setOperand(i, ToC);
2709 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002710 return;
2711 }
2712 }
2713
2714 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002715 assert(Replacement != this && "I didn't contain From!");
2716
Chris Lattner7a1450d2005-10-04 18:13:04 +00002717 // Everyone using this now uses the replacement.
2718 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002719
2720 // Delete the old constant!
2721 destroyConstant();
2722}
2723
2724void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002725 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002726 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002727 Constant *ToC = cast<Constant>(To);
2728
Chris Lattnerdff59112005-10-04 18:47:09 +00002729 unsigned OperandToUpdate = U-OperandList;
2730 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2731
Jim Laskeyc03caef2006-07-17 17:38:29 +00002732 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002733 Lookup.first.first = getType();
2734 Lookup.second = this;
2735 std::vector<Constant*> &Values = Lookup.first.second;
2736 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002737
Chris Lattnerdff59112005-10-04 18:47:09 +00002738
Chris Lattner8760ec72005-10-04 01:17:50 +00002739 // Fill values with the modified operands of the constant struct. Also,
2740 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002741 bool isAllZeros = false;
2742 if (!ToC->isNullValue()) {
2743 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2744 Values.push_back(cast<Constant>(O->get()));
2745 } else {
2746 isAllZeros = true;
2747 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2748 Constant *Val = cast<Constant>(O->get());
2749 Values.push_back(Val);
2750 if (isAllZeros) isAllZeros = Val->isNullValue();
2751 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002752 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002753 Values[OperandToUpdate] = ToC;
2754
Chris Lattner8760ec72005-10-04 01:17:50 +00002755 Constant *Replacement = 0;
2756 if (isAllZeros) {
2757 Replacement = ConstantAggregateZero::get(getType());
2758 } else {
2759 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002760 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002761 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002762 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002763 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002764
2765 if (Exists) {
2766 Replacement = I->second;
2767 } else {
2768 // Okay, the new shape doesn't exist in the system yet. Instead of
2769 // creating a new constant struct, inserting it, replaceallusesof'ing the
2770 // old with the new, then deleting the old... just update the current one
2771 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002772 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002773
Chris Lattnerdff59112005-10-04 18:47:09 +00002774 // Update to the new value.
2775 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002776 return;
2777 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002778 }
2779
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002780 assert(Replacement != this && "I didn't contain From!");
2781
Chris Lattner7a1450d2005-10-04 18:13:04 +00002782 // Everyone using this now uses the replacement.
2783 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002784
2785 // Delete the old constant!
2786 destroyConstant();
2787}
2788
Reid Spencerd84d35b2007-02-15 02:26:10 +00002789void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002790 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002791 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2792
2793 std::vector<Constant*> Values;
2794 Values.reserve(getNumOperands()); // Build replacement array...
2795 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2796 Constant *Val = getOperand(i);
2797 if (Val == From) Val = cast<Constant>(To);
2798 Values.push_back(Val);
2799 }
2800
Reid Spencerd84d35b2007-02-15 02:26:10 +00002801 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002802 assert(Replacement != this && "I didn't contain From!");
2803
Chris Lattner7a1450d2005-10-04 18:13:04 +00002804 // Everyone using this now uses the replacement.
2805 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002806
2807 // Delete the old constant!
2808 destroyConstant();
2809}
2810
2811void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002812 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002813 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2814 Constant *To = cast<Constant>(ToV);
2815
2816 Constant *Replacement = 0;
2817 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002818 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002819 Constant *Pointer = getOperand(0);
2820 Indices.reserve(getNumOperands()-1);
2821 if (Pointer == From) Pointer = To;
2822
2823 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2824 Constant *Val = getOperand(i);
2825 if (Val == From) Val = To;
2826 Indices.push_back(Val);
2827 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002828 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2829 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002830 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002831 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002832 if (Agg == From) Agg = To;
2833
Dan Gohman1ecaf452008-05-31 00:58:22 +00002834 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002835 Replacement = ConstantExpr::getExtractValue(Agg,
2836 &Indices[0], Indices.size());
2837 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002838 Constant *Agg = getOperand(0);
2839 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002840 if (Agg == From) Agg = To;
2841 if (Val == From) Val = To;
2842
Dan Gohman1ecaf452008-05-31 00:58:22 +00002843 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002844 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2845 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002846 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002847 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002848 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002849 } else if (getOpcode() == Instruction::Select) {
2850 Constant *C1 = getOperand(0);
2851 Constant *C2 = getOperand(1);
2852 Constant *C3 = getOperand(2);
2853 if (C1 == From) C1 = To;
2854 if (C2 == From) C2 = To;
2855 if (C3 == From) C3 = To;
2856 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002857 } else if (getOpcode() == Instruction::ExtractElement) {
2858 Constant *C1 = getOperand(0);
2859 Constant *C2 = getOperand(1);
2860 if (C1 == From) C1 = To;
2861 if (C2 == From) C2 = To;
2862 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002863 } else if (getOpcode() == Instruction::InsertElement) {
2864 Constant *C1 = getOperand(0);
2865 Constant *C2 = getOperand(1);
2866 Constant *C3 = getOperand(1);
2867 if (C1 == From) C1 = To;
2868 if (C2 == From) C2 = To;
2869 if (C3 == From) C3 = To;
2870 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2871 } else if (getOpcode() == Instruction::ShuffleVector) {
2872 Constant *C1 = getOperand(0);
2873 Constant *C2 = getOperand(1);
2874 Constant *C3 = getOperand(2);
2875 if (C1 == From) C1 = To;
2876 if (C2 == From) C2 = To;
2877 if (C3 == From) C3 = To;
2878 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002879 } else if (isCompare()) {
2880 Constant *C1 = getOperand(0);
2881 Constant *C2 = getOperand(1);
2882 if (C1 == From) C1 = To;
2883 if (C2 == From) C2 = To;
2884 if (getOpcode() == Instruction::ICmp)
2885 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002886 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002887 assert(getOpcode() == Instruction::FCmp);
2888 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002889 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002890 } else if (getNumOperands() == 2) {
2891 Constant *C1 = getOperand(0);
2892 Constant *C2 = getOperand(1);
2893 if (C1 == From) C1 = To;
2894 if (C2 == From) C2 = To;
2895 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2896 } else {
Torok Edwin56d06592009-07-11 20:10:48 +00002897 LLVM_UNREACHABLE("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002898 return;
2899 }
2900
2901 assert(Replacement != this && "I didn't contain From!");
2902
Chris Lattner7a1450d2005-10-04 18:13:04 +00002903 // Everyone using this now uses the replacement.
2904 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002905
2906 // Delete the old constant!
2907 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002908}
Nick Lewycky49f89192009-04-04 07:22:01 +00002909
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002910void MDNode::replaceElement(Value *From, Value *To) {
2911 SmallVector<Value*, 4> Values;
2912 Values.reserve(getNumElements()); // Build replacement array...
2913 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2914 Value *Val = getElement(i);
2915 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002916 Values.push_back(Val);
2917 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002918
2919 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002920 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002921
Nick Lewycky49f89192009-04-04 07:22:01 +00002922 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002923
Nick Lewycky49f89192009-04-04 07:22:01 +00002924 destroyConstant();
2925}