blob: 96f729a7e6641dee5185b5c97dc24b7361c21704 [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
Owen Andersonedb4a702009-07-24 23:12:02 +000014#include "LLVMContextImpl.h"
Chris Lattnerca142372002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000020#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000021#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000022#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000025#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000027#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000029#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000030#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000031#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000032#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000033#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000034#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000035#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000036#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000037#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000038using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000039
Chris Lattner2f7c9632001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000041// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000042//===----------------------------------------------------------------------===//
43
Owen Andersond830eb82009-06-18 19:10:19 +000044// Becomes a no-op when multithreading is disabled.
45ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000046
Chris Lattner3462ae32001-12-03 22:26:30 +000047void Constant::destroyConstantImpl() {
48 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000049 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000050 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000051 // but they don't know that. Because we only find out when the CPV is
52 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000053 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000054 //
55 while (!use_empty()) {
56 Value *V = use_back();
57#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000058 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000059 DOUT << "While deleting: " << *this
60 << "\n\nUse still stuck around after Def is destroyed: "
61 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000062#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000063 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000064 Constant *CV = cast<Constant>(V);
65 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000066
67 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000068 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000069 }
70
71 // Value has no outstanding references it is safe to delete it now...
72 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000073}
Chris Lattner2f7c9632001-06-06 20:29:01 +000074
Chris Lattner23dd1f62006-10-20 00:27:06 +000075/// canTrap - Return true if evaluation of this constant could trap. This is
76/// true for things like constant expressions that could divide by zero.
77bool Constant::canTrap() const {
78 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
79 // The only thing that could possibly trap are constant exprs.
80 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
81 if (!CE) return false;
82
83 // ConstantExpr traps if any operands can trap.
84 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
85 if (getOperand(i)->canTrap())
86 return true;
87
88 // Otherwise, only specific operations can trap.
89 switch (CE->getOpcode()) {
90 default:
91 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000092 case Instruction::UDiv:
93 case Instruction::SDiv:
94 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000095 case Instruction::URem:
96 case Instruction::SRem:
97 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000098 // Div and rem can trap if the RHS is not known to be non-zero.
99 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
100 return true;
101 return false;
102 }
103}
104
Chris Lattner4565ef52009-07-22 00:05:44 +0000105
106/// getRelocationInfo - This method classifies the entry according to
107/// whether or not it may generate a relocation entry. This must be
108/// conservative, so if it might codegen to a relocatable entry, it should say
109/// so. The return values are:
110///
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000111/// NoRelocation: This constant pool entry is guaranteed to never have a
112/// relocation applied to it (because it holds a simple constant like
113/// '4').
114/// LocalRelocation: This entry has relocations, but the entries are
115/// guaranteed to be resolvable by the static linker, so the dynamic
116/// linker will never see them.
117/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner4565ef52009-07-22 00:05:44 +0000118///
119/// FIXME: This really should not be in VMCore.
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000120Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
121 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000122 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000123 return LocalRelocation; // Local to this file/library.
124 return GlobalRelocations; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000125 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000126
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000127 PossibleRelocationsTy Result = NoRelocation;
Evan Chengf9e003b2007-03-08 00:59:12 +0000128 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner4565ef52009-07-22 00:05:44 +0000129 Result = std::max(Result, getOperand(i)->getRelocationInfo());
130
131 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000132}
133
Chris Lattner4565ef52009-07-22 00:05:44 +0000134
Chris Lattner2105d662008-07-10 00:28:11 +0000135/// getVectorElements - This method, which is only valid on constant of vector
136/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000137/// This handles breaking down a vector undef into undef elements, etc. For
138/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000139void Constant::getVectorElements(LLVMContext &Context,
140 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000141 assert(isa<VectorType>(getType()) && "Not a vector constant!");
142
143 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
144 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
145 Elts.push_back(CV->getOperand(i));
146 return;
147 }
148
149 const VectorType *VT = cast<VectorType>(getType());
150 if (isa<ConstantAggregateZero>(this)) {
151 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000152 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000153 return;
154 }
155
Chris Lattnerc5098a22008-07-14 05:10:41 +0000156 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000157 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000158 return;
159 }
160
161 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000162}
163
164
165
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000167// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168//===----------------------------------------------------------------------===//
169
Reid Spencerb31bffe2007-02-26 23:54:03 +0000170ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000171 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000172 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173}
174
Owen Andersonedb4a702009-07-24 23:12:02 +0000175// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
176// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
177// operator== and operator!= to ensure that the DenseMap doesn't attempt to
178// compare APInt's of different widths, which would violate an APInt class
179// invariant which generates an assertion.
180ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
181 // Get the corresponding integer type for the bit width of the value.
182 const IntegerType *ITy = Context.getIntegerType(V.getBitWidth());
183 // get an existing value or the insertion position
184 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
185
186 Context.pImpl->ConstantsLock.reader_acquire();
187 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
188 Context.pImpl->ConstantsLock.reader_release();
189
190 if (!Slot) {
191 sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock);
192 ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key];
193 if (!Slot) {
194 NewSlot = new ConstantInt(ITy, V);
195 }
196
197 return NewSlot;
198 } else {
199 return Slot;
200 }
201}
202
203Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
204 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
205 V, isSigned);
206
207 // For vectors, broadcast the value.
208 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
209 return Ty->getContext().getConstantVector(
210 std::vector<Constant *>(VTy->getNumElements(), C));
211
212 return C;
213}
214
215ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
216 bool isSigned) {
217 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
218}
219
220ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
221 return get(Ty, V, true);
222}
223
224Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
225 return get(Ty, V, true);
226}
227
228Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
229 ConstantInt *C = get(Ty->getContext(), V);
230 assert(C->getType() == Ty->getScalarType() &&
231 "ConstantInt type doesn't match the type implied by its value!");
232
233 // For vectors, broadcast the value.
234 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
235 return Ty->getContext().getConstantVector(
236 std::vector<Constant *>(VTy->getNumElements(), C));
237
238 return C;
239}
240
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000241//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000242// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000243//===----------------------------------------------------------------------===//
244
Daniel Dunbare974dc32009-07-17 18:33:52 +0000245#ifndef NDEBUG
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000246static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
247 if (Ty == Type::FloatTy)
248 return &APFloat::IEEEsingle;
249 if (Ty == Type::DoubleTy)
250 return &APFloat::IEEEdouble;
251 if (Ty == Type::X86_FP80Ty)
252 return &APFloat::x87DoubleExtended;
253 else if (Ty == Type::FP128Ty)
254 return &APFloat::IEEEquad;
255
256 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
257 return &APFloat::PPCDoubleDouble;
258}
Daniel Dunbare974dc32009-07-17 18:33:52 +0000259#endif
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000260
Dale Johannesend246b2c2007-08-30 00:23:21 +0000261ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
262 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000263 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
264 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265}
266
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000267bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000268 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000269}
270
Dale Johannesend246b2c2007-08-30 00:23:21 +0000271bool ConstantFP::isExactlyValue(const APFloat& V) const {
272 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000273}
274
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000275//===----------------------------------------------------------------------===//
276// ConstantXXX Classes
277//===----------------------------------------------------------------------===//
278
279
Chris Lattner3462ae32001-12-03 22:26:30 +0000280ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000281 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 : Constant(T, ConstantArrayVal,
283 OperandTraits<ConstantArray>::op_end(this) - V.size(),
284 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000285 assert(V.size() == T->getNumElements() &&
286 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000287 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000288 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
289 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000290 Constant *C = *I;
291 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000292 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000293 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000294 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000295 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296 }
297}
298
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000299
Chris Lattner3462ae32001-12-03 22:26:30 +0000300ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000301 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000302 : Constant(T, ConstantStructVal,
303 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
304 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000305 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000306 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000307 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000308 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
309 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000310 Constant *C = *I;
311 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000312 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000313 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000314 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000315 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000316 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000317 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000318 }
319}
320
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000321
Reid Spencerd84d35b2007-02-15 02:26:10 +0000322ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000323 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 : Constant(T, ConstantVectorVal,
325 OperandTraits<ConstantVector>::op_end(this) - V.size(),
326 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000327 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000328 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
329 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000330 Constant *C = *I;
331 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000332 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000333 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000334 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000335 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000336 }
337}
338
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000339
Gabor Greiff6caff662008-05-10 08:32:32 +0000340namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000341// We declare several classes private to this file, so use an anonymous
342// namespace
343namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000344
Gordon Henriksen14a55692007-12-10 02:14:30 +0000345/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
346/// behind the scenes to implement unary constant exprs.
347class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000348 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000349public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000350 // allocate space for exactly one operand
351 void *operator new(size_t s) {
352 return User::operator new(s, 1);
353 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000354 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000355 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
356 Op<0>() = C;
357 }
358 /// Transparently provide more efficient getOperand methods.
359 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000360};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000361
Gordon Henriksen14a55692007-12-10 02:14:30 +0000362/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
363/// behind the scenes to implement binary constant exprs.
364class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000365 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000366public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000367 // allocate space for exactly two operands
368 void *operator new(size_t s) {
369 return User::operator new(s, 2);
370 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000371 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000372 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000373 Op<0>() = C1;
374 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000375 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000376 /// Transparently provide more efficient getOperand methods.
377 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000378};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000379
Gordon Henriksen14a55692007-12-10 02:14:30 +0000380/// SelectConstantExpr - This class is private to Constants.cpp, and is used
381/// behind the scenes to implement select constant exprs.
382class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000383 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000384public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000385 // allocate space for exactly three operands
386 void *operator new(size_t s) {
387 return User::operator new(s, 3);
388 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000389 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000390 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000391 Op<0>() = C1;
392 Op<1>() = C2;
393 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000394 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000395 /// Transparently provide more efficient getOperand methods.
396 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000397};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000398
Gordon Henriksen14a55692007-12-10 02:14:30 +0000399/// ExtractElementConstantExpr - This class is private to
400/// Constants.cpp, and is used behind the scenes to implement
401/// extractelement constant exprs.
402class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000403 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000404public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000405 // allocate space for exactly two operands
406 void *operator new(size_t s) {
407 return User::operator new(s, 2);
408 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000409 ExtractElementConstantExpr(Constant *C1, Constant *C2)
410 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000411 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000412 Op<0>() = C1;
413 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000414 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000415 /// Transparently provide more efficient getOperand methods.
416 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000417};
Robert Bocchino23004482006-01-10 19:05:34 +0000418
Gordon Henriksen14a55692007-12-10 02:14:30 +0000419/// InsertElementConstantExpr - This class is private to
420/// Constants.cpp, and is used behind the scenes to implement
421/// insertelement constant exprs.
422class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000423 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000424public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000425 // allocate space for exactly three operands
426 void *operator new(size_t s) {
427 return User::operator new(s, 3);
428 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000429 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
430 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000431 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000432 Op<0>() = C1;
433 Op<1>() = C2;
434 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000435 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000436 /// Transparently provide more efficient getOperand methods.
437 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000438};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000439
Gordon Henriksen14a55692007-12-10 02:14:30 +0000440/// ShuffleVectorConstantExpr - This class is private to
441/// Constants.cpp, and is used behind the scenes to implement
442/// shufflevector constant exprs.
443class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000444 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000445public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000446 // allocate space for exactly three operands
447 void *operator new(size_t s) {
448 return User::operator new(s, 3);
449 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000450 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000451 : ConstantExpr(VectorType::get(
452 cast<VectorType>(C1->getType())->getElementType(),
453 cast<VectorType>(C3->getType())->getNumElements()),
454 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000455 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000456 Op<0>() = C1;
457 Op<1>() = C2;
458 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000459 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000460 /// Transparently provide more efficient getOperand methods.
461 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000462};
463
Dan Gohman12fce772008-05-15 19:50:34 +0000464/// ExtractValueConstantExpr - This class is private to
465/// Constants.cpp, and is used behind the scenes to implement
466/// extractvalue constant exprs.
467class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000468 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000469public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000470 // allocate space for exactly one operand
471 void *operator new(size_t s) {
472 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000473 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000474 ExtractValueConstantExpr(Constant *Agg,
475 const SmallVector<unsigned, 4> &IdxList,
476 const Type *DestTy)
477 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
478 Indices(IdxList) {
479 Op<0>() = Agg;
480 }
481
Dan Gohman7bb04502008-05-31 19:09:08 +0000482 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000483 const SmallVector<unsigned, 4> Indices;
484
Dan Gohman12fce772008-05-15 19:50:34 +0000485 /// Transparently provide more efficient getOperand methods.
486 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
487};
488
489/// InsertValueConstantExpr - This class is private to
490/// Constants.cpp, and is used behind the scenes to implement
491/// insertvalue constant exprs.
492class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000493 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000494public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000495 // allocate space for exactly one operand
496 void *operator new(size_t s) {
497 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000498 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000499 InsertValueConstantExpr(Constant *Agg, Constant *Val,
500 const SmallVector<unsigned, 4> &IdxList,
501 const Type *DestTy)
502 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
503 Indices(IdxList) {
504 Op<0>() = Agg;
505 Op<1>() = Val;
506 }
507
Dan Gohman7bb04502008-05-31 19:09:08 +0000508 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000509 const SmallVector<unsigned, 4> Indices;
510
Dan Gohman12fce772008-05-15 19:50:34 +0000511 /// Transparently provide more efficient getOperand methods.
512 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
513};
514
515
Gordon Henriksen14a55692007-12-10 02:14:30 +0000516/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
517/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000518class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000519 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000520 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000521public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000522 static GetElementPtrConstantExpr *Create(Constant *C,
523 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000524 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000525 return
526 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000527 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000528 /// Transparently provide more efficient getOperand methods.
529 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000530};
531
532// CompareConstantExpr - This class is private to Constants.cpp, and is used
533// behind the scenes to implement ICmp and FCmp constant expressions. This is
534// needed in order to store the predicate value for these instructions.
535struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000536 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
537 // allocate space for exactly two operands
538 void *operator new(size_t s) {
539 return User::operator new(s, 2);
540 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000541 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000542 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
543 unsigned short pred, Constant* LHS, Constant* RHS)
544 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000545 Op<0>() = LHS;
546 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000547 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 /// Transparently provide more efficient getOperand methods.
549 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000550};
551
552} // end anonymous namespace
553
Gabor Greiff6caff662008-05-10 08:32:32 +0000554template <>
555struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
556};
557DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
558
559template <>
560struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
561};
562DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
563
564template <>
565struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
566};
567DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
568
569template <>
570struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
571};
572DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
573
574template <>
575struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
576};
577DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
578
579template <>
580struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
581};
582DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
583
Dan Gohman12fce772008-05-15 19:50:34 +0000584template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000585struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000586};
Dan Gohman12fce772008-05-15 19:50:34 +0000587DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
588
589template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000590struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000591};
Dan Gohman12fce772008-05-15 19:50:34 +0000592DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
593
Gabor Greiff6caff662008-05-10 08:32:32 +0000594template <>
595struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
596};
597
598GetElementPtrConstantExpr::GetElementPtrConstantExpr
599 (Constant *C,
600 const std::vector<Constant*> &IdxList,
601 const Type *DestTy)
602 : ConstantExpr(DestTy, Instruction::GetElementPtr,
603 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
604 - (IdxList.size()+1),
605 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000606 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000607 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000608 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000609}
610
611DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
612
613
614template <>
615struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
616};
617DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
618
619
620} // End llvm namespace
621
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000622
623// Utility function for determining if a ConstantExpr is a CastOp or not. This
624// can't be inline because we don't want to #include Instruction.h into
625// Constant.h
626bool ConstantExpr::isCast() const {
627 return Instruction::isCast(getOpcode());
628}
629
Reid Spenceree3c9912006-12-04 05:19:50 +0000630bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000631 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000632}
633
Dan Gohman1ecaf452008-05-31 00:58:22 +0000634bool ConstantExpr::hasIndices() const {
635 return getOpcode() == Instruction::ExtractValue ||
636 getOpcode() == Instruction::InsertValue;
637}
638
639const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
640 if (const ExtractValueConstantExpr *EVCE =
641 dyn_cast<ExtractValueConstantExpr>(this))
642 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000643
644 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000645}
646
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000647unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000648 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000649 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000650 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000651}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000652
Chris Lattner7c1018a2006-07-14 19:37:40 +0000653/// getWithOperandReplaced - Return a constant expression identical to this
654/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000655Constant *
656ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000657 assert(OpNo < getNumOperands() && "Operand num is out of range!");
658 assert(Op->getType() == getOperand(OpNo)->getType() &&
659 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000660 if (getOperand(OpNo) == Op)
661 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000662
Chris Lattner227816342006-07-14 22:20:01 +0000663 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000664 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000665 case Instruction::Trunc:
666 case Instruction::ZExt:
667 case Instruction::SExt:
668 case Instruction::FPTrunc:
669 case Instruction::FPExt:
670 case Instruction::UIToFP:
671 case Instruction::SIToFP:
672 case Instruction::FPToUI:
673 case Instruction::FPToSI:
674 case Instruction::PtrToInt:
675 case Instruction::IntToPtr:
676 case Instruction::BitCast:
677 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000678 case Instruction::Select:
679 Op0 = (OpNo == 0) ? Op : getOperand(0);
680 Op1 = (OpNo == 1) ? Op : getOperand(1);
681 Op2 = (OpNo == 2) ? Op : getOperand(2);
682 return ConstantExpr::getSelect(Op0, Op1, Op2);
683 case Instruction::InsertElement:
684 Op0 = (OpNo == 0) ? Op : getOperand(0);
685 Op1 = (OpNo == 1) ? Op : getOperand(1);
686 Op2 = (OpNo == 2) ? Op : getOperand(2);
687 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
688 case Instruction::ExtractElement:
689 Op0 = (OpNo == 0) ? Op : getOperand(0);
690 Op1 = (OpNo == 1) ? Op : getOperand(1);
691 return ConstantExpr::getExtractElement(Op0, Op1);
692 case Instruction::ShuffleVector:
693 Op0 = (OpNo == 0) ? Op : getOperand(0);
694 Op1 = (OpNo == 1) ? Op : getOperand(1);
695 Op2 = (OpNo == 2) ? Op : getOperand(2);
696 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000697 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000698 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000699 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000700 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000701 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000702 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000703 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000704 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000705 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000706 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000707 default:
708 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000709 Op0 = (OpNo == 0) ? Op : getOperand(0);
710 Op1 = (OpNo == 1) ? Op : getOperand(1);
711 return ConstantExpr::get(getOpcode(), Op0, Op1);
712 }
713}
714
715/// getWithOperands - This returns the current constant expression with the
716/// operands replaced with the specified values. The specified operands must
717/// match count and type with the existing ones.
718Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000719getWithOperands(Constant* const *Ops, unsigned NumOps) const {
720 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000721 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000722 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000723 assert(Ops[i]->getType() == getOperand(i)->getType() &&
724 "Operand type mismatch!");
725 AnyChange |= Ops[i] != getOperand(i);
726 }
727 if (!AnyChange) // No operands changed, return self.
728 return const_cast<ConstantExpr*>(this);
729
730 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000731 case Instruction::Trunc:
732 case Instruction::ZExt:
733 case Instruction::SExt:
734 case Instruction::FPTrunc:
735 case Instruction::FPExt:
736 case Instruction::UIToFP:
737 case Instruction::SIToFP:
738 case Instruction::FPToUI:
739 case Instruction::FPToSI:
740 case Instruction::PtrToInt:
741 case Instruction::IntToPtr:
742 case Instruction::BitCast:
743 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000744 case Instruction::Select:
745 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
746 case Instruction::InsertElement:
747 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
748 case Instruction::ExtractElement:
749 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
750 case Instruction::ShuffleVector:
751 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000752 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000753 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000754 case Instruction::ICmp:
755 case Instruction::FCmp:
756 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000757 default:
758 assert(getNumOperands() == 2 && "Must be binary operator?");
759 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000760 }
761}
762
Chris Lattner2f7c9632001-06-06 20:29:01 +0000763
764//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000765// isValueValidForType implementations
766
Reid Spencere7334722006-12-19 01:28:19 +0000767bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000768 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000769 if (Ty == Type::Int1Ty)
770 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000771 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000772 return true; // always true, has to fit in largest type
773 uint64_t Max = (1ll << NumBits) - 1;
774 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000775}
776
Reid Spencere0fc4df2006-10-20 07:07:24 +0000777bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000778 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000779 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000780 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000781 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000782 return true; // always true, has to fit in largest type
783 int64_t Min = -(1ll << (NumBits-1));
784 int64_t Max = (1ll << (NumBits-1)) - 1;
785 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000786}
787
Dale Johannesend246b2c2007-08-30 00:23:21 +0000788bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
789 // convert modifies in place, so make a copy.
790 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000791 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000792 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000793 default:
794 return false; // These can't be represented as floating point!
795
Dale Johannesend246b2c2007-08-30 00:23:21 +0000796 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000797 case Type::FloatTyID: {
798 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
799 return true;
800 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
801 return !losesInfo;
802 }
803 case Type::DoubleTyID: {
804 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
805 &Val2.getSemantics() == &APFloat::IEEEdouble)
806 return true;
807 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
808 return !losesInfo;
809 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000810 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000811 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
812 &Val2.getSemantics() == &APFloat::IEEEdouble ||
813 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000814 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000815 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
816 &Val2.getSemantics() == &APFloat::IEEEdouble ||
817 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000818 case Type::PPC_FP128TyID:
819 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
820 &Val2.getSemantics() == &APFloat::IEEEdouble ||
821 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000822 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000823}
Chris Lattner9655e542001-07-20 19:16:02 +0000824
Chris Lattner49d855c2001-09-07 16:46:31 +0000825//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000826// Factory Function Implementation
827
Dan Gohman92b551b2009-03-03 02:55:14 +0000828/// destroyConstant - Remove the constant from the constant table...
829///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000830void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +0000831 // Implicitly locked.
Owen Anderson39ede7b2009-07-21 20:13:12 +0000832 getType()->getContext().erase(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000833 destroyConstantImpl();
834}
835
Dan Gohman92b551b2009-03-03 02:55:14 +0000836/// destroyConstant - Remove the constant from the constant table...
837///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000838void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +0000839 // Implicitly locked.
Owen Anderson3d344922009-07-21 20:55:28 +0000840 getType()->getContext().erase(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000841 destroyConstantImpl();
842}
843
Reid Spencer2546b762007-01-26 07:37:34 +0000844/// isString - This method returns true if the array is an array of i8, and
845/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000846bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000847 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000848 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000849 return false;
850 // Check the elements to make sure they are all integers, not constant
851 // expressions.
852 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
853 if (!isa<ConstantInt>(getOperand(i)))
854 return false;
855 return true;
856}
857
Evan Cheng3763c5b2006-10-26 19:15:05 +0000858/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +0000859/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +0000860/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +0000861bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000862 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000863 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +0000864 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +0000865
Evan Chenge974da62006-10-26 21:48:03 +0000866 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +0000867 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +0000868 return false;
869 // Other elements must be non-null integers.
870 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
871 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +0000872 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +0000873 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +0000874 return false;
875 }
Evan Cheng3763c5b2006-10-26 19:15:05 +0000876 return true;
877}
878
879
Dan Gohman92b551b2009-03-03 02:55:14 +0000880/// getAsString - If the sub-element type of this array is i8
881/// then this method converts the array to an std::string and returns it.
882/// Otherwise, it asserts out.
883///
Chris Lattner81fabb02002-08-26 17:53:56 +0000884std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000885 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000886 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +0000887 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +0000888 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +0000889 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +0000890 return Result;
891}
892
893
Chris Lattner3462ae32001-12-03 22:26:30 +0000894//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000895//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000896
Chris Lattner189d19f2003-11-21 20:23:48 +0000897namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000898
Chris Lattner49d855c2001-09-07 16:46:31 +0000899}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000900
Chris Lattnerd7a73302001-10-13 06:57:33 +0000901// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000902//
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000903void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +0000904 // Implicitly locked.
Owen Anderson909f6002009-07-23 23:25:33 +0000905 getType()->getContext().erase(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000906 destroyConstantImpl();
907}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000908
Brian Gaeke02209042004-08-20 06:00:58 +0000909// destroyConstant - Remove the constant from the constant table...
910//
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000911void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +0000912 // Implicitly locked.
Owen Anderson0348a132009-07-24 00:36:24 +0000913 getType()->getContext().erase(this);
Brian Gaeke02209042004-08-20 06:00:58 +0000914 destroyConstantImpl();
915}
916
Dan Gohman30978072007-05-24 14:36:04 +0000917/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +0000918/// is set to all ones.
919/// @returns true iff this constant's emements are all set to all ones.
920/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +0000921bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +0000922 // Check out first element.
923 const Constant *Elt = getOperand(0);
924 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
925 if (!CI || !CI->isAllOnesValue()) return false;
926 // Then make sure all remaining elements point to the same value.
927 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
928 if (getOperand(I) != Elt) return false;
929 }
930 return true;
931}
932
Dan Gohman07159202007-10-17 17:51:30 +0000933/// getSplatValue - If this is a splat constant, where all of the
934/// elements have the same value, return that value. Otherwise return null.
935Constant *ConstantVector::getSplatValue() {
936 // Check out first element.
937 Constant *Elt = getOperand(0);
938 // Then make sure all remaining elements point to the same value.
939 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
940 if (getOperand(I) != Elt) return 0;
941 return Elt;
942}
943
Chris Lattner3462ae32001-12-03 22:26:30 +0000944//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000945//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000946
Chris Lattner189d19f2003-11-21 20:23:48 +0000947namespace llvm {
948 // ConstantPointerNull does not take extra "value" argument...
949 template<class ValType>
950 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
951 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
952 return new ConstantPointerNull(Ty);
953 }
954 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000955
Chris Lattner189d19f2003-11-21 20:23:48 +0000956 template<>
957 struct ConvertConstantType<ConstantPointerNull, PointerType> {
958 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
959 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000960 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +0000961 assert(New != OldC && "Didn't replace constant??");
962 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000963 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +0000964 }
965 };
966}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000967
Chris Lattner69edc982006-09-28 00:35:06 +0000968static ManagedStatic<ValueMap<char, PointerType,
969 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000970
Chris Lattner3e650af2004-08-04 04:48:01 +0000971static char getValType(ConstantPointerNull *) {
972 return 0;
973}
974
975
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000976ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +0000977 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000978 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000979}
980
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000981// destroyConstant - Remove the constant from the constant table...
982//
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000983void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +0000984 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000985 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000986 destroyConstantImpl();
987}
988
989
Chris Lattnerd5f67d82004-10-16 18:07:16 +0000990//---- UndefValue::get() implementation...
991//
992
993namespace llvm {
994 // UndefValue does not take extra "value" argument...
995 template<class ValType>
996 struct ConstantCreator<UndefValue, Type, ValType> {
997 static UndefValue *create(const Type *Ty, const ValType &V) {
998 return new UndefValue(Ty);
999 }
1000 };
1001
1002 template<>
1003 struct ConvertConstantType<UndefValue, Type> {
1004 static void convert(UndefValue *OldC, const Type *NewTy) {
1005 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001006 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001007 assert(New != OldC && "Didn't replace constant??");
1008 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001009 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001010 }
1011 };
1012}
1013
Chris Lattner69edc982006-09-28 00:35:06 +00001014static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001015
1016static char getValType(UndefValue *) {
1017 return 0;
1018}
1019
1020
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001021UndefValue *UndefValue::get(const Type *Ty) {
1022 // Implicitly locked.
1023 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001024}
1025
1026// destroyConstant - Remove the constant from the constant table.
1027//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001028void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001029 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001030 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001031 destroyConstantImpl();
1032}
1033
Nick Lewycky49f89192009-04-04 07:22:01 +00001034//---- MDNode::get() implementation
1035//
1036
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001037MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Devang Patele059ba6e2009-07-23 01:07:34 +00001038 : MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001039 for (unsigned i = 0; i != NumVals; ++i)
Devang Patele059ba6e2009-07-23 01:07:34 +00001040 Node.push_back(WeakVH(Vals[i]));
Nick Lewycky49f89192009-04-04 07:22:01 +00001041}
1042
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001043void MDNode::Profile(FoldingSetNodeID &ID) const {
1044 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001045 ID.AddPointer(*I);
1046}
1047
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001048//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001049//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001050
Dan Gohmand78c4002008-05-13 00:00:25 +00001051namespace {
1052
Reid Spenceree3c9912006-12-04 05:19:50 +00001053struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001054 typedef SmallVector<unsigned, 4> IndexList;
1055
1056 ExprMapKeyType(unsigned opc,
1057 const std::vector<Constant*> &ops,
1058 unsigned short pred = 0,
1059 const IndexList &inds = IndexList())
1060 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001061 uint16_t opcode;
1062 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001063 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001064 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001065 bool operator==(const ExprMapKeyType& that) const {
1066 return this->opcode == that.opcode &&
1067 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001068 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001069 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001070 }
1071 bool operator<(const ExprMapKeyType & that) const {
1072 return this->opcode < that.opcode ||
1073 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1074 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001075 this->operands < that.operands) ||
1076 (this->opcode == that.opcode && this->predicate == that.predicate &&
1077 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001078 }
1079
1080 bool operator!=(const ExprMapKeyType& that) const {
1081 return !(*this == that);
1082 }
1083};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001084
Dan Gohmand78c4002008-05-13 00:00:25 +00001085}
1086
Chris Lattner189d19f2003-11-21 20:23:48 +00001087namespace llvm {
1088 template<>
1089 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001090 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1091 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001092 if (Instruction::isCast(V.opcode))
1093 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1094 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001095 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001096 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1097 if (V.opcode == Instruction::Select)
1098 return new SelectConstantExpr(V.operands[0], V.operands[1],
1099 V.operands[2]);
1100 if (V.opcode == Instruction::ExtractElement)
1101 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1102 if (V.opcode == Instruction::InsertElement)
1103 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1104 V.operands[2]);
1105 if (V.opcode == Instruction::ShuffleVector)
1106 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1107 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001108 if (V.opcode == Instruction::InsertValue)
1109 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1110 V.indices, Ty);
1111 if (V.opcode == Instruction::ExtractValue)
1112 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001113 if (V.opcode == Instruction::GetElementPtr) {
1114 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001115 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001116 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001117
Reid Spenceree3c9912006-12-04 05:19:50 +00001118 // The compare instructions are weird. We have to encode the predicate
1119 // value and it is combined with the instruction opcode by multiplying
1120 // the opcode by one hundred. We must decode this to get the predicate.
1121 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001122 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001123 V.operands[0], V.operands[1]);
1124 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001125 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1126 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001127 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001128 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001129 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001130 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001131
Chris Lattner189d19f2003-11-21 20:23:48 +00001132 template<>
1133 struct ConvertConstantType<ConstantExpr, Type> {
1134 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1135 Constant *New;
1136 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001137 case Instruction::Trunc:
1138 case Instruction::ZExt:
1139 case Instruction::SExt:
1140 case Instruction::FPTrunc:
1141 case Instruction::FPExt:
1142 case Instruction::UIToFP:
1143 case Instruction::SIToFP:
1144 case Instruction::FPToUI:
1145 case Instruction::FPToSI:
1146 case Instruction::PtrToInt:
1147 case Instruction::IntToPtr:
1148 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001149 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001150 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001151 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001152 case Instruction::Select:
1153 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1154 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001155 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001156 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001157 default:
1158 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001159 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001160 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001161 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001162 break;
1163 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001164 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001165 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001166 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001167 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001168 break;
1169 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001170
Chris Lattner189d19f2003-11-21 20:23:48 +00001171 assert(New != OldC && "Didn't replace constant??");
1172 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001173 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001174 }
1175 };
1176} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001177
1178
Chris Lattner3e650af2004-08-04 04:48:01 +00001179static ExprMapKeyType getValType(ConstantExpr *CE) {
1180 std::vector<Constant*> Operands;
1181 Operands.reserve(CE->getNumOperands());
1182 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1183 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001184 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001185 CE->isCompare() ? CE->getPredicate() : 0,
1186 CE->hasIndices() ?
1187 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001188}
1189
Chris Lattner69edc982006-09-28 00:35:06 +00001190static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1191 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001192
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001193/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001194/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001195static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001196 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001197 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001198 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001199 if (Constant *FC =
1200 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001201 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001202
Vikram S. Adve4c485332002-07-15 18:19:33 +00001203 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001204 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001205 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001206
Owen Anderson61794042009-06-17 20:10:08 +00001207 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001208 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001209}
Reid Spencerf37dc652006-12-05 19:14:13 +00001210
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001211Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001212 Instruction::CastOps opc = Instruction::CastOps(oc);
1213 assert(Instruction::isCast(opc) && "opcode out of range");
1214 assert(C && Ty && "Null arguments to getCast");
1215 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1216
1217 switch (opc) {
1218 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001219 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001220 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001221 case Instruction::Trunc: return getTrunc(C, Ty);
1222 case Instruction::ZExt: return getZExt(C, Ty);
1223 case Instruction::SExt: return getSExt(C, Ty);
1224 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1225 case Instruction::FPExt: return getFPExtend(C, Ty);
1226 case Instruction::UIToFP: return getUIToFP(C, Ty);
1227 case Instruction::SIToFP: return getSIToFP(C, Ty);
1228 case Instruction::FPToUI: return getFPToUI(C, Ty);
1229 case Instruction::FPToSI: return getFPToSI(C, Ty);
1230 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1231 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1232 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001233 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001234 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001235}
1236
Reid Spencer5c140882006-12-04 20:17:56 +00001237Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001238 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001239 return getCast(Instruction::BitCast, C, Ty);
1240 return getCast(Instruction::ZExt, C, Ty);
1241}
1242
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001243Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001244 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001245 return getCast(Instruction::BitCast, C, Ty);
1246 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001247}
1248
1249Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001250 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001251 return getCast(Instruction::BitCast, C, Ty);
1252 return getCast(Instruction::Trunc, C, Ty);
1253}
1254
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001255Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001256 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001257 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001258
Chris Lattner03c49532007-01-15 02:27:26 +00001259 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001260 return getCast(Instruction::PtrToInt, S, Ty);
1261 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001262}
1263
Reid Spencer56521c42006-12-12 00:51:07 +00001264Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1265 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001266 assert(C->getType()->isIntOrIntVector() &&
1267 Ty->isIntOrIntVector() && "Invalid cast");
1268 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1269 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001270 Instruction::CastOps opcode =
1271 (SrcBits == DstBits ? Instruction::BitCast :
1272 (SrcBits > DstBits ? Instruction::Trunc :
1273 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1274 return getCast(opcode, C, Ty);
1275}
1276
1277Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001278 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001279 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001280 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1281 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001282 if (SrcBits == DstBits)
1283 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001284 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001285 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001286 return getCast(opcode, C, Ty);
1287}
1288
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001289Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001290#ifndef NDEBUG
1291 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1292 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1293#endif
1294 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1295 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1296 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1297 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001298 "SrcTy must be larger than DestTy for Trunc!");
1299
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001300 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001301}
1302
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001303Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001304#ifndef NDEBUG
1305 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1306 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1307#endif
1308 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1309 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1310 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1311 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001312 "SrcTy must be smaller than DestTy for SExt!");
1313
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001314 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001315}
1316
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001317Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001318#ifndef NDEBUG
1319 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1320 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1321#endif
1322 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1323 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1324 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1325 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001326 "SrcTy must be smaller than DestTy for ZExt!");
1327
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001328 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001329}
1330
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001331Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001332#ifndef NDEBUG
1333 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1334 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1335#endif
1336 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1337 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1338 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001339 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001340 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001341}
1342
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001343Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001344#ifndef NDEBUG
1345 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1346 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1347#endif
1348 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1349 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1350 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001351 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001352 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001353}
1354
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001355Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001356#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001357 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1358 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001359#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001360 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1361 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1362 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001363 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001364}
1365
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001366Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001367#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001368 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1369 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001370#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001371 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1372 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001373 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001374 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001375}
1376
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001377Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001378#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001379 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1380 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001381#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001382 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1383 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1384 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001385 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001386}
1387
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001388Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001389#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001390 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1391 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001392#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001393 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1394 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1395 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001396 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001397}
1398
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001399Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001400 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001401 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001402 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001403}
1404
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001405Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001406 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001407 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001408 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001409}
1410
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001411Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001412 // BitCast implies a no-op cast of type only. No bits change. However, you
1413 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001414#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001415 const Type *SrcTy = C->getType();
1416 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001417 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001418
1419 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1420 // or nonptr->ptr). For all the other types, the cast is okay if source and
1421 // destination bit widths are identical.
1422 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1423 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001424#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001425 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001426
1427 // It is common to ask for a bitcast of a value to its own type, handle this
1428 // speedily.
1429 if (C->getType() == DstTy) return C;
1430
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001431 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001432}
1433
Chris Lattnerb50d1352003-10-05 00:17:43 +00001434Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001435 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001436 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001437 assert(Opcode >= Instruction::BinaryOpsBegin &&
1438 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001439 "Invalid opcode in binary constant expression");
1440 assert(C1->getType() == C2->getType() &&
1441 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001442
Reid Spencer542964f2007-01-11 18:21:29 +00001443 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001444 if (Constant *FC = ConstantFoldBinaryInstruction(
1445 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001446 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001447
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001448 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001449 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001450
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001451 // Implicitly locked.
1452 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001453}
1454
Reid Spencer266e42b2006-12-23 06:05:41 +00001455Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001456 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001457 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001458 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001459 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1460 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1461 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1462 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1463 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1464 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001465 return getFCmp(predicate, C1, C2);
1466
Nate Begemanc96e2e42008-07-25 17:35:37 +00001467 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1468 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1469 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1470 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001471 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001472 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001473}
1474
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001475Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001476 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1477 if (C1->getType()->isFPOrFPVector()) {
1478 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1479 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1480 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1481 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001482#ifndef NDEBUG
1483 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001484 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001485 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001486 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001487 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001488 assert(C1->getType()->isIntOrIntVector() &&
1489 "Tried to create an integer operation on a non-integer type!");
1490 break;
1491 case Instruction::FAdd:
1492 case Instruction::FSub:
1493 case Instruction::FMul:
1494 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1495 assert(C1->getType()->isFPOrFPVector() &&
1496 "Tried to create a floating-point operation on a "
1497 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001498 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001499 case Instruction::UDiv:
1500 case Instruction::SDiv:
1501 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001502 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001503 "Tried to create an arithmetic operation on a non-arithmetic type!");
1504 break;
1505 case Instruction::FDiv:
1506 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001507 assert(C1->getType()->isFPOrFPVector() &&
1508 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001509 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001510 case Instruction::URem:
1511 case Instruction::SRem:
1512 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001513 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001514 "Tried to create an arithmetic operation on a non-arithmetic type!");
1515 break;
1516 case Instruction::FRem:
1517 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001518 assert(C1->getType()->isFPOrFPVector() &&
1519 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001520 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001521 case Instruction::And:
1522 case Instruction::Or:
1523 case Instruction::Xor:
1524 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001525 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001526 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001527 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001528 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001529 case Instruction::LShr:
1530 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001531 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001532 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001533 "Tried to create a shift operation on a non-integer type!");
1534 break;
1535 default:
1536 break;
1537 }
1538#endif
1539
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001540 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001541}
1542
Reid Spencer266e42b2006-12-23 06:05:41 +00001543Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001544 Constant *C1, Constant *C2) {
1545 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001546 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001547}
1548
Chris Lattner6e415c02004-03-12 05:54:04 +00001549Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001550 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001551 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001552
1553 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001554 if (Constant *SC = ConstantFoldSelectInstruction(
1555 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001556 return SC; // Fold common cases
1557
1558 std::vector<Constant*> argVec(3, C);
1559 argVec[1] = V1;
1560 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001561 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001562
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001563 // Implicitly locked.
1564 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001565}
1566
Chris Lattnerb50d1352003-10-05 00:17:43 +00001567Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001568 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001569 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001570 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1571 Idxs+NumIdx) ==
1572 cast<PointerType>(ReqTy)->getElementType() &&
1573 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001574
Owen Anderson53a52212009-07-13 04:09:18 +00001575 if (Constant *FC = ConstantFoldGetElementPtr(
1576 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001577 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001578
Chris Lattnerb50d1352003-10-05 00:17:43 +00001579 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001580 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001581 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001582 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001583 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001584 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001585 for (unsigned i = 0; i != NumIdx; ++i)
1586 ArgVec.push_back(cast<Constant>(Idxs[i]));
1587 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001588
1589 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001590 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001591}
1592
Chris Lattner302116a2007-01-31 04:40:28 +00001593Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001594 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001595 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001596 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001597 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001598 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001599 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001600 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001601}
1602
Chris Lattner302116a2007-01-31 04:40:28 +00001603Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001604 unsigned NumIdx) {
1605 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001606}
1607
Chris Lattner302116a2007-01-31 04:40:28 +00001608
Reid Spenceree3c9912006-12-04 05:19:50 +00001609Constant *
1610ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1611 assert(LHS->getType() == RHS->getType());
1612 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1613 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1614
Owen Anderson53a52212009-07-13 04:09:18 +00001615 if (Constant *FC = ConstantFoldCompareInstruction(
1616 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001617 return FC; // Fold a few common cases...
1618
1619 // Look up the constant in the table first to ensure uniqueness
1620 std::vector<Constant*> ArgVec;
1621 ArgVec.push_back(LHS);
1622 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001623 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001624 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001625
1626 // Implicitly locked.
1627 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001628}
1629
1630Constant *
1631ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1632 assert(LHS->getType() == RHS->getType());
1633 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1634
Owen Anderson53a52212009-07-13 04:09:18 +00001635 if (Constant *FC = ConstantFoldCompareInstruction(
1636 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001637 return FC; // Fold a few common cases...
1638
1639 // Look up the constant in the table first to ensure uniqueness
1640 std::vector<Constant*> ArgVec;
1641 ArgVec.push_back(LHS);
1642 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001643 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001644 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001645
1646 // Implicitly locked.
1647 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001648}
1649
Robert Bocchino23004482006-01-10 19:05:34 +00001650Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1651 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001652 if (Constant *FC = ConstantFoldExtractElementInstruction(
1653 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001654 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001655 // Look up the constant in the table first to ensure uniqueness
1656 std::vector<Constant*> ArgVec(1, Val);
1657 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001658 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001659
1660 // Implicitly locked.
1661 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001662}
1663
1664Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001665 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001666 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001667 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001668 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001669 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001670 Val, Idx);
1671}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001672
Robert Bocchinoca27f032006-01-17 20:07:22 +00001673Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1674 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001675 if (Constant *FC = ConstantFoldInsertElementInstruction(
1676 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001677 return FC; // Fold a few common cases...
1678 // Look up the constant in the table first to ensure uniqueness
1679 std::vector<Constant*> ArgVec(1, Val);
1680 ArgVec.push_back(Elt);
1681 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001682 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001683
1684 // Implicitly locked.
1685 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001686}
1687
1688Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1689 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001690 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001691 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001692 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001693 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001694 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001695 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001696 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001697}
1698
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001699Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1700 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001701 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1702 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001703 return FC; // Fold a few common cases...
1704 // Look up the constant in the table first to ensure uniqueness
1705 std::vector<Constant*> ArgVec(1, V1);
1706 ArgVec.push_back(V2);
1707 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001708 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001709
1710 // Implicitly locked.
1711 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001712}
1713
1714Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1715 Constant *Mask) {
1716 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1717 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001718
1719 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1720 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1721 const Type *ShufTy = VectorType::get(EltTy, NElts);
1722 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001723}
1724
Dan Gohman12fce772008-05-15 19:50:34 +00001725Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1726 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001727 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001728 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1729 Idxs+NumIdx) == Val->getType() &&
1730 "insertvalue indices invalid!");
1731 assert(Agg->getType() == ReqTy &&
1732 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001733 assert(Agg->getType()->isFirstClassType() &&
1734 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001735 Constant *FC = ConstantFoldInsertValueInstruction(
1736 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001737 assert(FC && "InsertValue constant expr couldn't be folded!");
1738 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001739}
1740
1741Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001742 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001743 assert(Agg->getType()->isFirstClassType() &&
1744 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001745
Dan Gohman0752bff2008-05-23 00:36:11 +00001746 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001747#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001748 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001749 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001750#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001751 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001752 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1753}
1754
1755Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001756 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001757 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1758 Idxs+NumIdx) == ReqTy &&
1759 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001760 assert(Agg->getType()->isFirstClassType() &&
1761 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001762 Constant *FC = ConstantFoldExtractValueInstruction(
1763 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001764 assert(FC && "ExtractValue constant expr couldn't be folded!");
1765 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001766}
1767
1768Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001769 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001770 assert(Agg->getType()->isFirstClassType() &&
1771 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001772
1773 const Type *ReqTy =
1774 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1775 assert(ReqTy && "extractvalue indices invalid!");
1776 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1777}
1778
Vikram S. Adve4c485332002-07-15 18:19:33 +00001779// destroyConstant - Remove the constant from the constant table...
1780//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001781void ConstantExpr::destroyConstant() {
1782 // Implicitly locked.
1783 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001784 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001785}
1786
Chris Lattner3cd8c562002-07-30 18:54:25 +00001787const char *ConstantExpr::getOpcodeName() const {
1788 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001789}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001790
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001791//===----------------------------------------------------------------------===//
1792// replaceUsesOfWithOnConstant implementations
1793
Chris Lattner913849b2007-08-21 00:55:23 +00001794/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1795/// 'From' to be uses of 'To'. This must update the uniquing data structures
1796/// etc.
1797///
1798/// Note that we intentionally replace all uses of From with To here. Consider
1799/// a large array that uses 'From' 1000 times. By handling this case all here,
1800/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1801/// single invocation handles all 1000 uses. Handling them one at a time would
1802/// work, but would be really slow because it would have to unique each updated
1803/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001804void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001805 Use *U) {
Owen Anderson3d344922009-07-21 20:55:28 +00001806 Constant *Replacement =
1807 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
1808
1809 if (!Replacement) return;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001810
1811 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001812 assert(Replacement != this && "I didn't contain From!");
1813
Chris Lattner7a1450d2005-10-04 18:13:04 +00001814 // Everyone using this now uses the replacement.
1815 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001816
1817 // Delete the old constant!
1818 destroyConstant();
1819}
1820
1821void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001822 Use *U) {
Owen Anderson909f6002009-07-23 23:25:33 +00001823 Constant* Replacement =
1824 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
1825 if (!Replacement) return;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001826
Chris Lattner7a1450d2005-10-04 18:13:04 +00001827 // Everyone using this now uses the replacement.
1828 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001829
1830 // Delete the old constant!
1831 destroyConstant();
1832}
1833
Reid Spencerd84d35b2007-02-15 02:26:10 +00001834void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001835 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001836 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1837
1838 std::vector<Constant*> Values;
1839 Values.reserve(getNumOperands()); // Build replacement array...
1840 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1841 Constant *Val = getOperand(i);
1842 if (Val == From) Val = cast<Constant>(To);
1843 Values.push_back(Val);
1844 }
1845
Owen Anderson0348a132009-07-24 00:36:24 +00001846 Constant *Replacement =
1847 getType()->getContext().getConstantVector(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001848 assert(Replacement != this && "I didn't contain From!");
1849
Chris Lattner7a1450d2005-10-04 18:13:04 +00001850 // Everyone using this now uses the replacement.
1851 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001852
1853 // Delete the old constant!
1854 destroyConstant();
1855}
1856
1857void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001858 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001859 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1860 Constant *To = cast<Constant>(ToV);
1861
1862 Constant *Replacement = 0;
1863 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00001864 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001865 Constant *Pointer = getOperand(0);
1866 Indices.reserve(getNumOperands()-1);
1867 if (Pointer == From) Pointer = To;
1868
1869 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1870 Constant *Val = getOperand(i);
1871 if (Val == From) Val = To;
1872 Indices.push_back(Val);
1873 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00001874 Replacement = ConstantExpr::getGetElementPtr(Pointer,
1875 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00001876 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00001877 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00001878 if (Agg == From) Agg = To;
1879
Dan Gohman1ecaf452008-05-31 00:58:22 +00001880 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00001881 Replacement = ConstantExpr::getExtractValue(Agg,
1882 &Indices[0], Indices.size());
1883 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00001884 Constant *Agg = getOperand(0);
1885 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00001886 if (Agg == From) Agg = To;
1887 if (Val == From) Val = To;
1888
Dan Gohman1ecaf452008-05-31 00:58:22 +00001889 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00001890 Replacement = ConstantExpr::getInsertValue(Agg, Val,
1891 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001892 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001893 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001894 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001895 } else if (getOpcode() == Instruction::Select) {
1896 Constant *C1 = getOperand(0);
1897 Constant *C2 = getOperand(1);
1898 Constant *C3 = getOperand(2);
1899 if (C1 == From) C1 = To;
1900 if (C2 == From) C2 = To;
1901 if (C3 == From) C3 = To;
1902 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001903 } else if (getOpcode() == Instruction::ExtractElement) {
1904 Constant *C1 = getOperand(0);
1905 Constant *C2 = getOperand(1);
1906 if (C1 == From) C1 = To;
1907 if (C2 == From) C2 = To;
1908 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001909 } else if (getOpcode() == Instruction::InsertElement) {
1910 Constant *C1 = getOperand(0);
1911 Constant *C2 = getOperand(1);
1912 Constant *C3 = getOperand(1);
1913 if (C1 == From) C1 = To;
1914 if (C2 == From) C2 = To;
1915 if (C3 == From) C3 = To;
1916 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1917 } else if (getOpcode() == Instruction::ShuffleVector) {
1918 Constant *C1 = getOperand(0);
1919 Constant *C2 = getOperand(1);
1920 Constant *C3 = getOperand(2);
1921 if (C1 == From) C1 = To;
1922 if (C2 == From) C2 = To;
1923 if (C3 == From) C3 = To;
1924 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00001925 } else if (isCompare()) {
1926 Constant *C1 = getOperand(0);
1927 Constant *C2 = getOperand(1);
1928 if (C1 == From) C1 = To;
1929 if (C2 == From) C2 = To;
1930 if (getOpcode() == Instruction::ICmp)
1931 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00001932 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001933 assert(getOpcode() == Instruction::FCmp);
1934 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00001935 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001936 } else if (getNumOperands() == 2) {
1937 Constant *C1 = getOperand(0);
1938 Constant *C2 = getOperand(1);
1939 if (C1 == From) C1 = To;
1940 if (C2 == From) C2 = To;
1941 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1942 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001943 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001944 return;
1945 }
1946
1947 assert(Replacement != this && "I didn't contain From!");
1948
Chris Lattner7a1450d2005-10-04 18:13:04 +00001949 // Everyone using this now uses the replacement.
1950 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001951
1952 // Delete the old constant!
1953 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00001954}
Nick Lewycky49f89192009-04-04 07:22:01 +00001955
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001956void MDNode::replaceElement(Value *From, Value *To) {
1957 SmallVector<Value*, 4> Values;
1958 Values.reserve(getNumElements()); // Build replacement array...
1959 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1960 Value *Val = getElement(i);
1961 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00001962 Values.push_back(Val);
1963 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001964
Owen Anderson4118dde2009-07-16 23:44:30 +00001965 MDNode *Replacement =
1966 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00001967 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001968
Nick Lewycky49f89192009-04-04 07:22:01 +00001969 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewycky49f89192009-04-04 07:22:01 +00001970}