blob: 81f544b0e827b0fa45b94129f937215f6f5533e7 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000021#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000027#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000028#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000029#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000030#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000031#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000034#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000035#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Chris Lattner2f7c9632001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000039// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
41
Owen Andersond830eb82009-06-18 19:10:19 +000042// Becomes a no-op when multithreading is disabled.
43ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000044
Chris Lattner3462ae32001-12-03 22:26:30 +000045void Constant::destroyConstantImpl() {
46 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000047 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000048 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000049 // but they don't know that. Because we only find out when the CPV is
50 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000051 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000052 //
53 while (!use_empty()) {
54 Value *V = use_back();
55#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000056 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000057 DOUT << "While deleting: " << *this
58 << "\n\nUse still stuck around after Def is destroyed: "
59 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000060#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000061 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000062 Constant *CV = cast<Constant>(V);
63 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000064
65 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000066 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 }
68
69 // Value has no outstanding references it is safe to delete it now...
70 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000071}
Chris Lattner2f7c9632001-06-06 20:29:01 +000072
Chris Lattner23dd1f62006-10-20 00:27:06 +000073/// canTrap - Return true if evaluation of this constant could trap. This is
74/// true for things like constant expressions that could divide by zero.
75bool Constant::canTrap() const {
76 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
77 // The only thing that could possibly trap are constant exprs.
78 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
79 if (!CE) return false;
80
81 // ConstantExpr traps if any operands can trap.
82 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
83 if (getOperand(i)->canTrap())
84 return true;
85
86 // Otherwise, only specific operations can trap.
87 switch (CE->getOpcode()) {
88 default:
89 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000090 case Instruction::UDiv:
91 case Instruction::SDiv:
92 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000093 case Instruction::URem:
94 case Instruction::SRem:
95 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000096 // Div and rem can trap if the RHS is not known to be non-zero.
97 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
98 return true;
99 return false;
100 }
101}
102
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000103/// ContainsRelocations - Return true if the constant value contains relocations
104/// which cannot be resolved at compile time. Kind argument is used to filter
105/// only 'interesting' sorts of relocations.
106bool Constant::ContainsRelocations(unsigned Kind) const {
107 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
108 bool isLocal = GV->hasLocalLinkage();
109 if ((Kind & Reloc::Local) && isLocal) {
110 // Global has local linkage and 'local' kind of relocations are
111 // requested
112 return true;
113 }
114
115 if ((Kind & Reloc::Global) && !isLocal) {
116 // Global has non-local linkage and 'global' kind of relocations are
117 // requested
118 return true;
119 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000120
121 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000122 }
123
Evan Chengf9e003b2007-03-08 00:59:12 +0000124 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000125 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000126 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000127
Evan Chengf9e003b2007-03-08 00:59:12 +0000128 return false;
129}
130
Chris Lattner2105d662008-07-10 00:28:11 +0000131/// getVectorElements - This method, which is only valid on constant of vector
132/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000133/// This handles breaking down a vector undef into undef elements, etc. For
134/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000135void Constant::getVectorElements(LLVMContext &Context,
136 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000137 assert(isa<VectorType>(getType()) && "Not a vector constant!");
138
139 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
140 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
141 Elts.push_back(CV->getOperand(i));
142 return;
143 }
144
145 const VectorType *VT = cast<VectorType>(getType());
146 if (isa<ConstantAggregateZero>(this)) {
147 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000148 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000149 return;
150 }
151
Chris Lattnerc5098a22008-07-14 05:10:41 +0000152 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000153 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000154 return;
155 }
156
157 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000158}
159
160
161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000163// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164//===----------------------------------------------------------------------===//
165
Reid Spencerb31bffe2007-02-26 23:54:03 +0000166ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000167 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000168 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169}
170
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000171ConstantInt *ConstantInt::TheTrueVal = 0;
172ConstantInt *ConstantInt::TheFalseVal = 0;
173
174namespace llvm {
175 void CleanupTrueFalse(void *) {
176 ConstantInt::ResetTrueFalse();
177 }
178}
179
180static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
181
182ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
183 assert(TheTrueVal == 0 && TheFalseVal == 0);
Owen Andersonb6b25302009-07-14 23:09:55 +0000184 TheTrueVal = getGlobalContext().getConstantInt(Type::Int1Ty, 1);
185 TheFalseVal = getGlobalContext().getConstantInt(Type::Int1Ty, 0);
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000186
187 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
188 TrueFalseCleanup.Register();
189
190 return WhichOne ? TheTrueVal : TheFalseVal;
191}
192
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000193//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000194// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000195//===----------------------------------------------------------------------===//
196
Daniel Dunbare974dc32009-07-17 18:33:52 +0000197#ifndef NDEBUG
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000198static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
199 if (Ty == Type::FloatTy)
200 return &APFloat::IEEEsingle;
201 if (Ty == Type::DoubleTy)
202 return &APFloat::IEEEdouble;
203 if (Ty == Type::X86_FP80Ty)
204 return &APFloat::x87DoubleExtended;
205 else if (Ty == Type::FP128Ty)
206 return &APFloat::IEEEquad;
207
208 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
209 return &APFloat::PPCDoubleDouble;
210}
Daniel Dunbare974dc32009-07-17 18:33:52 +0000211#endif
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000212
Dale Johannesend246b2c2007-08-30 00:23:21 +0000213ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
214 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000215 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
216 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217}
218
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000219bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000220 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000221}
222
Dale Johannesend246b2c2007-08-30 00:23:21 +0000223bool ConstantFP::isExactlyValue(const APFloat& V) const {
224 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000225}
226
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000227//===----------------------------------------------------------------------===//
228// ConstantXXX Classes
229//===----------------------------------------------------------------------===//
230
231
Chris Lattner3462ae32001-12-03 22:26:30 +0000232ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000233 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000234 : Constant(T, ConstantArrayVal,
235 OperandTraits<ConstantArray>::op_end(this) - V.size(),
236 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000237 assert(V.size() == T->getNumElements() &&
238 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000239 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000240 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
241 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000242 Constant *C = *I;
243 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000244 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000245 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000246 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000247 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248 }
249}
250
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000251
Chris Lattner3462ae32001-12-03 22:26:30 +0000252ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000253 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000254 : Constant(T, ConstantStructVal,
255 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
256 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000257 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000258 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000259 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000260 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
261 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000262 Constant *C = *I;
263 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000264 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000265 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000266 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000267 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000268 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000269 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270 }
271}
272
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000273
Reid Spencerd84d35b2007-02-15 02:26:10 +0000274ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000275 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000276 : Constant(T, ConstantVectorVal,
277 OperandTraits<ConstantVector>::op_end(this) - V.size(),
278 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000279 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000280 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
281 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000282 Constant *C = *I;
283 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000284 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000285 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000286 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000287 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000288 }
289}
290
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000291
Gabor Greiff6caff662008-05-10 08:32:32 +0000292namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000293// We declare several classes private to this file, so use an anonymous
294// namespace
295namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000296
Gordon Henriksen14a55692007-12-10 02:14:30 +0000297/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
298/// behind the scenes to implement unary constant exprs.
299class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000300 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000301public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000302 // allocate space for exactly one operand
303 void *operator new(size_t s) {
304 return User::operator new(s, 1);
305 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000306 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000307 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
308 Op<0>() = C;
309 }
310 /// Transparently provide more efficient getOperand methods.
311 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000312};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000313
Gordon Henriksen14a55692007-12-10 02:14:30 +0000314/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
315/// behind the scenes to implement binary constant exprs.
316class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000317 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000318public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000319 // allocate space for exactly two operands
320 void *operator new(size_t s) {
321 return User::operator new(s, 2);
322 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000323 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000325 Op<0>() = C1;
326 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000327 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000328 /// Transparently provide more efficient getOperand methods.
329 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000330};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000331
Gordon Henriksen14a55692007-12-10 02:14:30 +0000332/// SelectConstantExpr - This class is private to Constants.cpp, and is used
333/// behind the scenes to implement select constant exprs.
334class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000335 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000336public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000337 // allocate space for exactly three operands
338 void *operator new(size_t s) {
339 return User::operator new(s, 3);
340 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000341 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000343 Op<0>() = C1;
344 Op<1>() = C2;
345 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000346 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000347 /// Transparently provide more efficient getOperand methods.
348 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000349};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000350
Gordon Henriksen14a55692007-12-10 02:14:30 +0000351/// ExtractElementConstantExpr - This class is private to
352/// Constants.cpp, and is used behind the scenes to implement
353/// extractelement constant exprs.
354class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000355 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000356public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000357 // allocate space for exactly two operands
358 void *operator new(size_t s) {
359 return User::operator new(s, 2);
360 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000361 ExtractElementConstantExpr(Constant *C1, Constant *C2)
362 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000363 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000364 Op<0>() = C1;
365 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000366 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000367 /// Transparently provide more efficient getOperand methods.
368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000369};
Robert Bocchino23004482006-01-10 19:05:34 +0000370
Gordon Henriksen14a55692007-12-10 02:14:30 +0000371/// InsertElementConstantExpr - This class is private to
372/// Constants.cpp, and is used behind the scenes to implement
373/// insertelement constant exprs.
374class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000375 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000376public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000377 // allocate space for exactly three operands
378 void *operator new(size_t s) {
379 return User::operator new(s, 3);
380 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000381 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
382 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000383 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000384 Op<0>() = C1;
385 Op<1>() = C2;
386 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000387 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000388 /// Transparently provide more efficient getOperand methods.
389 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000390};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000391
Gordon Henriksen14a55692007-12-10 02:14:30 +0000392/// ShuffleVectorConstantExpr - This class is private to
393/// Constants.cpp, and is used behind the scenes to implement
394/// shufflevector constant exprs.
395class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000396 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000397public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000398 // allocate space for exactly three operands
399 void *operator new(size_t s) {
400 return User::operator new(s, 3);
401 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000402 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000403 : ConstantExpr(VectorType::get(
404 cast<VectorType>(C1->getType())->getElementType(),
405 cast<VectorType>(C3->getType())->getNumElements()),
406 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000407 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000408 Op<0>() = C1;
409 Op<1>() = C2;
410 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000411 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000412 /// Transparently provide more efficient getOperand methods.
413 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000414};
415
Dan Gohman12fce772008-05-15 19:50:34 +0000416/// ExtractValueConstantExpr - This class is private to
417/// Constants.cpp, and is used behind the scenes to implement
418/// extractvalue constant exprs.
419class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000420 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000421public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000422 // allocate space for exactly one operand
423 void *operator new(size_t s) {
424 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000425 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000426 ExtractValueConstantExpr(Constant *Agg,
427 const SmallVector<unsigned, 4> &IdxList,
428 const Type *DestTy)
429 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
430 Indices(IdxList) {
431 Op<0>() = Agg;
432 }
433
Dan Gohman7bb04502008-05-31 19:09:08 +0000434 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000435 const SmallVector<unsigned, 4> Indices;
436
Dan Gohman12fce772008-05-15 19:50:34 +0000437 /// Transparently provide more efficient getOperand methods.
438 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
439};
440
441/// InsertValueConstantExpr - This class is private to
442/// Constants.cpp, and is used behind the scenes to implement
443/// insertvalue constant exprs.
444class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000445 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000446public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000447 // allocate space for exactly one operand
448 void *operator new(size_t s) {
449 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000450 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000451 InsertValueConstantExpr(Constant *Agg, Constant *Val,
452 const SmallVector<unsigned, 4> &IdxList,
453 const Type *DestTy)
454 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
455 Indices(IdxList) {
456 Op<0>() = Agg;
457 Op<1>() = Val;
458 }
459
Dan Gohman7bb04502008-05-31 19:09:08 +0000460 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000461 const SmallVector<unsigned, 4> Indices;
462
Dan Gohman12fce772008-05-15 19:50:34 +0000463 /// Transparently provide more efficient getOperand methods.
464 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
465};
466
467
Gordon Henriksen14a55692007-12-10 02:14:30 +0000468/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
469/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000470class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000471 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000472 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000473public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000474 static GetElementPtrConstantExpr *Create(Constant *C,
475 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000476 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000477 return new(IdxList.size() + 1)
478 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000479 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000480 /// Transparently provide more efficient getOperand methods.
481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000482};
483
484// CompareConstantExpr - This class is private to Constants.cpp, and is used
485// behind the scenes to implement ICmp and FCmp constant expressions. This is
486// needed in order to store the predicate value for these instructions.
487struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000488 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
489 // allocate space for exactly two operands
490 void *operator new(size_t s) {
491 return User::operator new(s, 2);
492 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000493 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000494 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
495 unsigned short pred, Constant* LHS, Constant* RHS)
496 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000497 Op<0>() = LHS;
498 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000499 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000500 /// Transparently provide more efficient getOperand methods.
501 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000502};
503
504} // end anonymous namespace
505
Gabor Greiff6caff662008-05-10 08:32:32 +0000506template <>
507struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
508};
509DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
510
511template <>
512struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
513};
514DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
515
516template <>
517struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
518};
519DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
520
521template <>
522struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
523};
524DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
525
526template <>
527struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
528};
529DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
530
531template <>
532struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
533};
534DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
535
Dan Gohman12fce772008-05-15 19:50:34 +0000536template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000537struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000538};
Dan Gohman12fce772008-05-15 19:50:34 +0000539DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
540
541template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000542struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000543};
Dan Gohman12fce772008-05-15 19:50:34 +0000544DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
545
Gabor Greiff6caff662008-05-10 08:32:32 +0000546template <>
547struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
548};
549
550GetElementPtrConstantExpr::GetElementPtrConstantExpr
551 (Constant *C,
552 const std::vector<Constant*> &IdxList,
553 const Type *DestTy)
554 : ConstantExpr(DestTy, Instruction::GetElementPtr,
555 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
556 - (IdxList.size()+1),
557 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000558 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000559 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000560 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000561}
562
563DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
564
565
566template <>
567struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
568};
569DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
570
571
572} // End llvm namespace
573
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000574
575// Utility function for determining if a ConstantExpr is a CastOp or not. This
576// can't be inline because we don't want to #include Instruction.h into
577// Constant.h
578bool ConstantExpr::isCast() const {
579 return Instruction::isCast(getOpcode());
580}
581
Reid Spenceree3c9912006-12-04 05:19:50 +0000582bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000583 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000584}
585
Dan Gohman1ecaf452008-05-31 00:58:22 +0000586bool ConstantExpr::hasIndices() const {
587 return getOpcode() == Instruction::ExtractValue ||
588 getOpcode() == Instruction::InsertValue;
589}
590
591const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
592 if (const ExtractValueConstantExpr *EVCE =
593 dyn_cast<ExtractValueConstantExpr>(this))
594 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000595
596 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000597}
598
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000599unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000600 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000601 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000602 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000603}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000604
Chris Lattner7c1018a2006-07-14 19:37:40 +0000605/// getWithOperandReplaced - Return a constant expression identical to this
606/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000607Constant *
608ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000609 assert(OpNo < getNumOperands() && "Operand num is out of range!");
610 assert(Op->getType() == getOperand(OpNo)->getType() &&
611 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000612 if (getOperand(OpNo) == Op)
613 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000614
Chris Lattner227816342006-07-14 22:20:01 +0000615 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000616 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000617 case Instruction::Trunc:
618 case Instruction::ZExt:
619 case Instruction::SExt:
620 case Instruction::FPTrunc:
621 case Instruction::FPExt:
622 case Instruction::UIToFP:
623 case Instruction::SIToFP:
624 case Instruction::FPToUI:
625 case Instruction::FPToSI:
626 case Instruction::PtrToInt:
627 case Instruction::IntToPtr:
628 case Instruction::BitCast:
629 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000630 case Instruction::Select:
631 Op0 = (OpNo == 0) ? Op : getOperand(0);
632 Op1 = (OpNo == 1) ? Op : getOperand(1);
633 Op2 = (OpNo == 2) ? Op : getOperand(2);
634 return ConstantExpr::getSelect(Op0, Op1, Op2);
635 case Instruction::InsertElement:
636 Op0 = (OpNo == 0) ? Op : getOperand(0);
637 Op1 = (OpNo == 1) ? Op : getOperand(1);
638 Op2 = (OpNo == 2) ? Op : getOperand(2);
639 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
640 case Instruction::ExtractElement:
641 Op0 = (OpNo == 0) ? Op : getOperand(0);
642 Op1 = (OpNo == 1) ? Op : getOperand(1);
643 return ConstantExpr::getExtractElement(Op0, Op1);
644 case Instruction::ShuffleVector:
645 Op0 = (OpNo == 0) ? Op : getOperand(0);
646 Op1 = (OpNo == 1) ? Op : getOperand(1);
647 Op2 = (OpNo == 2) ? Op : getOperand(2);
648 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000649 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000650 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000651 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000652 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000653 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000654 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000655 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000656 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000657 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000658 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000659 default:
660 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000661 Op0 = (OpNo == 0) ? Op : getOperand(0);
662 Op1 = (OpNo == 1) ? Op : getOperand(1);
663 return ConstantExpr::get(getOpcode(), Op0, Op1);
664 }
665}
666
667/// getWithOperands - This returns the current constant expression with the
668/// operands replaced with the specified values. The specified operands must
669/// match count and type with the existing ones.
670Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000671getWithOperands(Constant* const *Ops, unsigned NumOps) const {
672 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000673 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000674 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000675 assert(Ops[i]->getType() == getOperand(i)->getType() &&
676 "Operand type mismatch!");
677 AnyChange |= Ops[i] != getOperand(i);
678 }
679 if (!AnyChange) // No operands changed, return self.
680 return const_cast<ConstantExpr*>(this);
681
682 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000683 case Instruction::Trunc:
684 case Instruction::ZExt:
685 case Instruction::SExt:
686 case Instruction::FPTrunc:
687 case Instruction::FPExt:
688 case Instruction::UIToFP:
689 case Instruction::SIToFP:
690 case Instruction::FPToUI:
691 case Instruction::FPToSI:
692 case Instruction::PtrToInt:
693 case Instruction::IntToPtr:
694 case Instruction::BitCast:
695 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000696 case Instruction::Select:
697 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
698 case Instruction::InsertElement:
699 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
700 case Instruction::ExtractElement:
701 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
702 case Instruction::ShuffleVector:
703 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000704 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000705 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000706 case Instruction::ICmp:
707 case Instruction::FCmp:
708 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000709 default:
710 assert(getNumOperands() == 2 && "Must be binary operator?");
711 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000712 }
713}
714
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715
716//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717// isValueValidForType implementations
718
Reid Spencere7334722006-12-19 01:28:19 +0000719bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000720 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000721 if (Ty == Type::Int1Ty)
722 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000723 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000724 return true; // always true, has to fit in largest type
725 uint64_t Max = (1ll << NumBits) - 1;
726 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000727}
728
Reid Spencere0fc4df2006-10-20 07:07:24 +0000729bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000730 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000731 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000732 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000733 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000734 return true; // always true, has to fit in largest type
735 int64_t Min = -(1ll << (NumBits-1));
736 int64_t Max = (1ll << (NumBits-1)) - 1;
737 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000738}
739
Dale Johannesend246b2c2007-08-30 00:23:21 +0000740bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
741 // convert modifies in place, so make a copy.
742 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000743 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000744 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000745 default:
746 return false; // These can't be represented as floating point!
747
Dale Johannesend246b2c2007-08-30 00:23:21 +0000748 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000749 case Type::FloatTyID: {
750 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
751 return true;
752 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
753 return !losesInfo;
754 }
755 case Type::DoubleTyID: {
756 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
757 &Val2.getSemantics() == &APFloat::IEEEdouble)
758 return true;
759 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
760 return !losesInfo;
761 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000762 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000763 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
764 &Val2.getSemantics() == &APFloat::IEEEdouble ||
765 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000766 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000767 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
768 &Val2.getSemantics() == &APFloat::IEEEdouble ||
769 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000770 case Type::PPC_FP128TyID:
771 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
772 &Val2.getSemantics() == &APFloat::IEEEdouble ||
773 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000774 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000775}
Chris Lattner9655e542001-07-20 19:16:02 +0000776
Chris Lattner49d855c2001-09-07 16:46:31 +0000777//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000778// Factory Function Implementation
779
Gabor Greiff6caff662008-05-10 08:32:32 +0000780
781// The number of operands for each ConstantCreator::create method is
782// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000783// ConstantCreator - A class that is used to create constants by
784// ValueMap*. This class should be partially specialized if there is
785// something strange that needs to be done to interface to the ctor for the
786// constant.
787//
Chris Lattner189d19f2003-11-21 20:23:48 +0000788namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000789 template<class ValType>
790 struct ConstantTraits;
791
792 template<typename T, typename Alloc>
793 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
794 static unsigned uses(const std::vector<T, Alloc>& v) {
795 return v.size();
796 }
797 };
798
Chris Lattner189d19f2003-11-21 20:23:48 +0000799 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000800 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000801 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000802 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000803 }
804 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000805
Chris Lattner189d19f2003-11-21 20:23:48 +0000806 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000807 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000808 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000809 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000810 }
811 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000812
Chris Lattner935aa922005-10-04 17:48:46 +0000813 template<class ValType, class TypeClass, class ConstantClass,
814 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000815 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000816 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000817 typedef std::pair<const Type*, ValType> MapKey;
818 typedef std::map<MapKey, Constant *> MapTy;
819 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
820 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000821 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000822 /// Map - This is the main map from the element descriptor to the Constants.
823 /// This is the primary way we avoid creating two of the same shape
824 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000825 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000826
827 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
828 /// from the constants to their element in Map. This is important for
829 /// removal of constants from the array, which would otherwise have to scan
830 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000831 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000832
Jim Laskeyc03caef2006-07-17 17:38:29 +0000833 /// AbstractTypeMap - Map for abstract type constants.
834 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000835 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000836
837 /// ValueMapLock - Mutex for this map.
838 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000839
Chris Lattner98fa07b2003-05-23 20:03:32 +0000840 public:
Owen Anderson61794042009-06-17 20:10:08 +0000841 // NOTE: This function is not locked. It is the caller's responsibility
842 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000843 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000844
845 /// InsertOrGetItem - Return an iterator for the specified element.
846 /// If the element exists in the map, the returned iterator points to the
847 /// entry and Exists=true. If not, the iterator points to the newly
848 /// inserted entry and returns Exists=false. Newly inserted entries have
849 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000850 /// NOTE: This function is not locked. It is the caller's responsibility
851 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000852 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
853 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000854 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000855 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000856 Exists = !IP.second;
857 return IP.first;
858 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000859
Chris Lattner935aa922005-10-04 17:48:46 +0000860private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000861 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000862 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000863 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000864 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
865 IMI->second->second == CP &&
866 "InverseMap corrupt!");
867 return IMI->second;
868 }
869
Jim Laskeyc03caef2006-07-17 17:38:29 +0000870 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000871 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
872 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000873 if (I == Map.end() || I->second != CP) {
874 // FIXME: This should not use a linear scan. If this gets to be a
875 // performance problem, someone should look at this.
876 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
877 /* empty */;
878 }
Chris Lattner935aa922005-10-04 17:48:46 +0000879 return I;
880 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000881
882 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
883 typename MapTy::iterator I) {
884 ConstantClass* Result =
885 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
886
887 assert(Result->getType() == Ty && "Type specified is not correct!");
888 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
889
890 if (HasLargeKey) // Remember the reverse mapping if needed.
891 InverseMap.insert(std::make_pair(Result, I));
892
893 // If the type of the constant is abstract, make sure that an entry
894 // exists for it in the AbstractTypeMap.
895 if (Ty->isAbstract()) {
896 typename AbstractTypeMapTy::iterator TI =
897 AbstractTypeMap.find(Ty);
898
899 if (TI == AbstractTypeMap.end()) {
900 // Add ourselves to the ATU list of the type.
901 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
902
903 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
904 }
905 }
906
907 return Result;
908 }
Chris Lattner935aa922005-10-04 17:48:46 +0000909public:
910
Chris Lattnerb64419a2005-10-03 22:51:37 +0000911 /// getOrCreate - Return the specified constant from the map, creating it if
912 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000913 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000914 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +0000915 MapKey Lookup(Ty, V);
916 ConstantClass* Result = 0;
917
918 typename MapTy::iterator I = Map.find(Lookup);
919 // Is it in the map?
920 if (I != Map.end())
921 Result = static_cast<ConstantClass *>(I->second);
922
923 if (!Result) {
924 // If no preexisting value, create one now...
925 Result = Create(Ty, V, I);
926 }
927
928 return Result;
929 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000930
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000931 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000932 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000933 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000934 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000935 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000936
Chris Lattner935aa922005-10-04 17:48:46 +0000937 if (HasLargeKey) // Remember the reverse mapping if needed.
938 InverseMap.erase(CP);
939
Chris Lattnerb50d1352003-10-05 00:17:43 +0000940 // Now that we found the entry, make sure this isn't the entry that
941 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000942 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000943 if (Ty->isAbstract()) {
944 assert(AbstractTypeMap.count(Ty) &&
945 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000946 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000947 if (ATMEntryIt == I) {
948 // Yes, we are removing the representative entry for this type.
949 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000950 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000951
Chris Lattnerb50d1352003-10-05 00:17:43 +0000952 // First check the entry before this one...
953 if (TmpIt != Map.begin()) {
954 --TmpIt;
955 if (TmpIt->first.first != Ty) // Not the same type, move back...
956 ++TmpIt;
957 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000958
Chris Lattnerb50d1352003-10-05 00:17:43 +0000959 // If we didn't find the same type, try to move forward...
960 if (TmpIt == ATMEntryIt) {
961 ++TmpIt;
962 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
963 --TmpIt; // No entry afterwards with the same type
964 }
965
966 // If there is another entry in the map of the same abstract type,
967 // update the AbstractTypeMap entry now.
968 if (TmpIt != ATMEntryIt) {
969 ATMEntryIt = TmpIt;
970 } else {
971 // Otherwise, we are removing the last instance of this type
972 // from the table. Remove from the ATM, and from user list.
973 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
974 AbstractTypeMap.erase(Ty);
975 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000976 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000977 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000978
Chris Lattnerb50d1352003-10-05 00:17:43 +0000979 Map.erase(I);
980 }
981
Chris Lattner3b793c62005-10-04 21:35:50 +0000982
983 /// MoveConstantToNewSlot - If we are about to change C to be the element
984 /// specified by I, update our internal data structures to reflect this
985 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +0000986 /// NOTE: This function is not locked. It is the responsibility of the
987 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000988 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000989 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000990 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000991 assert(OldI != Map.end() && "Constant not found in constant table!");
992 assert(OldI->second == C && "Didn't find correct element?");
993
994 // If this constant is the representative element for its abstract type,
995 // update the AbstractTypeMap so that the representative element is I.
996 if (C->getType()->isAbstract()) {
997 typename AbstractTypeMapTy::iterator ATI =
998 AbstractTypeMap.find(C->getType());
999 assert(ATI != AbstractTypeMap.end() &&
1000 "Abstract type not in AbstractTypeMap?");
1001 if (ATI->second == OldI)
1002 ATI->second = I;
1003 }
1004
1005 // Remove the old entry from the map.
1006 Map.erase(OldI);
1007
1008 // Update the inverse map so that we know that this constant is now
1009 // located at descriptor I.
1010 if (HasLargeKey) {
1011 assert(I->second == C && "Bad inversemap entry!");
1012 InverseMap[C] = I;
1013 }
1014 }
1015
Chris Lattnerb50d1352003-10-05 00:17:43 +00001016 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001017 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001018 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001019 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001020
1021 assert(I != AbstractTypeMap.end() &&
1022 "Abstract type not in AbstractTypeMap?");
1023
1024 // Convert a constant at a time until the last one is gone. The last one
1025 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1026 // eliminated eventually.
1027 do {
1028 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001029 TypeClass>::convert(
1030 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001031 cast<TypeClass>(NewTy));
1032
Jim Laskeyc03caef2006-07-17 17:38:29 +00001033 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001034 } while (I != AbstractTypeMap.end());
1035 }
1036
1037 // If the type became concrete without being refined to any other existing
1038 // type, we just remove ourselves from the ATU list.
1039 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001040 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001041 }
1042
1043 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001044 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001045 }
1046 };
1047}
1048
Chris Lattnera84df0a22006-09-28 23:36:21 +00001049
Chris Lattner28173502007-02-20 06:11:36 +00001050
Chris Lattner9fba3da2004-02-15 05:53:04 +00001051//---- ConstantAggregateZero::get() implementation...
1052//
1053namespace llvm {
1054 // ConstantAggregateZero does not take extra "value" argument...
1055 template<class ValType>
1056 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1057 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1058 return new ConstantAggregateZero(Ty);
1059 }
1060 };
1061
1062 template<>
1063 struct ConvertConstantType<ConstantAggregateZero, Type> {
1064 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1065 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001066 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001067 assert(New != OldC && "Didn't replace constant??");
1068 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001069 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001070 }
1071 };
1072}
1073
Chris Lattner69edc982006-09-28 00:35:06 +00001074static ManagedStatic<ValueMap<char, Type,
1075 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001076
Chris Lattner3e650af2004-08-04 04:48:01 +00001077static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1078
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001079ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001080 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001081 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001082
1083 // Implicitly locked.
1084 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001085}
1086
Dan Gohman92b551b2009-03-03 02:55:14 +00001087/// destroyConstant - Remove the constant from the constant table...
1088///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001089void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001090 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001091 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001092 destroyConstantImpl();
1093}
1094
Chris Lattner3462ae32001-12-03 22:26:30 +00001095//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001096//
Chris Lattner189d19f2003-11-21 20:23:48 +00001097namespace llvm {
1098 template<>
1099 struct ConvertConstantType<ConstantArray, ArrayType> {
1100 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1101 // Make everyone now use a constant of the new type...
1102 std::vector<Constant*> C;
1103 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1104 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001105 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001106 assert(New != OldC && "Didn't replace constant??");
1107 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001108 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001109 }
1110 };
1111}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001112
Chris Lattner3e650af2004-08-04 04:48:01 +00001113static std::vector<Constant*> getValType(ConstantArray *CA) {
1114 std::vector<Constant*> Elements;
1115 Elements.reserve(CA->getNumOperands());
1116 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1117 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1118 return Elements;
1119}
1120
Chris Lattnerb64419a2005-10-03 22:51:37 +00001121typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001122 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001123static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001124
Chris Lattner015e8212004-02-15 04:14:47 +00001125Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001126 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001127 // If this is an all-zero array, return a ConstantAggregateZero object
1128 if (!V.empty()) {
1129 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001130 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001131 // Implicitly locked.
1132 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001133 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001134 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001135 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001136 // Implicitly locked.
1137 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001138 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001139 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001140
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001141 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001142}
1143
Dan Gohman92b551b2009-03-03 02:55:14 +00001144/// destroyConstant - Remove the constant from the constant table...
1145///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001146void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001147 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001148 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001149 destroyConstantImpl();
1150}
1151
Reid Spencer2546b762007-01-26 07:37:34 +00001152/// isString - This method returns true if the array is an array of i8, and
1153/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001154bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001155 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001156 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001157 return false;
1158 // Check the elements to make sure they are all integers, not constant
1159 // expressions.
1160 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1161 if (!isa<ConstantInt>(getOperand(i)))
1162 return false;
1163 return true;
1164}
1165
Evan Cheng3763c5b2006-10-26 19:15:05 +00001166/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001167/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001168/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001169bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001170 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001171 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001172 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001173
Evan Chenge974da62006-10-26 21:48:03 +00001174 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001175 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001176 return false;
1177 // Other elements must be non-null integers.
1178 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1179 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001180 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001181 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001182 return false;
1183 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001184 return true;
1185}
1186
1187
Dan Gohman92b551b2009-03-03 02:55:14 +00001188/// getAsString - If the sub-element type of this array is i8
1189/// then this method converts the array to an std::string and returns it.
1190/// Otherwise, it asserts out.
1191///
Chris Lattner81fabb02002-08-26 17:53:56 +00001192std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001193 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001194 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001195 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001196 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001197 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001198 return Result;
1199}
1200
1201
Chris Lattner3462ae32001-12-03 22:26:30 +00001202//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001203//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001204
Chris Lattner189d19f2003-11-21 20:23:48 +00001205namespace llvm {
1206 template<>
1207 struct ConvertConstantType<ConstantStruct, StructType> {
1208 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1209 // Make everyone now use a constant of the new type...
1210 std::vector<Constant*> C;
1211 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1212 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001213 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001214 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001215
Chris Lattner189d19f2003-11-21 20:23:48 +00001216 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001217 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001218 }
1219 };
1220}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001221
Chris Lattner8760ec72005-10-04 01:17:50 +00001222typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001223 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001224static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001225
Chris Lattner3e650af2004-08-04 04:48:01 +00001226static std::vector<Constant*> getValType(ConstantStruct *CS) {
1227 std::vector<Constant*> Elements;
1228 Elements.reserve(CS->getNumOperands());
1229 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1230 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1231 return Elements;
1232}
1233
Chris Lattner015e8212004-02-15 04:14:47 +00001234Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001235 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001236 // Create a ConstantAggregateZero value if all elements are zeros...
1237 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001238 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001239 // Implicitly locked.
1240 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001241
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001242 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001243}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001244
Chris Lattnerd7a73302001-10-13 06:57:33 +00001245// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001246//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001247void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001248 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001249 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001250 destroyConstantImpl();
1251}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001252
Reid Spencerd84d35b2007-02-15 02:26:10 +00001253//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001254//
1255namespace llvm {
1256 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001257 struct ConvertConstantType<ConstantVector, VectorType> {
1258 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001259 // Make everyone now use a constant of the new type...
1260 std::vector<Constant*> C;
1261 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1262 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001263 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001264 assert(New != OldC && "Didn't replace constant??");
1265 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001266 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001267 }
1268 };
1269}
1270
Reid Spencerd84d35b2007-02-15 02:26:10 +00001271static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001272 std::vector<Constant*> Elements;
1273 Elements.reserve(CP->getNumOperands());
1274 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1275 Elements.push_back(CP->getOperand(i));
1276 return Elements;
1277}
1278
Reid Spencerd84d35b2007-02-15 02:26:10 +00001279static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001280 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001281
Reid Spencerd84d35b2007-02-15 02:26:10 +00001282Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001283 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001284 assert(!V.empty() && "Vectors can't be empty");
1285 // If this is an all-undef or alll-zero vector, return a
1286 // ConstantAggregateZero or UndefValue.
1287 Constant *C = V[0];
1288 bool isZero = C->isNullValue();
1289 bool isUndef = isa<UndefValue>(C);
1290
1291 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001292 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001293 if (V[i] != C) {
1294 isZero = isUndef = false;
1295 break;
1296 }
Brian Gaeke02209042004-08-20 06:00:58 +00001297 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001298
1299 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001300 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001301 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001302 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001303
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001304 // Implicitly locked.
1305 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001306}
1307
Brian Gaeke02209042004-08-20 06:00:58 +00001308// destroyConstant - Remove the constant from the constant table...
1309//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001310void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001311 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001312 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001313 destroyConstantImpl();
1314}
1315
Dan Gohman30978072007-05-24 14:36:04 +00001316/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001317/// is set to all ones.
1318/// @returns true iff this constant's emements are all set to all ones.
1319/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001320bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001321 // Check out first element.
1322 const Constant *Elt = getOperand(0);
1323 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1324 if (!CI || !CI->isAllOnesValue()) return false;
1325 // Then make sure all remaining elements point to the same value.
1326 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1327 if (getOperand(I) != Elt) return false;
1328 }
1329 return true;
1330}
1331
Dan Gohman07159202007-10-17 17:51:30 +00001332/// getSplatValue - If this is a splat constant, where all of the
1333/// elements have the same value, return that value. Otherwise return null.
1334Constant *ConstantVector::getSplatValue() {
1335 // Check out first element.
1336 Constant *Elt = getOperand(0);
1337 // Then make sure all remaining elements point to the same value.
1338 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1339 if (getOperand(I) != Elt) return 0;
1340 return Elt;
1341}
1342
Chris Lattner3462ae32001-12-03 22:26:30 +00001343//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001344//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001345
Chris Lattner189d19f2003-11-21 20:23:48 +00001346namespace llvm {
1347 // ConstantPointerNull does not take extra "value" argument...
1348 template<class ValType>
1349 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1350 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1351 return new ConstantPointerNull(Ty);
1352 }
1353 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001354
Chris Lattner189d19f2003-11-21 20:23:48 +00001355 template<>
1356 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1357 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1358 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001359 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001360 assert(New != OldC && "Didn't replace constant??");
1361 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001362 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001363 }
1364 };
1365}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001366
Chris Lattner69edc982006-09-28 00:35:06 +00001367static ManagedStatic<ValueMap<char, PointerType,
1368 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001369
Chris Lattner3e650af2004-08-04 04:48:01 +00001370static char getValType(ConstantPointerNull *) {
1371 return 0;
1372}
1373
1374
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001375ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001376 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001377 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001378}
1379
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001380// destroyConstant - Remove the constant from the constant table...
1381//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001382void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001383 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001384 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001385 destroyConstantImpl();
1386}
1387
1388
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001389//---- UndefValue::get() implementation...
1390//
1391
1392namespace llvm {
1393 // UndefValue does not take extra "value" argument...
1394 template<class ValType>
1395 struct ConstantCreator<UndefValue, Type, ValType> {
1396 static UndefValue *create(const Type *Ty, const ValType &V) {
1397 return new UndefValue(Ty);
1398 }
1399 };
1400
1401 template<>
1402 struct ConvertConstantType<UndefValue, Type> {
1403 static void convert(UndefValue *OldC, const Type *NewTy) {
1404 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001405 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001406 assert(New != OldC && "Didn't replace constant??");
1407 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001408 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001409 }
1410 };
1411}
1412
Chris Lattner69edc982006-09-28 00:35:06 +00001413static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001414
1415static char getValType(UndefValue *) {
1416 return 0;
1417}
1418
1419
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001420UndefValue *UndefValue::get(const Type *Ty) {
1421 // Implicitly locked.
1422 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001423}
1424
1425// destroyConstant - Remove the constant from the constant table.
1426//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001427void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001428 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001429 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001430 destroyConstantImpl();
1431}
1432
Nick Lewycky49f89192009-04-04 07:22:01 +00001433//---- MDString::get() implementation
1434//
1435
1436MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001437 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001438 StrBegin(begin), StrEnd(end) {}
1439
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001440void MDString::destroyConstant() {
Owen Anderson69ab4162009-07-16 22:11:26 +00001441 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001442 destroyConstantImpl();
1443}
1444
1445//---- MDNode::get() implementation
1446//
1447
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001448MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001449 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001450 for (unsigned i = 0; i != NumVals; ++i)
1451 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001452}
1453
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001454void MDNode::Profile(FoldingSetNodeID &ID) const {
1455 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001456 ID.AddPointer(*I);
1457}
1458
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001459void MDNode::destroyConstant() {
Owen Anderson4118dde2009-07-16 23:44:30 +00001460 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001461 destroyConstantImpl();
1462}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001463
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001464//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001465//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001466
Dan Gohmand78c4002008-05-13 00:00:25 +00001467namespace {
1468
Reid Spenceree3c9912006-12-04 05:19:50 +00001469struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001470 typedef SmallVector<unsigned, 4> IndexList;
1471
1472 ExprMapKeyType(unsigned opc,
1473 const std::vector<Constant*> &ops,
1474 unsigned short pred = 0,
1475 const IndexList &inds = IndexList())
1476 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001477 uint16_t opcode;
1478 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001479 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001480 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001481 bool operator==(const ExprMapKeyType& that) const {
1482 return this->opcode == that.opcode &&
1483 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001484 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001485 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001486 }
1487 bool operator<(const ExprMapKeyType & that) const {
1488 return this->opcode < that.opcode ||
1489 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1490 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001491 this->operands < that.operands) ||
1492 (this->opcode == that.opcode && this->predicate == that.predicate &&
1493 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001494 }
1495
1496 bool operator!=(const ExprMapKeyType& that) const {
1497 return !(*this == that);
1498 }
1499};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001500
Dan Gohmand78c4002008-05-13 00:00:25 +00001501}
1502
Chris Lattner189d19f2003-11-21 20:23:48 +00001503namespace llvm {
1504 template<>
1505 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001506 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1507 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001508 if (Instruction::isCast(V.opcode))
1509 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1510 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001511 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001512 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1513 if (V.opcode == Instruction::Select)
1514 return new SelectConstantExpr(V.operands[0], V.operands[1],
1515 V.operands[2]);
1516 if (V.opcode == Instruction::ExtractElement)
1517 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1518 if (V.opcode == Instruction::InsertElement)
1519 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1520 V.operands[2]);
1521 if (V.opcode == Instruction::ShuffleVector)
1522 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1523 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001524 if (V.opcode == Instruction::InsertValue)
1525 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1526 V.indices, Ty);
1527 if (V.opcode == Instruction::ExtractValue)
1528 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001529 if (V.opcode == Instruction::GetElementPtr) {
1530 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001531 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001532 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001533
Reid Spenceree3c9912006-12-04 05:19:50 +00001534 // The compare instructions are weird. We have to encode the predicate
1535 // value and it is combined with the instruction opcode by multiplying
1536 // the opcode by one hundred. We must decode this to get the predicate.
1537 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001538 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001539 V.operands[0], V.operands[1]);
1540 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001541 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1542 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001543 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001544 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001545 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001546 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001547
Chris Lattner189d19f2003-11-21 20:23:48 +00001548 template<>
1549 struct ConvertConstantType<ConstantExpr, Type> {
1550 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1551 Constant *New;
1552 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001553 case Instruction::Trunc:
1554 case Instruction::ZExt:
1555 case Instruction::SExt:
1556 case Instruction::FPTrunc:
1557 case Instruction::FPExt:
1558 case Instruction::UIToFP:
1559 case Instruction::SIToFP:
1560 case Instruction::FPToUI:
1561 case Instruction::FPToSI:
1562 case Instruction::PtrToInt:
1563 case Instruction::IntToPtr:
1564 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001565 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001566 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001567 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001568 case Instruction::Select:
1569 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1570 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001571 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001572 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001573 default:
1574 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001575 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001576 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001577 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001578 break;
1579 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001580 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001581 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001582 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001583 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001584 break;
1585 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001586
Chris Lattner189d19f2003-11-21 20:23:48 +00001587 assert(New != OldC && "Didn't replace constant??");
1588 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001589 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001590 }
1591 };
1592} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001593
1594
Chris Lattner3e650af2004-08-04 04:48:01 +00001595static ExprMapKeyType getValType(ConstantExpr *CE) {
1596 std::vector<Constant*> Operands;
1597 Operands.reserve(CE->getNumOperands());
1598 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1599 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001600 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001601 CE->isCompare() ? CE->getPredicate() : 0,
1602 CE->hasIndices() ?
1603 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001604}
1605
Chris Lattner69edc982006-09-28 00:35:06 +00001606static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1607 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001608
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001610/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001611static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001612 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001613 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001614 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001615 if (Constant *FC =
1616 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001617 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001618
Vikram S. Adve4c485332002-07-15 18:19:33 +00001619 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001620 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001621 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001622
Owen Anderson61794042009-06-17 20:10:08 +00001623 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001624 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001625}
Reid Spencerf37dc652006-12-05 19:14:13 +00001626
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001627Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001628 Instruction::CastOps opc = Instruction::CastOps(oc);
1629 assert(Instruction::isCast(opc) && "opcode out of range");
1630 assert(C && Ty && "Null arguments to getCast");
1631 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1632
1633 switch (opc) {
1634 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001635 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001636 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001637 case Instruction::Trunc: return getTrunc(C, Ty);
1638 case Instruction::ZExt: return getZExt(C, Ty);
1639 case Instruction::SExt: return getSExt(C, Ty);
1640 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1641 case Instruction::FPExt: return getFPExtend(C, Ty);
1642 case Instruction::UIToFP: return getUIToFP(C, Ty);
1643 case Instruction::SIToFP: return getSIToFP(C, Ty);
1644 case Instruction::FPToUI: return getFPToUI(C, Ty);
1645 case Instruction::FPToSI: return getFPToSI(C, Ty);
1646 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1647 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1648 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001649 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001650 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001651}
1652
Reid Spencer5c140882006-12-04 20:17:56 +00001653Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001654 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001655 return getCast(Instruction::BitCast, C, Ty);
1656 return getCast(Instruction::ZExt, C, Ty);
1657}
1658
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001659Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001660 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001661 return getCast(Instruction::BitCast, C, Ty);
1662 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001663}
1664
1665Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001666 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001667 return getCast(Instruction::BitCast, C, Ty);
1668 return getCast(Instruction::Trunc, C, Ty);
1669}
1670
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001671Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001672 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001673 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001674
Chris Lattner03c49532007-01-15 02:27:26 +00001675 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001676 return getCast(Instruction::PtrToInt, S, Ty);
1677 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001678}
1679
Reid Spencer56521c42006-12-12 00:51:07 +00001680Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1681 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001682 assert(C->getType()->isIntOrIntVector() &&
1683 Ty->isIntOrIntVector() && "Invalid cast");
1684 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1685 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001686 Instruction::CastOps opcode =
1687 (SrcBits == DstBits ? Instruction::BitCast :
1688 (SrcBits > DstBits ? Instruction::Trunc :
1689 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1690 return getCast(opcode, C, Ty);
1691}
1692
1693Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001694 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001695 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001696 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1697 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001698 if (SrcBits == DstBits)
1699 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001700 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001701 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001702 return getCast(opcode, C, Ty);
1703}
1704
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001705Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001706#ifndef NDEBUG
1707 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1708 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1709#endif
1710 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1711 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1712 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1713 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714 "SrcTy must be larger than DestTy for Trunc!");
1715
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001716 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001717}
1718
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001719Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001720#ifndef NDEBUG
1721 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1722 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1723#endif
1724 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1725 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1726 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1727 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001728 "SrcTy must be smaller than DestTy for SExt!");
1729
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001730 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001731}
1732
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001733Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001734#ifndef NDEBUG
1735 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1736 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1737#endif
1738 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1739 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1740 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1741 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001742 "SrcTy must be smaller than DestTy for ZExt!");
1743
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001744 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001745}
1746
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001747Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001748#ifndef NDEBUG
1749 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1750 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1751#endif
1752 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1753 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1754 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001755 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001756 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001757}
1758
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001759Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001760#ifndef NDEBUG
1761 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1762 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1763#endif
1764 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1765 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1766 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001767 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001768 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001769}
1770
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001771Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001772#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001773 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1774 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001775#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001776 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1777 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1778 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001779 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001780}
1781
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001782Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001783#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001784 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1785 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001786#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001787 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1788 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001790 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001791}
1792
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001793Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001794#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001795 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1796 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001797#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001798 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1799 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1800 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001801 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001802}
1803
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001804Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001805#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001806 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1807 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001808#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001809 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1810 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1811 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001812 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813}
1814
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001815Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001816 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001817 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001818 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001819}
1820
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001821Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001822 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001823 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001824 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001825}
1826
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001827Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001828 // BitCast implies a no-op cast of type only. No bits change. However, you
1829 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001830#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001831 const Type *SrcTy = C->getType();
1832 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001833 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001834
1835 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1836 // or nonptr->ptr). For all the other types, the cast is okay if source and
1837 // destination bit widths are identical.
1838 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1839 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001840#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001841 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001842
1843 // It is common to ask for a bitcast of a value to its own type, handle this
1844 // speedily.
1845 if (C->getType() == DstTy) return C;
1846
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001847 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001848}
1849
Chris Lattnerb50d1352003-10-05 00:17:43 +00001850Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001851 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001852 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001853 assert(Opcode >= Instruction::BinaryOpsBegin &&
1854 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001855 "Invalid opcode in binary constant expression");
1856 assert(C1->getType() == C2->getType() &&
1857 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001858
Reid Spencer542964f2007-01-11 18:21:29 +00001859 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001860 if (Constant *FC = ConstantFoldBinaryInstruction(
1861 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001862 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001863
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001864 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001865 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001866
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001867 // Implicitly locked.
1868 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001869}
1870
Reid Spencer266e42b2006-12-23 06:05:41 +00001871Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001872 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001873 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001874 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001875 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1876 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1877 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1878 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1879 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1880 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001881 return getFCmp(predicate, C1, C2);
1882
Nate Begemanc96e2e42008-07-25 17:35:37 +00001883 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1884 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1885 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1886 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001887 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001888 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001889}
1890
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001891Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001892 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1893 if (C1->getType()->isFPOrFPVector()) {
1894 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1895 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1896 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1897 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001898#ifndef NDEBUG
1899 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001900 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001901 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001902 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001903 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001904 assert(C1->getType()->isIntOrIntVector() &&
1905 "Tried to create an integer operation on a non-integer type!");
1906 break;
1907 case Instruction::FAdd:
1908 case Instruction::FSub:
1909 case Instruction::FMul:
1910 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1911 assert(C1->getType()->isFPOrFPVector() &&
1912 "Tried to create a floating-point operation on a "
1913 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001914 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001915 case Instruction::UDiv:
1916 case Instruction::SDiv:
1917 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001918 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001919 "Tried to create an arithmetic operation on a non-arithmetic type!");
1920 break;
1921 case Instruction::FDiv:
1922 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001923 assert(C1->getType()->isFPOrFPVector() &&
1924 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001925 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001926 case Instruction::URem:
1927 case Instruction::SRem:
1928 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001929 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001930 "Tried to create an arithmetic operation on a non-arithmetic type!");
1931 break;
1932 case Instruction::FRem:
1933 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001934 assert(C1->getType()->isFPOrFPVector() &&
1935 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001936 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001937 case Instruction::And:
1938 case Instruction::Or:
1939 case Instruction::Xor:
1940 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001941 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001942 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001943 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001944 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001945 case Instruction::LShr:
1946 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001947 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001948 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001949 "Tried to create a shift operation on a non-integer type!");
1950 break;
1951 default:
1952 break;
1953 }
1954#endif
1955
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001956 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001957}
1958
Reid Spencer266e42b2006-12-23 06:05:41 +00001959Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001960 Constant *C1, Constant *C2) {
1961 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001962 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001963}
1964
Chris Lattner6e415c02004-03-12 05:54:04 +00001965Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001966 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001967 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001968
1969 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001970 if (Constant *SC = ConstantFoldSelectInstruction(
1971 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001972 return SC; // Fold common cases
1973
1974 std::vector<Constant*> argVec(3, C);
1975 argVec[1] = V1;
1976 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001977 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001978
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001979 // Implicitly locked.
1980 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001981}
1982
Chris Lattnerb50d1352003-10-05 00:17:43 +00001983Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001984 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001985 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001986 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1987 Idxs+NumIdx) ==
1988 cast<PointerType>(ReqTy)->getElementType() &&
1989 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001990
Owen Anderson53a52212009-07-13 04:09:18 +00001991 if (Constant *FC = ConstantFoldGetElementPtr(
1992 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001993 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001994
Chris Lattnerb50d1352003-10-05 00:17:43 +00001995 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001996 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001997 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001998 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001999 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002000 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002001 for (unsigned i = 0; i != NumIdx; ++i)
2002 ArgVec.push_back(cast<Constant>(Idxs[i]));
2003 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002004
2005 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002006 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002007}
2008
Chris Lattner302116a2007-01-31 04:40:28 +00002009Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002010 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002011 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002012 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002013 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002014 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002015 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002016 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002017}
2018
Chris Lattner302116a2007-01-31 04:40:28 +00002019Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002020 unsigned NumIdx) {
2021 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002022}
2023
Chris Lattner302116a2007-01-31 04:40:28 +00002024
Reid Spenceree3c9912006-12-04 05:19:50 +00002025Constant *
2026ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2027 assert(LHS->getType() == RHS->getType());
2028 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2029 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2030
Owen Anderson53a52212009-07-13 04:09:18 +00002031 if (Constant *FC = ConstantFoldCompareInstruction(
2032 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002033 return FC; // Fold a few common cases...
2034
2035 // Look up the constant in the table first to ensure uniqueness
2036 std::vector<Constant*> ArgVec;
2037 ArgVec.push_back(LHS);
2038 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002039 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002040 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002041
2042 // Implicitly locked.
2043 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002044}
2045
2046Constant *
2047ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2048 assert(LHS->getType() == RHS->getType());
2049 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2050
Owen Anderson53a52212009-07-13 04:09:18 +00002051 if (Constant *FC = ConstantFoldCompareInstruction(
2052 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002053 return FC; // Fold a few common cases...
2054
2055 // Look up the constant in the table first to ensure uniqueness
2056 std::vector<Constant*> ArgVec;
2057 ArgVec.push_back(LHS);
2058 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002059 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002060 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002061
2062 // Implicitly locked.
2063 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002064}
2065
Robert Bocchino23004482006-01-10 19:05:34 +00002066Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2067 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002068 if (Constant *FC = ConstantFoldExtractElementInstruction(
2069 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002070 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002071 // Look up the constant in the table first to ensure uniqueness
2072 std::vector<Constant*> ArgVec(1, Val);
2073 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002074 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002075
2076 // Implicitly locked.
2077 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002078}
2079
2080Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002081 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002082 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002083 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002084 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002085 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002086 Val, Idx);
2087}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002088
Robert Bocchinoca27f032006-01-17 20:07:22 +00002089Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2090 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002091 if (Constant *FC = ConstantFoldInsertElementInstruction(
2092 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002093 return FC; // Fold a few common cases...
2094 // Look up the constant in the table first to ensure uniqueness
2095 std::vector<Constant*> ArgVec(1, Val);
2096 ArgVec.push_back(Elt);
2097 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002098 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002099
2100 // Implicitly locked.
2101 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002102}
2103
2104Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2105 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002106 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002107 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002108 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002109 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002110 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002111 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002112 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002113}
2114
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002115Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2116 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002117 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2118 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002119 return FC; // Fold a few common cases...
2120 // Look up the constant in the table first to ensure uniqueness
2121 std::vector<Constant*> ArgVec(1, V1);
2122 ArgVec.push_back(V2);
2123 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002124 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002125
2126 // Implicitly locked.
2127 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002128}
2129
2130Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2131 Constant *Mask) {
2132 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2133 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002134
2135 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2136 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2137 const Type *ShufTy = VectorType::get(EltTy, NElts);
2138 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002139}
2140
Dan Gohman12fce772008-05-15 19:50:34 +00002141Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2142 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002143 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002144 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2145 Idxs+NumIdx) == Val->getType() &&
2146 "insertvalue indices invalid!");
2147 assert(Agg->getType() == ReqTy &&
2148 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002149 assert(Agg->getType()->isFirstClassType() &&
2150 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002151 Constant *FC = ConstantFoldInsertValueInstruction(
2152 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002153 assert(FC && "InsertValue constant expr couldn't be folded!");
2154 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002155}
2156
2157Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002158 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002159 assert(Agg->getType()->isFirstClassType() &&
2160 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002161
Dan Gohman0752bff2008-05-23 00:36:11 +00002162 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002163#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002164 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002165 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002166#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002167 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002168 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2169}
2170
2171Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002172 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002173 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2174 Idxs+NumIdx) == ReqTy &&
2175 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002176 assert(Agg->getType()->isFirstClassType() &&
2177 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002178 Constant *FC = ConstantFoldExtractValueInstruction(
2179 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002180 assert(FC && "ExtractValue constant expr couldn't be folded!");
2181 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002182}
2183
2184Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002185 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002186 assert(Agg->getType()->isFirstClassType() &&
2187 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002188
2189 const Type *ReqTy =
2190 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2191 assert(ReqTy && "extractvalue indices invalid!");
2192 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2193}
2194
Vikram S. Adve4c485332002-07-15 18:19:33 +00002195// destroyConstant - Remove the constant from the constant table...
2196//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002197void ConstantExpr::destroyConstant() {
2198 // Implicitly locked.
2199 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002200 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002201}
2202
Chris Lattner3cd8c562002-07-30 18:54:25 +00002203const char *ConstantExpr::getOpcodeName() const {
2204 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002205}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002206
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002207//===----------------------------------------------------------------------===//
2208// replaceUsesOfWithOnConstant implementations
2209
Chris Lattner913849b2007-08-21 00:55:23 +00002210/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2211/// 'From' to be uses of 'To'. This must update the uniquing data structures
2212/// etc.
2213///
2214/// Note that we intentionally replace all uses of From with To here. Consider
2215/// a large array that uses 'From' 1000 times. By handling this case all here,
2216/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2217/// single invocation handles all 1000 uses. Handling them one at a time would
2218/// work, but would be really slow because it would have to unique each updated
2219/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002220void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002221 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002222 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002223 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002224
Jim Laskeyc03caef2006-07-17 17:38:29 +00002225 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002226 Lookup.first.first = getType();
2227 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002228
Chris Lattnerb64419a2005-10-03 22:51:37 +00002229 std::vector<Constant*> &Values = Lookup.first.second;
2230 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002231
Chris Lattner8760ec72005-10-04 01:17:50 +00002232 // Fill values with the modified operands of the constant array. Also,
2233 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002234 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002235 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002236 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002237 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2238 Constant *Val = cast<Constant>(O->get());
2239 if (Val == From) {
2240 Val = ToC;
2241 ++NumUpdated;
2242 }
2243 Values.push_back(Val);
2244 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002245 } else {
2246 isAllZeros = true;
2247 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2248 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002249 if (Val == From) {
2250 Val = ToC;
2251 ++NumUpdated;
2252 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002253 Values.push_back(Val);
2254 if (isAllZeros) isAllZeros = Val->isNullValue();
2255 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002256 }
2257
Chris Lattnerb64419a2005-10-03 22:51:37 +00002258 Constant *Replacement = 0;
2259 if (isAllZeros) {
2260 Replacement = ConstantAggregateZero::get(getType());
2261 } else {
2262 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002263 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002264 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002265 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002266 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002267
2268 if (Exists) {
2269 Replacement = I->second;
2270 } else {
2271 // Okay, the new shape doesn't exist in the system yet. Instead of
2272 // creating a new constant array, inserting it, replaceallusesof'ing the
2273 // old with the new, then deleting the old... just update the current one
2274 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002275 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002276
Chris Lattner913849b2007-08-21 00:55:23 +00002277 // Update to the new value. Optimize for the case when we have a single
2278 // operand that we're changing, but handle bulk updates efficiently.
2279 if (NumUpdated == 1) {
2280 unsigned OperandToUpdate = U-OperandList;
2281 assert(getOperand(OperandToUpdate) == From &&
2282 "ReplaceAllUsesWith broken!");
2283 setOperand(OperandToUpdate, ToC);
2284 } else {
2285 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2286 if (getOperand(i) == From)
2287 setOperand(i, ToC);
2288 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002289 return;
2290 }
2291 }
2292
2293 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002294 assert(Replacement != this && "I didn't contain From!");
2295
Chris Lattner7a1450d2005-10-04 18:13:04 +00002296 // Everyone using this now uses the replacement.
2297 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002298
2299 // Delete the old constant!
2300 destroyConstant();
2301}
2302
2303void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002304 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002305 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002306 Constant *ToC = cast<Constant>(To);
2307
Chris Lattnerdff59112005-10-04 18:47:09 +00002308 unsigned OperandToUpdate = U-OperandList;
2309 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2310
Jim Laskeyc03caef2006-07-17 17:38:29 +00002311 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002312 Lookup.first.first = getType();
2313 Lookup.second = this;
2314 std::vector<Constant*> &Values = Lookup.first.second;
2315 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002316
Chris Lattnerdff59112005-10-04 18:47:09 +00002317
Chris Lattner8760ec72005-10-04 01:17:50 +00002318 // Fill values with the modified operands of the constant struct. Also,
2319 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002320 bool isAllZeros = false;
2321 if (!ToC->isNullValue()) {
2322 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2323 Values.push_back(cast<Constant>(O->get()));
2324 } else {
2325 isAllZeros = true;
2326 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2327 Constant *Val = cast<Constant>(O->get());
2328 Values.push_back(Val);
2329 if (isAllZeros) isAllZeros = Val->isNullValue();
2330 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002331 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002332 Values[OperandToUpdate] = ToC;
2333
Chris Lattner8760ec72005-10-04 01:17:50 +00002334 Constant *Replacement = 0;
2335 if (isAllZeros) {
2336 Replacement = ConstantAggregateZero::get(getType());
2337 } else {
2338 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002339 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002340 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002341 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002342 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002343
2344 if (Exists) {
2345 Replacement = I->second;
2346 } else {
2347 // Okay, the new shape doesn't exist in the system yet. Instead of
2348 // creating a new constant struct, inserting it, replaceallusesof'ing the
2349 // old with the new, then deleting the old... just update the current one
2350 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002351 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002352
Chris Lattnerdff59112005-10-04 18:47:09 +00002353 // Update to the new value.
2354 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002355 return;
2356 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002357 }
2358
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002359 assert(Replacement != this && "I didn't contain From!");
2360
Chris Lattner7a1450d2005-10-04 18:13:04 +00002361 // Everyone using this now uses the replacement.
2362 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002363
2364 // Delete the old constant!
2365 destroyConstant();
2366}
2367
Reid Spencerd84d35b2007-02-15 02:26:10 +00002368void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002369 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002370 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2371
2372 std::vector<Constant*> Values;
2373 Values.reserve(getNumOperands()); // Build replacement array...
2374 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2375 Constant *Val = getOperand(i);
2376 if (Val == From) Val = cast<Constant>(To);
2377 Values.push_back(Val);
2378 }
2379
Reid Spencerd84d35b2007-02-15 02:26:10 +00002380 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002381 assert(Replacement != this && "I didn't contain From!");
2382
Chris Lattner7a1450d2005-10-04 18:13:04 +00002383 // Everyone using this now uses the replacement.
2384 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002385
2386 // Delete the old constant!
2387 destroyConstant();
2388}
2389
2390void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002391 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002392 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2393 Constant *To = cast<Constant>(ToV);
2394
2395 Constant *Replacement = 0;
2396 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002397 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002398 Constant *Pointer = getOperand(0);
2399 Indices.reserve(getNumOperands()-1);
2400 if (Pointer == From) Pointer = To;
2401
2402 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2403 Constant *Val = getOperand(i);
2404 if (Val == From) Val = To;
2405 Indices.push_back(Val);
2406 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002407 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2408 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002409 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002410 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002411 if (Agg == From) Agg = To;
2412
Dan Gohman1ecaf452008-05-31 00:58:22 +00002413 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002414 Replacement = ConstantExpr::getExtractValue(Agg,
2415 &Indices[0], Indices.size());
2416 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002417 Constant *Agg = getOperand(0);
2418 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002419 if (Agg == From) Agg = To;
2420 if (Val == From) Val = To;
2421
Dan Gohman1ecaf452008-05-31 00:58:22 +00002422 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002423 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2424 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002426 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002428 } else if (getOpcode() == Instruction::Select) {
2429 Constant *C1 = getOperand(0);
2430 Constant *C2 = getOperand(1);
2431 Constant *C3 = getOperand(2);
2432 if (C1 == From) C1 = To;
2433 if (C2 == From) C2 = To;
2434 if (C3 == From) C3 = To;
2435 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002436 } else if (getOpcode() == Instruction::ExtractElement) {
2437 Constant *C1 = getOperand(0);
2438 Constant *C2 = getOperand(1);
2439 if (C1 == From) C1 = To;
2440 if (C2 == From) C2 = To;
2441 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002442 } else if (getOpcode() == Instruction::InsertElement) {
2443 Constant *C1 = getOperand(0);
2444 Constant *C2 = getOperand(1);
2445 Constant *C3 = getOperand(1);
2446 if (C1 == From) C1 = To;
2447 if (C2 == From) C2 = To;
2448 if (C3 == From) C3 = To;
2449 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2450 } else if (getOpcode() == Instruction::ShuffleVector) {
2451 Constant *C1 = getOperand(0);
2452 Constant *C2 = getOperand(1);
2453 Constant *C3 = getOperand(2);
2454 if (C1 == From) C1 = To;
2455 if (C2 == From) C2 = To;
2456 if (C3 == From) C3 = To;
2457 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002458 } else if (isCompare()) {
2459 Constant *C1 = getOperand(0);
2460 Constant *C2 = getOperand(1);
2461 if (C1 == From) C1 = To;
2462 if (C2 == From) C2 = To;
2463 if (getOpcode() == Instruction::ICmp)
2464 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002465 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002466 assert(getOpcode() == Instruction::FCmp);
2467 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002468 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002469 } else if (getNumOperands() == 2) {
2470 Constant *C1 = getOperand(0);
2471 Constant *C2 = getOperand(1);
2472 if (C1 == From) C1 = To;
2473 if (C2 == From) C2 = To;
2474 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2475 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002476 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002477 return;
2478 }
2479
2480 assert(Replacement != this && "I didn't contain From!");
2481
Chris Lattner7a1450d2005-10-04 18:13:04 +00002482 // Everyone using this now uses the replacement.
2483 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002484
2485 // Delete the old constant!
2486 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002487}
Nick Lewycky49f89192009-04-04 07:22:01 +00002488
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002489void MDNode::replaceElement(Value *From, Value *To) {
2490 SmallVector<Value*, 4> Values;
2491 Values.reserve(getNumElements()); // Build replacement array...
2492 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2493 Value *Val = getElement(i);
2494 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002495 Values.push_back(Val);
2496 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002497
Owen Anderson4118dde2009-07-16 23:44:30 +00002498 MDNode *Replacement =
2499 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002500 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002501
Nick Lewycky49f89192009-04-04 07:22:01 +00002502 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002503
Nick Lewycky49f89192009-04-04 07:22:01 +00002504 destroyConstant();
2505}