blob: 347cd166115a399a4c045d241eb132ec681ff58a [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000030#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000031#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000032#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000033#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000034#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000035#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000036#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000040// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Andersond830eb82009-06-18 19:10:19 +000043// Becomes a no-op when multithreading is disabled.
44ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000045
Chris Lattner3462ae32001-12-03 22:26:30 +000046void Constant::destroyConstantImpl() {
47 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000049 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000050 // but they don't know that. Because we only find out when the CPV is
51 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000052 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000053 //
54 while (!use_empty()) {
55 Value *V = use_back();
56#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000057 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000058 DOUT << "While deleting: " << *this
59 << "\n\nUse still stuck around after Def is destroyed: "
60 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000061#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000062 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000063 Constant *CV = cast<Constant>(V);
64 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000065
66 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000067 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000068 }
69
70 // Value has no outstanding references it is safe to delete it now...
71 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000072}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattner23dd1f62006-10-20 00:27:06 +000074/// canTrap - Return true if evaluation of this constant could trap. This is
75/// true for things like constant expressions that could divide by zero.
76bool Constant::canTrap() const {
77 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
78 // The only thing that could possibly trap are constant exprs.
79 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
80 if (!CE) return false;
81
82 // ConstantExpr traps if any operands can trap.
83 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
84 if (getOperand(i)->canTrap())
85 return true;
86
87 // Otherwise, only specific operations can trap.
88 switch (CE->getOpcode()) {
89 default:
90 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000091 case Instruction::UDiv:
92 case Instruction::SDiv:
93 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000094 case Instruction::URem:
95 case Instruction::SRem:
96 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000097 // Div and rem can trap if the RHS is not known to be non-zero.
98 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
99 return true;
100 return false;
101 }
102}
103
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000104/// ContainsRelocations - Return true if the constant value contains relocations
105/// which cannot be resolved at compile time. Kind argument is used to filter
106/// only 'interesting' sorts of relocations.
107bool Constant::ContainsRelocations(unsigned Kind) const {
108 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
109 bool isLocal = GV->hasLocalLinkage();
110 if ((Kind & Reloc::Local) && isLocal) {
111 // Global has local linkage and 'local' kind of relocations are
112 // requested
113 return true;
114 }
115
116 if ((Kind & Reloc::Global) && !isLocal) {
117 // Global has non-local linkage and 'global' kind of relocations are
118 // requested
119 return true;
120 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000121
122 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000123 }
124
Evan Chengf9e003b2007-03-08 00:59:12 +0000125 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000126 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000127 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000128
Evan Chengf9e003b2007-03-08 00:59:12 +0000129 return false;
130}
131
Chris Lattner2105d662008-07-10 00:28:11 +0000132/// getVectorElements - This method, which is only valid on constant of vector
133/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000134/// This handles breaking down a vector undef into undef elements, etc. For
135/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000136void Constant::getVectorElements(LLVMContext &Context,
137 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000138 assert(isa<VectorType>(getType()) && "Not a vector constant!");
139
140 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
141 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
142 Elts.push_back(CV->getOperand(i));
143 return;
144 }
145
146 const VectorType *VT = cast<VectorType>(getType());
147 if (isa<ConstantAggregateZero>(this)) {
148 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000149 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000150 return;
151 }
152
Chris Lattnerc5098a22008-07-14 05:10:41 +0000153 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000154 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000155 return;
156 }
157
158 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000159}
160
161
162
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000164// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165//===----------------------------------------------------------------------===//
166
Reid Spencerb31bffe2007-02-26 23:54:03 +0000167ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000168 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000169 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170}
171
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000172//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000173// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000174//===----------------------------------------------------------------------===//
175
Daniel Dunbare974dc32009-07-17 18:33:52 +0000176#ifndef NDEBUG
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000177static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
178 if (Ty == Type::FloatTy)
179 return &APFloat::IEEEsingle;
180 if (Ty == Type::DoubleTy)
181 return &APFloat::IEEEdouble;
182 if (Ty == Type::X86_FP80Ty)
183 return &APFloat::x87DoubleExtended;
184 else if (Ty == Type::FP128Ty)
185 return &APFloat::IEEEquad;
186
187 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
188 return &APFloat::PPCDoubleDouble;
189}
Daniel Dunbare974dc32009-07-17 18:33:52 +0000190#endif
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000191
Dale Johannesend246b2c2007-08-30 00:23:21 +0000192ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
193 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000194 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
195 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196}
197
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000198bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000199 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000200}
201
Dale Johannesend246b2c2007-08-30 00:23:21 +0000202bool ConstantFP::isExactlyValue(const APFloat& V) const {
203 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000204}
205
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000206//===----------------------------------------------------------------------===//
207// ConstantXXX Classes
208//===----------------------------------------------------------------------===//
209
210
Chris Lattner3462ae32001-12-03 22:26:30 +0000211ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000212 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000213 : Constant(T, ConstantArrayVal,
214 OperandTraits<ConstantArray>::op_end(this) - V.size(),
215 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000216 assert(V.size() == T->getNumElements() &&
217 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000218 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000219 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
220 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000221 Constant *C = *I;
222 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000223 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000224 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000225 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000226 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 }
228}
229
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000230
Chris Lattner3462ae32001-12-03 22:26:30 +0000231ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000232 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000233 : Constant(T, ConstantStructVal,
234 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
235 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000236 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000237 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000238 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000239 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
240 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000241 Constant *C = *I;
242 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000243 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000244 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000245 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000246 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000247 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000248 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 }
250}
251
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252
Reid Spencerd84d35b2007-02-15 02:26:10 +0000253ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000254 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 : Constant(T, ConstantVectorVal,
256 OperandTraits<ConstantVector>::op_end(this) - V.size(),
257 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000258 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000259 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
260 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000261 Constant *C = *I;
262 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000263 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000264 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000265 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000266 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000267 }
268}
269
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000270
Gabor Greiff6caff662008-05-10 08:32:32 +0000271namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000272// We declare several classes private to this file, so use an anonymous
273// namespace
274namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000275
Gordon Henriksen14a55692007-12-10 02:14:30 +0000276/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
277/// behind the scenes to implement unary constant exprs.
278class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000279 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000280public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000281 // allocate space for exactly one operand
282 void *operator new(size_t s) {
283 return User::operator new(s, 1);
284 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000285 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000286 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
287 Op<0>() = C;
288 }
289 /// Transparently provide more efficient getOperand methods.
290 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000291};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000292
Gordon Henriksen14a55692007-12-10 02:14:30 +0000293/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
294/// behind the scenes to implement binary constant exprs.
295class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000296 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000297public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000298 // allocate space for exactly two operands
299 void *operator new(size_t s) {
300 return User::operator new(s, 2);
301 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000302 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000303 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000304 Op<0>() = C1;
305 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000306 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000307 /// Transparently provide more efficient getOperand methods.
308 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000309};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000310
Gordon Henriksen14a55692007-12-10 02:14:30 +0000311/// SelectConstantExpr - This class is private to Constants.cpp, and is used
312/// behind the scenes to implement select constant exprs.
313class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000314 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000315public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000316 // allocate space for exactly three operands
317 void *operator new(size_t s) {
318 return User::operator new(s, 3);
319 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000320 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000321 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000322 Op<0>() = C1;
323 Op<1>() = C2;
324 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000325 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000326 /// Transparently provide more efficient getOperand methods.
327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000328};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000329
Gordon Henriksen14a55692007-12-10 02:14:30 +0000330/// ExtractElementConstantExpr - This class is private to
331/// Constants.cpp, and is used behind the scenes to implement
332/// extractelement constant exprs.
333class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000334 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000335public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000336 // allocate space for exactly two operands
337 void *operator new(size_t s) {
338 return User::operator new(s, 2);
339 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000340 ExtractElementConstantExpr(Constant *C1, Constant *C2)
341 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000343 Op<0>() = C1;
344 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000345 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 /// Transparently provide more efficient getOperand methods.
347 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000348};
Robert Bocchino23004482006-01-10 19:05:34 +0000349
Gordon Henriksen14a55692007-12-10 02:14:30 +0000350/// InsertElementConstantExpr - This class is private to
351/// Constants.cpp, and is used behind the scenes to implement
352/// insertelement constant exprs.
353class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000354 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000355public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000356 // allocate space for exactly three operands
357 void *operator new(size_t s) {
358 return User::operator new(s, 3);
359 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000360 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
361 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000362 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000363 Op<0>() = C1;
364 Op<1>() = C2;
365 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000366 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000367 /// Transparently provide more efficient getOperand methods.
368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000369};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000370
Gordon Henriksen14a55692007-12-10 02:14:30 +0000371/// ShuffleVectorConstantExpr - This class is private to
372/// Constants.cpp, and is used behind the scenes to implement
373/// shufflevector constant exprs.
374class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000375 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000376public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000377 // allocate space for exactly three operands
378 void *operator new(size_t s) {
379 return User::operator new(s, 3);
380 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000381 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000382 : ConstantExpr(VectorType::get(
383 cast<VectorType>(C1->getType())->getElementType(),
384 cast<VectorType>(C3->getType())->getNumElements()),
385 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000386 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000387 Op<0>() = C1;
388 Op<1>() = C2;
389 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000390 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000391 /// Transparently provide more efficient getOperand methods.
392 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000393};
394
Dan Gohman12fce772008-05-15 19:50:34 +0000395/// ExtractValueConstantExpr - This class is private to
396/// Constants.cpp, and is used behind the scenes to implement
397/// extractvalue constant exprs.
398class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000399 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000400public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000401 // allocate space for exactly one operand
402 void *operator new(size_t s) {
403 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000404 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000405 ExtractValueConstantExpr(Constant *Agg,
406 const SmallVector<unsigned, 4> &IdxList,
407 const Type *DestTy)
408 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
409 Indices(IdxList) {
410 Op<0>() = Agg;
411 }
412
Dan Gohman7bb04502008-05-31 19:09:08 +0000413 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000414 const SmallVector<unsigned, 4> Indices;
415
Dan Gohman12fce772008-05-15 19:50:34 +0000416 /// Transparently provide more efficient getOperand methods.
417 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
418};
419
420/// InsertValueConstantExpr - This class is private to
421/// Constants.cpp, and is used behind the scenes to implement
422/// insertvalue constant exprs.
423class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000424 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000425public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000426 // allocate space for exactly one operand
427 void *operator new(size_t s) {
428 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000429 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000430 InsertValueConstantExpr(Constant *Agg, Constant *Val,
431 const SmallVector<unsigned, 4> &IdxList,
432 const Type *DestTy)
433 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
434 Indices(IdxList) {
435 Op<0>() = Agg;
436 Op<1>() = Val;
437 }
438
Dan Gohman7bb04502008-05-31 19:09:08 +0000439 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000440 const SmallVector<unsigned, 4> Indices;
441
Dan Gohman12fce772008-05-15 19:50:34 +0000442 /// Transparently provide more efficient getOperand methods.
443 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
444};
445
446
Gordon Henriksen14a55692007-12-10 02:14:30 +0000447/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
448/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000449class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000450 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000451 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000452public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000453 static GetElementPtrConstantExpr *Create(Constant *C,
454 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000455 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000456 return
457 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000458 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000459 /// Transparently provide more efficient getOperand methods.
460 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000461};
462
463// CompareConstantExpr - This class is private to Constants.cpp, and is used
464// behind the scenes to implement ICmp and FCmp constant expressions. This is
465// needed in order to store the predicate value for these instructions.
466struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000467 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
468 // allocate space for exactly two operands
469 void *operator new(size_t s) {
470 return User::operator new(s, 2);
471 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000473 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
474 unsigned short pred, Constant* LHS, Constant* RHS)
475 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000476 Op<0>() = LHS;
477 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000478 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000479 /// Transparently provide more efficient getOperand methods.
480 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000481};
482
483} // end anonymous namespace
484
Gabor Greiff6caff662008-05-10 08:32:32 +0000485template <>
486struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
487};
488DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
489
490template <>
491struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
492};
493DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
494
495template <>
496struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
497};
498DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
499
500template <>
501struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
502};
503DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
504
505template <>
506struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
507};
508DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
509
510template <>
511struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
512};
513DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
514
Dan Gohman12fce772008-05-15 19:50:34 +0000515template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000516struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000517};
Dan Gohman12fce772008-05-15 19:50:34 +0000518DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
519
520template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000521struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000522};
Dan Gohman12fce772008-05-15 19:50:34 +0000523DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
524
Gabor Greiff6caff662008-05-10 08:32:32 +0000525template <>
526struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
527};
528
529GetElementPtrConstantExpr::GetElementPtrConstantExpr
530 (Constant *C,
531 const std::vector<Constant*> &IdxList,
532 const Type *DestTy)
533 : ConstantExpr(DestTy, Instruction::GetElementPtr,
534 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
535 - (IdxList.size()+1),
536 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000537 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000538 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000539 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000540}
541
542DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
543
544
545template <>
546struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
547};
548DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
549
550
551} // End llvm namespace
552
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000553
554// Utility function for determining if a ConstantExpr is a CastOp or not. This
555// can't be inline because we don't want to #include Instruction.h into
556// Constant.h
557bool ConstantExpr::isCast() const {
558 return Instruction::isCast(getOpcode());
559}
560
Reid Spenceree3c9912006-12-04 05:19:50 +0000561bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000562 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000563}
564
Dan Gohman1ecaf452008-05-31 00:58:22 +0000565bool ConstantExpr::hasIndices() const {
566 return getOpcode() == Instruction::ExtractValue ||
567 getOpcode() == Instruction::InsertValue;
568}
569
570const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
571 if (const ExtractValueConstantExpr *EVCE =
572 dyn_cast<ExtractValueConstantExpr>(this))
573 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000574
575 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000576}
577
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000578unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000579 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000580 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000581 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000582}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000583
Chris Lattner7c1018a2006-07-14 19:37:40 +0000584/// getWithOperandReplaced - Return a constant expression identical to this
585/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000586Constant *
587ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000588 assert(OpNo < getNumOperands() && "Operand num is out of range!");
589 assert(Op->getType() == getOperand(OpNo)->getType() &&
590 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000591 if (getOperand(OpNo) == Op)
592 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000593
Chris Lattner227816342006-07-14 22:20:01 +0000594 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000595 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000596 case Instruction::Trunc:
597 case Instruction::ZExt:
598 case Instruction::SExt:
599 case Instruction::FPTrunc:
600 case Instruction::FPExt:
601 case Instruction::UIToFP:
602 case Instruction::SIToFP:
603 case Instruction::FPToUI:
604 case Instruction::FPToSI:
605 case Instruction::PtrToInt:
606 case Instruction::IntToPtr:
607 case Instruction::BitCast:
608 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000609 case Instruction::Select:
610 Op0 = (OpNo == 0) ? Op : getOperand(0);
611 Op1 = (OpNo == 1) ? Op : getOperand(1);
612 Op2 = (OpNo == 2) ? Op : getOperand(2);
613 return ConstantExpr::getSelect(Op0, Op1, Op2);
614 case Instruction::InsertElement:
615 Op0 = (OpNo == 0) ? Op : getOperand(0);
616 Op1 = (OpNo == 1) ? Op : getOperand(1);
617 Op2 = (OpNo == 2) ? Op : getOperand(2);
618 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
619 case Instruction::ExtractElement:
620 Op0 = (OpNo == 0) ? Op : getOperand(0);
621 Op1 = (OpNo == 1) ? Op : getOperand(1);
622 return ConstantExpr::getExtractElement(Op0, Op1);
623 case Instruction::ShuffleVector:
624 Op0 = (OpNo == 0) ? Op : getOperand(0);
625 Op1 = (OpNo == 1) ? Op : getOperand(1);
626 Op2 = (OpNo == 2) ? Op : getOperand(2);
627 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000628 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000629 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000630 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000631 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000632 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000633 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000634 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000635 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000636 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000637 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000638 default:
639 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000640 Op0 = (OpNo == 0) ? Op : getOperand(0);
641 Op1 = (OpNo == 1) ? Op : getOperand(1);
642 return ConstantExpr::get(getOpcode(), Op0, Op1);
643 }
644}
645
646/// getWithOperands - This returns the current constant expression with the
647/// operands replaced with the specified values. The specified operands must
648/// match count and type with the existing ones.
649Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000650getWithOperands(Constant* const *Ops, unsigned NumOps) const {
651 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000652 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000653 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000654 assert(Ops[i]->getType() == getOperand(i)->getType() &&
655 "Operand type mismatch!");
656 AnyChange |= Ops[i] != getOperand(i);
657 }
658 if (!AnyChange) // No operands changed, return self.
659 return const_cast<ConstantExpr*>(this);
660
661 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000662 case Instruction::Trunc:
663 case Instruction::ZExt:
664 case Instruction::SExt:
665 case Instruction::FPTrunc:
666 case Instruction::FPExt:
667 case Instruction::UIToFP:
668 case Instruction::SIToFP:
669 case Instruction::FPToUI:
670 case Instruction::FPToSI:
671 case Instruction::PtrToInt:
672 case Instruction::IntToPtr:
673 case Instruction::BitCast:
674 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000675 case Instruction::Select:
676 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
677 case Instruction::InsertElement:
678 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
679 case Instruction::ExtractElement:
680 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
681 case Instruction::ShuffleVector:
682 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000683 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000684 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000685 case Instruction::ICmp:
686 case Instruction::FCmp:
687 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000688 default:
689 assert(getNumOperands() == 2 && "Must be binary operator?");
690 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000691 }
692}
693
Chris Lattner2f7c9632001-06-06 20:29:01 +0000694
695//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000696// isValueValidForType implementations
697
Reid Spencere7334722006-12-19 01:28:19 +0000698bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000699 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000700 if (Ty == Type::Int1Ty)
701 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000702 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000703 return true; // always true, has to fit in largest type
704 uint64_t Max = (1ll << NumBits) - 1;
705 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000706}
707
Reid Spencere0fc4df2006-10-20 07:07:24 +0000708bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000709 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000710 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000711 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000712 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000713 return true; // always true, has to fit in largest type
714 int64_t Min = -(1ll << (NumBits-1));
715 int64_t Max = (1ll << (NumBits-1)) - 1;
716 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717}
718
Dale Johannesend246b2c2007-08-30 00:23:21 +0000719bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
720 // convert modifies in place, so make a copy.
721 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000722 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000723 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724 default:
725 return false; // These can't be represented as floating point!
726
Dale Johannesend246b2c2007-08-30 00:23:21 +0000727 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000728 case Type::FloatTyID: {
729 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
730 return true;
731 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
732 return !losesInfo;
733 }
734 case Type::DoubleTyID: {
735 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
736 &Val2.getSemantics() == &APFloat::IEEEdouble)
737 return true;
738 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
739 return !losesInfo;
740 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000741 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000742 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
743 &Val2.getSemantics() == &APFloat::IEEEdouble ||
744 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000745 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000746 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
747 &Val2.getSemantics() == &APFloat::IEEEdouble ||
748 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000749 case Type::PPC_FP128TyID:
750 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
751 &Val2.getSemantics() == &APFloat::IEEEdouble ||
752 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000753 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000754}
Chris Lattner9655e542001-07-20 19:16:02 +0000755
Chris Lattner49d855c2001-09-07 16:46:31 +0000756//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000757// Factory Function Implementation
758
Gabor Greiff6caff662008-05-10 08:32:32 +0000759
760// The number of operands for each ConstantCreator::create method is
761// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000762// ConstantCreator - A class that is used to create constants by
763// ValueMap*. This class should be partially specialized if there is
764// something strange that needs to be done to interface to the ctor for the
765// constant.
766//
Chris Lattner189d19f2003-11-21 20:23:48 +0000767namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000768 template<class ValType>
769 struct ConstantTraits;
770
771 template<typename T, typename Alloc>
772 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
773 static unsigned uses(const std::vector<T, Alloc>& v) {
774 return v.size();
775 }
776 };
777
Chris Lattner189d19f2003-11-21 20:23:48 +0000778 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000779 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000780 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000781 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000782 }
783 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000784
Chris Lattner189d19f2003-11-21 20:23:48 +0000785 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000786 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000787 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000788 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000789 }
790 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000791
Chris Lattner935aa922005-10-04 17:48:46 +0000792 template<class ValType, class TypeClass, class ConstantClass,
793 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000794 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000795 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000796 typedef std::pair<const Type*, ValType> MapKey;
797 typedef std::map<MapKey, Constant *> MapTy;
798 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
799 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000800 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000801 /// Map - This is the main map from the element descriptor to the Constants.
802 /// This is the primary way we avoid creating two of the same shape
803 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000804 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000805
806 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
807 /// from the constants to their element in Map. This is important for
808 /// removal of constants from the array, which would otherwise have to scan
809 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000810 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000811
Jim Laskeyc03caef2006-07-17 17:38:29 +0000812 /// AbstractTypeMap - Map for abstract type constants.
813 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000814 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000815
816 /// ValueMapLock - Mutex for this map.
817 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000818
Chris Lattner98fa07b2003-05-23 20:03:32 +0000819 public:
Owen Anderson61794042009-06-17 20:10:08 +0000820 // NOTE: This function is not locked. It is the caller's responsibility
821 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000822 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000823
824 /// InsertOrGetItem - Return an iterator for the specified element.
825 /// If the element exists in the map, the returned iterator points to the
826 /// entry and Exists=true. If not, the iterator points to the newly
827 /// inserted entry and returns Exists=false. Newly inserted entries have
828 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000829 /// NOTE: This function is not locked. It is the caller's responsibility
830 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000831 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
832 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000833 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000834 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000835 Exists = !IP.second;
836 return IP.first;
837 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000838
Chris Lattner935aa922005-10-04 17:48:46 +0000839private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000840 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000841 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000842 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000843 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
844 IMI->second->second == CP &&
845 "InverseMap corrupt!");
846 return IMI->second;
847 }
848
Jim Laskeyc03caef2006-07-17 17:38:29 +0000849 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000850 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
851 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000852 if (I == Map.end() || I->second != CP) {
853 // FIXME: This should not use a linear scan. If this gets to be a
854 // performance problem, someone should look at this.
855 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
856 /* empty */;
857 }
Chris Lattner935aa922005-10-04 17:48:46 +0000858 return I;
859 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000860
861 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
862 typename MapTy::iterator I) {
863 ConstantClass* Result =
864 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
865
866 assert(Result->getType() == Ty && "Type specified is not correct!");
867 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
868
869 if (HasLargeKey) // Remember the reverse mapping if needed.
870 InverseMap.insert(std::make_pair(Result, I));
871
872 // If the type of the constant is abstract, make sure that an entry
873 // exists for it in the AbstractTypeMap.
874 if (Ty->isAbstract()) {
875 typename AbstractTypeMapTy::iterator TI =
876 AbstractTypeMap.find(Ty);
877
878 if (TI == AbstractTypeMap.end()) {
879 // Add ourselves to the ATU list of the type.
880 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
881
882 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
883 }
884 }
885
886 return Result;
887 }
Chris Lattner935aa922005-10-04 17:48:46 +0000888public:
889
Chris Lattnerb64419a2005-10-03 22:51:37 +0000890 /// getOrCreate - Return the specified constant from the map, creating it if
891 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000892 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000893 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +0000894 MapKey Lookup(Ty, V);
895 ConstantClass* Result = 0;
896
897 typename MapTy::iterator I = Map.find(Lookup);
898 // Is it in the map?
899 if (I != Map.end())
900 Result = static_cast<ConstantClass *>(I->second);
901
902 if (!Result) {
903 // If no preexisting value, create one now...
904 Result = Create(Ty, V, I);
905 }
906
907 return Result;
908 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000909
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000910 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000911 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000912 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000913 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000914 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000915
Chris Lattner935aa922005-10-04 17:48:46 +0000916 if (HasLargeKey) // Remember the reverse mapping if needed.
917 InverseMap.erase(CP);
918
Chris Lattnerb50d1352003-10-05 00:17:43 +0000919 // Now that we found the entry, make sure this isn't the entry that
920 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000921 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000922 if (Ty->isAbstract()) {
923 assert(AbstractTypeMap.count(Ty) &&
924 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000925 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000926 if (ATMEntryIt == I) {
927 // Yes, we are removing the representative entry for this type.
928 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000929 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000930
Chris Lattnerb50d1352003-10-05 00:17:43 +0000931 // First check the entry before this one...
932 if (TmpIt != Map.begin()) {
933 --TmpIt;
934 if (TmpIt->first.first != Ty) // Not the same type, move back...
935 ++TmpIt;
936 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000937
Chris Lattnerb50d1352003-10-05 00:17:43 +0000938 // If we didn't find the same type, try to move forward...
939 if (TmpIt == ATMEntryIt) {
940 ++TmpIt;
941 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
942 --TmpIt; // No entry afterwards with the same type
943 }
944
945 // If there is another entry in the map of the same abstract type,
946 // update the AbstractTypeMap entry now.
947 if (TmpIt != ATMEntryIt) {
948 ATMEntryIt = TmpIt;
949 } else {
950 // Otherwise, we are removing the last instance of this type
951 // from the table. Remove from the ATM, and from user list.
952 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
953 AbstractTypeMap.erase(Ty);
954 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000955 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000956 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000957
Chris Lattnerb50d1352003-10-05 00:17:43 +0000958 Map.erase(I);
959 }
960
Chris Lattner3b793c62005-10-04 21:35:50 +0000961
962 /// MoveConstantToNewSlot - If we are about to change C to be the element
963 /// specified by I, update our internal data structures to reflect this
964 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +0000965 /// NOTE: This function is not locked. It is the responsibility of the
966 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000967 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000968 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000969 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000970 assert(OldI != Map.end() && "Constant not found in constant table!");
971 assert(OldI->second == C && "Didn't find correct element?");
972
973 // If this constant is the representative element for its abstract type,
974 // update the AbstractTypeMap so that the representative element is I.
975 if (C->getType()->isAbstract()) {
976 typename AbstractTypeMapTy::iterator ATI =
977 AbstractTypeMap.find(C->getType());
978 assert(ATI != AbstractTypeMap.end() &&
979 "Abstract type not in AbstractTypeMap?");
980 if (ATI->second == OldI)
981 ATI->second = I;
982 }
983
984 // Remove the old entry from the map.
985 Map.erase(OldI);
986
987 // Update the inverse map so that we know that this constant is now
988 // located at descriptor I.
989 if (HasLargeKey) {
990 assert(I->second == C && "Bad inversemap entry!");
991 InverseMap[C] = I;
992 }
993 }
994
Chris Lattnerb50d1352003-10-05 00:17:43 +0000995 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000996 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000997 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000998 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000999
1000 assert(I != AbstractTypeMap.end() &&
1001 "Abstract type not in AbstractTypeMap?");
1002
1003 // Convert a constant at a time until the last one is gone. The last one
1004 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1005 // eliminated eventually.
1006 do {
1007 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001008 TypeClass>::convert(
1009 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001010 cast<TypeClass>(NewTy));
1011
Jim Laskeyc03caef2006-07-17 17:38:29 +00001012 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001013 } while (I != AbstractTypeMap.end());
1014 }
1015
1016 // If the type became concrete without being refined to any other existing
1017 // type, we just remove ourselves from the ATU list.
1018 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001019 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001020 }
1021
1022 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001023 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001024 }
1025 };
1026}
1027
Chris Lattnera84df0a22006-09-28 23:36:21 +00001028
Chris Lattner28173502007-02-20 06:11:36 +00001029
Chris Lattner9fba3da2004-02-15 05:53:04 +00001030//---- ConstantAggregateZero::get() implementation...
1031//
1032namespace llvm {
1033 // ConstantAggregateZero does not take extra "value" argument...
1034 template<class ValType>
1035 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1036 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1037 return new ConstantAggregateZero(Ty);
1038 }
1039 };
1040
1041 template<>
1042 struct ConvertConstantType<ConstantAggregateZero, Type> {
1043 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1044 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001045 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001046 assert(New != OldC && "Didn't replace constant??");
1047 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001048 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001049 }
1050 };
1051}
1052
Chris Lattner69edc982006-09-28 00:35:06 +00001053static ManagedStatic<ValueMap<char, Type,
1054 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001055
Chris Lattner3e650af2004-08-04 04:48:01 +00001056static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1057
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001058ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001059 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001060 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001061
1062 // Implicitly locked.
1063 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001064}
1065
Dan Gohman92b551b2009-03-03 02:55:14 +00001066/// destroyConstant - Remove the constant from the constant table...
1067///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001068void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001069 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001070 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001071 destroyConstantImpl();
1072}
1073
Chris Lattner3462ae32001-12-03 22:26:30 +00001074//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001075//
Chris Lattner189d19f2003-11-21 20:23:48 +00001076namespace llvm {
1077 template<>
1078 struct ConvertConstantType<ConstantArray, ArrayType> {
1079 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1080 // Make everyone now use a constant of the new type...
1081 std::vector<Constant*> C;
1082 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1083 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001084 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001085 assert(New != OldC && "Didn't replace constant??");
1086 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001087 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001088 }
1089 };
1090}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001091
Chris Lattner3e650af2004-08-04 04:48:01 +00001092static std::vector<Constant*> getValType(ConstantArray *CA) {
1093 std::vector<Constant*> Elements;
1094 Elements.reserve(CA->getNumOperands());
1095 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1096 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1097 return Elements;
1098}
1099
Chris Lattnerb64419a2005-10-03 22:51:37 +00001100typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001101 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001102static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001103
Chris Lattner015e8212004-02-15 04:14:47 +00001104Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001105 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001106 // If this is an all-zero array, return a ConstantAggregateZero object
1107 if (!V.empty()) {
1108 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001109 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001110 // Implicitly locked.
1111 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001112 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001113 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001114 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001115 // Implicitly locked.
1116 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001117 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001118 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001119
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001120 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001121}
1122
Dan Gohman92b551b2009-03-03 02:55:14 +00001123/// destroyConstant - Remove the constant from the constant table...
1124///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001125void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001126 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001127 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001128 destroyConstantImpl();
1129}
1130
Reid Spencer2546b762007-01-26 07:37:34 +00001131/// isString - This method returns true if the array is an array of i8, and
1132/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001133bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001134 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001135 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001136 return false;
1137 // Check the elements to make sure they are all integers, not constant
1138 // expressions.
1139 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1140 if (!isa<ConstantInt>(getOperand(i)))
1141 return false;
1142 return true;
1143}
1144
Evan Cheng3763c5b2006-10-26 19:15:05 +00001145/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001146/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001147/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001148bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001149 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001150 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001151 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001152
Evan Chenge974da62006-10-26 21:48:03 +00001153 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001154 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001155 return false;
1156 // Other elements must be non-null integers.
1157 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1158 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001159 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001160 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001161 return false;
1162 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001163 return true;
1164}
1165
1166
Dan Gohman92b551b2009-03-03 02:55:14 +00001167/// getAsString - If the sub-element type of this array is i8
1168/// then this method converts the array to an std::string and returns it.
1169/// Otherwise, it asserts out.
1170///
Chris Lattner81fabb02002-08-26 17:53:56 +00001171std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001172 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001173 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001174 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001175 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001176 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001177 return Result;
1178}
1179
1180
Chris Lattner3462ae32001-12-03 22:26:30 +00001181//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001182//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001183
Chris Lattner189d19f2003-11-21 20:23:48 +00001184namespace llvm {
1185 template<>
1186 struct ConvertConstantType<ConstantStruct, StructType> {
1187 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1188 // Make everyone now use a constant of the new type...
1189 std::vector<Constant*> C;
1190 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1191 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001192 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001193 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001194
Chris Lattner189d19f2003-11-21 20:23:48 +00001195 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001196 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001197 }
1198 };
1199}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001200
Chris Lattner8760ec72005-10-04 01:17:50 +00001201typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001202 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001203static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001204
Chris Lattner3e650af2004-08-04 04:48:01 +00001205static std::vector<Constant*> getValType(ConstantStruct *CS) {
1206 std::vector<Constant*> Elements;
1207 Elements.reserve(CS->getNumOperands());
1208 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1209 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1210 return Elements;
1211}
1212
Chris Lattner015e8212004-02-15 04:14:47 +00001213Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001214 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001215 // Create a ConstantAggregateZero value if all elements are zeros...
1216 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001217 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001218 // Implicitly locked.
1219 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001220
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001221 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001222}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001223
Chris Lattnerd7a73302001-10-13 06:57:33 +00001224// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001225//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001226void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001227 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001228 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001229 destroyConstantImpl();
1230}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001231
Reid Spencerd84d35b2007-02-15 02:26:10 +00001232//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001233//
1234namespace llvm {
1235 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001236 struct ConvertConstantType<ConstantVector, VectorType> {
1237 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001238 // Make everyone now use a constant of the new type...
1239 std::vector<Constant*> C;
1240 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1241 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001242 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001243 assert(New != OldC && "Didn't replace constant??");
1244 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001245 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001246 }
1247 };
1248}
1249
Reid Spencerd84d35b2007-02-15 02:26:10 +00001250static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001251 std::vector<Constant*> Elements;
1252 Elements.reserve(CP->getNumOperands());
1253 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1254 Elements.push_back(CP->getOperand(i));
1255 return Elements;
1256}
1257
Reid Spencerd84d35b2007-02-15 02:26:10 +00001258static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001259 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001260
Reid Spencerd84d35b2007-02-15 02:26:10 +00001261Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001262 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001263 assert(!V.empty() && "Vectors can't be empty");
1264 // If this is an all-undef or alll-zero vector, return a
1265 // ConstantAggregateZero or UndefValue.
1266 Constant *C = V[0];
1267 bool isZero = C->isNullValue();
1268 bool isUndef = isa<UndefValue>(C);
1269
1270 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001271 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001272 if (V[i] != C) {
1273 isZero = isUndef = false;
1274 break;
1275 }
Brian Gaeke02209042004-08-20 06:00:58 +00001276 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001277
1278 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001279 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001280 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001281 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001282
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001283 // Implicitly locked.
1284 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001285}
1286
Brian Gaeke02209042004-08-20 06:00:58 +00001287// destroyConstant - Remove the constant from the constant table...
1288//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001289void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001290 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001291 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001292 destroyConstantImpl();
1293}
1294
Dan Gohman30978072007-05-24 14:36:04 +00001295/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001296/// is set to all ones.
1297/// @returns true iff this constant's emements are all set to all ones.
1298/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001299bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001300 // Check out first element.
1301 const Constant *Elt = getOperand(0);
1302 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1303 if (!CI || !CI->isAllOnesValue()) return false;
1304 // Then make sure all remaining elements point to the same value.
1305 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1306 if (getOperand(I) != Elt) return false;
1307 }
1308 return true;
1309}
1310
Dan Gohman07159202007-10-17 17:51:30 +00001311/// getSplatValue - If this is a splat constant, where all of the
1312/// elements have the same value, return that value. Otherwise return null.
1313Constant *ConstantVector::getSplatValue() {
1314 // Check out first element.
1315 Constant *Elt = getOperand(0);
1316 // Then make sure all remaining elements point to the same value.
1317 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1318 if (getOperand(I) != Elt) return 0;
1319 return Elt;
1320}
1321
Chris Lattner3462ae32001-12-03 22:26:30 +00001322//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001323//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001324
Chris Lattner189d19f2003-11-21 20:23:48 +00001325namespace llvm {
1326 // ConstantPointerNull does not take extra "value" argument...
1327 template<class ValType>
1328 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1329 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1330 return new ConstantPointerNull(Ty);
1331 }
1332 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001333
Chris Lattner189d19f2003-11-21 20:23:48 +00001334 template<>
1335 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1336 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1337 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001338 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001339 assert(New != OldC && "Didn't replace constant??");
1340 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001341 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001342 }
1343 };
1344}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001345
Chris Lattner69edc982006-09-28 00:35:06 +00001346static ManagedStatic<ValueMap<char, PointerType,
1347 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001348
Chris Lattner3e650af2004-08-04 04:48:01 +00001349static char getValType(ConstantPointerNull *) {
1350 return 0;
1351}
1352
1353
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001354ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001355 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001356 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001357}
1358
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001359// destroyConstant - Remove the constant from the constant table...
1360//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001361void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001362 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001363 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001364 destroyConstantImpl();
1365}
1366
1367
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001368//---- UndefValue::get() implementation...
1369//
1370
1371namespace llvm {
1372 // UndefValue does not take extra "value" argument...
1373 template<class ValType>
1374 struct ConstantCreator<UndefValue, Type, ValType> {
1375 static UndefValue *create(const Type *Ty, const ValType &V) {
1376 return new UndefValue(Ty);
1377 }
1378 };
1379
1380 template<>
1381 struct ConvertConstantType<UndefValue, Type> {
1382 static void convert(UndefValue *OldC, const Type *NewTy) {
1383 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001384 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001385 assert(New != OldC && "Didn't replace constant??");
1386 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001387 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001388 }
1389 };
1390}
1391
Chris Lattner69edc982006-09-28 00:35:06 +00001392static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001393
1394static char getValType(UndefValue *) {
1395 return 0;
1396}
1397
1398
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001399UndefValue *UndefValue::get(const Type *Ty) {
1400 // Implicitly locked.
1401 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001402}
1403
1404// destroyConstant - Remove the constant from the constant table.
1405//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001406void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001407 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001408 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001409 destroyConstantImpl();
1410}
1411
Nick Lewycky49f89192009-04-04 07:22:01 +00001412//---- MDString::get() implementation
1413//
1414
1415MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001416 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001417 StrBegin(begin), StrEnd(end) {}
1418
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001419void MDString::destroyConstant() {
Owen Anderson69ab4162009-07-16 22:11:26 +00001420 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001421 destroyConstantImpl();
1422}
1423
1424//---- MDNode::get() implementation
1425//
1426
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001427MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001428 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001429 for (unsigned i = 0; i != NumVals; ++i)
1430 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001431}
1432
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001433void MDNode::Profile(FoldingSetNodeID &ID) const {
1434 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001435 ID.AddPointer(*I);
1436}
1437
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001438void MDNode::destroyConstant() {
Owen Anderson4118dde2009-07-16 23:44:30 +00001439 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001440 destroyConstantImpl();
1441}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001442
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001443//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001444//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001445
Dan Gohmand78c4002008-05-13 00:00:25 +00001446namespace {
1447
Reid Spenceree3c9912006-12-04 05:19:50 +00001448struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001449 typedef SmallVector<unsigned, 4> IndexList;
1450
1451 ExprMapKeyType(unsigned opc,
1452 const std::vector<Constant*> &ops,
1453 unsigned short pred = 0,
1454 const IndexList &inds = IndexList())
1455 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001456 uint16_t opcode;
1457 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001458 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001459 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001460 bool operator==(const ExprMapKeyType& that) const {
1461 return this->opcode == that.opcode &&
1462 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001463 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001464 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001465 }
1466 bool operator<(const ExprMapKeyType & that) const {
1467 return this->opcode < that.opcode ||
1468 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1469 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001470 this->operands < that.operands) ||
1471 (this->opcode == that.opcode && this->predicate == that.predicate &&
1472 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001473 }
1474
1475 bool operator!=(const ExprMapKeyType& that) const {
1476 return !(*this == that);
1477 }
1478};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001479
Dan Gohmand78c4002008-05-13 00:00:25 +00001480}
1481
Chris Lattner189d19f2003-11-21 20:23:48 +00001482namespace llvm {
1483 template<>
1484 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001485 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1486 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001487 if (Instruction::isCast(V.opcode))
1488 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1489 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001490 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001491 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1492 if (V.opcode == Instruction::Select)
1493 return new SelectConstantExpr(V.operands[0], V.operands[1],
1494 V.operands[2]);
1495 if (V.opcode == Instruction::ExtractElement)
1496 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1497 if (V.opcode == Instruction::InsertElement)
1498 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1499 V.operands[2]);
1500 if (V.opcode == Instruction::ShuffleVector)
1501 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1502 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001503 if (V.opcode == Instruction::InsertValue)
1504 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1505 V.indices, Ty);
1506 if (V.opcode == Instruction::ExtractValue)
1507 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001508 if (V.opcode == Instruction::GetElementPtr) {
1509 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001510 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001511 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001512
Reid Spenceree3c9912006-12-04 05:19:50 +00001513 // The compare instructions are weird. We have to encode the predicate
1514 // value and it is combined with the instruction opcode by multiplying
1515 // the opcode by one hundred. We must decode this to get the predicate.
1516 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001517 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001518 V.operands[0], V.operands[1]);
1519 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001520 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1521 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001522 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001523 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001524 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001525 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001526
Chris Lattner189d19f2003-11-21 20:23:48 +00001527 template<>
1528 struct ConvertConstantType<ConstantExpr, Type> {
1529 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1530 Constant *New;
1531 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001532 case Instruction::Trunc:
1533 case Instruction::ZExt:
1534 case Instruction::SExt:
1535 case Instruction::FPTrunc:
1536 case Instruction::FPExt:
1537 case Instruction::UIToFP:
1538 case Instruction::SIToFP:
1539 case Instruction::FPToUI:
1540 case Instruction::FPToSI:
1541 case Instruction::PtrToInt:
1542 case Instruction::IntToPtr:
1543 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001544 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001545 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001546 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001547 case Instruction::Select:
1548 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1549 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001550 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001551 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001552 default:
1553 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001554 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001555 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001556 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001557 break;
1558 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001559 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001560 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001561 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001562 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001563 break;
1564 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001565
Chris Lattner189d19f2003-11-21 20:23:48 +00001566 assert(New != OldC && "Didn't replace constant??");
1567 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001568 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001569 }
1570 };
1571} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001572
1573
Chris Lattner3e650af2004-08-04 04:48:01 +00001574static ExprMapKeyType getValType(ConstantExpr *CE) {
1575 std::vector<Constant*> Operands;
1576 Operands.reserve(CE->getNumOperands());
1577 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1578 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001579 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001580 CE->isCompare() ? CE->getPredicate() : 0,
1581 CE->hasIndices() ?
1582 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001583}
1584
Chris Lattner69edc982006-09-28 00:35:06 +00001585static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1586 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001587
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001588/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001589/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001590static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001591 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001592 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001593 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001594 if (Constant *FC =
1595 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001596 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001597
Vikram S. Adve4c485332002-07-15 18:19:33 +00001598 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001599 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001600 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001601
Owen Anderson61794042009-06-17 20:10:08 +00001602 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001603 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001604}
Reid Spencerf37dc652006-12-05 19:14:13 +00001605
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001606Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001607 Instruction::CastOps opc = Instruction::CastOps(oc);
1608 assert(Instruction::isCast(opc) && "opcode out of range");
1609 assert(C && Ty && "Null arguments to getCast");
1610 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1611
1612 switch (opc) {
1613 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001614 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001615 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001616 case Instruction::Trunc: return getTrunc(C, Ty);
1617 case Instruction::ZExt: return getZExt(C, Ty);
1618 case Instruction::SExt: return getSExt(C, Ty);
1619 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1620 case Instruction::FPExt: return getFPExtend(C, Ty);
1621 case Instruction::UIToFP: return getUIToFP(C, Ty);
1622 case Instruction::SIToFP: return getSIToFP(C, Ty);
1623 case Instruction::FPToUI: return getFPToUI(C, Ty);
1624 case Instruction::FPToSI: return getFPToSI(C, Ty);
1625 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1626 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1627 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001628 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001629 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001630}
1631
Reid Spencer5c140882006-12-04 20:17:56 +00001632Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001633 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001634 return getCast(Instruction::BitCast, C, Ty);
1635 return getCast(Instruction::ZExt, C, Ty);
1636}
1637
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001638Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001639 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001640 return getCast(Instruction::BitCast, C, Ty);
1641 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001642}
1643
1644Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001645 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001646 return getCast(Instruction::BitCast, C, Ty);
1647 return getCast(Instruction::Trunc, C, Ty);
1648}
1649
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001650Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001651 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001652 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001653
Chris Lattner03c49532007-01-15 02:27:26 +00001654 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001655 return getCast(Instruction::PtrToInt, S, Ty);
1656 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001657}
1658
Reid Spencer56521c42006-12-12 00:51:07 +00001659Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1660 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001661 assert(C->getType()->isIntOrIntVector() &&
1662 Ty->isIntOrIntVector() && "Invalid cast");
1663 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1664 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001665 Instruction::CastOps opcode =
1666 (SrcBits == DstBits ? Instruction::BitCast :
1667 (SrcBits > DstBits ? Instruction::Trunc :
1668 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1669 return getCast(opcode, C, Ty);
1670}
1671
1672Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001673 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001674 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001675 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1676 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001677 if (SrcBits == DstBits)
1678 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001679 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001680 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001681 return getCast(opcode, C, Ty);
1682}
1683
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001684Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001685#ifndef NDEBUG
1686 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1687 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1688#endif
1689 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1690 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1691 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1692 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001693 "SrcTy must be larger than DestTy for Trunc!");
1694
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001695 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001696}
1697
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001698Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001699#ifndef NDEBUG
1700 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1701 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1702#endif
1703 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1704 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1705 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1706 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001707 "SrcTy must be smaller than DestTy for SExt!");
1708
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001709 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001710}
1711
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001712Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001713#ifndef NDEBUG
1714 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1715 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1716#endif
1717 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1718 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1719 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1720 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001721 "SrcTy must be smaller than DestTy for ZExt!");
1722
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001723 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724}
1725
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001726Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001727#ifndef NDEBUG
1728 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1729 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1730#endif
1731 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1732 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1733 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001734 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001735 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001736}
1737
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001738Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001739#ifndef NDEBUG
1740 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1741 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1742#endif
1743 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1744 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1745 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001746 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001747 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001748}
1749
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001750Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001751#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001752 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1753 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001754#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001755 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1756 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1757 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001758 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001759}
1760
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001761Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001762#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001763 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1764 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001765#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001766 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1767 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001768 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001769 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001770}
1771
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001772Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001773#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001774 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1775 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001776#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001777 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1778 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1779 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001780 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001781}
1782
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001783Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001784#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001785 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1786 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001787#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001788 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1789 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1790 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001791 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001792}
1793
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001794Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001796 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001797 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001798}
1799
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001800Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001801 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001802 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001803 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001804}
1805
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001806Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001807 // BitCast implies a no-op cast of type only. No bits change. However, you
1808 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001809#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001810 const Type *SrcTy = C->getType();
1811 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001812 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813
1814 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1815 // or nonptr->ptr). For all the other types, the cast is okay if source and
1816 // destination bit widths are identical.
1817 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1818 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001819#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001820 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001821
1822 // It is common to ask for a bitcast of a value to its own type, handle this
1823 // speedily.
1824 if (C->getType() == DstTy) return C;
1825
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001826 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001827}
1828
Chris Lattnerb50d1352003-10-05 00:17:43 +00001829Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001830 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001831 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001832 assert(Opcode >= Instruction::BinaryOpsBegin &&
1833 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001834 "Invalid opcode in binary constant expression");
1835 assert(C1->getType() == C2->getType() &&
1836 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001837
Reid Spencer542964f2007-01-11 18:21:29 +00001838 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001839 if (Constant *FC = ConstantFoldBinaryInstruction(
1840 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001841 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001842
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001843 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001844 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001845
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001846 // Implicitly locked.
1847 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001848}
1849
Reid Spencer266e42b2006-12-23 06:05:41 +00001850Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001851 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001852 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001853 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001854 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1855 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1856 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1857 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1858 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1859 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001860 return getFCmp(predicate, C1, C2);
1861
Nate Begemanc96e2e42008-07-25 17:35:37 +00001862 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1863 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1864 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1865 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001866 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001867 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001868}
1869
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001870Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001871 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1872 if (C1->getType()->isFPOrFPVector()) {
1873 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1874 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1875 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1876 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001877#ifndef NDEBUG
1878 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001879 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001880 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001881 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001882 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001883 assert(C1->getType()->isIntOrIntVector() &&
1884 "Tried to create an integer operation on a non-integer type!");
1885 break;
1886 case Instruction::FAdd:
1887 case Instruction::FSub:
1888 case Instruction::FMul:
1889 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1890 assert(C1->getType()->isFPOrFPVector() &&
1891 "Tried to create a floating-point operation on a "
1892 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001893 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001894 case Instruction::UDiv:
1895 case Instruction::SDiv:
1896 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001897 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001898 "Tried to create an arithmetic operation on a non-arithmetic type!");
1899 break;
1900 case Instruction::FDiv:
1901 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001902 assert(C1->getType()->isFPOrFPVector() &&
1903 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001904 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001905 case Instruction::URem:
1906 case Instruction::SRem:
1907 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001908 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001909 "Tried to create an arithmetic operation on a non-arithmetic type!");
1910 break;
1911 case Instruction::FRem:
1912 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001913 assert(C1->getType()->isFPOrFPVector() &&
1914 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001915 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001916 case Instruction::And:
1917 case Instruction::Or:
1918 case Instruction::Xor:
1919 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001920 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001921 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001922 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001923 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001924 case Instruction::LShr:
1925 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001926 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001927 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001928 "Tried to create a shift operation on a non-integer type!");
1929 break;
1930 default:
1931 break;
1932 }
1933#endif
1934
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001935 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001936}
1937
Reid Spencer266e42b2006-12-23 06:05:41 +00001938Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001939 Constant *C1, Constant *C2) {
1940 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001941 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001942}
1943
Chris Lattner6e415c02004-03-12 05:54:04 +00001944Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001945 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001946 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001947
1948 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001949 if (Constant *SC = ConstantFoldSelectInstruction(
1950 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001951 return SC; // Fold common cases
1952
1953 std::vector<Constant*> argVec(3, C);
1954 argVec[1] = V1;
1955 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001956 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001957
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001958 // Implicitly locked.
1959 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001960}
1961
Chris Lattnerb50d1352003-10-05 00:17:43 +00001962Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001963 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001964 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001965 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1966 Idxs+NumIdx) ==
1967 cast<PointerType>(ReqTy)->getElementType() &&
1968 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001969
Owen Anderson53a52212009-07-13 04:09:18 +00001970 if (Constant *FC = ConstantFoldGetElementPtr(
1971 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001972 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001973
Chris Lattnerb50d1352003-10-05 00:17:43 +00001974 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001975 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001976 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001977 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001978 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001979 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001980 for (unsigned i = 0; i != NumIdx; ++i)
1981 ArgVec.push_back(cast<Constant>(Idxs[i]));
1982 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001983
1984 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001985 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001986}
1987
Chris Lattner302116a2007-01-31 04:40:28 +00001988Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001989 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001990 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001991 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001992 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001993 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001994 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001995 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001996}
1997
Chris Lattner302116a2007-01-31 04:40:28 +00001998Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001999 unsigned NumIdx) {
2000 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002001}
2002
Chris Lattner302116a2007-01-31 04:40:28 +00002003
Reid Spenceree3c9912006-12-04 05:19:50 +00002004Constant *
2005ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2006 assert(LHS->getType() == RHS->getType());
2007 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2008 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2009
Owen Anderson53a52212009-07-13 04:09:18 +00002010 if (Constant *FC = ConstantFoldCompareInstruction(
2011 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002012 return FC; // Fold a few common cases...
2013
2014 // Look up the constant in the table first to ensure uniqueness
2015 std::vector<Constant*> ArgVec;
2016 ArgVec.push_back(LHS);
2017 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002018 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002019 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002020
2021 // Implicitly locked.
2022 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002023}
2024
2025Constant *
2026ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2027 assert(LHS->getType() == RHS->getType());
2028 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2029
Owen Anderson53a52212009-07-13 04:09:18 +00002030 if (Constant *FC = ConstantFoldCompareInstruction(
2031 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002032 return FC; // Fold a few common cases...
2033
2034 // Look up the constant in the table first to ensure uniqueness
2035 std::vector<Constant*> ArgVec;
2036 ArgVec.push_back(LHS);
2037 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002038 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002039 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002040
2041 // Implicitly locked.
2042 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002043}
2044
Robert Bocchino23004482006-01-10 19:05:34 +00002045Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2046 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002047 if (Constant *FC = ConstantFoldExtractElementInstruction(
2048 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002049 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002050 // Look up the constant in the table first to ensure uniqueness
2051 std::vector<Constant*> ArgVec(1, Val);
2052 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002053 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002054
2055 // Implicitly locked.
2056 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002057}
2058
2059Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002060 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002061 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002062 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002063 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002064 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002065 Val, Idx);
2066}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002067
Robert Bocchinoca27f032006-01-17 20:07:22 +00002068Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2069 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002070 if (Constant *FC = ConstantFoldInsertElementInstruction(
2071 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002072 return FC; // Fold a few common cases...
2073 // Look up the constant in the table first to ensure uniqueness
2074 std::vector<Constant*> ArgVec(1, Val);
2075 ArgVec.push_back(Elt);
2076 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002077 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002078
2079 // Implicitly locked.
2080 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002081}
2082
2083Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2084 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002085 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002086 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002087 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002088 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002089 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002090 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002091 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002092}
2093
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002094Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2095 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002096 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2097 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002098 return FC; // Fold a few common cases...
2099 // Look up the constant in the table first to ensure uniqueness
2100 std::vector<Constant*> ArgVec(1, V1);
2101 ArgVec.push_back(V2);
2102 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002103 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002104
2105 // Implicitly locked.
2106 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002107}
2108
2109Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2110 Constant *Mask) {
2111 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2112 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002113
2114 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2115 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2116 const Type *ShufTy = VectorType::get(EltTy, NElts);
2117 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002118}
2119
Dan Gohman12fce772008-05-15 19:50:34 +00002120Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2121 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002122 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002123 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2124 Idxs+NumIdx) == Val->getType() &&
2125 "insertvalue indices invalid!");
2126 assert(Agg->getType() == ReqTy &&
2127 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002128 assert(Agg->getType()->isFirstClassType() &&
2129 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002130 Constant *FC = ConstantFoldInsertValueInstruction(
2131 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002132 assert(FC && "InsertValue constant expr couldn't be folded!");
2133 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002134}
2135
2136Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002137 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002138 assert(Agg->getType()->isFirstClassType() &&
2139 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002140
Dan Gohman0752bff2008-05-23 00:36:11 +00002141 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002142#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002143 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002144 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002145#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002146 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002147 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2148}
2149
2150Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002151 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002152 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2153 Idxs+NumIdx) == ReqTy &&
2154 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002155 assert(Agg->getType()->isFirstClassType() &&
2156 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002157 Constant *FC = ConstantFoldExtractValueInstruction(
2158 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002159 assert(FC && "ExtractValue constant expr couldn't be folded!");
2160 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002161}
2162
2163Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002164 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002165 assert(Agg->getType()->isFirstClassType() &&
2166 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002167
2168 const Type *ReqTy =
2169 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2170 assert(ReqTy && "extractvalue indices invalid!");
2171 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2172}
2173
Vikram S. Adve4c485332002-07-15 18:19:33 +00002174// destroyConstant - Remove the constant from the constant table...
2175//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002176void ConstantExpr::destroyConstant() {
2177 // Implicitly locked.
2178 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002179 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002180}
2181
Chris Lattner3cd8c562002-07-30 18:54:25 +00002182const char *ConstantExpr::getOpcodeName() const {
2183 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002184}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002185
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002186//===----------------------------------------------------------------------===//
2187// replaceUsesOfWithOnConstant implementations
2188
Chris Lattner913849b2007-08-21 00:55:23 +00002189/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2190/// 'From' to be uses of 'To'. This must update the uniquing data structures
2191/// etc.
2192///
2193/// Note that we intentionally replace all uses of From with To here. Consider
2194/// a large array that uses 'From' 1000 times. By handling this case all here,
2195/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2196/// single invocation handles all 1000 uses. Handling them one at a time would
2197/// work, but would be really slow because it would have to unique each updated
2198/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002199void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002200 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002201 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002202 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002203
Jim Laskeyc03caef2006-07-17 17:38:29 +00002204 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002205 Lookup.first.first = getType();
2206 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002207
Chris Lattnerb64419a2005-10-03 22:51:37 +00002208 std::vector<Constant*> &Values = Lookup.first.second;
2209 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002210
Chris Lattner8760ec72005-10-04 01:17:50 +00002211 // Fill values with the modified operands of the constant array. Also,
2212 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002213 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002214 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002215 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002216 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2217 Constant *Val = cast<Constant>(O->get());
2218 if (Val == From) {
2219 Val = ToC;
2220 ++NumUpdated;
2221 }
2222 Values.push_back(Val);
2223 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002224 } else {
2225 isAllZeros = true;
2226 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2227 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002228 if (Val == From) {
2229 Val = ToC;
2230 ++NumUpdated;
2231 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002232 Values.push_back(Val);
2233 if (isAllZeros) isAllZeros = Val->isNullValue();
2234 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002235 }
2236
Chris Lattnerb64419a2005-10-03 22:51:37 +00002237 Constant *Replacement = 0;
2238 if (isAllZeros) {
2239 Replacement = ConstantAggregateZero::get(getType());
2240 } else {
2241 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002242 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002243 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002244 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002245 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002246
2247 if (Exists) {
2248 Replacement = I->second;
2249 } else {
2250 // Okay, the new shape doesn't exist in the system yet. Instead of
2251 // creating a new constant array, inserting it, replaceallusesof'ing the
2252 // old with the new, then deleting the old... just update the current one
2253 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002254 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002255
Chris Lattner913849b2007-08-21 00:55:23 +00002256 // Update to the new value. Optimize for the case when we have a single
2257 // operand that we're changing, but handle bulk updates efficiently.
2258 if (NumUpdated == 1) {
2259 unsigned OperandToUpdate = U-OperandList;
2260 assert(getOperand(OperandToUpdate) == From &&
2261 "ReplaceAllUsesWith broken!");
2262 setOperand(OperandToUpdate, ToC);
2263 } else {
2264 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2265 if (getOperand(i) == From)
2266 setOperand(i, ToC);
2267 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002268 return;
2269 }
2270 }
2271
2272 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002273 assert(Replacement != this && "I didn't contain From!");
2274
Chris Lattner7a1450d2005-10-04 18:13:04 +00002275 // Everyone using this now uses the replacement.
2276 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002277
2278 // Delete the old constant!
2279 destroyConstant();
2280}
2281
2282void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002283 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002284 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002285 Constant *ToC = cast<Constant>(To);
2286
Chris Lattnerdff59112005-10-04 18:47:09 +00002287 unsigned OperandToUpdate = U-OperandList;
2288 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2289
Jim Laskeyc03caef2006-07-17 17:38:29 +00002290 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002291 Lookup.first.first = getType();
2292 Lookup.second = this;
2293 std::vector<Constant*> &Values = Lookup.first.second;
2294 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002295
Chris Lattnerdff59112005-10-04 18:47:09 +00002296
Chris Lattner8760ec72005-10-04 01:17:50 +00002297 // Fill values with the modified operands of the constant struct. Also,
2298 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002299 bool isAllZeros = false;
2300 if (!ToC->isNullValue()) {
2301 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2302 Values.push_back(cast<Constant>(O->get()));
2303 } else {
2304 isAllZeros = true;
2305 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2306 Constant *Val = cast<Constant>(O->get());
2307 Values.push_back(Val);
2308 if (isAllZeros) isAllZeros = Val->isNullValue();
2309 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002310 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002311 Values[OperandToUpdate] = ToC;
2312
Chris Lattner8760ec72005-10-04 01:17:50 +00002313 Constant *Replacement = 0;
2314 if (isAllZeros) {
2315 Replacement = ConstantAggregateZero::get(getType());
2316 } else {
2317 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002318 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002319 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002320 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002321 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002322
2323 if (Exists) {
2324 Replacement = I->second;
2325 } else {
2326 // Okay, the new shape doesn't exist in the system yet. Instead of
2327 // creating a new constant struct, inserting it, replaceallusesof'ing the
2328 // old with the new, then deleting the old... just update the current one
2329 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002330 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002331
Chris Lattnerdff59112005-10-04 18:47:09 +00002332 // Update to the new value.
2333 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002334 return;
2335 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002336 }
2337
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002338 assert(Replacement != this && "I didn't contain From!");
2339
Chris Lattner7a1450d2005-10-04 18:13:04 +00002340 // Everyone using this now uses the replacement.
2341 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002342
2343 // Delete the old constant!
2344 destroyConstant();
2345}
2346
Reid Spencerd84d35b2007-02-15 02:26:10 +00002347void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002348 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002349 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2350
2351 std::vector<Constant*> Values;
2352 Values.reserve(getNumOperands()); // Build replacement array...
2353 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2354 Constant *Val = getOperand(i);
2355 if (Val == From) Val = cast<Constant>(To);
2356 Values.push_back(Val);
2357 }
2358
Reid Spencerd84d35b2007-02-15 02:26:10 +00002359 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002360 assert(Replacement != this && "I didn't contain From!");
2361
Chris Lattner7a1450d2005-10-04 18:13:04 +00002362 // Everyone using this now uses the replacement.
2363 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002364
2365 // Delete the old constant!
2366 destroyConstant();
2367}
2368
2369void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002370 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002371 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2372 Constant *To = cast<Constant>(ToV);
2373
2374 Constant *Replacement = 0;
2375 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002376 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002377 Constant *Pointer = getOperand(0);
2378 Indices.reserve(getNumOperands()-1);
2379 if (Pointer == From) Pointer = To;
2380
2381 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2382 Constant *Val = getOperand(i);
2383 if (Val == From) Val = To;
2384 Indices.push_back(Val);
2385 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002386 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2387 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002388 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002389 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002390 if (Agg == From) Agg = To;
2391
Dan Gohman1ecaf452008-05-31 00:58:22 +00002392 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002393 Replacement = ConstantExpr::getExtractValue(Agg,
2394 &Indices[0], Indices.size());
2395 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002396 Constant *Agg = getOperand(0);
2397 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002398 if (Agg == From) Agg = To;
2399 if (Val == From) Val = To;
2400
Dan Gohman1ecaf452008-05-31 00:58:22 +00002401 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002402 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2403 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002405 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002407 } else if (getOpcode() == Instruction::Select) {
2408 Constant *C1 = getOperand(0);
2409 Constant *C2 = getOperand(1);
2410 Constant *C3 = getOperand(2);
2411 if (C1 == From) C1 = To;
2412 if (C2 == From) C2 = To;
2413 if (C3 == From) C3 = To;
2414 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002415 } else if (getOpcode() == Instruction::ExtractElement) {
2416 Constant *C1 = getOperand(0);
2417 Constant *C2 = getOperand(1);
2418 if (C1 == From) C1 = To;
2419 if (C2 == From) C2 = To;
2420 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002421 } else if (getOpcode() == Instruction::InsertElement) {
2422 Constant *C1 = getOperand(0);
2423 Constant *C2 = getOperand(1);
2424 Constant *C3 = getOperand(1);
2425 if (C1 == From) C1 = To;
2426 if (C2 == From) C2 = To;
2427 if (C3 == From) C3 = To;
2428 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2429 } else if (getOpcode() == Instruction::ShuffleVector) {
2430 Constant *C1 = getOperand(0);
2431 Constant *C2 = getOperand(1);
2432 Constant *C3 = getOperand(2);
2433 if (C1 == From) C1 = To;
2434 if (C2 == From) C2 = To;
2435 if (C3 == From) C3 = To;
2436 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002437 } else if (isCompare()) {
2438 Constant *C1 = getOperand(0);
2439 Constant *C2 = getOperand(1);
2440 if (C1 == From) C1 = To;
2441 if (C2 == From) C2 = To;
2442 if (getOpcode() == Instruction::ICmp)
2443 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002444 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002445 assert(getOpcode() == Instruction::FCmp);
2446 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002447 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002448 } else if (getNumOperands() == 2) {
2449 Constant *C1 = getOperand(0);
2450 Constant *C2 = getOperand(1);
2451 if (C1 == From) C1 = To;
2452 if (C2 == From) C2 = To;
2453 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2454 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002455 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002456 return;
2457 }
2458
2459 assert(Replacement != this && "I didn't contain From!");
2460
Chris Lattner7a1450d2005-10-04 18:13:04 +00002461 // Everyone using this now uses the replacement.
2462 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002463
2464 // Delete the old constant!
2465 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002466}
Nick Lewycky49f89192009-04-04 07:22:01 +00002467
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002468void MDNode::replaceElement(Value *From, Value *To) {
2469 SmallVector<Value*, 4> Values;
2470 Values.reserve(getNumElements()); // Build replacement array...
2471 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2472 Value *Val = getElement(i);
2473 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002474 Values.push_back(Val);
2475 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002476
Owen Anderson4118dde2009-07-16 23:44:30 +00002477 MDNode *Replacement =
2478 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002479 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002480
Nick Lewycky49f89192009-04-04 07:22:01 +00002481 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002482
Nick Lewycky49f89192009-04-04 07:22:01 +00002483 destroyConstant();
2484}