blob: 7cd3599a61a221f63436dd1589f029a39ffd04f2 [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"
Chris Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000021#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000022#include "llvm/Support/Debug.h"
Chris Lattner69edc982006-09-28 00:35:06 +000023#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000027#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000028#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000032// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000038 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000047 DOUT << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000052 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000054
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattner23dd1f62006-10-20 00:27:06 +000063/// canTrap - Return true if evaluation of this constant could trap. This is
64/// true for things like constant expressions that could divide by zero.
65bool Constant::canTrap() const {
66 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
67 // The only thing that could possibly trap are constant exprs.
68 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
69 if (!CE) return false;
70
71 // ConstantExpr traps if any operands can trap.
72 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
73 if (getOperand(i)->canTrap())
74 return true;
75
76 // Otherwise, only specific operations can trap.
77 switch (CE->getOpcode()) {
78 default:
79 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000080 case Instruction::UDiv:
81 case Instruction::SDiv:
82 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000083 case Instruction::URem:
84 case Instruction::SRem:
85 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000086 // Div and rem can trap if the RHS is not known to be non-zero.
87 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
88 return true;
89 return false;
90 }
91}
92
Anton Korobeynikov7437b592009-03-29 17:13:18 +000093/// ContainsRelocations - Return true if the constant value contains relocations
94/// which cannot be resolved at compile time. Kind argument is used to filter
95/// only 'interesting' sorts of relocations.
96bool Constant::ContainsRelocations(unsigned Kind) const {
97 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
98 bool isLocal = GV->hasLocalLinkage();
99 if ((Kind & Reloc::Local) && isLocal) {
100 // Global has local linkage and 'local' kind of relocations are
101 // requested
102 return true;
103 }
104
105 if ((Kind & Reloc::Global) && !isLocal) {
106 // Global has non-local linkage and 'global' kind of relocations are
107 // requested
108 return true;
109 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000110
111 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000112 }
113
Evan Chengf9e003b2007-03-08 00:59:12 +0000114 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000115 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000116 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000117
Evan Chengf9e003b2007-03-08 00:59:12 +0000118 return false;
119}
120
Chris Lattnerb1585a92002-08-13 17:50:20 +0000121// Static constructor to create a '0' constant of arbitrary type...
122Constant *Constant::getNullValue(const Type *Ty) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000123 static uint64_t zero[2] = {0, 0};
Chris Lattner6b727592004-06-17 18:19:28 +0000124 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000125 case Type::IntegerTyID:
126 return ConstantInt::get(Ty, 0);
127 case Type::FloatTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000128 return ConstantFP::get(APFloat(APInt(32, 0)));
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000129 case Type::DoubleTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000130 return ConstantFP::get(APFloat(APInt(64, 0)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000131 case Type::X86_FP80TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000132 return ConstantFP::get(APFloat(APInt(80, 2, zero)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000133 case Type::FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000134 return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
Dale Johannesen98d3a082007-09-14 22:26:36 +0000135 case Type::PPC_FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000136 return ConstantFP::get(APFloat(APInt(128, 2, zero)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000137 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000138 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000139 case Type::StructTyID:
140 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000141 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000142 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000143 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000144 // Function, Label, or Opaque type?
145 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000146 return 0;
147 }
148}
149
Chris Lattner72e39582007-06-15 06:10:53 +0000150Constant *Constant::getAllOnesValue(const Type *Ty) {
151 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
152 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
153 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
154}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000155
156// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000157ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000158 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000159 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000160 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000161}
162
Dan Gohman30978072007-05-24 14:36:04 +0000163/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000164/// has all its bits set to true.
165/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000166ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000167 std::vector<Constant*> Elts;
168 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000169 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000170 assert(Elts[0] && "Not a vector integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000171 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000172}
173
174
Chris Lattner2105d662008-07-10 00:28:11 +0000175/// getVectorElements - This method, which is only valid on constant of vector
176/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000177/// This handles breaking down a vector undef into undef elements, etc. For
178/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattner2105d662008-07-10 00:28:11 +0000179void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
180 assert(isa<VectorType>(getType()) && "Not a vector constant!");
181
182 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
183 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
184 Elts.push_back(CV->getOperand(i));
185 return;
186 }
187
188 const VectorType *VT = cast<VectorType>(getType());
189 if (isa<ConstantAggregateZero>(this)) {
190 Elts.assign(VT->getNumElements(),
191 Constant::getNullValue(VT->getElementType()));
192 return;
193 }
194
Chris Lattnerc5098a22008-07-14 05:10:41 +0000195 if (isa<UndefValue>(this)) {
196 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
197 return;
198 }
199
200 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000201}
202
203
204
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000206// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207//===----------------------------------------------------------------------===//
208
Reid Spencerb31bffe2007-02-26 23:54:03 +0000209ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000210 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000211 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212}
213
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000214ConstantInt *ConstantInt::TheTrueVal = 0;
215ConstantInt *ConstantInt::TheFalseVal = 0;
216
217namespace llvm {
218 void CleanupTrueFalse(void *) {
219 ConstantInt::ResetTrueFalse();
220 }
221}
222
223static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
224
225ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
226 assert(TheTrueVal == 0 && TheFalseVal == 0);
227 TheTrueVal = get(Type::Int1Ty, 1);
228 TheFalseVal = get(Type::Int1Ty, 0);
229
230 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
231 TrueFalseCleanup.Register();
232
233 return WhichOne ? TheTrueVal : TheFalseVal;
234}
235
236
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000237namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000238 struct DenseMapAPIntKeyInfo {
239 struct KeyTy {
240 APInt val;
241 const Type* type;
242 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
243 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
244 bool operator==(const KeyTy& that) const {
245 return type == that.type && this->val == that.val;
246 }
247 bool operator!=(const KeyTy& that) const {
248 return !this->operator==(that);
249 }
250 };
251 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
252 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000253 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000254 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000255 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000256 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000257 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
258 return LHS == RHS;
259 }
Dale Johannesena719a602007-08-24 00:56:33 +0000260 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000261 };
262}
263
264
Reid Spencerb31bffe2007-02-26 23:54:03 +0000265typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
266 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000267static ManagedStatic<IntMapTy> IntConstants;
268
Reid Spencer362fb292007-03-19 20:39:08 +0000269ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000270 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencer362fb292007-03-19 20:39:08 +0000271 return get(APInt(ITy->getBitWidth(), V, isSigned));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000272}
273
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000274// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000275// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000276// operator== and operator!= to ensure that the DenseMap doesn't attempt to
277// compare APInt's of different widths, which would violate an APInt class
278// invariant which generates an assertion.
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000279ConstantInt *ConstantInt::get(const APInt& V) {
280 // Get the corresponding integer type for the bit width of the value.
281 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000282 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000283 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Reid Spencerb31bffe2007-02-26 23:54:03 +0000284 ConstantInt *&Slot = (*IntConstants)[Key];
285 // if it exists, return it.
286 if (Slot)
287 return Slot;
288 // otherwise create a new one, insert it, and return it.
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000289 return Slot = new ConstantInt(ITy, V);
290}
291
292//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000293// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000294//===----------------------------------------------------------------------===//
295
Chris Lattner98bd9392008-04-09 06:38:30 +0000296static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
297 if (Ty == Type::FloatTy)
298 return &APFloat::IEEEsingle;
299 if (Ty == Type::DoubleTy)
300 return &APFloat::IEEEdouble;
301 if (Ty == Type::X86_FP80Ty)
302 return &APFloat::x87DoubleExtended;
303 else if (Ty == Type::FP128Ty)
304 return &APFloat::IEEEquad;
305
306 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
307 return &APFloat::PPCDoubleDouble;
308}
309
Dale Johannesend246b2c2007-08-30 00:23:21 +0000310ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
311 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000312 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
313 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314}
315
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000316bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000317 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000318}
319
Dale Johannesen98d3a082007-09-14 22:26:36 +0000320ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
321 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
322 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000323 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000324}
325
Dale Johannesend246b2c2007-08-30 00:23:21 +0000326bool ConstantFP::isExactlyValue(const APFloat& V) const {
327 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000328}
329
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000330namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000331 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000332 struct KeyTy {
333 APFloat val;
334 KeyTy(const APFloat& V) : val(V){}
335 KeyTy(const KeyTy& that) : val(that.val) {}
336 bool operator==(const KeyTy& that) const {
337 return this->val.bitwiseIsEqual(that.val);
338 }
339 bool operator!=(const KeyTy& that) const {
340 return !this->operator==(that);
341 }
342 };
343 static inline KeyTy getEmptyKey() {
344 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000345 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000346 static inline KeyTy getTombstoneKey() {
347 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000348 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000349 static unsigned getHashValue(const KeyTy &Key) {
350 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000351 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000352 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
353 return LHS == RHS;
354 }
Dale Johannesena719a602007-08-24 00:56:33 +0000355 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000356 };
357}
358
359//---- ConstantFP::get() implementation...
360//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000361typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000362 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000363
Dale Johannesena719a602007-08-24 00:56:33 +0000364static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000365
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000366ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000367 DenseMapAPFloatKeyInfo::KeyTy Key(V);
368 ConstantFP *&Slot = (*FPConstants)[Key];
369 if (Slot) return Slot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000370
371 const Type *Ty;
372 if (&V.getSemantics() == &APFloat::IEEEsingle)
373 Ty = Type::FloatTy;
374 else if (&V.getSemantics() == &APFloat::IEEEdouble)
375 Ty = Type::DoubleTy;
376 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
377 Ty = Type::X86_FP80Ty;
378 else if (&V.getSemantics() == &APFloat::IEEEquad)
379 Ty = Type::FP128Ty;
380 else {
381 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format");
382 Ty = Type::PPC_FP128Ty;
383 }
384
Dale Johannesend246b2c2007-08-30 00:23:21 +0000385 return Slot = new ConstantFP(Ty, V);
386}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000387
Chris Lattner98bd9392008-04-09 06:38:30 +0000388/// get() - This returns a constant fp for the specified value in the
389/// specified type. This should only be used for simple constant values like
390/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
391ConstantFP *ConstantFP::get(const Type *Ty, double V) {
392 APFloat FV(V);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000393 bool ignored;
394 FV.convert(*TypeToFloatSemantics(Ty), APFloat::rmNearestTiesToEven, &ignored);
Chris Lattner98bd9392008-04-09 06:38:30 +0000395 return get(FV);
396}
397
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000398//===----------------------------------------------------------------------===//
399// ConstantXXX Classes
400//===----------------------------------------------------------------------===//
401
402
Chris Lattner3462ae32001-12-03 22:26:30 +0000403ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000404 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 : Constant(T, ConstantArrayVal,
406 OperandTraits<ConstantArray>::op_end(this) - V.size(),
407 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000408 assert(V.size() == T->getNumElements() &&
409 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000410 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000411 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
412 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000413 Constant *C = *I;
414 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000415 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000416 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000417 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000418 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000419 }
420}
421
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000422
Chris Lattner3462ae32001-12-03 22:26:30 +0000423ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000424 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000425 : Constant(T, ConstantStructVal,
426 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
427 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000428 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000429 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000430 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000431 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
432 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000433 Constant *C = *I;
434 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000435 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000436 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000437 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000438 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000439 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000440 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000441 }
442}
443
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000444
Reid Spencerd84d35b2007-02-15 02:26:10 +0000445ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000446 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000447 : Constant(T, ConstantVectorVal,
448 OperandTraits<ConstantVector>::op_end(this) - V.size(),
449 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000450 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000451 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
452 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000453 Constant *C = *I;
454 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000455 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000456 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000457 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000458 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000459 }
460}
461
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000462
Gabor Greiff6caff662008-05-10 08:32:32 +0000463namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000464// We declare several classes private to this file, so use an anonymous
465// namespace
466namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000467
Gordon Henriksen14a55692007-12-10 02:14:30 +0000468/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
469/// behind the scenes to implement unary constant exprs.
470class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000471 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000473 // allocate space for exactly one operand
474 void *operator new(size_t s) {
475 return User::operator new(s, 1);
476 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000477 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000478 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
479 Op<0>() = C;
480 }
481 /// Transparently provide more efficient getOperand methods.
482 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000483};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000484
Gordon Henriksen14a55692007-12-10 02:14:30 +0000485/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
486/// behind the scenes to implement binary constant exprs.
487class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000488 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000489public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000490 // allocate space for exactly two operands
491 void *operator new(size_t s) {
492 return User::operator new(s, 2);
493 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000494 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000495 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000496 Op<0>() = C1;
497 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000498 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000499 /// Transparently provide more efficient getOperand methods.
500 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000501};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000502
Gordon Henriksen14a55692007-12-10 02:14:30 +0000503/// SelectConstantExpr - This class is private to Constants.cpp, and is used
504/// behind the scenes to implement select constant exprs.
505class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000506 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000507public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000508 // allocate space for exactly three operands
509 void *operator new(size_t s) {
510 return User::operator new(s, 3);
511 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000512 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000513 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000514 Op<0>() = C1;
515 Op<1>() = C2;
516 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000517 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000518 /// Transparently provide more efficient getOperand methods.
519 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000520};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000521
Gordon Henriksen14a55692007-12-10 02:14:30 +0000522/// ExtractElementConstantExpr - This class is private to
523/// Constants.cpp, and is used behind the scenes to implement
524/// extractelement constant exprs.
525class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000526 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000527public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000528 // allocate space for exactly two operands
529 void *operator new(size_t s) {
530 return User::operator new(s, 2);
531 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000532 ExtractElementConstantExpr(Constant *C1, Constant *C2)
533 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000534 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000535 Op<0>() = C1;
536 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000537 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000538 /// Transparently provide more efficient getOperand methods.
539 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000540};
Robert Bocchino23004482006-01-10 19:05:34 +0000541
Gordon Henriksen14a55692007-12-10 02:14:30 +0000542/// InsertElementConstantExpr - This class is private to
543/// Constants.cpp, and is used behind the scenes to implement
544/// insertelement constant exprs.
545class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000546 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000547public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000548 // allocate space for exactly three operands
549 void *operator new(size_t s) {
550 return User::operator new(s, 3);
551 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000552 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
553 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000554 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000555 Op<0>() = C1;
556 Op<1>() = C2;
557 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000558 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000559 /// Transparently provide more efficient getOperand methods.
560 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000561};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000562
Gordon Henriksen14a55692007-12-10 02:14:30 +0000563/// ShuffleVectorConstantExpr - This class is private to
564/// Constants.cpp, and is used behind the scenes to implement
565/// shufflevector constant exprs.
566class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000567 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000568public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000569 // allocate space for exactly three operands
570 void *operator new(size_t s) {
571 return User::operator new(s, 3);
572 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000573 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000574 : ConstantExpr(VectorType::get(
575 cast<VectorType>(C1->getType())->getElementType(),
576 cast<VectorType>(C3->getType())->getNumElements()),
577 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000578 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000579 Op<0>() = C1;
580 Op<1>() = C2;
581 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000582 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000583 /// Transparently provide more efficient getOperand methods.
584 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000585};
586
Dan Gohman12fce772008-05-15 19:50:34 +0000587/// ExtractValueConstantExpr - This class is private to
588/// Constants.cpp, and is used behind the scenes to implement
589/// extractvalue constant exprs.
590class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000591 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000592public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000593 // allocate space for exactly one operand
594 void *operator new(size_t s) {
595 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000596 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000597 ExtractValueConstantExpr(Constant *Agg,
598 const SmallVector<unsigned, 4> &IdxList,
599 const Type *DestTy)
600 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
601 Indices(IdxList) {
602 Op<0>() = Agg;
603 }
604
Dan Gohman7bb04502008-05-31 19:09:08 +0000605 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000606 const SmallVector<unsigned, 4> Indices;
607
Dan Gohman12fce772008-05-15 19:50:34 +0000608 /// Transparently provide more efficient getOperand methods.
609 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
610};
611
612/// InsertValueConstantExpr - This class is private to
613/// Constants.cpp, and is used behind the scenes to implement
614/// insertvalue constant exprs.
615class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000616 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000617public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000618 // allocate space for exactly one operand
619 void *operator new(size_t s) {
620 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000621 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000622 InsertValueConstantExpr(Constant *Agg, Constant *Val,
623 const SmallVector<unsigned, 4> &IdxList,
624 const Type *DestTy)
625 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
626 Indices(IdxList) {
627 Op<0>() = Agg;
628 Op<1>() = Val;
629 }
630
Dan Gohman7bb04502008-05-31 19:09:08 +0000631 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000632 const SmallVector<unsigned, 4> Indices;
633
Dan Gohman12fce772008-05-15 19:50:34 +0000634 /// Transparently provide more efficient getOperand methods.
635 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
636};
637
638
Gordon Henriksen14a55692007-12-10 02:14:30 +0000639/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
640/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000641class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000642 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000643 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000644public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000645 static GetElementPtrConstantExpr *Create(Constant *C,
646 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000647 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000648 return new(IdxList.size() + 1)
649 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000650 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000651 /// Transparently provide more efficient getOperand methods.
652 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000653};
654
655// CompareConstantExpr - This class is private to Constants.cpp, and is used
656// behind the scenes to implement ICmp and FCmp constant expressions. This is
657// needed in order to store the predicate value for these instructions.
658struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000659 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
660 // allocate space for exactly two operands
661 void *operator new(size_t s) {
662 return User::operator new(s, 2);
663 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000664 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000665 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
666 unsigned short pred, Constant* LHS, Constant* RHS)
667 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000668 Op<0>() = LHS;
669 Op<1>() = RHS;
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
675} // end anonymous namespace
676
Gabor Greiff6caff662008-05-10 08:32:32 +0000677template <>
678struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
679};
680DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
681
682template <>
683struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
684};
685DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
686
687template <>
688struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
689};
690DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
691
692template <>
693struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
694};
695DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
696
697template <>
698struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
699};
700DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
701
702template <>
703struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
704};
705DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
706
Dan Gohman12fce772008-05-15 19:50:34 +0000707template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000708struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000709};
Dan Gohman12fce772008-05-15 19:50:34 +0000710DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
711
712template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000713struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000714};
Dan Gohman12fce772008-05-15 19:50:34 +0000715DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
716
Gabor Greiff6caff662008-05-10 08:32:32 +0000717template <>
718struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
719};
720
721GetElementPtrConstantExpr::GetElementPtrConstantExpr
722 (Constant *C,
723 const std::vector<Constant*> &IdxList,
724 const Type *DestTy)
725 : ConstantExpr(DestTy, Instruction::GetElementPtr,
726 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
727 - (IdxList.size()+1),
728 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000729 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000730 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000731 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000732}
733
734DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
735
736
737template <>
738struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
739};
740DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
741
742
743} // End llvm namespace
744
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000745
746// Utility function for determining if a ConstantExpr is a CastOp or not. This
747// can't be inline because we don't want to #include Instruction.h into
748// Constant.h
749bool ConstantExpr::isCast() const {
750 return Instruction::isCast(getOpcode());
751}
752
Reid Spenceree3c9912006-12-04 05:19:50 +0000753bool ConstantExpr::isCompare() const {
Chris Lattnereab49262008-07-14 05:17:31 +0000754 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp ||
755 getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000756}
757
Dan Gohman1ecaf452008-05-31 00:58:22 +0000758bool ConstantExpr::hasIndices() const {
759 return getOpcode() == Instruction::ExtractValue ||
760 getOpcode() == Instruction::InsertValue;
761}
762
763const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
764 if (const ExtractValueConstantExpr *EVCE =
765 dyn_cast<ExtractValueConstantExpr>(this))
766 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000767
768 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000769}
770
Chris Lattner817175f2004-03-29 02:37:53 +0000771/// ConstantExpr::get* - Return some common constants without having to
772/// specify the full Instruction::OPCODE identifier.
773///
774Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000775 return get(Instruction::Sub,
776 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
777 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000778}
779Constant *ConstantExpr::getNot(Constant *C) {
Dale Johannesen47a5ef32008-08-21 21:20:09 +0000780 assert((isa<IntegerType>(C->getType()) ||
781 cast<VectorType>(C->getType())->getElementType()->isInteger()) &&
782 "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000783 return get(Instruction::Xor, C,
Dale Johannesen47a5ef32008-08-21 21:20:09 +0000784 Constant::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000785}
786Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
787 return get(Instruction::Add, C1, C2);
788}
789Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
790 return get(Instruction::Sub, C1, C2);
791}
792Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
793 return get(Instruction::Mul, C1, C2);
794}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000795Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
796 return get(Instruction::UDiv, C1, C2);
797}
798Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
799 return get(Instruction::SDiv, C1, C2);
800}
801Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
802 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000803}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000804Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
805 return get(Instruction::URem, C1, C2);
806}
807Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
808 return get(Instruction::SRem, C1, C2);
809}
810Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
811 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000812}
813Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
814 return get(Instruction::And, C1, C2);
815}
816Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
817 return get(Instruction::Or, C1, C2);
818}
819Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
820 return get(Instruction::Xor, C1, C2);
821}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000822unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000823 assert(getOpcode() == Instruction::FCmp ||
824 getOpcode() == Instruction::ICmp ||
825 getOpcode() == Instruction::VFCmp ||
826 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000827 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000828}
Chris Lattner817175f2004-03-29 02:37:53 +0000829Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
830 return get(Instruction::Shl, C1, C2);
831}
Reid Spencerfdff9382006-11-08 06:47:33 +0000832Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
833 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000834}
Reid Spencerfdff9382006-11-08 06:47:33 +0000835Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
836 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000837}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000838
Chris Lattner7c1018a2006-07-14 19:37:40 +0000839/// getWithOperandReplaced - Return a constant expression identical to this
840/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000841Constant *
842ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000843 assert(OpNo < getNumOperands() && "Operand num is out of range!");
844 assert(Op->getType() == getOperand(OpNo)->getType() &&
845 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000846 if (getOperand(OpNo) == Op)
847 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000848
Chris Lattner227816342006-07-14 22:20:01 +0000849 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000850 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000851 case Instruction::Trunc:
852 case Instruction::ZExt:
853 case Instruction::SExt:
854 case Instruction::FPTrunc:
855 case Instruction::FPExt:
856 case Instruction::UIToFP:
857 case Instruction::SIToFP:
858 case Instruction::FPToUI:
859 case Instruction::FPToSI:
860 case Instruction::PtrToInt:
861 case Instruction::IntToPtr:
862 case Instruction::BitCast:
863 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000864 case Instruction::Select:
865 Op0 = (OpNo == 0) ? Op : getOperand(0);
866 Op1 = (OpNo == 1) ? Op : getOperand(1);
867 Op2 = (OpNo == 2) ? Op : getOperand(2);
868 return ConstantExpr::getSelect(Op0, Op1, Op2);
869 case Instruction::InsertElement:
870 Op0 = (OpNo == 0) ? Op : getOperand(0);
871 Op1 = (OpNo == 1) ? Op : getOperand(1);
872 Op2 = (OpNo == 2) ? Op : getOperand(2);
873 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
874 case Instruction::ExtractElement:
875 Op0 = (OpNo == 0) ? Op : getOperand(0);
876 Op1 = (OpNo == 1) ? Op : getOperand(1);
877 return ConstantExpr::getExtractElement(Op0, Op1);
878 case Instruction::ShuffleVector:
879 Op0 = (OpNo == 0) ? Op : getOperand(0);
880 Op1 = (OpNo == 1) ? Op : getOperand(1);
881 Op2 = (OpNo == 2) ? Op : getOperand(2);
882 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000883 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000884 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000885 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000886 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000887 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000888 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000889 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000890 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000891 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000892 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000893 default:
894 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000895 Op0 = (OpNo == 0) ? Op : getOperand(0);
896 Op1 = (OpNo == 1) ? Op : getOperand(1);
897 return ConstantExpr::get(getOpcode(), Op0, Op1);
898 }
899}
900
901/// getWithOperands - This returns the current constant expression with the
902/// operands replaced with the specified values. The specified operands must
903/// match count and type with the existing ones.
904Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000905getWithOperands(Constant* const *Ops, unsigned NumOps) const {
906 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000907 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000908 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000909 assert(Ops[i]->getType() == getOperand(i)->getType() &&
910 "Operand type mismatch!");
911 AnyChange |= Ops[i] != getOperand(i);
912 }
913 if (!AnyChange) // No operands changed, return self.
914 return const_cast<ConstantExpr*>(this);
915
916 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000917 case Instruction::Trunc:
918 case Instruction::ZExt:
919 case Instruction::SExt:
920 case Instruction::FPTrunc:
921 case Instruction::FPExt:
922 case Instruction::UIToFP:
923 case Instruction::SIToFP:
924 case Instruction::FPToUI:
925 case Instruction::FPToSI:
926 case Instruction::PtrToInt:
927 case Instruction::IntToPtr:
928 case Instruction::BitCast:
929 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000930 case Instruction::Select:
931 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
932 case Instruction::InsertElement:
933 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
934 case Instruction::ExtractElement:
935 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
936 case Instruction::ShuffleVector:
937 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000938 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000939 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000940 case Instruction::ICmp:
941 case Instruction::FCmp:
Nate Begeman098cc6f2008-07-25 17:56:27 +0000942 case Instruction::VICmp:
943 case Instruction::VFCmp:
Reid Spencer266e42b2006-12-23 06:05:41 +0000944 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000945 default:
946 assert(getNumOperands() == 2 && "Must be binary operator?");
947 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000948 }
949}
950
Chris Lattner2f7c9632001-06-06 20:29:01 +0000951
952//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000953// isValueValidForType implementations
954
Reid Spencere7334722006-12-19 01:28:19 +0000955bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000956 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000957 if (Ty == Type::Int1Ty)
958 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000959 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000960 return true; // always true, has to fit in largest type
961 uint64_t Max = (1ll << NumBits) - 1;
962 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000963}
964
Reid Spencere0fc4df2006-10-20 07:07:24 +0000965bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000966 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000967 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000968 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000969 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000970 return true; // always true, has to fit in largest type
971 int64_t Min = -(1ll << (NumBits-1));
972 int64_t Max = (1ll << (NumBits-1)) - 1;
973 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000974}
975
Dale Johannesend246b2c2007-08-30 00:23:21 +0000976bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
977 // convert modifies in place, so make a copy.
978 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000979 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000980 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000981 default:
982 return false; // These can't be represented as floating point!
983
Dale Johannesend246b2c2007-08-30 00:23:21 +0000984 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000985 case Type::FloatTyID: {
986 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
987 return true;
988 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
989 return !losesInfo;
990 }
991 case Type::DoubleTyID: {
992 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
993 &Val2.getSemantics() == &APFloat::IEEEdouble)
994 return true;
995 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
996 return !losesInfo;
997 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000998 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000999 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1000 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1001 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001002 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001003 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1004 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1005 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001006 case Type::PPC_FP128TyID:
1007 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1008 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1009 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001010 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001011}
Chris Lattner9655e542001-07-20 19:16:02 +00001012
Chris Lattner49d855c2001-09-07 16:46:31 +00001013//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001014// Factory Function Implementation
1015
Gabor Greiff6caff662008-05-10 08:32:32 +00001016
1017// The number of operands for each ConstantCreator::create method is
1018// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001019// ConstantCreator - A class that is used to create constants by
1020// ValueMap*. This class should be partially specialized if there is
1021// something strange that needs to be done to interface to the ctor for the
1022// constant.
1023//
Chris Lattner189d19f2003-11-21 20:23:48 +00001024namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001025 template<class ValType>
1026 struct ConstantTraits;
1027
1028 template<typename T, typename Alloc>
1029 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1030 static unsigned uses(const std::vector<T, Alloc>& v) {
1031 return v.size();
1032 }
1033 };
1034
Chris Lattner189d19f2003-11-21 20:23:48 +00001035 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001036 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001037 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001038 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001039 }
1040 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001041
Chris Lattner189d19f2003-11-21 20:23:48 +00001042 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001043 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001044 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1045 assert(0 && "This type cannot be converted!\n");
1046 abort();
1047 }
1048 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001049
Chris Lattner935aa922005-10-04 17:48:46 +00001050 template<class ValType, class TypeClass, class ConstantClass,
1051 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001052 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001053 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001054 typedef std::pair<const Type*, ValType> MapKey;
1055 typedef std::map<MapKey, Constant *> MapTy;
1056 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1057 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001058 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001059 /// Map - This is the main map from the element descriptor to the Constants.
1060 /// This is the primary way we avoid creating two of the same shape
1061 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001062 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001063
1064 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1065 /// from the constants to their element in Map. This is important for
1066 /// removal of constants from the array, which would otherwise have to scan
1067 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001068 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001069
Jim Laskeyc03caef2006-07-17 17:38:29 +00001070 /// AbstractTypeMap - Map for abstract type constants.
1071 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001072 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +00001073
Chris Lattner98fa07b2003-05-23 20:03:32 +00001074 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001075 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001076
1077 /// InsertOrGetItem - Return an iterator for the specified element.
1078 /// If the element exists in the map, the returned iterator points to the
1079 /// entry and Exists=true. If not, the iterator points to the newly
1080 /// inserted entry and returns Exists=false. Newly inserted entries have
1081 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001082 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1083 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001084 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001085 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001086 Exists = !IP.second;
1087 return IP.first;
1088 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001089
Chris Lattner935aa922005-10-04 17:48:46 +00001090private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001091 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001092 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001093 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001094 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1095 IMI->second->second == CP &&
1096 "InverseMap corrupt!");
1097 return IMI->second;
1098 }
1099
Jim Laskeyc03caef2006-07-17 17:38:29 +00001100 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001101 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1102 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001103 if (I == Map.end() || I->second != CP) {
1104 // FIXME: This should not use a linear scan. If this gets to be a
1105 // performance problem, someone should look at this.
1106 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1107 /* empty */;
1108 }
Chris Lattner935aa922005-10-04 17:48:46 +00001109 return I;
1110 }
1111public:
1112
Chris Lattnerb64419a2005-10-03 22:51:37 +00001113 /// getOrCreate - Return the specified constant from the map, creating it if
1114 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001115 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001116 MapKey Lookup(Ty, V);
Dan Gohman3707f1d2008-07-11 20:58:19 +00001117 typename MapTy::iterator I = Map.find(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +00001118 // Is it in the map?
Dan Gohman3707f1d2008-07-11 20:58:19 +00001119 if (I != Map.end())
Reid Spencere0fc4df2006-10-20 07:07:24 +00001120 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001121
1122 // If no preexisting value, create one now...
1123 ConstantClass *Result =
1124 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1125
Chris Lattnerf97ab6d2008-08-23 03:48:35 +00001126 assert(Result->getType() == Ty && "Type specified is not correct!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001127 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1128
Chris Lattner935aa922005-10-04 17:48:46 +00001129 if (HasLargeKey) // Remember the reverse mapping if needed.
1130 InverseMap.insert(std::make_pair(Result, I));
1131
Chris Lattnerb50d1352003-10-05 00:17:43 +00001132 // If the type of the constant is abstract, make sure that an entry exists
1133 // for it in the AbstractTypeMap.
1134 if (Ty->isAbstract()) {
Dan Gohman3707f1d2008-07-11 20:58:19 +00001135 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001136
Dan Gohman3707f1d2008-07-11 20:58:19 +00001137 if (TI == AbstractTypeMap.end()) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001138 // Add ourselves to the ATU list of the type.
1139 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1140
1141 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1142 }
1143 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001144 return Result;
1145 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001146
Chris Lattner98fa07b2003-05-23 20:03:32 +00001147 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001148 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001149 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001150 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001151
Chris Lattner935aa922005-10-04 17:48:46 +00001152 if (HasLargeKey) // Remember the reverse mapping if needed.
1153 InverseMap.erase(CP);
1154
Chris Lattnerb50d1352003-10-05 00:17:43 +00001155 // Now that we found the entry, make sure this isn't the entry that
1156 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001157 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001158 if (Ty->isAbstract()) {
1159 assert(AbstractTypeMap.count(Ty) &&
1160 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001161 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001162 if (ATMEntryIt == I) {
1163 // Yes, we are removing the representative entry for this type.
1164 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001165 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001166
Chris Lattnerb50d1352003-10-05 00:17:43 +00001167 // First check the entry before this one...
1168 if (TmpIt != Map.begin()) {
1169 --TmpIt;
1170 if (TmpIt->first.first != Ty) // Not the same type, move back...
1171 ++TmpIt;
1172 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001173
Chris Lattnerb50d1352003-10-05 00:17:43 +00001174 // If we didn't find the same type, try to move forward...
1175 if (TmpIt == ATMEntryIt) {
1176 ++TmpIt;
1177 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1178 --TmpIt; // No entry afterwards with the same type
1179 }
1180
1181 // If there is another entry in the map of the same abstract type,
1182 // update the AbstractTypeMap entry now.
1183 if (TmpIt != ATMEntryIt) {
1184 ATMEntryIt = TmpIt;
1185 } else {
1186 // Otherwise, we are removing the last instance of this type
1187 // from the table. Remove from the ATM, and from user list.
1188 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1189 AbstractTypeMap.erase(Ty);
1190 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001191 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001192 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001193
Chris Lattnerb50d1352003-10-05 00:17:43 +00001194 Map.erase(I);
1195 }
1196
Chris Lattner3b793c62005-10-04 21:35:50 +00001197
1198 /// MoveConstantToNewSlot - If we are about to change C to be the element
1199 /// specified by I, update our internal data structures to reflect this
1200 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001201 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001202 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001203 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001204 assert(OldI != Map.end() && "Constant not found in constant table!");
1205 assert(OldI->second == C && "Didn't find correct element?");
1206
1207 // If this constant is the representative element for its abstract type,
1208 // update the AbstractTypeMap so that the representative element is I.
1209 if (C->getType()->isAbstract()) {
1210 typename AbstractTypeMapTy::iterator ATI =
1211 AbstractTypeMap.find(C->getType());
1212 assert(ATI != AbstractTypeMap.end() &&
1213 "Abstract type not in AbstractTypeMap?");
1214 if (ATI->second == OldI)
1215 ATI->second = I;
1216 }
1217
1218 // Remove the old entry from the map.
1219 Map.erase(OldI);
1220
1221 // Update the inverse map so that we know that this constant is now
1222 // located at descriptor I.
1223 if (HasLargeKey) {
1224 assert(I->second == C && "Bad inversemap entry!");
1225 InverseMap[C] = I;
1226 }
1227 }
1228
Chris Lattnerb50d1352003-10-05 00:17:43 +00001229 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001230 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001231 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001232
1233 assert(I != AbstractTypeMap.end() &&
1234 "Abstract type not in AbstractTypeMap?");
1235
1236 // Convert a constant at a time until the last one is gone. The last one
1237 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1238 // eliminated eventually.
1239 do {
1240 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001241 TypeClass>::convert(
1242 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001243 cast<TypeClass>(NewTy));
1244
Jim Laskeyc03caef2006-07-17 17:38:29 +00001245 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001246 } while (I != AbstractTypeMap.end());
1247 }
1248
1249 // If the type became concrete without being refined to any other existing
1250 // type, we just remove ourselves from the ATU list.
1251 void typeBecameConcrete(const DerivedType *AbsTy) {
1252 AbsTy->removeAbstractTypeUser(this);
1253 }
1254
1255 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001256 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001257 }
1258 };
1259}
1260
Chris Lattnera84df0a22006-09-28 23:36:21 +00001261
Chris Lattner28173502007-02-20 06:11:36 +00001262
Chris Lattner9fba3da2004-02-15 05:53:04 +00001263//---- ConstantAggregateZero::get() implementation...
1264//
1265namespace llvm {
1266 // ConstantAggregateZero does not take extra "value" argument...
1267 template<class ValType>
1268 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1269 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1270 return new ConstantAggregateZero(Ty);
1271 }
1272 };
1273
1274 template<>
1275 struct ConvertConstantType<ConstantAggregateZero, Type> {
1276 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1277 // Make everyone now use a constant of the new type...
1278 Constant *New = ConstantAggregateZero::get(NewTy);
1279 assert(New != OldC && "Didn't replace constant??");
1280 OldC->uncheckedReplaceAllUsesWith(New);
1281 OldC->destroyConstant(); // This constant is now dead, destroy it.
1282 }
1283 };
1284}
1285
Chris Lattner69edc982006-09-28 00:35:06 +00001286static ManagedStatic<ValueMap<char, Type,
1287 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001288
Chris Lattner3e650af2004-08-04 04:48:01 +00001289static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1290
Dan Gohman8214fc12008-12-08 07:10:54 +00001291ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001292 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001293 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +00001294 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001295}
1296
Dan Gohman92b551b2009-03-03 02:55:14 +00001297/// destroyConstant - Remove the constant from the constant table...
1298///
Chris Lattner9fba3da2004-02-15 05:53:04 +00001299void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001300 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001301 destroyConstantImpl();
1302}
1303
Chris Lattner3462ae32001-12-03 22:26:30 +00001304//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001305//
Chris Lattner189d19f2003-11-21 20:23:48 +00001306namespace llvm {
1307 template<>
1308 struct ConvertConstantType<ConstantArray, ArrayType> {
1309 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1310 // Make everyone now use a constant of the new type...
1311 std::vector<Constant*> C;
1312 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1313 C.push_back(cast<Constant>(OldC->getOperand(i)));
1314 Constant *New = ConstantArray::get(NewTy, C);
1315 assert(New != OldC && "Didn't replace constant??");
1316 OldC->uncheckedReplaceAllUsesWith(New);
1317 OldC->destroyConstant(); // This constant is now dead, destroy it.
1318 }
1319 };
1320}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001321
Chris Lattner3e650af2004-08-04 04:48:01 +00001322static std::vector<Constant*> getValType(ConstantArray *CA) {
1323 std::vector<Constant*> Elements;
1324 Elements.reserve(CA->getNumOperands());
1325 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1326 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1327 return Elements;
1328}
1329
Chris Lattnerb64419a2005-10-03 22:51:37 +00001330typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001331 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001332static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001333
Chris Lattner015e8212004-02-15 04:14:47 +00001334Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001335 const std::vector<Constant*> &V) {
1336 // If this is an all-zero array, return a ConstantAggregateZero object
1337 if (!V.empty()) {
1338 Constant *C = V[0];
1339 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001340 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001341 for (unsigned i = 1, e = V.size(); i != e; ++i)
1342 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001343 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001344 }
1345 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001346}
1347
Dan Gohman92b551b2009-03-03 02:55:14 +00001348/// destroyConstant - Remove the constant from the constant table...
1349///
Chris Lattner98fa07b2003-05-23 20:03:32 +00001350void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001351 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001352 destroyConstantImpl();
1353}
1354
Reid Spencer6f614532006-05-30 08:23:18 +00001355/// ConstantArray::get(const string&) - Return an array that is initialized to
1356/// contain the specified string. If length is zero then a null terminator is
1357/// added to the specified string so that it may be used in a natural way.
1358/// Otherwise, the length parameter specifies how much of the string to use
1359/// and it won't be null terminated.
1360///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001361Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001362 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001363 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001364 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001365
1366 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001367 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001368 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001369 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001370
Reid Spencer8d9336d2006-12-31 05:26:44 +00001371 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001372 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001373}
1374
Reid Spencer2546b762007-01-26 07:37:34 +00001375/// isString - This method returns true if the array is an array of i8, and
1376/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001377bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001378 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001379 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001380 return false;
1381 // Check the elements to make sure they are all integers, not constant
1382 // expressions.
1383 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1384 if (!isa<ConstantInt>(getOperand(i)))
1385 return false;
1386 return true;
1387}
1388
Evan Cheng3763c5b2006-10-26 19:15:05 +00001389/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001390/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001391/// null bytes except its terminator.
1392bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001393 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001394 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001395 return false;
1396 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1397 // Last element must be a null.
1398 if (getOperand(getNumOperands()-1) != Zero)
1399 return false;
1400 // Other elements must be non-null integers.
1401 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1402 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001403 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001404 if (getOperand(i) == Zero)
1405 return false;
1406 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001407 return true;
1408}
1409
1410
Dan Gohman92b551b2009-03-03 02:55:14 +00001411/// getAsString - If the sub-element type of this array is i8
1412/// then this method converts the array to an std::string and returns it.
1413/// Otherwise, it asserts out.
1414///
Chris Lattner81fabb02002-08-26 17:53:56 +00001415std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001416 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001417 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001418 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001419 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001420 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001421 return Result;
1422}
1423
1424
Chris Lattner3462ae32001-12-03 22:26:30 +00001425//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001426//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001427
Chris Lattner189d19f2003-11-21 20:23:48 +00001428namespace llvm {
1429 template<>
1430 struct ConvertConstantType<ConstantStruct, StructType> {
1431 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1432 // Make everyone now use a constant of the new type...
1433 std::vector<Constant*> C;
1434 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1435 C.push_back(cast<Constant>(OldC->getOperand(i)));
1436 Constant *New = ConstantStruct::get(NewTy, C);
1437 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001438
Chris Lattner189d19f2003-11-21 20:23:48 +00001439 OldC->uncheckedReplaceAllUsesWith(New);
1440 OldC->destroyConstant(); // This constant is now dead, destroy it.
1441 }
1442 };
1443}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001444
Chris Lattner8760ec72005-10-04 01:17:50 +00001445typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001446 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001447static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001448
Chris Lattner3e650af2004-08-04 04:48:01 +00001449static std::vector<Constant*> getValType(ConstantStruct *CS) {
1450 std::vector<Constant*> Elements;
1451 Elements.reserve(CS->getNumOperands());
1452 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1453 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1454 return Elements;
1455}
1456
Chris Lattner015e8212004-02-15 04:14:47 +00001457Constant *ConstantStruct::get(const StructType *Ty,
1458 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001459 // Create a ConstantAggregateZero value if all elements are zeros...
1460 for (unsigned i = 0, e = V.size(); i != e; ++i)
1461 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001462 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001463
1464 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001465}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001466
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001467Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001468 std::vector<const Type*> StructEls;
1469 StructEls.reserve(V.size());
1470 for (unsigned i = 0, e = V.size(); i != e; ++i)
1471 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001472 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001473}
1474
Chris Lattnerd7a73302001-10-13 06:57:33 +00001475// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001476//
Chris Lattner3462ae32001-12-03 22:26:30 +00001477void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001478 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001479 destroyConstantImpl();
1480}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001481
Reid Spencerd84d35b2007-02-15 02:26:10 +00001482//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001483//
1484namespace llvm {
1485 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001486 struct ConvertConstantType<ConstantVector, VectorType> {
1487 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001488 // Make everyone now use a constant of the new type...
1489 std::vector<Constant*> C;
1490 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1491 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001492 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001493 assert(New != OldC && "Didn't replace constant??");
1494 OldC->uncheckedReplaceAllUsesWith(New);
1495 OldC->destroyConstant(); // This constant is now dead, destroy it.
1496 }
1497 };
1498}
1499
Reid Spencerd84d35b2007-02-15 02:26:10 +00001500static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001501 std::vector<Constant*> Elements;
1502 Elements.reserve(CP->getNumOperands());
1503 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1504 Elements.push_back(CP->getOperand(i));
1505 return Elements;
1506}
1507
Reid Spencerd84d35b2007-02-15 02:26:10 +00001508static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001509 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001510
Reid Spencerd84d35b2007-02-15 02:26:10 +00001511Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001512 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001513 assert(!V.empty() && "Vectors can't be empty");
1514 // If this is an all-undef or alll-zero vector, return a
1515 // ConstantAggregateZero or UndefValue.
1516 Constant *C = V[0];
1517 bool isZero = C->isNullValue();
1518 bool isUndef = isa<UndefValue>(C);
1519
1520 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001521 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001522 if (V[i] != C) {
1523 isZero = isUndef = false;
1524 break;
1525 }
Brian Gaeke02209042004-08-20 06:00:58 +00001526 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001527
1528 if (isZero)
1529 return ConstantAggregateZero::get(Ty);
1530 if (isUndef)
1531 return UndefValue::get(Ty);
1532 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001533}
1534
Reid Spencerd84d35b2007-02-15 02:26:10 +00001535Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001536 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001537 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001538}
1539
1540// destroyConstant - Remove the constant from the constant table...
1541//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001542void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001543 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001544 destroyConstantImpl();
1545}
1546
Dan Gohman30978072007-05-24 14:36:04 +00001547/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001548/// is set to all ones.
1549/// @returns true iff this constant's emements are all set to all ones.
1550/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001551bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001552 // Check out first element.
1553 const Constant *Elt = getOperand(0);
1554 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1555 if (!CI || !CI->isAllOnesValue()) return false;
1556 // Then make sure all remaining elements point to the same value.
1557 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1558 if (getOperand(I) != Elt) return false;
1559 }
1560 return true;
1561}
1562
Dan Gohman07159202007-10-17 17:51:30 +00001563/// getSplatValue - If this is a splat constant, where all of the
1564/// elements have the same value, return that value. Otherwise return null.
1565Constant *ConstantVector::getSplatValue() {
1566 // Check out first element.
1567 Constant *Elt = getOperand(0);
1568 // Then make sure all remaining elements point to the same value.
1569 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1570 if (getOperand(I) != Elt) return 0;
1571 return Elt;
1572}
1573
Chris Lattner3462ae32001-12-03 22:26:30 +00001574//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001575//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001576
Chris Lattner189d19f2003-11-21 20:23:48 +00001577namespace llvm {
1578 // ConstantPointerNull does not take extra "value" argument...
1579 template<class ValType>
1580 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1581 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1582 return new ConstantPointerNull(Ty);
1583 }
1584 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001585
Chris Lattner189d19f2003-11-21 20:23:48 +00001586 template<>
1587 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1588 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1589 // Make everyone now use a constant of the new type...
1590 Constant *New = ConstantPointerNull::get(NewTy);
1591 assert(New != OldC && "Didn't replace constant??");
1592 OldC->uncheckedReplaceAllUsesWith(New);
1593 OldC->destroyConstant(); // This constant is now dead, destroy it.
1594 }
1595 };
1596}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001597
Chris Lattner69edc982006-09-28 00:35:06 +00001598static ManagedStatic<ValueMap<char, PointerType,
1599 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001600
Chris Lattner3e650af2004-08-04 04:48:01 +00001601static char getValType(ConstantPointerNull *) {
1602 return 0;
1603}
1604
1605
Chris Lattner3462ae32001-12-03 22:26:30 +00001606ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001607 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001608}
1609
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001610// destroyConstant - Remove the constant from the constant table...
1611//
1612void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001613 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001614 destroyConstantImpl();
1615}
1616
1617
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001618//---- UndefValue::get() implementation...
1619//
1620
1621namespace llvm {
1622 // UndefValue does not take extra "value" argument...
1623 template<class ValType>
1624 struct ConstantCreator<UndefValue, Type, ValType> {
1625 static UndefValue *create(const Type *Ty, const ValType &V) {
1626 return new UndefValue(Ty);
1627 }
1628 };
1629
1630 template<>
1631 struct ConvertConstantType<UndefValue, Type> {
1632 static void convert(UndefValue *OldC, const Type *NewTy) {
1633 // Make everyone now use a constant of the new type.
1634 Constant *New = UndefValue::get(NewTy);
1635 assert(New != OldC && "Didn't replace constant??");
1636 OldC->uncheckedReplaceAllUsesWith(New);
1637 OldC->destroyConstant(); // This constant is now dead, destroy it.
1638 }
1639 };
1640}
1641
Chris Lattner69edc982006-09-28 00:35:06 +00001642static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001643
1644static char getValType(UndefValue *) {
1645 return 0;
1646}
1647
1648
1649UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001650 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001651}
1652
1653// destroyConstant - Remove the constant from the constant table.
1654//
1655void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001656 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001657 destroyConstantImpl();
1658}
1659
1660
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001661//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001662//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001663
Dan Gohmand78c4002008-05-13 00:00:25 +00001664namespace {
1665
Reid Spenceree3c9912006-12-04 05:19:50 +00001666struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001667 typedef SmallVector<unsigned, 4> IndexList;
1668
1669 ExprMapKeyType(unsigned opc,
1670 const std::vector<Constant*> &ops,
1671 unsigned short pred = 0,
1672 const IndexList &inds = IndexList())
1673 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001674 uint16_t opcode;
1675 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001676 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001677 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001678 bool operator==(const ExprMapKeyType& that) const {
1679 return this->opcode == that.opcode &&
1680 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001681 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001682 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001683 }
1684 bool operator<(const ExprMapKeyType & that) const {
1685 return this->opcode < that.opcode ||
1686 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1687 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001688 this->operands < that.operands) ||
1689 (this->opcode == that.opcode && this->predicate == that.predicate &&
1690 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001691 }
1692
1693 bool operator!=(const ExprMapKeyType& that) const {
1694 return !(*this == that);
1695 }
1696};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001697
Dan Gohmand78c4002008-05-13 00:00:25 +00001698}
1699
Chris Lattner189d19f2003-11-21 20:23:48 +00001700namespace llvm {
1701 template<>
1702 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001703 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1704 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001705 if (Instruction::isCast(V.opcode))
1706 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1707 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001708 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001709 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1710 if (V.opcode == Instruction::Select)
1711 return new SelectConstantExpr(V.operands[0], V.operands[1],
1712 V.operands[2]);
1713 if (V.opcode == Instruction::ExtractElement)
1714 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1715 if (V.opcode == Instruction::InsertElement)
1716 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1717 V.operands[2]);
1718 if (V.opcode == Instruction::ShuffleVector)
1719 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1720 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001721 if (V.opcode == Instruction::InsertValue)
1722 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1723 V.indices, Ty);
1724 if (V.opcode == Instruction::ExtractValue)
1725 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001726 if (V.opcode == Instruction::GetElementPtr) {
1727 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001728 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001729 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001730
Reid Spenceree3c9912006-12-04 05:19:50 +00001731 // The compare instructions are weird. We have to encode the predicate
1732 // value and it is combined with the instruction opcode by multiplying
1733 // the opcode by one hundred. We must decode this to get the predicate.
1734 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001735 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001736 V.operands[0], V.operands[1]);
1737 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001738 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1739 V.operands[0], V.operands[1]);
1740 if (V.opcode == Instruction::VICmp)
1741 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1742 V.operands[0], V.operands[1]);
1743 if (V.opcode == Instruction::VFCmp)
1744 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001745 V.operands[0], V.operands[1]);
1746 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001747 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001748 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001749 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001750
Chris Lattner189d19f2003-11-21 20:23:48 +00001751 template<>
1752 struct ConvertConstantType<ConstantExpr, Type> {
1753 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1754 Constant *New;
1755 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001756 case Instruction::Trunc:
1757 case Instruction::ZExt:
1758 case Instruction::SExt:
1759 case Instruction::FPTrunc:
1760 case Instruction::FPExt:
1761 case Instruction::UIToFP:
1762 case Instruction::SIToFP:
1763 case Instruction::FPToUI:
1764 case Instruction::FPToSI:
1765 case Instruction::PtrToInt:
1766 case Instruction::IntToPtr:
1767 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001768 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1769 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001770 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001771 case Instruction::Select:
1772 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1773 OldC->getOperand(1),
1774 OldC->getOperand(2));
1775 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001776 default:
1777 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001778 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001779 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1780 OldC->getOperand(1));
1781 break;
1782 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001783 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001784 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001785 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1786 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001787 break;
1788 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001789
Chris Lattner189d19f2003-11-21 20:23:48 +00001790 assert(New != OldC && "Didn't replace constant??");
1791 OldC->uncheckedReplaceAllUsesWith(New);
1792 OldC->destroyConstant(); // This constant is now dead, destroy it.
1793 }
1794 };
1795} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001796
1797
Chris Lattner3e650af2004-08-04 04:48:01 +00001798static ExprMapKeyType getValType(ConstantExpr *CE) {
1799 std::vector<Constant*> Operands;
1800 Operands.reserve(CE->getNumOperands());
1801 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1802 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001803 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001804 CE->isCompare() ? CE->getPredicate() : 0,
1805 CE->hasIndices() ?
1806 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001807}
1808
Chris Lattner69edc982006-09-28 00:35:06 +00001809static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1810 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001811
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001812/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001813/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001814static inline Constant *getFoldedCast(
1815 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001816 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817 // Fold a few common cases
1818 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1819 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001820
Vikram S. Adve4c485332002-07-15 18:19:33 +00001821 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001822 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001823 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001824 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001825}
Reid Spencerf37dc652006-12-05 19:14:13 +00001826
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1828 Instruction::CastOps opc = Instruction::CastOps(oc);
1829 assert(Instruction::isCast(opc) && "opcode out of range");
1830 assert(C && Ty && "Null arguments to getCast");
1831 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1832
1833 switch (opc) {
1834 default:
1835 assert(0 && "Invalid cast opcode");
1836 break;
1837 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001838 case Instruction::ZExt: return getZExt(C, Ty);
1839 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1841 case Instruction::FPExt: return getFPExtend(C, Ty);
1842 case Instruction::UIToFP: return getUIToFP(C, Ty);
1843 case Instruction::SIToFP: return getSIToFP(C, Ty);
1844 case Instruction::FPToUI: return getFPToUI(C, Ty);
1845 case Instruction::FPToSI: return getFPToSI(C, Ty);
1846 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1847 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1848 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001849 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001850 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001851}
1852
Reid Spencer5c140882006-12-04 20:17:56 +00001853Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1854 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1855 return getCast(Instruction::BitCast, C, Ty);
1856 return getCast(Instruction::ZExt, C, Ty);
1857}
1858
1859Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1860 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1861 return getCast(Instruction::BitCast, C, Ty);
1862 return getCast(Instruction::SExt, C, Ty);
1863}
1864
1865Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1866 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1867 return getCast(Instruction::BitCast, C, Ty);
1868 return getCast(Instruction::Trunc, C, Ty);
1869}
1870
Reid Spencerbc245a02006-12-05 03:25:26 +00001871Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1872 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001873 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001874
Chris Lattner03c49532007-01-15 02:27:26 +00001875 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001876 return getCast(Instruction::PtrToInt, S, Ty);
1877 return getCast(Instruction::BitCast, S, Ty);
1878}
1879
Reid Spencer56521c42006-12-12 00:51:07 +00001880Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1881 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001882 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001883 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1884 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1885 Instruction::CastOps opcode =
1886 (SrcBits == DstBits ? Instruction::BitCast :
1887 (SrcBits > DstBits ? Instruction::Trunc :
1888 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1889 return getCast(opcode, C, Ty);
1890}
1891
1892Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1893 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1894 "Invalid cast");
1895 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1896 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001897 if (SrcBits == DstBits)
1898 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001899 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001900 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001901 return getCast(opcode, C, Ty);
1902}
1903
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001905 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1906 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001907 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1908 "SrcTy must be larger than DestTy for Trunc!");
1909
1910 return getFoldedCast(Instruction::Trunc, C, Ty);
1911}
1912
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001913Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001914 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1915 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001916 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1917 "SrcTy must be smaller than DestTy for SExt!");
1918
1919 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001920}
1921
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001922Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001923 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1924 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001925 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1926 "SrcTy must be smaller than DestTy for ZExt!");
1927
1928 return getFoldedCast(Instruction::ZExt, C, Ty);
1929}
1930
1931Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1932 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1933 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1934 "This is an illegal floating point truncation!");
1935 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1936}
1937
1938Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1939 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1940 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1941 "This is an illegal floating point extension!");
1942 return getFoldedCast(Instruction::FPExt, C, Ty);
1943}
1944
1945Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001946#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001947 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1948 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001949#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001950 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1951 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1952 "This is an illegal uint to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953 return getFoldedCast(Instruction::UIToFP, C, Ty);
1954}
1955
1956Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001957#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001958 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1959 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001960#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001961 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1962 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 "This is an illegal sint to floating point cast!");
1964 return getFoldedCast(Instruction::SIToFP, C, Ty);
1965}
1966
1967Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001968#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001969 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1970 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001971#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001972 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1973 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1974 "This is an illegal floating point to uint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975 return getFoldedCast(Instruction::FPToUI, C, Ty);
1976}
1977
1978Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001979#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001980 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1981 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001982#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001983 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1984 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1985 "This is an illegal floating point to sint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986 return getFoldedCast(Instruction::FPToSI, C, Ty);
1987}
1988
1989Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1990 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001991 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001992 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1993}
1994
1995Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001996 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1998 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1999}
2000
2001Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
2002 // BitCast implies a no-op cast of type only. No bits change. However, you
2003 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002004#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005 const Type *SrcTy = C->getType();
2006 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002007 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002008
2009 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2010 // or nonptr->ptr). For all the other types, the cast is okay if source and
2011 // destination bit widths are identical.
2012 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2013 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002014#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002015 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002016
2017 // It is common to ask for a bitcast of a value to its own type, handle this
2018 // speedily.
2019 if (C->getType() == DstTy) return C;
2020
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002022}
2023
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00002024Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00002025 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00002026 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
2027 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00002028 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00002029 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00002030}
2031
Chris Lattnerb50d1352003-10-05 00:17:43 +00002032Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00002033 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002034 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002035 assert(Opcode >= Instruction::BinaryOpsBegin &&
2036 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002037 "Invalid opcode in binary constant expression");
2038 assert(C1->getType() == C2->getType() &&
2039 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002040
Reid Spencer542964f2007-01-11 18:21:29 +00002041 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00002042 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
2043 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002044
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002045 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002046 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002047 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002048}
2049
Reid Spencer266e42b2006-12-23 06:05:41 +00002050Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002051 Constant *C1, Constant *C2) {
2052 bool isVectorType = C1->getType()->getTypeID() == Type::VectorTyID;
Reid Spencer266e42b2006-12-23 06:05:41 +00002053 switch (predicate) {
2054 default: assert(0 && "Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002055 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2056 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2057 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2058 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2059 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2060 case CmpInst::FCMP_TRUE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002061 return isVectorType ? getVFCmp(predicate, C1, C2)
2062 : getFCmp(predicate, C1, C2);
Nate Begemanc96e2e42008-07-25 17:35:37 +00002063 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2064 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2065 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2066 case CmpInst::ICMP_SLE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002067 return isVectorType ? getVICmp(predicate, C1, C2)
2068 : getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002069 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002070}
2071
2072Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002073#ifndef NDEBUG
2074 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00002075 case Instruction::Add:
2076 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002077 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002078 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002079 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00002080 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002081 "Tried to create an arithmetic operation on a non-arithmetic type!");
2082 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002083 case Instruction::UDiv:
2084 case Instruction::SDiv:
2085 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002086 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2087 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002088 "Tried to create an arithmetic operation on a non-arithmetic type!");
2089 break;
2090 case Instruction::FDiv:
2091 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002092 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2093 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002094 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2095 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002096 case Instruction::URem:
2097 case Instruction::SRem:
2098 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002099 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2100 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002101 "Tried to create an arithmetic operation on a non-arithmetic type!");
2102 break;
2103 case Instruction::FRem:
2104 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002105 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2106 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00002107 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2108 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002109 case Instruction::And:
2110 case Instruction::Or:
2111 case Instruction::Xor:
2112 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002113 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002114 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002115 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002116 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002117 case Instruction::LShr:
2118 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002119 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002120 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002121 "Tried to create a shift operation on a non-integer type!");
2122 break;
2123 default:
2124 break;
2125 }
2126#endif
2127
Reid Spencera009d0d2006-12-04 21:35:24 +00002128 return getTy(C1->getType(), Opcode, C1, C2);
2129}
2130
Reid Spencer266e42b2006-12-23 06:05:41 +00002131Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002132 Constant *C1, Constant *C2) {
2133 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002134 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002135}
2136
Chris Lattner6e415c02004-03-12 05:54:04 +00002137Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
2138 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002139 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002140
2141 if (ReqTy == V1->getType())
2142 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
2143 return SC; // Fold common cases
2144
2145 std::vector<Constant*> argVec(3, C);
2146 argVec[1] = V1;
2147 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002148 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002149 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002150}
2151
Chris Lattnerb50d1352003-10-05 00:17:43 +00002152Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002153 Value* const *Idxs,
2154 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002155 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2156 Idxs+NumIdx) ==
2157 cast<PointerType>(ReqTy)->getElementType() &&
2158 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002159
Chris Lattner302116a2007-01-31 04:40:28 +00002160 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002161 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002162
Chris Lattnerb50d1352003-10-05 00:17:43 +00002163 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002164 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002165 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002166 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002167 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002168 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002169 for (unsigned i = 0; i != NumIdx; ++i)
2170 ArgVec.push_back(cast<Constant>(Idxs[i]));
2171 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002172 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002173}
2174
Chris Lattner302116a2007-01-31 04:40:28 +00002175Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
2176 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002177 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002178 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002179 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002180 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002181 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
2182 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002183}
2184
Chris Lattner302116a2007-01-31 04:40:28 +00002185Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
2186 unsigned NumIdx) {
2187 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002188}
2189
Chris Lattner302116a2007-01-31 04:40:28 +00002190
Reid Spenceree3c9912006-12-04 05:19:50 +00002191Constant *
2192ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2193 assert(LHS->getType() == RHS->getType());
2194 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2195 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2196
Reid Spencer266e42b2006-12-23 06:05:41 +00002197 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002198 return FC; // Fold a few common cases...
2199
2200 // Look up the constant in the table first to ensure uniqueness
2201 std::vector<Constant*> ArgVec;
2202 ArgVec.push_back(LHS);
2203 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002204 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002205 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002206 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002207}
2208
2209Constant *
2210ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2211 assert(LHS->getType() == RHS->getType());
2212 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2213
Reid Spencer266e42b2006-12-23 06:05:41 +00002214 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002215 return FC; // Fold a few common cases...
2216
2217 // Look up the constant in the table first to ensure uniqueness
2218 std::vector<Constant*> ArgVec;
2219 ArgVec.push_back(LHS);
2220 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002221 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002222 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002223 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002224}
2225
Nate Begemand2195702008-05-12 19:01:56 +00002226Constant *
2227ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
Chris Lattnereab49262008-07-14 05:17:31 +00002228 assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() &&
Nate Begemand2195702008-05-12 19:01:56 +00002229 "Tried to create vicmp operation on non-vector type!");
Nate Begemand2195702008-05-12 19:01:56 +00002230 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2231 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2232
Nate Begemanac7f3d92008-05-12 19:23:22 +00002233 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002234 const Type *EltTy = VTy->getElementType();
2235 unsigned NumElts = VTy->getNumElements();
2236
Chris Lattnereab49262008-07-14 05:17:31 +00002237 // See if we can fold the element-wise comparison of the LHS and RHS.
2238 SmallVector<Constant *, 16> LHSElts, RHSElts;
2239 LHS->getVectorElements(LHSElts);
2240 RHS->getVectorElements(RHSElts);
2241
2242 if (!LHSElts.empty() && !RHSElts.empty()) {
2243 SmallVector<Constant *, 16> Elts;
2244 for (unsigned i = 0; i != NumElts; ++i) {
2245 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2246 RHSElts[i]);
2247 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2248 if (FCI->getZExtValue())
2249 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2250 else
2251 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2252 } else if (FC && isa<UndefValue>(FC)) {
2253 Elts.push_back(UndefValue::get(EltTy));
2254 } else {
2255 break;
2256 }
Nate Begemand2195702008-05-12 19:01:56 +00002257 }
Chris Lattnereab49262008-07-14 05:17:31 +00002258 if (Elts.size() == NumElts)
2259 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002260 }
Nate Begemand2195702008-05-12 19:01:56 +00002261
2262 // Look up the constant in the table first to ensure uniqueness
2263 std::vector<Constant*> ArgVec;
2264 ArgVec.push_back(LHS);
2265 ArgVec.push_back(RHS);
2266 // Get the key type with both the opcode and predicate
2267 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
2268 return ExprConstants->getOrCreate(LHS->getType(), Key);
2269}
2270
2271Constant *
2272ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2273 assert(isa<VectorType>(LHS->getType()) &&
2274 "Tried to create vfcmp operation on non-vector type!");
2275 assert(LHS->getType() == RHS->getType());
2276 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2277
2278 const VectorType *VTy = cast<VectorType>(LHS->getType());
2279 unsigned NumElts = VTy->getNumElements();
2280 const Type *EltTy = VTy->getElementType();
2281 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2282 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2283
Chris Lattnereab49262008-07-14 05:17:31 +00002284 // See if we can fold the element-wise comparison of the LHS and RHS.
2285 SmallVector<Constant *, 16> LHSElts, RHSElts;
2286 LHS->getVectorElements(LHSElts);
2287 RHS->getVectorElements(RHSElts);
2288
2289 if (!LHSElts.empty() && !RHSElts.empty()) {
2290 SmallVector<Constant *, 16> Elts;
2291 for (unsigned i = 0; i != NumElts; ++i) {
2292 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2293 RHSElts[i]);
2294 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2295 if (FCI->getZExtValue())
2296 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2297 else
2298 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2299 } else if (FC && isa<UndefValue>(FC)) {
2300 Elts.push_back(UndefValue::get(REltTy));
2301 } else {
2302 break;
2303 }
Nate Begemand2195702008-05-12 19:01:56 +00002304 }
Chris Lattnereab49262008-07-14 05:17:31 +00002305 if (Elts.size() == NumElts)
2306 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002307 }
Nate Begemand2195702008-05-12 19:01:56 +00002308
2309 // Look up the constant in the table first to ensure uniqueness
2310 std::vector<Constant*> ArgVec;
2311 ArgVec.push_back(LHS);
2312 ArgVec.push_back(RHS);
2313 // Get the key type with both the opcode and predicate
2314 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
2315 return ExprConstants->getOrCreate(ResultTy, Key);
2316}
2317
Robert Bocchino23004482006-01-10 19:05:34 +00002318Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2319 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002320 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2321 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002322 // Look up the constant in the table first to ensure uniqueness
2323 std::vector<Constant*> ArgVec(1, Val);
2324 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002325 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002326 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002327}
2328
2329Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002330 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002331 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002332 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002333 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002334 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002335 Val, Idx);
2336}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002337
Robert Bocchinoca27f032006-01-17 20:07:22 +00002338Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2339 Constant *Elt, Constant *Idx) {
2340 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2341 return FC; // Fold a few common cases...
2342 // Look up the constant in the table first to ensure uniqueness
2343 std::vector<Constant*> ArgVec(1, Val);
2344 ArgVec.push_back(Elt);
2345 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002346 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002347 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002348}
2349
2350Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2351 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002352 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002353 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002354 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002355 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002356 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002357 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002358 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002359}
2360
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002361Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2362 Constant *V2, Constant *Mask) {
2363 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2364 return FC; // Fold a few common cases...
2365 // Look up the constant in the table first to ensure uniqueness
2366 std::vector<Constant*> ArgVec(1, V1);
2367 ArgVec.push_back(V2);
2368 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002369 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002370 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002371}
2372
2373Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2374 Constant *Mask) {
2375 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2376 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002377
2378 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2379 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2380 const Type *ShufTy = VectorType::get(EltTy, NElts);
2381 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002382}
2383
Dan Gohman12fce772008-05-15 19:50:34 +00002384Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2385 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002386 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002387 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2388 Idxs+NumIdx) == Val->getType() &&
2389 "insertvalue indices invalid!");
2390 assert(Agg->getType() == ReqTy &&
2391 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002392 assert(Agg->getType()->isFirstClassType() &&
2393 "Non-first-class type for constant InsertValue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002394 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
2395 assert(FC && "InsertValue constant expr couldn't be folded!");
2396 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002397}
2398
2399Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002400 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002401 assert(Agg->getType()->isFirstClassType() &&
2402 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002403
Dan Gohman0752bff2008-05-23 00:36:11 +00002404 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002405#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002406 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002407 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002408#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002409 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002410 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2411}
2412
2413Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002414 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002415 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2416 Idxs+NumIdx) == ReqTy &&
2417 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002418 assert(Agg->getType()->isFirstClassType() &&
2419 "Non-first-class type for constant extractvalue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002420 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
2421 assert(FC && "ExtractValue constant expr couldn't be folded!");
2422 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002423}
2424
2425Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002426 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002427 assert(Agg->getType()->isFirstClassType() &&
2428 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002429
2430 const Type *ReqTy =
2431 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2432 assert(ReqTy && "extractvalue indices invalid!");
2433 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2434}
2435
Reid Spencer2eadb532007-01-21 00:29:26 +00002436Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002437 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002438 if (PTy->getElementType()->isFloatingPoint()) {
2439 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002440 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002441 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002442 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002443
Dale Johannesen98d3a082007-09-14 22:26:36 +00002444 if (Ty->isFloatingPoint())
2445 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002446
2447 return Constant::getNullValue(Ty);
2448}
2449
Vikram S. Adve4c485332002-07-15 18:19:33 +00002450// destroyConstant - Remove the constant from the constant table...
2451//
2452void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00002453 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002454 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002455}
2456
Chris Lattner3cd8c562002-07-30 18:54:25 +00002457const char *ConstantExpr::getOpcodeName() const {
2458 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002459}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002460
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002461//===----------------------------------------------------------------------===//
2462// replaceUsesOfWithOnConstant implementations
2463
Chris Lattner913849b2007-08-21 00:55:23 +00002464/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2465/// 'From' to be uses of 'To'. This must update the uniquing data structures
2466/// etc.
2467///
2468/// Note that we intentionally replace all uses of From with To here. Consider
2469/// a large array that uses 'From' 1000 times. By handling this case all here,
2470/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2471/// single invocation handles all 1000 uses. Handling them one at a time would
2472/// work, but would be really slow because it would have to unique each updated
2473/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002474void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002475 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002476 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002477 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002478
Jim Laskeyc03caef2006-07-17 17:38:29 +00002479 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002480 Lookup.first.first = getType();
2481 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002482
Chris Lattnerb64419a2005-10-03 22:51:37 +00002483 std::vector<Constant*> &Values = Lookup.first.second;
2484 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002485
Chris Lattner8760ec72005-10-04 01:17:50 +00002486 // Fill values with the modified operands of the constant array. Also,
2487 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002488 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002489 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002490 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002491 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2492 Constant *Val = cast<Constant>(O->get());
2493 if (Val == From) {
2494 Val = ToC;
2495 ++NumUpdated;
2496 }
2497 Values.push_back(Val);
2498 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002499 } else {
2500 isAllZeros = true;
2501 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2502 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002503 if (Val == From) {
2504 Val = ToC;
2505 ++NumUpdated;
2506 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002507 Values.push_back(Val);
2508 if (isAllZeros) isAllZeros = Val->isNullValue();
2509 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002510 }
2511
Chris Lattnerb64419a2005-10-03 22:51:37 +00002512 Constant *Replacement = 0;
2513 if (isAllZeros) {
2514 Replacement = ConstantAggregateZero::get(getType());
2515 } else {
2516 // Check to see if we have this array type already.
2517 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002518 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002519 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002520
2521 if (Exists) {
2522 Replacement = I->second;
2523 } else {
2524 // Okay, the new shape doesn't exist in the system yet. Instead of
2525 // creating a new constant array, inserting it, replaceallusesof'ing the
2526 // old with the new, then deleting the old... just update the current one
2527 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002528 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002529
Chris Lattner913849b2007-08-21 00:55:23 +00002530 // Update to the new value. Optimize for the case when we have a single
2531 // operand that we're changing, but handle bulk updates efficiently.
2532 if (NumUpdated == 1) {
2533 unsigned OperandToUpdate = U-OperandList;
2534 assert(getOperand(OperandToUpdate) == From &&
2535 "ReplaceAllUsesWith broken!");
2536 setOperand(OperandToUpdate, ToC);
2537 } else {
2538 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2539 if (getOperand(i) == From)
2540 setOperand(i, ToC);
2541 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002542 return;
2543 }
2544 }
2545
2546 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002547 assert(Replacement != this && "I didn't contain From!");
2548
Chris Lattner7a1450d2005-10-04 18:13:04 +00002549 // Everyone using this now uses the replacement.
2550 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002551
2552 // Delete the old constant!
2553 destroyConstant();
2554}
2555
2556void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002557 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002558 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002559 Constant *ToC = cast<Constant>(To);
2560
Chris Lattnerdff59112005-10-04 18:47:09 +00002561 unsigned OperandToUpdate = U-OperandList;
2562 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2563
Jim Laskeyc03caef2006-07-17 17:38:29 +00002564 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002565 Lookup.first.first = getType();
2566 Lookup.second = this;
2567 std::vector<Constant*> &Values = Lookup.first.second;
2568 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002569
Chris Lattnerdff59112005-10-04 18:47:09 +00002570
Chris Lattner8760ec72005-10-04 01:17:50 +00002571 // Fill values with the modified operands of the constant struct. Also,
2572 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002573 bool isAllZeros = false;
2574 if (!ToC->isNullValue()) {
2575 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2576 Values.push_back(cast<Constant>(O->get()));
2577 } else {
2578 isAllZeros = true;
2579 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2580 Constant *Val = cast<Constant>(O->get());
2581 Values.push_back(Val);
2582 if (isAllZeros) isAllZeros = Val->isNullValue();
2583 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002584 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002585 Values[OperandToUpdate] = ToC;
2586
Chris Lattner8760ec72005-10-04 01:17:50 +00002587 Constant *Replacement = 0;
2588 if (isAllZeros) {
2589 Replacement = ConstantAggregateZero::get(getType());
2590 } else {
2591 // Check to see if we have this array type already.
2592 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002593 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002594 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002595
2596 if (Exists) {
2597 Replacement = I->second;
2598 } else {
2599 // Okay, the new shape doesn't exist in the system yet. Instead of
2600 // creating a new constant struct, inserting it, replaceallusesof'ing the
2601 // old with the new, then deleting the old... just update the current one
2602 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002603 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002604
Chris Lattnerdff59112005-10-04 18:47:09 +00002605 // Update to the new value.
2606 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002607 return;
2608 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002609 }
2610
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002611 assert(Replacement != this && "I didn't contain From!");
2612
Chris Lattner7a1450d2005-10-04 18:13:04 +00002613 // Everyone using this now uses the replacement.
2614 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002615
2616 // Delete the old constant!
2617 destroyConstant();
2618}
2619
Reid Spencerd84d35b2007-02-15 02:26:10 +00002620void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002621 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002622 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2623
2624 std::vector<Constant*> Values;
2625 Values.reserve(getNumOperands()); // Build replacement array...
2626 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2627 Constant *Val = getOperand(i);
2628 if (Val == From) Val = cast<Constant>(To);
2629 Values.push_back(Val);
2630 }
2631
Reid Spencerd84d35b2007-02-15 02:26:10 +00002632 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002633 assert(Replacement != this && "I didn't contain From!");
2634
Chris Lattner7a1450d2005-10-04 18:13:04 +00002635 // Everyone using this now uses the replacement.
2636 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002637
2638 // Delete the old constant!
2639 destroyConstant();
2640}
2641
2642void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002643 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002644 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2645 Constant *To = cast<Constant>(ToV);
2646
2647 Constant *Replacement = 0;
2648 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002649 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002650 Constant *Pointer = getOperand(0);
2651 Indices.reserve(getNumOperands()-1);
2652 if (Pointer == From) Pointer = To;
2653
2654 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2655 Constant *Val = getOperand(i);
2656 if (Val == From) Val = To;
2657 Indices.push_back(Val);
2658 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002659 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2660 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002661 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002662 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002663 if (Agg == From) Agg = To;
2664
Dan Gohman1ecaf452008-05-31 00:58:22 +00002665 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002666 Replacement = ConstantExpr::getExtractValue(Agg,
2667 &Indices[0], Indices.size());
2668 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002669 Constant *Agg = getOperand(0);
2670 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002671 if (Agg == From) Agg = To;
2672 if (Val == From) Val = To;
2673
Dan Gohman1ecaf452008-05-31 00:58:22 +00002674 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002675 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2676 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002678 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002680 } else if (getOpcode() == Instruction::Select) {
2681 Constant *C1 = getOperand(0);
2682 Constant *C2 = getOperand(1);
2683 Constant *C3 = getOperand(2);
2684 if (C1 == From) C1 = To;
2685 if (C2 == From) C2 = To;
2686 if (C3 == From) C3 = To;
2687 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002688 } else if (getOpcode() == Instruction::ExtractElement) {
2689 Constant *C1 = getOperand(0);
2690 Constant *C2 = getOperand(1);
2691 if (C1 == From) C1 = To;
2692 if (C2 == From) C2 = To;
2693 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002694 } else if (getOpcode() == Instruction::InsertElement) {
2695 Constant *C1 = getOperand(0);
2696 Constant *C2 = getOperand(1);
2697 Constant *C3 = getOperand(1);
2698 if (C1 == From) C1 = To;
2699 if (C2 == From) C2 = To;
2700 if (C3 == From) C3 = To;
2701 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2702 } else if (getOpcode() == Instruction::ShuffleVector) {
2703 Constant *C1 = getOperand(0);
2704 Constant *C2 = getOperand(1);
2705 Constant *C3 = getOperand(2);
2706 if (C1 == From) C1 = To;
2707 if (C2 == From) C2 = To;
2708 if (C3 == From) C3 = To;
2709 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002710 } else if (isCompare()) {
2711 Constant *C1 = getOperand(0);
2712 Constant *C2 = getOperand(1);
2713 if (C1 == From) C1 = To;
2714 if (C2 == From) C2 = To;
2715 if (getOpcode() == Instruction::ICmp)
2716 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002717 else if (getOpcode() == Instruction::FCmp)
Reid Spenceree3c9912006-12-04 05:19:50 +00002718 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002719 else if (getOpcode() == Instruction::VICmp)
2720 Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2);
2721 else {
2722 assert(getOpcode() == Instruction::VFCmp);
2723 Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2);
2724 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002725 } else if (getNumOperands() == 2) {
2726 Constant *C1 = getOperand(0);
2727 Constant *C2 = getOperand(1);
2728 if (C1 == From) C1 = To;
2729 if (C2 == From) C2 = To;
2730 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2731 } else {
2732 assert(0 && "Unknown ConstantExpr type!");
2733 return;
2734 }
2735
2736 assert(Replacement != this && "I didn't contain From!");
2737
Chris Lattner7a1450d2005-10-04 18:13:04 +00002738 // Everyone using this now uses the replacement.
2739 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002740
2741 // Delete the old constant!
2742 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002743}