10SN/A//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===// 216428Sdarcy// 30SN/A// The LLVM Compiler Infrastructure 40SN/A// 50SN/A// This file is distributed under the University of Illinois Open Source 60SN/A// License. See LICENSE.TXT for details. 72362SN/A// 80SN/A//===----------------------------------------------------------------------===// 92362SN/A// 100SN/A/// @file 110SN/A/// This file contains the declarations for the subclasses of Constant, 120SN/A/// which represent the different flavors of constant values that live in LLVM. 130SN/A/// Note that Constants are immutable (once created they never change) and are 140SN/A/// fully shared by structural equivalence. This means that two structurally 150SN/A/// equivalent constants will always have the same address. Constant's are 160SN/A/// created on demand as needed and never deleted: thus clients don't have to 170SN/A/// worry about the lifetime of the objects. 180SN/A// 190SN/A//===----------------------------------------------------------------------===// 200SN/A 212362SN/A#ifndef LLVM_IR_CONSTANTS_H 222362SN/A#define LLVM_IR_CONSTANTS_H 232362SN/A 240SN/A#include "llvm/ADT/APFloat.h" 250SN/A#include "llvm/ADT/APInt.h" 260SN/A#include "llvm/ADT/ArrayRef.h" 270SN/A#include "llvm/IR/Constant.h" 2813901Salanb#include "llvm/IR/OperandTraits.h" 2913901Salanb#include "llvm/IR/DerivedTypes.h" 300SN/A 3113901Salanbnamespace llvm { 320SN/A 330SN/Aclass ArrayType; 3416177Salanbclass IntegerType; 3516177Salanbclass StructType; 3616177Salanbclass PointerType; 3716177Salanbclass VectorType; 3816177Salanbclass SequentialType; 3916177Salanb 4016177Salanbtemplate<class ConstantClass, class TypeClass, class ValType> 4116177Salanbstruct ConstantCreator; 4216177Salanbtemplate<class ConstantClass, class TypeClass> 4316177Salanbstruct ConstantArrayCreator; 4416177Salanbtemplate<class ConstantClass, class TypeClass> 4516177Salanbstruct ConvertConstantType; 4616177Salanb 4716177Salanb//===----------------------------------------------------------------------===// 4816177Salanb/// This is the shared class of boolean and integer constants. This class 4913901Salanb/// represents both boolean and integral constants. 500SN/A/// @brief Class for constant integers. 510SN/Aclass ConstantInt : public Constant { 520SN/A virtual void anchor(); 5317234Salanb void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 540SN/A ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION; 5516177Salanb ConstantInt(IntegerType *Ty, const APInt& V); 560SN/A APInt Val; 578166SN/Aprotected: 580SN/A // allocate space for exactly zero operands 5916177Salanb void *operator new(size_t s) { 6016177Salanb return User::operator new(s, 0); 610SN/A } 6210554Sigerasimpublic: 6316177Salanb static ConstantInt *getTrue(LLVMContext &Context); 6413901Salanb static ConstantInt *getFalse(LLVMContext &Context); 6513901Salanb static Constant *getTrue(Type *Ty); 6613901Salanb static Constant *getFalse(Type *Ty); 6713050Schegar 6813418Schegar /// If Ty is a vector type, return a Constant with a splat of the given 6916909Salanb /// value. Otherwise return a ConstantInt for the given value. 7014176Schegar static Constant *get(Type *Ty, uint64_t V, bool isSigned = false); 7114176Schegar 7214176Schegar /// Return a ConstantInt with the specified integer value for the specified 7314176Schegar /// type. If the type is wider than 64 bits, the value will be zero-extended 7415282Sredestad /// to fit the type, unless isSigned is true, in which case the value will 750SN/A /// be interpreted as a 64-bit signed integer and sign-extended to fit 760SN/A /// the type. 770SN/A /// @brief Get a ConstantInt for a specific value. 780SN/A static ConstantInt *get(IntegerType *Ty, uint64_t V, 790SN/A bool isSigned = false); 800SN/A 810SN/A /// Return a ConstantInt with the specified value for the specified type. The 820SN/A /// value V will be canonicalized to a an unsigned APInt. Accessing it with 836725SN/A /// either getSExtValue() or getZExtValue() will yield a correctly sized and 840SN/A /// signed value for the type Ty. 850SN/A /// @brief Get a ConstantInt for a specific signed value. 860SN/A static ConstantInt *getSigned(IntegerType *Ty, int64_t V); 870SN/A static Constant *getSigned(Type *Ty, int64_t V); 880SN/A 890SN/A /// Return a ConstantInt with the specified value and an implied Type. The 900SN/A /// type is the integer type that corresponds to the bit width of the value. 910SN/A static ConstantInt *get(LLVMContext &Context, const APInt &V); 920SN/A 930SN/A /// Return a ConstantInt constructed from the string strStart with the given 940SN/A /// radix. 950SN/A static ConstantInt *get(IntegerType *Ty, StringRef Str, 960SN/A uint8_t radix); 970SN/A 980SN/A /// If Ty is a vector type, return a Constant with a splat of the given 990SN/A /// value. Otherwise return a ConstantInt for the given value. 1000SN/A static Constant *get(Type* Ty, const APInt& V); 1010SN/A 1020SN/A /// Return the constant as an APInt value reference. This allows clients to 1030SN/A /// obtain a copy of the value, with all its precision in tact. 1040SN/A /// @brief Return the constant's value. 1058621SN/A inline const APInt &getValue() const { 1060SN/A return Val; 1070SN/A } 1080SN/A 1090SN/A /// getBitWidth - Return the bitwidth of this constant. 1100SN/A unsigned getBitWidth() const { return Val.getBitWidth(); } 1110SN/A 1120SN/A /// Return the constant as a 64-bit unsigned integer value after it 1134008SN/A /// has been zero extended as appropriate for the type of this constant. Note 1144008SN/A /// that this method can assert if the value does not fit in 64 bits. 1150SN/A /// @brief Return the zero extended value. 1160SN/A inline uint64_t getZExtValue() const { 1178621SN/A return Val.getZExtValue(); 1180SN/A } 1190SN/A 1200SN/A /// Return the constant as a 64-bit integer value after it has been sign 1210SN/A /// extended as appropriate for the type of this constant. Note that 1220SN/A /// this method can assert if the value does not fit in 64 bits. 1230SN/A /// @brief Return the sign extended value. 1240SN/A inline int64_t getSExtValue() const { 1250SN/A return Val.getSExtValue(); 1260SN/A } 1270SN/A 12810071SN/A /// A helper method that can be used to determine if the constant contained 1290SN/A /// within is equal to a constant. This only works for very small values, 1306869SN/A /// because this is all that can be represented with all types. 1317215SN/A /// @brief Determine if this constant's value is same as an unsigned char. 1327215SN/A bool equalsInt(uint64_t V) const { 1337215SN/A return Val == V; 1340SN/A } 1350SN/A 1360SN/A /// getType - Specialize the getType() method to always return an IntegerType, 1370SN/A /// which reduces the amount of casting needed in parts of the compiler. 1380SN/A /// 1390SN/A inline IntegerType *getType() const { 1400SN/A return cast<IntegerType>(Value::getType()); 1410SN/A } 1420SN/A 1430SN/A /// This static method returns true if the type Ty is big enough to 14410260SN/A /// represent the value V. This can be used to avoid having the get method 14510260SN/A /// assert when V is larger than Ty can represent. Note that there are two 14610260SN/A /// versions of this method, one for unsigned and one for signed integers. 1470SN/A /// Although ConstantInt canonicalizes everything to an unsigned integer, 14810289SN/A /// the signed version avoids callers having to convert a signed quantity 14910260SN/A /// to the appropriate unsigned type before calling the method. 15010260SN/A /// @returns true if V is a valid value for type Ty 15110260SN/A /// @brief Determine if the value is in range for the given type. 15210289SN/A static bool isValueValidForType(Type *Ty, uint64_t V); 15310260SN/A static bool isValueValidForType(Type *Ty, int64_t V); 1540SN/A 1550SN/A bool isNegative() const { return Val.isNegative(); } 1560SN/A 1570SN/A /// This is just a convenience method to make client code smaller for a 1580SN/A /// common code. It also correctly performs the comparison without the 1590SN/A /// potential for an assertion from getZExtValue(). 1600SN/A bool isZero() const { 1610SN/A return Val == 0; 16211253Sdarcy } 16311253Sdarcy 1640SN/A /// This is just a convenience method to make client code smaller for a 1650SN/A /// common case. It also correctly performs the comparison without the 1660SN/A /// potential for an assertion from getZExtValue(). 1670SN/A /// @brief Determine if the value is one. 1680SN/A bool isOne() const { 1690SN/A return Val == 1; 1700SN/A } 1710SN/A 1726869SN/A /// This function will return true iff every bit in this constant is set 1736869SN/A /// to true. 1746869SN/A /// @returns true iff this constant's bits are all set to true. 1756869SN/A /// @brief Determine if the value is all ones. 1766869SN/A bool isMinusOne() const { 1776869SN/A return Val.isAllOnesValue(); 1787717SN/A } 1797717SN/A 1807717SN/A /// This function will return true iff this constant represents the largest 1817717SN/A /// value that may be represented by the constant's type. 1826869SN/A /// @returns true iff this is the largest value that may be represented 1836869SN/A /// by this type. 1846869SN/A /// @brief Determine if the value is maximal. 1856869SN/A bool isMaxValue(bool isSigned) const { 1866869SN/A if (isSigned) 1876869SN/A return Val.isMaxSignedValue(); 18811253Sdarcy else 18911253Sdarcy return Val.isMaxValue(); 19011253Sdarcy } 19111253Sdarcy 19211253Sdarcy /// This function will return true iff this constant represents the smallest 19311253Sdarcy /// value that may be represented by this constant's type. 1946869SN/A /// @returns true if this is the smallest value that may be represented by 1956869SN/A /// this type. 1966869SN/A /// @brief Determine if the value is minimal. 1976869SN/A bool isMinValue(bool isSigned) const { 1986869SN/A if (isSigned) 1996869SN/A return Val.isMinSignedValue(); 2006869SN/A else 2016869SN/A return Val.isMinValue(); 2026869SN/A } 2036869SN/A 2046869SN/A /// This function will return true iff this constant represents a value with 2056869SN/A /// active bits bigger than 64 bits or a value greater than the given uint64_t 2066869SN/A /// value. 2076869SN/A /// @returns true iff this constant is greater or equal to the given number. 2086869SN/A /// @brief Determine if the value is greater or equal to the given number. 20911253Sdarcy bool uge(uint64_t Num) const { 21011253Sdarcy return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num; 21111253Sdarcy } 21211253Sdarcy 21311253Sdarcy /// getLimitedValue - If the value is smaller than the specified limit, 21411253Sdarcy /// return it, otherwise return the limit value. This causes the value 21511253Sdarcy /// to saturate to the limit. 21611253Sdarcy /// @returns the min of the value of the constant and the specified value 21711253Sdarcy /// @brief Get the constant's value with a saturation limit 21811253Sdarcy uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const { 21911253Sdarcy return Val.getLimitedValue(Limit); 22011253Sdarcy } 22111253Sdarcy 22211253Sdarcy /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. 22311253Sdarcy static bool classof(const Value *V) { 22411253Sdarcy return V->getValueID() == ConstantIntVal; 22511253Sdarcy } 22611253Sdarcy}; 22711253Sdarcy 22811253Sdarcy 22911253Sdarcy//===----------------------------------------------------------------------===// 23011253Sdarcy/// ConstantFP - Floating Point Values [float, double] 23111253Sdarcy/// 23211253Sdarcyclass ConstantFP : public Constant { 23311253Sdarcy APFloat Val; 23411253Sdarcy virtual void anchor(); 23511253Sdarcy void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 23611253Sdarcy ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION; 2376869SN/A friend class LLVMContextImpl; 23811253Sdarcyprotected: 2396869SN/A ConstantFP(Type *Ty, const APFloat& V); 24011253Sdarcyprotected: 24111253Sdarcy // allocate space for exactly zero operands 2426869SN/A void *operator new(size_t s) { 24315094Sdarcy return User::operator new(s, 0); 2446869SN/A } 24515094Sdarcypublic: 2466869SN/A /// Floating point negation must be implemented with f(x) = -0.0 - x. This 24715094Sdarcy /// method returns the negative zero constant for floating point or vector 2486869SN/A /// floating point types; for all other types, it returns the null value. 2496869SN/A static Constant *getZeroValueForNegation(Type *Ty); 25011253Sdarcy 25111253Sdarcy /// get() - This returns a ConstantFP, or a vector containing a splat of a 25211253Sdarcy /// ConstantFP, for the specified value in the specified type. This should 2536869SN/A /// only be used for simple constant values like 2.0/1.0 etc, that are 2546869SN/A /// known-valid both as host double and as the target format. 2556869SN/A static Constant *get(Type* Ty, double V); 2560SN/A static Constant *get(Type* Ty, StringRef Str); 2570SN/A static ConstantFP *get(LLVMContext &Context, const APFloat &V); 2580SN/A static ConstantFP *getNegativeZero(Type* Ty); 2590SN/A static ConstantFP *getInfinity(Type *Ty, bool Negative = false); 2600SN/A 2610SN/A /// isValueValidForType - return true if Ty is big enough to represent V. 2620SN/A static bool isValueValidForType(Type *Ty, const APFloat &V); 2630SN/A inline const APFloat &getValueAPF() const { return Val; } 2640SN/A 2650SN/A /// isZero - Return true if the value is positive or negative zero. 2660SN/A bool isZero() const { return Val.isZero(); } 2670SN/A 2680SN/A /// isNegative - Return true if the sign bit is set. 2690SN/A bool isNegative() const { return Val.isNegative(); } 2700SN/A 2710SN/A /// isNaN - Return true if the value is a NaN. 2720SN/A bool isNaN() const { return Val.isNaN(); } 2730SN/A 2740SN/A /// isExactlyValue - We don't rely on operator== working on double values, as 2750SN/A /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 2760SN/A /// As such, this method can be used to do an exact bit-for-bit comparison of 2770SN/A /// two floating point values. The version with a double operand is retained 2780SN/A /// because it's so convenient to write isExactlyValue(2.0), but please use 2790SN/A /// it only for simple constants. 2800SN/A bool isExactlyValue(const APFloat &V) const; 2810SN/A 2820SN/A bool isExactlyValue(double V) const { 2830SN/A bool ignored; 2840SN/A APFloat FV(V); 2850SN/A FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored); 2860SN/A return isExactlyValue(FV); 2870SN/A } 2886951SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 2890SN/A static bool classof(const Value *V) { 2900SN/A return V->getValueID() == ConstantFPVal; 29110826Scoleenp } 29210826Scoleenp}; 2930SN/A 2940SN/A//===----------------------------------------------------------------------===// 2950SN/A/// ConstantAggregateZero - All zero aggregate value 2960SN/A/// 2970SN/Aclass ConstantAggregateZero : public Constant { 2980SN/A void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 2990SN/A ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION; 3000SN/Aprotected: 3010SN/A explicit ConstantAggregateZero(Type *ty) 3020SN/A : Constant(ty, ConstantAggregateZeroVal, 0, 0) {} 3030SN/Aprotected: 3040SN/A // allocate space for exactly zero operands 3050SN/A void *operator new(size_t s) { 3060SN/A return User::operator new(s, 0); 3070SN/A } 3080SN/Apublic: 3090SN/A static ConstantAggregateZero *get(Type *Ty); 3100SN/A 3110SN/A virtual void destroyConstant(); 3120SN/A 3130SN/A /// getSequentialElement - If this CAZ has array or vector type, return a zero 3140SN/A /// with the right element type. 3150SN/A Constant *getSequentialElement() const; 3160SN/A 3170SN/A /// getStructElement - If this CAZ has struct type, return a zero with the 3180SN/A /// right element type for the specified element. 3190SN/A Constant *getStructElement(unsigned Elt) const; 3200SN/A 3210SN/A /// getElementValue - Return a zero of the right value for the specified GEP 3220SN/A /// index. 3230SN/A Constant *getElementValue(Constant *C) const; 3240SN/A 3250SN/A /// getElementValue - Return a zero of the right value for the specified GEP 3260SN/A /// index. 3270SN/A Constant *getElementValue(unsigned Idx) const; 3280SN/A 3290SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 3300SN/A /// 3310SN/A static bool classof(const Value *V) { 3320SN/A return V->getValueID() == ConstantAggregateZeroVal; 3330SN/A } 3340SN/A}; 3350SN/A 3365753SN/A 3375753SN/A//===----------------------------------------------------------------------===// 3380SN/A/// ConstantArray - Constant Array Declarations 3390SN/A/// 3400SN/Aclass ConstantArray : public Constant { 3410SN/A friend struct ConstantArrayCreator<ConstantArray, ArrayType>; 3420SN/A ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION; 3430SN/Aprotected: 3440SN/A ConstantArray(ArrayType *T, ArrayRef<Constant *> Val); 3450SN/Apublic: 34615446Sbchristi // ConstantArray accessors 34715446Sbchristi static Constant *get(ArrayType *T, ArrayRef<Constant*> V); 34815446Sbchristi 34915446Sbchristi /// Transparently provide more efficient getOperand methods. 35015446Sbchristi DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 3510SN/A 3520SN/A /// getType - Specialize the getType() method to always return an ArrayType, 3530SN/A /// which reduces the amount of casting needed in parts of the compiler. 3540SN/A /// 3550SN/A inline ArrayType *getType() const { 3566951SN/A return cast<ArrayType>(Value::getType()); 3570SN/A } 3580SN/A 3590SN/A virtual void destroyConstant(); 3600SN/A virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 36110826Scoleenp 36210826Scoleenp /// Methods for support type inquiry through isa, cast, and dyn_cast: 36310826Scoleenp static bool classof(const Value *V) { 36410826Scoleenp return V->getValueID() == ConstantArrayVal; 36510826Scoleenp } 36610826Scoleenp}; 36713418Schegar 36810826Scoleenptemplate <> 36913418Schegarstruct OperandTraits<ConstantArray> : 3700SN/A public VariadicOperandTraits<ConstantArray> { 3710SN/A}; 3720SN/A 3730SN/ADEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant) 3740SN/A 37510826Scoleenp//===----------------------------------------------------------------------===// 3760SN/A// ConstantStruct - Constant Struct Declarations 3770SN/A// 37810826Scoleenpclass ConstantStruct : public Constant { 3791717SN/A friend struct ConstantArrayCreator<ConstantStruct, StructType>; 38010826Scoleenp ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION; 38110826Scoleenpprotected: 3820SN/A ConstantStruct(StructType *T, ArrayRef<Constant *> Val); 3830SN/Apublic: 38413901Salanb // ConstantStruct accessors 38513901Salanb static Constant *get(StructType *T, ArrayRef<Constant*> V); 38613901Salanb static Constant *get(StructType *T, ...) END_WITH_NULL; 38713901Salanb 38813901Salanb /// getAnon - Return an anonymous struct that has the specified 38913901Salanb /// elements. If the struct is possibly empty, then you must specify a 39013901Salanb /// context. 39113901Salanb static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) { 39213901Salanb return get(getTypeForElements(V, Packed), V); 39313901Salanb } 39413901Salanb static Constant *getAnon(LLVMContext &Ctx, 39513901Salanb ArrayRef<Constant*> V, bool Packed = false) { 39613901Salanb return get(getTypeForElements(Ctx, V, Packed), V); 39713901Salanb } 39813901Salanb 39913901Salanb /// getTypeForElements - Return an anonymous struct type to use for a constant 40013901Salanb /// with the specified set of elements. The list must not be empty. 40113901Salanb static StructType *getTypeForElements(ArrayRef<Constant*> V, 40213901Salanb bool Packed = false); 40313901Salanb /// getTypeForElements - This version of the method allows an empty list. 40413901Salanb static StructType *getTypeForElements(LLVMContext &Ctx, 40513901Salanb ArrayRef<Constant*> V, 40613901Salanb bool Packed = false); 40713901Salanb 40813901Salanb /// Transparently provide more efficient getOperand methods. 40913901Salanb DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 41013901Salanb 41113901Salanb /// getType() specialization - Reduce amount of casting... 41213901Salanb /// 41313901Salanb inline StructType *getType() const { 41413901Salanb return cast<StructType>(Value::getType()); 41513901Salanb } 41613901Salanb 41713901Salanb virtual void destroyConstant(); 41813901Salanb virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 41913901Salanb 42013901Salanb /// Methods for support type inquiry through isa, cast, and dyn_cast: 42113901Salanb static bool classof(const Value *V) { 42213901Salanb return V->getValueID() == ConstantStructVal; 42313901Salanb } 42413901Salanb}; 42513901Salanb 42613901Salanbtemplate <> 42713901Salanbstruct OperandTraits<ConstantStruct> : 42816672Salanb public VariadicOperandTraits<ConstantStruct> { 42913901Salanb}; 43013901Salanb 43113901SalanbDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant) 43213901Salanb 43313901Salanb 43413901Salanb//===----------------------------------------------------------------------===// 43513901Salanb/// ConstantVector - Constant Vector Declarations 43613901Salanb/// 43713901Salanbclass ConstantVector : public Constant { 43813901Salanb friend struct ConstantArrayCreator<ConstantVector, VectorType>; 43913901Salanb ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION; 44013901Salanbprotected: 44113901Salanb ConstantVector(VectorType *T, ArrayRef<Constant *> Val); 44213901Salanbpublic: 44313901Salanb // ConstantVector accessors 44413901Salanb static Constant *get(ArrayRef<Constant*> V); 44513901Salanb 44613901Salanb /// getSplat - Return a ConstantVector with the specified constant in each 44713901Salanb /// element. 44816177Salanb static Constant *getSplat(unsigned NumElts, Constant *Elt); 44913901Salanb 45016177Salanb /// Transparently provide more efficient getOperand methods. 45113901Salanb DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 45213901Salanb 45313901Salanb /// getType - Specialize the getType() method to always return a VectorType, 4540SN/A /// which reduces the amount of casting needed in parts of the compiler. 4550SN/A /// 4560SN/A inline VectorType *getType() const { 4570SN/A return cast<VectorType>(Value::getType()); 4580SN/A } 4590SN/A 46014360Sdarcy /// getSplatValue - If this is a splat constant, meaning that all of the 4610SN/A /// elements have the same value, return that value. Otherwise return NULL. 4620SN/A Constant *getSplatValue() const; 4630SN/A 4640SN/A virtual void destroyConstant(); 4650SN/A virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 4660SN/A 4670SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 4680SN/A static bool classof(const Value *V) { 4690SN/A return V->getValueID() == ConstantVectorVal; 47014746Sdarcy } 47114746Sdarcy}; 47214746Sdarcy 47314746Sdarcytemplate <> 47414746Sdarcystruct OperandTraits<ConstantVector> : 47514746Sdarcy public VariadicOperandTraits<ConstantVector> { 47614746Sdarcy}; 47714746Sdarcy 47814746SdarcyDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant) 47915995Sdarcy 48014746Sdarcy//===----------------------------------------------------------------------===// 48114746Sdarcy/// ConstantPointerNull - a constant pointer value that points to null 48214746Sdarcy/// 48314746Sdarcyclass ConstantPointerNull : public Constant { 48414746Sdarcy void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 48514746Sdarcy ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION; 48614746Sdarcyprotected: 48714746Sdarcy explicit ConstantPointerNull(PointerType *T) 4887622SN/A : Constant(T, 4897622SN/A Value::ConstantPointerNullVal, 0, 0) {} 4907622SN/A 4917622SN/Aprotected: 4927622SN/A // allocate space for exactly zero operands 4937622SN/A void *operator new(size_t s) { 4947622SN/A return User::operator new(s, 0); 4957622SN/A } 4967622SN/Apublic: 4977622SN/A /// get() - Static factory methods - Return objects of the specified value 4987622SN/A static ConstantPointerNull *get(PointerType *T); 4997622SN/A 5007622SN/A virtual void destroyConstant(); 5017622SN/A 5027622SN/A /// getType - Specialize the getType() method to always return an PointerType, 5037622SN/A /// which reduces the amount of casting needed in parts of the compiler. 5047622SN/A /// 5057622SN/A inline PointerType *getType() const { 5060SN/A return cast<PointerType>(Value::getType()); 5076951SN/A } 50814360Sdarcy 5090SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 5100SN/A static bool classof(const Value *V) { 5110SN/A return V->getValueID() == ConstantPointerNullVal; 51216480Sredestad } 51316480Sredestad}; 51416480Sredestad 5150SN/A//===----------------------------------------------------------------------===// 5160SN/A/// ConstantDataSequential - A vector or array constant whose element type is a 5170SN/A/// simple 1/2/4/8-byte integer or float/double, and whose elements are just 5180SN/A/// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no 5190SN/A/// operands because it stores all of the elements of the constant as densely 5200SN/A/// packed data, instead of as Value*'s. 5210SN/A/// 5220SN/A/// This is the common base class of ConstantDataArray and ConstantDataVector. 5230SN/A/// 5240SN/Aclass ConstantDataSequential : public Constant { 5250SN/A friend class LLVMContextImpl; 5260SN/A /// DataElements - A pointer to the bytes underlying this constant (which is 5270SN/A /// owned by the uniquing StringMap). 5281717SN/A const char *DataElements; 52916411Splevart 53016411Splevart /// Next - This forms a link list of ConstantDataSequential nodes that have 5310SN/A /// the same value but different type. For example, 0,0,0,1 could be a 4 5320SN/A /// element array of i8, or a 1-element array of i32. They'll both end up in 5330SN/A /// the same StringMap bucket, linked up. 5340SN/A ConstantDataSequential *Next; 53528SN/A void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 53611805Sdarcy ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION; 53728SN/Aprotected: 5380SN/A explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data) 5390SN/A : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {} 5400SN/A ~ConstantDataSequential() { delete Next; } 5410SN/A 5420SN/A static Constant *getImpl(StringRef Bytes, Type *Ty); 5430SN/A 5444449SN/Aprotected: 5454449SN/A // allocate space for exactly zero operands. 5460SN/A void *operator new(size_t s) { 5470SN/A return User::operator new(s, 0); 5480SN/A } 5490SN/Apublic: 55013901Salanb 55113901Salanb /// isElementTypeCompatible - Return true if a ConstantDataSequential can be 55213901Salanb /// formed with a vector or array of the specified element type. 55315845Splevart /// ConstantDataArray only works with normal float and int types that are 55413901Salanb /// stored densely in memory, not with things like i42 or x86_f80. 5550SN/A static bool isElementTypeCompatible(const Type *Ty); 5560SN/A 5570SN/A /// getElementAsInteger - If this is a sequential container of integers (of 5580SN/A /// any size), return the specified element in the low bits of a uint64_t. 5590SN/A uint64_t getElementAsInteger(unsigned i) const; 5600SN/A 5610SN/A /// getElementAsAPFloat - If this is a sequential container of floating point 5620SN/A /// type, return the specified element as an APFloat. 5630SN/A APFloat getElementAsAPFloat(unsigned i) const; 5640SN/A 56512745Smartin /// getElementAsFloat - If this is an sequential container of floats, return 56612745Smartin /// the specified element as a float. 5670SN/A float getElementAsFloat(unsigned i) const; 5680SN/A 5690SN/A /// getElementAsDouble - If this is an sequential container of doubles, return 5700SN/A /// the specified element as a double. 5710SN/A double getElementAsDouble(unsigned i) const; 5720SN/A 5730SN/A /// getElementAsConstant - Return a Constant for a specified index's element. 5740SN/A /// Note that this has to compute a new constant to return, so it isn't as 5750SN/A /// efficient as getElementAsInteger/Float/Double. 5760SN/A Constant *getElementAsConstant(unsigned i) const; 5770SN/A 5780SN/A /// getType - Specialize the getType() method to always return a 5790SN/A /// SequentialType, which reduces the amount of casting needed in parts of the 5800SN/A /// compiler. 5810SN/A inline SequentialType *getType() const { 5820SN/A return cast<SequentialType>(Value::getType()); 5830SN/A } 5840SN/A 5850SN/A /// getElementType - Return the element type of the array/vector. 5860SN/A Type *getElementType() const; 5870SN/A 5880SN/A /// getNumElements - Return the number of elements in the array or vector. 5890SN/A unsigned getNumElements() const; 5900SN/A 5910SN/A /// getElementByteSize - Return the size (in bytes) of each element in the 5920SN/A /// array/vector. The size of the elements is known to be a multiple of one 5930SN/A /// byte. 5940SN/A uint64_t getElementByteSize() const; 5950SN/A 5960SN/A 59710071SN/A /// isString - This method returns true if this is an array of i8. 5980SN/A bool isString() const; 59912352Szmajo 6000SN/A /// isCString - This method returns true if the array "isString", ends with a 6010SN/A /// nul byte, and does not contains any other nul bytes. 6020SN/A bool isCString() const; 6030SN/A 6040SN/A /// getAsString - If this array is isString(), then this method returns the 6050SN/A /// array as a StringRef. Otherwise, it asserts out. 6060SN/A /// 6070SN/A StringRef getAsString() const { 6080SN/A assert(isString() && "Not a string"); 6090SN/A return getRawDataValues(); 6100SN/A } 6110SN/A 6120SN/A /// getAsCString - If this array is isCString(), then this method returns the 6130SN/A /// array (without the trailing null byte) as a StringRef. Otherwise, it 6140SN/A /// asserts out. 6150SN/A /// 6160SN/A StringRef getAsCString() const { 6170SN/A assert(isCString() && "Isn't a C string"); 6180SN/A StringRef Str = getAsString(); 6190SN/A return Str.substr(0, Str.size()-1); 6200SN/A } 6210SN/A 6220SN/A /// getRawDataValues - Return the raw, underlying, bytes of this data. Note 6230SN/A /// that this is an extremely tricky thing to work with, as it exposes the 6240SN/A /// host endianness of the data elements. 62510071SN/A StringRef getRawDataValues() const; 6260SN/A 62712352Szmajo virtual void destroyConstant(); 6280SN/A 6290SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 6300SN/A /// 6310SN/A static bool classof(const Value *V) { 6320SN/A return V->getValueID() == ConstantDataArrayVal || 6330SN/A V->getValueID() == ConstantDataVectorVal; 6340SN/A } 6350SN/Aprivate: 6360SN/A const char *getElementPointer(unsigned Elt) const; 6370SN/A}; 63812352Szmajo 6390SN/A//===----------------------------------------------------------------------===// 6400SN/A/// ConstantDataArray - An array constant whose element type is a simple 6410SN/A/// 1/2/4/8-byte integer or float/double, and whose elements are just simple 6420SN/A/// data values (i.e. ConstantInt/ConstantFP). This Constant node has no 6430SN/A/// operands because it stores all of the elements of the constant as densely 6440SN/A/// packed data, instead of as Value*'s. 6450SN/Aclass ConstantDataArray : public ConstantDataSequential { 6460SN/A void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 64710071SN/A ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION; 6480SN/A virtual void anchor(); 64912352Szmajo friend class ConstantDataSequential; 6500SN/A explicit ConstantDataArray(Type *ty, const char *Data) 6510SN/A : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {} 6520SN/Aprotected: 6530SN/A // allocate space for exactly zero operands. 6540SN/A void *operator new(size_t s) { 6550SN/A return User::operator new(s, 0); 6560SN/A } 6570SN/Apublic: 6580SN/A 6590SN/A /// get() constructors - Return a constant with array type with an element 6600SN/A /// count and element type matching the ArrayRef passed in. Note that this 6610SN/A /// can return a ConstantAggregateZero object. 6620SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts); 6630SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts); 6640SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts); 6650SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts); 6660SN/A static Constant *get(LLVMContext &Context, ArrayRef<float> Elts); 6670SN/A static Constant *get(LLVMContext &Context, ArrayRef<double> Elts); 6680SN/A 6690SN/A /// getString - This method constructs a CDS and initializes it with a text 6700SN/A /// string. The default behavior (AddNull==true) causes a null terminator to 6710SN/A /// be placed at the end of the array (increasing the length of the string by 6720SN/A /// one more than the StringRef would normally indicate. Pass AddNull=false 6730SN/A /// to disable this behavior. 6740SN/A static Constant *getString(LLVMContext &Context, StringRef Initializer, 6750SN/A bool AddNull = true); 6760SN/A 6770SN/A /// getType - Specialize the getType() method to always return an ArrayType, 6780SN/A /// which reduces the amount of casting needed in parts of the compiler. 67910071SN/A /// 6800SN/A inline ArrayType *getType() const { 68112352Szmajo return cast<ArrayType>(Value::getType()); 6820SN/A } 6830SN/A 6840SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 6850SN/A /// 6860SN/A static bool classof(const Value *V) { 6870SN/A return V->getValueID() == ConstantDataArrayVal; 6880SN/A } 6890SN/A}; 6900SN/A 6910SN/A//===----------------------------------------------------------------------===// 6920SN/A/// ConstantDataVector - A vector constant whose element type is a simple 6930SN/A/// 1/2/4/8-byte integer or float/double, and whose elements are just simple 6940SN/A/// data values (i.e. ConstantInt/ConstantFP). This Constant node has no 6950SN/A/// operands because it stores all of the elements of the constant as densely 6960SN/A/// packed data, instead of as Value*'s. 6970SN/Aclass ConstantDataVector : public ConstantDataSequential { 6980SN/A void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 6990SN/A ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION; 7000SN/A virtual void anchor(); 7010SN/A friend class ConstantDataSequential; 7026296SN/A explicit ConstantDataVector(Type *ty, const char *Data) 7030SN/A : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {} 7040SN/Aprotected: 7050SN/A // allocate space for exactly zero operands. 7060SN/A void *operator new(size_t s) { 7070SN/A return User::operator new(s, 0); 7080SN/A } 7090SN/Apublic: 7100SN/A 7110SN/A /// get() constructors - Return a constant with vector type with an element 7120SN/A /// count and element type matching the ArrayRef passed in. Note that this 7130SN/A /// can return a ConstantAggregateZero object. 7140SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts); 7150SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts); 7164008SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts); 7174008SN/A static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts); 7180SN/A static Constant *get(LLVMContext &Context, ArrayRef<float> Elts); 7190SN/A static Constant *get(LLVMContext &Context, ArrayRef<double> Elts); 7200SN/A 7210SN/A /// getSplat - Return a ConstantVector with the specified constant in each 7220SN/A /// element. The specified constant has to be a of a compatible type (i8/i16/ 7230SN/A /// i32/i64/float/double) and must be a ConstantFP or ConstantInt. 7240SN/A static Constant *getSplat(unsigned NumElts, Constant *Elt); 7250SN/A 7260SN/A /// getSplatValue - If this is a splat constant, meaning that all of the 7270SN/A /// elements have the same value, return that value. Otherwise return NULL. 72817125Sjjg Constant *getSplatValue() const; 72917125Sjjg 73017125Sjjg /// getType - Specialize the getType() method to always return a VectorType, 73117125Sjjg /// which reduces the amount of casting needed in parts of the compiler. 73217125Sjjg /// 73317125Sjjg inline VectorType *getType() const { 73417125Sjjg return cast<VectorType>(Value::getType()); 73517125Sjjg } 73617125Sjjg 7370SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 73817125Sjjg /// 73917125Sjjg static bool classof(const Value *V) { 74017125Sjjg return V->getValueID() == ConstantDataVectorVal; 74117125Sjjg } 74217125Sjjg}; 74317125Sjjg 74417125Sjjg 7450SN/A 7460SN/A/// BlockAddress - The address of a basic block. 7470SN/A/// 7480SN/Aclass BlockAddress : public Constant { 7490SN/A void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 7500SN/A void *operator new(size_t s) { return User::operator new(s, 2); } 7510SN/A BlockAddress(Function *F, BasicBlock *BB); 7520SN/Apublic: 7530SN/A /// get - Return a BlockAddress for the specified function and basic block. 7540SN/A static BlockAddress *get(Function *F, BasicBlock *BB); 7550SN/A 7560SN/A /// get - Return a BlockAddress for the specified basic block. The basic 7570SN/A /// block must be embedded into a function. 7580SN/A static BlockAddress *get(BasicBlock *BB); 7590SN/A 7600SN/A /// Transparently provide more efficient getOperand methods. 7610SN/A DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 7620SN/A 7630SN/A Function *getFunction() const { return (Function*)Op<0>().get(); } 7640SN/A BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); } 7650SN/A 7661656SN/A virtual void destroyConstant(); 7670SN/A virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 7681656SN/A 7690SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 7700SN/A static inline bool classof(const Value *V) { 7710SN/A return V->getValueID() == BlockAddressVal; 7720SN/A } 7730SN/A}; 7740SN/A 7750SN/Atemplate <> 7760SN/Astruct OperandTraits<BlockAddress> : 7770SN/A public FixedNumOperandTraits<BlockAddress, 2> { 7780SN/A}; 7790SN/A 7800SN/ADEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value) 7810SN/A 7820SN/A 7830SN/A//===----------------------------------------------------------------------===// 7840SN/A/// ConstantExpr - a constant value that is initialized with an expression using 7850SN/A/// other constant values. 7860SN/A/// 78715446Sbchristi/// This class uses the standard Instruction opcodes to define the various 78815446Sbchristi/// constant expressions. The Opcode field for the ConstantExpr class is 78915446Sbchristi/// maintained in the Value::SubclassData field. 79015446Sbchristiclass ConstantExpr : public Constant { 79115446Sbchristi friend struct ConstantCreator<ConstantExpr,Type, 79215446Sbchristi std::pair<unsigned, std::vector<Constant*> > >; 7930SN/A friend struct ConvertConstantType<ConstantExpr, Type>; 7940SN/A 7950SN/Aprotected: 7960SN/A ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps) 7976951SN/A : Constant(ty, ConstantExprVal, Ops, NumOps) { 79815282Sredestad // Operation type (an Instruction opcode) is stored as the SubclassData. 7990SN/A setValueSubclassData(Opcode); 8000SN/A } 8010SN/A 8020SN/Apublic: 8030SN/A // Static methods to construct a ConstantExpr of different kinds. Note that 8040SN/A // these methods may return a object that is not an instance of the 8056951SN/A // ConstantExpr class, because they will attempt to fold the constant 8060SN/A // expression into something simpler if possible. 8070SN/A 8080SN/A /// getAlignOf constant expr - computes the alignment of a type in a target 8090SN/A /// independent way (Note: the return type is an i64). 8100SN/A static Constant *getAlignOf(Type *Ty); 81110260SN/A 81210260SN/A /// getSizeOf constant expr - computes the (alloc) size of a type (in 81313901Salanb /// address-units, not bits) in a target independent way (Note: the return 81413901Salanb /// type is an i64). 81513901Salanb /// 81613901Salanb static Constant *getSizeOf(Type *Ty); 81713901Salanb 81813901Salanb /// getOffsetOf constant expr - computes the offset of a struct field in a 81913901Salanb /// target independent way (Note: the return type is an i64). 82013901Salanb /// 82113901Salanb static Constant *getOffsetOf(StructType *STy, unsigned FieldNo); 82213901Salanb 82313901Salanb /// getOffsetOf constant expr - This is a generalized form of getOffsetOf, 82413901Salanb /// which supports any aggregate type, and any Constant index. 82513901Salanb /// 82613901Salanb static Constant *getOffsetOf(Type *Ty, Constant *FieldNo); 82713901Salanb 82816672Salanb static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false); 82913901Salanb static Constant *getFNeg(Constant *C); 83013901Salanb static Constant *getNot(Constant *C); 83113901Salanb static Constant *getAdd(Constant *C1, Constant *C2, 83213901Salanb bool HasNUW = false, bool HasNSW = false); 83313901Salanb static Constant *getFAdd(Constant *C1, Constant *C2); 83413901Salanb static Constant *getSub(Constant *C1, Constant *C2, 83513901Salanb bool HasNUW = false, bool HasNSW = false); 83613901Salanb static Constant *getFSub(Constant *C1, Constant *C2); 83710260SN/A static Constant *getMul(Constant *C1, Constant *C2, 83811037Sdfuchs bool HasNUW = false, bool HasNSW = false); 83911037Sdfuchs static Constant *getFMul(Constant *C1, Constant *C2); 84010260SN/A static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false); 8410SN/A static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false); 8420SN/A static Constant *getFDiv(Constant *C1, Constant *C2); 8430SN/A static Constant *getURem(Constant *C1, Constant *C2); 8440SN/A static Constant *getSRem(Constant *C1, Constant *C2); 8450SN/A static Constant *getFRem(Constant *C1, Constant *C2); 8460SN/A static Constant *getAnd(Constant *C1, Constant *C2); 8470SN/A static Constant *getOr(Constant *C1, Constant *C2); 8480SN/A static Constant *getXor(Constant *C1, Constant *C2); 8490SN/A static Constant *getShl(Constant *C1, Constant *C2, 8500SN/A bool HasNUW = false, bool HasNSW = false); 8511385SN/A static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false); 8520SN/A static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false); 8534008SN/A static Constant *getTrunc (Constant *C, Type *Ty); 8544008SN/A static Constant *getSExt (Constant *C, Type *Ty); 8550SN/A static Constant *getZExt (Constant *C, Type *Ty); 8560SN/A static Constant *getFPTrunc (Constant *C, Type *Ty); 8574849SN/A static Constant *getFPExtend(Constant *C, Type *Ty); 8580SN/A static Constant *getUIToFP (Constant *C, Type *Ty); 8597388SN/A static Constant *getSIToFP (Constant *C, Type *Ty); 8607388SN/A static Constant *getFPToUI (Constant *C, Type *Ty); 8617388SN/A static Constant *getFPToSI (Constant *C, Type *Ty); 8620SN/A static Constant *getPtrToInt(Constant *C, Type *Ty); 8631717SN/A static Constant *getIntToPtr(Constant *C, Type *Ty); 8640SN/A static Constant *getBitCast (Constant *C, Type *Ty); 8650SN/A static Constant *getAddrSpaceCast(Constant *C, Type *Ty); 8660SN/A 8670SN/A static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); } 86810984Smartin static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); } 86910984Smartin static Constant *getNSWAdd(Constant *C1, Constant *C2) { 87010984Smartin return getAdd(C1, C2, false, true); 8710SN/A } 8720SN/A static Constant *getNUWAdd(Constant *C1, Constant *C2) { 8730SN/A return getAdd(C1, C2, true, false); 8740SN/A } 8750SN/A static Constant *getNSWSub(Constant *C1, Constant *C2) { 87610984Smartin return getSub(C1, C2, false, true); 8770SN/A } 87812352Szmajo static Constant *getNUWSub(Constant *C1, Constant *C2) { 8790SN/A return getSub(C1, C2, true, false); 8800SN/A } 8810SN/A static Constant *getNSWMul(Constant *C1, Constant *C2) { 8820SN/A return getMul(C1, C2, false, true); 8830SN/A } 8840SN/A static Constant *getNUWMul(Constant *C1, Constant *C2) { 8850SN/A return getMul(C1, C2, true, false); 8860SN/A } 8870SN/A static Constant *getNSWShl(Constant *C1, Constant *C2) { 8880SN/A return getShl(C1, C2, false, true); 8890SN/A } 8900SN/A static Constant *getNUWShl(Constant *C1, Constant *C2) { 8910SN/A return getShl(C1, C2, true, false); 8920SN/A } 8930SN/A static Constant *getExactSDiv(Constant *C1, Constant *C2) { 8940SN/A return getSDiv(C1, C2, true); 8950SN/A } 8960SN/A static Constant *getExactUDiv(Constant *C1, Constant *C2) { 8970SN/A return getUDiv(C1, C2, true); 8980SN/A } 8990SN/A static Constant *getExactAShr(Constant *C1, Constant *C2) { 9001385SN/A return getAShr(C1, C2, true); 9014008SN/A } 9024008SN/A static Constant *getExactLShr(Constant *C1, Constant *C2) { 9030SN/A return getLShr(C1, C2, true); 9040SN/A } 9051385SN/A 9060SN/A /// getBinOpIdentity - Return the identity for the given binary operation, 9070SN/A /// i.e. a constant C such that X op C = X and C op X = X for every X. It 90810984Smartin /// returns null if the operator doesn't have an identity. 9090SN/A static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty); 9100SN/A 9110SN/A /// getBinOpAbsorber - Return the absorbing element for the given binary 9127388SN/A /// operation, i.e. a constant C such that X op C = C and C op X = C for 9137388SN/A /// every X. For example, this returns zero for integer multiplication. 9140SN/A /// It returns null if the operator doesn't have an absorbing element. 9157388SN/A static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty); 9167388SN/A 9177388SN/A /// Transparently provide more efficient getOperand methods. 9187388SN/A DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 9197388SN/A 9207388SN/A // @brief Convenience function for getting one of the casting operations 9217388SN/A // using a CastOps opcode. 9227388SN/A static Constant *getCast( 9237388SN/A unsigned ops, ///< The opcode for the conversion 9247388SN/A Constant *C, ///< The constant to be converted 9250SN/A Type *Ty ///< The type to which the constant is converted 9260SN/A ); 9270SN/A 92813901Salanb // @brief Create a ZExt or BitCast cast constant expression 9290SN/A static Constant *getZExtOrBitCast( 93013901Salanb Constant *C, ///< The constant to zext or bitcast 93113901Salanb Type *Ty ///< The type to zext or bitcast C to 9320SN/A ); 93313901Salanb 93416672Salanb // @brief Create a SExt or BitCast cast constant expression 93516672Salanb static Constant *getSExtOrBitCast( 9360SN/A Constant *C, ///< The constant to sext or bitcast 9370SN/A Type *Ty ///< The type to sext or bitcast C to 93813901Salanb ); 93913901Salanb 94013901Salanb // @brief Create a Trunc or BitCast cast constant expression 94113901Salanb static Constant *getTruncOrBitCast( 94213901Salanb Constant *C, ///< The constant to trunc or bitcast 94313901Salanb Type *Ty ///< The type to trunc or bitcast C to 9440SN/A ); 9450SN/A 94613901Salanb /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant 94713901Salanb /// expression. 94813901Salanb static Constant *getPointerCast( 94913901Salanb Constant *C, ///< The pointer value to be casted (operand 0) 95013901Salanb Type *Ty ///< The type to which cast should be made 95113901Salanb ); 95213901Salanb 95313901Salanb /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on 95413901Salanb /// the address space. 95513901Salanb static Constant *getPointerBitCastOrAddrSpaceCast( 95613901Salanb Constant *C, ///< The constant to addrspacecast or bitcast 95713901Salanb Type *Ty ///< The type to bitcast or addrspacecast C to 95813901Salanb ); 95913901Salanb 96013901Salanb /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts 96113901Salanb static Constant *getIntegerCast( 96213901Salanb Constant *C, ///< The integer constant to be casted 96316672Salanb Type *Ty, ///< The integer type to cast to 96416672Salanb bool isSigned ///< Whether C should be treated as signed or not 96516672Salanb ); 96613901Salanb 96713901Salanb /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts 96813901Salanb static Constant *getFPCast( 96913901Salanb Constant *C, ///< The integer constant to be casted 97016672Salanb Type *Ty ///< The integer type to cast to 97113901Salanb ); 97213901Salanb 97313901Salanb /// @brief Return true if this is a convert constant expression 97413901Salanb bool isCast() const; 97516672Salanb 97616672Salanb /// @brief Return true if this is a compare constant expression 97716672Salanb bool isCompare() const; 97816672Salanb 97916672Salanb /// @brief Return true if this is an insertvalue or extractvalue expression, 98016672Salanb /// and the getIndices() method may be used. 98116672Salanb bool hasIndices() const; 98216672Salanb 98316672Salanb /// @brief Return true if this is a getelementptr expression and all 98416672Salanb /// the index operands are compile-time known integers within the 98516672Salanb /// corresponding notional static array extents. Note that this is 98616672Salanb /// not equivalant to, a subset of, or a superset of the "inbounds" 98713901Salanb /// property. 98813901Salanb bool isGEPWithNoNotionalOverIndexing() const; 98913901Salanb 99013901Salanb /// Select constant expr 99113901Salanb /// 99213901Salanb static Constant *getSelect(Constant *C, Constant *V1, Constant *V2); 99316177Salanb 9940SN/A /// get - Return a binary or shift operator constant expression, 9950SN/A /// folding if possible. 99610984Smartin /// 9970SN/A static Constant *get(unsigned Opcode, Constant *C1, Constant *C2, 9980SN/A unsigned Flags = 0); 99910984Smartin 100010984Smartin /// @brief Return an ICmp or FCmp comparison operator constant expression. 100110984Smartin static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2); 100210984Smartin 100310984Smartin /// get* - Return some common constants without having to 100410984Smartin /// specify the full Instruction::OPCODE identifier. 10050SN/A /// 10060SN/A static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS); 10070SN/A static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS); 10080SN/A 10090SN/A /// Getelementptr form. Value* is only accepted for convenience; 10100SN/A /// all elements must be Constant's. 10110SN/A /// 10120SN/A static Constant *getGetElementPtr(Constant *C, 10130SN/A ArrayRef<Constant *> IdxList, 10140SN/A bool InBounds = false) { 10150SN/A return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(), 10160SN/A IdxList.size()), 10170SN/A InBounds); 10180SN/A } 10190SN/A static Constant *getGetElementPtr(Constant *C, 10200SN/A Constant *Idx, 102110984Smartin bool InBounds = false) { 102210984Smartin // This form of the function only exists to avoid ambiguous overload 102310984Smartin // warnings about whether to convert Idx to ArrayRef<Constant *> or 102410984Smartin // ArrayRef<Value *>. 102510984Smartin return getGetElementPtr(C, cast<Value>(Idx), InBounds); 10260SN/A } 102710984Smartin static Constant *getGetElementPtr(Constant *C, 10280SN/A ArrayRef<Value *> IdxList, 10290SN/A bool InBounds = false); 103010984Smartin 10310SN/A /// Create an "inbounds" getelementptr. See the documentation for the 10320SN/A /// "inbounds" flag in LangRef.html for details. 103310984Smartin static Constant *getInBoundsGetElementPtr(Constant *C, 10348035SN/A ArrayRef<Constant *> IdxList) { 10358035SN/A return getGetElementPtr(C, IdxList, true); 10368035SN/A } 103710984Smartin static Constant *getInBoundsGetElementPtr(Constant *C, 10380SN/A Constant *Idx) { 10397388SN/A // This form of the function only exists to avoid ambiguous overload 104016411Splevart // warnings about whether to convert Idx to ArrayRef<Constant *> or 104116411Splevart // ArrayRef<Value *>. 104216411Splevart return getGetElementPtr(C, Idx, true); 104316411Splevart } 104416411Splevart static Constant *getInBoundsGetElementPtr(Constant *C, 10457388SN/A ArrayRef<Value *> IdxList) { 10467388SN/A return getGetElementPtr(C, IdxList, true); 10477388SN/A } 10487388SN/A 10497388SN/A static Constant *getExtractElement(Constant *Vec, Constant *Idx); 10507388SN/A static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx); 10517388SN/A static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask); 10527388SN/A static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs); 10537388SN/A static Constant *getInsertValue(Constant *Agg, Constant *Val, 10547388SN/A ArrayRef<unsigned> Idxs); 105516411Splevart 105616411Splevart /// getOpcode - Return the opcode at the root of this constant expression 10577388SN/A unsigned getOpcode() const { return getSubclassDataFromValue(); } 10587388SN/A 10597388SN/A /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is 10607388SN/A /// not an ICMP or FCMP constant expression. 10610SN/A unsigned getPredicate() const; 10620SN/A 10630SN/A /// getIndices - Assert that this is an insertvalue or exactvalue 10640SN/A /// expression and return the list of indices. 10650SN/A ArrayRef<unsigned> getIndices() const; 10660SN/A 10670SN/A /// getOpcodeName - Return a string representation for an opcode. 10680SN/A const char *getOpcodeName() const; 10690SN/A 10700SN/A /// getWithOperandReplaced - Return a constant expression identical to this 10710SN/A /// one, but with the specified operand set to the specified value. 10720SN/A Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const; 10730SN/A 10740SN/A /// getWithOperands - This returns the current constant expression with the 10750SN/A /// operands replaced with the specified values. The specified array must 107610984Smartin /// have the same number of operands as our current one. 107710984Smartin Constant *getWithOperands(ArrayRef<Constant*> Ops) const { 107810984Smartin return getWithOperands(Ops, getType()); 107910984Smartin } 108010984Smartin 10810SN/A /// getWithOperands - This returns the current constant expression with the 108210984Smartin /// operands replaced with the specified values and with the specified result 108310984Smartin /// type. The specified array must have the same number of operands as our 108410984Smartin /// current one. 108510984Smartin Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const; 108610984Smartin 10870SN/A /// getAsInstruction - Returns an Instruction which implements the same operation 108810984Smartin /// as this ConstantExpr. The instruction is not linked to any basic block. 108910984Smartin /// 10900SN/A /// A better approach to this could be to have a constructor for Instruction 109110984Smartin /// which would take a ConstantExpr parameter, but that would have spread 109210984Smartin /// implementation details of ConstantExpr outside of Constants.cpp, which 109310984Smartin /// would make it harder to remove ConstantExprs altogether. 109410984Smartin Instruction *getAsInstruction(); 109510984Smartin 109610984Smartin virtual void destroyConstant(); 10970SN/A virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 10981385SN/A 10990SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 11004008SN/A static inline bool classof(const Value *V) { 11014008SN/A return V->getValueID() == ConstantExprVal; 11020SN/A } 11030SN/A 11041385SN/Aprivate: 11051385SN/A // Shadow Value::setValueSubclassData with a private forwarding method so that 11061385SN/A // subclasses cannot accidentally use it. 110710984Smartin void setValueSubclassData(unsigned short D) { 11080SN/A Value::setValueSubclassData(D); 11090SN/A } 11100SN/A}; 11117388SN/A 11127388SN/Atemplate <> 11130SN/Astruct OperandTraits<ConstantExpr> : 11140SN/A public VariadicOperandTraits<ConstantExpr, 1> { 11150SN/A}; 11160SN/A 11170SN/ADEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant) 11180SN/A 11190SN/A//===----------------------------------------------------------------------===// 11200SN/A/// UndefValue - 'undef' values are things that do not have specified contents. 11210SN/A/// These are used for a variety of purposes, including global variable 11220SN/A/// initializers and operands to instructions. 'undef' values can occur with 11230SN/A/// any first-class type. 112410071SN/A/// 11250SN/A/// Undef values aren't exactly constants; if they have multiple uses, they 112610289SN/A/// can appear to have different bit patterns at each use. See 112710289SN/A/// LangRef.html#undefvalues for details. 112810289SN/A/// 112910289SN/Aclass UndefValue : public Constant { 113010289SN/A void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 113110289SN/A UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION; 113210289SN/Aprotected: 113310289SN/A explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {} 113410289SN/Aprotected: 113510289SN/A // allocate space for exactly zero operands 11360SN/A void *operator new(size_t s) { 11370SN/A return User::operator new(s, 0); 11380SN/A } 11390SN/Apublic: 11400SN/A /// get() - Static factory methods - Return an 'undef' object of the specified 11410SN/A /// type. 11420SN/A /// 11430SN/A static UndefValue *get(Type *T); 11440SN/A 11450SN/A /// getSequentialElement - If this Undef has array or vector type, return a 11460SN/A /// undef with the right element type. 11470SN/A UndefValue *getSequentialElement() const; 11480SN/A 11490SN/A /// getStructElement - If this undef has struct type, return a undef with the 11500SN/A /// right element type for the specified element. 11510SN/A UndefValue *getStructElement(unsigned Elt) const; 11520SN/A 11530SN/A /// getElementValue - Return an undef of the right value for the specified GEP 11540SN/A /// index. 11550SN/A UndefValue *getElementValue(Constant *C) const; 11560SN/A 11570SN/A /// getElementValue - Return an undef of the right value for the specified GEP 11580SN/A /// index. 11590SN/A UndefValue *getElementValue(unsigned Idx) const; 11600SN/A 11610SN/A virtual void destroyConstant(); 11620SN/A 116310071SN/A /// Methods for support type inquiry through isa, cast, and dyn_cast: 11640SN/A static bool classof(const Value *V) { 116512352Szmajo return V->getValueID() == UndefValueVal; 11660SN/A } 11670SN/A}; 11680SN/A 11690SN/A} // End llvm namespace 11700SN/A 11710SN/A#endif 11720SN/A