blob: 7631e3cdb2d2fa77a6b6aaa983696da6bccc5bac [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
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000197static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
198 if (Ty == Type::FloatTy)
199 return &APFloat::IEEEsingle;
200 if (Ty == Type::DoubleTy)
201 return &APFloat::IEEEdouble;
202 if (Ty == Type::X86_FP80Ty)
203 return &APFloat::x87DoubleExtended;
204 else if (Ty == Type::FP128Ty)
205 return &APFloat::IEEEquad;
206
207 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
208 return &APFloat::PPCDoubleDouble;
209}
210
Dale Johannesend246b2c2007-08-30 00:23:21 +0000211ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
212 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000213 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
214 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215}
216
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000217bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000218 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000219}
220
Dale Johannesend246b2c2007-08-30 00:23:21 +0000221bool ConstantFP::isExactlyValue(const APFloat& V) const {
222 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000223}
224
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000225//===----------------------------------------------------------------------===//
226// ConstantXXX Classes
227//===----------------------------------------------------------------------===//
228
229
Chris Lattner3462ae32001-12-03 22:26:30 +0000230ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000231 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000232 : Constant(T, ConstantArrayVal,
233 OperandTraits<ConstantArray>::op_end(this) - V.size(),
234 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000235 assert(V.size() == T->getNumElements() &&
236 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000237 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000238 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
239 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000240 Constant *C = *I;
241 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000242 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000243 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000244 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000245 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000246 }
247}
248
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000249
Chris Lattner3462ae32001-12-03 22:26:30 +0000250ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000251 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000252 : Constant(T, ConstantStructVal,
253 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
254 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000255 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000256 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000257 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000258 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
259 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000260 Constant *C = *I;
261 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000262 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000263 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000264 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000265 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000266 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000267 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268 }
269}
270
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000271
Reid Spencerd84d35b2007-02-15 02:26:10 +0000272ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000273 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000274 : Constant(T, ConstantVectorVal,
275 OperandTraits<ConstantVector>::op_end(this) - V.size(),
276 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000277 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000278 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
279 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000280 Constant *C = *I;
281 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000282 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000283 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000284 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000285 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000286 }
287}
288
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000289
Gabor Greiff6caff662008-05-10 08:32:32 +0000290namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000291// We declare several classes private to this file, so use an anonymous
292// namespace
293namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000294
Gordon Henriksen14a55692007-12-10 02:14:30 +0000295/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
296/// behind the scenes to implement unary constant exprs.
297class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000298 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000299public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000300 // allocate space for exactly one operand
301 void *operator new(size_t s) {
302 return User::operator new(s, 1);
303 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000304 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000305 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
306 Op<0>() = C;
307 }
308 /// Transparently provide more efficient getOperand methods.
309 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000310};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000311
Gordon Henriksen14a55692007-12-10 02:14:30 +0000312/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
313/// behind the scenes to implement binary constant exprs.
314class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000315 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000316public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000317 // allocate space for exactly two operands
318 void *operator new(size_t s) {
319 return User::operator new(s, 2);
320 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000321 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000322 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000323 Op<0>() = C1;
324 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000325 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000326 /// Transparently provide more efficient getOperand methods.
327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000328};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000329
Gordon Henriksen14a55692007-12-10 02:14:30 +0000330/// SelectConstantExpr - This class is private to Constants.cpp, and is used
331/// behind the scenes to implement select constant exprs.
332class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000333 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000334public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000335 // allocate space for exactly three operands
336 void *operator new(size_t s) {
337 return User::operator new(s, 3);
338 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000339 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000341 Op<0>() = C1;
342 Op<1>() = C2;
343 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000344 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000345 /// Transparently provide more efficient getOperand methods.
346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000347};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000348
Gordon Henriksen14a55692007-12-10 02:14:30 +0000349/// ExtractElementConstantExpr - This class is private to
350/// Constants.cpp, and is used behind the scenes to implement
351/// extractelement constant exprs.
352class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000353 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000354public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000355 // allocate space for exactly two operands
356 void *operator new(size_t s) {
357 return User::operator new(s, 2);
358 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000359 ExtractElementConstantExpr(Constant *C1, Constant *C2)
360 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000361 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000362 Op<0>() = C1;
363 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000364 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000365 /// Transparently provide more efficient getOperand methods.
366 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000367};
Robert Bocchino23004482006-01-10 19:05:34 +0000368
Gordon Henriksen14a55692007-12-10 02:14:30 +0000369/// InsertElementConstantExpr - This class is private to
370/// Constants.cpp, and is used behind the scenes to implement
371/// insertelement constant exprs.
372class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000373 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000374public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000375 // allocate space for exactly three operands
376 void *operator new(size_t s) {
377 return User::operator new(s, 3);
378 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000379 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
380 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000381 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000382 Op<0>() = C1;
383 Op<1>() = C2;
384 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000385 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000386 /// Transparently provide more efficient getOperand methods.
387 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000388};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000389
Gordon Henriksen14a55692007-12-10 02:14:30 +0000390/// ShuffleVectorConstantExpr - This class is private to
391/// Constants.cpp, and is used behind the scenes to implement
392/// shufflevector constant exprs.
393class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000394 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000395public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000396 // allocate space for exactly three operands
397 void *operator new(size_t s) {
398 return User::operator new(s, 3);
399 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000400 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000401 : ConstantExpr(VectorType::get(
402 cast<VectorType>(C1->getType())->getElementType(),
403 cast<VectorType>(C3->getType())->getNumElements()),
404 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000406 Op<0>() = C1;
407 Op<1>() = C2;
408 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000409 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000410 /// Transparently provide more efficient getOperand methods.
411 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000412};
413
Dan Gohman12fce772008-05-15 19:50:34 +0000414/// ExtractValueConstantExpr - This class is private to
415/// Constants.cpp, and is used behind the scenes to implement
416/// extractvalue constant exprs.
417class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000418 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000419public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000420 // allocate space for exactly one operand
421 void *operator new(size_t s) {
422 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000423 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000424 ExtractValueConstantExpr(Constant *Agg,
425 const SmallVector<unsigned, 4> &IdxList,
426 const Type *DestTy)
427 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
428 Indices(IdxList) {
429 Op<0>() = Agg;
430 }
431
Dan Gohman7bb04502008-05-31 19:09:08 +0000432 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000433 const SmallVector<unsigned, 4> Indices;
434
Dan Gohman12fce772008-05-15 19:50:34 +0000435 /// Transparently provide more efficient getOperand methods.
436 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
437};
438
439/// InsertValueConstantExpr - This class is private to
440/// Constants.cpp, and is used behind the scenes to implement
441/// insertvalue constant exprs.
442class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000443 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000444public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000445 // allocate space for exactly one operand
446 void *operator new(size_t s) {
447 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000448 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000449 InsertValueConstantExpr(Constant *Agg, Constant *Val,
450 const SmallVector<unsigned, 4> &IdxList,
451 const Type *DestTy)
452 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
453 Indices(IdxList) {
454 Op<0>() = Agg;
455 Op<1>() = Val;
456 }
457
Dan Gohman7bb04502008-05-31 19:09:08 +0000458 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000459 const SmallVector<unsigned, 4> Indices;
460
Dan Gohman12fce772008-05-15 19:50:34 +0000461 /// Transparently provide more efficient getOperand methods.
462 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
463};
464
465
Gordon Henriksen14a55692007-12-10 02:14:30 +0000466/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
467/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000468class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000469 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000470 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000471public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000472 static GetElementPtrConstantExpr *Create(Constant *C,
473 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000474 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000475 return new(IdxList.size() + 1)
476 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000477 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000478 /// Transparently provide more efficient getOperand methods.
479 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000480};
481
482// CompareConstantExpr - This class is private to Constants.cpp, and is used
483// behind the scenes to implement ICmp and FCmp constant expressions. This is
484// needed in order to store the predicate value for these instructions.
485struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000486 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
487 // allocate space for exactly two operands
488 void *operator new(size_t s) {
489 return User::operator new(s, 2);
490 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000491 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000492 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
493 unsigned short pred, Constant* LHS, Constant* RHS)
494 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000495 Op<0>() = LHS;
496 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000497 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000498 /// Transparently provide more efficient getOperand methods.
499 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000500};
501
502} // end anonymous namespace
503
Gabor Greiff6caff662008-05-10 08:32:32 +0000504template <>
505struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
506};
507DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
508
509template <>
510struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
511};
512DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
513
514template <>
515struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
516};
517DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
518
519template <>
520struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
521};
522DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
523
524template <>
525struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
526};
527DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
528
529template <>
530struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
531};
532DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
533
Dan Gohman12fce772008-05-15 19:50:34 +0000534template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000535struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000536};
Dan Gohman12fce772008-05-15 19:50:34 +0000537DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
538
539template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000540struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000541};
Dan Gohman12fce772008-05-15 19:50:34 +0000542DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
543
Gabor Greiff6caff662008-05-10 08:32:32 +0000544template <>
545struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
546};
547
548GetElementPtrConstantExpr::GetElementPtrConstantExpr
549 (Constant *C,
550 const std::vector<Constant*> &IdxList,
551 const Type *DestTy)
552 : ConstantExpr(DestTy, Instruction::GetElementPtr,
553 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
554 - (IdxList.size()+1),
555 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000556 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000557 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000558 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000559}
560
561DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
562
563
564template <>
565struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
566};
567DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
568
569
570} // End llvm namespace
571
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000572
573// Utility function for determining if a ConstantExpr is a CastOp or not. This
574// can't be inline because we don't want to #include Instruction.h into
575// Constant.h
576bool ConstantExpr::isCast() const {
577 return Instruction::isCast(getOpcode());
578}
579
Reid Spenceree3c9912006-12-04 05:19:50 +0000580bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000581 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000582}
583
Dan Gohman1ecaf452008-05-31 00:58:22 +0000584bool ConstantExpr::hasIndices() const {
585 return getOpcode() == Instruction::ExtractValue ||
586 getOpcode() == Instruction::InsertValue;
587}
588
589const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
590 if (const ExtractValueConstantExpr *EVCE =
591 dyn_cast<ExtractValueConstantExpr>(this))
592 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000593
594 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000595}
596
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000597unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000598 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000599 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000600 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000601}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000602
Chris Lattner7c1018a2006-07-14 19:37:40 +0000603/// getWithOperandReplaced - Return a constant expression identical to this
604/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000605Constant *
606ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000607 assert(OpNo < getNumOperands() && "Operand num is out of range!");
608 assert(Op->getType() == getOperand(OpNo)->getType() &&
609 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000610 if (getOperand(OpNo) == Op)
611 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000612
Chris Lattner227816342006-07-14 22:20:01 +0000613 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000614 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000615 case Instruction::Trunc:
616 case Instruction::ZExt:
617 case Instruction::SExt:
618 case Instruction::FPTrunc:
619 case Instruction::FPExt:
620 case Instruction::UIToFP:
621 case Instruction::SIToFP:
622 case Instruction::FPToUI:
623 case Instruction::FPToSI:
624 case Instruction::PtrToInt:
625 case Instruction::IntToPtr:
626 case Instruction::BitCast:
627 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000628 case Instruction::Select:
629 Op0 = (OpNo == 0) ? Op : getOperand(0);
630 Op1 = (OpNo == 1) ? Op : getOperand(1);
631 Op2 = (OpNo == 2) ? Op : getOperand(2);
632 return ConstantExpr::getSelect(Op0, Op1, Op2);
633 case Instruction::InsertElement:
634 Op0 = (OpNo == 0) ? Op : getOperand(0);
635 Op1 = (OpNo == 1) ? Op : getOperand(1);
636 Op2 = (OpNo == 2) ? Op : getOperand(2);
637 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
638 case Instruction::ExtractElement:
639 Op0 = (OpNo == 0) ? Op : getOperand(0);
640 Op1 = (OpNo == 1) ? Op : getOperand(1);
641 return ConstantExpr::getExtractElement(Op0, Op1);
642 case Instruction::ShuffleVector:
643 Op0 = (OpNo == 0) ? Op : getOperand(0);
644 Op1 = (OpNo == 1) ? Op : getOperand(1);
645 Op2 = (OpNo == 2) ? Op : getOperand(2);
646 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000647 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000648 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000649 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000650 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000651 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000652 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000653 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000654 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000655 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000656 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000657 default:
658 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000659 Op0 = (OpNo == 0) ? Op : getOperand(0);
660 Op1 = (OpNo == 1) ? Op : getOperand(1);
661 return ConstantExpr::get(getOpcode(), Op0, Op1);
662 }
663}
664
665/// getWithOperands - This returns the current constant expression with the
666/// operands replaced with the specified values. The specified operands must
667/// match count and type with the existing ones.
668Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000669getWithOperands(Constant* const *Ops, unsigned NumOps) const {
670 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000671 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000672 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000673 assert(Ops[i]->getType() == getOperand(i)->getType() &&
674 "Operand type mismatch!");
675 AnyChange |= Ops[i] != getOperand(i);
676 }
677 if (!AnyChange) // No operands changed, return self.
678 return const_cast<ConstantExpr*>(this);
679
680 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000681 case Instruction::Trunc:
682 case Instruction::ZExt:
683 case Instruction::SExt:
684 case Instruction::FPTrunc:
685 case Instruction::FPExt:
686 case Instruction::UIToFP:
687 case Instruction::SIToFP:
688 case Instruction::FPToUI:
689 case Instruction::FPToSI:
690 case Instruction::PtrToInt:
691 case Instruction::IntToPtr:
692 case Instruction::BitCast:
693 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000694 case Instruction::Select:
695 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
696 case Instruction::InsertElement:
697 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
698 case Instruction::ExtractElement:
699 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
700 case Instruction::ShuffleVector:
701 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000702 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000703 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000704 case Instruction::ICmp:
705 case Instruction::FCmp:
706 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000707 default:
708 assert(getNumOperands() == 2 && "Must be binary operator?");
709 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000710 }
711}
712
Chris Lattner2f7c9632001-06-06 20:29:01 +0000713
714//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715// isValueValidForType implementations
716
Reid Spencere7334722006-12-19 01:28:19 +0000717bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000718 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000719 if (Ty == Type::Int1Ty)
720 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000721 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000722 return true; // always true, has to fit in largest type
723 uint64_t Max = (1ll << NumBits) - 1;
724 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000725}
726
Reid Spencere0fc4df2006-10-20 07:07:24 +0000727bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000728 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000729 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000730 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000731 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000732 return true; // always true, has to fit in largest type
733 int64_t Min = -(1ll << (NumBits-1));
734 int64_t Max = (1ll << (NumBits-1)) - 1;
735 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000736}
737
Dale Johannesend246b2c2007-08-30 00:23:21 +0000738bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
739 // convert modifies in place, so make a copy.
740 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000741 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000742 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000743 default:
744 return false; // These can't be represented as floating point!
745
Dale Johannesend246b2c2007-08-30 00:23:21 +0000746 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000747 case Type::FloatTyID: {
748 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
749 return true;
750 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
751 return !losesInfo;
752 }
753 case Type::DoubleTyID: {
754 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
755 &Val2.getSemantics() == &APFloat::IEEEdouble)
756 return true;
757 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
758 return !losesInfo;
759 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000760 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000761 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
762 &Val2.getSemantics() == &APFloat::IEEEdouble ||
763 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000764 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000765 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
766 &Val2.getSemantics() == &APFloat::IEEEdouble ||
767 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000768 case Type::PPC_FP128TyID:
769 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
770 &Val2.getSemantics() == &APFloat::IEEEdouble ||
771 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000772 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000773}
Chris Lattner9655e542001-07-20 19:16:02 +0000774
Chris Lattner49d855c2001-09-07 16:46:31 +0000775//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000776// Factory Function Implementation
777
Gabor Greiff6caff662008-05-10 08:32:32 +0000778
779// The number of operands for each ConstantCreator::create method is
780// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000781// ConstantCreator - A class that is used to create constants by
782// ValueMap*. This class should be partially specialized if there is
783// something strange that needs to be done to interface to the ctor for the
784// constant.
785//
Chris Lattner189d19f2003-11-21 20:23:48 +0000786namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000787 template<class ValType>
788 struct ConstantTraits;
789
790 template<typename T, typename Alloc>
791 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
792 static unsigned uses(const std::vector<T, Alloc>& v) {
793 return v.size();
794 }
795 };
796
Chris Lattner189d19f2003-11-21 20:23:48 +0000797 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000798 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000799 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000800 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000801 }
802 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000803
Chris Lattner189d19f2003-11-21 20:23:48 +0000804 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000805 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000806 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000807 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000808 }
809 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000810
Chris Lattner935aa922005-10-04 17:48:46 +0000811 template<class ValType, class TypeClass, class ConstantClass,
812 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000813 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000814 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000815 typedef std::pair<const Type*, ValType> MapKey;
816 typedef std::map<MapKey, Constant *> MapTy;
817 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
818 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000819 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000820 /// Map - This is the main map from the element descriptor to the Constants.
821 /// This is the primary way we avoid creating two of the same shape
822 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000823 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000824
825 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
826 /// from the constants to their element in Map. This is important for
827 /// removal of constants from the array, which would otherwise have to scan
828 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000829 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000830
Jim Laskeyc03caef2006-07-17 17:38:29 +0000831 /// AbstractTypeMap - Map for abstract type constants.
832 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000833 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000834
835 /// ValueMapLock - Mutex for this map.
836 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000837
Chris Lattner98fa07b2003-05-23 20:03:32 +0000838 public:
Owen Anderson61794042009-06-17 20:10:08 +0000839 // NOTE: This function is not locked. It is the caller's responsibility
840 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000841 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000842
843 /// InsertOrGetItem - Return an iterator for the specified element.
844 /// If the element exists in the map, the returned iterator points to the
845 /// entry and Exists=true. If not, the iterator points to the newly
846 /// inserted entry and returns Exists=false. Newly inserted entries have
847 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +0000848 /// NOTE: This function is not locked. It is the caller's responsibility
849 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000850 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
851 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000852 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000853 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000854 Exists = !IP.second;
855 return IP.first;
856 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000857
Chris Lattner935aa922005-10-04 17:48:46 +0000858private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000859 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000860 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000861 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000862 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
863 IMI->second->second == CP &&
864 "InverseMap corrupt!");
865 return IMI->second;
866 }
867
Jim Laskeyc03caef2006-07-17 17:38:29 +0000868 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +0000869 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
870 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000871 if (I == Map.end() || I->second != CP) {
872 // FIXME: This should not use a linear scan. If this gets to be a
873 // performance problem, someone should look at this.
874 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
875 /* empty */;
876 }
Chris Lattner935aa922005-10-04 17:48:46 +0000877 return I;
878 }
Owen Andersonf89c38c2009-06-17 20:43:39 +0000879
880 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
881 typename MapTy::iterator I) {
882 ConstantClass* Result =
883 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
884
885 assert(Result->getType() == Ty && "Type specified is not correct!");
886 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
887
888 if (HasLargeKey) // Remember the reverse mapping if needed.
889 InverseMap.insert(std::make_pair(Result, I));
890
891 // If the type of the constant is abstract, make sure that an entry
892 // exists for it in the AbstractTypeMap.
893 if (Ty->isAbstract()) {
894 typename AbstractTypeMapTy::iterator TI =
895 AbstractTypeMap.find(Ty);
896
897 if (TI == AbstractTypeMap.end()) {
898 // Add ourselves to the ATU list of the type.
899 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
900
901 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
902 }
903 }
904
905 return Result;
906 }
Chris Lattner935aa922005-10-04 17:48:46 +0000907public:
908
Chris Lattnerb64419a2005-10-03 22:51:37 +0000909 /// getOrCreate - Return the specified constant from the map, creating it if
910 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000911 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000912 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +0000913 MapKey Lookup(Ty, V);
914 ConstantClass* Result = 0;
915
916 typename MapTy::iterator I = Map.find(Lookup);
917 // Is it in the map?
918 if (I != Map.end())
919 Result = static_cast<ConstantClass *>(I->second);
920
921 if (!Result) {
922 // If no preexisting value, create one now...
923 Result = Create(Ty, V, I);
924 }
925
926 return Result;
927 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000928
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000929 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000930 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000931 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000932 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000933 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000934
Chris Lattner935aa922005-10-04 17:48:46 +0000935 if (HasLargeKey) // Remember the reverse mapping if needed.
936 InverseMap.erase(CP);
937
Chris Lattnerb50d1352003-10-05 00:17:43 +0000938 // Now that we found the entry, make sure this isn't the entry that
939 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000940 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000941 if (Ty->isAbstract()) {
942 assert(AbstractTypeMap.count(Ty) &&
943 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000944 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000945 if (ATMEntryIt == I) {
946 // Yes, we are removing the representative entry for this type.
947 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000948 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000949
Chris Lattnerb50d1352003-10-05 00:17:43 +0000950 // First check the entry before this one...
951 if (TmpIt != Map.begin()) {
952 --TmpIt;
953 if (TmpIt->first.first != Ty) // Not the same type, move back...
954 ++TmpIt;
955 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000956
Chris Lattnerb50d1352003-10-05 00:17:43 +0000957 // If we didn't find the same type, try to move forward...
958 if (TmpIt == ATMEntryIt) {
959 ++TmpIt;
960 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
961 --TmpIt; // No entry afterwards with the same type
962 }
963
964 // If there is another entry in the map of the same abstract type,
965 // update the AbstractTypeMap entry now.
966 if (TmpIt != ATMEntryIt) {
967 ATMEntryIt = TmpIt;
968 } else {
969 // Otherwise, we are removing the last instance of this type
970 // from the table. Remove from the ATM, and from user list.
971 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
972 AbstractTypeMap.erase(Ty);
973 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000974 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000975 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000976
Chris Lattnerb50d1352003-10-05 00:17:43 +0000977 Map.erase(I);
978 }
979
Chris Lattner3b793c62005-10-04 21:35:50 +0000980
981 /// MoveConstantToNewSlot - If we are about to change C to be the element
982 /// specified by I, update our internal data structures to reflect this
983 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +0000984 /// NOTE: This function is not locked. It is the responsibility of the
985 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000986 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000987 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000988 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000989 assert(OldI != Map.end() && "Constant not found in constant table!");
990 assert(OldI->second == C && "Didn't find correct element?");
991
992 // If this constant is the representative element for its abstract type,
993 // update the AbstractTypeMap so that the representative element is I.
994 if (C->getType()->isAbstract()) {
995 typename AbstractTypeMapTy::iterator ATI =
996 AbstractTypeMap.find(C->getType());
997 assert(ATI != AbstractTypeMap.end() &&
998 "Abstract type not in AbstractTypeMap?");
999 if (ATI->second == OldI)
1000 ATI->second = I;
1001 }
1002
1003 // Remove the old entry from the map.
1004 Map.erase(OldI);
1005
1006 // Update the inverse map so that we know that this constant is now
1007 // located at descriptor I.
1008 if (HasLargeKey) {
1009 assert(I->second == C && "Bad inversemap entry!");
1010 InverseMap[C] = I;
1011 }
1012 }
1013
Chris Lattnerb50d1352003-10-05 00:17:43 +00001014 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001015 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001016 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001017 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001018
1019 assert(I != AbstractTypeMap.end() &&
1020 "Abstract type not in AbstractTypeMap?");
1021
1022 // Convert a constant at a time until the last one is gone. The last one
1023 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1024 // eliminated eventually.
1025 do {
1026 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001027 TypeClass>::convert(
1028 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001029 cast<TypeClass>(NewTy));
1030
Jim Laskeyc03caef2006-07-17 17:38:29 +00001031 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001032 } while (I != AbstractTypeMap.end());
1033 }
1034
1035 // If the type became concrete without being refined to any other existing
1036 // type, we just remove ourselves from the ATU list.
1037 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001038 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001039 }
1040
1041 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001042 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001043 }
1044 };
1045}
1046
Chris Lattnera84df0a22006-09-28 23:36:21 +00001047
Chris Lattner28173502007-02-20 06:11:36 +00001048
Chris Lattner9fba3da2004-02-15 05:53:04 +00001049//---- ConstantAggregateZero::get() implementation...
1050//
1051namespace llvm {
1052 // ConstantAggregateZero does not take extra "value" argument...
1053 template<class ValType>
1054 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1055 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1056 return new ConstantAggregateZero(Ty);
1057 }
1058 };
1059
1060 template<>
1061 struct ConvertConstantType<ConstantAggregateZero, Type> {
1062 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1063 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001064 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001065 assert(New != OldC && "Didn't replace constant??");
1066 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001067 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001068 }
1069 };
1070}
1071
Chris Lattner69edc982006-09-28 00:35:06 +00001072static ManagedStatic<ValueMap<char, Type,
1073 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001074
Chris Lattner3e650af2004-08-04 04:48:01 +00001075static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1076
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001077ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001078 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001079 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001080
1081 // Implicitly locked.
1082 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001083}
1084
Dan Gohman92b551b2009-03-03 02:55:14 +00001085/// destroyConstant - Remove the constant from the constant table...
1086///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001087void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001088 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001089 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001090 destroyConstantImpl();
1091}
1092
Chris Lattner3462ae32001-12-03 22:26:30 +00001093//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001094//
Chris Lattner189d19f2003-11-21 20:23:48 +00001095namespace llvm {
1096 template<>
1097 struct ConvertConstantType<ConstantArray, ArrayType> {
1098 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1099 // Make everyone now use a constant of the new type...
1100 std::vector<Constant*> C;
1101 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1102 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001103 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001104 assert(New != OldC && "Didn't replace constant??");
1105 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001106 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001107 }
1108 };
1109}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001110
Chris Lattner3e650af2004-08-04 04:48:01 +00001111static std::vector<Constant*> getValType(ConstantArray *CA) {
1112 std::vector<Constant*> Elements;
1113 Elements.reserve(CA->getNumOperands());
1114 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1115 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1116 return Elements;
1117}
1118
Chris Lattnerb64419a2005-10-03 22:51:37 +00001119typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001120 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001121static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001122
Chris Lattner015e8212004-02-15 04:14:47 +00001123Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001124 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001125 // If this is an all-zero array, return a ConstantAggregateZero object
1126 if (!V.empty()) {
1127 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001128 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001129 // Implicitly locked.
1130 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001131 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001132 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001133 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001134 // Implicitly locked.
1135 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001136 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001137 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001138
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001139 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001140}
1141
Dan Gohman92b551b2009-03-03 02:55:14 +00001142/// destroyConstant - Remove the constant from the constant table...
1143///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001144void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001145 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001146 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001147 destroyConstantImpl();
1148}
1149
Reid Spencer2546b762007-01-26 07:37:34 +00001150/// isString - This method returns true if the array is an array of i8, and
1151/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001152bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001153 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001154 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001155 return false;
1156 // Check the elements to make sure they are all integers, not constant
1157 // expressions.
1158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1159 if (!isa<ConstantInt>(getOperand(i)))
1160 return false;
1161 return true;
1162}
1163
Evan Cheng3763c5b2006-10-26 19:15:05 +00001164/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001165/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001166/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001167bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001168 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001169 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001170 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001171
Evan Chenge974da62006-10-26 21:48:03 +00001172 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001173 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001174 return false;
1175 // Other elements must be non-null integers.
1176 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1177 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001178 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001179 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001180 return false;
1181 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001182 return true;
1183}
1184
1185
Dan Gohman92b551b2009-03-03 02:55:14 +00001186/// getAsString - If the sub-element type of this array is i8
1187/// then this method converts the array to an std::string and returns it.
1188/// Otherwise, it asserts out.
1189///
Chris Lattner81fabb02002-08-26 17:53:56 +00001190std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001191 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001192 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001193 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001194 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001195 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001196 return Result;
1197}
1198
1199
Chris Lattner3462ae32001-12-03 22:26:30 +00001200//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001201//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001202
Chris Lattner189d19f2003-11-21 20:23:48 +00001203namespace llvm {
1204 template<>
1205 struct ConvertConstantType<ConstantStruct, StructType> {
1206 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1207 // Make everyone now use a constant of the new type...
1208 std::vector<Constant*> C;
1209 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1210 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001211 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001212 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001213
Chris Lattner189d19f2003-11-21 20:23:48 +00001214 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001215 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001216 }
1217 };
1218}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001219
Chris Lattner8760ec72005-10-04 01:17:50 +00001220typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001221 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001222static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001223
Chris Lattner3e650af2004-08-04 04:48:01 +00001224static std::vector<Constant*> getValType(ConstantStruct *CS) {
1225 std::vector<Constant*> Elements;
1226 Elements.reserve(CS->getNumOperands());
1227 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1228 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1229 return Elements;
1230}
1231
Chris Lattner015e8212004-02-15 04:14:47 +00001232Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001233 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001234 // Create a ConstantAggregateZero value if all elements are zeros...
1235 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001236 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001237 // Implicitly locked.
1238 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001239
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001240 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001241}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001242
Chris Lattnerd7a73302001-10-13 06:57:33 +00001243// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001244//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001245void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001246 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001247 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001248 destroyConstantImpl();
1249}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001250
Reid Spencerd84d35b2007-02-15 02:26:10 +00001251//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001252//
1253namespace llvm {
1254 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001255 struct ConvertConstantType<ConstantVector, VectorType> {
1256 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001257 // Make everyone now use a constant of the new type...
1258 std::vector<Constant*> C;
1259 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1260 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001261 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001262 assert(New != OldC && "Didn't replace constant??");
1263 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001264 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001265 }
1266 };
1267}
1268
Reid Spencerd84d35b2007-02-15 02:26:10 +00001269static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001270 std::vector<Constant*> Elements;
1271 Elements.reserve(CP->getNumOperands());
1272 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1273 Elements.push_back(CP->getOperand(i));
1274 return Elements;
1275}
1276
Reid Spencerd84d35b2007-02-15 02:26:10 +00001277static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001278 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001279
Reid Spencerd84d35b2007-02-15 02:26:10 +00001280Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001281 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001282 assert(!V.empty() && "Vectors can't be empty");
1283 // If this is an all-undef or alll-zero vector, return a
1284 // ConstantAggregateZero or UndefValue.
1285 Constant *C = V[0];
1286 bool isZero = C->isNullValue();
1287 bool isUndef = isa<UndefValue>(C);
1288
1289 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001290 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001291 if (V[i] != C) {
1292 isZero = isUndef = false;
1293 break;
1294 }
Brian Gaeke02209042004-08-20 06:00:58 +00001295 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001296
1297 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001298 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001299 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001300 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001301
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001302 // Implicitly locked.
1303 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001304}
1305
Brian Gaeke02209042004-08-20 06:00:58 +00001306// destroyConstant - Remove the constant from the constant table...
1307//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001308void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001309 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001310 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001311 destroyConstantImpl();
1312}
1313
Dan Gohman30978072007-05-24 14:36:04 +00001314/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001315/// is set to all ones.
1316/// @returns true iff this constant's emements are all set to all ones.
1317/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001318bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001319 // Check out first element.
1320 const Constant *Elt = getOperand(0);
1321 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1322 if (!CI || !CI->isAllOnesValue()) return false;
1323 // Then make sure all remaining elements point to the same value.
1324 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1325 if (getOperand(I) != Elt) return false;
1326 }
1327 return true;
1328}
1329
Dan Gohman07159202007-10-17 17:51:30 +00001330/// getSplatValue - If this is a splat constant, where all of the
1331/// elements have the same value, return that value. Otherwise return null.
1332Constant *ConstantVector::getSplatValue() {
1333 // Check out first element.
1334 Constant *Elt = getOperand(0);
1335 // Then make sure all remaining elements point to the same value.
1336 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1337 if (getOperand(I) != Elt) return 0;
1338 return Elt;
1339}
1340
Chris Lattner3462ae32001-12-03 22:26:30 +00001341//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001342//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001343
Chris Lattner189d19f2003-11-21 20:23:48 +00001344namespace llvm {
1345 // ConstantPointerNull does not take extra "value" argument...
1346 template<class ValType>
1347 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1348 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1349 return new ConstantPointerNull(Ty);
1350 }
1351 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001352
Chris Lattner189d19f2003-11-21 20:23:48 +00001353 template<>
1354 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1355 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1356 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001357 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001358 assert(New != OldC && "Didn't replace constant??");
1359 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001360 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001361 }
1362 };
1363}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001364
Chris Lattner69edc982006-09-28 00:35:06 +00001365static ManagedStatic<ValueMap<char, PointerType,
1366 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001367
Chris Lattner3e650af2004-08-04 04:48:01 +00001368static char getValType(ConstantPointerNull *) {
1369 return 0;
1370}
1371
1372
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001373ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001374 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001375 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001376}
1377
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001378// destroyConstant - Remove the constant from the constant table...
1379//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001380void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001381 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001382 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001383 destroyConstantImpl();
1384}
1385
1386
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001387//---- UndefValue::get() implementation...
1388//
1389
1390namespace llvm {
1391 // UndefValue does not take extra "value" argument...
1392 template<class ValType>
1393 struct ConstantCreator<UndefValue, Type, ValType> {
1394 static UndefValue *create(const Type *Ty, const ValType &V) {
1395 return new UndefValue(Ty);
1396 }
1397 };
1398
1399 template<>
1400 struct ConvertConstantType<UndefValue, Type> {
1401 static void convert(UndefValue *OldC, const Type *NewTy) {
1402 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001403 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001404 assert(New != OldC && "Didn't replace constant??");
1405 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001406 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001407 }
1408 };
1409}
1410
Chris Lattner69edc982006-09-28 00:35:06 +00001411static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001412
1413static char getValType(UndefValue *) {
1414 return 0;
1415}
1416
1417
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001418UndefValue *UndefValue::get(const Type *Ty) {
1419 // Implicitly locked.
1420 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001421}
1422
1423// destroyConstant - Remove the constant from the constant table.
1424//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001425void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001426 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001427 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001428 destroyConstantImpl();
1429}
1430
Nick Lewycky49f89192009-04-04 07:22:01 +00001431//---- MDString::get() implementation
1432//
1433
1434MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001435 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001436 StrBegin(begin), StrEnd(end) {}
1437
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001438void MDString::destroyConstant() {
Owen Anderson69ab4162009-07-16 22:11:26 +00001439 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001440 destroyConstantImpl();
1441}
1442
1443//---- MDNode::get() implementation
1444//
1445
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001446MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001447 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001448 for (unsigned i = 0; i != NumVals; ++i)
1449 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001450}
1451
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001452void MDNode::Profile(FoldingSetNodeID &ID) const {
1453 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001454 ID.AddPointer(*I);
1455}
1456
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001457void MDNode::destroyConstant() {
Owen Anderson4118dde2009-07-16 23:44:30 +00001458 getType()->getContext().erase(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001459 destroyConstantImpl();
1460}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001461
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001462//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001463//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001464
Dan Gohmand78c4002008-05-13 00:00:25 +00001465namespace {
1466
Reid Spenceree3c9912006-12-04 05:19:50 +00001467struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001468 typedef SmallVector<unsigned, 4> IndexList;
1469
1470 ExprMapKeyType(unsigned opc,
1471 const std::vector<Constant*> &ops,
1472 unsigned short pred = 0,
1473 const IndexList &inds = IndexList())
1474 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001475 uint16_t opcode;
1476 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001477 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001478 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001479 bool operator==(const ExprMapKeyType& that) const {
1480 return this->opcode == that.opcode &&
1481 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001482 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001483 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001484 }
1485 bool operator<(const ExprMapKeyType & that) const {
1486 return this->opcode < that.opcode ||
1487 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1488 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001489 this->operands < that.operands) ||
1490 (this->opcode == that.opcode && this->predicate == that.predicate &&
1491 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001492 }
1493
1494 bool operator!=(const ExprMapKeyType& that) const {
1495 return !(*this == that);
1496 }
1497};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001498
Dan Gohmand78c4002008-05-13 00:00:25 +00001499}
1500
Chris Lattner189d19f2003-11-21 20:23:48 +00001501namespace llvm {
1502 template<>
1503 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001504 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1505 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001506 if (Instruction::isCast(V.opcode))
1507 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1508 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001509 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001510 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1511 if (V.opcode == Instruction::Select)
1512 return new SelectConstantExpr(V.operands[0], V.operands[1],
1513 V.operands[2]);
1514 if (V.opcode == Instruction::ExtractElement)
1515 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1516 if (V.opcode == Instruction::InsertElement)
1517 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1518 V.operands[2]);
1519 if (V.opcode == Instruction::ShuffleVector)
1520 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1521 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001522 if (V.opcode == Instruction::InsertValue)
1523 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1524 V.indices, Ty);
1525 if (V.opcode == Instruction::ExtractValue)
1526 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001527 if (V.opcode == Instruction::GetElementPtr) {
1528 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001529 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001530 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001531
Reid Spenceree3c9912006-12-04 05:19:50 +00001532 // The compare instructions are weird. We have to encode the predicate
1533 // value and it is combined with the instruction opcode by multiplying
1534 // the opcode by one hundred. We must decode this to get the predicate.
1535 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001536 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001537 V.operands[0], V.operands[1]);
1538 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001539 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1540 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001541 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001542 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001543 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001544 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001545
Chris Lattner189d19f2003-11-21 20:23:48 +00001546 template<>
1547 struct ConvertConstantType<ConstantExpr, Type> {
1548 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1549 Constant *New;
1550 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001551 case Instruction::Trunc:
1552 case Instruction::ZExt:
1553 case Instruction::SExt:
1554 case Instruction::FPTrunc:
1555 case Instruction::FPExt:
1556 case Instruction::UIToFP:
1557 case Instruction::SIToFP:
1558 case Instruction::FPToUI:
1559 case Instruction::FPToSI:
1560 case Instruction::PtrToInt:
1561 case Instruction::IntToPtr:
1562 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001563 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001564 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001565 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001566 case Instruction::Select:
1567 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1568 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001569 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001570 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001571 default:
1572 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001573 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001574 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001575 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001576 break;
1577 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001578 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001579 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001580 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001581 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001582 break;
1583 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001584
Chris Lattner189d19f2003-11-21 20:23:48 +00001585 assert(New != OldC && "Didn't replace constant??");
1586 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001587 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001588 }
1589 };
1590} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001591
1592
Chris Lattner3e650af2004-08-04 04:48:01 +00001593static ExprMapKeyType getValType(ConstantExpr *CE) {
1594 std::vector<Constant*> Operands;
1595 Operands.reserve(CE->getNumOperands());
1596 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1597 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001598 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001599 CE->isCompare() ? CE->getPredicate() : 0,
1600 CE->hasIndices() ?
1601 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001602}
1603
Chris Lattner69edc982006-09-28 00:35:06 +00001604static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1605 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001606
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001607/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001608/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001610 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001611 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001612 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001613 if (Constant *FC =
1614 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001615 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001616
Vikram S. Adve4c485332002-07-15 18:19:33 +00001617 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001618 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001619 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001620
Owen Anderson61794042009-06-17 20:10:08 +00001621 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001622 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001623}
Reid Spencerf37dc652006-12-05 19:14:13 +00001624
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001625Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001626 Instruction::CastOps opc = Instruction::CastOps(oc);
1627 assert(Instruction::isCast(opc) && "opcode out of range");
1628 assert(C && Ty && "Null arguments to getCast");
1629 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1630
1631 switch (opc) {
1632 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001633 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001634 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001635 case Instruction::Trunc: return getTrunc(C, Ty);
1636 case Instruction::ZExt: return getZExt(C, Ty);
1637 case Instruction::SExt: return getSExt(C, Ty);
1638 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1639 case Instruction::FPExt: return getFPExtend(C, Ty);
1640 case Instruction::UIToFP: return getUIToFP(C, Ty);
1641 case Instruction::SIToFP: return getSIToFP(C, Ty);
1642 case Instruction::FPToUI: return getFPToUI(C, Ty);
1643 case Instruction::FPToSI: return getFPToSI(C, Ty);
1644 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1645 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1646 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001647 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001648 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001649}
1650
Reid Spencer5c140882006-12-04 20:17:56 +00001651Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001652 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001653 return getCast(Instruction::BitCast, C, Ty);
1654 return getCast(Instruction::ZExt, C, Ty);
1655}
1656
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001657Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001658 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001659 return getCast(Instruction::BitCast, C, Ty);
1660 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001661}
1662
1663Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001664 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001665 return getCast(Instruction::BitCast, C, Ty);
1666 return getCast(Instruction::Trunc, C, Ty);
1667}
1668
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001669Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001670 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001671 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001672
Chris Lattner03c49532007-01-15 02:27:26 +00001673 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001674 return getCast(Instruction::PtrToInt, S, Ty);
1675 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001676}
1677
Reid Spencer56521c42006-12-12 00:51:07 +00001678Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1679 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001680 assert(C->getType()->isIntOrIntVector() &&
1681 Ty->isIntOrIntVector() && "Invalid cast");
1682 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1683 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001684 Instruction::CastOps opcode =
1685 (SrcBits == DstBits ? Instruction::BitCast :
1686 (SrcBits > DstBits ? Instruction::Trunc :
1687 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1688 return getCast(opcode, C, Ty);
1689}
1690
1691Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001692 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001693 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001694 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1695 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001696 if (SrcBits == DstBits)
1697 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001698 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001699 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001700 return getCast(opcode, C, Ty);
1701}
1702
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001703Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001704#ifndef NDEBUG
1705 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1706 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1707#endif
1708 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1709 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1710 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1711 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001712 "SrcTy must be larger than DestTy for Trunc!");
1713
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001714 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715}
1716
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001717Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001718#ifndef NDEBUG
1719 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1720 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1721#endif
1722 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1723 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1724 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1725 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001726 "SrcTy must be smaller than DestTy for SExt!");
1727
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001728 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001729}
1730
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001731Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001732#ifndef NDEBUG
1733 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1734 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1735#endif
1736 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1737 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1738 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1739 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001740 "SrcTy must be smaller than DestTy for ZExt!");
1741
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001742 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001743}
1744
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001745Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001746#ifndef NDEBUG
1747 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1748 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1749#endif
1750 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1751 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1752 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001753 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001754 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001755}
1756
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001757Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001758#ifndef NDEBUG
1759 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1760 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1761#endif
1762 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1763 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1764 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001765 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001766 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001767}
1768
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001769Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001770#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001771 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1772 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001773#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001774 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1775 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1776 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001777 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001778}
1779
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001780Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001781#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001782 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1783 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001784#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001785 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1786 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001787 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001788 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789}
1790
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001791Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001792#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001793 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1794 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001795#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001796 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1797 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1798 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001799 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001800}
1801
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001802Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001803#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001804 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1805 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001806#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001807 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1808 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1809 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001810 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001811}
1812
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001813Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001814 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001815 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001816 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817}
1818
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001819Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001820 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001821 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001822 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001823}
1824
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001825Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001826 // BitCast implies a no-op cast of type only. No bits change. However, you
1827 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001828#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001829 const Type *SrcTy = C->getType();
1830 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001831 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001832
1833 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1834 // or nonptr->ptr). For all the other types, the cast is okay if source and
1835 // destination bit widths are identical.
1836 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1837 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001838#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001839 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001840
1841 // It is common to ask for a bitcast of a value to its own type, handle this
1842 // speedily.
1843 if (C->getType() == DstTy) return C;
1844
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001845 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001846}
1847
Chris Lattnerb50d1352003-10-05 00:17:43 +00001848Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001849 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001850 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001851 assert(Opcode >= Instruction::BinaryOpsBegin &&
1852 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001853 "Invalid opcode in binary constant expression");
1854 assert(C1->getType() == C2->getType() &&
1855 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001856
Reid Spencer542964f2007-01-11 18:21:29 +00001857 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001858 if (Constant *FC = ConstantFoldBinaryInstruction(
1859 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001860 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001861
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001862 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001863 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001864
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001865 // Implicitly locked.
1866 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001867}
1868
Reid Spencer266e42b2006-12-23 06:05:41 +00001869Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001870 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001871 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001872 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001873 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1874 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1875 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1876 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1877 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1878 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001879 return getFCmp(predicate, C1, C2);
1880
Nate Begemanc96e2e42008-07-25 17:35:37 +00001881 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1882 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1883 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1884 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001885 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001886 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001887}
1888
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001889Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001890 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1891 if (C1->getType()->isFPOrFPVector()) {
1892 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1893 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1894 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1895 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001896#ifndef NDEBUG
1897 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001898 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001899 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001900 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001901 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001902 assert(C1->getType()->isIntOrIntVector() &&
1903 "Tried to create an integer operation on a non-integer type!");
1904 break;
1905 case Instruction::FAdd:
1906 case Instruction::FSub:
1907 case Instruction::FMul:
1908 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1909 assert(C1->getType()->isFPOrFPVector() &&
1910 "Tried to create a floating-point operation on a "
1911 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001912 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001913 case Instruction::UDiv:
1914 case Instruction::SDiv:
1915 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001916 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001917 "Tried to create an arithmetic operation on a non-arithmetic type!");
1918 break;
1919 case Instruction::FDiv:
1920 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001921 assert(C1->getType()->isFPOrFPVector() &&
1922 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001923 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001924 case Instruction::URem:
1925 case Instruction::SRem:
1926 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001927 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001928 "Tried to create an arithmetic operation on a non-arithmetic type!");
1929 break;
1930 case Instruction::FRem:
1931 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001932 assert(C1->getType()->isFPOrFPVector() &&
1933 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001934 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001935 case Instruction::And:
1936 case Instruction::Or:
1937 case Instruction::Xor:
1938 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001939 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001940 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001941 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001942 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001943 case Instruction::LShr:
1944 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001945 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001946 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001947 "Tried to create a shift operation on a non-integer type!");
1948 break;
1949 default:
1950 break;
1951 }
1952#endif
1953
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001954 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001955}
1956
Reid Spencer266e42b2006-12-23 06:05:41 +00001957Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001958 Constant *C1, Constant *C2) {
1959 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001960 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001961}
1962
Chris Lattner6e415c02004-03-12 05:54:04 +00001963Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001964 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001965 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001966
1967 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001968 if (Constant *SC = ConstantFoldSelectInstruction(
1969 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001970 return SC; // Fold common cases
1971
1972 std::vector<Constant*> argVec(3, C);
1973 argVec[1] = V1;
1974 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001975 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001976
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001977 // Implicitly locked.
1978 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001979}
1980
Chris Lattnerb50d1352003-10-05 00:17:43 +00001981Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001982 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001983 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001984 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1985 Idxs+NumIdx) ==
1986 cast<PointerType>(ReqTy)->getElementType() &&
1987 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001988
Owen Anderson53a52212009-07-13 04:09:18 +00001989 if (Constant *FC = ConstantFoldGetElementPtr(
1990 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001991 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001992
Chris Lattnerb50d1352003-10-05 00:17:43 +00001993 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001994 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001995 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001996 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001997 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001998 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001999 for (unsigned i = 0; i != NumIdx; ++i)
2000 ArgVec.push_back(cast<Constant>(Idxs[i]));
2001 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002002
2003 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002004 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002005}
2006
Chris Lattner302116a2007-01-31 04:40:28 +00002007Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002008 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002009 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002010 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002011 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002012 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002013 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002014 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002015}
2016
Chris Lattner302116a2007-01-31 04:40:28 +00002017Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002018 unsigned NumIdx) {
2019 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002020}
2021
Chris Lattner302116a2007-01-31 04:40:28 +00002022
Reid Spenceree3c9912006-12-04 05:19:50 +00002023Constant *
2024ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2025 assert(LHS->getType() == RHS->getType());
2026 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2027 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2028
Owen Anderson53a52212009-07-13 04:09:18 +00002029 if (Constant *FC = ConstantFoldCompareInstruction(
2030 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002031 return FC; // Fold a few common cases...
2032
2033 // Look up the constant in the table first to ensure uniqueness
2034 std::vector<Constant*> ArgVec;
2035 ArgVec.push_back(LHS);
2036 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002037 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002038 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002039
2040 // Implicitly locked.
2041 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002042}
2043
2044Constant *
2045ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2046 assert(LHS->getType() == RHS->getType());
2047 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2048
Owen Anderson53a52212009-07-13 04:09:18 +00002049 if (Constant *FC = ConstantFoldCompareInstruction(
2050 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002051 return FC; // Fold a few common cases...
2052
2053 // Look up the constant in the table first to ensure uniqueness
2054 std::vector<Constant*> ArgVec;
2055 ArgVec.push_back(LHS);
2056 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002057 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002058 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002059
2060 // Implicitly locked.
2061 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002062}
2063
Robert Bocchino23004482006-01-10 19:05:34 +00002064Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2065 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002066 if (Constant *FC = ConstantFoldExtractElementInstruction(
2067 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002068 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002069 // Look up the constant in the table first to ensure uniqueness
2070 std::vector<Constant*> ArgVec(1, Val);
2071 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002072 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002073
2074 // Implicitly locked.
2075 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002076}
2077
2078Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002079 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002080 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002081 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002082 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002083 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002084 Val, Idx);
2085}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002086
Robert Bocchinoca27f032006-01-17 20:07:22 +00002087Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2088 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002089 if (Constant *FC = ConstantFoldInsertElementInstruction(
2090 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002091 return FC; // Fold a few common cases...
2092 // Look up the constant in the table first to ensure uniqueness
2093 std::vector<Constant*> ArgVec(1, Val);
2094 ArgVec.push_back(Elt);
2095 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002096 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002097
2098 // Implicitly locked.
2099 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002100}
2101
2102Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2103 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002104 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002105 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002106 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002107 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002108 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002109 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002110 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002111}
2112
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002113Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2114 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002115 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2116 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002117 return FC; // Fold a few common cases...
2118 // Look up the constant in the table first to ensure uniqueness
2119 std::vector<Constant*> ArgVec(1, V1);
2120 ArgVec.push_back(V2);
2121 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002122 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002123
2124 // Implicitly locked.
2125 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002126}
2127
2128Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2129 Constant *Mask) {
2130 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2131 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002132
2133 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2134 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2135 const Type *ShufTy = VectorType::get(EltTy, NElts);
2136 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002137}
2138
Dan Gohman12fce772008-05-15 19:50:34 +00002139Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2140 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002141 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002142 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2143 Idxs+NumIdx) == Val->getType() &&
2144 "insertvalue indices invalid!");
2145 assert(Agg->getType() == ReqTy &&
2146 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002147 assert(Agg->getType()->isFirstClassType() &&
2148 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002149 Constant *FC = ConstantFoldInsertValueInstruction(
2150 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002151 assert(FC && "InsertValue constant expr couldn't be folded!");
2152 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002153}
2154
2155Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002156 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002157 assert(Agg->getType()->isFirstClassType() &&
2158 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002159
Dan Gohman0752bff2008-05-23 00:36:11 +00002160 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002161#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002162 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002163 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002164#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002165 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002166 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2167}
2168
2169Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002170 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002171 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2172 Idxs+NumIdx) == ReqTy &&
2173 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002174 assert(Agg->getType()->isFirstClassType() &&
2175 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002176 Constant *FC = ConstantFoldExtractValueInstruction(
2177 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002178 assert(FC && "ExtractValue constant expr couldn't be folded!");
2179 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002180}
2181
2182Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002183 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002184 assert(Agg->getType()->isFirstClassType() &&
2185 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002186
2187 const Type *ReqTy =
2188 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2189 assert(ReqTy && "extractvalue indices invalid!");
2190 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2191}
2192
Vikram S. Adve4c485332002-07-15 18:19:33 +00002193// destroyConstant - Remove the constant from the constant table...
2194//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002195void ConstantExpr::destroyConstant() {
2196 // Implicitly locked.
2197 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002198 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002199}
2200
Chris Lattner3cd8c562002-07-30 18:54:25 +00002201const char *ConstantExpr::getOpcodeName() const {
2202 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002203}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002204
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002205//===----------------------------------------------------------------------===//
2206// replaceUsesOfWithOnConstant implementations
2207
Chris Lattner913849b2007-08-21 00:55:23 +00002208/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2209/// 'From' to be uses of 'To'. This must update the uniquing data structures
2210/// etc.
2211///
2212/// Note that we intentionally replace all uses of From with To here. Consider
2213/// a large array that uses 'From' 1000 times. By handling this case all here,
2214/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2215/// single invocation handles all 1000 uses. Handling them one at a time would
2216/// work, but would be really slow because it would have to unique each updated
2217/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002218void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002219 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002220 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002221 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002222
Jim Laskeyc03caef2006-07-17 17:38:29 +00002223 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002224 Lookup.first.first = getType();
2225 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002226
Chris Lattnerb64419a2005-10-03 22:51:37 +00002227 std::vector<Constant*> &Values = Lookup.first.second;
2228 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002229
Chris Lattner8760ec72005-10-04 01:17:50 +00002230 // Fill values with the modified operands of the constant array. Also,
2231 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002232 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002233 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002234 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002235 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2236 Constant *Val = cast<Constant>(O->get());
2237 if (Val == From) {
2238 Val = ToC;
2239 ++NumUpdated;
2240 }
2241 Values.push_back(Val);
2242 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002243 } else {
2244 isAllZeros = true;
2245 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2246 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002247 if (Val == From) {
2248 Val = ToC;
2249 ++NumUpdated;
2250 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002251 Values.push_back(Val);
2252 if (isAllZeros) isAllZeros = Val->isNullValue();
2253 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002254 }
2255
Chris Lattnerb64419a2005-10-03 22:51:37 +00002256 Constant *Replacement = 0;
2257 if (isAllZeros) {
2258 Replacement = ConstantAggregateZero::get(getType());
2259 } else {
2260 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002261 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002262 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002263 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002264 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002265
2266 if (Exists) {
2267 Replacement = I->second;
2268 } else {
2269 // Okay, the new shape doesn't exist in the system yet. Instead of
2270 // creating a new constant array, inserting it, replaceallusesof'ing the
2271 // old with the new, then deleting the old... just update the current one
2272 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002273 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002274
Chris Lattner913849b2007-08-21 00:55:23 +00002275 // Update to the new value. Optimize for the case when we have a single
2276 // operand that we're changing, but handle bulk updates efficiently.
2277 if (NumUpdated == 1) {
2278 unsigned OperandToUpdate = U-OperandList;
2279 assert(getOperand(OperandToUpdate) == From &&
2280 "ReplaceAllUsesWith broken!");
2281 setOperand(OperandToUpdate, ToC);
2282 } else {
2283 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2284 if (getOperand(i) == From)
2285 setOperand(i, ToC);
2286 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002287 return;
2288 }
2289 }
2290
2291 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002292 assert(Replacement != this && "I didn't contain From!");
2293
Chris Lattner7a1450d2005-10-04 18:13:04 +00002294 // Everyone using this now uses the replacement.
2295 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002296
2297 // Delete the old constant!
2298 destroyConstant();
2299}
2300
2301void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002302 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002303 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002304 Constant *ToC = cast<Constant>(To);
2305
Chris Lattnerdff59112005-10-04 18:47:09 +00002306 unsigned OperandToUpdate = U-OperandList;
2307 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2308
Jim Laskeyc03caef2006-07-17 17:38:29 +00002309 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002310 Lookup.first.first = getType();
2311 Lookup.second = this;
2312 std::vector<Constant*> &Values = Lookup.first.second;
2313 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002314
Chris Lattnerdff59112005-10-04 18:47:09 +00002315
Chris Lattner8760ec72005-10-04 01:17:50 +00002316 // Fill values with the modified operands of the constant struct. Also,
2317 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002318 bool isAllZeros = false;
2319 if (!ToC->isNullValue()) {
2320 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2321 Values.push_back(cast<Constant>(O->get()));
2322 } else {
2323 isAllZeros = true;
2324 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2325 Constant *Val = cast<Constant>(O->get());
2326 Values.push_back(Val);
2327 if (isAllZeros) isAllZeros = Val->isNullValue();
2328 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002329 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002330 Values[OperandToUpdate] = ToC;
2331
Chris Lattner8760ec72005-10-04 01:17:50 +00002332 Constant *Replacement = 0;
2333 if (isAllZeros) {
2334 Replacement = ConstantAggregateZero::get(getType());
2335 } else {
2336 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002337 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002338 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002339 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002340 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002341
2342 if (Exists) {
2343 Replacement = I->second;
2344 } else {
2345 // Okay, the new shape doesn't exist in the system yet. Instead of
2346 // creating a new constant struct, inserting it, replaceallusesof'ing the
2347 // old with the new, then deleting the old... just update the current one
2348 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002349 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002350
Chris Lattnerdff59112005-10-04 18:47:09 +00002351 // Update to the new value.
2352 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002353 return;
2354 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002355 }
2356
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002357 assert(Replacement != this && "I didn't contain From!");
2358
Chris Lattner7a1450d2005-10-04 18:13:04 +00002359 // Everyone using this now uses the replacement.
2360 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002361
2362 // Delete the old constant!
2363 destroyConstant();
2364}
2365
Reid Spencerd84d35b2007-02-15 02:26:10 +00002366void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002367 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002368 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2369
2370 std::vector<Constant*> Values;
2371 Values.reserve(getNumOperands()); // Build replacement array...
2372 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2373 Constant *Val = getOperand(i);
2374 if (Val == From) Val = cast<Constant>(To);
2375 Values.push_back(Val);
2376 }
2377
Reid Spencerd84d35b2007-02-15 02:26:10 +00002378 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002379 assert(Replacement != this && "I didn't contain From!");
2380
Chris Lattner7a1450d2005-10-04 18:13:04 +00002381 // Everyone using this now uses the replacement.
2382 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002383
2384 // Delete the old constant!
2385 destroyConstant();
2386}
2387
2388void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002389 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002390 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2391 Constant *To = cast<Constant>(ToV);
2392
2393 Constant *Replacement = 0;
2394 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002395 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002396 Constant *Pointer = getOperand(0);
2397 Indices.reserve(getNumOperands()-1);
2398 if (Pointer == From) Pointer = To;
2399
2400 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2401 Constant *Val = getOperand(i);
2402 if (Val == From) Val = To;
2403 Indices.push_back(Val);
2404 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002405 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2406 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002407 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002408 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002409 if (Agg == From) Agg = To;
2410
Dan Gohman1ecaf452008-05-31 00:58:22 +00002411 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002412 Replacement = ConstantExpr::getExtractValue(Agg,
2413 &Indices[0], Indices.size());
2414 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002415 Constant *Agg = getOperand(0);
2416 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002417 if (Agg == From) Agg = To;
2418 if (Val == From) Val = To;
2419
Dan Gohman1ecaf452008-05-31 00:58:22 +00002420 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002421 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2422 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002424 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002426 } else if (getOpcode() == Instruction::Select) {
2427 Constant *C1 = getOperand(0);
2428 Constant *C2 = getOperand(1);
2429 Constant *C3 = getOperand(2);
2430 if (C1 == From) C1 = To;
2431 if (C2 == From) C2 = To;
2432 if (C3 == From) C3 = To;
2433 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002434 } else if (getOpcode() == Instruction::ExtractElement) {
2435 Constant *C1 = getOperand(0);
2436 Constant *C2 = getOperand(1);
2437 if (C1 == From) C1 = To;
2438 if (C2 == From) C2 = To;
2439 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002440 } else if (getOpcode() == Instruction::InsertElement) {
2441 Constant *C1 = getOperand(0);
2442 Constant *C2 = getOperand(1);
2443 Constant *C3 = getOperand(1);
2444 if (C1 == From) C1 = To;
2445 if (C2 == From) C2 = To;
2446 if (C3 == From) C3 = To;
2447 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2448 } else if (getOpcode() == Instruction::ShuffleVector) {
2449 Constant *C1 = getOperand(0);
2450 Constant *C2 = getOperand(1);
2451 Constant *C3 = getOperand(2);
2452 if (C1 == From) C1 = To;
2453 if (C2 == From) C2 = To;
2454 if (C3 == From) C3 = To;
2455 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002456 } else if (isCompare()) {
2457 Constant *C1 = getOperand(0);
2458 Constant *C2 = getOperand(1);
2459 if (C1 == From) C1 = To;
2460 if (C2 == From) C2 = To;
2461 if (getOpcode() == Instruction::ICmp)
2462 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002463 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002464 assert(getOpcode() == Instruction::FCmp);
2465 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002466 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002467 } else if (getNumOperands() == 2) {
2468 Constant *C1 = getOperand(0);
2469 Constant *C2 = getOperand(1);
2470 if (C1 == From) C1 = To;
2471 if (C2 == From) C2 = To;
2472 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2473 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002474 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002475 return;
2476 }
2477
2478 assert(Replacement != this && "I didn't contain From!");
2479
Chris Lattner7a1450d2005-10-04 18:13:04 +00002480 // Everyone using this now uses the replacement.
2481 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002482
2483 // Delete the old constant!
2484 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002485}
Nick Lewycky49f89192009-04-04 07:22:01 +00002486
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002487void MDNode::replaceElement(Value *From, Value *To) {
2488 SmallVector<Value*, 4> Values;
2489 Values.reserve(getNumElements()); // Build replacement array...
2490 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2491 Value *Val = getElement(i);
2492 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002493 Values.push_back(Val);
2494 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002495
Owen Anderson4118dde2009-07-16 23:44:30 +00002496 MDNode *Replacement =
2497 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002498 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002499
Nick Lewycky49f89192009-04-04 07:22:01 +00002500 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002501
Nick Lewycky49f89192009-04-04 07:22:01 +00002502 destroyConstant();
2503}