blob: f6d8e86850f4ff5efb6f3984fb2a1d3df2782676 [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 +0000172ConstantInt *ConstantInt::TheTrueVal = 0;
173ConstantInt *ConstantInt::TheFalseVal = 0;
174
175namespace llvm {
176 void CleanupTrueFalse(void *) {
177 ConstantInt::ResetTrueFalse();
178 }
179}
180
181static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
182
183ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
184 assert(TheTrueVal == 0 && TheFalseVal == 0);
Owen Andersonb6b25302009-07-14 23:09:55 +0000185 TheTrueVal = getGlobalContext().getConstantInt(Type::Int1Ty, 1);
186 TheFalseVal = getGlobalContext().getConstantInt(Type::Int1Ty, 0);
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000187
188 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
189 TrueFalseCleanup.Register();
190
191 return WhichOne ? TheTrueVal : TheFalseVal;
192}
193
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000194//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000195// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000196//===----------------------------------------------------------------------===//
197
Daniel Dunbare974dc32009-07-17 18:33:52 +0000198#ifndef NDEBUG
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000199static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
200 if (Ty == Type::FloatTy)
201 return &APFloat::IEEEsingle;
202 if (Ty == Type::DoubleTy)
203 return &APFloat::IEEEdouble;
204 if (Ty == Type::X86_FP80Ty)
205 return &APFloat::x87DoubleExtended;
206 else if (Ty == Type::FP128Ty)
207 return &APFloat::IEEEquad;
208
209 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
210 return &APFloat::PPCDoubleDouble;
211}
Daniel Dunbare974dc32009-07-17 18:33:52 +0000212#endif
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000213
Dale Johannesend246b2c2007-08-30 00:23:21 +0000214ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
215 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000216 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
217 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218}
219
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000220bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000221 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000222}
223
Dale Johannesend246b2c2007-08-30 00:23:21 +0000224bool ConstantFP::isExactlyValue(const APFloat& V) const {
225 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000226}
227
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000228//===----------------------------------------------------------------------===//
229// ConstantXXX Classes
230//===----------------------------------------------------------------------===//
231
232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000234 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000235 : Constant(T, ConstantArrayVal,
236 OperandTraits<ConstantArray>::op_end(this) - V.size(),
237 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000238 assert(V.size() == T->getNumElements() &&
239 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000240 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000241 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
242 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000243 Constant *C = *I;
244 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000245 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000246 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000247 "Initializer for array element doesn't match array 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
Chris Lattner3462ae32001-12-03 22:26:30 +0000253ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000254 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 : Constant(T, ConstantStructVal,
256 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
257 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000258 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000259 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000260 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000261 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
262 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000263 Constant *C = *I;
264 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000265 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000266 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000267 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000268 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000269 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000270 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271 }
272}
273
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000274
Reid Spencerd84d35b2007-02-15 02:26:10 +0000275ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000276 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000277 : Constant(T, ConstantVectorVal,
278 OperandTraits<ConstantVector>::op_end(this) - V.size(),
279 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000280 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000281 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
282 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000283 Constant *C = *I;
284 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000285 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000286 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000287 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000288 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000289 }
290}
291
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000292
Gabor Greiff6caff662008-05-10 08:32:32 +0000293namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000294// We declare several classes private to this file, so use an anonymous
295// namespace
296namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000297
Gordon Henriksen14a55692007-12-10 02:14:30 +0000298/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
299/// behind the scenes to implement unary constant exprs.
300class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000301 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000302public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000303 // allocate space for exactly one operand
304 void *operator new(size_t s) {
305 return User::operator new(s, 1);
306 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000307 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000308 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
309 Op<0>() = C;
310 }
311 /// Transparently provide more efficient getOperand methods.
312 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000313};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000314
Gordon Henriksen14a55692007-12-10 02:14:30 +0000315/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
316/// behind the scenes to implement binary constant exprs.
317class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000318 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000319public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000320 // allocate space for exactly two operands
321 void *operator new(size_t s) {
322 return User::operator new(s, 2);
323 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000324 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000326 Op<0>() = C1;
327 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000328 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000329 /// Transparently provide more efficient getOperand methods.
330 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000331};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000332
Gordon Henriksen14a55692007-12-10 02:14:30 +0000333/// SelectConstantExpr - This class is private to Constants.cpp, and is used
334/// behind the scenes to implement select constant exprs.
335class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000336 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000337public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000338 // allocate space for exactly three operands
339 void *operator new(size_t s) {
340 return User::operator new(s, 3);
341 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000342 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000343 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000344 Op<0>() = C1;
345 Op<1>() = C2;
346 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000347 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000348 /// Transparently provide more efficient getOperand methods.
349 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000350};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000351
Gordon Henriksen14a55692007-12-10 02:14:30 +0000352/// ExtractElementConstantExpr - This class is private to
353/// Constants.cpp, and is used behind the scenes to implement
354/// extractelement constant exprs.
355class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000356 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000357public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000358 // allocate space for exactly two operands
359 void *operator new(size_t s) {
360 return User::operator new(s, 2);
361 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000362 ExtractElementConstantExpr(Constant *C1, Constant *C2)
363 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000364 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000365 Op<0>() = C1;
366 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000367 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000368 /// Transparently provide more efficient getOperand methods.
369 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000370};
Robert Bocchino23004482006-01-10 19:05:34 +0000371
Gordon Henriksen14a55692007-12-10 02:14:30 +0000372/// InsertElementConstantExpr - This class is private to
373/// Constants.cpp, and is used behind the scenes to implement
374/// insertelement constant exprs.
375class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000376 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000377public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000378 // allocate space for exactly three operands
379 void *operator new(size_t s) {
380 return User::operator new(s, 3);
381 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000382 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
383 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000384 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000385 Op<0>() = C1;
386 Op<1>() = C2;
387 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000388 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000389 /// Transparently provide more efficient getOperand methods.
390 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000391};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000392
Gordon Henriksen14a55692007-12-10 02:14:30 +0000393/// ShuffleVectorConstantExpr - This class is private to
394/// Constants.cpp, and is used behind the scenes to implement
395/// shufflevector constant exprs.
396class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000397 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000398public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000399 // allocate space for exactly three operands
400 void *operator new(size_t s) {
401 return User::operator new(s, 3);
402 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000403 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000404 : ConstantExpr(VectorType::get(
405 cast<VectorType>(C1->getType())->getElementType(),
406 cast<VectorType>(C3->getType())->getNumElements()),
407 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000408 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000409 Op<0>() = C1;
410 Op<1>() = C2;
411 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000412 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000413 /// Transparently provide more efficient getOperand methods.
414 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000415};
416
Dan Gohman12fce772008-05-15 19:50:34 +0000417/// ExtractValueConstantExpr - This class is private to
418/// Constants.cpp, and is used behind the scenes to implement
419/// extractvalue constant exprs.
420class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000421 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000422public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000423 // allocate space for exactly one operand
424 void *operator new(size_t s) {
425 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000426 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000427 ExtractValueConstantExpr(Constant *Agg,
428 const SmallVector<unsigned, 4> &IdxList,
429 const Type *DestTy)
430 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
431 Indices(IdxList) {
432 Op<0>() = Agg;
433 }
434
Dan Gohman7bb04502008-05-31 19:09:08 +0000435 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000436 const SmallVector<unsigned, 4> Indices;
437
Dan Gohman12fce772008-05-15 19:50:34 +0000438 /// Transparently provide more efficient getOperand methods.
439 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
440};
441
442/// InsertValueConstantExpr - This class is private to
443/// Constants.cpp, and is used behind the scenes to implement
444/// insertvalue constant exprs.
445class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000446 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000447public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000448 // allocate space for exactly one operand
449 void *operator new(size_t s) {
450 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000451 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000452 InsertValueConstantExpr(Constant *Agg, Constant *Val,
453 const SmallVector<unsigned, 4> &IdxList,
454 const Type *DestTy)
455 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
456 Indices(IdxList) {
457 Op<0>() = Agg;
458 Op<1>() = Val;
459 }
460
Dan Gohman7bb04502008-05-31 19:09:08 +0000461 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000462 const SmallVector<unsigned, 4> Indices;
463
Dan Gohman12fce772008-05-15 19:50:34 +0000464 /// Transparently provide more efficient getOperand methods.
465 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
466};
467
468
Gordon Henriksen14a55692007-12-10 02:14:30 +0000469/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
470/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000471class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000473 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000474public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000475 static GetElementPtrConstantExpr *Create(Constant *C,
476 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000477 const Type *DestTy) {
Dan Gohman7d82e132009-07-18 01:49:22 +0000478 GetElementPtrConstantExpr *Result = new(IdxList.size() + 1)
Gabor Greif697e94c2008-05-15 10:04:30 +0000479 GetElementPtrConstantExpr(C, IdxList, DestTy);
Dan Gohman7d82e132009-07-18 01:49:22 +0000480 // Getelementptr defaults to having no pointer overflow.
481 cast<GEPOperator>(Result)->setHasNoPointerOverflow(true);
482 return Result;
Gabor Greife9ecc682008-04-06 20:25:17 +0000483 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000484 /// Transparently provide more efficient getOperand methods.
485 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000486};
487
488// CompareConstantExpr - This class is private to Constants.cpp, and is used
489// behind the scenes to implement ICmp and FCmp constant expressions. This is
490// needed in order to store the predicate value for these instructions.
491struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000492 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
493 // allocate space for exactly two operands
494 void *operator new(size_t s) {
495 return User::operator new(s, 2);
496 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000497 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000498 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
499 unsigned short pred, Constant* LHS, Constant* RHS)
500 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000501 Op<0>() = LHS;
502 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000503 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000504 /// Transparently provide more efficient getOperand methods.
505 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000506};
507
508} // end anonymous namespace
509
Gabor Greiff6caff662008-05-10 08:32:32 +0000510template <>
511struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
512};
513DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
514
515template <>
516struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
517};
518DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
519
520template <>
521struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
522};
523DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
524
525template <>
526struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
527};
528DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
529
530template <>
531struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
532};
533DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
534
535template <>
536struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
537};
538DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
539
Dan Gohman12fce772008-05-15 19:50:34 +0000540template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000541struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000542};
Dan Gohman12fce772008-05-15 19:50:34 +0000543DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
544
545template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000546struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000547};
Dan Gohman12fce772008-05-15 19:50:34 +0000548DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
549
Gabor Greiff6caff662008-05-10 08:32:32 +0000550template <>
551struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
552};
553
554GetElementPtrConstantExpr::GetElementPtrConstantExpr
555 (Constant *C,
556 const std::vector<Constant*> &IdxList,
557 const Type *DestTy)
558 : ConstantExpr(DestTy, Instruction::GetElementPtr,
559 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
560 - (IdxList.size()+1),
561 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000562 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000563 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000564 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000565}
566
567DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
568
569
570template <>
571struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
572};
573DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
574
575
576} // End llvm namespace
577
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000578
579// Utility function for determining if a ConstantExpr is a CastOp or not. This
580// can't be inline because we don't want to #include Instruction.h into
581// Constant.h
582bool ConstantExpr::isCast() const {
583 return Instruction::isCast(getOpcode());
584}
585
Reid Spenceree3c9912006-12-04 05:19:50 +0000586bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000587 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000588}
589
Dan Gohman1ecaf452008-05-31 00:58:22 +0000590bool ConstantExpr::hasIndices() const {
591 return getOpcode() == Instruction::ExtractValue ||
592 getOpcode() == Instruction::InsertValue;
593}
594
595const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
596 if (const ExtractValueConstantExpr *EVCE =
597 dyn_cast<ExtractValueConstantExpr>(this))
598 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000599
600 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000601}
602
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000603unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000604 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000605 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000606 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000607}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000608
Chris Lattner7c1018a2006-07-14 19:37:40 +0000609/// getWithOperandReplaced - Return a constant expression identical to this
610/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000611Constant *
612ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000613 assert(OpNo < getNumOperands() && "Operand num is out of range!");
614 assert(Op->getType() == getOperand(OpNo)->getType() &&
615 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000616 if (getOperand(OpNo) == Op)
617 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000618
Chris Lattner227816342006-07-14 22:20:01 +0000619 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000620 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000621 case Instruction::Trunc:
622 case Instruction::ZExt:
623 case Instruction::SExt:
624 case Instruction::FPTrunc:
625 case Instruction::FPExt:
626 case Instruction::UIToFP:
627 case Instruction::SIToFP:
628 case Instruction::FPToUI:
629 case Instruction::FPToSI:
630 case Instruction::PtrToInt:
631 case Instruction::IntToPtr:
632 case Instruction::BitCast:
633 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000634 case Instruction::Select:
635 Op0 = (OpNo == 0) ? Op : getOperand(0);
636 Op1 = (OpNo == 1) ? Op : getOperand(1);
637 Op2 = (OpNo == 2) ? Op : getOperand(2);
638 return ConstantExpr::getSelect(Op0, Op1, Op2);
639 case Instruction::InsertElement:
640 Op0 = (OpNo == 0) ? Op : getOperand(0);
641 Op1 = (OpNo == 1) ? Op : getOperand(1);
642 Op2 = (OpNo == 2) ? Op : getOperand(2);
643 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
644 case Instruction::ExtractElement:
645 Op0 = (OpNo == 0) ? Op : getOperand(0);
646 Op1 = (OpNo == 1) ? Op : getOperand(1);
647 return ConstantExpr::getExtractElement(Op0, Op1);
648 case Instruction::ShuffleVector:
649 Op0 = (OpNo == 0) ? Op : getOperand(0);
650 Op1 = (OpNo == 1) ? Op : getOperand(1);
651 Op2 = (OpNo == 2) ? Op : getOperand(2);
652 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000653 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000654 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000655 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000656 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000657 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000658 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000659 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000660 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000661 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000662 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000663 default:
664 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000665 Op0 = (OpNo == 0) ? Op : getOperand(0);
666 Op1 = (OpNo == 1) ? Op : getOperand(1);
667 return ConstantExpr::get(getOpcode(), Op0, Op1);
668 }
669}
670
671/// getWithOperands - This returns the current constant expression with the
672/// operands replaced with the specified values. The specified operands must
673/// match count and type with the existing ones.
674Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000675getWithOperands(Constant* const *Ops, unsigned NumOps) const {
676 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000677 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000678 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000679 assert(Ops[i]->getType() == getOperand(i)->getType() &&
680 "Operand type mismatch!");
681 AnyChange |= Ops[i] != getOperand(i);
682 }
683 if (!AnyChange) // No operands changed, return self.
684 return const_cast<ConstantExpr*>(this);
685
686 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000687 case Instruction::Trunc:
688 case Instruction::ZExt:
689 case Instruction::SExt:
690 case Instruction::FPTrunc:
691 case Instruction::FPExt:
692 case Instruction::UIToFP:
693 case Instruction::SIToFP:
694 case Instruction::FPToUI:
695 case Instruction::FPToSI:
696 case Instruction::PtrToInt:
697 case Instruction::IntToPtr:
698 case Instruction::BitCast:
699 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000700 case Instruction::Select:
701 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
702 case Instruction::InsertElement:
703 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
704 case Instruction::ExtractElement:
705 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
706 case Instruction::ShuffleVector:
707 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000708 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000709 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000710 case Instruction::ICmp:
711 case Instruction::FCmp:
712 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000713 default:
714 assert(getNumOperands() == 2 && "Must be binary operator?");
715 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000716 }
717}
718
Chris Lattner2f7c9632001-06-06 20:29:01 +0000719
720//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000721// isValueValidForType implementations
722
Reid Spencere7334722006-12-19 01:28:19 +0000723bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000724 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000725 if (Ty == Type::Int1Ty)
726 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000727 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000728 return true; // always true, has to fit in largest type
729 uint64_t Max = (1ll << NumBits) - 1;
730 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000731}
732
Reid Spencere0fc4df2006-10-20 07:07:24 +0000733bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000734 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000735 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000736 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000737 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000738 return true; // always true, has to fit in largest type
739 int64_t Min = -(1ll << (NumBits-1));
740 int64_t Max = (1ll << (NumBits-1)) - 1;
741 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000742}
743
Dale Johannesend246b2c2007-08-30 00:23:21 +0000744bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
745 // convert modifies in place, so make a copy.
746 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000747 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000748 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000749 default:
750 return false; // These can't be represented as floating point!
751
Dale Johannesend246b2c2007-08-30 00:23:21 +0000752 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000753 case Type::FloatTyID: {
754 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
755 return true;
756 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
757 return !losesInfo;
758 }
759 case Type::DoubleTyID: {
760 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
761 &Val2.getSemantics() == &APFloat::IEEEdouble)
762 return true;
763 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
764 return !losesInfo;
765 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000766 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000767 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
768 &Val2.getSemantics() == &APFloat::IEEEdouble ||
769 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000770 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000771 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
772 &Val2.getSemantics() == &APFloat::IEEEdouble ||
773 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000774 case Type::PPC_FP128TyID:
775 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
776 &Val2.getSemantics() == &APFloat::IEEEdouble ||
777 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000778 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000779}
Chris Lattner9655e542001-07-20 19:16:02 +0000780
Chris Lattner49d855c2001-09-07 16:46:31 +0000781//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000782// Factory Function Implementation
783
Gabor Greiff6caff662008-05-10 08:32:32 +0000784
785// The number of operands for each ConstantCreator::create method is
786// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000787// ConstantCreator - A class that is used to create constants by
788// ValueMap*. This class should be partially specialized if there is
789// something strange that needs to be done to interface to the ctor for the
790// constant.
791//
Chris Lattner189d19f2003-11-21 20:23:48 +0000792namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000793 template<class ValType>
794 struct ConstantTraits;
795
796 template<typename T, typename Alloc>
797 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
798 static unsigned uses(const std::vector<T, Alloc>& v) {
799 return v.size();
800 }
801 };
802
Chris Lattner189d19f2003-11-21 20:23:48 +0000803 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000804 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000805 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000806 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000807 }
808 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000809
Chris Lattner189d19f2003-11-21 20:23:48 +0000810 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000811 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000812 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000813 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000814 }
815 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000816
Chris Lattner935aa922005-10-04 17:48:46 +0000817 template<class ValType, class TypeClass, class ConstantClass,
818 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000819 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000820 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000821 typedef std::pair<const Type*, ValType> MapKey;
822 typedef std::map<MapKey, Constant *> MapTy;
823 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
824 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000825 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000826 /// Map - This is the main map from the element descriptor to the Constants.
827 /// This is the primary way we avoid creating two of the same shape
828 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000829 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000830
831 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
832 /// from the constants to their element in Map. This is important for
833 /// removal of constants from the array, which would otherwise have to scan
834 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000835 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000836
Jim Laskeyc03caef2006-07-17 17:38:29 +0000837 /// AbstractTypeMap - Map for abstract type constants.
838 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000839 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000840
841 /// ValueMapLock - Mutex for this map.
842 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000843
Chris Lattner98fa07b2003-05-23 20:03:32 +0000844 public:
Owen Anderson61794042009-06-17 20:10:08 +0000845 // NOTE: This function is not locked. It is the caller's responsibility
846 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000847 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000848
849 /// InsertOrGetItem - Return an iterator for the specified element.
850 /// If the element exists in the map, the returned iterator points to the
851 /// entry and Exists=true. If not, the iterator points to the newly
852 /// inserted entry and returns Exists=false. Newly inserted entries have
853 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000854 /// NOTE: This function is not locked. It is the caller's responsibility
855 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000856 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
857 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000858 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000859 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000860 Exists = !IP.second;
861 return IP.first;
862 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000863
Chris Lattner935aa922005-10-04 17:48:46 +0000864private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000865 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000866 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000867 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000868 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
869 IMI->second->second == CP &&
870 "InverseMap corrupt!");
871 return IMI->second;
872 }
873
Jim Laskeyc03caef2006-07-17 17:38:29 +0000874 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000875 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
876 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000877 if (I == Map.end() || I->second != CP) {
878 // FIXME: This should not use a linear scan. If this gets to be a
879 // performance problem, someone should look at this.
880 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
881 /* empty */;
882 }
Chris Lattner935aa922005-10-04 17:48:46 +0000883 return I;
884 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000885
886 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
887 typename MapTy::iterator I) {
888 ConstantClass* Result =
889 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
890
891 assert(Result->getType() == Ty && "Type specified is not correct!");
892 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
893
894 if (HasLargeKey) // Remember the reverse mapping if needed.
895 InverseMap.insert(std::make_pair(Result, I));
896
897 // If the type of the constant is abstract, make sure that an entry
898 // exists for it in the AbstractTypeMap.
899 if (Ty->isAbstract()) {
900 typename AbstractTypeMapTy::iterator TI =
901 AbstractTypeMap.find(Ty);
902
903 if (TI == AbstractTypeMap.end()) {
904 // Add ourselves to the ATU list of the type.
905 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
906
907 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
908 }
909 }
910
911 return Result;
912 }
Chris Lattner935aa922005-10-04 17:48:46 +0000913public:
914
Chris Lattnerb64419a2005-10-03 22:51:37 +0000915 /// getOrCreate - Return the specified constant from the map, creating it if
916 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000917 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000918 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +0000919 MapKey Lookup(Ty, V);
920 ConstantClass* Result = 0;
921
922 typename MapTy::iterator I = Map.find(Lookup);
923 // Is it in the map?
924 if (I != Map.end())
925 Result = static_cast<ConstantClass *>(I->second);
926
927 if (!Result) {
928 // If no preexisting value, create one now...
929 Result = Create(Ty, V, I);
930 }
931
932 return Result;
933 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000934
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000935 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000936 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000937 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000938 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000939 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000940
Chris Lattner935aa922005-10-04 17:48:46 +0000941 if (HasLargeKey) // Remember the reverse mapping if needed.
942 InverseMap.erase(CP);
943
Chris Lattnerb50d1352003-10-05 00:17:43 +0000944 // Now that we found the entry, make sure this isn't the entry that
945 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000946 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000947 if (Ty->isAbstract()) {
948 assert(AbstractTypeMap.count(Ty) &&
949 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000950 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000951 if (ATMEntryIt == I) {
952 // Yes, we are removing the representative entry for this type.
953 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000954 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000955
Chris Lattnerb50d1352003-10-05 00:17:43 +0000956 // First check the entry before this one...
957 if (TmpIt != Map.begin()) {
958 --TmpIt;
959 if (TmpIt->first.first != Ty) // Not the same type, move back...
960 ++TmpIt;
961 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000962
Chris Lattnerb50d1352003-10-05 00:17:43 +0000963 // If we didn't find the same type, try to move forward...
964 if (TmpIt == ATMEntryIt) {
965 ++TmpIt;
966 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
967 --TmpIt; // No entry afterwards with the same type
968 }
969
970 // If there is another entry in the map of the same abstract type,
971 // update the AbstractTypeMap entry now.
972 if (TmpIt != ATMEntryIt) {
973 ATMEntryIt = TmpIt;
974 } else {
975 // Otherwise, we are removing the last instance of this type
976 // from the table. Remove from the ATM, and from user list.
977 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
978 AbstractTypeMap.erase(Ty);
979 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000980 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000981 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000982
Chris Lattnerb50d1352003-10-05 00:17:43 +0000983 Map.erase(I);
984 }
985
Chris Lattner3b793c62005-10-04 21:35:50 +0000986
987 /// MoveConstantToNewSlot - If we are about to change C to be the element
988 /// specified by I, update our internal data structures to reflect this
989 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +0000990 /// NOTE: This function is not locked. It is the responsibility of the
991 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000992 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000993 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000994 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000995 assert(OldI != Map.end() && "Constant not found in constant table!");
996 assert(OldI->second == C && "Didn't find correct element?");
997
998 // If this constant is the representative element for its abstract type,
999 // update the AbstractTypeMap so that the representative element is I.
1000 if (C->getType()->isAbstract()) {
1001 typename AbstractTypeMapTy::iterator ATI =
1002 AbstractTypeMap.find(C->getType());
1003 assert(ATI != AbstractTypeMap.end() &&
1004 "Abstract type not in AbstractTypeMap?");
1005 if (ATI->second == OldI)
1006 ATI->second = I;
1007 }
1008
1009 // Remove the old entry from the map.
1010 Map.erase(OldI);
1011
1012 // Update the inverse map so that we know that this constant is now
1013 // located at descriptor I.
1014 if (HasLargeKey) {
1015 assert(I->second == C && "Bad inversemap entry!");
1016 InverseMap[C] = I;
1017 }
1018 }
1019
Chris Lattnerb50d1352003-10-05 00:17:43 +00001020 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001021 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001022 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001023 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001024
1025 assert(I != AbstractTypeMap.end() &&
1026 "Abstract type not in AbstractTypeMap?");
1027
1028 // Convert a constant at a time until the last one is gone. The last one
1029 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1030 // eliminated eventually.
1031 do {
1032 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001033 TypeClass>::convert(
1034 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001035 cast<TypeClass>(NewTy));
1036
Jim Laskeyc03caef2006-07-17 17:38:29 +00001037 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001038 } while (I != AbstractTypeMap.end());
1039 }
1040
1041 // If the type became concrete without being refined to any other existing
1042 // type, we just remove ourselves from the ATU list.
1043 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001044 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001045 }
1046
1047 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001048 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001049 }
1050 };
1051}
1052
Chris Lattnera84df0a22006-09-28 23:36:21 +00001053
Chris Lattner28173502007-02-20 06:11:36 +00001054
Chris Lattner9fba3da2004-02-15 05:53:04 +00001055//---- ConstantAggregateZero::get() implementation...
1056//
1057namespace llvm {
1058 // ConstantAggregateZero does not take extra "value" argument...
1059 template<class ValType>
1060 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1061 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1062 return new ConstantAggregateZero(Ty);
1063 }
1064 };
1065
1066 template<>
1067 struct ConvertConstantType<ConstantAggregateZero, Type> {
1068 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1069 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001070 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001071 assert(New != OldC && "Didn't replace constant??");
1072 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001073 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001074 }
1075 };
1076}
1077
Chris Lattner69edc982006-09-28 00:35:06 +00001078static ManagedStatic<ValueMap<char, Type,
1079 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001080
Chris Lattner3e650af2004-08-04 04:48:01 +00001081static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1082
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001083ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001084 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001085 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001086
1087 // Implicitly locked.
1088 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001089}
1090
Dan Gohman92b551b2009-03-03 02:55:14 +00001091/// destroyConstant - Remove the constant from the constant table...
1092///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001093void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001094 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001095 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001096 destroyConstantImpl();
1097}
1098
Chris Lattner3462ae32001-12-03 22:26:30 +00001099//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001100//
Chris Lattner189d19f2003-11-21 20:23:48 +00001101namespace llvm {
1102 template<>
1103 struct ConvertConstantType<ConstantArray, ArrayType> {
1104 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1105 // Make everyone now use a constant of the new type...
1106 std::vector<Constant*> C;
1107 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1108 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001109 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001110 assert(New != OldC && "Didn't replace constant??");
1111 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001112 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001113 }
1114 };
1115}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001116
Chris Lattner3e650af2004-08-04 04:48:01 +00001117static std::vector<Constant*> getValType(ConstantArray *CA) {
1118 std::vector<Constant*> Elements;
1119 Elements.reserve(CA->getNumOperands());
1120 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1121 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1122 return Elements;
1123}
1124
Chris Lattnerb64419a2005-10-03 22:51:37 +00001125typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001126 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001127static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001128
Chris Lattner015e8212004-02-15 04:14:47 +00001129Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001130 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001131 // If this is an all-zero array, return a ConstantAggregateZero object
1132 if (!V.empty()) {
1133 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001134 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001135 // Implicitly locked.
1136 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001137 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001138 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001139 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001140 // Implicitly locked.
1141 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001142 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001143 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001144
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001145 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001146}
1147
Dan Gohman92b551b2009-03-03 02:55:14 +00001148/// destroyConstant - Remove the constant from the constant table...
1149///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001150void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001151 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001152 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001153 destroyConstantImpl();
1154}
1155
Reid Spencer2546b762007-01-26 07:37:34 +00001156/// isString - This method returns true if the array is an array of i8, and
1157/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001158bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001159 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001160 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001161 return false;
1162 // Check the elements to make sure they are all integers, not constant
1163 // expressions.
1164 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1165 if (!isa<ConstantInt>(getOperand(i)))
1166 return false;
1167 return true;
1168}
1169
Evan Cheng3763c5b2006-10-26 19:15:05 +00001170/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001171/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001172/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001173bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001174 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001175 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001176 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001177
Evan Chenge974da62006-10-26 21:48:03 +00001178 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001179 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001180 return false;
1181 // Other elements must be non-null integers.
1182 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1183 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001184 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001185 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001186 return false;
1187 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001188 return true;
1189}
1190
1191
Dan Gohman92b551b2009-03-03 02:55:14 +00001192/// getAsString - If the sub-element type of this array is i8
1193/// then this method converts the array to an std::string and returns it.
1194/// Otherwise, it asserts out.
1195///
Chris Lattner81fabb02002-08-26 17:53:56 +00001196std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001197 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001198 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001199 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001200 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001201 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001202 return Result;
1203}
1204
1205
Chris Lattner3462ae32001-12-03 22:26:30 +00001206//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001207//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001208
Chris Lattner189d19f2003-11-21 20:23:48 +00001209namespace llvm {
1210 template<>
1211 struct ConvertConstantType<ConstantStruct, StructType> {
1212 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1213 // Make everyone now use a constant of the new type...
1214 std::vector<Constant*> C;
1215 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1216 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001217 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001218 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001219
Chris Lattner189d19f2003-11-21 20:23:48 +00001220 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001221 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001222 }
1223 };
1224}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001225
Chris Lattner8760ec72005-10-04 01:17:50 +00001226typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001227 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001228static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001229
Chris Lattner3e650af2004-08-04 04:48:01 +00001230static std::vector<Constant*> getValType(ConstantStruct *CS) {
1231 std::vector<Constant*> Elements;
1232 Elements.reserve(CS->getNumOperands());
1233 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1234 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1235 return Elements;
1236}
1237
Chris Lattner015e8212004-02-15 04:14:47 +00001238Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001239 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001240 // Create a ConstantAggregateZero value if all elements are zeros...
1241 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001242 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001243 // Implicitly locked.
1244 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001245
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001246 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001247}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001248
Chris Lattnerd7a73302001-10-13 06:57:33 +00001249// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001250//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001251void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001252 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001253 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001254 destroyConstantImpl();
1255}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001256
Reid Spencerd84d35b2007-02-15 02:26:10 +00001257//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001258//
1259namespace llvm {
1260 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001261 struct ConvertConstantType<ConstantVector, VectorType> {
1262 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001263 // Make everyone now use a constant of the new type...
1264 std::vector<Constant*> C;
1265 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1266 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001267 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001268 assert(New != OldC && "Didn't replace constant??");
1269 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001270 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001271 }
1272 };
1273}
1274
Reid Spencerd84d35b2007-02-15 02:26:10 +00001275static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001276 std::vector<Constant*> Elements;
1277 Elements.reserve(CP->getNumOperands());
1278 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1279 Elements.push_back(CP->getOperand(i));
1280 return Elements;
1281}
1282
Reid Spencerd84d35b2007-02-15 02:26:10 +00001283static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001284 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001285
Reid Spencerd84d35b2007-02-15 02:26:10 +00001286Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001287 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001288 assert(!V.empty() && "Vectors can't be empty");
1289 // If this is an all-undef or alll-zero vector, return a
1290 // ConstantAggregateZero or UndefValue.
1291 Constant *C = V[0];
1292 bool isZero = C->isNullValue();
1293 bool isUndef = isa<UndefValue>(C);
1294
1295 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001296 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001297 if (V[i] != C) {
1298 isZero = isUndef = false;
1299 break;
1300 }
Brian Gaeke02209042004-08-20 06:00:58 +00001301 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001302
1303 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001304 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001305 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001306 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001307
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001308 // Implicitly locked.
1309 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001310}
1311
Brian Gaeke02209042004-08-20 06:00:58 +00001312// destroyConstant - Remove the constant from the constant table...
1313//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001314void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001315 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001316 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001317 destroyConstantImpl();
1318}
1319
Dan Gohman30978072007-05-24 14:36:04 +00001320/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001321/// is set to all ones.
1322/// @returns true iff this constant's emements are all set to all ones.
1323/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001324bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001325 // Check out first element.
1326 const Constant *Elt = getOperand(0);
1327 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1328 if (!CI || !CI->isAllOnesValue()) return false;
1329 // Then make sure all remaining elements point to the same value.
1330 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1331 if (getOperand(I) != Elt) return false;
1332 }
1333 return true;
1334}
1335
Dan Gohman07159202007-10-17 17:51:30 +00001336/// getSplatValue - If this is a splat constant, where all of the
1337/// elements have the same value, return that value. Otherwise return null.
1338Constant *ConstantVector::getSplatValue() {
1339 // Check out first element.
1340 Constant *Elt = getOperand(0);
1341 // Then make sure all remaining elements point to the same value.
1342 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1343 if (getOperand(I) != Elt) return 0;
1344 return Elt;
1345}
1346
Chris Lattner3462ae32001-12-03 22:26:30 +00001347//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001348//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001349
Chris Lattner189d19f2003-11-21 20:23:48 +00001350namespace llvm {
1351 // ConstantPointerNull does not take extra "value" argument...
1352 template<class ValType>
1353 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1354 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1355 return new ConstantPointerNull(Ty);
1356 }
1357 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001358
Chris Lattner189d19f2003-11-21 20:23:48 +00001359 template<>
1360 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1361 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1362 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001363 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001364 assert(New != OldC && "Didn't replace constant??");
1365 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001366 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001367 }
1368 };
1369}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001370
Chris Lattner69edc982006-09-28 00:35:06 +00001371static ManagedStatic<ValueMap<char, PointerType,
1372 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001373
Chris Lattner3e650af2004-08-04 04:48:01 +00001374static char getValType(ConstantPointerNull *) {
1375 return 0;
1376}
1377
1378
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001379ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001380 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001381 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001382}
1383
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001384// destroyConstant - Remove the constant from the constant table...
1385//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001386void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001387 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001388 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001389 destroyConstantImpl();
1390}
1391
1392
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001393//---- UndefValue::get() implementation...
1394//
1395
1396namespace llvm {
1397 // UndefValue does not take extra "value" argument...
1398 template<class ValType>
1399 struct ConstantCreator<UndefValue, Type, ValType> {
1400 static UndefValue *create(const Type *Ty, const ValType &V) {
1401 return new UndefValue(Ty);
1402 }
1403 };
1404
1405 template<>
1406 struct ConvertConstantType<UndefValue, Type> {
1407 static void convert(UndefValue *OldC, const Type *NewTy) {
1408 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001409 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001410 assert(New != OldC && "Didn't replace constant??");
1411 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001412 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001413 }
1414 };
1415}
1416
Chris Lattner69edc982006-09-28 00:35:06 +00001417static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001418
1419static char getValType(UndefValue *) {
1420 return 0;
1421}
1422
1423
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001424UndefValue *UndefValue::get(const Type *Ty) {
1425 // Implicitly locked.
1426 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001427}
1428
1429// destroyConstant - Remove the constant from the constant table.
1430//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001431void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001432 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001433 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001434 destroyConstantImpl();
1435}
1436
Nick Lewycky49f89192009-04-04 07:22:01 +00001437//---- MDString::get() implementation
1438//
1439
1440MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001441 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001442 StrBegin(begin), StrEnd(end) {}
1443
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001444void MDString::destroyConstant() {
Owen Anderson69ab4162009-07-16 22:11:26 +00001445 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001446 destroyConstantImpl();
1447}
1448
1449//---- MDNode::get() implementation
1450//
1451
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001452MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001453 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001454 for (unsigned i = 0; i != NumVals; ++i)
1455 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001456}
1457
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001458void MDNode::Profile(FoldingSetNodeID &ID) const {
1459 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001460 ID.AddPointer(*I);
1461}
1462
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001463void MDNode::destroyConstant() {
Owen Anderson4118dde2009-07-16 23:44:30 +00001464 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001465 destroyConstantImpl();
1466}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001467
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001468//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001469//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001470
Dan Gohmand78c4002008-05-13 00:00:25 +00001471namespace {
1472
Reid Spenceree3c9912006-12-04 05:19:50 +00001473struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001474 typedef SmallVector<unsigned, 4> IndexList;
1475
1476 ExprMapKeyType(unsigned opc,
1477 const std::vector<Constant*> &ops,
1478 unsigned short pred = 0,
1479 const IndexList &inds = IndexList())
1480 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001481 uint16_t opcode;
1482 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001483 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001484 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001485 bool operator==(const ExprMapKeyType& that) const {
1486 return this->opcode == that.opcode &&
1487 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001488 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001489 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001490 }
1491 bool operator<(const ExprMapKeyType & that) const {
1492 return this->opcode < that.opcode ||
1493 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1494 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001495 this->operands < that.operands) ||
1496 (this->opcode == that.opcode && this->predicate == that.predicate &&
1497 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001498 }
1499
1500 bool operator!=(const ExprMapKeyType& that) const {
1501 return !(*this == that);
1502 }
1503};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001504
Dan Gohmand78c4002008-05-13 00:00:25 +00001505}
1506
Chris Lattner189d19f2003-11-21 20:23:48 +00001507namespace llvm {
1508 template<>
1509 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001510 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1511 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001512 if (Instruction::isCast(V.opcode))
1513 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1514 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001515 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001516 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1517 if (V.opcode == Instruction::Select)
1518 return new SelectConstantExpr(V.operands[0], V.operands[1],
1519 V.operands[2]);
1520 if (V.opcode == Instruction::ExtractElement)
1521 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1522 if (V.opcode == Instruction::InsertElement)
1523 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1524 V.operands[2]);
1525 if (V.opcode == Instruction::ShuffleVector)
1526 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1527 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001528 if (V.opcode == Instruction::InsertValue)
1529 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1530 V.indices, Ty);
1531 if (V.opcode == Instruction::ExtractValue)
1532 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001533 if (V.opcode == Instruction::GetElementPtr) {
1534 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001535 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001536 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001537
Reid Spenceree3c9912006-12-04 05:19:50 +00001538 // The compare instructions are weird. We have to encode the predicate
1539 // value and it is combined with the instruction opcode by multiplying
1540 // the opcode by one hundred. We must decode this to get the predicate.
1541 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001542 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001543 V.operands[0], V.operands[1]);
1544 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001545 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1546 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001547 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001548 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001549 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001550 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001551
Chris Lattner189d19f2003-11-21 20:23:48 +00001552 template<>
1553 struct ConvertConstantType<ConstantExpr, Type> {
1554 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1555 Constant *New;
1556 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001557 case Instruction::Trunc:
1558 case Instruction::ZExt:
1559 case Instruction::SExt:
1560 case Instruction::FPTrunc:
1561 case Instruction::FPExt:
1562 case Instruction::UIToFP:
1563 case Instruction::SIToFP:
1564 case Instruction::FPToUI:
1565 case Instruction::FPToSI:
1566 case Instruction::PtrToInt:
1567 case Instruction::IntToPtr:
1568 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001569 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001570 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001571 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001572 case Instruction::Select:
1573 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1574 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001575 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001576 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001577 default:
1578 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001579 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001580 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001581 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001582 break;
1583 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001584 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001585 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001586 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001587 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001588 break;
1589 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001590
Chris Lattner189d19f2003-11-21 20:23:48 +00001591 assert(New != OldC && "Didn't replace constant??");
1592 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001593 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001594 }
1595 };
1596} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001597
1598
Chris Lattner3e650af2004-08-04 04:48:01 +00001599static ExprMapKeyType getValType(ConstantExpr *CE) {
1600 std::vector<Constant*> Operands;
1601 Operands.reserve(CE->getNumOperands());
1602 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1603 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001604 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001605 CE->isCompare() ? CE->getPredicate() : 0,
1606 CE->hasIndices() ?
1607 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001608}
1609
Chris Lattner69edc982006-09-28 00:35:06 +00001610static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1611 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001612
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001613/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001614/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001615static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001616 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001617 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001618 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001619 if (Constant *FC =
1620 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001621 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001622
Vikram S. Adve4c485332002-07-15 18:19:33 +00001623 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001624 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001625 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001626
Owen Anderson61794042009-06-17 20:10:08 +00001627 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001628 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001629}
Reid Spencerf37dc652006-12-05 19:14:13 +00001630
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001631Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001632 Instruction::CastOps opc = Instruction::CastOps(oc);
1633 assert(Instruction::isCast(opc) && "opcode out of range");
1634 assert(C && Ty && "Null arguments to getCast");
1635 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1636
1637 switch (opc) {
1638 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001639 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001640 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001641 case Instruction::Trunc: return getTrunc(C, Ty);
1642 case Instruction::ZExt: return getZExt(C, Ty);
1643 case Instruction::SExt: return getSExt(C, Ty);
1644 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1645 case Instruction::FPExt: return getFPExtend(C, Ty);
1646 case Instruction::UIToFP: return getUIToFP(C, Ty);
1647 case Instruction::SIToFP: return getSIToFP(C, Ty);
1648 case Instruction::FPToUI: return getFPToUI(C, Ty);
1649 case Instruction::FPToSI: return getFPToSI(C, Ty);
1650 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1651 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1652 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001653 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001654 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001655}
1656
Reid Spencer5c140882006-12-04 20:17:56 +00001657Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001658 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001659 return getCast(Instruction::BitCast, C, Ty);
1660 return getCast(Instruction::ZExt, C, Ty);
1661}
1662
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001663Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001664 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001665 return getCast(Instruction::BitCast, C, Ty);
1666 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001667}
1668
1669Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001670 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001671 return getCast(Instruction::BitCast, C, Ty);
1672 return getCast(Instruction::Trunc, C, Ty);
1673}
1674
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001675Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001676 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001677 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001678
Chris Lattner03c49532007-01-15 02:27:26 +00001679 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001680 return getCast(Instruction::PtrToInt, S, Ty);
1681 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001682}
1683
Reid Spencer56521c42006-12-12 00:51:07 +00001684Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1685 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001686 assert(C->getType()->isIntOrIntVector() &&
1687 Ty->isIntOrIntVector() && "Invalid cast");
1688 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1689 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001690 Instruction::CastOps opcode =
1691 (SrcBits == DstBits ? Instruction::BitCast :
1692 (SrcBits > DstBits ? Instruction::Trunc :
1693 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1694 return getCast(opcode, C, Ty);
1695}
1696
1697Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001698 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001699 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001700 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1701 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001702 if (SrcBits == DstBits)
1703 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001704 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001705 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001706 return getCast(opcode, C, Ty);
1707}
1708
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001709Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001710#ifndef NDEBUG
1711 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1712 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1713#endif
1714 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1715 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1716 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1717 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001718 "SrcTy must be larger than DestTy for Trunc!");
1719
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001720 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001721}
1722
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001723Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001724#ifndef NDEBUG
1725 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1726 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1727#endif
1728 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1729 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1730 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1731 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001732 "SrcTy must be smaller than DestTy for SExt!");
1733
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001734 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001735}
1736
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001737Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001738#ifndef NDEBUG
1739 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1740 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1741#endif
1742 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1743 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1744 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1745 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001746 "SrcTy must be smaller than DestTy for ZExt!");
1747
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001748 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001749}
1750
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001751Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001752#ifndef NDEBUG
1753 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1754 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1755#endif
1756 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1757 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1758 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001759 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001760 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001761}
1762
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001763Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001764#ifndef NDEBUG
1765 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1766 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1767#endif
1768 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1769 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1770 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001771 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001772 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001773}
1774
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001775Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001776#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001777 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1778 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001779#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001780 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1781 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1782 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001783 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001784}
1785
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001786Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001787#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001788 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1789 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001790#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001791 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1792 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001794 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795}
1796
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001797Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001798#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001799 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1800 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001801#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001802 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1803 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1804 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001805 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001806}
1807
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001808Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001809#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001810 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1811 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001812#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001813 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1814 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1815 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001816 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817}
1818
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001819Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001820 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001821 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001822 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001823}
1824
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001825Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001826 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001828 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001829}
1830
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001831Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001832 // BitCast implies a no-op cast of type only. No bits change. However, you
1833 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001834#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001835 const Type *SrcTy = C->getType();
1836 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001837 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001838
1839 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1840 // or nonptr->ptr). For all the other types, the cast is okay if source and
1841 // destination bit widths are identical.
1842 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1843 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001844#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001845 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001846
1847 // It is common to ask for a bitcast of a value to its own type, handle this
1848 // speedily.
1849 if (C->getType() == DstTy) return C;
1850
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001851 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001852}
1853
Chris Lattnerb50d1352003-10-05 00:17:43 +00001854Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001855 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001856 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001857 assert(Opcode >= Instruction::BinaryOpsBegin &&
1858 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001859 "Invalid opcode in binary constant expression");
1860 assert(C1->getType() == C2->getType() &&
1861 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001862
Reid Spencer542964f2007-01-11 18:21:29 +00001863 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001864 if (Constant *FC = ConstantFoldBinaryInstruction(
1865 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001866 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001867
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001868 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001869 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001870
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001871 // Implicitly locked.
1872 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001873}
1874
Reid Spencer266e42b2006-12-23 06:05:41 +00001875Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001876 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001877 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001878 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001879 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1880 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1881 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1882 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1883 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1884 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001885 return getFCmp(predicate, C1, C2);
1886
Nate Begemanc96e2e42008-07-25 17:35:37 +00001887 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1888 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1889 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1890 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001891 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001892 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001893}
1894
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001895Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001896 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1897 if (C1->getType()->isFPOrFPVector()) {
1898 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1899 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1900 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1901 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001902#ifndef NDEBUG
1903 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001904 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001905 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001906 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001907 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001908 assert(C1->getType()->isIntOrIntVector() &&
1909 "Tried to create an integer operation on a non-integer type!");
1910 break;
1911 case Instruction::FAdd:
1912 case Instruction::FSub:
1913 case Instruction::FMul:
1914 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1915 assert(C1->getType()->isFPOrFPVector() &&
1916 "Tried to create a floating-point operation on a "
1917 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001918 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001919 case Instruction::UDiv:
1920 case Instruction::SDiv:
1921 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001922 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001923 "Tried to create an arithmetic operation on a non-arithmetic type!");
1924 break;
1925 case Instruction::FDiv:
1926 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001927 assert(C1->getType()->isFPOrFPVector() &&
1928 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001929 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001930 case Instruction::URem:
1931 case Instruction::SRem:
1932 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001933 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001934 "Tried to create an arithmetic operation on a non-arithmetic type!");
1935 break;
1936 case Instruction::FRem:
1937 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001938 assert(C1->getType()->isFPOrFPVector() &&
1939 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001940 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001941 case Instruction::And:
1942 case Instruction::Or:
1943 case Instruction::Xor:
1944 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001945 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001946 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001947 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001948 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001949 case Instruction::LShr:
1950 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001951 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001952 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001953 "Tried to create a shift operation on a non-integer type!");
1954 break;
1955 default:
1956 break;
1957 }
1958#endif
1959
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001960 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001961}
1962
Reid Spencer266e42b2006-12-23 06:05:41 +00001963Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001964 Constant *C1, Constant *C2) {
1965 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001966 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001967}
1968
Chris Lattner6e415c02004-03-12 05:54:04 +00001969Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001970 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001971 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001972
1973 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001974 if (Constant *SC = ConstantFoldSelectInstruction(
1975 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001976 return SC; // Fold common cases
1977
1978 std::vector<Constant*> argVec(3, C);
1979 argVec[1] = V1;
1980 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001981 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001982
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001983 // Implicitly locked.
1984 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001985}
1986
Chris Lattnerb50d1352003-10-05 00:17:43 +00001987Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001988 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001989 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001990 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1991 Idxs+NumIdx) ==
1992 cast<PointerType>(ReqTy)->getElementType() &&
1993 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001994
Owen Anderson53a52212009-07-13 04:09:18 +00001995 if (Constant *FC = ConstantFoldGetElementPtr(
1996 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001997 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001998
Chris Lattnerb50d1352003-10-05 00:17:43 +00001999 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002000 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002001 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002002 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002003 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002004 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002005 for (unsigned i = 0; i != NumIdx; ++i)
2006 ArgVec.push_back(cast<Constant>(Idxs[i]));
2007 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002008
2009 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002010 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002011}
2012
Chris Lattner302116a2007-01-31 04:40:28 +00002013Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002014 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002015 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002016 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002017 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002018 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002019 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002020 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002021}
2022
Chris Lattner302116a2007-01-31 04:40:28 +00002023Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002024 unsigned NumIdx) {
2025 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002026}
2027
Chris Lattner302116a2007-01-31 04:40:28 +00002028
Reid Spenceree3c9912006-12-04 05:19:50 +00002029Constant *
2030ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2031 assert(LHS->getType() == RHS->getType());
2032 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2033 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2034
Owen Anderson53a52212009-07-13 04:09:18 +00002035 if (Constant *FC = ConstantFoldCompareInstruction(
2036 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002037 return FC; // Fold a few common cases...
2038
2039 // Look up the constant in the table first to ensure uniqueness
2040 std::vector<Constant*> ArgVec;
2041 ArgVec.push_back(LHS);
2042 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002043 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002044 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002045
2046 // Implicitly locked.
2047 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002048}
2049
2050Constant *
2051ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2052 assert(LHS->getType() == RHS->getType());
2053 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2054
Owen Anderson53a52212009-07-13 04:09:18 +00002055 if (Constant *FC = ConstantFoldCompareInstruction(
2056 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002057 return FC; // Fold a few common cases...
2058
2059 // Look up the constant in the table first to ensure uniqueness
2060 std::vector<Constant*> ArgVec;
2061 ArgVec.push_back(LHS);
2062 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002063 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002064 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002065
2066 // Implicitly locked.
2067 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002068}
2069
Robert Bocchino23004482006-01-10 19:05:34 +00002070Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2071 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002072 if (Constant *FC = ConstantFoldExtractElementInstruction(
2073 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002074 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002075 // Look up the constant in the table first to ensure uniqueness
2076 std::vector<Constant*> ArgVec(1, Val);
2077 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002078 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002079
2080 // Implicitly locked.
2081 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002082}
2083
2084Constant *ConstantExpr::getExtractElement(Constant *Val, 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 extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002087 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002088 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002089 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002090 Val, Idx);
2091}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002092
Robert Bocchinoca27f032006-01-17 20:07:22 +00002093Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2094 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002095 if (Constant *FC = ConstantFoldInsertElementInstruction(
2096 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002097 return FC; // Fold a few common cases...
2098 // Look up the constant in the table first to ensure uniqueness
2099 std::vector<Constant*> ArgVec(1, Val);
2100 ArgVec.push_back(Elt);
2101 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002102 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002103
2104 // Implicitly locked.
2105 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002106}
2107
2108Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2109 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002110 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002111 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002112 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002113 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002114 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002115 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002116 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002117}
2118
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002119Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2120 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002121 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2122 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002123 return FC; // Fold a few common cases...
2124 // Look up the constant in the table first to ensure uniqueness
2125 std::vector<Constant*> ArgVec(1, V1);
2126 ArgVec.push_back(V2);
2127 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002128 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002129
2130 // Implicitly locked.
2131 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002132}
2133
2134Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2135 Constant *Mask) {
2136 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2137 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002138
2139 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2140 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2141 const Type *ShufTy = VectorType::get(EltTy, NElts);
2142 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002143}
2144
Dan Gohman12fce772008-05-15 19:50:34 +00002145Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2146 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002147 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002148 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2149 Idxs+NumIdx) == Val->getType() &&
2150 "insertvalue indices invalid!");
2151 assert(Agg->getType() == ReqTy &&
2152 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002153 assert(Agg->getType()->isFirstClassType() &&
2154 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002155 Constant *FC = ConstantFoldInsertValueInstruction(
2156 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002157 assert(FC && "InsertValue constant expr couldn't be folded!");
2158 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002159}
2160
2161Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002162 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002163 assert(Agg->getType()->isFirstClassType() &&
2164 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002165
Dan Gohman0752bff2008-05-23 00:36:11 +00002166 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002167#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002168 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002169 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002170#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002171 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002172 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2173}
2174
2175Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002176 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002177 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2178 Idxs+NumIdx) == ReqTy &&
2179 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002180 assert(Agg->getType()->isFirstClassType() &&
2181 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002182 Constant *FC = ConstantFoldExtractValueInstruction(
2183 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002184 assert(FC && "ExtractValue constant expr couldn't be folded!");
2185 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002186}
2187
2188Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002189 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002190 assert(Agg->getType()->isFirstClassType() &&
2191 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002192
2193 const Type *ReqTy =
2194 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2195 assert(ReqTy && "extractvalue indices invalid!");
2196 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2197}
2198
Vikram S. Adve4c485332002-07-15 18:19:33 +00002199// destroyConstant - Remove the constant from the constant table...
2200//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002201void ConstantExpr::destroyConstant() {
2202 // Implicitly locked.
2203 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002204 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002205}
2206
Chris Lattner3cd8c562002-07-30 18:54:25 +00002207const char *ConstantExpr::getOpcodeName() const {
2208 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002209}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002210
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002211//===----------------------------------------------------------------------===//
2212// replaceUsesOfWithOnConstant implementations
2213
Chris Lattner913849b2007-08-21 00:55:23 +00002214/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2215/// 'From' to be uses of 'To'. This must update the uniquing data structures
2216/// etc.
2217///
2218/// Note that we intentionally replace all uses of From with To here. Consider
2219/// a large array that uses 'From' 1000 times. By handling this case all here,
2220/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2221/// single invocation handles all 1000 uses. Handling them one at a time would
2222/// work, but would be really slow because it would have to unique each updated
2223/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002224void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002225 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002226 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002227 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002228
Jim Laskeyc03caef2006-07-17 17:38:29 +00002229 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002230 Lookup.first.first = getType();
2231 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002232
Chris Lattnerb64419a2005-10-03 22:51:37 +00002233 std::vector<Constant*> &Values = Lookup.first.second;
2234 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002235
Chris Lattner8760ec72005-10-04 01:17:50 +00002236 // Fill values with the modified operands of the constant array. Also,
2237 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002238 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002239 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002240 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002241 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2242 Constant *Val = cast<Constant>(O->get());
2243 if (Val == From) {
2244 Val = ToC;
2245 ++NumUpdated;
2246 }
2247 Values.push_back(Val);
2248 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002249 } else {
2250 isAllZeros = true;
2251 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2252 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002253 if (Val == From) {
2254 Val = ToC;
2255 ++NumUpdated;
2256 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002257 Values.push_back(Val);
2258 if (isAllZeros) isAllZeros = Val->isNullValue();
2259 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002260 }
2261
Chris Lattnerb64419a2005-10-03 22:51:37 +00002262 Constant *Replacement = 0;
2263 if (isAllZeros) {
2264 Replacement = ConstantAggregateZero::get(getType());
2265 } else {
2266 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002267 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002268 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002269 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002270 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002271
2272 if (Exists) {
2273 Replacement = I->second;
2274 } else {
2275 // Okay, the new shape doesn't exist in the system yet. Instead of
2276 // creating a new constant array, inserting it, replaceallusesof'ing the
2277 // old with the new, then deleting the old... just update the current one
2278 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002279 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002280
Chris Lattner913849b2007-08-21 00:55:23 +00002281 // Update to the new value. Optimize for the case when we have a single
2282 // operand that we're changing, but handle bulk updates efficiently.
2283 if (NumUpdated == 1) {
2284 unsigned OperandToUpdate = U-OperandList;
2285 assert(getOperand(OperandToUpdate) == From &&
2286 "ReplaceAllUsesWith broken!");
2287 setOperand(OperandToUpdate, ToC);
2288 } else {
2289 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2290 if (getOperand(i) == From)
2291 setOperand(i, ToC);
2292 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002293 return;
2294 }
2295 }
2296
2297 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002298 assert(Replacement != this && "I didn't contain From!");
2299
Chris Lattner7a1450d2005-10-04 18:13:04 +00002300 // Everyone using this now uses the replacement.
2301 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002302
2303 // Delete the old constant!
2304 destroyConstant();
2305}
2306
2307void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002308 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002309 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002310 Constant *ToC = cast<Constant>(To);
2311
Chris Lattnerdff59112005-10-04 18:47:09 +00002312 unsigned OperandToUpdate = U-OperandList;
2313 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2314
Jim Laskeyc03caef2006-07-17 17:38:29 +00002315 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002316 Lookup.first.first = getType();
2317 Lookup.second = this;
2318 std::vector<Constant*> &Values = Lookup.first.second;
2319 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002320
Chris Lattnerdff59112005-10-04 18:47:09 +00002321
Chris Lattner8760ec72005-10-04 01:17:50 +00002322 // Fill values with the modified operands of the constant struct. Also,
2323 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002324 bool isAllZeros = false;
2325 if (!ToC->isNullValue()) {
2326 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2327 Values.push_back(cast<Constant>(O->get()));
2328 } else {
2329 isAllZeros = true;
2330 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2331 Constant *Val = cast<Constant>(O->get());
2332 Values.push_back(Val);
2333 if (isAllZeros) isAllZeros = Val->isNullValue();
2334 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002335 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002336 Values[OperandToUpdate] = ToC;
2337
Chris Lattner8760ec72005-10-04 01:17:50 +00002338 Constant *Replacement = 0;
2339 if (isAllZeros) {
2340 Replacement = ConstantAggregateZero::get(getType());
2341 } else {
2342 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002343 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002344 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002345 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002346 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002347
2348 if (Exists) {
2349 Replacement = I->second;
2350 } else {
2351 // Okay, the new shape doesn't exist in the system yet. Instead of
2352 // creating a new constant struct, inserting it, replaceallusesof'ing the
2353 // old with the new, then deleting the old... just update the current one
2354 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002355 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002356
Chris Lattnerdff59112005-10-04 18:47:09 +00002357 // Update to the new value.
2358 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002359 return;
2360 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002361 }
2362
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002363 assert(Replacement != this && "I didn't contain From!");
2364
Chris Lattner7a1450d2005-10-04 18:13:04 +00002365 // Everyone using this now uses the replacement.
2366 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002367
2368 // Delete the old constant!
2369 destroyConstant();
2370}
2371
Reid Spencerd84d35b2007-02-15 02:26:10 +00002372void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002373 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002374 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2375
2376 std::vector<Constant*> Values;
2377 Values.reserve(getNumOperands()); // Build replacement array...
2378 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2379 Constant *Val = getOperand(i);
2380 if (Val == From) Val = cast<Constant>(To);
2381 Values.push_back(Val);
2382 }
2383
Reid Spencerd84d35b2007-02-15 02:26:10 +00002384 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002385 assert(Replacement != this && "I didn't contain From!");
2386
Chris Lattner7a1450d2005-10-04 18:13:04 +00002387 // Everyone using this now uses the replacement.
2388 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002389
2390 // Delete the old constant!
2391 destroyConstant();
2392}
2393
2394void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002395 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002396 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2397 Constant *To = cast<Constant>(ToV);
2398
2399 Constant *Replacement = 0;
2400 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002401 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002402 Constant *Pointer = getOperand(0);
2403 Indices.reserve(getNumOperands()-1);
2404 if (Pointer == From) Pointer = To;
2405
2406 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2407 Constant *Val = getOperand(i);
2408 if (Val == From) Val = To;
2409 Indices.push_back(Val);
2410 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002411 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2412 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002413 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002414 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002415 if (Agg == From) Agg = To;
2416
Dan Gohman1ecaf452008-05-31 00:58:22 +00002417 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002418 Replacement = ConstantExpr::getExtractValue(Agg,
2419 &Indices[0], Indices.size());
2420 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002421 Constant *Agg = getOperand(0);
2422 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002423 if (Agg == From) Agg = To;
2424 if (Val == From) Val = To;
2425
Dan Gohman1ecaf452008-05-31 00:58:22 +00002426 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002427 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2428 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002430 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002432 } else if (getOpcode() == Instruction::Select) {
2433 Constant *C1 = getOperand(0);
2434 Constant *C2 = getOperand(1);
2435 Constant *C3 = getOperand(2);
2436 if (C1 == From) C1 = To;
2437 if (C2 == From) C2 = To;
2438 if (C3 == From) C3 = To;
2439 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002440 } else if (getOpcode() == Instruction::ExtractElement) {
2441 Constant *C1 = getOperand(0);
2442 Constant *C2 = getOperand(1);
2443 if (C1 == From) C1 = To;
2444 if (C2 == From) C2 = To;
2445 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002446 } else if (getOpcode() == Instruction::InsertElement) {
2447 Constant *C1 = getOperand(0);
2448 Constant *C2 = getOperand(1);
2449 Constant *C3 = getOperand(1);
2450 if (C1 == From) C1 = To;
2451 if (C2 == From) C2 = To;
2452 if (C3 == From) C3 = To;
2453 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2454 } else if (getOpcode() == Instruction::ShuffleVector) {
2455 Constant *C1 = getOperand(0);
2456 Constant *C2 = getOperand(1);
2457 Constant *C3 = getOperand(2);
2458 if (C1 == From) C1 = To;
2459 if (C2 == From) C2 = To;
2460 if (C3 == From) C3 = To;
2461 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002462 } else if (isCompare()) {
2463 Constant *C1 = getOperand(0);
2464 Constant *C2 = getOperand(1);
2465 if (C1 == From) C1 = To;
2466 if (C2 == From) C2 = To;
2467 if (getOpcode() == Instruction::ICmp)
2468 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002469 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002470 assert(getOpcode() == Instruction::FCmp);
2471 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002472 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002473 } else if (getNumOperands() == 2) {
2474 Constant *C1 = getOperand(0);
2475 Constant *C2 = getOperand(1);
2476 if (C1 == From) C1 = To;
2477 if (C2 == From) C2 = To;
2478 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2479 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002480 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002481 return;
2482 }
2483
2484 assert(Replacement != this && "I didn't contain From!");
2485
Chris Lattner7a1450d2005-10-04 18:13:04 +00002486 // Everyone using this now uses the replacement.
2487 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002488
2489 // Delete the old constant!
2490 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002491}
Nick Lewycky49f89192009-04-04 07:22:01 +00002492
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002493void MDNode::replaceElement(Value *From, Value *To) {
2494 SmallVector<Value*, 4> Values;
2495 Values.reserve(getNumElements()); // Build replacement array...
2496 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2497 Value *Val = getElement(i);
2498 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002499 Values.push_back(Val);
2500 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002501
Owen Anderson4118dde2009-07-16 23:44:30 +00002502 MDNode *Replacement =
2503 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002504 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002505
Nick Lewycky49f89192009-04-04 07:22:01 +00002506 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002507
Nick Lewycky49f89192009-04-04 07:22:01 +00002508 destroyConstant();
2509}