blob: 9c10e75c8dfa1d82b6663941c76a561127627e91 [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
Evan Chengf9e003b2007-03-08 00:59:12 +000093/// ContaintsRelocations - Return true if the constant value contains
94/// relocations which cannot be resolved at compile time.
95bool Constant::ContainsRelocations() const {
96 if (isa<GlobalValue>(this))
97 return true;
98 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
99 if (getOperand(i)->ContainsRelocations())
100 return true;
101 return false;
102}
103
Chris Lattnerb1585a92002-08-13 17:50:20 +0000104// Static constructor to create a '0' constant of arbitrary type...
105Constant *Constant::getNullValue(const Type *Ty) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000106 static uint64_t zero[2] = {0, 0};
Chris Lattner6b727592004-06-17 18:19:28 +0000107 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000108 case Type::IntegerTyID:
109 return ConstantInt::get(Ty, 0);
110 case Type::FloatTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000111 return ConstantFP::get(APFloat(APInt(32, 0)));
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000112 case Type::DoubleTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000113 return ConstantFP::get(APFloat(APInt(64, 0)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000114 case Type::X86_FP80TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000115 return ConstantFP::get(APFloat(APInt(80, 2, zero)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000116 case Type::FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000117 return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
Dale Johannesen98d3a082007-09-14 22:26:36 +0000118 case Type::PPC_FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000119 return ConstantFP::get(APFloat(APInt(128, 2, zero)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000120 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000121 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000122 case Type::StructTyID:
123 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000124 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000125 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000126 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000127 // Function, Label, or Opaque type?
128 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 return 0;
130 }
131}
132
Chris Lattner72e39582007-06-15 06:10:53 +0000133Constant *Constant::getAllOnesValue(const Type *Ty) {
134 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
135 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
136 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
137}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000138
139// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000140ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000141 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000142 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000143 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000144}
145
Dan Gohman30978072007-05-24 14:36:04 +0000146/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000147/// has all its bits set to true.
148/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000149ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000150 std::vector<Constant*> Elts;
151 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000152 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000153 assert(Elts[0] && "Not a vector integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000154 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000155}
156
157
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000159// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160//===----------------------------------------------------------------------===//
161
Reid Spencerb31bffe2007-02-26 23:54:03 +0000162ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000163 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000164 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165}
166
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000167ConstantInt *ConstantInt::TheTrueVal = 0;
168ConstantInt *ConstantInt::TheFalseVal = 0;
169
170namespace llvm {
171 void CleanupTrueFalse(void *) {
172 ConstantInt::ResetTrueFalse();
173 }
174}
175
176static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
177
178ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
179 assert(TheTrueVal == 0 && TheFalseVal == 0);
180 TheTrueVal = get(Type::Int1Ty, 1);
181 TheFalseVal = get(Type::Int1Ty, 0);
182
183 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
184 TrueFalseCleanup.Register();
185
186 return WhichOne ? TheTrueVal : TheFalseVal;
187}
188
189
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000190namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000191 struct DenseMapAPIntKeyInfo {
192 struct KeyTy {
193 APInt val;
194 const Type* type;
195 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
196 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
197 bool operator==(const KeyTy& that) const {
198 return type == that.type && this->val == that.val;
199 }
200 bool operator!=(const KeyTy& that) const {
201 return !this->operator==(that);
202 }
203 };
204 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
205 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000206 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000207 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000208 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000209 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000210 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
211 return LHS == RHS;
212 }
Dale Johannesena719a602007-08-24 00:56:33 +0000213 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000214 };
215}
216
217
Reid Spencerb31bffe2007-02-26 23:54:03 +0000218typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
219 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000220static ManagedStatic<IntMapTy> IntConstants;
221
Reid Spencer362fb292007-03-19 20:39:08 +0000222ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000223 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencer362fb292007-03-19 20:39:08 +0000224 return get(APInt(ITy->getBitWidth(), V, isSigned));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000225}
226
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000227// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000228// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000229// operator== and operator!= to ensure that the DenseMap doesn't attempt to
230// compare APInt's of different widths, which would violate an APInt class
231// invariant which generates an assertion.
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000232ConstantInt *ConstantInt::get(const APInt& V) {
233 // Get the corresponding integer type for the bit width of the value.
234 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000235 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000236 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Reid Spencerb31bffe2007-02-26 23:54:03 +0000237 ConstantInt *&Slot = (*IntConstants)[Key];
238 // if it exists, return it.
239 if (Slot)
240 return Slot;
241 // otherwise create a new one, insert it, and return it.
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000242 return Slot = new ConstantInt(ITy, V);
243}
244
245//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000246// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000247//===----------------------------------------------------------------------===//
248
Chris Lattner98bd9392008-04-09 06:38:30 +0000249static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
250 if (Ty == Type::FloatTy)
251 return &APFloat::IEEEsingle;
252 if (Ty == Type::DoubleTy)
253 return &APFloat::IEEEdouble;
254 if (Ty == Type::X86_FP80Ty)
255 return &APFloat::x87DoubleExtended;
256 else if (Ty == Type::FP128Ty)
257 return &APFloat::IEEEquad;
258
259 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
260 return &APFloat::PPCDoubleDouble;
261}
262
Dale Johannesend246b2c2007-08-30 00:23:21 +0000263ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
264 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000265 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
266 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267}
268
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000269bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000270 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000271}
272
Dale Johannesen98d3a082007-09-14 22:26:36 +0000273ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
274 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
275 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000276 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000277}
278
Dale Johannesend246b2c2007-08-30 00:23:21 +0000279bool ConstantFP::isExactlyValue(const APFloat& V) const {
280 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000281}
282
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000283namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000284 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000285 struct KeyTy {
286 APFloat val;
287 KeyTy(const APFloat& V) : val(V){}
288 KeyTy(const KeyTy& that) : val(that.val) {}
289 bool operator==(const KeyTy& that) const {
290 return this->val.bitwiseIsEqual(that.val);
291 }
292 bool operator!=(const KeyTy& that) const {
293 return !this->operator==(that);
294 }
295 };
296 static inline KeyTy getEmptyKey() {
297 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000298 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000299 static inline KeyTy getTombstoneKey() {
300 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000301 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000302 static unsigned getHashValue(const KeyTy &Key) {
303 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000304 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000305 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
306 return LHS == RHS;
307 }
Dale Johannesena719a602007-08-24 00:56:33 +0000308 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000309 };
310}
311
312//---- ConstantFP::get() implementation...
313//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000314typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000315 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000316
Dale Johannesena719a602007-08-24 00:56:33 +0000317static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000318
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000319ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000320 DenseMapAPFloatKeyInfo::KeyTy Key(V);
321 ConstantFP *&Slot = (*FPConstants)[Key];
322 if (Slot) return Slot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000323
324 const Type *Ty;
325 if (&V.getSemantics() == &APFloat::IEEEsingle)
326 Ty = Type::FloatTy;
327 else if (&V.getSemantics() == &APFloat::IEEEdouble)
328 Ty = Type::DoubleTy;
329 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
330 Ty = Type::X86_FP80Ty;
331 else if (&V.getSemantics() == &APFloat::IEEEquad)
332 Ty = Type::FP128Ty;
333 else {
334 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format");
335 Ty = Type::PPC_FP128Ty;
336 }
337
Dale Johannesend246b2c2007-08-30 00:23:21 +0000338 return Slot = new ConstantFP(Ty, V);
339}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000340
Chris Lattner98bd9392008-04-09 06:38:30 +0000341/// get() - This returns a constant fp for the specified value in the
342/// specified type. This should only be used for simple constant values like
343/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
344ConstantFP *ConstantFP::get(const Type *Ty, double V) {
345 APFloat FV(V);
346 FV.convert(*TypeToFloatSemantics(Ty), APFloat::rmNearestTiesToEven);
347 return get(FV);
348}
349
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000350//===----------------------------------------------------------------------===//
351// ConstantXXX Classes
352//===----------------------------------------------------------------------===//
353
354
Chris Lattner3462ae32001-12-03 22:26:30 +0000355ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000356 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 : Constant(T, ConstantArrayVal,
358 OperandTraits<ConstantArray>::op_end(this) - V.size(),
359 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000360 assert(V.size() == T->getNumElements() &&
361 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000362 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000363 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
364 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000365 Constant *C = *I;
366 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000367 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000368 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000369 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000370 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000371 }
372}
373
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000374
Chris Lattner3462ae32001-12-03 22:26:30 +0000375ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000376 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000377 : Constant(T, ConstantStructVal,
378 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
379 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000380 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000381 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000382 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000383 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
384 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000385 Constant *C = *I;
386 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000387 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000388 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000389 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000390 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000391 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000392 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000393 }
394}
395
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000396
Reid Spencerd84d35b2007-02-15 02:26:10 +0000397ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000398 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000399 : Constant(T, ConstantVectorVal,
400 OperandTraits<ConstantVector>::op_end(this) - V.size(),
401 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000402 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000403 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
404 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000405 Constant *C = *I;
406 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000407 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000408 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000409 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000410 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000411 }
412}
413
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000414
Gabor Greiff6caff662008-05-10 08:32:32 +0000415namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000416// We declare several classes private to this file, so use an anonymous
417// namespace
418namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000419
Gordon Henriksen14a55692007-12-10 02:14:30 +0000420/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
421/// behind the scenes to implement unary constant exprs.
422class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000423 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000424public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000425 // allocate space for exactly one operand
426 void *operator new(size_t s) {
427 return User::operator new(s, 1);
428 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000429 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000430 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
431 Op<0>() = C;
432 }
433 /// Transparently provide more efficient getOperand methods.
434 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000435};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000436
Gordon Henriksen14a55692007-12-10 02:14:30 +0000437/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
438/// behind the scenes to implement binary constant exprs.
439class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000440 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000441public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000442 // allocate space for exactly two operands
443 void *operator new(size_t s) {
444 return User::operator new(s, 2);
445 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000446 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000447 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000448 Op<0>() = C1;
449 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000450 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000451 /// Transparently provide more efficient getOperand methods.
452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000453};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000454
Gordon Henriksen14a55692007-12-10 02:14:30 +0000455/// SelectConstantExpr - This class is private to Constants.cpp, and is used
456/// behind the scenes to implement select constant exprs.
457class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000458 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000459public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000460 // allocate space for exactly three operands
461 void *operator new(size_t s) {
462 return User::operator new(s, 3);
463 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000464 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000465 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000466 Op<0>() = C1;
467 Op<1>() = C2;
468 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000469 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000470 /// Transparently provide more efficient getOperand methods.
471 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000473
Gordon Henriksen14a55692007-12-10 02:14:30 +0000474/// ExtractElementConstantExpr - This class is private to
475/// Constants.cpp, and is used behind the scenes to implement
476/// extractelement constant exprs.
477class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000478 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000479public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000480 // allocate space for exactly two operands
481 void *operator new(size_t s) {
482 return User::operator new(s, 2);
483 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000484 ExtractElementConstantExpr(Constant *C1, Constant *C2)
485 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000486 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000487 Op<0>() = C1;
488 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000489 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000490 /// Transparently provide more efficient getOperand methods.
491 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000492};
Robert Bocchino23004482006-01-10 19:05:34 +0000493
Gordon Henriksen14a55692007-12-10 02:14:30 +0000494/// InsertElementConstantExpr - This class is private to
495/// Constants.cpp, and is used behind the scenes to implement
496/// insertelement constant exprs.
497class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000498 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000499public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000500 // allocate space for exactly three operands
501 void *operator new(size_t s) {
502 return User::operator new(s, 3);
503 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000504 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
505 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000506 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000507 Op<0>() = C1;
508 Op<1>() = C2;
509 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000510 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 /// Transparently provide more efficient getOperand methods.
512 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000513};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000514
Gordon Henriksen14a55692007-12-10 02:14:30 +0000515/// ShuffleVectorConstantExpr - This class is private to
516/// Constants.cpp, and is used behind the scenes to implement
517/// shufflevector constant exprs.
518class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000519 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000520public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000521 // allocate space for exactly three operands
522 void *operator new(size_t s) {
523 return User::operator new(s, 3);
524 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000525 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
526 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000527 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000528 Op<0>() = C1;
529 Op<1>() = C2;
530 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000531 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000532 /// Transparently provide more efficient getOperand methods.
533 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000534};
535
Dan Gohman12fce772008-05-15 19:50:34 +0000536/// ExtractValueConstantExpr - This class is private to
537/// Constants.cpp, and is used behind the scenes to implement
538/// extractvalue constant exprs.
539class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000540 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000541public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000542 // allocate space for exactly one operand
543 void *operator new(size_t s) {
544 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000545 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000546 ExtractValueConstantExpr(Constant *Agg,
547 const SmallVector<unsigned, 4> &IdxList,
548 const Type *DestTy)
549 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
550 Indices(IdxList) {
551 Op<0>() = Agg;
552 }
553
Dan Gohman7bb04502008-05-31 19:09:08 +0000554 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000555 const SmallVector<unsigned, 4> Indices;
556
Dan Gohman12fce772008-05-15 19:50:34 +0000557 /// Transparently provide more efficient getOperand methods.
558 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
559};
560
561/// InsertValueConstantExpr - This class is private to
562/// Constants.cpp, and is used behind the scenes to implement
563/// insertvalue constant exprs.
564class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000565 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000566public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000567 // allocate space for exactly one operand
568 void *operator new(size_t s) {
569 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000570 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000571 InsertValueConstantExpr(Constant *Agg, Constant *Val,
572 const SmallVector<unsigned, 4> &IdxList,
573 const Type *DestTy)
574 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
575 Indices(IdxList) {
576 Op<0>() = Agg;
577 Op<1>() = Val;
578 }
579
Dan Gohman7bb04502008-05-31 19:09:08 +0000580 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000581 const SmallVector<unsigned, 4> Indices;
582
Dan Gohman12fce772008-05-15 19:50:34 +0000583 /// Transparently provide more efficient getOperand methods.
584 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
585};
586
587
Gordon Henriksen14a55692007-12-10 02:14:30 +0000588/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
589/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000590class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000591 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000592 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000593public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000594 static GetElementPtrConstantExpr *Create(Constant *C,
595 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000596 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000597 return new(IdxList.size() + 1)
598 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000599 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000600 /// Transparently provide more efficient getOperand methods.
601 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000602};
603
604// CompareConstantExpr - This class is private to Constants.cpp, and is used
605// behind the scenes to implement ICmp and FCmp constant expressions. This is
606// needed in order to store the predicate value for these instructions.
607struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000608 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
609 // allocate space for exactly two operands
610 void *operator new(size_t s) {
611 return User::operator new(s, 2);
612 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000613 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000614 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
615 unsigned short pred, Constant* LHS, Constant* RHS)
616 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000617 Op<0>() = LHS;
618 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000619 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000620 /// Transparently provide more efficient getOperand methods.
621 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000622};
623
624} // end anonymous namespace
625
Gabor Greiff6caff662008-05-10 08:32:32 +0000626template <>
627struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
628};
629DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
630
631template <>
632struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
633};
634DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
635
636template <>
637struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
638};
639DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
640
641template <>
642struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
643};
644DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
645
646template <>
647struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
648};
649DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
650
651template <>
652struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
653};
654DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
655
Dan Gohman12fce772008-05-15 19:50:34 +0000656template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000657struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000658};
Dan Gohman12fce772008-05-15 19:50:34 +0000659DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
660
661template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000662struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000663};
Dan Gohman12fce772008-05-15 19:50:34 +0000664DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
665
Gabor Greiff6caff662008-05-10 08:32:32 +0000666template <>
667struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
668};
669
670GetElementPtrConstantExpr::GetElementPtrConstantExpr
671 (Constant *C,
672 const std::vector<Constant*> &IdxList,
673 const Type *DestTy)
674 : ConstantExpr(DestTy, Instruction::GetElementPtr,
675 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
676 - (IdxList.size()+1),
677 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000678 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000679 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000680 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000681}
682
683DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
684
685
686template <>
687struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
688};
689DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
690
691
692} // End llvm namespace
693
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000694
695// Utility function for determining if a ConstantExpr is a CastOp or not. This
696// can't be inline because we don't want to #include Instruction.h into
697// Constant.h
698bool ConstantExpr::isCast() const {
699 return Instruction::isCast(getOpcode());
700}
701
Reid Spenceree3c9912006-12-04 05:19:50 +0000702bool ConstantExpr::isCompare() const {
703 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
704}
705
Dan Gohman1ecaf452008-05-31 00:58:22 +0000706bool ConstantExpr::hasIndices() const {
707 return getOpcode() == Instruction::ExtractValue ||
708 getOpcode() == Instruction::InsertValue;
709}
710
711const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
712 if (const ExtractValueConstantExpr *EVCE =
713 dyn_cast<ExtractValueConstantExpr>(this))
714 return EVCE->Indices;
715 if (const InsertValueConstantExpr *IVCE =
716 dyn_cast<InsertValueConstantExpr>(this))
717 return IVCE->Indices;
718 assert(0 && "ConstantExpr does not have indices!");
719}
720
Chris Lattner817175f2004-03-29 02:37:53 +0000721/// ConstantExpr::get* - Return some common constants without having to
722/// specify the full Instruction::OPCODE identifier.
723///
724Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000725 return get(Instruction::Sub,
726 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
727 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000728}
729Constant *ConstantExpr::getNot(Constant *C) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000730 assert(isa<IntegerType>(C->getType()) && "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000731 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000732 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000733}
734Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
735 return get(Instruction::Add, C1, C2);
736}
737Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
738 return get(Instruction::Sub, C1, C2);
739}
740Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
741 return get(Instruction::Mul, C1, C2);
742}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000743Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
744 return get(Instruction::UDiv, C1, C2);
745}
746Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
747 return get(Instruction::SDiv, C1, C2);
748}
749Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
750 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000751}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000752Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
753 return get(Instruction::URem, C1, C2);
754}
755Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
756 return get(Instruction::SRem, C1, C2);
757}
758Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
759 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000760}
761Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
762 return get(Instruction::And, C1, C2);
763}
764Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
765 return get(Instruction::Or, C1, C2);
766}
767Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
768 return get(Instruction::Xor, C1, C2);
769}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000770unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000771 assert(getOpcode() == Instruction::FCmp ||
772 getOpcode() == Instruction::ICmp ||
773 getOpcode() == Instruction::VFCmp ||
774 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000775 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000776}
Chris Lattner817175f2004-03-29 02:37:53 +0000777Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
778 return get(Instruction::Shl, C1, C2);
779}
Reid Spencerfdff9382006-11-08 06:47:33 +0000780Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
781 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000782}
Reid Spencerfdff9382006-11-08 06:47:33 +0000783Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
784 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000785}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000786
Chris Lattner7c1018a2006-07-14 19:37:40 +0000787/// getWithOperandReplaced - Return a constant expression identical to this
788/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000789Constant *
790ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000791 assert(OpNo < getNumOperands() && "Operand num is out of range!");
792 assert(Op->getType() == getOperand(OpNo)->getType() &&
793 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000794 if (getOperand(OpNo) == Op)
795 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000796
Chris Lattner227816342006-07-14 22:20:01 +0000797 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000798 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000799 case Instruction::Trunc:
800 case Instruction::ZExt:
801 case Instruction::SExt:
802 case Instruction::FPTrunc:
803 case Instruction::FPExt:
804 case Instruction::UIToFP:
805 case Instruction::SIToFP:
806 case Instruction::FPToUI:
807 case Instruction::FPToSI:
808 case Instruction::PtrToInt:
809 case Instruction::IntToPtr:
810 case Instruction::BitCast:
811 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000812 case Instruction::Select:
813 Op0 = (OpNo == 0) ? Op : getOperand(0);
814 Op1 = (OpNo == 1) ? Op : getOperand(1);
815 Op2 = (OpNo == 2) ? Op : getOperand(2);
816 return ConstantExpr::getSelect(Op0, Op1, Op2);
817 case Instruction::InsertElement:
818 Op0 = (OpNo == 0) ? Op : getOperand(0);
819 Op1 = (OpNo == 1) ? Op : getOperand(1);
820 Op2 = (OpNo == 2) ? Op : getOperand(2);
821 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
822 case Instruction::ExtractElement:
823 Op0 = (OpNo == 0) ? Op : getOperand(0);
824 Op1 = (OpNo == 1) ? Op : getOperand(1);
825 return ConstantExpr::getExtractElement(Op0, Op1);
826 case Instruction::ShuffleVector:
827 Op0 = (OpNo == 0) ? Op : getOperand(0);
828 Op1 = (OpNo == 1) ? Op : getOperand(1);
829 Op2 = (OpNo == 2) ? Op : getOperand(2);
830 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Dan Gohman12fce772008-05-15 19:50:34 +0000831 case Instruction::InsertValue: {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000832 const SmallVector<unsigned, 4> Indices = getIndices();
833 Op0 = (OpNo == 0) ? Op : getOperand(0);
834 Op1 = (OpNo == 1) ? Op : getOperand(1);
835 return ConstantExpr::getInsertValue(Op0, Op1,
836 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +0000837 }
838 case Instruction::ExtractValue: {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000839 assert(OpNo == 0 && "ExtractaValue has only one operand!");
840 const SmallVector<unsigned, 4> Indices = getIndices();
841 return
842 ConstantExpr::getExtractValue(Op, &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +0000843 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000844 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000845 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000846 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000847 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000848 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000849 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000850 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000851 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000852 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000853 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000854 default:
855 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000856 Op0 = (OpNo == 0) ? Op : getOperand(0);
857 Op1 = (OpNo == 1) ? Op : getOperand(1);
858 return ConstantExpr::get(getOpcode(), Op0, Op1);
859 }
860}
861
862/// getWithOperands - This returns the current constant expression with the
863/// operands replaced with the specified values. The specified operands must
864/// match count and type with the existing ones.
865Constant *ConstantExpr::
866getWithOperands(const std::vector<Constant*> &Ops) const {
867 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
868 bool AnyChange = false;
869 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
870 assert(Ops[i]->getType() == getOperand(i)->getType() &&
871 "Operand type mismatch!");
872 AnyChange |= Ops[i] != getOperand(i);
873 }
874 if (!AnyChange) // No operands changed, return self.
875 return const_cast<ConstantExpr*>(this);
876
877 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000878 case Instruction::Trunc:
879 case Instruction::ZExt:
880 case Instruction::SExt:
881 case Instruction::FPTrunc:
882 case Instruction::FPExt:
883 case Instruction::UIToFP:
884 case Instruction::SIToFP:
885 case Instruction::FPToUI:
886 case Instruction::FPToSI:
887 case Instruction::PtrToInt:
888 case Instruction::IntToPtr:
889 case Instruction::BitCast:
890 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000891 case Instruction::Select:
892 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
893 case Instruction::InsertElement:
894 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
895 case Instruction::ExtractElement:
896 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
897 case Instruction::ShuffleVector:
898 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +0000899 case Instruction::InsertValue: {
900 const SmallVector<unsigned, 4> Indices = getIndices();
901 return ConstantExpr::getInsertValue(Ops[0], Ops[1],
902 &Indices[0], Indices.size());
903 }
904 case Instruction::ExtractValue: {
905 const SmallVector<unsigned, 4> Indices = getIndices();
906 return ConstantExpr::getExtractValue(Ops[0],
907 &Indices[0], Indices.size());
908 }
Chris Lattnerb5d70302007-02-19 20:01:23 +0000909 case Instruction::GetElementPtr:
910 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000911 case Instruction::ICmp:
912 case Instruction::FCmp:
913 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000914 default:
915 assert(getNumOperands() == 2 && "Must be binary operator?");
916 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000917 }
918}
919
Chris Lattner2f7c9632001-06-06 20:29:01 +0000920
921//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000922// isValueValidForType implementations
923
Reid Spencere7334722006-12-19 01:28:19 +0000924bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000925 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000926 if (Ty == Type::Int1Ty)
927 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000928 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000929 return true; // always true, has to fit in largest type
930 uint64_t Max = (1ll << NumBits) - 1;
931 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000932}
933
Reid Spencere0fc4df2006-10-20 07:07:24 +0000934bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000935 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000936 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000937 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000938 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000939 return true; // always true, has to fit in largest type
940 int64_t Min = -(1ll << (NumBits-1));
941 int64_t Max = (1ll << (NumBits-1)) - 1;
942 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000943}
944
Dale Johannesend246b2c2007-08-30 00:23:21 +0000945bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
946 // convert modifies in place, so make a copy.
947 APFloat Val2 = APFloat(Val);
Chris Lattner6b727592004-06-17 18:19:28 +0000948 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000949 default:
950 return false; // These can't be represented as floating point!
951
Dale Johannesend246b2c2007-08-30 00:23:21 +0000952 // FIXME rounding mode needs to be more flexible
Chris Lattner2f7c9632001-06-06 20:29:01 +0000953 case Type::FloatTyID:
Dale Johannesend246b2c2007-08-30 00:23:21 +0000954 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
955 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
956 APFloat::opOK;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000957 case Type::DoubleTyID:
Dale Johannesend246b2c2007-08-30 00:23:21 +0000958 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
959 &Val2.getSemantics() == &APFloat::IEEEdouble ||
960 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
961 APFloat::opOK;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000962 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000963 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
964 &Val2.getSemantics() == &APFloat::IEEEdouble ||
965 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000966 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000967 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
968 &Val2.getSemantics() == &APFloat::IEEEdouble ||
969 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000970 case Type::PPC_FP128TyID:
971 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
972 &Val2.getSemantics() == &APFloat::IEEEdouble ||
973 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000974 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000975}
Chris Lattner9655e542001-07-20 19:16:02 +0000976
Chris Lattner49d855c2001-09-07 16:46:31 +0000977//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000978// Factory Function Implementation
979
Gabor Greiff6caff662008-05-10 08:32:32 +0000980
981// The number of operands for each ConstantCreator::create method is
982// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000983// ConstantCreator - A class that is used to create constants by
984// ValueMap*. This class should be partially specialized if there is
985// something strange that needs to be done to interface to the ctor for the
986// constant.
987//
Chris Lattner189d19f2003-11-21 20:23:48 +0000988namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000989 template<class ValType>
990 struct ConstantTraits;
991
992 template<typename T, typename Alloc>
993 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
994 static unsigned uses(const std::vector<T, Alloc>& v) {
995 return v.size();
996 }
997 };
998
Chris Lattner189d19f2003-11-21 20:23:48 +0000999 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001000 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001001 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001002 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001003 }
1004 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001005
Chris Lattner189d19f2003-11-21 20:23:48 +00001006 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001007 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001008 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1009 assert(0 && "This type cannot be converted!\n");
1010 abort();
1011 }
1012 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001013
Chris Lattner935aa922005-10-04 17:48:46 +00001014 template<class ValType, class TypeClass, class ConstantClass,
1015 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001016 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001017 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001018 typedef std::pair<const Type*, ValType> MapKey;
1019 typedef std::map<MapKey, Constant *> MapTy;
1020 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1021 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001022 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001023 /// Map - This is the main map from the element descriptor to the Constants.
1024 /// This is the primary way we avoid creating two of the same shape
1025 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001026 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001027
1028 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1029 /// from the constants to their element in Map. This is important for
1030 /// removal of constants from the array, which would otherwise have to scan
1031 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001032 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001033
Jim Laskeyc03caef2006-07-17 17:38:29 +00001034 /// AbstractTypeMap - Map for abstract type constants.
1035 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001036 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +00001037
Chris Lattner98fa07b2003-05-23 20:03:32 +00001038 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001039 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001040
1041 /// InsertOrGetItem - Return an iterator for the specified element.
1042 /// If the element exists in the map, the returned iterator points to the
1043 /// entry and Exists=true. If not, the iterator points to the newly
1044 /// inserted entry and returns Exists=false. Newly inserted entries have
1045 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001046 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1047 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001048 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001049 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001050 Exists = !IP.second;
1051 return IP.first;
1052 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001053
Chris Lattner935aa922005-10-04 17:48:46 +00001054private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001055 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001056 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001057 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001058 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1059 IMI->second->second == CP &&
1060 "InverseMap corrupt!");
1061 return IMI->second;
1062 }
1063
Jim Laskeyc03caef2006-07-17 17:38:29 +00001064 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +00001065 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001066 if (I == Map.end() || I->second != CP) {
1067 // FIXME: This should not use a linear scan. If this gets to be a
1068 // performance problem, someone should look at this.
1069 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1070 /* empty */;
1071 }
Chris Lattner935aa922005-10-04 17:48:46 +00001072 return I;
1073 }
1074public:
1075
Chris Lattnerb64419a2005-10-03 22:51:37 +00001076 /// getOrCreate - Return the specified constant from the map, creating it if
1077 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001078 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001079 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001080 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +00001081 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +00001082 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001083 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001084
1085 // If no preexisting value, create one now...
1086 ConstantClass *Result =
1087 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1088
Chris Lattnerb50d1352003-10-05 00:17:43 +00001089 /// FIXME: why does this assert fail when loading 176.gcc?
1090 //assert(Result->getType() == Ty && "Type specified is not correct!");
1091 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1092
Chris Lattner935aa922005-10-04 17:48:46 +00001093 if (HasLargeKey) // Remember the reverse mapping if needed.
1094 InverseMap.insert(std::make_pair(Result, I));
1095
Chris Lattnerb50d1352003-10-05 00:17:43 +00001096 // If the type of the constant is abstract, make sure that an entry exists
1097 // for it in the AbstractTypeMap.
1098 if (Ty->isAbstract()) {
1099 typename AbstractTypeMapTy::iterator TI =
1100 AbstractTypeMap.lower_bound(Ty);
1101
1102 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
1103 // Add ourselves to the ATU list of the type.
1104 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1105
1106 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1107 }
1108 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001109 return Result;
1110 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001111
Chris Lattner98fa07b2003-05-23 20:03:32 +00001112 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001113 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001114 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001115 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001116
Chris Lattner935aa922005-10-04 17:48:46 +00001117 if (HasLargeKey) // Remember the reverse mapping if needed.
1118 InverseMap.erase(CP);
1119
Chris Lattnerb50d1352003-10-05 00:17:43 +00001120 // Now that we found the entry, make sure this isn't the entry that
1121 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001122 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001123 if (Ty->isAbstract()) {
1124 assert(AbstractTypeMap.count(Ty) &&
1125 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001126 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001127 if (ATMEntryIt == I) {
1128 // Yes, we are removing the representative entry for this type.
1129 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001130 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001131
Chris Lattnerb50d1352003-10-05 00:17:43 +00001132 // First check the entry before this one...
1133 if (TmpIt != Map.begin()) {
1134 --TmpIt;
1135 if (TmpIt->first.first != Ty) // Not the same type, move back...
1136 ++TmpIt;
1137 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001138
Chris Lattnerb50d1352003-10-05 00:17:43 +00001139 // If we didn't find the same type, try to move forward...
1140 if (TmpIt == ATMEntryIt) {
1141 ++TmpIt;
1142 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1143 --TmpIt; // No entry afterwards with the same type
1144 }
1145
1146 // If there is another entry in the map of the same abstract type,
1147 // update the AbstractTypeMap entry now.
1148 if (TmpIt != ATMEntryIt) {
1149 ATMEntryIt = TmpIt;
1150 } else {
1151 // Otherwise, we are removing the last instance of this type
1152 // from the table. Remove from the ATM, and from user list.
1153 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1154 AbstractTypeMap.erase(Ty);
1155 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001156 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001157 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001158
Chris Lattnerb50d1352003-10-05 00:17:43 +00001159 Map.erase(I);
1160 }
1161
Chris Lattner3b793c62005-10-04 21:35:50 +00001162
1163 /// MoveConstantToNewSlot - If we are about to change C to be the element
1164 /// specified by I, update our internal data structures to reflect this
1165 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001166 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001167 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001168 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001169 assert(OldI != Map.end() && "Constant not found in constant table!");
1170 assert(OldI->second == C && "Didn't find correct element?");
1171
1172 // If this constant is the representative element for its abstract type,
1173 // update the AbstractTypeMap so that the representative element is I.
1174 if (C->getType()->isAbstract()) {
1175 typename AbstractTypeMapTy::iterator ATI =
1176 AbstractTypeMap.find(C->getType());
1177 assert(ATI != AbstractTypeMap.end() &&
1178 "Abstract type not in AbstractTypeMap?");
1179 if (ATI->second == OldI)
1180 ATI->second = I;
1181 }
1182
1183 // Remove the old entry from the map.
1184 Map.erase(OldI);
1185
1186 // Update the inverse map so that we know that this constant is now
1187 // located at descriptor I.
1188 if (HasLargeKey) {
1189 assert(I->second == C && "Bad inversemap entry!");
1190 InverseMap[C] = I;
1191 }
1192 }
1193
Chris Lattnerb50d1352003-10-05 00:17:43 +00001194 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001195 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001196 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001197
1198 assert(I != AbstractTypeMap.end() &&
1199 "Abstract type not in AbstractTypeMap?");
1200
1201 // Convert a constant at a time until the last one is gone. The last one
1202 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1203 // eliminated eventually.
1204 do {
1205 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001206 TypeClass>::convert(
1207 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001208 cast<TypeClass>(NewTy));
1209
Jim Laskeyc03caef2006-07-17 17:38:29 +00001210 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001211 } while (I != AbstractTypeMap.end());
1212 }
1213
1214 // If the type became concrete without being refined to any other existing
1215 // type, we just remove ourselves from the ATU list.
1216 void typeBecameConcrete(const DerivedType *AbsTy) {
1217 AbsTy->removeAbstractTypeUser(this);
1218 }
1219
1220 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001221 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001222 }
1223 };
1224}
1225
Chris Lattnera84df0a22006-09-28 23:36:21 +00001226
Chris Lattner28173502007-02-20 06:11:36 +00001227
Chris Lattner9fba3da2004-02-15 05:53:04 +00001228//---- ConstantAggregateZero::get() implementation...
1229//
1230namespace llvm {
1231 // ConstantAggregateZero does not take extra "value" argument...
1232 template<class ValType>
1233 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1234 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1235 return new ConstantAggregateZero(Ty);
1236 }
1237 };
1238
1239 template<>
1240 struct ConvertConstantType<ConstantAggregateZero, Type> {
1241 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1242 // Make everyone now use a constant of the new type...
1243 Constant *New = ConstantAggregateZero::get(NewTy);
1244 assert(New != OldC && "Didn't replace constant??");
1245 OldC->uncheckedReplaceAllUsesWith(New);
1246 OldC->destroyConstant(); // This constant is now dead, destroy it.
1247 }
1248 };
1249}
1250
Chris Lattner69edc982006-09-28 00:35:06 +00001251static ManagedStatic<ValueMap<char, Type,
1252 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001253
Chris Lattner3e650af2004-08-04 04:48:01 +00001254static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1255
Chris Lattner9fba3da2004-02-15 05:53:04 +00001256Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001257 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001258 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +00001259 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001260}
1261
1262// destroyConstant - Remove the constant from the constant table...
1263//
1264void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001265 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001266 destroyConstantImpl();
1267}
1268
Chris Lattner3462ae32001-12-03 22:26:30 +00001269//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001270//
Chris Lattner189d19f2003-11-21 20:23:48 +00001271namespace llvm {
1272 template<>
1273 struct ConvertConstantType<ConstantArray, ArrayType> {
1274 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1275 // Make everyone now use a constant of the new type...
1276 std::vector<Constant*> C;
1277 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1278 C.push_back(cast<Constant>(OldC->getOperand(i)));
1279 Constant *New = ConstantArray::get(NewTy, C);
1280 assert(New != OldC && "Didn't replace constant??");
1281 OldC->uncheckedReplaceAllUsesWith(New);
1282 OldC->destroyConstant(); // This constant is now dead, destroy it.
1283 }
1284 };
1285}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001286
Chris Lattner3e650af2004-08-04 04:48:01 +00001287static std::vector<Constant*> getValType(ConstantArray *CA) {
1288 std::vector<Constant*> Elements;
1289 Elements.reserve(CA->getNumOperands());
1290 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1291 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1292 return Elements;
1293}
1294
Chris Lattnerb64419a2005-10-03 22:51:37 +00001295typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001296 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001297static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001298
Chris Lattner015e8212004-02-15 04:14:47 +00001299Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001300 const std::vector<Constant*> &V) {
1301 // If this is an all-zero array, return a ConstantAggregateZero object
1302 if (!V.empty()) {
1303 Constant *C = V[0];
1304 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001305 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001306 for (unsigned i = 1, e = V.size(); i != e; ++i)
1307 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001308 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001309 }
1310 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001311}
1312
Chris Lattner98fa07b2003-05-23 20:03:32 +00001313// destroyConstant - Remove the constant from the constant table...
1314//
1315void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001316 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001317 destroyConstantImpl();
1318}
1319
Reid Spencer6f614532006-05-30 08:23:18 +00001320/// ConstantArray::get(const string&) - Return an array that is initialized to
1321/// contain the specified string. If length is zero then a null terminator is
1322/// added to the specified string so that it may be used in a natural way.
1323/// Otherwise, the length parameter specifies how much of the string to use
1324/// and it won't be null terminated.
1325///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001326Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001327 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001328 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001329 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001330
1331 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001332 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001333 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001334 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001335
Reid Spencer8d9336d2006-12-31 05:26:44 +00001336 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001337 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001338}
1339
Reid Spencer2546b762007-01-26 07:37:34 +00001340/// isString - This method returns true if the array is an array of i8, and
1341/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001342bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001343 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001344 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001345 return false;
1346 // Check the elements to make sure they are all integers, not constant
1347 // expressions.
1348 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1349 if (!isa<ConstantInt>(getOperand(i)))
1350 return false;
1351 return true;
1352}
1353
Evan Cheng3763c5b2006-10-26 19:15:05 +00001354/// isCString - This method returns true if the array is a string (see
1355/// isString) and it ends in a null byte \0 and does not contains any other
1356/// null bytes except its terminator.
1357bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001358 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001359 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001360 return false;
1361 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1362 // Last element must be a null.
1363 if (getOperand(getNumOperands()-1) != Zero)
1364 return false;
1365 // Other elements must be non-null integers.
1366 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1367 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001368 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001369 if (getOperand(i) == Zero)
1370 return false;
1371 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001372 return true;
1373}
1374
1375
Reid Spencer2546b762007-01-26 07:37:34 +00001376// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001377// then this method converts the array to an std::string and returns it.
1378// Otherwise, it asserts out.
1379//
1380std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001381 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001382 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001383 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001384 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001385 return Result;
1386}
1387
1388
Chris Lattner3462ae32001-12-03 22:26:30 +00001389//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001390//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001391
Chris Lattner189d19f2003-11-21 20:23:48 +00001392namespace llvm {
1393 template<>
1394 struct ConvertConstantType<ConstantStruct, StructType> {
1395 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1396 // Make everyone now use a constant of the new type...
1397 std::vector<Constant*> C;
1398 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1399 C.push_back(cast<Constant>(OldC->getOperand(i)));
1400 Constant *New = ConstantStruct::get(NewTy, C);
1401 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001402
Chris Lattner189d19f2003-11-21 20:23:48 +00001403 OldC->uncheckedReplaceAllUsesWith(New);
1404 OldC->destroyConstant(); // This constant is now dead, destroy it.
1405 }
1406 };
1407}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001408
Chris Lattner8760ec72005-10-04 01:17:50 +00001409typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001410 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001411static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001412
Chris Lattner3e650af2004-08-04 04:48:01 +00001413static std::vector<Constant*> getValType(ConstantStruct *CS) {
1414 std::vector<Constant*> Elements;
1415 Elements.reserve(CS->getNumOperands());
1416 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1417 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1418 return Elements;
1419}
1420
Chris Lattner015e8212004-02-15 04:14:47 +00001421Constant *ConstantStruct::get(const StructType *Ty,
1422 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001423 // Create a ConstantAggregateZero value if all elements are zeros...
1424 for (unsigned i = 0, e = V.size(); i != e; ++i)
1425 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001426 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001427
1428 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001429}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001430
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001431Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001432 std::vector<const Type*> StructEls;
1433 StructEls.reserve(V.size());
1434 for (unsigned i = 0, e = V.size(); i != e; ++i)
1435 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001436 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001437}
1438
Chris Lattnerd7a73302001-10-13 06:57:33 +00001439// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001440//
Chris Lattner3462ae32001-12-03 22:26:30 +00001441void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001442 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001443 destroyConstantImpl();
1444}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001445
Reid Spencerd84d35b2007-02-15 02:26:10 +00001446//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001447//
1448namespace llvm {
1449 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001450 struct ConvertConstantType<ConstantVector, VectorType> {
1451 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001452 // Make everyone now use a constant of the new type...
1453 std::vector<Constant*> C;
1454 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1455 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001456 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001457 assert(New != OldC && "Didn't replace constant??");
1458 OldC->uncheckedReplaceAllUsesWith(New);
1459 OldC->destroyConstant(); // This constant is now dead, destroy it.
1460 }
1461 };
1462}
1463
Reid Spencerd84d35b2007-02-15 02:26:10 +00001464static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001465 std::vector<Constant*> Elements;
1466 Elements.reserve(CP->getNumOperands());
1467 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1468 Elements.push_back(CP->getOperand(i));
1469 return Elements;
1470}
1471
Reid Spencerd84d35b2007-02-15 02:26:10 +00001472static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001473 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001474
Reid Spencerd84d35b2007-02-15 02:26:10 +00001475Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001476 const std::vector<Constant*> &V) {
Dan Gohman30978072007-05-24 14:36:04 +00001477 // If this is an all-zero vector, return a ConstantAggregateZero object
Brian Gaeke02209042004-08-20 06:00:58 +00001478 if (!V.empty()) {
1479 Constant *C = V[0];
1480 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001481 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001482 for (unsigned i = 1, e = V.size(); i != e; ++i)
1483 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001484 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001485 }
1486 return ConstantAggregateZero::get(Ty);
1487}
1488
Reid Spencerd84d35b2007-02-15 02:26:10 +00001489Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001490 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001491 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001492}
1493
1494// destroyConstant - Remove the constant from the constant table...
1495//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001496void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001497 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001498 destroyConstantImpl();
1499}
1500
Dan Gohman30978072007-05-24 14:36:04 +00001501/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001502/// is set to all ones.
1503/// @returns true iff this constant's emements are all set to all ones.
1504/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001505bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001506 // Check out first element.
1507 const Constant *Elt = getOperand(0);
1508 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1509 if (!CI || !CI->isAllOnesValue()) return false;
1510 // Then make sure all remaining elements point to the same value.
1511 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1512 if (getOperand(I) != Elt) return false;
1513 }
1514 return true;
1515}
1516
Dan Gohman07159202007-10-17 17:51:30 +00001517/// getSplatValue - If this is a splat constant, where all of the
1518/// elements have the same value, return that value. Otherwise return null.
1519Constant *ConstantVector::getSplatValue() {
1520 // Check out first element.
1521 Constant *Elt = getOperand(0);
1522 // Then make sure all remaining elements point to the same value.
1523 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1524 if (getOperand(I) != Elt) return 0;
1525 return Elt;
1526}
1527
Chris Lattner3462ae32001-12-03 22:26:30 +00001528//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001529//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001530
Chris Lattner189d19f2003-11-21 20:23:48 +00001531namespace llvm {
1532 // ConstantPointerNull does not take extra "value" argument...
1533 template<class ValType>
1534 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1535 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1536 return new ConstantPointerNull(Ty);
1537 }
1538 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001539
Chris Lattner189d19f2003-11-21 20:23:48 +00001540 template<>
1541 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1542 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1543 // Make everyone now use a constant of the new type...
1544 Constant *New = ConstantPointerNull::get(NewTy);
1545 assert(New != OldC && "Didn't replace constant??");
1546 OldC->uncheckedReplaceAllUsesWith(New);
1547 OldC->destroyConstant(); // This constant is now dead, destroy it.
1548 }
1549 };
1550}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001551
Chris Lattner69edc982006-09-28 00:35:06 +00001552static ManagedStatic<ValueMap<char, PointerType,
1553 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001554
Chris Lattner3e650af2004-08-04 04:48:01 +00001555static char getValType(ConstantPointerNull *) {
1556 return 0;
1557}
1558
1559
Chris Lattner3462ae32001-12-03 22:26:30 +00001560ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001561 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001562}
1563
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001564// destroyConstant - Remove the constant from the constant table...
1565//
1566void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001567 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001568 destroyConstantImpl();
1569}
1570
1571
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001572//---- UndefValue::get() implementation...
1573//
1574
1575namespace llvm {
1576 // UndefValue does not take extra "value" argument...
1577 template<class ValType>
1578 struct ConstantCreator<UndefValue, Type, ValType> {
1579 static UndefValue *create(const Type *Ty, const ValType &V) {
1580 return new UndefValue(Ty);
1581 }
1582 };
1583
1584 template<>
1585 struct ConvertConstantType<UndefValue, Type> {
1586 static void convert(UndefValue *OldC, const Type *NewTy) {
1587 // Make everyone now use a constant of the new type.
1588 Constant *New = UndefValue::get(NewTy);
1589 assert(New != OldC && "Didn't replace constant??");
1590 OldC->uncheckedReplaceAllUsesWith(New);
1591 OldC->destroyConstant(); // This constant is now dead, destroy it.
1592 }
1593 };
1594}
1595
Chris Lattner69edc982006-09-28 00:35:06 +00001596static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001597
1598static char getValType(UndefValue *) {
1599 return 0;
1600}
1601
1602
1603UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001604 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001605}
1606
1607// destroyConstant - Remove the constant from the constant table.
1608//
1609void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001610 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001611 destroyConstantImpl();
1612}
1613
1614
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001615//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001616//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001617
Dan Gohmand78c4002008-05-13 00:00:25 +00001618namespace {
1619
Reid Spenceree3c9912006-12-04 05:19:50 +00001620struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001621 typedef SmallVector<unsigned, 4> IndexList;
1622
1623 ExprMapKeyType(unsigned opc,
1624 const std::vector<Constant*> &ops,
1625 unsigned short pred = 0,
1626 const IndexList &inds = IndexList())
1627 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001628 uint16_t opcode;
1629 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001630 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001631 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001632 bool operator==(const ExprMapKeyType& that) const {
1633 return this->opcode == that.opcode &&
1634 this->predicate == that.predicate &&
1635 this->operands == that.operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001636 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001637 }
1638 bool operator<(const ExprMapKeyType & that) const {
1639 return this->opcode < that.opcode ||
1640 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1641 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001642 this->operands < that.operands) ||
1643 (this->opcode == that.opcode && this->predicate == that.predicate &&
1644 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001645 }
1646
1647 bool operator!=(const ExprMapKeyType& that) const {
1648 return !(*this == that);
1649 }
1650};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001651
Dan Gohmand78c4002008-05-13 00:00:25 +00001652}
1653
Chris Lattner189d19f2003-11-21 20:23:48 +00001654namespace llvm {
1655 template<>
1656 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001657 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1658 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001659 if (Instruction::isCast(V.opcode))
1660 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1661 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001662 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001663 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1664 if (V.opcode == Instruction::Select)
1665 return new SelectConstantExpr(V.operands[0], V.operands[1],
1666 V.operands[2]);
1667 if (V.opcode == Instruction::ExtractElement)
1668 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1669 if (V.opcode == Instruction::InsertElement)
1670 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1671 V.operands[2]);
1672 if (V.opcode == Instruction::ShuffleVector)
1673 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1674 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001675 if (V.opcode == Instruction::InsertValue)
1676 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1677 V.indices, Ty);
1678 if (V.opcode == Instruction::ExtractValue)
1679 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001680 if (V.opcode == Instruction::GetElementPtr) {
1681 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001682 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001683 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001684
Reid Spenceree3c9912006-12-04 05:19:50 +00001685 // The compare instructions are weird. We have to encode the predicate
1686 // value and it is combined with the instruction opcode by multiplying
1687 // the opcode by one hundred. We must decode this to get the predicate.
1688 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001689 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001690 V.operands[0], V.operands[1]);
1691 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001692 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1693 V.operands[0], V.operands[1]);
1694 if (V.opcode == Instruction::VICmp)
1695 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1696 V.operands[0], V.operands[1]);
1697 if (V.opcode == Instruction::VFCmp)
1698 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001699 V.operands[0], V.operands[1]);
1700 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001701 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001702 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001703 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001704
Chris Lattner189d19f2003-11-21 20:23:48 +00001705 template<>
1706 struct ConvertConstantType<ConstantExpr, Type> {
1707 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1708 Constant *New;
1709 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001710 case Instruction::Trunc:
1711 case Instruction::ZExt:
1712 case Instruction::SExt:
1713 case Instruction::FPTrunc:
1714 case Instruction::FPExt:
1715 case Instruction::UIToFP:
1716 case Instruction::SIToFP:
1717 case Instruction::FPToUI:
1718 case Instruction::FPToSI:
1719 case Instruction::PtrToInt:
1720 case Instruction::IntToPtr:
1721 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001722 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1723 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001724 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001725 case Instruction::Select:
1726 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1727 OldC->getOperand(1),
1728 OldC->getOperand(2));
1729 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001730 default:
1731 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001732 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001733 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1734 OldC->getOperand(1));
1735 break;
1736 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001737 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001738 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001739 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1740 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001741 break;
1742 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001743
Chris Lattner189d19f2003-11-21 20:23:48 +00001744 assert(New != OldC && "Didn't replace constant??");
1745 OldC->uncheckedReplaceAllUsesWith(New);
1746 OldC->destroyConstant(); // This constant is now dead, destroy it.
1747 }
1748 };
1749} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001750
1751
Chris Lattner3e650af2004-08-04 04:48:01 +00001752static ExprMapKeyType getValType(ConstantExpr *CE) {
1753 std::vector<Constant*> Operands;
1754 Operands.reserve(CE->getNumOperands());
1755 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1756 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001757 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001758 CE->isCompare() ? CE->getPredicate() : 0,
1759 CE->hasIndices() ?
1760 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001761}
1762
Chris Lattner69edc982006-09-28 00:35:06 +00001763static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1764 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001765
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001766/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001767/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001768static inline Constant *getFoldedCast(
1769 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001770 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001771 // Fold a few common cases
1772 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1773 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001774
Vikram S. Adve4c485332002-07-15 18:19:33 +00001775 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001776 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001777 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001778 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001779}
Reid Spencerf37dc652006-12-05 19:14:13 +00001780
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001781Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1782 Instruction::CastOps opc = Instruction::CastOps(oc);
1783 assert(Instruction::isCast(opc) && "opcode out of range");
1784 assert(C && Ty && "Null arguments to getCast");
1785 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1786
1787 switch (opc) {
1788 default:
1789 assert(0 && "Invalid cast opcode");
1790 break;
1791 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001792 case Instruction::ZExt: return getZExt(C, Ty);
1793 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001794 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1795 case Instruction::FPExt: return getFPExtend(C, Ty);
1796 case Instruction::UIToFP: return getUIToFP(C, Ty);
1797 case Instruction::SIToFP: return getSIToFP(C, Ty);
1798 case Instruction::FPToUI: return getFPToUI(C, Ty);
1799 case Instruction::FPToSI: return getFPToSI(C, Ty);
1800 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1801 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1802 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001803 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001804 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001805}
1806
Reid Spencer5c140882006-12-04 20:17:56 +00001807Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1808 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1809 return getCast(Instruction::BitCast, C, Ty);
1810 return getCast(Instruction::ZExt, C, Ty);
1811}
1812
1813Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1814 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1815 return getCast(Instruction::BitCast, C, Ty);
1816 return getCast(Instruction::SExt, C, Ty);
1817}
1818
1819Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1820 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1821 return getCast(Instruction::BitCast, C, Ty);
1822 return getCast(Instruction::Trunc, C, Ty);
1823}
1824
Reid Spencerbc245a02006-12-05 03:25:26 +00001825Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1826 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001827 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001828
Chris Lattner03c49532007-01-15 02:27:26 +00001829 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001830 return getCast(Instruction::PtrToInt, S, Ty);
1831 return getCast(Instruction::BitCast, S, Ty);
1832}
1833
Reid Spencer56521c42006-12-12 00:51:07 +00001834Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1835 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001836 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001837 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1838 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1839 Instruction::CastOps opcode =
1840 (SrcBits == DstBits ? Instruction::BitCast :
1841 (SrcBits > DstBits ? Instruction::Trunc :
1842 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1843 return getCast(opcode, C, Ty);
1844}
1845
1846Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1847 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1848 "Invalid cast");
1849 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1850 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001851 if (SrcBits == DstBits)
1852 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001853 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001854 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001855 return getCast(opcode, C, Ty);
1856}
1857
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001859 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1860 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001861 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1862 "SrcTy must be larger than DestTy for Trunc!");
1863
1864 return getFoldedCast(Instruction::Trunc, C, Ty);
1865}
1866
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001867Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001868 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1869 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1871 "SrcTy must be smaller than DestTy for SExt!");
1872
1873 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001874}
1875
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001876Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001877 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1878 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1880 "SrcTy must be smaller than DestTy for ZExt!");
1881
1882 return getFoldedCast(Instruction::ZExt, C, Ty);
1883}
1884
1885Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1886 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1887 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1888 "This is an illegal floating point truncation!");
1889 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1890}
1891
1892Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1893 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1894 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1895 "This is an illegal floating point extension!");
1896 return getFoldedCast(Instruction::FPExt, C, Ty);
1897}
1898
1899Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001900 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1901 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1902 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1903 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1904 "This is an illegal uint to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 return getFoldedCast(Instruction::UIToFP, C, Ty);
1906}
1907
1908Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001909 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1910 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1911 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1912 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001913 "This is an illegal sint to floating point cast!");
1914 return getFoldedCast(Instruction::SIToFP, C, Ty);
1915}
1916
1917Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001918 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1919 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1920 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1921 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1922 "This is an illegal floating point to uint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001923 return getFoldedCast(Instruction::FPToUI, C, Ty);
1924}
1925
1926Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001927 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1928 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1929 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1930 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1931 "This is an illegal floating point to sint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001932 return getFoldedCast(Instruction::FPToSI, C, Ty);
1933}
1934
1935Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1936 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001937 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1939}
1940
1941Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001942 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001943 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1944 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1945}
1946
1947Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1948 // BitCast implies a no-op cast of type only. No bits change. However, you
1949 // can't cast pointers to anything but pointers.
1950 const Type *SrcTy = C->getType();
1951 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001952 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953
1954 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1955 // or nonptr->ptr). For all the other types, the cast is okay if source and
1956 // destination bit widths are identical.
1957 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1958 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001959 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001961}
1962
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001963Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001964 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001965 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1966 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00001967 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001968 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001969}
1970
Chris Lattnerb50d1352003-10-05 00:17:43 +00001971Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001972 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001973 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001974 assert(Opcode >= Instruction::BinaryOpsBegin &&
1975 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001976 "Invalid opcode in binary constant expression");
1977 assert(C1->getType() == C2->getType() &&
1978 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001979
Reid Spencer542964f2007-01-11 18:21:29 +00001980 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001981 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1982 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001983
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001984 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001985 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001986 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001987}
1988
Reid Spencer266e42b2006-12-23 06:05:41 +00001989Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001990 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001991 switch (predicate) {
1992 default: assert(0 && "Invalid CmpInst predicate");
1993 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1994 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1995 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1996 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1997 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1998 case FCmpInst::FCMP_TRUE:
1999 return getFCmp(predicate, C1, C2);
2000 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
2001 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
2002 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
2003 case ICmpInst::ICMP_SLE:
2004 return getICmp(predicate, C1, C2);
2005 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002006}
2007
2008Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002009#ifndef NDEBUG
2010 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00002011 case Instruction::Add:
2012 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002013 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002014 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002015 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00002016 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002017 "Tried to create an arithmetic operation on a non-arithmetic type!");
2018 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002019 case Instruction::UDiv:
2020 case Instruction::SDiv:
2021 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002022 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2023 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002024 "Tried to create an arithmetic operation on a non-arithmetic type!");
2025 break;
2026 case Instruction::FDiv:
2027 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002028 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2029 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002030 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2031 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002032 case Instruction::URem:
2033 case Instruction::SRem:
2034 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002035 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2036 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002037 "Tried to create an arithmetic operation on a non-arithmetic type!");
2038 break;
2039 case Instruction::FRem:
2040 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002041 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2042 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00002043 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2044 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002045 case Instruction::And:
2046 case Instruction::Or:
2047 case Instruction::Xor:
2048 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002049 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002050 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002051 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002052 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002053 case Instruction::LShr:
2054 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002055 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002056 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002057 "Tried to create a shift operation on a non-integer type!");
2058 break;
2059 default:
2060 break;
2061 }
2062#endif
2063
Reid Spencera009d0d2006-12-04 21:35:24 +00002064 return getTy(C1->getType(), Opcode, C1, C2);
2065}
2066
Reid Spencer266e42b2006-12-23 06:05:41 +00002067Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002068 Constant *C1, Constant *C2) {
2069 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002070 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002071}
2072
Chris Lattner6e415c02004-03-12 05:54:04 +00002073Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
2074 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00002075 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00002076 assert(V1->getType() == V2->getType() && "Select value types must match!");
2077 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
2078
2079 if (ReqTy == V1->getType())
2080 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
2081 return SC; // Fold common cases
2082
2083 std::vector<Constant*> argVec(3, C);
2084 argVec[1] = V1;
2085 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002086 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002087 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002088}
2089
Chris Lattnerb50d1352003-10-05 00:17:43 +00002090Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002091 Value* const *Idxs,
2092 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002093 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2094 Idxs+NumIdx) ==
2095 cast<PointerType>(ReqTy)->getElementType() &&
2096 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002097
Chris Lattner302116a2007-01-31 04:40:28 +00002098 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002099 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002100
Chris Lattnerb50d1352003-10-05 00:17:43 +00002101 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002102 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002103 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002104 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002105 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002106 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002107 for (unsigned i = 0; i != NumIdx; ++i)
2108 ArgVec.push_back(cast<Constant>(Idxs[i]));
2109 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002110 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002111}
2112
Chris Lattner302116a2007-01-31 04:40:28 +00002113Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
2114 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002115 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002116 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002117 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002118 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002119 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
2120 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002121}
2122
Chris Lattner302116a2007-01-31 04:40:28 +00002123Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
2124 unsigned NumIdx) {
2125 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002126}
2127
Chris Lattner302116a2007-01-31 04:40:28 +00002128
Reid Spenceree3c9912006-12-04 05:19:50 +00002129Constant *
2130ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2131 assert(LHS->getType() == RHS->getType());
2132 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2133 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2134
Reid Spencer266e42b2006-12-23 06:05:41 +00002135 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002136 return FC; // Fold a few common cases...
2137
2138 // Look up the constant in the table first to ensure uniqueness
2139 std::vector<Constant*> ArgVec;
2140 ArgVec.push_back(LHS);
2141 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002142 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002143 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002144 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002145}
2146
2147Constant *
2148ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2149 assert(LHS->getType() == RHS->getType());
2150 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2151
Reid Spencer266e42b2006-12-23 06:05:41 +00002152 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002153 return FC; // Fold a few common cases...
2154
2155 // Look up the constant in the table first to ensure uniqueness
2156 std::vector<Constant*> ArgVec;
2157 ArgVec.push_back(LHS);
2158 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002159 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002160 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002161 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002162}
2163
Nate Begemand2195702008-05-12 19:01:56 +00002164Constant *
2165ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2166 assert(isa<VectorType>(LHS->getType()) &&
2167 "Tried to create vicmp operation on non-vector type!");
2168 assert(LHS->getType() == RHS->getType());
2169 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2170 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2171
Nate Begemanac7f3d92008-05-12 19:23:22 +00002172 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002173 const Type *EltTy = VTy->getElementType();
2174 unsigned NumElts = VTy->getNumElements();
2175
2176 SmallVector<Constant *, 8> Elts;
2177 for (unsigned i = 0; i != NumElts; ++i) {
2178 Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
2179 RHS->getOperand(i));
2180 if (FC) {
2181 uint64_t Val = cast<ConstantInt>(FC)->getZExtValue();
2182 if (Val != 0ULL)
2183 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2184 else
2185 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2186 }
2187 }
2188 if (Elts.size() == NumElts)
2189 return ConstantVector::get(&Elts[0], Elts.size());
2190
2191 // Look up the constant in the table first to ensure uniqueness
2192 std::vector<Constant*> ArgVec;
2193 ArgVec.push_back(LHS);
2194 ArgVec.push_back(RHS);
2195 // Get the key type with both the opcode and predicate
2196 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
2197 return ExprConstants->getOrCreate(LHS->getType(), Key);
2198}
2199
2200Constant *
2201ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2202 assert(isa<VectorType>(LHS->getType()) &&
2203 "Tried to create vfcmp operation on non-vector type!");
2204 assert(LHS->getType() == RHS->getType());
2205 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2206
2207 const VectorType *VTy = cast<VectorType>(LHS->getType());
2208 unsigned NumElts = VTy->getNumElements();
2209 const Type *EltTy = VTy->getElementType();
2210 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2211 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2212
2213 SmallVector<Constant *, 8> Elts;
2214 for (unsigned i = 0; i != NumElts; ++i) {
2215 Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
2216 RHS->getOperand(i));
2217 if (FC) {
2218 uint64_t Val = cast<ConstantInt>(FC)->getZExtValue();
2219 if (Val != 0ULL)
2220 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2221 else
2222 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2223 }
2224 }
2225 if (Elts.size() == NumElts)
2226 return ConstantVector::get(&Elts[0], Elts.size());
2227
2228 // Look up the constant in the table first to ensure uniqueness
2229 std::vector<Constant*> ArgVec;
2230 ArgVec.push_back(LHS);
2231 ArgVec.push_back(RHS);
2232 // Get the key type with both the opcode and predicate
2233 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
2234 return ExprConstants->getOrCreate(ResultTy, Key);
2235}
2236
Robert Bocchino23004482006-01-10 19:05:34 +00002237Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2238 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002239 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2240 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002241 // Look up the constant in the table first to ensure uniqueness
2242 std::vector<Constant*> ArgVec(1, Val);
2243 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002244 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002245 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002246}
2247
2248Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002249 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002250 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002251 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002252 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002253 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002254 Val, Idx);
2255}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002256
Robert Bocchinoca27f032006-01-17 20:07:22 +00002257Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2258 Constant *Elt, Constant *Idx) {
2259 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2260 return FC; // Fold a few common cases...
2261 // Look up the constant in the table first to ensure uniqueness
2262 std::vector<Constant*> ArgVec(1, Val);
2263 ArgVec.push_back(Elt);
2264 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002265 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002266 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002267}
2268
2269Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2270 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002271 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002272 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002273 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002274 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002275 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002276 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002277 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00002278 Val, Elt, Idx);
2279}
2280
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002281Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2282 Constant *V2, Constant *Mask) {
2283 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2284 return FC; // Fold a few common cases...
2285 // Look up the constant in the table first to ensure uniqueness
2286 std::vector<Constant*> ArgVec(1, V1);
2287 ArgVec.push_back(V2);
2288 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002289 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002290 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002291}
2292
2293Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2294 Constant *Mask) {
2295 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2296 "Invalid shuffle vector constant expr operands!");
2297 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
2298}
2299
Dan Gohman12fce772008-05-15 19:50:34 +00002300Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2301 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002302 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002303 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2304 Idxs+NumIdx) == Val->getType() &&
2305 "insertvalue indices invalid!");
2306 assert(Agg->getType() == ReqTy &&
2307 "insertvalue type invalid!");
2308
Dan Gohman0752bff2008-05-23 00:36:11 +00002309 assert(Agg->getType()->isFirstClassType() &&
2310 "Non-first-class type for constant InsertValue expression");
Dan Gohman12fce772008-05-15 19:50:34 +00002311 // Look up the constant in the table first to ensure uniqueness
2312 std::vector<Constant*> ArgVec;
Dan Gohman12fce772008-05-15 19:50:34 +00002313 ArgVec.push_back(Agg);
2314 ArgVec.push_back(Val);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002315 SmallVector<unsigned, 4> Indices(Idxs, Idxs + NumIdx);
2316 const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, Indices);
Dan Gohman12fce772008-05-15 19:50:34 +00002317 return ExprConstants->getOrCreate(ReqTy, Key);
2318}
2319
2320Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002321 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002322 assert(Agg->getType()->isFirstClassType() &&
2323 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002324
Dan Gohman0752bff2008-05-23 00:36:11 +00002325 const Type *ReqTy = Agg->getType();
2326 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002327 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Dan Gohman0752bff2008-05-23 00:36:11 +00002328 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002329 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2330}
2331
2332Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002333 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002334 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2335 Idxs+NumIdx) == ReqTy &&
2336 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002337 assert(Agg->getType()->isFirstClassType() &&
2338 "Non-first-class type for constant extractvalue expression");
Dan Gohman12fce772008-05-15 19:50:34 +00002339 // Look up the constant in the table first to ensure uniqueness
2340 std::vector<Constant*> ArgVec;
Dan Gohman12fce772008-05-15 19:50:34 +00002341 ArgVec.push_back(Agg);
Dan Gohman7bb04502008-05-31 19:09:08 +00002342 SmallVector<unsigned, 4> Indices(Idxs, Idxs + NumIdx);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002343 const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, Indices);
Dan Gohman12fce772008-05-15 19:50:34 +00002344 return ExprConstants->getOrCreate(ReqTy, Key);
2345}
2346
2347Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002348 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002349 assert(Agg->getType()->isFirstClassType() &&
2350 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002351
2352 const Type *ReqTy =
2353 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2354 assert(ReqTy && "extractvalue indices invalid!");
2355 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2356}
2357
Reid Spencer2eadb532007-01-21 00:29:26 +00002358Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002359 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002360 if (PTy->getElementType()->isFloatingPoint()) {
2361 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002362 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002363 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002364 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002365
Dale Johannesen98d3a082007-09-14 22:26:36 +00002366 if (Ty->isFloatingPoint())
2367 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002368
2369 return Constant::getNullValue(Ty);
2370}
2371
Vikram S. Adve4c485332002-07-15 18:19:33 +00002372// destroyConstant - Remove the constant from the constant table...
2373//
2374void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00002375 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002376 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002377}
2378
Chris Lattner3cd8c562002-07-30 18:54:25 +00002379const char *ConstantExpr::getOpcodeName() const {
2380 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002381}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002382
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002383//===----------------------------------------------------------------------===//
2384// replaceUsesOfWithOnConstant implementations
2385
Chris Lattner913849b2007-08-21 00:55:23 +00002386/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2387/// 'From' to be uses of 'To'. This must update the uniquing data structures
2388/// etc.
2389///
2390/// Note that we intentionally replace all uses of From with To here. Consider
2391/// a large array that uses 'From' 1000 times. By handling this case all here,
2392/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2393/// single invocation handles all 1000 uses. Handling them one at a time would
2394/// work, but would be really slow because it would have to unique each updated
2395/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002396void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002397 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002398 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002399 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002400
Jim Laskeyc03caef2006-07-17 17:38:29 +00002401 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002402 Lookup.first.first = getType();
2403 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002404
Chris Lattnerb64419a2005-10-03 22:51:37 +00002405 std::vector<Constant*> &Values = Lookup.first.second;
2406 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002407
Chris Lattner8760ec72005-10-04 01:17:50 +00002408 // Fill values with the modified operands of the constant array. Also,
2409 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002410 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002411 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002412 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002413 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2414 Constant *Val = cast<Constant>(O->get());
2415 if (Val == From) {
2416 Val = ToC;
2417 ++NumUpdated;
2418 }
2419 Values.push_back(Val);
2420 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002421 } else {
2422 isAllZeros = true;
2423 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2424 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002425 if (Val == From) {
2426 Val = ToC;
2427 ++NumUpdated;
2428 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002429 Values.push_back(Val);
2430 if (isAllZeros) isAllZeros = Val->isNullValue();
2431 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002432 }
2433
Chris Lattnerb64419a2005-10-03 22:51:37 +00002434 Constant *Replacement = 0;
2435 if (isAllZeros) {
2436 Replacement = ConstantAggregateZero::get(getType());
2437 } else {
2438 // Check to see if we have this array type already.
2439 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002440 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002441 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002442
2443 if (Exists) {
2444 Replacement = I->second;
2445 } else {
2446 // Okay, the new shape doesn't exist in the system yet. Instead of
2447 // creating a new constant array, inserting it, replaceallusesof'ing the
2448 // old with the new, then deleting the old... just update the current one
2449 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002450 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002451
Chris Lattner913849b2007-08-21 00:55:23 +00002452 // Update to the new value. Optimize for the case when we have a single
2453 // operand that we're changing, but handle bulk updates efficiently.
2454 if (NumUpdated == 1) {
2455 unsigned OperandToUpdate = U-OperandList;
2456 assert(getOperand(OperandToUpdate) == From &&
2457 "ReplaceAllUsesWith broken!");
2458 setOperand(OperandToUpdate, ToC);
2459 } else {
2460 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2461 if (getOperand(i) == From)
2462 setOperand(i, ToC);
2463 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002464 return;
2465 }
2466 }
2467
2468 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002469 assert(Replacement != this && "I didn't contain From!");
2470
Chris Lattner7a1450d2005-10-04 18:13:04 +00002471 // Everyone using this now uses the replacement.
2472 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002473
2474 // Delete the old constant!
2475 destroyConstant();
2476}
2477
2478void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002479 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002480 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002481 Constant *ToC = cast<Constant>(To);
2482
Chris Lattnerdff59112005-10-04 18:47:09 +00002483 unsigned OperandToUpdate = U-OperandList;
2484 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2485
Jim Laskeyc03caef2006-07-17 17:38:29 +00002486 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002487 Lookup.first.first = getType();
2488 Lookup.second = this;
2489 std::vector<Constant*> &Values = Lookup.first.second;
2490 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002491
Chris Lattnerdff59112005-10-04 18:47:09 +00002492
Chris Lattner8760ec72005-10-04 01:17:50 +00002493 // Fill values with the modified operands of the constant struct. Also,
2494 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002495 bool isAllZeros = false;
2496 if (!ToC->isNullValue()) {
2497 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2498 Values.push_back(cast<Constant>(O->get()));
2499 } else {
2500 isAllZeros = true;
2501 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2502 Constant *Val = cast<Constant>(O->get());
2503 Values.push_back(Val);
2504 if (isAllZeros) isAllZeros = Val->isNullValue();
2505 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002506 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002507 Values[OperandToUpdate] = ToC;
2508
Chris Lattner8760ec72005-10-04 01:17:50 +00002509 Constant *Replacement = 0;
2510 if (isAllZeros) {
2511 Replacement = ConstantAggregateZero::get(getType());
2512 } else {
2513 // Check to see if we have this array type already.
2514 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002515 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002516 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002517
2518 if (Exists) {
2519 Replacement = I->second;
2520 } else {
2521 // Okay, the new shape doesn't exist in the system yet. Instead of
2522 // creating a new constant struct, inserting it, replaceallusesof'ing the
2523 // old with the new, then deleting the old... just update the current one
2524 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002525 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002526
Chris Lattnerdff59112005-10-04 18:47:09 +00002527 // Update to the new value.
2528 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002529 return;
2530 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002531 }
2532
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002533 assert(Replacement != this && "I didn't contain From!");
2534
Chris Lattner7a1450d2005-10-04 18:13:04 +00002535 // Everyone using this now uses the replacement.
2536 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002537
2538 // Delete the old constant!
2539 destroyConstant();
2540}
2541
Reid Spencerd84d35b2007-02-15 02:26:10 +00002542void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002543 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002544 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2545
2546 std::vector<Constant*> Values;
2547 Values.reserve(getNumOperands()); // Build replacement array...
2548 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2549 Constant *Val = getOperand(i);
2550 if (Val == From) Val = cast<Constant>(To);
2551 Values.push_back(Val);
2552 }
2553
Reid Spencerd84d35b2007-02-15 02:26:10 +00002554 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002555 assert(Replacement != this && "I didn't contain From!");
2556
Chris Lattner7a1450d2005-10-04 18:13:04 +00002557 // Everyone using this now uses the replacement.
2558 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002559
2560 // Delete the old constant!
2561 destroyConstant();
2562}
2563
2564void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002565 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002566 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2567 Constant *To = cast<Constant>(ToV);
2568
2569 Constant *Replacement = 0;
2570 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002571 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002572 Constant *Pointer = getOperand(0);
2573 Indices.reserve(getNumOperands()-1);
2574 if (Pointer == From) Pointer = To;
2575
2576 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2577 Constant *Val = getOperand(i);
2578 if (Val == From) Val = To;
2579 Indices.push_back(Val);
2580 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002581 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2582 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002583 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002584 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002585 if (Agg == From) Agg = To;
2586
Dan Gohman1ecaf452008-05-31 00:58:22 +00002587 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002588 Replacement = ConstantExpr::getExtractValue(Agg,
2589 &Indices[0], Indices.size());
2590 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002591 Constant *Agg = getOperand(0);
2592 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002593 if (Agg == From) Agg = To;
2594 if (Val == From) Val = To;
2595
Dan Gohman1ecaf452008-05-31 00:58:22 +00002596 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002597 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2598 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002600 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002602 } else if (getOpcode() == Instruction::Select) {
2603 Constant *C1 = getOperand(0);
2604 Constant *C2 = getOperand(1);
2605 Constant *C3 = getOperand(2);
2606 if (C1 == From) C1 = To;
2607 if (C2 == From) C2 = To;
2608 if (C3 == From) C3 = To;
2609 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002610 } else if (getOpcode() == Instruction::ExtractElement) {
2611 Constant *C1 = getOperand(0);
2612 Constant *C2 = getOperand(1);
2613 if (C1 == From) C1 = To;
2614 if (C2 == From) C2 = To;
2615 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002616 } else if (getOpcode() == Instruction::InsertElement) {
2617 Constant *C1 = getOperand(0);
2618 Constant *C2 = getOperand(1);
2619 Constant *C3 = getOperand(1);
2620 if (C1 == From) C1 = To;
2621 if (C2 == From) C2 = To;
2622 if (C3 == From) C3 = To;
2623 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2624 } else if (getOpcode() == Instruction::ShuffleVector) {
2625 Constant *C1 = getOperand(0);
2626 Constant *C2 = getOperand(1);
2627 Constant *C3 = getOperand(2);
2628 if (C1 == From) C1 = To;
2629 if (C2 == From) C2 = To;
2630 if (C3 == From) C3 = To;
2631 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002632 } else if (isCompare()) {
2633 Constant *C1 = getOperand(0);
2634 Constant *C2 = getOperand(1);
2635 if (C1 == From) C1 = To;
2636 if (C2 == From) C2 = To;
2637 if (getOpcode() == Instruction::ICmp)
2638 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2639 else
2640 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002641 } else if (getNumOperands() == 2) {
2642 Constant *C1 = getOperand(0);
2643 Constant *C2 = getOperand(1);
2644 if (C1 == From) C1 = To;
2645 if (C2 == From) C2 = To;
2646 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2647 } else {
2648 assert(0 && "Unknown ConstantExpr type!");
2649 return;
2650 }
2651
2652 assert(Replacement != this && "I didn't contain From!");
2653
Chris Lattner7a1450d2005-10-04 18:13:04 +00002654 // Everyone using this now uses the replacement.
2655 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002656
2657 // Delete the old constant!
2658 destroyConstant();
2659}
2660
2661
Jim Laskey2698f0d2006-03-08 18:11:07 +00002662/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2663/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002664/// Parameter Chop determines if the result is chopped at the first null
2665/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002666///
Evan Cheng38280c02006-03-10 23:52:03 +00002667std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002668 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2669 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2670 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2671 if (Init->isString()) {
2672 std::string Result = Init->getAsString();
2673 if (Offset < Result.size()) {
2674 // If we are pointing INTO The string, erase the beginning...
2675 Result.erase(Result.begin(), Result.begin()+Offset);
2676
2677 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002678 if (Chop) {
2679 std::string::size_type NullPos = Result.find_first_of((char)0);
2680 if (NullPos != std::string::npos)
2681 Result.erase(Result.begin()+NullPos, Result.end());
2682 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002683 return Result;
2684 }
2685 }
2686 }
Chris Lattner6ab19ed2007-11-01 02:30:35 +00002687 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
2688 if (CE->getOpcode() == Instruction::GetElementPtr) {
2689 // Turn a gep into the specified offset.
2690 if (CE->getNumOperands() == 3 &&
2691 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2692 isa<ConstantInt>(CE->getOperand(2))) {
2693 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
2694 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002695 }
2696 }
2697 }
2698 return "";
2699}