blob: a35003162781c4fbf6e84de731bafa0085567a59 [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"
Chris Lattner69edc982006-09-28 00:35:06 +000026#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000027#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000028#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000029#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000030#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000033#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000034#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner2f7c9632001-06-06 20:29:01 +000037//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000038// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
40
Owen Andersond830eb82009-06-18 19:10:19 +000041// Becomes a no-op when multithreading is disabled.
42ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000043
Chris Lattner3462ae32001-12-03 22:26:30 +000044void Constant::destroyConstantImpl() {
45 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000046 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000047 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 // but they don't know that. Because we only find out when the CPV is
49 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000050 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000051 //
52 while (!use_empty()) {
53 Value *V = use_back();
54#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000055 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000056 DOUT << "While deleting: " << *this
57 << "\n\nUse still stuck around after Def is destroyed: "
58 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000059#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000060 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000061 Constant *CV = cast<Constant>(V);
62 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000063
64 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000065 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000066 }
67
68 // Value has no outstanding references it is safe to delete it now...
69 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000070}
Chris Lattner2f7c9632001-06-06 20:29:01 +000071
Chris Lattner23dd1f62006-10-20 00:27:06 +000072/// canTrap - Return true if evaluation of this constant could trap. This is
73/// true for things like constant expressions that could divide by zero.
74bool Constant::canTrap() const {
75 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
76 // The only thing that could possibly trap are constant exprs.
77 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
78 if (!CE) return false;
79
80 // ConstantExpr traps if any operands can trap.
81 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
82 if (getOperand(i)->canTrap())
83 return true;
84
85 // Otherwise, only specific operations can trap.
86 switch (CE->getOpcode()) {
87 default:
88 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000089 case Instruction::UDiv:
90 case Instruction::SDiv:
91 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000092 case Instruction::URem:
93 case Instruction::SRem:
94 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000095 // Div and rem can trap if the RHS is not known to be non-zero.
96 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
97 return true;
98 return false;
99 }
100}
101
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000102/// ContainsRelocations - Return true if the constant value contains relocations
103/// which cannot be resolved at compile time. Kind argument is used to filter
104/// only 'interesting' sorts of relocations.
105bool Constant::ContainsRelocations(unsigned Kind) const {
106 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
107 bool isLocal = GV->hasLocalLinkage();
108 if ((Kind & Reloc::Local) && isLocal) {
109 // Global has local linkage and 'local' kind of relocations are
110 // requested
111 return true;
112 }
113
114 if ((Kind & Reloc::Global) && !isLocal) {
115 // Global has non-local linkage and 'global' kind of relocations are
116 // requested
117 return true;
118 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000119
120 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000121 }
122
Evan Chengf9e003b2007-03-08 00:59:12 +0000123 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000124 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000125 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000126
Evan Chengf9e003b2007-03-08 00:59:12 +0000127 return false;
128}
129
Chris Lattnerb1585a92002-08-13 17:50:20 +0000130// Static constructor to create a '0' constant of arbitrary type...
Owen Andersonf92b4322009-06-26 20:21:18 +0000131static const uint64_t zero[2] = {0, 0};
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000132Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000133 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000134 case Type::IntegerTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000135 return ConstantInt::get(Ty, 0);
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000136 case Type::FloatTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000137 return ConstantFP::get(APFloat(APInt(32, 0)));
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000138 case Type::DoubleTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000139 return ConstantFP::get(APFloat(APInt(64, 0)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000140 case Type::X86_FP80TyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000141 return ConstantFP::get(APFloat(APInt(80, 2, zero)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000142 case Type::FP128TyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000143 return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
Dale Johannesen98d3a082007-09-14 22:26:36 +0000144 case Type::PPC_FP128TyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000145 return ConstantFP::get(APFloat(APInt(128, 2, zero)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000146 case Type::PointerTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000147 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000148 case Type::StructTyID:
149 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000150 case Type::VectorTyID:
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000151 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000152 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000153 // Function, Label, or Opaque type?
154 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000155 return 0;
156 }
157}
158
Chris Lattner72e39582007-06-15 06:10:53 +0000159Constant *Constant::getAllOnesValue(const Type *Ty) {
160 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
161 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
162 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
163}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000164
165// Static constructor to create an integral constant with all bits set
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000166ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000167 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000168 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000169 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000170}
171
Dan Gohman30978072007-05-24 14:36:04 +0000172/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000173/// has all its bits set to true.
174/// @brief Get the all ones value
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000175ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000176 std::vector<Constant*> Elts;
177 Elts.resize(Ty->getNumElements(),
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000178 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000179 assert(Elts[0] && "Not a vector integer type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000180 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000181}
182
183
Chris Lattner2105d662008-07-10 00:28:11 +0000184/// getVectorElements - This method, which is only valid on constant of vector
185/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000186/// This handles breaking down a vector undef into undef elements, etc. For
187/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattner2105d662008-07-10 00:28:11 +0000188void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
189 assert(isa<VectorType>(getType()) && "Not a vector constant!");
190
191 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
192 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
193 Elts.push_back(CV->getOperand(i));
194 return;
195 }
196
197 const VectorType *VT = cast<VectorType>(getType());
198 if (isa<ConstantAggregateZero>(this)) {
199 Elts.assign(VT->getNumElements(),
200 Constant::getNullValue(VT->getElementType()));
201 return;
202 }
203
Chris Lattnerc5098a22008-07-14 05:10:41 +0000204 if (isa<UndefValue>(this)) {
205 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
206 return;
207 }
208
209 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000210}
211
212
213
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000215// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216//===----------------------------------------------------------------------===//
217
Reid Spencerb31bffe2007-02-26 23:54:03 +0000218ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000219 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000220 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221}
222
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000223ConstantInt *ConstantInt::TheTrueVal = 0;
224ConstantInt *ConstantInt::TheFalseVal = 0;
225
226namespace llvm {
227 void CleanupTrueFalse(void *) {
228 ConstantInt::ResetTrueFalse();
229 }
230}
231
232static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
233
234ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
235 assert(TheTrueVal == 0 && TheFalseVal == 0);
236 TheTrueVal = get(Type::Int1Ty, 1);
237 TheFalseVal = get(Type::Int1Ty, 0);
238
239 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
240 TrueFalseCleanup.Register();
241
242 return WhichOne ? TheTrueVal : TheFalseVal;
243}
244
245
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000246namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000247 struct DenseMapAPIntKeyInfo {
248 struct KeyTy {
249 APInt val;
250 const Type* type;
251 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
252 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
253 bool operator==(const KeyTy& that) const {
254 return type == that.type && this->val == that.val;
255 }
256 bool operator!=(const KeyTy& that) const {
257 return !this->operator==(that);
258 }
259 };
260 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
261 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000262 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000263 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000264 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000265 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000266 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
267 return LHS == RHS;
268 }
Dale Johannesena719a602007-08-24 00:56:33 +0000269 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000270 };
271}
272
273
Reid Spencerb31bffe2007-02-26 23:54:03 +0000274typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
275 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000276static ManagedStatic<IntMapTy> IntConstants;
277
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000278ConstantInt *ConstantInt::get(const IntegerType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000279 uint64_t V, bool isSigned) {
280 return get(APInt(Ty->getBitWidth(), V, isSigned));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000281}
282
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000283Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000284 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
285
286 // For vectors, broadcast the value.
287 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
288 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000289 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000290
291 return C;
Reid Spencerb31bffe2007-02-26 23:54:03 +0000292}
293
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000294// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000295// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000296// operator== and operator!= to ensure that the DenseMap doesn't attempt to
297// compare APInt's of different widths, which would violate an APInt class
298// invariant which generates an assertion.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000299ConstantInt *ConstantInt::get(const APInt& V) {
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000300 // Get the corresponding integer type for the bit width of the value.
301 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000302 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000303 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000304
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000305 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000306 ConstantInt *&Slot = (*IntConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000307 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000308
Owen Andersond830eb82009-06-18 19:10:19 +0000309 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000310 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000311 ConstantInt *&NewSlot = (*IntConstants)[Key];
312 if (!Slot) {
313 NewSlot = new ConstantInt(ITy, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000314 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000315
316 return NewSlot;
317 } else {
318 return Slot;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000319 }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000320}
321
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000322Constant *ConstantInt::get(const Type *Ty, const APInt &V) {
323 ConstantInt *C = ConstantInt::get(V);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000324 assert(C->getType() == Ty->getScalarType() &&
325 "ConstantInt type doesn't match the type implied by its value!");
326
327 // For vectors, broadcast the value.
328 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
329 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000330 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000331
332 return C;
333}
334
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000335//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000336// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000337//===----------------------------------------------------------------------===//
338
Chris Lattner98bd9392008-04-09 06:38:30 +0000339static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
340 if (Ty == Type::FloatTy)
341 return &APFloat::IEEEsingle;
342 if (Ty == Type::DoubleTy)
343 return &APFloat::IEEEdouble;
344 if (Ty == Type::X86_FP80Ty)
345 return &APFloat::x87DoubleExtended;
346 else if (Ty == Type::FP128Ty)
347 return &APFloat::IEEEquad;
348
349 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
350 return &APFloat::PPCDoubleDouble;
351}
352
Dale Johannesend246b2c2007-08-30 00:23:21 +0000353ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
354 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000355 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
356 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357}
358
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000359bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000360 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000361}
362
Dale Johannesen98d3a082007-09-14 22:26:36 +0000363ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
364 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
365 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000366 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000367}
368
Dale Johannesend246b2c2007-08-30 00:23:21 +0000369bool ConstantFP::isExactlyValue(const APFloat& V) const {
370 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000371}
372
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000373namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000374 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000375 struct KeyTy {
376 APFloat val;
377 KeyTy(const APFloat& V) : val(V){}
378 KeyTy(const KeyTy& that) : val(that.val) {}
379 bool operator==(const KeyTy& that) const {
380 return this->val.bitwiseIsEqual(that.val);
381 }
382 bool operator!=(const KeyTy& that) const {
383 return !this->operator==(that);
384 }
385 };
386 static inline KeyTy getEmptyKey() {
387 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000388 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000389 static inline KeyTy getTombstoneKey() {
390 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000391 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000392 static unsigned getHashValue(const KeyTy &Key) {
393 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000394 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000395 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
396 return LHS == RHS;
397 }
Dale Johannesena719a602007-08-24 00:56:33 +0000398 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000399 };
400}
401
402//---- ConstantFP::get() implementation...
403//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000404typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000405 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000406
Dale Johannesena719a602007-08-24 00:56:33 +0000407static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000408
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000409ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000410 DenseMapAPFloatKeyInfo::KeyTy Key(V);
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000411
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000412 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000413 ConstantFP *&Slot = (*FPConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000414 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000415
Owen Andersond830eb82009-06-18 19:10:19 +0000416 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000417 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000418 ConstantFP *&NewSlot = (*FPConstants)[Key];
419 if (!NewSlot) {
Owen Andersond830eb82009-06-18 19:10:19 +0000420 const Type *Ty;
421 if (&V.getSemantics() == &APFloat::IEEEsingle)
422 Ty = Type::FloatTy;
423 else if (&V.getSemantics() == &APFloat::IEEEdouble)
424 Ty = Type::DoubleTy;
425 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
426 Ty = Type::X86_FP80Ty;
427 else if (&V.getSemantics() == &APFloat::IEEEquad)
428 Ty = Type::FP128Ty;
429 else {
430 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
431 "Unknown FP format");
432 Ty = Type::PPC_FP128Ty;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000433 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000434 NewSlot = new ConstantFP(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000435 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000436
437 return NewSlot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000438 }
Owen Andersond830eb82009-06-18 19:10:19 +0000439
440 return Slot;
Dale Johannesend246b2c2007-08-30 00:23:21 +0000441}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000442
Chris Lattner98bd9392008-04-09 06:38:30 +0000443/// get() - This returns a constant fp for the specified value in the
444/// specified type. This should only be used for simple constant values like
445/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000446Constant *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000447 APFloat FV(V);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000448 bool ignored;
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000449 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
450 APFloat::rmNearestTiesToEven, &ignored);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000451 Constant *C = get(FV);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000452
453 // For vectors, broadcast the value.
454 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
455 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000456 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000457
458 return C;
Chris Lattner98bd9392008-04-09 06:38:30 +0000459}
460
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000461//===----------------------------------------------------------------------===//
462// ConstantXXX Classes
463//===----------------------------------------------------------------------===//
464
465
Chris Lattner3462ae32001-12-03 22:26:30 +0000466ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000467 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000468 : Constant(T, ConstantArrayVal,
469 OperandTraits<ConstantArray>::op_end(this) - V.size(),
470 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000471 assert(V.size() == T->getNumElements() &&
472 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000473 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000474 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
475 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000476 Constant *C = *I;
477 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000478 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000479 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000480 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000481 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000482 }
483}
484
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000485
Chris Lattner3462ae32001-12-03 22:26:30 +0000486ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000487 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000488 : Constant(T, ConstantStructVal,
489 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
490 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000491 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000492 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000493 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000494 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
495 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000496 Constant *C = *I;
497 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000498 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000499 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000500 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000501 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000502 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000503 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504 }
505}
506
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000507
Reid Spencerd84d35b2007-02-15 02:26:10 +0000508ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000509 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000510 : Constant(T, ConstantVectorVal,
511 OperandTraits<ConstantVector>::op_end(this) - V.size(),
512 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000513 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000514 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
515 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000516 Constant *C = *I;
517 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000518 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000519 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000520 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000521 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000522 }
523}
524
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000525
Gabor Greiff6caff662008-05-10 08:32:32 +0000526namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000527// We declare several classes private to this file, so use an anonymous
528// namespace
529namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000530
Gordon Henriksen14a55692007-12-10 02:14:30 +0000531/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
532/// behind the scenes to implement unary constant exprs.
533class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000534 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000535public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000536 // allocate space for exactly one operand
537 void *operator new(size_t s) {
538 return User::operator new(s, 1);
539 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000540 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000541 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
542 Op<0>() = C;
543 }
544 /// Transparently provide more efficient getOperand methods.
545 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000546};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000547
Gordon Henriksen14a55692007-12-10 02:14:30 +0000548/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
549/// behind the scenes to implement binary constant exprs.
550class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000551 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000552public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000553 // allocate space for exactly two operands
554 void *operator new(size_t s) {
555 return User::operator new(s, 2);
556 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000557 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000558 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000559 Op<0>() = C1;
560 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000561 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000562 /// Transparently provide more efficient getOperand methods.
563 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000564};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000565
Gordon Henriksen14a55692007-12-10 02:14:30 +0000566/// SelectConstantExpr - This class is private to Constants.cpp, and is used
567/// behind the scenes to implement select constant exprs.
568class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000569 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000570public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000571 // allocate space for exactly three operands
572 void *operator new(size_t s) {
573 return User::operator new(s, 3);
574 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000575 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000576 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000577 Op<0>() = C1;
578 Op<1>() = C2;
579 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000580 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000581 /// Transparently provide more efficient getOperand methods.
582 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000583};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000584
Gordon Henriksen14a55692007-12-10 02:14:30 +0000585/// ExtractElementConstantExpr - This class is private to
586/// Constants.cpp, and is used behind the scenes to implement
587/// extractelement constant exprs.
588class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000589 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000590public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000591 // allocate space for exactly two operands
592 void *operator new(size_t s) {
593 return User::operator new(s, 2);
594 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000595 ExtractElementConstantExpr(Constant *C1, Constant *C2)
596 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000597 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000598 Op<0>() = C1;
599 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000600 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000601 /// Transparently provide more efficient getOperand methods.
602 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000603};
Robert Bocchino23004482006-01-10 19:05:34 +0000604
Gordon Henriksen14a55692007-12-10 02:14:30 +0000605/// InsertElementConstantExpr - This class is private to
606/// Constants.cpp, and is used behind the scenes to implement
607/// insertelement constant exprs.
608class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000609 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000610public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000611 // allocate space for exactly three operands
612 void *operator new(size_t s) {
613 return User::operator new(s, 3);
614 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000615 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
616 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000617 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000618 Op<0>() = C1;
619 Op<1>() = C2;
620 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000621 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 /// Transparently provide more efficient getOperand methods.
623 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000624};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000625
Gordon Henriksen14a55692007-12-10 02:14:30 +0000626/// ShuffleVectorConstantExpr - This class is private to
627/// Constants.cpp, and is used behind the scenes to implement
628/// shufflevector constant exprs.
629class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000630 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000631public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000632 // allocate space for exactly three operands
633 void *operator new(size_t s) {
634 return User::operator new(s, 3);
635 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000636 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000637 : ConstantExpr(VectorType::get(
638 cast<VectorType>(C1->getType())->getElementType(),
639 cast<VectorType>(C3->getType())->getNumElements()),
640 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000641 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000642 Op<0>() = C1;
643 Op<1>() = C2;
644 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000645 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000646 /// Transparently provide more efficient getOperand methods.
647 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000648};
649
Dan Gohman12fce772008-05-15 19:50:34 +0000650/// ExtractValueConstantExpr - This class is private to
651/// Constants.cpp, and is used behind the scenes to implement
652/// extractvalue constant exprs.
653class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000654 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000655public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000656 // allocate space for exactly one operand
657 void *operator new(size_t s) {
658 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000659 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000660 ExtractValueConstantExpr(Constant *Agg,
661 const SmallVector<unsigned, 4> &IdxList,
662 const Type *DestTy)
663 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
664 Indices(IdxList) {
665 Op<0>() = Agg;
666 }
667
Dan Gohman7bb04502008-05-31 19:09:08 +0000668 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000669 const SmallVector<unsigned, 4> Indices;
670
Dan Gohman12fce772008-05-15 19:50:34 +0000671 /// Transparently provide more efficient getOperand methods.
672 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
673};
674
675/// InsertValueConstantExpr - This class is private to
676/// Constants.cpp, and is used behind the scenes to implement
677/// insertvalue constant exprs.
678class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000679 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000680public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000681 // allocate space for exactly one operand
682 void *operator new(size_t s) {
683 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000684 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000685 InsertValueConstantExpr(Constant *Agg, Constant *Val,
686 const SmallVector<unsigned, 4> &IdxList,
687 const Type *DestTy)
688 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
689 Indices(IdxList) {
690 Op<0>() = Agg;
691 Op<1>() = Val;
692 }
693
Dan Gohman7bb04502008-05-31 19:09:08 +0000694 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000695 const SmallVector<unsigned, 4> Indices;
696
Dan Gohman12fce772008-05-15 19:50:34 +0000697 /// Transparently provide more efficient getOperand methods.
698 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
699};
700
701
Gordon Henriksen14a55692007-12-10 02:14:30 +0000702/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
703/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000704class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000705 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000706 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000707public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000708 static GetElementPtrConstantExpr *Create(Constant *C,
709 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000710 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000711 return new(IdxList.size() + 1)
712 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000713 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000714 /// Transparently provide more efficient getOperand methods.
715 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000716};
717
718// CompareConstantExpr - This class is private to Constants.cpp, and is used
719// behind the scenes to implement ICmp and FCmp constant expressions. This is
720// needed in order to store the predicate value for these instructions.
721struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000722 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
723 // allocate space for exactly two operands
724 void *operator new(size_t s) {
725 return User::operator new(s, 2);
726 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000727 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000728 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
729 unsigned short pred, Constant* LHS, Constant* RHS)
730 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000731 Op<0>() = LHS;
732 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000733 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000734 /// Transparently provide more efficient getOperand methods.
735 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000736};
737
738} // end anonymous namespace
739
Gabor Greiff6caff662008-05-10 08:32:32 +0000740template <>
741struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
742};
743DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
744
745template <>
746struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
747};
748DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
749
750template <>
751struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
752};
753DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
754
755template <>
756struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
757};
758DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
759
760template <>
761struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
762};
763DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
764
765template <>
766struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
767};
768DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
769
Dan Gohman12fce772008-05-15 19:50:34 +0000770template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000771struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000772};
Dan Gohman12fce772008-05-15 19:50:34 +0000773DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
774
775template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000776struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000777};
Dan Gohman12fce772008-05-15 19:50:34 +0000778DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
779
Gabor Greiff6caff662008-05-10 08:32:32 +0000780template <>
781struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
782};
783
784GetElementPtrConstantExpr::GetElementPtrConstantExpr
785 (Constant *C,
786 const std::vector<Constant*> &IdxList,
787 const Type *DestTy)
788 : ConstantExpr(DestTy, Instruction::GetElementPtr,
789 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
790 - (IdxList.size()+1),
791 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000792 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000793 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000794 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000795}
796
797DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
798
799
800template <>
801struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
802};
803DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
804
805
806} // End llvm namespace
807
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000808
809// Utility function for determining if a ConstantExpr is a CastOp or not. This
810// can't be inline because we don't want to #include Instruction.h into
811// Constant.h
812bool ConstantExpr::isCast() const {
813 return Instruction::isCast(getOpcode());
814}
815
Reid Spenceree3c9912006-12-04 05:19:50 +0000816bool ConstantExpr::isCompare() const {
Chris Lattnereab49262008-07-14 05:17:31 +0000817 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp ||
818 getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp;
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 ||
907 getOpcode() == Instruction::ICmp ||
908 getOpcode() == Instruction::VFCmp ||
909 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000910 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000911}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000912Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
913 return get(Instruction::Shl, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000914}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000915Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
916 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000917}
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000918Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
919 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000920}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000921
Chris Lattner7c1018a2006-07-14 19:37:40 +0000922/// getWithOperandReplaced - Return a constant expression identical to this
923/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000924Constant *
925ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000926 assert(OpNo < getNumOperands() && "Operand num is out of range!");
927 assert(Op->getType() == getOperand(OpNo)->getType() &&
928 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000929 if (getOperand(OpNo) == Op)
930 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000931
Chris Lattner227816342006-07-14 22:20:01 +0000932 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000933 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000934 case Instruction::Trunc:
935 case Instruction::ZExt:
936 case Instruction::SExt:
937 case Instruction::FPTrunc:
938 case Instruction::FPExt:
939 case Instruction::UIToFP:
940 case Instruction::SIToFP:
941 case Instruction::FPToUI:
942 case Instruction::FPToSI:
943 case Instruction::PtrToInt:
944 case Instruction::IntToPtr:
945 case Instruction::BitCast:
946 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000947 case Instruction::Select:
948 Op0 = (OpNo == 0) ? Op : getOperand(0);
949 Op1 = (OpNo == 1) ? Op : getOperand(1);
950 Op2 = (OpNo == 2) ? Op : getOperand(2);
951 return ConstantExpr::getSelect(Op0, Op1, Op2);
952 case Instruction::InsertElement:
953 Op0 = (OpNo == 0) ? Op : getOperand(0);
954 Op1 = (OpNo == 1) ? Op : getOperand(1);
955 Op2 = (OpNo == 2) ? Op : getOperand(2);
956 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
957 case Instruction::ExtractElement:
958 Op0 = (OpNo == 0) ? Op : getOperand(0);
959 Op1 = (OpNo == 1) ? Op : getOperand(1);
960 return ConstantExpr::getExtractElement(Op0, Op1);
961 case Instruction::ShuffleVector:
962 Op0 = (OpNo == 0) ? Op : getOperand(0);
963 Op1 = (OpNo == 1) ? Op : getOperand(1);
964 Op2 = (OpNo == 2) ? Op : getOperand(2);
965 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000966 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000967 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000968 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000969 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000970 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000971 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000972 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000973 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000974 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000975 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000976 default:
977 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000978 Op0 = (OpNo == 0) ? Op : getOperand(0);
979 Op1 = (OpNo == 1) ? Op : getOperand(1);
980 return ConstantExpr::get(getOpcode(), Op0, Op1);
981 }
982}
983
984/// getWithOperands - This returns the current constant expression with the
985/// operands replaced with the specified values. The specified operands must
986/// match count and type with the existing ones.
987Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000988getWithOperands(Constant* const *Ops, unsigned NumOps) const {
989 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000990 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000991 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000992 assert(Ops[i]->getType() == getOperand(i)->getType() &&
993 "Operand type mismatch!");
994 AnyChange |= Ops[i] != getOperand(i);
995 }
996 if (!AnyChange) // No operands changed, return self.
997 return const_cast<ConstantExpr*>(this);
998
999 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001000 case Instruction::Trunc:
1001 case Instruction::ZExt:
1002 case Instruction::SExt:
1003 case Instruction::FPTrunc:
1004 case Instruction::FPExt:
1005 case Instruction::UIToFP:
1006 case Instruction::SIToFP:
1007 case Instruction::FPToUI:
1008 case Instruction::FPToSI:
1009 case Instruction::PtrToInt:
1010 case Instruction::IntToPtr:
1011 case Instruction::BitCast:
1012 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +00001013 case Instruction::Select:
1014 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1015 case Instruction::InsertElement:
1016 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1017 case Instruction::ExtractElement:
1018 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1019 case Instruction::ShuffleVector:
1020 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001021 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +00001022 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +00001023 case Instruction::ICmp:
1024 case Instruction::FCmp:
Nate Begeman098cc6f2008-07-25 17:56:27 +00001025 case Instruction::VICmp:
1026 case Instruction::VFCmp:
Reid Spencer266e42b2006-12-23 06:05:41 +00001027 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +00001028 default:
1029 assert(getNumOperands() == 2 && "Must be binary operator?");
1030 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001031 }
1032}
1033
Chris Lattner2f7c9632001-06-06 20:29:01 +00001034
1035//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001036// isValueValidForType implementations
1037
Reid Spencere7334722006-12-19 01:28:19 +00001038bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001039 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001040 if (Ty == Type::Int1Ty)
1041 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001042 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001043 return true; // always true, has to fit in largest type
1044 uint64_t Max = (1ll << NumBits) - 1;
1045 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +00001046}
1047
Reid Spencere0fc4df2006-10-20 07:07:24 +00001048bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001049 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001050 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +00001051 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001052 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001053 return true; // always true, has to fit in largest type
1054 int64_t Min = -(1ll << (NumBits-1));
1055 int64_t Max = (1ll << (NumBits-1)) - 1;
1056 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001057}
1058
Dale Johannesend246b2c2007-08-30 00:23:21 +00001059bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1060 // convert modifies in place, so make a copy.
1061 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001062 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001063 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001064 default:
1065 return false; // These can't be represented as floating point!
1066
Dale Johannesend246b2c2007-08-30 00:23:21 +00001067 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001068 case Type::FloatTyID: {
1069 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1070 return true;
1071 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1072 return !losesInfo;
1073 }
1074 case Type::DoubleTyID: {
1075 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1076 &Val2.getSemantics() == &APFloat::IEEEdouble)
1077 return true;
1078 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1079 return !losesInfo;
1080 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001081 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001082 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1083 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1084 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001085 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001086 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1087 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1088 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001089 case Type::PPC_FP128TyID:
1090 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1091 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1092 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001093 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001094}
Chris Lattner9655e542001-07-20 19:16:02 +00001095
Chris Lattner49d855c2001-09-07 16:46:31 +00001096//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001097// Factory Function Implementation
1098
Gabor Greiff6caff662008-05-10 08:32:32 +00001099
1100// The number of operands for each ConstantCreator::create method is
1101// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001102// ConstantCreator - A class that is used to create constants by
1103// ValueMap*. This class should be partially specialized if there is
1104// something strange that needs to be done to interface to the ctor for the
1105// constant.
1106//
Chris Lattner189d19f2003-11-21 20:23:48 +00001107namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001108 template<class ValType>
1109 struct ConstantTraits;
1110
1111 template<typename T, typename Alloc>
1112 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1113 static unsigned uses(const std::vector<T, Alloc>& v) {
1114 return v.size();
1115 }
1116 };
1117
Chris Lattner189d19f2003-11-21 20:23:48 +00001118 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001119 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001120 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001121 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001122 }
1123 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001124
Chris Lattner189d19f2003-11-21 20:23:48 +00001125 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001126 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001127 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1128 assert(0 && "This type cannot be converted!\n");
1129 abort();
1130 }
1131 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001132
Chris Lattner935aa922005-10-04 17:48:46 +00001133 template<class ValType, class TypeClass, class ConstantClass,
1134 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001135 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001136 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001137 typedef std::pair<const Type*, ValType> MapKey;
1138 typedef std::map<MapKey, Constant *> MapTy;
1139 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1140 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001141 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001142 /// Map - This is the main map from the element descriptor to the Constants.
1143 /// This is the primary way we avoid creating two of the same shape
1144 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001145 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001146
1147 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1148 /// from the constants to their element in Map. This is important for
1149 /// removal of constants from the array, which would otherwise have to scan
1150 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001151 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001152
Jim Laskeyc03caef2006-07-17 17:38:29 +00001153 /// AbstractTypeMap - Map for abstract type constants.
1154 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001155 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001156
1157 /// ValueMapLock - Mutex for this map.
1158 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +00001159
Chris Lattner98fa07b2003-05-23 20:03:32 +00001160 public:
Owen Anderson61794042009-06-17 20:10:08 +00001161 // NOTE: This function is not locked. It is the caller's responsibility
1162 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001163 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001164
1165 /// InsertOrGetItem - Return an iterator for the specified element.
1166 /// If the element exists in the map, the returned iterator points to the
1167 /// entry and Exists=true. If not, the iterator points to the newly
1168 /// inserted entry and returns Exists=false. Newly inserted entries have
1169 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +00001170 /// NOTE: This function is not locked. It is the caller's responsibility
1171 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001172 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1173 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001174 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001175 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001176 Exists = !IP.second;
1177 return IP.first;
1178 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001179
Chris Lattner935aa922005-10-04 17:48:46 +00001180private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001181 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001182 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001183 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001184 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1185 IMI->second->second == CP &&
1186 "InverseMap corrupt!");
1187 return IMI->second;
1188 }
1189
Jim Laskeyc03caef2006-07-17 17:38:29 +00001190 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001191 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1192 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001193 if (I == Map.end() || I->second != CP) {
1194 // FIXME: This should not use a linear scan. If this gets to be a
1195 // performance problem, someone should look at this.
1196 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1197 /* empty */;
1198 }
Chris Lattner935aa922005-10-04 17:48:46 +00001199 return I;
1200 }
Owen Andersonf89c38c2009-06-17 20:43:39 +00001201
1202 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
1203 typename MapTy::iterator I) {
1204 ConstantClass* Result =
1205 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1206
1207 assert(Result->getType() == Ty && "Type specified is not correct!");
1208 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1209
1210 if (HasLargeKey) // Remember the reverse mapping if needed.
1211 InverseMap.insert(std::make_pair(Result, I));
1212
1213 // If the type of the constant is abstract, make sure that an entry
1214 // exists for it in the AbstractTypeMap.
1215 if (Ty->isAbstract()) {
1216 typename AbstractTypeMapTy::iterator TI =
1217 AbstractTypeMap.find(Ty);
1218
1219 if (TI == AbstractTypeMap.end()) {
1220 // Add ourselves to the ATU list of the type.
1221 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1222
1223 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1224 }
1225 }
1226
1227 return Result;
1228 }
Chris Lattner935aa922005-10-04 17:48:46 +00001229public:
1230
Chris Lattnerb64419a2005-10-03 22:51:37 +00001231 /// getOrCreate - Return the specified constant from the map, creating it if
1232 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001233 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001234 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +00001235 MapKey Lookup(Ty, V);
1236 ConstantClass* Result = 0;
1237
1238 typename MapTy::iterator I = Map.find(Lookup);
1239 // Is it in the map?
1240 if (I != Map.end())
1241 Result = static_cast<ConstantClass *>(I->second);
1242
1243 if (!Result) {
1244 // If no preexisting value, create one now...
1245 Result = Create(Ty, V, I);
1246 }
1247
1248 return Result;
1249 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001250
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001251 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001252 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001253 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001254 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001255 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001256
Chris Lattner935aa922005-10-04 17:48:46 +00001257 if (HasLargeKey) // Remember the reverse mapping if needed.
1258 InverseMap.erase(CP);
1259
Chris Lattnerb50d1352003-10-05 00:17:43 +00001260 // Now that we found the entry, make sure this isn't the entry that
1261 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001262 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001263 if (Ty->isAbstract()) {
1264 assert(AbstractTypeMap.count(Ty) &&
1265 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001266 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001267 if (ATMEntryIt == I) {
1268 // Yes, we are removing the representative entry for this type.
1269 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001270 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001271
Chris Lattnerb50d1352003-10-05 00:17:43 +00001272 // First check the entry before this one...
1273 if (TmpIt != Map.begin()) {
1274 --TmpIt;
1275 if (TmpIt->first.first != Ty) // Not the same type, move back...
1276 ++TmpIt;
1277 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001278
Chris Lattnerb50d1352003-10-05 00:17:43 +00001279 // If we didn't find the same type, try to move forward...
1280 if (TmpIt == ATMEntryIt) {
1281 ++TmpIt;
1282 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1283 --TmpIt; // No entry afterwards with the same type
1284 }
1285
1286 // If there is another entry in the map of the same abstract type,
1287 // update the AbstractTypeMap entry now.
1288 if (TmpIt != ATMEntryIt) {
1289 ATMEntryIt = TmpIt;
1290 } else {
1291 // Otherwise, we are removing the last instance of this type
1292 // from the table. Remove from the ATM, and from user list.
1293 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1294 AbstractTypeMap.erase(Ty);
1295 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001296 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001297 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001298
Chris Lattnerb50d1352003-10-05 00:17:43 +00001299 Map.erase(I);
1300 }
1301
Chris Lattner3b793c62005-10-04 21:35:50 +00001302
1303 /// MoveConstantToNewSlot - If we are about to change C to be the element
1304 /// specified by I, update our internal data structures to reflect this
1305 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +00001306 /// NOTE: This function is not locked. It is the responsibility of the
1307 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001308 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001309 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001310 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001311 assert(OldI != Map.end() && "Constant not found in constant table!");
1312 assert(OldI->second == C && "Didn't find correct element?");
1313
1314 // If this constant is the representative element for its abstract type,
1315 // update the AbstractTypeMap so that the representative element is I.
1316 if (C->getType()->isAbstract()) {
1317 typename AbstractTypeMapTy::iterator ATI =
1318 AbstractTypeMap.find(C->getType());
1319 assert(ATI != AbstractTypeMap.end() &&
1320 "Abstract type not in AbstractTypeMap?");
1321 if (ATI->second == OldI)
1322 ATI->second = I;
1323 }
1324
1325 // Remove the old entry from the map.
1326 Map.erase(OldI);
1327
1328 // Update the inverse map so that we know that this constant is now
1329 // located at descriptor I.
1330 if (HasLargeKey) {
1331 assert(I->second == C && "Bad inversemap entry!");
1332 InverseMap[C] = I;
1333 }
1334 }
1335
Chris Lattnerb50d1352003-10-05 00:17:43 +00001336 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001337 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001338 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001339 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001340
1341 assert(I != AbstractTypeMap.end() &&
1342 "Abstract type not in AbstractTypeMap?");
1343
1344 // Convert a constant at a time until the last one is gone. The last one
1345 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1346 // eliminated eventually.
1347 do {
1348 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001349 TypeClass>::convert(
1350 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001351 cast<TypeClass>(NewTy));
1352
Jim Laskeyc03caef2006-07-17 17:38:29 +00001353 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001354 } while (I != AbstractTypeMap.end());
1355 }
1356
1357 // If the type became concrete without being refined to any other existing
1358 // type, we just remove ourselves from the ATU list.
1359 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001360 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001361 }
1362
1363 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001364 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001365 }
1366 };
1367}
1368
Chris Lattnera84df0a22006-09-28 23:36:21 +00001369
Chris Lattner28173502007-02-20 06:11:36 +00001370
Chris Lattner9fba3da2004-02-15 05:53:04 +00001371//---- ConstantAggregateZero::get() implementation...
1372//
1373namespace llvm {
1374 // ConstantAggregateZero does not take extra "value" argument...
1375 template<class ValType>
1376 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1377 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1378 return new ConstantAggregateZero(Ty);
1379 }
1380 };
1381
1382 template<>
1383 struct ConvertConstantType<ConstantAggregateZero, Type> {
1384 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1385 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001386 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001387 assert(New != OldC && "Didn't replace constant??");
1388 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001389 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001390 }
1391 };
1392}
1393
Chris Lattner69edc982006-09-28 00:35:06 +00001394static ManagedStatic<ValueMap<char, Type,
1395 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001396
Chris Lattner3e650af2004-08-04 04:48:01 +00001397static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1398
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001399ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001400 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001401 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001402
1403 // Implicitly locked.
1404 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001405}
1406
Dan Gohman92b551b2009-03-03 02:55:14 +00001407/// destroyConstant - Remove the constant from the constant table...
1408///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001409void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001410 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001411 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001412 destroyConstantImpl();
1413}
1414
Chris Lattner3462ae32001-12-03 22:26:30 +00001415//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001416//
Chris Lattner189d19f2003-11-21 20:23:48 +00001417namespace llvm {
1418 template<>
1419 struct ConvertConstantType<ConstantArray, ArrayType> {
1420 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1421 // Make everyone now use a constant of the new type...
1422 std::vector<Constant*> C;
1423 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1424 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001425 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001426 assert(New != OldC && "Didn't replace constant??");
1427 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001428 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001429 }
1430 };
1431}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001432
Chris Lattner3e650af2004-08-04 04:48:01 +00001433static std::vector<Constant*> getValType(ConstantArray *CA) {
1434 std::vector<Constant*> Elements;
1435 Elements.reserve(CA->getNumOperands());
1436 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1437 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1438 return Elements;
1439}
1440
Chris Lattnerb64419a2005-10-03 22:51:37 +00001441typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001442 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001443static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001444
Chris Lattner015e8212004-02-15 04:14:47 +00001445Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001446 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001447 // If this is an all-zero array, return a ConstantAggregateZero object
1448 if (!V.empty()) {
1449 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001450 if (!C->isNullValue()) {
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 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001455 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001456 // Implicitly locked.
1457 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001458 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001459 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001460
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001461 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001462}
1463
Dan Gohman92b551b2009-03-03 02:55:14 +00001464/// destroyConstant - Remove the constant from the constant table...
1465///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001466void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001467 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001468 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001469 destroyConstantImpl();
1470}
1471
Reid Spencer6f614532006-05-30 08:23:18 +00001472/// ConstantArray::get(const string&) - Return an array that is initialized to
1473/// contain the specified string. If length is zero then a null terminator is
1474/// added to the specified string so that it may be used in a natural way.
1475/// Otherwise, the length parameter specifies how much of the string to use
1476/// and it won't be null terminated.
1477///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001478Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001479 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001480 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001481 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001482
1483 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001484 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001485 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001486 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001487
Reid Spencer8d9336d2006-12-31 05:26:44 +00001488 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001489 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001490}
1491
Reid Spencer2546b762007-01-26 07:37:34 +00001492/// isString - This method returns true if the array is an array of i8, and
1493/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001494bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001495 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001496 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001497 return false;
1498 // Check the elements to make sure they are all integers, not constant
1499 // expressions.
1500 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1501 if (!isa<ConstantInt>(getOperand(i)))
1502 return false;
1503 return true;
1504}
1505
Evan Cheng3763c5b2006-10-26 19:15:05 +00001506/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001507/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001508/// null bytes except its terminator.
1509bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001510 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001511 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001512 return false;
1513 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1514 // Last element must be a null.
1515 if (getOperand(getNumOperands()-1) != Zero)
1516 return false;
1517 // Other elements must be non-null integers.
1518 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1519 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001520 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001521 if (getOperand(i) == Zero)
1522 return false;
1523 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001524 return true;
1525}
1526
1527
Dan Gohman92b551b2009-03-03 02:55:14 +00001528/// getAsString - If the sub-element type of this array is i8
1529/// then this method converts the array to an std::string and returns it.
1530/// Otherwise, it asserts out.
1531///
Chris Lattner81fabb02002-08-26 17:53:56 +00001532std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001533 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001534 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001535 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001536 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001537 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001538 return Result;
1539}
1540
1541
Chris Lattner3462ae32001-12-03 22:26:30 +00001542//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001543//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001544
Chris Lattner189d19f2003-11-21 20:23:48 +00001545namespace llvm {
1546 template<>
1547 struct ConvertConstantType<ConstantStruct, StructType> {
1548 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1549 // Make everyone now use a constant of the new type...
1550 std::vector<Constant*> C;
1551 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1552 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001553 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001554 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001555
Chris Lattner189d19f2003-11-21 20:23:48 +00001556 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001557 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001558 }
1559 };
1560}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001561
Chris Lattner8760ec72005-10-04 01:17:50 +00001562typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001563 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001564static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001565
Chris Lattner3e650af2004-08-04 04:48:01 +00001566static std::vector<Constant*> getValType(ConstantStruct *CS) {
1567 std::vector<Constant*> Elements;
1568 Elements.reserve(CS->getNumOperands());
1569 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1570 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1571 return Elements;
1572}
1573
Chris Lattner015e8212004-02-15 04:14:47 +00001574Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001575 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001576 // Create a ConstantAggregateZero value if all elements are zeros...
1577 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001578 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001579 // Implicitly locked.
1580 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001581
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001582 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001583}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001584
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001585Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001586 std::vector<const Type*> StructEls;
1587 StructEls.reserve(V.size());
1588 for (unsigned i = 0, e = V.size(); i != e; ++i)
1589 StructEls.push_back(V[i]->getType());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001590 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001591}
1592
Chris Lattnerd7a73302001-10-13 06:57:33 +00001593// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001594//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001595void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001596 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001597 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001598 destroyConstantImpl();
1599}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001600
Reid Spencerd84d35b2007-02-15 02:26:10 +00001601//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001602//
1603namespace llvm {
1604 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001605 struct ConvertConstantType<ConstantVector, VectorType> {
1606 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001607 // Make everyone now use a constant of the new type...
1608 std::vector<Constant*> C;
1609 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1610 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001611 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001612 assert(New != OldC && "Didn't replace constant??");
1613 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001614 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001615 }
1616 };
1617}
1618
Reid Spencerd84d35b2007-02-15 02:26:10 +00001619static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001620 std::vector<Constant*> Elements;
1621 Elements.reserve(CP->getNumOperands());
1622 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1623 Elements.push_back(CP->getOperand(i));
1624 return Elements;
1625}
1626
Reid Spencerd84d35b2007-02-15 02:26:10 +00001627static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001628 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001629
Reid Spencerd84d35b2007-02-15 02:26:10 +00001630Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001631 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001632 assert(!V.empty() && "Vectors can't be empty");
1633 // If this is an all-undef or alll-zero vector, return a
1634 // ConstantAggregateZero or UndefValue.
1635 Constant *C = V[0];
1636 bool isZero = C->isNullValue();
1637 bool isUndef = isa<UndefValue>(C);
1638
1639 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001640 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001641 if (V[i] != C) {
1642 isZero = isUndef = false;
1643 break;
1644 }
Brian Gaeke02209042004-08-20 06:00:58 +00001645 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001646
1647 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001648 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001649 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001650 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001651
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001652 // Implicitly locked.
1653 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001654}
1655
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001656Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001657 assert(!V.empty() && "Cannot infer type if V is empty");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001658 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001659}
1660
1661// destroyConstant - Remove the constant from the constant table...
1662//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001663void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001664 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001665 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001666 destroyConstantImpl();
1667}
1668
Dan Gohman30978072007-05-24 14:36:04 +00001669/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001670/// is set to all ones.
1671/// @returns true iff this constant's emements are all set to all ones.
1672/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001673bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001674 // Check out first element.
1675 const Constant *Elt = getOperand(0);
1676 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1677 if (!CI || !CI->isAllOnesValue()) return false;
1678 // Then make sure all remaining elements point to the same value.
1679 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1680 if (getOperand(I) != Elt) return false;
1681 }
1682 return true;
1683}
1684
Dan Gohman07159202007-10-17 17:51:30 +00001685/// getSplatValue - If this is a splat constant, where all of the
1686/// elements have the same value, return that value. Otherwise return null.
1687Constant *ConstantVector::getSplatValue() {
1688 // Check out first element.
1689 Constant *Elt = getOperand(0);
1690 // Then make sure all remaining elements point to the same value.
1691 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1692 if (getOperand(I) != Elt) return 0;
1693 return Elt;
1694}
1695
Chris Lattner3462ae32001-12-03 22:26:30 +00001696//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001697//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001698
Chris Lattner189d19f2003-11-21 20:23:48 +00001699namespace llvm {
1700 // ConstantPointerNull does not take extra "value" argument...
1701 template<class ValType>
1702 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1703 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1704 return new ConstantPointerNull(Ty);
1705 }
1706 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001707
Chris Lattner189d19f2003-11-21 20:23:48 +00001708 template<>
1709 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1710 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1711 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001712 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001713 assert(New != OldC && "Didn't replace constant??");
1714 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001715 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001716 }
1717 };
1718}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001719
Chris Lattner69edc982006-09-28 00:35:06 +00001720static ManagedStatic<ValueMap<char, PointerType,
1721 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001722
Chris Lattner3e650af2004-08-04 04:48:01 +00001723static char getValType(ConstantPointerNull *) {
1724 return 0;
1725}
1726
1727
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001728ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001729 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001730 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001731}
1732
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001733// destroyConstant - Remove the constant from the constant table...
1734//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001735void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001736 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001737 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001738 destroyConstantImpl();
1739}
1740
1741
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001742//---- UndefValue::get() implementation...
1743//
1744
1745namespace llvm {
1746 // UndefValue does not take extra "value" argument...
1747 template<class ValType>
1748 struct ConstantCreator<UndefValue, Type, ValType> {
1749 static UndefValue *create(const Type *Ty, const ValType &V) {
1750 return new UndefValue(Ty);
1751 }
1752 };
1753
1754 template<>
1755 struct ConvertConstantType<UndefValue, Type> {
1756 static void convert(UndefValue *OldC, const Type *NewTy) {
1757 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001758 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001759 assert(New != OldC && "Didn't replace constant??");
1760 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001761 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001762 }
1763 };
1764}
1765
Chris Lattner69edc982006-09-28 00:35:06 +00001766static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001767
1768static char getValType(UndefValue *) {
1769 return 0;
1770}
1771
1772
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001773UndefValue *UndefValue::get(const Type *Ty) {
1774 // Implicitly locked.
1775 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001776}
1777
1778// destroyConstant - Remove the constant from the constant table.
1779//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001780void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001781 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001782 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001783 destroyConstantImpl();
1784}
1785
Nick Lewycky49f89192009-04-04 07:22:01 +00001786//---- MDString::get() implementation
1787//
1788
1789MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001790 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001791 StrBegin(begin), StrEnd(end) {}
1792
1793static ManagedStatic<StringMap<MDString*> > MDStringCache;
1794
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001795MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001796 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001797 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1798 StrBegin, StrEnd);
1799 MDString *&S = Entry.getValue();
1800 if (!S) S = new MDString(Entry.getKeyData(),
1801 Entry.getKeyData() + Entry.getKeyLength());
Owen Anderson65c5cd72009-06-17 20:34:43 +00001802
Owen Andersond830eb82009-06-18 19:10:19 +00001803 return S;
Nick Lewycky49f89192009-04-04 07:22:01 +00001804}
1805
Devang Patel4c563162009-06-24 22:42:39 +00001806MDString *MDString::get(const std::string &Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001807 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Devang Patel4c563162009-06-24 22:42:39 +00001808 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1809 Str.data(), Str.data() + Str.size());
1810 MDString *&S = Entry.getValue();
1811 if (!S) S = new MDString(Entry.getKeyData(),
1812 Entry.getKeyData() + Entry.getKeyLength());
1813
1814 return S;
1815}
1816
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001817void MDString::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001818 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001819 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
Nick Lewycky49f89192009-04-04 07:22:01 +00001820 destroyConstantImpl();
1821}
1822
1823//---- MDNode::get() implementation
1824//
1825
1826static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1827
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001828MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001829 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001830 for (unsigned i = 0; i != NumVals; ++i)
1831 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001832}
1833
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001834void MDNode::Profile(FoldingSetNodeID &ID) const {
1835 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001836 ID.AddPointer(*I);
1837}
1838
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001839MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001840 FoldingSetNodeID ID;
1841 for (unsigned i = 0; i != NumVals; ++i)
1842 ID.AddPointer(Vals[i]);
1843
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001844 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001845 void *InsertPoint;
1846 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001847 ConstantsLock->reader_release();
Owen Andersond830eb82009-06-18 19:10:19 +00001848
1849 if (!N) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001850 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001851 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1852 if (!N) {
Owen Andersond830eb82009-06-18 19:10:19 +00001853 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1854 N = new(0) MDNode(Vals, NumVals);
1855 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001856 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001857 }
Owen Andersond830eb82009-06-18 19:10:19 +00001858 return N;
Nick Lewycky49f89192009-04-04 07:22:01 +00001859}
1860
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001861void MDNode::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001862 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001863 MDNodeSet->RemoveNode(this);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001864
Nick Lewycky49f89192009-04-04 07:22:01 +00001865 destroyConstantImpl();
1866}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001867
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001868//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001869//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001870
Dan Gohmand78c4002008-05-13 00:00:25 +00001871namespace {
1872
Reid Spenceree3c9912006-12-04 05:19:50 +00001873struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001874 typedef SmallVector<unsigned, 4> IndexList;
1875
1876 ExprMapKeyType(unsigned opc,
1877 const std::vector<Constant*> &ops,
1878 unsigned short pred = 0,
1879 const IndexList &inds = IndexList())
1880 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001881 uint16_t opcode;
1882 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001883 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001884 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001885 bool operator==(const ExprMapKeyType& that) const {
1886 return this->opcode == that.opcode &&
1887 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001888 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001889 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001890 }
1891 bool operator<(const ExprMapKeyType & that) const {
1892 return this->opcode < that.opcode ||
1893 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1894 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001895 this->operands < that.operands) ||
1896 (this->opcode == that.opcode && this->predicate == that.predicate &&
1897 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001898 }
1899
1900 bool operator!=(const ExprMapKeyType& that) const {
1901 return !(*this == that);
1902 }
1903};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001904
Dan Gohmand78c4002008-05-13 00:00:25 +00001905}
1906
Chris Lattner189d19f2003-11-21 20:23:48 +00001907namespace llvm {
1908 template<>
1909 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001910 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1911 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001912 if (Instruction::isCast(V.opcode))
1913 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1914 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001915 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001916 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1917 if (V.opcode == Instruction::Select)
1918 return new SelectConstantExpr(V.operands[0], V.operands[1],
1919 V.operands[2]);
1920 if (V.opcode == Instruction::ExtractElement)
1921 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1922 if (V.opcode == Instruction::InsertElement)
1923 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1924 V.operands[2]);
1925 if (V.opcode == Instruction::ShuffleVector)
1926 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1927 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001928 if (V.opcode == Instruction::InsertValue)
1929 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1930 V.indices, Ty);
1931 if (V.opcode == Instruction::ExtractValue)
1932 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001933 if (V.opcode == Instruction::GetElementPtr) {
1934 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001935 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001936 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001937
Reid Spenceree3c9912006-12-04 05:19:50 +00001938 // The compare instructions are weird. We have to encode the predicate
1939 // value and it is combined with the instruction opcode by multiplying
1940 // the opcode by one hundred. We must decode this to get the predicate.
1941 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001942 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001943 V.operands[0], V.operands[1]);
1944 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001945 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1946 V.operands[0], V.operands[1]);
1947 if (V.opcode == Instruction::VICmp)
1948 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1949 V.operands[0], V.operands[1]);
1950 if (V.opcode == Instruction::VFCmp)
1951 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001952 V.operands[0], V.operands[1]);
1953 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001954 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001955 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001956 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001957
Chris Lattner189d19f2003-11-21 20:23:48 +00001958 template<>
1959 struct ConvertConstantType<ConstantExpr, Type> {
1960 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1961 Constant *New;
1962 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 case Instruction::Trunc:
1964 case Instruction::ZExt:
1965 case Instruction::SExt:
1966 case Instruction::FPTrunc:
1967 case Instruction::FPExt:
1968 case Instruction::UIToFP:
1969 case Instruction::SIToFP:
1970 case Instruction::FPToUI:
1971 case Instruction::FPToSI:
1972 case Instruction::PtrToInt:
1973 case Instruction::IntToPtr:
1974 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001975 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001976 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001977 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001978 case Instruction::Select:
1979 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1980 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001981 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001982 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001983 default:
1984 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001985 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001986 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001987 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001988 break;
1989 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001990 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001991 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001992 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001993 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001994 break;
1995 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001996
Chris Lattner189d19f2003-11-21 20:23:48 +00001997 assert(New != OldC && "Didn't replace constant??");
1998 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001999 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00002000 }
2001 };
2002} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00002003
2004
Chris Lattner3e650af2004-08-04 04:48:01 +00002005static ExprMapKeyType getValType(ConstantExpr *CE) {
2006 std::vector<Constant*> Operands;
2007 Operands.reserve(CE->getNumOperands());
2008 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
2009 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00002010 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002011 CE->isCompare() ? CE->getPredicate() : 0,
2012 CE->hasIndices() ?
2013 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00002014}
2015
Chris Lattner69edc982006-09-28 00:35:06 +00002016static ManagedStatic<ValueMap<ExprMapKeyType, Type,
2017 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00002018
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002019/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00002020/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002022 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00002023 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002024 // Fold a few common cases
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002025 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00002027
Vikram S. Adve4c485332002-07-15 18:19:33 +00002028 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002029 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00002030 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00002031
Owen Anderson61794042009-06-17 20:10:08 +00002032 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002033 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002034}
Reid Spencerf37dc652006-12-05 19:14:13 +00002035
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002036Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037 Instruction::CastOps opc = Instruction::CastOps(oc);
2038 assert(Instruction::isCast(opc) && "opcode out of range");
2039 assert(C && Ty && "Null arguments to getCast");
2040 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
2041
2042 switch (opc) {
2043 default:
2044 assert(0 && "Invalid cast opcode");
2045 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002046 case Instruction::Trunc: return getTrunc(C, Ty);
2047 case Instruction::ZExt: return getZExt(C, Ty);
2048 case Instruction::SExt: return getSExt(C, Ty);
2049 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
2050 case Instruction::FPExt: return getFPExtend(C, Ty);
2051 case Instruction::UIToFP: return getUIToFP(C, Ty);
2052 case Instruction::SIToFP: return getSIToFP(C, Ty);
2053 case Instruction::FPToUI: return getFPToUI(C, Ty);
2054 case Instruction::FPToSI: return getFPToSI(C, Ty);
2055 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
2056 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
2057 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00002058 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00002060}
2061
Reid Spencer5c140882006-12-04 20:17:56 +00002062Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002063 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002064 return getCast(Instruction::BitCast, C, Ty);
2065 return getCast(Instruction::ZExt, C, Ty);
2066}
2067
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002068Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002069 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002070 return getCast(Instruction::BitCast, C, Ty);
2071 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00002072}
2073
2074Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002075 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002076 return getCast(Instruction::BitCast, C, Ty);
2077 return getCast(Instruction::Trunc, C, Ty);
2078}
2079
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002080Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00002081 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002082 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00002083
Chris Lattner03c49532007-01-15 02:27:26 +00002084 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002085 return getCast(Instruction::PtrToInt, S, Ty);
2086 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00002087}
2088
Reid Spencer56521c42006-12-12 00:51:07 +00002089Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
2090 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002091 assert(C->getType()->isIntOrIntVector() &&
2092 Ty->isIntOrIntVector() && "Invalid cast");
2093 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2094 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00002095 Instruction::CastOps opcode =
2096 (SrcBits == DstBits ? Instruction::BitCast :
2097 (SrcBits > DstBits ? Instruction::Trunc :
2098 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2099 return getCast(opcode, C, Ty);
2100}
2101
2102Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002103 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00002104 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002105 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2106 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00002107 if (SrcBits == DstBits)
2108 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00002109 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00002110 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00002111 return getCast(opcode, C, Ty);
2112}
2113
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002114Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002115#ifndef NDEBUG
2116 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2117 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2118#endif
2119 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2120 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
2121 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
2122 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002123 "SrcTy must be larger than DestTy for Trunc!");
2124
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002125 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126}
2127
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002128Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002129#ifndef NDEBUG
2130 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2131 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2132#endif
2133 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2134 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
2135 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
2136 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002137 "SrcTy must be smaller than DestTy for SExt!");
2138
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002139 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00002140}
2141
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002142Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002143#ifndef NDEBUG
2144 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2145 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2146#endif
2147 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2148 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
2149 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
2150 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151 "SrcTy must be smaller than DestTy for ZExt!");
2152
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002153 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002154}
2155
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002156Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002157#ifndef NDEBUG
2158 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2159 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2160#endif
2161 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2162 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2163 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002165 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166}
2167
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002168Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002169#ifndef NDEBUG
2170 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2171 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2172#endif
2173 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2174 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2175 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002177 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178}
2179
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002180Constant *ConstantExpr::getUIToFP(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() &&
2187 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002188 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189}
2190
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002191Constant *ConstantExpr::getSIToFP(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()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002198 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002199 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200}
2201
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002202Constant *ConstantExpr::getFPToUI(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 uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002210 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211}
2212
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002213Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002214#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002215 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2216 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002217#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002218 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2219 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2220 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002221 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222}
2223
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002224Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00002226 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002227 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228}
2229
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002230Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00002231 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002233 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002234}
2235
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002236Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 // BitCast implies a no-op cast of type only. No bits change. However, you
2238 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002239#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240 const Type *SrcTy = C->getType();
2241 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002242 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243
2244 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2245 // or nonptr->ptr). For all the other types, the cast is okay if source and
2246 // destination bit widths are identical.
2247 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2248 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002249#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002250 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002251
2252 // It is common to ask for a bitcast of a value to its own type, handle this
2253 // speedily.
2254 if (C->getType() == DstTy) return C;
2255
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002256 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002257}
2258
Duncan Sandsd334aca2009-05-21 15:52:21 +00002259Constant *ConstantExpr::getAlignOf(const Type *Ty) {
2260 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
2261 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
2262 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
2263 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
2264 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
2265 Constant *Indices[2] = { Zero, One };
2266 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
2267 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
2268}
2269
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00002270Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00002271 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00002272 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
2273 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00002274 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00002275 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00002276}
2277
Chris Lattnerb50d1352003-10-05 00:17:43 +00002278Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002279 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002280 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002281 assert(Opcode >= Instruction::BinaryOpsBegin &&
2282 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002283 "Invalid opcode in binary constant expression");
2284 assert(C1->getType() == C2->getType() &&
2285 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002286
Reid Spencer542964f2007-01-11 18:21:29 +00002287 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002288 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00002289 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002290
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002291 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002292 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002293
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002294 // Implicitly locked.
2295 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002296}
2297
Reid Spencer266e42b2006-12-23 06:05:41 +00002298Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002299 Constant *C1, Constant *C2) {
2300 bool isVectorType = C1->getType()->getTypeID() == Type::VectorTyID;
Reid Spencer266e42b2006-12-23 06:05:41 +00002301 switch (predicate) {
2302 default: assert(0 && "Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002303 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2304 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2305 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2306 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2307 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2308 case CmpInst::FCMP_TRUE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002309 return isVectorType ? getVFCmp(predicate, C1, C2)
2310 : getFCmp(predicate, C1, C2);
Nate Begemanc96e2e42008-07-25 17:35:37 +00002311 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2312 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2313 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2314 case CmpInst::ICMP_SLE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002315 return isVectorType ? getVICmp(predicate, C1, C2)
2316 : getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002317 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002318}
2319
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002320Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002321 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2322 if (C1->getType()->isFPOrFPVector()) {
2323 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2324 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2325 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2326 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002327#ifndef NDEBUG
2328 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002329 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002330 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002331 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002332 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002333 assert(C1->getType()->isIntOrIntVector() &&
2334 "Tried to create an integer operation on a non-integer type!");
2335 break;
2336 case Instruction::FAdd:
2337 case Instruction::FSub:
2338 case Instruction::FMul:
2339 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2340 assert(C1->getType()->isFPOrFPVector() &&
2341 "Tried to create a floating-point operation on a "
2342 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002343 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002344 case Instruction::UDiv:
2345 case Instruction::SDiv:
2346 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002347 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002348 "Tried to create an arithmetic operation on a non-arithmetic type!");
2349 break;
2350 case Instruction::FDiv:
2351 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002352 assert(C1->getType()->isFPOrFPVector() &&
2353 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002354 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002355 case Instruction::URem:
2356 case Instruction::SRem:
2357 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002358 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002359 "Tried to create an arithmetic operation on a non-arithmetic type!");
2360 break;
2361 case Instruction::FRem:
2362 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002363 assert(C1->getType()->isFPOrFPVector() &&
2364 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002365 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002366 case Instruction::And:
2367 case Instruction::Or:
2368 case Instruction::Xor:
2369 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002370 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002371 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002372 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002373 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002374 case Instruction::LShr:
2375 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002376 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002377 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002378 "Tried to create a shift operation on a non-integer type!");
2379 break;
2380 default:
2381 break;
2382 }
2383#endif
2384
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002385 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002386}
2387
Reid Spencer266e42b2006-12-23 06:05:41 +00002388Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002389 Constant *C1, Constant *C2) {
2390 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002391 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002392}
2393
Chris Lattner6e415c02004-03-12 05:54:04 +00002394Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002395 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002396 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002397
2398 if (ReqTy == V1->getType())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002399 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00002400 return SC; // Fold common cases
2401
2402 std::vector<Constant*> argVec(3, C);
2403 argVec[1] = V1;
2404 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002405 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002406
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002407 // Implicitly locked.
2408 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002409}
2410
Chris Lattnerb50d1352003-10-05 00:17:43 +00002411Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002412 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002413 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002414 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2415 Idxs+NumIdx) ==
2416 cast<PointerType>(ReqTy)->getElementType() &&
2417 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002418
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002419 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002420 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002421
Chris Lattnerb50d1352003-10-05 00:17:43 +00002422 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002423 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002424 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002425 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002426 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002427 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002428 for (unsigned i = 0; i != NumIdx; ++i)
2429 ArgVec.push_back(cast<Constant>(Idxs[i]));
2430 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002431
2432 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002433 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002434}
2435
Chris Lattner302116a2007-01-31 04:40:28 +00002436Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002437 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002438 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002439 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002440 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002441 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002442 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002443 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002444}
2445
Chris Lattner302116a2007-01-31 04:40:28 +00002446Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002447 unsigned NumIdx) {
2448 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002449}
2450
Chris Lattner302116a2007-01-31 04:40:28 +00002451
Reid Spenceree3c9912006-12-04 05:19:50 +00002452Constant *
2453ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2454 assert(LHS->getType() == RHS->getType());
2455 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2456 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2457
Reid Spencer266e42b2006-12-23 06:05:41 +00002458 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002459 return FC; // Fold a few common cases...
2460
2461 // Look up the constant in the table first to ensure uniqueness
2462 std::vector<Constant*> ArgVec;
2463 ArgVec.push_back(LHS);
2464 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002465 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002466 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002467
2468 // Implicitly locked.
2469 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002470}
2471
2472Constant *
2473ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2474 assert(LHS->getType() == RHS->getType());
2475 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2476
Reid Spencer266e42b2006-12-23 06:05:41 +00002477 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002478 return FC; // Fold a few common cases...
2479
2480 // Look up the constant in the table first to ensure uniqueness
2481 std::vector<Constant*> ArgVec;
2482 ArgVec.push_back(LHS);
2483 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002484 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002485 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002486
2487 // Implicitly locked.
2488 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002489}
2490
Nate Begemand2195702008-05-12 19:01:56 +00002491Constant *
2492ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
Chris Lattnereab49262008-07-14 05:17:31 +00002493 assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() &&
Nate Begemand2195702008-05-12 19:01:56 +00002494 "Tried to create vicmp operation on non-vector type!");
Nate Begemand2195702008-05-12 19:01:56 +00002495 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2496 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2497
Nate Begemanac7f3d92008-05-12 19:23:22 +00002498 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002499 const Type *EltTy = VTy->getElementType();
2500 unsigned NumElts = VTy->getNumElements();
2501
Chris Lattnereab49262008-07-14 05:17:31 +00002502 // See if we can fold the element-wise comparison of the LHS and RHS.
2503 SmallVector<Constant *, 16> LHSElts, RHSElts;
2504 LHS->getVectorElements(LHSElts);
2505 RHS->getVectorElements(RHSElts);
2506
2507 if (!LHSElts.empty() && !RHSElts.empty()) {
2508 SmallVector<Constant *, 16> Elts;
2509 for (unsigned i = 0; i != NumElts; ++i) {
2510 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2511 RHSElts[i]);
2512 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2513 if (FCI->getZExtValue())
2514 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2515 else
2516 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2517 } else if (FC && isa<UndefValue>(FC)) {
2518 Elts.push_back(UndefValue::get(EltTy));
2519 } else {
2520 break;
2521 }
Nate Begemand2195702008-05-12 19:01:56 +00002522 }
Chris Lattnereab49262008-07-14 05:17:31 +00002523 if (Elts.size() == NumElts)
2524 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002525 }
Nate Begemand2195702008-05-12 19:01:56 +00002526
2527 // Look up the constant in the table first to ensure uniqueness
2528 std::vector<Constant*> ArgVec;
2529 ArgVec.push_back(LHS);
2530 ArgVec.push_back(RHS);
2531 // Get the key type with both the opcode and predicate
2532 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002533
2534 // Implicitly locked.
2535 return ExprConstants->getOrCreate(LHS->getType(), Key);
Nate Begemand2195702008-05-12 19:01:56 +00002536}
2537
2538Constant *
2539ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2540 assert(isa<VectorType>(LHS->getType()) &&
2541 "Tried to create vfcmp operation on non-vector type!");
2542 assert(LHS->getType() == RHS->getType());
2543 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2544
2545 const VectorType *VTy = cast<VectorType>(LHS->getType());
2546 unsigned NumElts = VTy->getNumElements();
2547 const Type *EltTy = VTy->getElementType();
2548 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2549 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2550
Chris Lattnereab49262008-07-14 05:17:31 +00002551 // See if we can fold the element-wise comparison of the LHS and RHS.
2552 SmallVector<Constant *, 16> LHSElts, RHSElts;
2553 LHS->getVectorElements(LHSElts);
2554 RHS->getVectorElements(RHSElts);
2555
2556 if (!LHSElts.empty() && !RHSElts.empty()) {
2557 SmallVector<Constant *, 16> Elts;
2558 for (unsigned i = 0; i != NumElts; ++i) {
2559 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2560 RHSElts[i]);
2561 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2562 if (FCI->getZExtValue())
2563 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2564 else
2565 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2566 } else if (FC && isa<UndefValue>(FC)) {
2567 Elts.push_back(UndefValue::get(REltTy));
2568 } else {
2569 break;
2570 }
Nate Begemand2195702008-05-12 19:01:56 +00002571 }
Chris Lattnereab49262008-07-14 05:17:31 +00002572 if (Elts.size() == NumElts)
2573 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002574 }
Nate Begemand2195702008-05-12 19:01:56 +00002575
2576 // Look up the constant in the table first to ensure uniqueness
2577 std::vector<Constant*> ArgVec;
2578 ArgVec.push_back(LHS);
2579 ArgVec.push_back(RHS);
2580 // Get the key type with both the opcode and predicate
2581 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002582
2583 // Implicitly locked.
2584 return ExprConstants->getOrCreate(ResultTy, Key);
Nate Begemand2195702008-05-12 19:01:56 +00002585}
2586
Robert Bocchino23004482006-01-10 19:05:34 +00002587Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2588 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002589 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2590 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002591 // Look up the constant in the table first to ensure uniqueness
2592 std::vector<Constant*> ArgVec(1, Val);
2593 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002594 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002595
2596 // Implicitly locked.
2597 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002598}
2599
2600Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002601 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002602 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002603 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002604 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002605 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002606 Val, Idx);
2607}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002608
Robert Bocchinoca27f032006-01-17 20:07:22 +00002609Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2610 Constant *Elt, Constant *Idx) {
2611 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2612 return FC; // Fold a few common cases...
2613 // Look up the constant in the table first to ensure uniqueness
2614 std::vector<Constant*> ArgVec(1, Val);
2615 ArgVec.push_back(Elt);
2616 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002617 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002618
2619 // Implicitly locked.
2620 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002621}
2622
2623Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2624 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002625 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002626 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002627 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002628 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002629 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002630 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002631 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002632}
2633
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002634Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2635 Constant *V2, Constant *Mask) {
2636 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2637 return FC; // Fold a few common cases...
2638 // Look up the constant in the table first to ensure uniqueness
2639 std::vector<Constant*> ArgVec(1, V1);
2640 ArgVec.push_back(V2);
2641 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002642 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002643
2644 // Implicitly locked.
2645 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002646}
2647
2648Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2649 Constant *Mask) {
2650 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2651 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002652
2653 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2654 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2655 const Type *ShufTy = VectorType::get(EltTy, NElts);
2656 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002657}
2658
Dan Gohman12fce772008-05-15 19:50:34 +00002659Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2660 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002661 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002662 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2663 Idxs+NumIdx) == Val->getType() &&
2664 "insertvalue indices invalid!");
2665 assert(Agg->getType() == ReqTy &&
2666 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002667 assert(Agg->getType()->isFirstClassType() &&
2668 "Non-first-class type for constant InsertValue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002669 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
2670 assert(FC && "InsertValue constant expr couldn't be folded!");
2671 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002672}
2673
2674Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002675 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002676 assert(Agg->getType()->isFirstClassType() &&
2677 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002678
Dan Gohman0752bff2008-05-23 00:36:11 +00002679 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002680#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002681 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002682 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002683#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002684 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002685 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2686}
2687
2688Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002689 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002690 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2691 Idxs+NumIdx) == ReqTy &&
2692 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002693 assert(Agg->getType()->isFirstClassType() &&
2694 "Non-first-class type for constant extractvalue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002695 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
2696 assert(FC && "ExtractValue constant expr couldn't be folded!");
2697 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002698}
2699
2700Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002701 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002702 assert(Agg->getType()->isFirstClassType() &&
2703 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002704
2705 const Type *ReqTy =
2706 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2707 assert(ReqTy && "extractvalue indices invalid!");
2708 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2709}
2710
Reid Spencer2eadb532007-01-21 00:29:26 +00002711Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002712 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002713 if (PTy->getElementType()->isFloatingPoint()) {
2714 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002715 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002716 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002717 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002718
Dale Johannesen98d3a082007-09-14 22:26:36 +00002719 if (Ty->isFloatingPoint())
2720 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002721
2722 return Constant::getNullValue(Ty);
2723}
2724
Vikram S. Adve4c485332002-07-15 18:19:33 +00002725// destroyConstant - Remove the constant from the constant table...
2726//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002727void ConstantExpr::destroyConstant() {
2728 // Implicitly locked.
2729 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002730 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002731}
2732
Chris Lattner3cd8c562002-07-30 18:54:25 +00002733const char *ConstantExpr::getOpcodeName() const {
2734 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002735}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002736
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002737//===----------------------------------------------------------------------===//
2738// replaceUsesOfWithOnConstant implementations
2739
Chris Lattner913849b2007-08-21 00:55:23 +00002740/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2741/// 'From' to be uses of 'To'. This must update the uniquing data structures
2742/// etc.
2743///
2744/// Note that we intentionally replace all uses of From with To here. Consider
2745/// a large array that uses 'From' 1000 times. By handling this case all here,
2746/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2747/// single invocation handles all 1000 uses. Handling them one at a time would
2748/// work, but would be really slow because it would have to unique each updated
2749/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002750void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002751 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002752 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002753 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002754
Jim Laskeyc03caef2006-07-17 17:38:29 +00002755 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002756 Lookup.first.first = getType();
2757 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002758
Chris Lattnerb64419a2005-10-03 22:51:37 +00002759 std::vector<Constant*> &Values = Lookup.first.second;
2760 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002761
Chris Lattner8760ec72005-10-04 01:17:50 +00002762 // Fill values with the modified operands of the constant array. Also,
2763 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002764 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002765 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002766 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002767 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2768 Constant *Val = cast<Constant>(O->get());
2769 if (Val == From) {
2770 Val = ToC;
2771 ++NumUpdated;
2772 }
2773 Values.push_back(Val);
2774 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002775 } else {
2776 isAllZeros = true;
2777 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2778 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002779 if (Val == From) {
2780 Val = ToC;
2781 ++NumUpdated;
2782 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002783 Values.push_back(Val);
2784 if (isAllZeros) isAllZeros = Val->isNullValue();
2785 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002786 }
2787
Chris Lattnerb64419a2005-10-03 22:51:37 +00002788 Constant *Replacement = 0;
2789 if (isAllZeros) {
2790 Replacement = ConstantAggregateZero::get(getType());
2791 } else {
2792 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002793 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002794 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002795 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002796 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002797
2798 if (Exists) {
2799 Replacement = I->second;
2800 } else {
2801 // Okay, the new shape doesn't exist in the system yet. Instead of
2802 // creating a new constant array, inserting it, replaceallusesof'ing the
2803 // old with the new, then deleting the old... just update the current one
2804 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002805 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002806
Chris Lattner913849b2007-08-21 00:55:23 +00002807 // Update to the new value. Optimize for the case when we have a single
2808 // operand that we're changing, but handle bulk updates efficiently.
2809 if (NumUpdated == 1) {
2810 unsigned OperandToUpdate = U-OperandList;
2811 assert(getOperand(OperandToUpdate) == From &&
2812 "ReplaceAllUsesWith broken!");
2813 setOperand(OperandToUpdate, ToC);
2814 } else {
2815 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2816 if (getOperand(i) == From)
2817 setOperand(i, ToC);
2818 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002819 return;
2820 }
2821 }
2822
2823 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002824 assert(Replacement != this && "I didn't contain From!");
2825
Chris Lattner7a1450d2005-10-04 18:13:04 +00002826 // Everyone using this now uses the replacement.
2827 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002828
2829 // Delete the old constant!
2830 destroyConstant();
2831}
2832
2833void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002834 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002835 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002836 Constant *ToC = cast<Constant>(To);
2837
Chris Lattnerdff59112005-10-04 18:47:09 +00002838 unsigned OperandToUpdate = U-OperandList;
2839 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2840
Jim Laskeyc03caef2006-07-17 17:38:29 +00002841 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002842 Lookup.first.first = getType();
2843 Lookup.second = this;
2844 std::vector<Constant*> &Values = Lookup.first.second;
2845 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002846
Chris Lattnerdff59112005-10-04 18:47:09 +00002847
Chris Lattner8760ec72005-10-04 01:17:50 +00002848 // Fill values with the modified operands of the constant struct. Also,
2849 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002850 bool isAllZeros = false;
2851 if (!ToC->isNullValue()) {
2852 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2853 Values.push_back(cast<Constant>(O->get()));
2854 } else {
2855 isAllZeros = true;
2856 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2857 Constant *Val = cast<Constant>(O->get());
2858 Values.push_back(Val);
2859 if (isAllZeros) isAllZeros = Val->isNullValue();
2860 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002861 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002862 Values[OperandToUpdate] = ToC;
2863
Chris Lattner8760ec72005-10-04 01:17:50 +00002864 Constant *Replacement = 0;
2865 if (isAllZeros) {
2866 Replacement = ConstantAggregateZero::get(getType());
2867 } else {
2868 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002869 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002870 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002871 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002872 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002873
2874 if (Exists) {
2875 Replacement = I->second;
2876 } else {
2877 // Okay, the new shape doesn't exist in the system yet. Instead of
2878 // creating a new constant struct, inserting it, replaceallusesof'ing the
2879 // old with the new, then deleting the old... just update the current one
2880 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002881 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002882
Chris Lattnerdff59112005-10-04 18:47:09 +00002883 // Update to the new value.
2884 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002885 return;
2886 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002887 }
2888
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002889 assert(Replacement != this && "I didn't contain From!");
2890
Chris Lattner7a1450d2005-10-04 18:13:04 +00002891 // Everyone using this now uses the replacement.
2892 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002893
2894 // Delete the old constant!
2895 destroyConstant();
2896}
2897
Reid Spencerd84d35b2007-02-15 02:26:10 +00002898void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002899 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002900 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2901
2902 std::vector<Constant*> Values;
2903 Values.reserve(getNumOperands()); // Build replacement array...
2904 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2905 Constant *Val = getOperand(i);
2906 if (Val == From) Val = cast<Constant>(To);
2907 Values.push_back(Val);
2908 }
2909
Reid Spencerd84d35b2007-02-15 02:26:10 +00002910 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002911 assert(Replacement != this && "I didn't contain From!");
2912
Chris Lattner7a1450d2005-10-04 18:13:04 +00002913 // Everyone using this now uses the replacement.
2914 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002915
2916 // Delete the old constant!
2917 destroyConstant();
2918}
2919
2920void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002921 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002922 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2923 Constant *To = cast<Constant>(ToV);
2924
2925 Constant *Replacement = 0;
2926 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002927 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002928 Constant *Pointer = getOperand(0);
2929 Indices.reserve(getNumOperands()-1);
2930 if (Pointer == From) Pointer = To;
2931
2932 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2933 Constant *Val = getOperand(i);
2934 if (Val == From) Val = To;
2935 Indices.push_back(Val);
2936 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002937 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2938 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002939 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002940 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002941 if (Agg == From) Agg = To;
2942
Dan Gohman1ecaf452008-05-31 00:58:22 +00002943 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002944 Replacement = ConstantExpr::getExtractValue(Agg,
2945 &Indices[0], Indices.size());
2946 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002947 Constant *Agg = getOperand(0);
2948 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002949 if (Agg == From) Agg = To;
2950 if (Val == From) Val = To;
2951
Dan Gohman1ecaf452008-05-31 00:58:22 +00002952 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002953 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2954 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002956 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002957 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002958 } else if (getOpcode() == Instruction::Select) {
2959 Constant *C1 = getOperand(0);
2960 Constant *C2 = getOperand(1);
2961 Constant *C3 = getOperand(2);
2962 if (C1 == From) C1 = To;
2963 if (C2 == From) C2 = To;
2964 if (C3 == From) C3 = To;
2965 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002966 } else if (getOpcode() == Instruction::ExtractElement) {
2967 Constant *C1 = getOperand(0);
2968 Constant *C2 = getOperand(1);
2969 if (C1 == From) C1 = To;
2970 if (C2 == From) C2 = To;
2971 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002972 } else if (getOpcode() == Instruction::InsertElement) {
2973 Constant *C1 = getOperand(0);
2974 Constant *C2 = getOperand(1);
2975 Constant *C3 = getOperand(1);
2976 if (C1 == From) C1 = To;
2977 if (C2 == From) C2 = To;
2978 if (C3 == From) C3 = To;
2979 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2980 } else if (getOpcode() == Instruction::ShuffleVector) {
2981 Constant *C1 = getOperand(0);
2982 Constant *C2 = getOperand(1);
2983 Constant *C3 = getOperand(2);
2984 if (C1 == From) C1 = To;
2985 if (C2 == From) C2 = To;
2986 if (C3 == From) C3 = To;
2987 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002988 } else if (isCompare()) {
2989 Constant *C1 = getOperand(0);
2990 Constant *C2 = getOperand(1);
2991 if (C1 == From) C1 = To;
2992 if (C2 == From) C2 = To;
2993 if (getOpcode() == Instruction::ICmp)
2994 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002995 else if (getOpcode() == Instruction::FCmp)
Reid Spenceree3c9912006-12-04 05:19:50 +00002996 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002997 else if (getOpcode() == Instruction::VICmp)
2998 Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2);
2999 else {
3000 assert(getOpcode() == Instruction::VFCmp);
3001 Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2);
3002 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003003 } else if (getNumOperands() == 2) {
3004 Constant *C1 = getOperand(0);
3005 Constant *C2 = getOperand(1);
3006 if (C1 == From) C1 = To;
3007 if (C2 == From) C2 = To;
3008 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
3009 } else {
3010 assert(0 && "Unknown ConstantExpr type!");
3011 return;
3012 }
3013
3014 assert(Replacement != this && "I didn't contain From!");
3015
Chris Lattner7a1450d2005-10-04 18:13:04 +00003016 // Everyone using this now uses the replacement.
3017 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003018
3019 // Delete the old constant!
3020 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00003021}
Nick Lewycky49f89192009-04-04 07:22:01 +00003022
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003023void MDNode::replaceElement(Value *From, Value *To) {
3024 SmallVector<Value*, 4> Values;
3025 Values.reserve(getNumElements()); // Build replacement array...
3026 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3027 Value *Val = getElement(i);
3028 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00003029 Values.push_back(Val);
3030 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003031
3032 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00003033 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003034
Nick Lewycky49f89192009-04-04 07:22:01 +00003035 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003036
Nick Lewycky49f89192009-04-04 07:22:01 +00003037 destroyConstant();
3038}