blob: 138a19646243ca3bd202b5cdb7a406de8ac39c6a [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 Anderson2d7231d2009-06-17 18:40:29 +000028#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000029#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000032#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000033#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner2f7c9632001-06-06 20:29:01 +000036//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000037// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
39
Owen Andersond830eb82009-06-18 19:10:19 +000040// Becomes a no-op when multithreading is disabled.
41ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000042
Chris Lattner3462ae32001-12-03 22:26:30 +000043void Constant::destroyConstantImpl() {
44 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000045 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000046 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000047 // but they don't know that. Because we only find out when the CPV is
48 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000049 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000050 //
51 while (!use_empty()) {
52 Value *V = use_back();
53#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000054 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000055 DOUT << "While deleting: " << *this
56 << "\n\nUse still stuck around after Def is destroyed: "
57 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000058#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000059 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000060 Constant *CV = cast<Constant>(V);
61 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000062
63 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000064 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000065 }
66
67 // Value has no outstanding references it is safe to delete it now...
68 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000069}
Chris Lattner2f7c9632001-06-06 20:29:01 +000070
Chris Lattner23dd1f62006-10-20 00:27:06 +000071/// canTrap - Return true if evaluation of this constant could trap. This is
72/// true for things like constant expressions that could divide by zero.
73bool Constant::canTrap() const {
74 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
75 // The only thing that could possibly trap are constant exprs.
76 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
77 if (!CE) return false;
78
79 // ConstantExpr traps if any operands can trap.
80 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
81 if (getOperand(i)->canTrap())
82 return true;
83
84 // Otherwise, only specific operations can trap.
85 switch (CE->getOpcode()) {
86 default:
87 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000088 case Instruction::UDiv:
89 case Instruction::SDiv:
90 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000091 case Instruction::URem:
92 case Instruction::SRem:
93 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000094 // Div and rem can trap if the RHS is not known to be non-zero.
95 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
96 return true;
97 return false;
98 }
99}
100
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000101/// ContainsRelocations - Return true if the constant value contains relocations
102/// which cannot be resolved at compile time. Kind argument is used to filter
103/// only 'interesting' sorts of relocations.
104bool Constant::ContainsRelocations(unsigned Kind) const {
105 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
106 bool isLocal = GV->hasLocalLinkage();
107 if ((Kind & Reloc::Local) && isLocal) {
108 // Global has local linkage and 'local' kind of relocations are
109 // requested
110 return true;
111 }
112
113 if ((Kind & Reloc::Global) && !isLocal) {
114 // Global has non-local linkage and 'global' kind of relocations are
115 // requested
116 return true;
117 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000118
119 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000120 }
121
Evan Chengf9e003b2007-03-08 00:59:12 +0000122 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000123 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000124 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000125
Evan Chengf9e003b2007-03-08 00:59:12 +0000126 return false;
127}
128
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129// Static constructor to create a '0' constant of arbitrary type...
Owen Andersonb07dd952009-06-19 23:16:19 +0000130Constant *Constant::getNullValue(const Type *Ty, bool locked) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000131 static uint64_t zero[2] = {0, 0};
Chris Lattner6b727592004-06-17 18:19:28 +0000132 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000133 case Type::IntegerTyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000134 return ConstantInt::get(Ty, 0, locked);
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000135 case Type::FloatTyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000136 return ConstantFP::get(APFloat(APInt(32, 0)), locked);
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000137 case Type::DoubleTyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000138 return ConstantFP::get(APFloat(APInt(64, 0)), locked);
Dale Johannesenbdad8092007-08-09 22:51:36 +0000139 case Type::X86_FP80TyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000140 return ConstantFP::get(APFloat(APInt(80, 2, zero)), locked);
Dale Johannesenbdad8092007-08-09 22:51:36 +0000141 case Type::FP128TyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000142 return ConstantFP::get(APFloat(APInt(128, 2, zero), true), locked);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000143 case Type::PPC_FP128TyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000144 return ConstantFP::get(APFloat(APInt(128, 2, zero)), locked);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000145 case Type::PointerTyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000146 return ConstantPointerNull::get(cast<PointerType>(Ty), locked);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000147 case Type::StructTyID:
148 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000149 case Type::VectorTyID:
Owen Andersonb07dd952009-06-19 23:16:19 +0000150 return ConstantAggregateZero::get(Ty, locked);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000151 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000152 // Function, Label, or Opaque type?
153 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000154 return 0;
155 }
156}
157
Chris Lattner72e39582007-06-15 06:10:53 +0000158Constant *Constant::getAllOnesValue(const Type *Ty) {
159 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
160 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
161 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
162}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000163
164// Static constructor to create an integral constant with all bits set
Owen Andersonb07dd952009-06-19 23:16:19 +0000165ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty, bool locked) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000166 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Owen Andersonb07dd952009-06-19 23:16:19 +0000167 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()), locked);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000168 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000169}
170
Dan Gohman30978072007-05-24 14:36:04 +0000171/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000172/// has all its bits set to true.
173/// @brief Get the all ones value
Owen Andersonb07dd952009-06-19 23:16:19 +0000174ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty,
175 bool locked) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000176 std::vector<Constant*> Elts;
177 Elts.resize(Ty->getNumElements(),
Owen Andersonb07dd952009-06-19 23:16:19 +0000178 ConstantInt::getAllOnesValue(Ty->getElementType(), locked));
Dan Gohman30978072007-05-24 14:36:04 +0000179 assert(Elts[0] && "Not a vector integer type!");
Owen Andersonb07dd952009-06-19 23:16:19 +0000180 return cast<ConstantVector>(ConstantVector::get(Elts, locked));
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 Andersonb07dd952009-06-19 23:16:19 +0000279 uint64_t V, bool isSigned, bool locked) {
280 return get(APInt(Ty->getBitWidth(), V, isSigned), locked);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000281}
282
Owen Andersonb07dd952009-06-19 23:16:19 +0000283Constant *ConstantInt::get(const Type *Ty, uint64_t V,
284 bool isSigned, bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000285 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
286
287 // For vectors, broadcast the value.
288 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
289 return
Owen Andersonb07dd952009-06-19 23:16:19 +0000290 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C),
291 locked);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000292
293 return C;
Reid Spencerb31bffe2007-02-26 23:54:03 +0000294}
295
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000296// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000297// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000298// operator== and operator!= to ensure that the DenseMap doesn't attempt to
299// compare APInt's of different widths, which would violate an APInt class
300// invariant which generates an assertion.
Owen Andersonb07dd952009-06-19 23:16:19 +0000301ConstantInt *ConstantInt::get(const APInt& V, bool locked) {
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000302 // Get the corresponding integer type for the bit width of the value.
303 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000304 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000305 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000306
Owen Andersonb07dd952009-06-19 23:16:19 +0000307 if (locked) ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000308 ConstantInt *&Slot = (*IntConstants)[Key];
Owen Andersonb07dd952009-06-19 23:16:19 +0000309 if (locked) ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000310
Owen Andersond830eb82009-06-18 19:10:19 +0000311 if (!Slot) {
Owen Andersonb07dd952009-06-19 23:16:19 +0000312 if (locked) {
313 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
314 ConstantInt *&NewSlot = (*IntConstants)[Key];
315 if (!Slot) {
316 NewSlot = new ConstantInt(ITy, V);
317 }
318 return NewSlot;
319 } else {
320 Slot = new ConstantInt(ITy, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000321 }
Owen Anderson2d7231d2009-06-17 18:40:29 +0000322 }
Owen Andersonb07dd952009-06-19 23:16:19 +0000323
324 return Slot;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000325}
326
Owen Andersonb07dd952009-06-19 23:16:19 +0000327Constant *ConstantInt::get(const Type *Ty, const APInt &V, bool locked) {
328 ConstantInt *C = ConstantInt::get(V, locked);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000329 assert(C->getType() == Ty->getScalarType() &&
330 "ConstantInt type doesn't match the type implied by its value!");
331
332 // For vectors, broadcast the value.
333 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
334 return
Owen Andersonb07dd952009-06-19 23:16:19 +0000335 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C),
336 locked);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000337
338 return C;
339}
340
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000341//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000342// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000343//===----------------------------------------------------------------------===//
344
Chris Lattner98bd9392008-04-09 06:38:30 +0000345static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
346 if (Ty == Type::FloatTy)
347 return &APFloat::IEEEsingle;
348 if (Ty == Type::DoubleTy)
349 return &APFloat::IEEEdouble;
350 if (Ty == Type::X86_FP80Ty)
351 return &APFloat::x87DoubleExtended;
352 else if (Ty == Type::FP128Ty)
353 return &APFloat::IEEEquad;
354
355 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
356 return &APFloat::PPCDoubleDouble;
357}
358
Dale Johannesend246b2c2007-08-30 00:23:21 +0000359ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
360 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000361 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
362 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363}
364
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000365bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000366 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000367}
368
Dale Johannesen98d3a082007-09-14 22:26:36 +0000369ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
370 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
371 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000372 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000373}
374
Dale Johannesend246b2c2007-08-30 00:23:21 +0000375bool ConstantFP::isExactlyValue(const APFloat& V) const {
376 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000377}
378
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000379namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000380 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000381 struct KeyTy {
382 APFloat val;
383 KeyTy(const APFloat& V) : val(V){}
384 KeyTy(const KeyTy& that) : val(that.val) {}
385 bool operator==(const KeyTy& that) const {
386 return this->val.bitwiseIsEqual(that.val);
387 }
388 bool operator!=(const KeyTy& that) const {
389 return !this->operator==(that);
390 }
391 };
392 static inline KeyTy getEmptyKey() {
393 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000394 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000395 static inline KeyTy getTombstoneKey() {
396 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000397 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000398 static unsigned getHashValue(const KeyTy &Key) {
399 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000400 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000401 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
402 return LHS == RHS;
403 }
Dale Johannesena719a602007-08-24 00:56:33 +0000404 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000405 };
406}
407
408//---- ConstantFP::get() implementation...
409//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000410typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000411 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000412
Dale Johannesena719a602007-08-24 00:56:33 +0000413static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000414
Owen Andersonb07dd952009-06-19 23:16:19 +0000415ConstantFP *ConstantFP::get(const APFloat &V, bool locked) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000416 DenseMapAPFloatKeyInfo::KeyTy Key(V);
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000417
Owen Andersonb07dd952009-06-19 23:16:19 +0000418 if (locked) ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000419 ConstantFP *&Slot = (*FPConstants)[Key];
Owen Andersonb07dd952009-06-19 23:16:19 +0000420 if (locked) ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000421
Owen Andersond830eb82009-06-18 19:10:19 +0000422 if (!Slot) {
Owen Andersonb07dd952009-06-19 23:16:19 +0000423 if (locked) {
424 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
425 ConstantFP *&NewSlot = (*FPConstants)[Key];
426 if (!NewSlot) {
427 const Type *Ty;
428 if (&V.getSemantics() == &APFloat::IEEEsingle)
429 Ty = Type::FloatTy;
430 else if (&V.getSemantics() == &APFloat::IEEEdouble)
431 Ty = Type::DoubleTy;
432 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
433 Ty = Type::X86_FP80Ty;
434 else if (&V.getSemantics() == &APFloat::IEEEquad)
435 Ty = Type::FP128Ty;
436 else {
437 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
438 "Unknown FP format");
439 Ty = Type::PPC_FP128Ty;
440 }
441 NewSlot = new ConstantFP(Ty, V);
442 }
443
444 return NewSlot;
445 } else {
Owen Andersond830eb82009-06-18 19:10:19 +0000446 const Type *Ty;
447 if (&V.getSemantics() == &APFloat::IEEEsingle)
448 Ty = Type::FloatTy;
449 else if (&V.getSemantics() == &APFloat::IEEEdouble)
450 Ty = Type::DoubleTy;
451 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
452 Ty = Type::X86_FP80Ty;
453 else if (&V.getSemantics() == &APFloat::IEEEquad)
454 Ty = Type::FP128Ty;
455 else {
456 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
457 "Unknown FP format");
458 Ty = Type::PPC_FP128Ty;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000459 }
Owen Andersonb07dd952009-06-19 23:16:19 +0000460 Slot = new ConstantFP(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000461 }
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000462 }
Owen Andersond830eb82009-06-18 19:10:19 +0000463
464 return Slot;
Dale Johannesend246b2c2007-08-30 00:23:21 +0000465}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000466
Chris Lattner98bd9392008-04-09 06:38:30 +0000467/// get() - This returns a constant fp for the specified value in the
468/// specified type. This should only be used for simple constant values like
469/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Owen Andersonb07dd952009-06-19 23:16:19 +0000470Constant *ConstantFP::get(const Type *Ty, double V, bool locked) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000471 APFloat FV(V);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000472 bool ignored;
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000473 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
474 APFloat::rmNearestTiesToEven, &ignored);
Owen Andersonb07dd952009-06-19 23:16:19 +0000475 Constant *C = get(FV, locked);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000476
477 // For vectors, broadcast the value.
478 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
479 return
Owen Andersonb07dd952009-06-19 23:16:19 +0000480 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C),
481 locked);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000482
483 return C;
Chris Lattner98bd9392008-04-09 06:38:30 +0000484}
485
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000486//===----------------------------------------------------------------------===//
487// ConstantXXX Classes
488//===----------------------------------------------------------------------===//
489
490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000492 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000493 : Constant(T, ConstantArrayVal,
494 OperandTraits<ConstantArray>::op_end(this) - V.size(),
495 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000496 assert(V.size() == T->getNumElements() &&
497 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000498 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000499 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
500 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000501 Constant *C = *I;
502 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000503 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000504 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000505 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000506 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000507 }
508}
509
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000510
Chris Lattner3462ae32001-12-03 22:26:30 +0000511ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000512 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000513 : Constant(T, ConstantStructVal,
514 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
515 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000516 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000517 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000518 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000519 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
520 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000521 Constant *C = *I;
522 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000523 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000524 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000525 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000526 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000527 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000528 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000529 }
530}
531
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000532
Reid Spencerd84d35b2007-02-15 02:26:10 +0000533ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000534 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000535 : Constant(T, ConstantVectorVal,
536 OperandTraits<ConstantVector>::op_end(this) - V.size(),
537 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000538 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000539 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
540 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000541 Constant *C = *I;
542 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000543 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000544 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000545 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000546 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000547 }
548}
549
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000550
Gabor Greiff6caff662008-05-10 08:32:32 +0000551namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000552// We declare several classes private to this file, so use an anonymous
553// namespace
554namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000555
Gordon Henriksen14a55692007-12-10 02:14:30 +0000556/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
557/// behind the scenes to implement unary constant exprs.
558class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000559 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000560public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000561 // allocate space for exactly one operand
562 void *operator new(size_t s) {
563 return User::operator new(s, 1);
564 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000565 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000566 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
567 Op<0>() = C;
568 }
569 /// Transparently provide more efficient getOperand methods.
570 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000571};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000572
Gordon Henriksen14a55692007-12-10 02:14:30 +0000573/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
574/// behind the scenes to implement binary constant exprs.
575class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000576 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000577public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000578 // allocate space for exactly two operands
579 void *operator new(size_t s) {
580 return User::operator new(s, 2);
581 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000582 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000583 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000584 Op<0>() = C1;
585 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000586 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000587 /// Transparently provide more efficient getOperand methods.
588 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000589};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000590
Gordon Henriksen14a55692007-12-10 02:14:30 +0000591/// SelectConstantExpr - This class is private to Constants.cpp, and is used
592/// behind the scenes to implement select constant exprs.
593class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000594 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000595public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000596 // allocate space for exactly three operands
597 void *operator new(size_t s) {
598 return User::operator new(s, 3);
599 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000600 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000601 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000602 Op<0>() = C1;
603 Op<1>() = C2;
604 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000605 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000606 /// Transparently provide more efficient getOperand methods.
607 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000608};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000609
Gordon Henriksen14a55692007-12-10 02:14:30 +0000610/// ExtractElementConstantExpr - This class is private to
611/// Constants.cpp, and is used behind the scenes to implement
612/// extractelement constant exprs.
613class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000614 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000615public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000616 // allocate space for exactly two operands
617 void *operator new(size_t s) {
618 return User::operator new(s, 2);
619 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000620 ExtractElementConstantExpr(Constant *C1, Constant *C2)
621 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000623 Op<0>() = C1;
624 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000625 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000626 /// Transparently provide more efficient getOperand methods.
627 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000628};
Robert Bocchino23004482006-01-10 19:05:34 +0000629
Gordon Henriksen14a55692007-12-10 02:14:30 +0000630/// InsertElementConstantExpr - This class is private to
631/// Constants.cpp, and is used behind the scenes to implement
632/// insertelement constant exprs.
633class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000634 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000635public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000636 // allocate space for exactly three operands
637 void *operator new(size_t s) {
638 return User::operator new(s, 3);
639 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000640 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
641 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000642 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000643 Op<0>() = C1;
644 Op<1>() = C2;
645 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000646 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000647 /// Transparently provide more efficient getOperand methods.
648 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000649};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000650
Gordon Henriksen14a55692007-12-10 02:14:30 +0000651/// ShuffleVectorConstantExpr - This class is private to
652/// Constants.cpp, and is used behind the scenes to implement
653/// shufflevector constant exprs.
654class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000655 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000656public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000657 // allocate space for exactly three operands
658 void *operator new(size_t s) {
659 return User::operator new(s, 3);
660 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000661 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000662 : ConstantExpr(VectorType::get(
663 cast<VectorType>(C1->getType())->getElementType(),
664 cast<VectorType>(C3->getType())->getNumElements()),
665 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000666 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000667 Op<0>() = C1;
668 Op<1>() = C2;
669 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000670 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000671 /// Transparently provide more efficient getOperand methods.
672 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000673};
674
Dan Gohman12fce772008-05-15 19:50:34 +0000675/// ExtractValueConstantExpr - This class is private to
676/// Constants.cpp, and is used behind the scenes to implement
677/// extractvalue constant exprs.
678class VISIBILITY_HIDDEN ExtractValueConstantExpr : 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, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000684 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000685 ExtractValueConstantExpr(Constant *Agg,
686 const SmallVector<unsigned, 4> &IdxList,
687 const Type *DestTy)
688 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
689 Indices(IdxList) {
690 Op<0>() = Agg;
691 }
692
Dan Gohman7bb04502008-05-31 19:09:08 +0000693 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000694 const SmallVector<unsigned, 4> Indices;
695
Dan Gohman12fce772008-05-15 19:50:34 +0000696 /// Transparently provide more efficient getOperand methods.
697 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
698};
699
700/// InsertValueConstantExpr - This class is private to
701/// Constants.cpp, and is used behind the scenes to implement
702/// insertvalue constant exprs.
703class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000704 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000705public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000706 // allocate space for exactly one operand
707 void *operator new(size_t s) {
708 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000709 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000710 InsertValueConstantExpr(Constant *Agg, Constant *Val,
711 const SmallVector<unsigned, 4> &IdxList,
712 const Type *DestTy)
713 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
714 Indices(IdxList) {
715 Op<0>() = Agg;
716 Op<1>() = Val;
717 }
718
Dan Gohman7bb04502008-05-31 19:09:08 +0000719 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000720 const SmallVector<unsigned, 4> Indices;
721
Dan Gohman12fce772008-05-15 19:50:34 +0000722 /// Transparently provide more efficient getOperand methods.
723 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
724};
725
726
Gordon Henriksen14a55692007-12-10 02:14:30 +0000727/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
728/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000729class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000730 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000731 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000732public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000733 static GetElementPtrConstantExpr *Create(Constant *C,
734 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000735 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000736 return new(IdxList.size() + 1)
737 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000738 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000739 /// Transparently provide more efficient getOperand methods.
740 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000741};
742
743// CompareConstantExpr - This class is private to Constants.cpp, and is used
744// behind the scenes to implement ICmp and FCmp constant expressions. This is
745// needed in order to store the predicate value for these instructions.
746struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000747 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
748 // allocate space for exactly two operands
749 void *operator new(size_t s) {
750 return User::operator new(s, 2);
751 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000752 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000753 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
754 unsigned short pred, Constant* LHS, Constant* RHS)
755 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000756 Op<0>() = LHS;
757 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000758 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000759 /// Transparently provide more efficient getOperand methods.
760 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000761};
762
763} // end anonymous namespace
764
Gabor Greiff6caff662008-05-10 08:32:32 +0000765template <>
766struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
767};
768DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
769
770template <>
771struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
772};
773DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
774
775template <>
776struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
777};
778DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
779
780template <>
781struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
782};
783DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
784
785template <>
786struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
787};
788DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
789
790template <>
791struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
792};
793DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
794
Dan Gohman12fce772008-05-15 19:50:34 +0000795template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000796struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000797};
Dan Gohman12fce772008-05-15 19:50:34 +0000798DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
799
800template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000801struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000802};
Dan Gohman12fce772008-05-15 19:50:34 +0000803DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
804
Gabor Greiff6caff662008-05-10 08:32:32 +0000805template <>
806struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
807};
808
809GetElementPtrConstantExpr::GetElementPtrConstantExpr
810 (Constant *C,
811 const std::vector<Constant*> &IdxList,
812 const Type *DestTy)
813 : ConstantExpr(DestTy, Instruction::GetElementPtr,
814 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
815 - (IdxList.size()+1),
816 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000817 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000818 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000819 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000820}
821
822DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
823
824
825template <>
826struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
827};
828DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
829
830
831} // End llvm namespace
832
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000833
834// Utility function for determining if a ConstantExpr is a CastOp or not. This
835// can't be inline because we don't want to #include Instruction.h into
836// Constant.h
837bool ConstantExpr::isCast() const {
838 return Instruction::isCast(getOpcode());
839}
840
Reid Spenceree3c9912006-12-04 05:19:50 +0000841bool ConstantExpr::isCompare() const {
Chris Lattnereab49262008-07-14 05:17:31 +0000842 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp ||
843 getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000844}
845
Dan Gohman1ecaf452008-05-31 00:58:22 +0000846bool ConstantExpr::hasIndices() const {
847 return getOpcode() == Instruction::ExtractValue ||
848 getOpcode() == Instruction::InsertValue;
849}
850
851const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
852 if (const ExtractValueConstantExpr *EVCE =
853 dyn_cast<ExtractValueConstantExpr>(this))
854 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000855
856 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000857}
858
Chris Lattner817175f2004-03-29 02:37:53 +0000859/// ConstantExpr::get* - Return some common constants without having to
860/// specify the full Instruction::OPCODE identifier.
861///
862Constant *ConstantExpr::getNeg(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000863 // API compatibility: Adjust integer opcodes to floating-point opcodes.
864 if (C->getType()->isFPOrFPVector())
865 return getFNeg(C);
866 assert(C->getType()->isIntOrIntVector() &&
867 "Cannot NEG a nonintegral value!");
Reid Spencer2eadb532007-01-21 00:29:26 +0000868 return get(Instruction::Sub,
869 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
870 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000871}
Dan Gohmana5b96452009-06-04 22:49:04 +0000872Constant *ConstantExpr::getFNeg(Constant *C) {
873 assert(C->getType()->isFPOrFPVector() &&
874 "Cannot FNEG a non-floating-point value!");
875 return get(Instruction::FSub,
876 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
877 C);
878}
Chris Lattner817175f2004-03-29 02:37:53 +0000879Constant *ConstantExpr::getNot(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000880 assert(C->getType()->isIntOrIntVector() &&
881 "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000882 return get(Instruction::Xor, C,
Dale Johannesen47a5ef32008-08-21 21:20:09 +0000883 Constant::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000884}
Owen Andersonb07dd952009-06-19 23:16:19 +0000885Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2, bool locked) {
886 return get(Instruction::Add, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000887}
Owen Andersonb07dd952009-06-19 23:16:19 +0000888Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2, bool locked) {
889 return get(Instruction::FAdd, C1, C2, locked);
Dan Gohmana5b96452009-06-04 22:49:04 +0000890}
Owen Andersonb07dd952009-06-19 23:16:19 +0000891Constant *ConstantExpr::getSub(Constant *C1, Constant *C2, bool locked) {
892 return get(Instruction::Sub, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000893}
Owen Andersonb07dd952009-06-19 23:16:19 +0000894Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2, bool locked) {
895 return get(Instruction::FSub, C1, C2, locked);
Dan Gohmana5b96452009-06-04 22:49:04 +0000896}
Owen Andersonb07dd952009-06-19 23:16:19 +0000897Constant *ConstantExpr::getMul(Constant *C1, Constant *C2, bool locked) {
898 return get(Instruction::Mul, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000899}
Owen Andersonb07dd952009-06-19 23:16:19 +0000900Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2, bool locked) {
901 return get(Instruction::FMul, C1, C2, locked);
Dan Gohmana5b96452009-06-04 22:49:04 +0000902}
Owen Andersonb07dd952009-06-19 23:16:19 +0000903Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool locked) {
904 return get(Instruction::UDiv, C1, C2, locked);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000905}
Owen Andersonb07dd952009-06-19 23:16:19 +0000906Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool locked) {
907 return get(Instruction::SDiv, C1, C2, locked);
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000908}
Owen Andersonb07dd952009-06-19 23:16:19 +0000909Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2, bool locked) {
910 return get(Instruction::FDiv, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000911}
Owen Andersonb07dd952009-06-19 23:16:19 +0000912Constant *ConstantExpr::getURem(Constant *C1, Constant *C2, bool locked) {
913 return get(Instruction::URem, C1, C2, locked);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000914}
Owen Andersonb07dd952009-06-19 23:16:19 +0000915Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2, bool locked) {
916 return get(Instruction::SRem, C1, C2, locked);
Reid Spencer7eb55b32006-11-02 01:53:59 +0000917}
Owen Andersonb07dd952009-06-19 23:16:19 +0000918Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2, bool locked) {
919 return get(Instruction::FRem, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000920}
Owen Andersonb07dd952009-06-19 23:16:19 +0000921Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2, bool locked) {
922 return get(Instruction::And, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000923}
Owen Andersonb07dd952009-06-19 23:16:19 +0000924Constant *ConstantExpr::getOr(Constant *C1, Constant *C2, bool locked) {
925 return get(Instruction::Or, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000926}
Owen Andersonb07dd952009-06-19 23:16:19 +0000927Constant *ConstantExpr::getXor(Constant *C1, Constant *C2, bool locked) {
928 return get(Instruction::Xor, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000929}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000930unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000931 assert(getOpcode() == Instruction::FCmp ||
932 getOpcode() == Instruction::ICmp ||
933 getOpcode() == Instruction::VFCmp ||
934 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000935 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000936}
Owen Andersonb07dd952009-06-19 23:16:19 +0000937Constant *ConstantExpr::getShl(Constant *C1, Constant *C2, bool locked) {
938 return get(Instruction::Shl, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000939}
Owen Andersonb07dd952009-06-19 23:16:19 +0000940Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool locked) {
941 return get(Instruction::LShr, C1, C2, locked);
Chris Lattner817175f2004-03-29 02:37:53 +0000942}
Owen Andersonb07dd952009-06-19 23:16:19 +0000943Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool locked) {
944 return get(Instruction::AShr, C1, C2, locked);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000945}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000946
Chris Lattner7c1018a2006-07-14 19:37:40 +0000947/// getWithOperandReplaced - Return a constant expression identical to this
948/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000949Constant *
950ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000951 assert(OpNo < getNumOperands() && "Operand num is out of range!");
952 assert(Op->getType() == getOperand(OpNo)->getType() &&
953 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000954 if (getOperand(OpNo) == Op)
955 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000956
Chris Lattner227816342006-07-14 22:20:01 +0000957 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000958 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000959 case Instruction::Trunc:
960 case Instruction::ZExt:
961 case Instruction::SExt:
962 case Instruction::FPTrunc:
963 case Instruction::FPExt:
964 case Instruction::UIToFP:
965 case Instruction::SIToFP:
966 case Instruction::FPToUI:
967 case Instruction::FPToSI:
968 case Instruction::PtrToInt:
969 case Instruction::IntToPtr:
970 case Instruction::BitCast:
971 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000972 case Instruction::Select:
973 Op0 = (OpNo == 0) ? Op : getOperand(0);
974 Op1 = (OpNo == 1) ? Op : getOperand(1);
975 Op2 = (OpNo == 2) ? Op : getOperand(2);
976 return ConstantExpr::getSelect(Op0, Op1, Op2);
977 case Instruction::InsertElement:
978 Op0 = (OpNo == 0) ? Op : getOperand(0);
979 Op1 = (OpNo == 1) ? Op : getOperand(1);
980 Op2 = (OpNo == 2) ? Op : getOperand(2);
981 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
982 case Instruction::ExtractElement:
983 Op0 = (OpNo == 0) ? Op : getOperand(0);
984 Op1 = (OpNo == 1) ? Op : getOperand(1);
985 return ConstantExpr::getExtractElement(Op0, Op1);
986 case Instruction::ShuffleVector:
987 Op0 = (OpNo == 0) ? Op : getOperand(0);
988 Op1 = (OpNo == 1) ? Op : getOperand(1);
989 Op2 = (OpNo == 2) ? Op : getOperand(2);
990 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000991 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000992 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000993 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000994 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000995 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000996 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000997 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000998 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000999 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +00001000 }
Chris Lattner7c1018a2006-07-14 19:37:40 +00001001 default:
1002 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +00001003 Op0 = (OpNo == 0) ? Op : getOperand(0);
1004 Op1 = (OpNo == 1) ? Op : getOperand(1);
1005 return ConstantExpr::get(getOpcode(), Op0, Op1);
1006 }
1007}
1008
1009/// getWithOperands - This returns the current constant expression with the
1010/// operands replaced with the specified values. The specified operands must
1011/// match count and type with the existing ones.
1012Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +00001013getWithOperands(Constant* const *Ops, unsigned NumOps) const {
1014 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +00001015 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +00001016 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +00001017 assert(Ops[i]->getType() == getOperand(i)->getType() &&
1018 "Operand type mismatch!");
1019 AnyChange |= Ops[i] != getOperand(i);
1020 }
1021 if (!AnyChange) // No operands changed, return self.
1022 return const_cast<ConstantExpr*>(this);
1023
1024 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001025 case Instruction::Trunc:
1026 case Instruction::ZExt:
1027 case Instruction::SExt:
1028 case Instruction::FPTrunc:
1029 case Instruction::FPExt:
1030 case Instruction::UIToFP:
1031 case Instruction::SIToFP:
1032 case Instruction::FPToUI:
1033 case Instruction::FPToSI:
1034 case Instruction::PtrToInt:
1035 case Instruction::IntToPtr:
1036 case Instruction::BitCast:
1037 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +00001038 case Instruction::Select:
1039 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1040 case Instruction::InsertElement:
1041 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1042 case Instruction::ExtractElement:
1043 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1044 case Instruction::ShuffleVector:
1045 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001046 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +00001047 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +00001048 case Instruction::ICmp:
1049 case Instruction::FCmp:
Nate Begeman098cc6f2008-07-25 17:56:27 +00001050 case Instruction::VICmp:
1051 case Instruction::VFCmp:
Reid Spencer266e42b2006-12-23 06:05:41 +00001052 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +00001053 default:
1054 assert(getNumOperands() == 2 && "Must be binary operator?");
1055 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001056 }
1057}
1058
Chris Lattner2f7c9632001-06-06 20:29:01 +00001059
1060//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001061// isValueValidForType implementations
1062
Reid Spencere7334722006-12-19 01:28:19 +00001063bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001064 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001065 if (Ty == Type::Int1Ty)
1066 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001067 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001068 return true; // always true, has to fit in largest type
1069 uint64_t Max = (1ll << NumBits) - 1;
1070 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +00001071}
1072
Reid Spencere0fc4df2006-10-20 07:07:24 +00001073bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001074 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001075 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +00001076 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001077 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001078 return true; // always true, has to fit in largest type
1079 int64_t Min = -(1ll << (NumBits-1));
1080 int64_t Max = (1ll << (NumBits-1)) - 1;
1081 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001082}
1083
Dale Johannesend246b2c2007-08-30 00:23:21 +00001084bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1085 // convert modifies in place, so make a copy.
1086 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001087 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001088 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001089 default:
1090 return false; // These can't be represented as floating point!
1091
Dale Johannesend246b2c2007-08-30 00:23:21 +00001092 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001093 case Type::FloatTyID: {
1094 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1095 return true;
1096 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1097 return !losesInfo;
1098 }
1099 case Type::DoubleTyID: {
1100 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1101 &Val2.getSemantics() == &APFloat::IEEEdouble)
1102 return true;
1103 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1104 return !losesInfo;
1105 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001106 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001107 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1108 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1109 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001110 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001111 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1112 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1113 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001114 case Type::PPC_FP128TyID:
1115 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1116 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1117 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001118 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001119}
Chris Lattner9655e542001-07-20 19:16:02 +00001120
Chris Lattner49d855c2001-09-07 16:46:31 +00001121//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001122// Factory Function Implementation
1123
Gabor Greiff6caff662008-05-10 08:32:32 +00001124
1125// The number of operands for each ConstantCreator::create method is
1126// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001127// ConstantCreator - A class that is used to create constants by
1128// ValueMap*. This class should be partially specialized if there is
1129// something strange that needs to be done to interface to the ctor for the
1130// constant.
1131//
Chris Lattner189d19f2003-11-21 20:23:48 +00001132namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001133 template<class ValType>
1134 struct ConstantTraits;
1135
1136 template<typename T, typename Alloc>
1137 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1138 static unsigned uses(const std::vector<T, Alloc>& v) {
1139 return v.size();
1140 }
1141 };
1142
Chris Lattner189d19f2003-11-21 20:23:48 +00001143 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001144 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001145 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001146 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001147 }
1148 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001149
Chris Lattner189d19f2003-11-21 20:23:48 +00001150 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001151 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001152 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1153 assert(0 && "This type cannot be converted!\n");
1154 abort();
1155 }
1156 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001157
Chris Lattner935aa922005-10-04 17:48:46 +00001158 template<class ValType, class TypeClass, class ConstantClass,
1159 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001160 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001161 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001162 typedef std::pair<const Type*, ValType> MapKey;
1163 typedef std::map<MapKey, Constant *> MapTy;
1164 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1165 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001166 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001167 /// Map - This is the main map from the element descriptor to the Constants.
1168 /// This is the primary way we avoid creating two of the same shape
1169 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001170 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001171
1172 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1173 /// from the constants to their element in Map. This is important for
1174 /// removal of constants from the array, which would otherwise have to scan
1175 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001176 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001177
Jim Laskeyc03caef2006-07-17 17:38:29 +00001178 /// AbstractTypeMap - Map for abstract type constants.
1179 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001180 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +00001181
Chris Lattner98fa07b2003-05-23 20:03:32 +00001182 public:
Owen Anderson61794042009-06-17 20:10:08 +00001183 // NOTE: This function is not locked. It is the caller's responsibility
1184 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001185 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001186
1187 /// InsertOrGetItem - Return an iterator for the specified element.
1188 /// If the element exists in the map, the returned iterator points to the
1189 /// entry and Exists=true. If not, the iterator points to the newly
1190 /// inserted entry and returns Exists=false. Newly inserted entries have
1191 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +00001192 /// NOTE: This function is not locked. It is the caller's responsibility
1193 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001194 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1195 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001196 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001197 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001198 Exists = !IP.second;
1199 return IP.first;
1200 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001201
Chris Lattner935aa922005-10-04 17:48:46 +00001202private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001203 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001204 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001205 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001206 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1207 IMI->second->second == CP &&
1208 "InverseMap corrupt!");
1209 return IMI->second;
1210 }
1211
Jim Laskeyc03caef2006-07-17 17:38:29 +00001212 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001213 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1214 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001215 if (I == Map.end() || I->second != CP) {
1216 // FIXME: This should not use a linear scan. If this gets to be a
1217 // performance problem, someone should look at this.
1218 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1219 /* empty */;
1220 }
Chris Lattner935aa922005-10-04 17:48:46 +00001221 return I;
1222 }
Owen Andersonf89c38c2009-06-17 20:43:39 +00001223
1224 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
1225 typename MapTy::iterator I) {
1226 ConstantClass* Result =
1227 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1228
1229 assert(Result->getType() == Ty && "Type specified is not correct!");
1230 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1231
1232 if (HasLargeKey) // Remember the reverse mapping if needed.
1233 InverseMap.insert(std::make_pair(Result, I));
1234
1235 // If the type of the constant is abstract, make sure that an entry
1236 // exists for it in the AbstractTypeMap.
1237 if (Ty->isAbstract()) {
1238 typename AbstractTypeMapTy::iterator TI =
1239 AbstractTypeMap.find(Ty);
1240
1241 if (TI == AbstractTypeMap.end()) {
1242 // Add ourselves to the ATU list of the type.
1243 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1244
1245 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1246 }
1247 }
1248
1249 return Result;
1250 }
Chris Lattner935aa922005-10-04 17:48:46 +00001251public:
1252
Chris Lattnerb64419a2005-10-03 22:51:37 +00001253 /// getOrCreate - Return the specified constant from the map, creating it if
1254 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001255 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001256 MapKey Lookup(Ty, V);
Owen Andersond830eb82009-06-18 19:10:19 +00001257 ConstantClass* Result = 0;
1258
1259 ConstantsLock->reader_acquire();
1260 typename MapTy::iterator I = Map.find(Lookup);
1261 // Is it in the map?
1262 if (I != Map.end())
1263 Result = static_cast<ConstantClass *>(I->second);
1264 ConstantsLock->reader_release();
Owen Anderson61794042009-06-17 20:10:08 +00001265
Owen Andersond830eb82009-06-18 19:10:19 +00001266 if (!Result) {
1267 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
1268 I = Map.find(Lookup);
Owen Anderson61794042009-06-17 20:10:08 +00001269 // Is it in the map?
1270 if (I != Map.end())
1271 Result = static_cast<ConstantClass *>(I->second);
Owen Anderson61794042009-06-17 20:10:08 +00001272 if (!Result) {
Owen Andersond830eb82009-06-18 19:10:19 +00001273 // If no preexisting value, create one now...
1274 Result = Create(Ty, V, I);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001275 }
1276 }
Owen Andersond830eb82009-06-18 19:10:19 +00001277
1278 return Result;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001279 }
Owen Andersonb07dd952009-06-19 23:16:19 +00001280
1281 /// unlockedGetOrCreate - Return the specified constant from the map,
1282 /// creating it if necessary. This version performs no locking, and should
1283 /// only be used during recursive type refinement, when the locks are
1284 /// already held.
1285 ConstantClass *unlockedGetOrCreate(const TypeClass *Ty, const ValType &V) {
1286 MapKey Lookup(Ty, V);
1287 ConstantClass* Result = 0;
1288
1289 typename MapTy::iterator I = Map.find(Lookup);
1290 // Is it in the map?
1291 if (I != Map.end())
1292 Result = static_cast<ConstantClass *>(I->second);
1293
1294 if (!Result) {
1295 // If no preexisting value, create one now...
1296 Result = Create(Ty, V, I);
1297 }
1298
1299 return Result;
1300 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001301
Owen Andersonb07dd952009-06-19 23:16:19 +00001302 void remove(ConstantClass *CP, bool locked = true) {
1303 if (locked) ConstantsLock->writer_acquire();
Jim Laskeyc03caef2006-07-17 17:38:29 +00001304 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001305 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001306 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001307
Chris Lattner935aa922005-10-04 17:48:46 +00001308 if (HasLargeKey) // Remember the reverse mapping if needed.
1309 InverseMap.erase(CP);
1310
Chris Lattnerb50d1352003-10-05 00:17:43 +00001311 // Now that we found the entry, make sure this isn't the entry that
1312 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001313 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001314 if (Ty->isAbstract()) {
1315 assert(AbstractTypeMap.count(Ty) &&
1316 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001317 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001318 if (ATMEntryIt == I) {
1319 // Yes, we are removing the representative entry for this type.
1320 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001321 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001322
Chris Lattnerb50d1352003-10-05 00:17:43 +00001323 // First check the entry before this one...
1324 if (TmpIt != Map.begin()) {
1325 --TmpIt;
1326 if (TmpIt->first.first != Ty) // Not the same type, move back...
1327 ++TmpIt;
1328 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001329
Chris Lattnerb50d1352003-10-05 00:17:43 +00001330 // If we didn't find the same type, try to move forward...
1331 if (TmpIt == ATMEntryIt) {
1332 ++TmpIt;
1333 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1334 --TmpIt; // No entry afterwards with the same type
1335 }
1336
1337 // If there is another entry in the map of the same abstract type,
1338 // update the AbstractTypeMap entry now.
1339 if (TmpIt != ATMEntryIt) {
1340 ATMEntryIt = TmpIt;
1341 } else {
1342 // Otherwise, we are removing the last instance of this type
1343 // from the table. Remove from the ATM, and from user list.
1344 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1345 AbstractTypeMap.erase(Ty);
1346 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001347 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001348 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001349
Chris Lattnerb50d1352003-10-05 00:17:43 +00001350 Map.erase(I);
Owen Anderson61794042009-06-17 20:10:08 +00001351
Owen Andersonb07dd952009-06-19 23:16:19 +00001352 if (locked) ConstantsLock->writer_release();
Chris Lattnerb50d1352003-10-05 00:17:43 +00001353 }
1354
Chris Lattner3b793c62005-10-04 21:35:50 +00001355
1356 /// MoveConstantToNewSlot - If we are about to change C to be the element
1357 /// specified by I, update our internal data structures to reflect this
1358 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +00001359 /// NOTE: This function is not locked. It is the responsibility of the
1360 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001361 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001362 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001363 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001364 assert(OldI != Map.end() && "Constant not found in constant table!");
1365 assert(OldI->second == C && "Didn't find correct element?");
1366
1367 // If this constant is the representative element for its abstract type,
1368 // update the AbstractTypeMap so that the representative element is I.
1369 if (C->getType()->isAbstract()) {
1370 typename AbstractTypeMapTy::iterator ATI =
1371 AbstractTypeMap.find(C->getType());
1372 assert(ATI != AbstractTypeMap.end() &&
1373 "Abstract type not in AbstractTypeMap?");
1374 if (ATI->second == OldI)
1375 ATI->second = I;
1376 }
1377
1378 // Remove the old entry from the map.
1379 Map.erase(OldI);
1380
1381 // Update the inverse map so that we know that this constant is now
1382 // located at descriptor I.
1383 if (HasLargeKey) {
1384 assert(I->second == C && "Bad inversemap entry!");
1385 InverseMap[C] = I;
1386 }
1387 }
1388
Chris Lattnerb50d1352003-10-05 00:17:43 +00001389 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001390 ConstantsLock->writer_acquire();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001391 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001392 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001393
1394 assert(I != AbstractTypeMap.end() &&
1395 "Abstract type not in AbstractTypeMap?");
1396
1397 // Convert a constant at a time until the last one is gone. The last one
1398 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1399 // eliminated eventually.
1400 do {
1401 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001402 TypeClass>::convert(
1403 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001404 cast<TypeClass>(NewTy));
1405
Jim Laskeyc03caef2006-07-17 17:38:29 +00001406 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001407 } while (I != AbstractTypeMap.end());
Owen Anderson61794042009-06-17 20:10:08 +00001408
Owen Andersond830eb82009-06-18 19:10:19 +00001409 ConstantsLock->writer_release();
Chris Lattnerb50d1352003-10-05 00:17:43 +00001410 }
1411
1412 // If the type became concrete without being refined to any other existing
1413 // type, we just remove ourselves from the ATU list.
1414 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001415 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
1416 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001417 }
1418
1419 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001420 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001421 }
1422 };
1423}
1424
Chris Lattnera84df0a22006-09-28 23:36:21 +00001425
Chris Lattner28173502007-02-20 06:11:36 +00001426
Chris Lattner9fba3da2004-02-15 05:53:04 +00001427//---- ConstantAggregateZero::get() implementation...
1428//
1429namespace llvm {
1430 // ConstantAggregateZero does not take extra "value" argument...
1431 template<class ValType>
1432 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1433 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1434 return new ConstantAggregateZero(Ty);
1435 }
1436 };
1437
1438 template<>
1439 struct ConvertConstantType<ConstantAggregateZero, Type> {
1440 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1441 // Make everyone now use a constant of the new type...
Owen Andersonb07dd952009-06-19 23:16:19 +00001442 Constant *New = ConstantAggregateZero::get(NewTy, false);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001443 assert(New != OldC && "Didn't replace constant??");
1444 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00001445 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001446 }
1447 };
1448}
1449
Chris Lattner69edc982006-09-28 00:35:06 +00001450static ManagedStatic<ValueMap<char, Type,
1451 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001452
Chris Lattner3e650af2004-08-04 04:48:01 +00001453static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1454
Owen Andersonb07dd952009-06-19 23:16:19 +00001455ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty, bool locked) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001456 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001457 "Cannot create an aggregate zero of non-aggregate type!");
Owen Andersonb07dd952009-06-19 23:16:19 +00001458
1459 return locked ? AggZeroConstants->getOrCreate(Ty, 0) :
1460 AggZeroConstants->unlockedGetOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001461}
1462
Dan Gohman92b551b2009-03-03 02:55:14 +00001463/// destroyConstant - Remove the constant from the constant table...
1464///
Owen Andersonb07dd952009-06-19 23:16:19 +00001465void ConstantAggregateZero::destroyConstant(bool locked) {
Owen Anderson61794042009-06-17 20:10:08 +00001466 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001467 AggZeroConstants->remove(this, locked);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001468 destroyConstantImpl();
1469}
1470
Chris Lattner3462ae32001-12-03 22:26:30 +00001471//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001472//
Chris Lattner189d19f2003-11-21 20:23:48 +00001473namespace llvm {
1474 template<>
1475 struct ConvertConstantType<ConstantArray, ArrayType> {
1476 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1477 // Make everyone now use a constant of the new type...
1478 std::vector<Constant*> C;
1479 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1480 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Andersonb07dd952009-06-19 23:16:19 +00001481 Constant *New = ConstantArray::get(NewTy, C, false);
Chris Lattner189d19f2003-11-21 20:23:48 +00001482 assert(New != OldC && "Didn't replace constant??");
1483 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00001484 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001485 }
1486 };
1487}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001488
Chris Lattner3e650af2004-08-04 04:48:01 +00001489static std::vector<Constant*> getValType(ConstantArray *CA) {
1490 std::vector<Constant*> Elements;
1491 Elements.reserve(CA->getNumOperands());
1492 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1493 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1494 return Elements;
1495}
1496
Chris Lattnerb64419a2005-10-03 22:51:37 +00001497typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001498 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001499static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001500
Chris Lattner015e8212004-02-15 04:14:47 +00001501Constant *ConstantArray::get(const ArrayType *Ty,
Owen Andersonb07dd952009-06-19 23:16:19 +00001502 const std::vector<Constant*> &V,
1503 bool locked) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001504 // If this is an all-zero array, return a ConstantAggregateZero object
1505 if (!V.empty()) {
1506 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001507 if (!C->isNullValue()) {
Owen Andersonb07dd952009-06-19 23:16:19 +00001508 if (locked)
1509 // Implicitly locked.
1510 return ArrayConstants->getOrCreate(Ty, V);
1511 else
1512 return ArrayConstants->unlockedGetOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001513 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001514 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001515 if (V[i] != C) {
Owen Andersonb07dd952009-06-19 23:16:19 +00001516 if (locked)
1517 // Implicitly locked.
1518 return ArrayConstants->getOrCreate(Ty, V);
1519 else
1520 return ArrayConstants->unlockedGetOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001521 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001522 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001523
Owen Andersonb07dd952009-06-19 23:16:19 +00001524 return ConstantAggregateZero::get(Ty, locked);
Chris Lattner49d855c2001-09-07 16:46:31 +00001525}
1526
Dan Gohman92b551b2009-03-03 02:55:14 +00001527/// destroyConstant - Remove the constant from the constant table...
1528///
Owen Andersonb07dd952009-06-19 23:16:19 +00001529void ConstantArray::destroyConstant(bool locked) {
Owen Anderson59ba8142009-06-19 18:34:09 +00001530 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001531 ArrayConstants->remove(this, locked);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001532 destroyConstantImpl();
1533}
1534
Reid Spencer6f614532006-05-30 08:23:18 +00001535/// ConstantArray::get(const string&) - Return an array that is initialized to
1536/// contain the specified string. If length is zero then a null terminator is
1537/// added to the specified string so that it may be used in a natural way.
1538/// Otherwise, the length parameter specifies how much of the string to use
1539/// and it won't be null terminated.
1540///
Owen Andersonb07dd952009-06-19 23:16:19 +00001541Constant *ConstantArray::get(const std::string &Str,
1542 bool AddNull, bool locked) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001543 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001544 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001545 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001546
1547 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001548 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001549 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001550 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001551
Reid Spencer8d9336d2006-12-31 05:26:44 +00001552 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Owen Andersonb07dd952009-06-19 23:16:19 +00001553 return ConstantArray::get(ATy, ElementVals, locked);
Vikram S. Adve34410432001-10-14 23:17:20 +00001554}
1555
Reid Spencer2546b762007-01-26 07:37:34 +00001556/// isString - This method returns true if the array is an array of i8, and
1557/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001558bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001559 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001560 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001561 return false;
1562 // Check the elements to make sure they are all integers, not constant
1563 // expressions.
1564 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1565 if (!isa<ConstantInt>(getOperand(i)))
1566 return false;
1567 return true;
1568}
1569
Evan Cheng3763c5b2006-10-26 19:15:05 +00001570/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001571/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001572/// null bytes except its terminator.
1573bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001574 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001575 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001576 return false;
1577 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1578 // Last element must be a null.
1579 if (getOperand(getNumOperands()-1) != Zero)
1580 return false;
1581 // Other elements must be non-null integers.
1582 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1583 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001584 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001585 if (getOperand(i) == Zero)
1586 return false;
1587 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001588 return true;
1589}
1590
1591
Dan Gohman92b551b2009-03-03 02:55:14 +00001592/// getAsString - If the sub-element type of this array is i8
1593/// then this method converts the array to an std::string and returns it.
1594/// Otherwise, it asserts out.
1595///
Chris Lattner81fabb02002-08-26 17:53:56 +00001596std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001597 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001598 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001599 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001600 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001601 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001602 return Result;
1603}
1604
1605
Chris Lattner3462ae32001-12-03 22:26:30 +00001606//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001607//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001608
Chris Lattner189d19f2003-11-21 20:23:48 +00001609namespace llvm {
1610 template<>
1611 struct ConvertConstantType<ConstantStruct, StructType> {
1612 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1613 // Make everyone now use a constant of the new type...
1614 std::vector<Constant*> C;
1615 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1616 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Andersonb07dd952009-06-19 23:16:19 +00001617 Constant *New = ConstantStruct::get(NewTy, C, false);
Chris Lattner189d19f2003-11-21 20:23:48 +00001618 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001619
Chris Lattner189d19f2003-11-21 20:23:48 +00001620 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00001621 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001622 }
1623 };
1624}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001625
Chris Lattner8760ec72005-10-04 01:17:50 +00001626typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001627 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001628static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001629
Chris Lattner3e650af2004-08-04 04:48:01 +00001630static std::vector<Constant*> getValType(ConstantStruct *CS) {
1631 std::vector<Constant*> Elements;
1632 Elements.reserve(CS->getNumOperands());
1633 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1634 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1635 return Elements;
1636}
1637
Chris Lattner015e8212004-02-15 04:14:47 +00001638Constant *ConstantStruct::get(const StructType *Ty,
Owen Andersonb07dd952009-06-19 23:16:19 +00001639 const std::vector<Constant*> &V,
1640 bool locked) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001641 // Create a ConstantAggregateZero value if all elements are zeros...
1642 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001643 if (!V[i]->isNullValue())
Owen Andersonb07dd952009-06-19 23:16:19 +00001644 return locked ? StructConstants->getOrCreate(Ty, V) :
1645 StructConstants->unlockedGetOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001646
Owen Andersonb07dd952009-06-19 23:16:19 +00001647 return ConstantAggregateZero::get(Ty, locked);
Chris Lattner49d855c2001-09-07 16:46:31 +00001648}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001649
Owen Andersonb07dd952009-06-19 23:16:19 +00001650Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed,
1651 bool locked) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001652 std::vector<const Type*> StructEls;
1653 StructEls.reserve(V.size());
1654 for (unsigned i = 0, e = V.size(); i != e; ++i)
1655 StructEls.push_back(V[i]->getType());
Owen Andersonb07dd952009-06-19 23:16:19 +00001656 return get(StructType::get(StructEls, packed), V, locked);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001657}
1658
Chris Lattnerd7a73302001-10-13 06:57:33 +00001659// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001660//
Owen Andersonb07dd952009-06-19 23:16:19 +00001661void ConstantStruct::destroyConstant(bool locked) {
Owen Anderson59ba8142009-06-19 18:34:09 +00001662 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001663 StructConstants->remove(this, locked);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001664 destroyConstantImpl();
1665}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001666
Reid Spencerd84d35b2007-02-15 02:26:10 +00001667//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001668//
1669namespace llvm {
1670 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001671 struct ConvertConstantType<ConstantVector, VectorType> {
1672 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001673 // Make everyone now use a constant of the new type...
1674 std::vector<Constant*> C;
1675 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1676 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Andersonb07dd952009-06-19 23:16:19 +00001677 Constant *New = ConstantVector::get(NewTy, C, false);
Brian Gaeke02209042004-08-20 06:00:58 +00001678 assert(New != OldC && "Didn't replace constant??");
1679 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00001680 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001681 }
1682 };
1683}
1684
Reid Spencerd84d35b2007-02-15 02:26:10 +00001685static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001686 std::vector<Constant*> Elements;
1687 Elements.reserve(CP->getNumOperands());
1688 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1689 Elements.push_back(CP->getOperand(i));
1690 return Elements;
1691}
1692
Reid Spencerd84d35b2007-02-15 02:26:10 +00001693static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001694 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001695
Reid Spencerd84d35b2007-02-15 02:26:10 +00001696Constant *ConstantVector::get(const VectorType *Ty,
Owen Andersonb07dd952009-06-19 23:16:19 +00001697 const std::vector<Constant*> &V,
1698 bool locked) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001699 assert(!V.empty() && "Vectors can't be empty");
1700 // If this is an all-undef or alll-zero vector, return a
1701 // ConstantAggregateZero or UndefValue.
1702 Constant *C = V[0];
1703 bool isZero = C->isNullValue();
1704 bool isUndef = isa<UndefValue>(C);
1705
1706 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001707 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001708 if (V[i] != C) {
1709 isZero = isUndef = false;
1710 break;
1711 }
Brian Gaeke02209042004-08-20 06:00:58 +00001712 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001713
1714 if (isZero)
Owen Andersonb07dd952009-06-19 23:16:19 +00001715 return ConstantAggregateZero::get(Ty, locked);
Chris Lattnerd977c072008-07-10 00:44:03 +00001716 if (isUndef)
Owen Andersonb07dd952009-06-19 23:16:19 +00001717 return UndefValue::get(Ty, locked);
Owen Anderson61794042009-06-17 20:10:08 +00001718
Owen Andersonb07dd952009-06-19 23:16:19 +00001719 return locked ? VectorConstants->getOrCreate(Ty, V) :
1720 VectorConstants->unlockedGetOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001721}
1722
Owen Andersonb07dd952009-06-19 23:16:19 +00001723Constant *ConstantVector::get(const std::vector<Constant*> &V, bool locked) {
Brian Gaeke02209042004-08-20 06:00:58 +00001724 assert(!V.empty() && "Cannot infer type if V is empty");
Owen Andersonb07dd952009-06-19 23:16:19 +00001725 return get(VectorType::get(V.front()->getType(),V.size()), V, locked);
Brian Gaeke02209042004-08-20 06:00:58 +00001726}
1727
1728// destroyConstant - Remove the constant from the constant table...
1729//
Owen Andersonb07dd952009-06-19 23:16:19 +00001730void ConstantVector::destroyConstant(bool locked) {
Owen Anderson59ba8142009-06-19 18:34:09 +00001731 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001732 VectorConstants->remove(this, locked);
Brian Gaeke02209042004-08-20 06:00:58 +00001733 destroyConstantImpl();
1734}
1735
Dan Gohman30978072007-05-24 14:36:04 +00001736/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001737/// is set to all ones.
1738/// @returns true iff this constant's emements are all set to all ones.
1739/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001740bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001741 // Check out first element.
1742 const Constant *Elt = getOperand(0);
1743 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1744 if (!CI || !CI->isAllOnesValue()) return false;
1745 // Then make sure all remaining elements point to the same value.
1746 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1747 if (getOperand(I) != Elt) return false;
1748 }
1749 return true;
1750}
1751
Dan Gohman07159202007-10-17 17:51:30 +00001752/// getSplatValue - If this is a splat constant, where all of the
1753/// elements have the same value, return that value. Otherwise return null.
1754Constant *ConstantVector::getSplatValue() {
1755 // Check out first element.
1756 Constant *Elt = getOperand(0);
1757 // Then make sure all remaining elements point to the same value.
1758 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1759 if (getOperand(I) != Elt) return 0;
1760 return Elt;
1761}
1762
Chris Lattner3462ae32001-12-03 22:26:30 +00001763//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001764//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001765
Chris Lattner189d19f2003-11-21 20:23:48 +00001766namespace llvm {
1767 // ConstantPointerNull does not take extra "value" argument...
1768 template<class ValType>
1769 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1770 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1771 return new ConstantPointerNull(Ty);
1772 }
1773 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001774
Chris Lattner189d19f2003-11-21 20:23:48 +00001775 template<>
1776 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1777 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1778 // Make everyone now use a constant of the new type...
Owen Andersonb07dd952009-06-19 23:16:19 +00001779 Constant *New = ConstantPointerNull::get(NewTy, false);
Chris Lattner189d19f2003-11-21 20:23:48 +00001780 assert(New != OldC && "Didn't replace constant??");
1781 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00001782 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001783 }
1784 };
1785}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001786
Chris Lattner69edc982006-09-28 00:35:06 +00001787static ManagedStatic<ValueMap<char, PointerType,
1788 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001789
Chris Lattner3e650af2004-08-04 04:48:01 +00001790static char getValType(ConstantPointerNull *) {
1791 return 0;
1792}
1793
1794
Owen Andersonb07dd952009-06-19 23:16:19 +00001795ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty,
1796 bool locked) {
Owen Anderson61794042009-06-17 20:10:08 +00001797 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001798 return locked ? NullPtrConstants->getOrCreate(Ty, 0) :
1799 NullPtrConstants->unlockedGetOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001800}
1801
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001802// destroyConstant - Remove the constant from the constant table...
1803//
Owen Andersonb07dd952009-06-19 23:16:19 +00001804void ConstantPointerNull::destroyConstant(bool locked) {
Owen Anderson59ba8142009-06-19 18:34:09 +00001805 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001806 NullPtrConstants->remove(this, locked);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001807 destroyConstantImpl();
1808}
1809
1810
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001811//---- UndefValue::get() implementation...
1812//
1813
1814namespace llvm {
1815 // UndefValue does not take extra "value" argument...
1816 template<class ValType>
1817 struct ConstantCreator<UndefValue, Type, ValType> {
1818 static UndefValue *create(const Type *Ty, const ValType &V) {
1819 return new UndefValue(Ty);
1820 }
1821 };
1822
1823 template<>
1824 struct ConvertConstantType<UndefValue, Type> {
1825 static void convert(UndefValue *OldC, const Type *NewTy) {
1826 // Make everyone now use a constant of the new type.
Owen Andersonb07dd952009-06-19 23:16:19 +00001827 Constant *New = UndefValue::get(NewTy, false);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001828 assert(New != OldC && "Didn't replace constant??");
1829 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00001830 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001831 }
1832 };
1833}
1834
Chris Lattner69edc982006-09-28 00:35:06 +00001835static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001836
1837static char getValType(UndefValue *) {
1838 return 0;
1839}
1840
1841
Owen Andersonb07dd952009-06-19 23:16:19 +00001842UndefValue *UndefValue::get(const Type *Ty, bool locked) {
1843 return locked ? UndefValueConstants->getOrCreate(Ty, 0) :
1844 UndefValueConstants->unlockedGetOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001845}
1846
1847// destroyConstant - Remove the constant from the constant table.
1848//
Owen Andersonb07dd952009-06-19 23:16:19 +00001849void UndefValue::destroyConstant(bool locked) {
Owen Anderson61794042009-06-17 20:10:08 +00001850 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00001851 UndefValueConstants->remove(this, locked);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001852 destroyConstantImpl();
1853}
1854
Nick Lewycky49f89192009-04-04 07:22:01 +00001855//---- MDString::get() implementation
1856//
1857
1858MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001859 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001860 StrBegin(begin), StrEnd(end) {}
1861
1862static ManagedStatic<StringMap<MDString*> > MDStringCache;
1863
Owen Andersonb07dd952009-06-19 23:16:19 +00001864MDString *MDString::get(const char *StrBegin, const char *StrEnd, bool locked) {
1865 if (locked) ConstantsLock->writer_acquire();
1866
Owen Andersond830eb82009-06-18 19:10:19 +00001867 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1868 StrBegin, StrEnd);
1869 MDString *&S = Entry.getValue();
1870 if (!S) S = new MDString(Entry.getKeyData(),
1871 Entry.getKeyData() + Entry.getKeyLength());
Owen Anderson65c5cd72009-06-17 20:34:43 +00001872
Owen Andersonb07dd952009-06-19 23:16:19 +00001873 if (locked) ConstantsLock->writer_release();
1874
Owen Andersond830eb82009-06-18 19:10:19 +00001875 return S;
Nick Lewycky49f89192009-04-04 07:22:01 +00001876}
1877
Owen Andersonb07dd952009-06-19 23:16:19 +00001878void MDString::destroyConstant(bool locked) {
1879 if (locked) ConstantsLock->writer_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001880 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
Nick Lewycky49f89192009-04-04 07:22:01 +00001881 destroyConstantImpl();
Owen Andersonb07dd952009-06-19 23:16:19 +00001882 if (locked) ConstantsLock->writer_release();
Nick Lewycky49f89192009-04-04 07:22:01 +00001883}
1884
1885//---- MDNode::get() implementation
1886//
1887
1888static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1889
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001890MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001891 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001892 for (unsigned i = 0; i != NumVals; ++i)
1893 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001894}
1895
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001896void MDNode::Profile(FoldingSetNodeID &ID) const {
1897 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001898 ID.AddPointer(*I);
1899}
1900
Owen Andersonb07dd952009-06-19 23:16:19 +00001901MDNode *MDNode::get(Value*const* Vals, unsigned NumVals, bool locked) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001902 FoldingSetNodeID ID;
1903 for (unsigned i = 0; i != NumVals; ++i)
1904 ID.AddPointer(Vals[i]);
1905
Owen Andersonb07dd952009-06-19 23:16:19 +00001906 if (locked) ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001907 void *InsertPoint;
1908 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Andersonb07dd952009-06-19 23:16:19 +00001909 if (locked) ConstantsLock->reader_release();
Owen Andersond830eb82009-06-18 19:10:19 +00001910
1911 if (!N) {
Owen Andersonb07dd952009-06-19 23:16:19 +00001912 if (locked) {
1913 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
1914 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1915 if (!N) {
1916 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1917 N = new(0) MDNode(Vals, NumVals);
1918 MDNodeSet->InsertNode(N, InsertPoint);
1919 }
1920 } else {
Owen Andersond830eb82009-06-18 19:10:19 +00001921 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1922 N = new(0) MDNode(Vals, NumVals);
1923 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001924 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001925 }
Owen Andersond830eb82009-06-18 19:10:19 +00001926 return N;
Nick Lewycky49f89192009-04-04 07:22:01 +00001927}
1928
Owen Andersonb07dd952009-06-19 23:16:19 +00001929void MDNode::destroyConstant(bool locked) {
1930 if (locked) ConstantsLock->writer_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001931 MDNodeSet->RemoveNode(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001932 destroyConstantImpl();
Owen Andersonb07dd952009-06-19 23:16:19 +00001933 if (locked) ConstantsLock->writer_release();
Nick Lewycky49f89192009-04-04 07:22:01 +00001934}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001935
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001936//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001937//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001938
Dan Gohmand78c4002008-05-13 00:00:25 +00001939namespace {
1940
Reid Spenceree3c9912006-12-04 05:19:50 +00001941struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001942 typedef SmallVector<unsigned, 4> IndexList;
1943
1944 ExprMapKeyType(unsigned opc,
1945 const std::vector<Constant*> &ops,
1946 unsigned short pred = 0,
1947 const IndexList &inds = IndexList())
1948 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001949 uint16_t opcode;
1950 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001951 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001952 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001953 bool operator==(const ExprMapKeyType& that) const {
1954 return this->opcode == that.opcode &&
1955 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001956 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001957 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001958 }
1959 bool operator<(const ExprMapKeyType & that) const {
1960 return this->opcode < that.opcode ||
1961 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1962 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001963 this->operands < that.operands) ||
1964 (this->opcode == that.opcode && this->predicate == that.predicate &&
1965 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001966 }
1967
1968 bool operator!=(const ExprMapKeyType& that) const {
1969 return !(*this == that);
1970 }
1971};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001972
Dan Gohmand78c4002008-05-13 00:00:25 +00001973}
1974
Chris Lattner189d19f2003-11-21 20:23:48 +00001975namespace llvm {
1976 template<>
1977 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001978 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1979 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001980 if (Instruction::isCast(V.opcode))
1981 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1982 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001983 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001984 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1985 if (V.opcode == Instruction::Select)
1986 return new SelectConstantExpr(V.operands[0], V.operands[1],
1987 V.operands[2]);
1988 if (V.opcode == Instruction::ExtractElement)
1989 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1990 if (V.opcode == Instruction::InsertElement)
1991 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1992 V.operands[2]);
1993 if (V.opcode == Instruction::ShuffleVector)
1994 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1995 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001996 if (V.opcode == Instruction::InsertValue)
1997 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1998 V.indices, Ty);
1999 if (V.opcode == Instruction::ExtractValue)
2000 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00002001 if (V.opcode == Instruction::GetElementPtr) {
2002 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00002003 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00002004 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002005
Reid Spenceree3c9912006-12-04 05:19:50 +00002006 // The compare instructions are weird. We have to encode the predicate
2007 // value and it is combined with the instruction opcode by multiplying
2008 // the opcode by one hundred. We must decode this to get the predicate.
2009 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00002010 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00002011 V.operands[0], V.operands[1]);
2012 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00002013 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
2014 V.operands[0], V.operands[1]);
2015 if (V.opcode == Instruction::VICmp)
2016 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
2017 V.operands[0], V.operands[1]);
2018 if (V.opcode == Instruction::VFCmp)
2019 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00002020 V.operands[0], V.operands[1]);
2021 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00002022 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00002023 }
Chris Lattner189d19f2003-11-21 20:23:48 +00002024 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00002025
Chris Lattner189d19f2003-11-21 20:23:48 +00002026 template<>
2027 struct ConvertConstantType<ConstantExpr, Type> {
2028 static void convert(ConstantExpr *OldC, const Type *NewTy) {
2029 Constant *New;
2030 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031 case Instruction::Trunc:
2032 case Instruction::ZExt:
2033 case Instruction::SExt:
2034 case Instruction::FPTrunc:
2035 case Instruction::FPExt:
2036 case Instruction::UIToFP:
2037 case Instruction::SIToFP:
2038 case Instruction::FPToUI:
2039 case Instruction::FPToSI:
2040 case Instruction::PtrToInt:
2041 case Instruction::IntToPtr:
2042 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00002043 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Andersonb07dd952009-06-19 23:16:19 +00002044 NewTy, false);
Chris Lattner189d19f2003-11-21 20:23:48 +00002045 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00002046 case Instruction::Select:
2047 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
2048 OldC->getOperand(1),
Owen Andersonb07dd952009-06-19 23:16:19 +00002049 OldC->getOperand(2), false);
Chris Lattner6e415c02004-03-12 05:54:04 +00002050 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00002051 default:
2052 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002053 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00002054 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Andersonb07dd952009-06-19 23:16:19 +00002055 OldC->getOperand(1), false);
Chris Lattner189d19f2003-11-21 20:23:48 +00002056 break;
2057 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00002058 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00002059 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00002060 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Andersonb07dd952009-06-19 23:16:19 +00002061 &Idx[0], Idx.size(), false);
Chris Lattner189d19f2003-11-21 20:23:48 +00002062 break;
2063 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002064
Chris Lattner189d19f2003-11-21 20:23:48 +00002065 assert(New != OldC && "Didn't replace constant??");
2066 OldC->uncheckedReplaceAllUsesWith(New);
Owen Andersonb07dd952009-06-19 23:16:19 +00002067 OldC->destroyConstant(false); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00002068 }
2069 };
2070} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00002071
2072
Chris Lattner3e650af2004-08-04 04:48:01 +00002073static ExprMapKeyType getValType(ConstantExpr *CE) {
2074 std::vector<Constant*> Operands;
2075 Operands.reserve(CE->getNumOperands());
2076 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
2077 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00002078 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002079 CE->isCompare() ? CE->getPredicate() : 0,
2080 CE->hasIndices() ?
2081 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00002082}
2083
Chris Lattner69edc982006-09-28 00:35:06 +00002084static ManagedStatic<ValueMap<ExprMapKeyType, Type,
2085 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00002086
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002087/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00002088/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089static inline Constant *getFoldedCast(
Owen Andersonb07dd952009-06-19 23:16:19 +00002090 Instruction::CastOps opc, Constant *C, const Type *Ty, bool locked) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00002091 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 // Fold a few common cases
Owen Andersonb07dd952009-06-19 23:16:19 +00002093 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty, locked))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00002095
Vikram S. Adve4c485332002-07-15 18:19:33 +00002096 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002097 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00002098 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00002099
Owen Anderson61794042009-06-17 20:10:08 +00002100 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00002101 return locked ? ExprConstants->getOrCreate(Ty, Key) :
2102 ExprConstants->unlockedGetOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002103}
Reid Spencerf37dc652006-12-05 19:14:13 +00002104
Owen Andersonb07dd952009-06-19 23:16:19 +00002105Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty,
2106 bool locked) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002107 Instruction::CastOps opc = Instruction::CastOps(oc);
2108 assert(Instruction::isCast(opc) && "opcode out of range");
2109 assert(C && Ty && "Null arguments to getCast");
2110 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
2111
2112 switch (opc) {
2113 default:
2114 assert(0 && "Invalid cast opcode");
2115 break;
Owen Andersonb07dd952009-06-19 23:16:19 +00002116 case Instruction::Trunc: return getTrunc(C, Ty, locked);
2117 case Instruction::ZExt: return getZExt(C, Ty, locked);
2118 case Instruction::SExt: return getSExt(C, Ty, locked);
2119 case Instruction::FPTrunc: return getFPTrunc(C, Ty, locked);
2120 case Instruction::FPExt: return getFPExtend(C, Ty, locked);
2121 case Instruction::UIToFP: return getUIToFP(C, Ty, locked);
2122 case Instruction::SIToFP: return getSIToFP(C, Ty, locked);
2123 case Instruction::FPToUI: return getFPToUI(C, Ty, locked);
2124 case Instruction::FPToSI: return getFPToSI(C, Ty, locked);
2125 case Instruction::PtrToInt: return getPtrToInt(C, Ty, locked);
2126 case Instruction::IntToPtr: return getIntToPtr(C, Ty, locked);
2127 case Instruction::BitCast: return getBitCast(C, Ty, locked);
Chris Lattner1ece6f82005-01-01 15:59:57 +00002128 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002129 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00002130}
2131
Reid Spencer5c140882006-12-04 20:17:56 +00002132Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002133 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002134 return getCast(Instruction::BitCast, C, Ty);
2135 return getCast(Instruction::ZExt, C, Ty);
2136}
2137
Owen Andersonb07dd952009-06-19 23:16:19 +00002138Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty,
2139 bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002140 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Andersonb07dd952009-06-19 23:16:19 +00002141 return getCast(Instruction::BitCast, C, Ty, locked);
2142 return getCast(Instruction::SExt, C, Ty, locked);
Reid Spencer5c140882006-12-04 20:17:56 +00002143}
2144
2145Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002146 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00002147 return getCast(Instruction::BitCast, C, Ty);
2148 return getCast(Instruction::Trunc, C, Ty);
2149}
2150
Owen Andersonb07dd952009-06-19 23:16:19 +00002151Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty,
2152 bool locked) {
Reid Spencerbc245a02006-12-05 03:25:26 +00002153 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002154 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00002155
Chris Lattner03c49532007-01-15 02:27:26 +00002156 if (Ty->isInteger())
Owen Andersonb07dd952009-06-19 23:16:19 +00002157 return getCast(Instruction::PtrToInt, S, Ty, locked);
2158 return getCast(Instruction::BitCast, S, Ty, locked);
Reid Spencerbc245a02006-12-05 03:25:26 +00002159}
2160
Reid Spencer56521c42006-12-12 00:51:07 +00002161Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
2162 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002163 assert(C->getType()->isIntOrIntVector() &&
2164 Ty->isIntOrIntVector() && "Invalid cast");
2165 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2166 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00002167 Instruction::CastOps opcode =
2168 (SrcBits == DstBits ? Instruction::BitCast :
2169 (SrcBits > DstBits ? Instruction::Trunc :
2170 (isSigned ? Instruction::SExt : Instruction::ZExt)));
2171 return getCast(opcode, C, Ty);
2172}
2173
2174Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002175 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00002176 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002177 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2178 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00002179 if (SrcBits == DstBits)
2180 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00002181 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00002182 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00002183 return getCast(opcode, C, Ty);
2184}
2185
Owen Andersonb07dd952009-06-19 23:16:19 +00002186Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty, bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002187#ifndef NDEBUG
2188 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2189 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2190#endif
2191 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2192 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
2193 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
2194 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195 "SrcTy must be larger than DestTy for Trunc!");
2196
Owen Andersonb07dd952009-06-19 23:16:19 +00002197 return getFoldedCast(Instruction::Trunc, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002198}
2199
Owen Andersonb07dd952009-06-19 23:16:19 +00002200Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty, bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002201#ifndef NDEBUG
2202 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2203 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2204#endif
2205 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2206 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
2207 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
2208 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 "SrcTy must be smaller than DestTy for SExt!");
2210
Owen Andersonb07dd952009-06-19 23:16:19 +00002211 return getFoldedCast(Instruction::SExt, C, Ty, locked);
Chris Lattnerdd284742004-04-04 23:20:30 +00002212}
2213
Owen Andersonb07dd952009-06-19 23:16:19 +00002214Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty, bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002215#ifndef NDEBUG
2216 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2217 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2218#endif
2219 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2220 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
2221 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
2222 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223 "SrcTy must be smaller than DestTy for ZExt!");
2224
Owen Andersonb07dd952009-06-19 23:16:19 +00002225 return getFoldedCast(Instruction::ZExt, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226}
2227
Owen Andersonb07dd952009-06-19 23:16:19 +00002228Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty, bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002229#ifndef NDEBUG
2230 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2231 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2232#endif
2233 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2234 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2235 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236 "This is an illegal floating point truncation!");
Owen Andersonb07dd952009-06-19 23:16:19 +00002237 return getFoldedCast(Instruction::FPTrunc, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238}
2239
Owen Andersonb07dd952009-06-19 23:16:19 +00002240Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty, bool locked) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002241#ifndef NDEBUG
2242 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2243 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2244#endif
2245 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2246 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2247 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 "This is an illegal floating point extension!");
Owen Andersonb07dd952009-06-19 23:16:19 +00002249 return getFoldedCast(Instruction::FPExt, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250}
2251
Owen Andersonb07dd952009-06-19 23:16:19 +00002252Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty, bool locked) {
Devang Pateld26344d2008-11-03 23:20:04 +00002253#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002254 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2255 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002256#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002257 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2258 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
2259 "This is an illegal uint to floating point cast!");
Owen Andersonb07dd952009-06-19 23:16:19 +00002260 return getFoldedCast(Instruction::UIToFP, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261}
2262
Owen Andersonb07dd952009-06-19 23:16:19 +00002263Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty, bool locked) {
Devang Pateld26344d2008-11-03 23:20:04 +00002264#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002265 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2266 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002267#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002268 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2269 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 "This is an illegal sint to floating point cast!");
Owen Andersonb07dd952009-06-19 23:16:19 +00002271 return getFoldedCast(Instruction::SIToFP, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272}
2273
Owen Andersonb07dd952009-06-19 23:16:19 +00002274Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty, bool locked) {
Devang Pateld26344d2008-11-03 23:20:04 +00002275#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002276 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2277 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002278#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002279 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2280 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2281 "This is an illegal floating point to uint cast!");
Owen Andersonb07dd952009-06-19 23:16:19 +00002282 return getFoldedCast(Instruction::FPToUI, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283}
2284
Owen Andersonb07dd952009-06-19 23:16:19 +00002285Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty, bool locked) {
Devang Pateld26344d2008-11-03 23:20:04 +00002286#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002287 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2288 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002289#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002290 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2291 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2292 "This is an illegal floating point to sint cast!");
Owen Andersonb07dd952009-06-19 23:16:19 +00002293 return getFoldedCast(Instruction::FPToSI, C, Ty, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294}
2295
Owen Andersonb07dd952009-06-19 23:16:19 +00002296Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy,
2297 bool locked) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00002299 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Andersonb07dd952009-06-19 23:16:19 +00002300 return getFoldedCast(Instruction::PtrToInt, C, DstTy, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301}
2302
Owen Andersonb07dd952009-06-19 23:16:19 +00002303Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy,
2304 bool locked) {
Chris Lattner03c49532007-01-15 02:27:26 +00002305 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Andersonb07dd952009-06-19 23:16:19 +00002307 return getFoldedCast(Instruction::IntToPtr, C, DstTy, locked);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308}
2309
Owen Andersonb07dd952009-06-19 23:16:19 +00002310Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy,
2311 bool locked) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 // BitCast implies a no-op cast of type only. No bits change. However, you
2313 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002314#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 const Type *SrcTy = C->getType();
2316 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002317 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318
2319 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2320 // or nonptr->ptr). For all the other types, the cast is okay if source and
2321 // destination bit widths are identical.
2322 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2323 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002324#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002325 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002326
2327 // It is common to ask for a bitcast of a value to its own type, handle this
2328 // speedily.
2329 if (C->getType() == DstTy) return C;
2330
Owen Andersonb07dd952009-06-19 23:16:19 +00002331 return getFoldedCast(Instruction::BitCast, C, DstTy, locked);
Chris Lattnerdd284742004-04-04 23:20:30 +00002332}
2333
Duncan Sandsd334aca2009-05-21 15:52:21 +00002334Constant *ConstantExpr::getAlignOf(const Type *Ty) {
2335 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
2336 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
2337 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
2338 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
2339 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
2340 Constant *Indices[2] = { Zero, One };
2341 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
2342 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
2343}
2344
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00002345Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00002346 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00002347 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
2348 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00002349 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00002350 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00002351}
2352
Chris Lattnerb50d1352003-10-05 00:17:43 +00002353Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Andersonb07dd952009-06-19 23:16:19 +00002354 Constant *C1, Constant *C2, bool locked) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002355 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002356 assert(Opcode >= Instruction::BinaryOpsBegin &&
2357 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002358 "Invalid opcode in binary constant expression");
2359 assert(C1->getType() == C2->getType() &&
2360 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002361
Reid Spencer542964f2007-01-11 18:21:29 +00002362 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Andersonb07dd952009-06-19 23:16:19 +00002363 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2, locked))
Chris Lattnerb50d1352003-10-05 00:17:43 +00002364 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002365
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002366 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002367 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002368
Owen Andersonb07dd952009-06-19 23:16:19 +00002369 return locked ? ExprConstants->getOrCreate(ReqTy, Key) :
2370 ExprConstants->unlockedGetOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002371}
2372
Reid Spencer266e42b2006-12-23 06:05:41 +00002373Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002374 Constant *C1, Constant *C2) {
2375 bool isVectorType = C1->getType()->getTypeID() == Type::VectorTyID;
Reid Spencer266e42b2006-12-23 06:05:41 +00002376 switch (predicate) {
2377 default: assert(0 && "Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002378 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2379 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2380 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2381 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2382 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2383 case CmpInst::FCMP_TRUE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002384 return isVectorType ? getVFCmp(predicate, C1, C2)
2385 : getFCmp(predicate, C1, C2);
Nate Begemanc96e2e42008-07-25 17:35:37 +00002386 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2387 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2388 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2389 case CmpInst::ICMP_SLE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002390 return isVectorType ? getVICmp(predicate, C1, C2)
2391 : getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002392 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002393}
2394
Owen Andersonb07dd952009-06-19 23:16:19 +00002395Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
2396 bool locked) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002397 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2398 if (C1->getType()->isFPOrFPVector()) {
2399 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2400 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2401 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2402 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002403#ifndef NDEBUG
2404 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002405 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002406 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002407 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002408 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002409 assert(C1->getType()->isIntOrIntVector() &&
2410 "Tried to create an integer operation on a non-integer type!");
2411 break;
2412 case Instruction::FAdd:
2413 case Instruction::FSub:
2414 case Instruction::FMul:
2415 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2416 assert(C1->getType()->isFPOrFPVector() &&
2417 "Tried to create a floating-point operation on a "
2418 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002419 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002420 case Instruction::UDiv:
2421 case Instruction::SDiv:
2422 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002423 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002424 "Tried to create an arithmetic operation on a non-arithmetic type!");
2425 break;
2426 case Instruction::FDiv:
2427 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002428 assert(C1->getType()->isFPOrFPVector() &&
2429 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002430 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002431 case Instruction::URem:
2432 case Instruction::SRem:
2433 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002434 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002435 "Tried to create an arithmetic operation on a non-arithmetic type!");
2436 break;
2437 case Instruction::FRem:
2438 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002439 assert(C1->getType()->isFPOrFPVector() &&
2440 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002441 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002442 case Instruction::And:
2443 case Instruction::Or:
2444 case Instruction::Xor:
2445 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002446 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002447 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002448 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002449 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002450 case Instruction::LShr:
2451 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002452 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002453 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002454 "Tried to create a shift operation on a non-integer type!");
2455 break;
2456 default:
2457 break;
2458 }
2459#endif
2460
Owen Andersonb07dd952009-06-19 23:16:19 +00002461 return getTy(C1->getType(), Opcode, C1, C2, locked);
Reid Spencera009d0d2006-12-04 21:35:24 +00002462}
2463
Reid Spencer266e42b2006-12-23 06:05:41 +00002464Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002465 Constant *C1, Constant *C2) {
2466 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002467 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002468}
2469
Chris Lattner6e415c02004-03-12 05:54:04 +00002470Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Andersonb07dd952009-06-19 23:16:19 +00002471 Constant *V1, Constant *V2, bool locked) {
Chris Lattner41632132008-12-29 00:16:12 +00002472 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002473
2474 if (ReqTy == V1->getType())
Owen Andersonb07dd952009-06-19 23:16:19 +00002475 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2, locked))
Chris Lattner6e415c02004-03-12 05:54:04 +00002476 return SC; // Fold common cases
2477
2478 std::vector<Constant*> argVec(3, C);
2479 argVec[1] = V1;
2480 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002481 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002482
Owen Andersonb07dd952009-06-19 23:16:19 +00002483 return locked ? ExprConstants->getOrCreate(ReqTy, Key) :
2484 ExprConstants->unlockedGetOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002485}
2486
Chris Lattnerb50d1352003-10-05 00:17:43 +00002487Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002488 Value* const *Idxs,
Owen Andersonb07dd952009-06-19 23:16:19 +00002489 unsigned NumIdx, bool locked) {
Dan Gohman12fce772008-05-15 19:50:34 +00002490 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2491 Idxs+NumIdx) ==
2492 cast<PointerType>(ReqTy)->getElementType() &&
2493 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002494
Owen Andersonb07dd952009-06-19 23:16:19 +00002495 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs,
2496 NumIdx, locked))
Chris Lattneracdbe712003-04-17 19:24:48 +00002497 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002498
Chris Lattnerb50d1352003-10-05 00:17:43 +00002499 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002500 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002501 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002502 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002503 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002504 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002505 for (unsigned i = 0; i != NumIdx; ++i)
2506 ArgVec.push_back(cast<Constant>(Idxs[i]));
2507 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002508
2509 // Implicitly locked.
Owen Andersonb07dd952009-06-19 23:16:19 +00002510 return locked ? ExprConstants->getOrCreate(ReqTy, Key) :
2511 ExprConstants->unlockedGetOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002512}
2513
Chris Lattner302116a2007-01-31 04:40:28 +00002514Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Andersonb07dd952009-06-19 23:16:19 +00002515 unsigned NumIdx, bool locked) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002516 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002517 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002518 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002519 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002520 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Andersonb07dd952009-06-19 23:16:19 +00002521 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs,
2522 NumIdx, locked);
Chris Lattner13128ab2004-10-11 22:52:25 +00002523}
2524
Chris Lattner302116a2007-01-31 04:40:28 +00002525Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Andersonb07dd952009-06-19 23:16:19 +00002526 unsigned NumIdx, bool locked) {
2527 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx, locked);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002528}
2529
Chris Lattner302116a2007-01-31 04:40:28 +00002530
Reid Spenceree3c9912006-12-04 05:19:50 +00002531Constant *
2532ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2533 assert(LHS->getType() == RHS->getType());
2534 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2535 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2536
Reid Spencer266e42b2006-12-23 06:05:41 +00002537 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002538 return FC; // Fold a few common cases...
2539
2540 // Look up the constant in the table first to ensure uniqueness
2541 std::vector<Constant*> ArgVec;
2542 ArgVec.push_back(LHS);
2543 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002544 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002545 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002546
2547 // Implicitly locked.
2548 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002549}
2550
2551Constant *
2552ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2553 assert(LHS->getType() == RHS->getType());
2554 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2555
Reid Spencer266e42b2006-12-23 06:05:41 +00002556 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002557 return FC; // Fold a few common cases...
2558
2559 // Look up the constant in the table first to ensure uniqueness
2560 std::vector<Constant*> ArgVec;
2561 ArgVec.push_back(LHS);
2562 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002563 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002564 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002565
2566 // Implicitly locked.
2567 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002568}
2569
Nate Begemand2195702008-05-12 19:01:56 +00002570Constant *
2571ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
Chris Lattnereab49262008-07-14 05:17:31 +00002572 assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() &&
Nate Begemand2195702008-05-12 19:01:56 +00002573 "Tried to create vicmp operation on non-vector type!");
Nate Begemand2195702008-05-12 19:01:56 +00002574 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2575 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2576
Nate Begemanac7f3d92008-05-12 19:23:22 +00002577 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002578 const Type *EltTy = VTy->getElementType();
2579 unsigned NumElts = VTy->getNumElements();
2580
Chris Lattnereab49262008-07-14 05:17:31 +00002581 // See if we can fold the element-wise comparison of the LHS and RHS.
2582 SmallVector<Constant *, 16> LHSElts, RHSElts;
2583 LHS->getVectorElements(LHSElts);
2584 RHS->getVectorElements(RHSElts);
2585
2586 if (!LHSElts.empty() && !RHSElts.empty()) {
2587 SmallVector<Constant *, 16> Elts;
2588 for (unsigned i = 0; i != NumElts; ++i) {
2589 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2590 RHSElts[i]);
2591 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2592 if (FCI->getZExtValue())
2593 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2594 else
2595 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2596 } else if (FC && isa<UndefValue>(FC)) {
2597 Elts.push_back(UndefValue::get(EltTy));
2598 } else {
2599 break;
2600 }
Nate Begemand2195702008-05-12 19:01:56 +00002601 }
Chris Lattnereab49262008-07-14 05:17:31 +00002602 if (Elts.size() == NumElts)
2603 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002604 }
Nate Begemand2195702008-05-12 19:01:56 +00002605
2606 // Look up the constant in the table first to ensure uniqueness
2607 std::vector<Constant*> ArgVec;
2608 ArgVec.push_back(LHS);
2609 ArgVec.push_back(RHS);
2610 // Get the key type with both the opcode and predicate
2611 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002612
2613 // Implicitly locked.
2614 return ExprConstants->getOrCreate(LHS->getType(), Key);
Nate Begemand2195702008-05-12 19:01:56 +00002615}
2616
2617Constant *
2618ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2619 assert(isa<VectorType>(LHS->getType()) &&
2620 "Tried to create vfcmp operation on non-vector type!");
2621 assert(LHS->getType() == RHS->getType());
2622 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2623
2624 const VectorType *VTy = cast<VectorType>(LHS->getType());
2625 unsigned NumElts = VTy->getNumElements();
2626 const Type *EltTy = VTy->getElementType();
2627 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2628 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2629
Chris Lattnereab49262008-07-14 05:17:31 +00002630 // See if we can fold the element-wise comparison of the LHS and RHS.
2631 SmallVector<Constant *, 16> LHSElts, RHSElts;
2632 LHS->getVectorElements(LHSElts);
2633 RHS->getVectorElements(RHSElts);
2634
2635 if (!LHSElts.empty() && !RHSElts.empty()) {
2636 SmallVector<Constant *, 16> Elts;
2637 for (unsigned i = 0; i != NumElts; ++i) {
2638 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2639 RHSElts[i]);
2640 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2641 if (FCI->getZExtValue())
2642 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2643 else
2644 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2645 } else if (FC && isa<UndefValue>(FC)) {
2646 Elts.push_back(UndefValue::get(REltTy));
2647 } else {
2648 break;
2649 }
Nate Begemand2195702008-05-12 19:01:56 +00002650 }
Chris Lattnereab49262008-07-14 05:17:31 +00002651 if (Elts.size() == NumElts)
2652 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002653 }
Nate Begemand2195702008-05-12 19:01:56 +00002654
2655 // Look up the constant in the table first to ensure uniqueness
2656 std::vector<Constant*> ArgVec;
2657 ArgVec.push_back(LHS);
2658 ArgVec.push_back(RHS);
2659 // Get the key type with both the opcode and predicate
2660 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002661
2662 // Implicitly locked.
2663 return ExprConstants->getOrCreate(ResultTy, Key);
Nate Begemand2195702008-05-12 19:01:56 +00002664}
2665
Robert Bocchino23004482006-01-10 19:05:34 +00002666Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2667 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002668 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2669 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002670 // Look up the constant in the table first to ensure uniqueness
2671 std::vector<Constant*> ArgVec(1, Val);
2672 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002673 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002674
2675 // Implicitly locked.
2676 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002677}
2678
2679Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002680 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002681 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002682 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002683 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002684 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002685 Val, Idx);
2686}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002687
Robert Bocchinoca27f032006-01-17 20:07:22 +00002688Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2689 Constant *Elt, Constant *Idx) {
2690 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2691 return FC; // Fold a few common cases...
2692 // Look up the constant in the table first to ensure uniqueness
2693 std::vector<Constant*> ArgVec(1, Val);
2694 ArgVec.push_back(Elt);
2695 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002696 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002697
2698 // Implicitly locked.
2699 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002700}
2701
2702Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2703 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002704 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002705 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002706 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002707 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002708 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002709 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002710 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002711}
2712
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002713Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2714 Constant *V2, Constant *Mask) {
2715 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2716 return FC; // Fold a few common cases...
2717 // Look up the constant in the table first to ensure uniqueness
2718 std::vector<Constant*> ArgVec(1, V1);
2719 ArgVec.push_back(V2);
2720 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002721 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002722
2723 // Implicitly locked.
2724 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002725}
2726
2727Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2728 Constant *Mask) {
2729 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2730 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002731
2732 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2733 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2734 const Type *ShufTy = VectorType::get(EltTy, NElts);
2735 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002736}
2737
Dan Gohman12fce772008-05-15 19:50:34 +00002738Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2739 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002740 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002741 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2742 Idxs+NumIdx) == Val->getType() &&
2743 "insertvalue indices invalid!");
2744 assert(Agg->getType() == ReqTy &&
2745 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002746 assert(Agg->getType()->isFirstClassType() &&
2747 "Non-first-class type for constant InsertValue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002748 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
2749 assert(FC && "InsertValue constant expr couldn't be folded!");
2750 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002751}
2752
2753Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002754 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002755 assert(Agg->getType()->isFirstClassType() &&
2756 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002757
Dan Gohman0752bff2008-05-23 00:36:11 +00002758 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002759#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002760 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002761 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002762#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002763 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002764 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2765}
2766
2767Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002768 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002769 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2770 Idxs+NumIdx) == ReqTy &&
2771 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002772 assert(Agg->getType()->isFirstClassType() &&
2773 "Non-first-class type for constant extractvalue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002774 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
2775 assert(FC && "ExtractValue constant expr couldn't be folded!");
2776 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002777}
2778
2779Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002780 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002781 assert(Agg->getType()->isFirstClassType() &&
2782 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002783
2784 const Type *ReqTy =
2785 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2786 assert(ReqTy && "extractvalue indices invalid!");
2787 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2788}
2789
Reid Spencer2eadb532007-01-21 00:29:26 +00002790Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002791 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002792 if (PTy->getElementType()->isFloatingPoint()) {
2793 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002794 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002795 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002796 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002797
Dale Johannesen98d3a082007-09-14 22:26:36 +00002798 if (Ty->isFloatingPoint())
2799 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002800
2801 return Constant::getNullValue(Ty);
2802}
2803
Vikram S. Adve4c485332002-07-15 18:19:33 +00002804// destroyConstant - Remove the constant from the constant table...
2805//
Owen Andersonb07dd952009-06-19 23:16:19 +00002806void ConstantExpr::destroyConstant(bool locked) {
2807 ExprConstants->remove(this, locked);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002808 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002809}
2810
Chris Lattner3cd8c562002-07-30 18:54:25 +00002811const char *ConstantExpr::getOpcodeName() const {
2812 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002813}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002814
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002815//===----------------------------------------------------------------------===//
2816// replaceUsesOfWithOnConstant implementations
2817
Chris Lattner913849b2007-08-21 00:55:23 +00002818/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2819/// 'From' to be uses of 'To'. This must update the uniquing data structures
2820/// etc.
2821///
2822/// Note that we intentionally replace all uses of From with To here. Consider
2823/// a large array that uses 'From' 1000 times. By handling this case all here,
2824/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2825/// single invocation handles all 1000 uses. Handling them one at a time would
2826/// work, but would be really slow because it would have to unique each updated
2827/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002828void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002829 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002830 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002831 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002832
Jim Laskeyc03caef2006-07-17 17:38:29 +00002833 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002834 Lookup.first.first = getType();
2835 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002836
Chris Lattnerb64419a2005-10-03 22:51:37 +00002837 std::vector<Constant*> &Values = Lookup.first.second;
2838 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002839
Chris Lattner8760ec72005-10-04 01:17:50 +00002840 // Fill values with the modified operands of the constant array. Also,
2841 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002842 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002843 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002844 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002845 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2846 Constant *Val = cast<Constant>(O->get());
2847 if (Val == From) {
2848 Val = ToC;
2849 ++NumUpdated;
2850 }
2851 Values.push_back(Val);
2852 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002853 } else {
2854 isAllZeros = true;
2855 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2856 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002857 if (Val == From) {
2858 Val = ToC;
2859 ++NumUpdated;
2860 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002861 Values.push_back(Val);
2862 if (isAllZeros) isAllZeros = Val->isNullValue();
2863 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002864 }
2865
Chris Lattnerb64419a2005-10-03 22:51:37 +00002866 Constant *Replacement = 0;
2867 if (isAllZeros) {
2868 Replacement = ConstantAggregateZero::get(getType());
2869 } else {
2870 // Check to see if we have this array type already.
Owen Andersond830eb82009-06-18 19:10:19 +00002871 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002872 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002873 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002874 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002875
2876 if (Exists) {
2877 Replacement = I->second;
2878 } else {
2879 // Okay, the new shape doesn't exist in the system yet. Instead of
2880 // creating a new constant array, inserting it, replaceallusesof'ing the
2881 // old with the new, then deleting the old... just update the current one
2882 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002883 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002884
Chris Lattner913849b2007-08-21 00:55:23 +00002885 // Update to the new value. Optimize for the case when we have a single
2886 // operand that we're changing, but handle bulk updates efficiently.
2887 if (NumUpdated == 1) {
2888 unsigned OperandToUpdate = U-OperandList;
2889 assert(getOperand(OperandToUpdate) == From &&
2890 "ReplaceAllUsesWith broken!");
2891 setOperand(OperandToUpdate, ToC);
2892 } else {
2893 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2894 if (getOperand(i) == From)
2895 setOperand(i, ToC);
2896 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002897 return;
2898 }
2899 }
2900
2901 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002902 assert(Replacement != this && "I didn't contain From!");
2903
Chris Lattner7a1450d2005-10-04 18:13:04 +00002904 // Everyone using this now uses the replacement.
2905 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002906
2907 // Delete the old constant!
2908 destroyConstant();
2909}
2910
2911void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002912 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002913 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002914 Constant *ToC = cast<Constant>(To);
2915
Chris Lattnerdff59112005-10-04 18:47:09 +00002916 unsigned OperandToUpdate = U-OperandList;
2917 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2918
Jim Laskeyc03caef2006-07-17 17:38:29 +00002919 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002920 Lookup.first.first = getType();
2921 Lookup.second = this;
2922 std::vector<Constant*> &Values = Lookup.first.second;
2923 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002924
Chris Lattnerdff59112005-10-04 18:47:09 +00002925
Chris Lattner8760ec72005-10-04 01:17:50 +00002926 // Fill values with the modified operands of the constant struct. Also,
2927 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002928 bool isAllZeros = false;
2929 if (!ToC->isNullValue()) {
2930 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2931 Values.push_back(cast<Constant>(O->get()));
2932 } else {
2933 isAllZeros = true;
2934 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2935 Constant *Val = cast<Constant>(O->get());
2936 Values.push_back(Val);
2937 if (isAllZeros) isAllZeros = Val->isNullValue();
2938 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002939 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002940 Values[OperandToUpdate] = ToC;
2941
Chris Lattner8760ec72005-10-04 01:17:50 +00002942 Constant *Replacement = 0;
2943 if (isAllZeros) {
Owen Andersonb07dd952009-06-19 23:16:19 +00002944 // We're
Chris Lattner8760ec72005-10-04 01:17:50 +00002945 Replacement = ConstantAggregateZero::get(getType());
2946 } else {
2947 // Check to see if we have this array type already.
Owen Andersond830eb82009-06-18 19:10:19 +00002948 sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002949 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002950 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002951 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002952
2953 if (Exists) {
2954 Replacement = I->second;
2955 } else {
2956 // Okay, the new shape doesn't exist in the system yet. Instead of
2957 // creating a new constant struct, inserting it, replaceallusesof'ing the
2958 // old with the new, then deleting the old... just update the current one
2959 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002960 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002961
Chris Lattnerdff59112005-10-04 18:47:09 +00002962 // Update to the new value.
2963 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002964 return;
2965 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002966 }
2967
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002968 assert(Replacement != this && "I didn't contain From!");
2969
Chris Lattner7a1450d2005-10-04 18:13:04 +00002970 // Everyone using this now uses the replacement.
2971 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002972
2973 // Delete the old constant!
2974 destroyConstant();
2975}
2976
Reid Spencerd84d35b2007-02-15 02:26:10 +00002977void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002978 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002979 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2980
2981 std::vector<Constant*> Values;
2982 Values.reserve(getNumOperands()); // Build replacement array...
2983 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2984 Constant *Val = getOperand(i);
2985 if (Val == From) Val = cast<Constant>(To);
2986 Values.push_back(Val);
2987 }
2988
Reid Spencerd84d35b2007-02-15 02:26:10 +00002989 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002990 assert(Replacement != this && "I didn't contain From!");
2991
Chris Lattner7a1450d2005-10-04 18:13:04 +00002992 // Everyone using this now uses the replacement.
2993 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002994
2995 // Delete the old constant!
2996 destroyConstant();
2997}
2998
2999void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00003000 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003001 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
3002 Constant *To = cast<Constant>(ToV);
3003
3004 Constant *Replacement = 0;
3005 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00003006 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003007 Constant *Pointer = getOperand(0);
3008 Indices.reserve(getNumOperands()-1);
3009 if (Pointer == From) Pointer = To;
3010
3011 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
3012 Constant *Val = getOperand(i);
3013 if (Val == From) Val = To;
3014 Indices.push_back(Val);
3015 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00003016 Replacement = ConstantExpr::getGetElementPtr(Pointer,
3017 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00003018 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00003019 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00003020 if (Agg == From) Agg = To;
3021
Dan Gohman1ecaf452008-05-31 00:58:22 +00003022 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00003023 Replacement = ConstantExpr::getExtractValue(Agg,
3024 &Indices[0], Indices.size());
3025 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00003026 Constant *Agg = getOperand(0);
3027 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00003028 if (Agg == From) Agg = To;
3029 if (Val == From) Val = To;
3030
Dan Gohman1ecaf452008-05-31 00:58:22 +00003031 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00003032 Replacement = ConstantExpr::getInsertValue(Agg, Val,
3033 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003034 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003035 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003037 } else if (getOpcode() == Instruction::Select) {
3038 Constant *C1 = getOperand(0);
3039 Constant *C2 = getOperand(1);
3040 Constant *C3 = getOperand(2);
3041 if (C1 == From) C1 = To;
3042 if (C2 == From) C2 = To;
3043 if (C3 == From) C3 = To;
3044 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00003045 } else if (getOpcode() == Instruction::ExtractElement) {
3046 Constant *C1 = getOperand(0);
3047 Constant *C2 = getOperand(1);
3048 if (C1 == From) C1 = To;
3049 if (C2 == From) C2 = To;
3050 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00003051 } else if (getOpcode() == Instruction::InsertElement) {
3052 Constant *C1 = getOperand(0);
3053 Constant *C2 = getOperand(1);
3054 Constant *C3 = getOperand(1);
3055 if (C1 == From) C1 = To;
3056 if (C2 == From) C2 = To;
3057 if (C3 == From) C3 = To;
3058 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
3059 } else if (getOpcode() == Instruction::ShuffleVector) {
3060 Constant *C1 = getOperand(0);
3061 Constant *C2 = getOperand(1);
3062 Constant *C3 = getOperand(2);
3063 if (C1 == From) C1 = To;
3064 if (C2 == From) C2 = To;
3065 if (C3 == From) C3 = To;
3066 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00003067 } else if (isCompare()) {
3068 Constant *C1 = getOperand(0);
3069 Constant *C2 = getOperand(1);
3070 if (C1 == From) C1 = To;
3071 if (C2 == From) C2 = To;
3072 if (getOpcode() == Instruction::ICmp)
3073 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00003074 else if (getOpcode() == Instruction::FCmp)
Reid Spenceree3c9912006-12-04 05:19:50 +00003075 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00003076 else if (getOpcode() == Instruction::VICmp)
3077 Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2);
3078 else {
3079 assert(getOpcode() == Instruction::VFCmp);
3080 Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2);
3081 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003082 } else if (getNumOperands() == 2) {
3083 Constant *C1 = getOperand(0);
3084 Constant *C2 = getOperand(1);
3085 if (C1 == From) C1 = To;
3086 if (C2 == From) C2 = To;
3087 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
3088 } else {
3089 assert(0 && "Unknown ConstantExpr type!");
3090 return;
3091 }
3092
3093 assert(Replacement != this && "I didn't contain From!");
3094
Chris Lattner7a1450d2005-10-04 18:13:04 +00003095 // Everyone using this now uses the replacement.
3096 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00003097
3098 // Delete the old constant!
3099 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00003100}
Nick Lewycky49f89192009-04-04 07:22:01 +00003101
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003102void MDNode::replaceElement(Value *From, Value *To) {
3103 SmallVector<Value*, 4> Values;
3104 Values.reserve(getNumElements()); // Build replacement array...
3105 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3106 Value *Val = getElement(i);
3107 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00003108 Values.push_back(Val);
3109 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003110
3111 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00003112 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003113
Nick Lewycky49f89192009-04-04 07:22:01 +00003114 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00003115
Nick Lewycky49f89192009-04-04 07:22:01 +00003116 destroyConstant();
3117}