1 //===-- llvm/ConstPoolVals.h - Constant Value nodes --------------*- C++ -*--=//
3 // This file contains the declarations for the ConstPoolVal class and all of
4 // its subclasses, which represent the different type of constant pool values
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
11 #include "llvm/User.h"
12 #include "llvm/SymTabValue.h"
13 #include "llvm/Support/DataTypes.h"
19 //===----------------------------------------------------------------------===//
21 //===----------------------------------------------------------------------===//
23 class ConstPoolVal : public User {
26 friend class ValueHolder<ConstPoolVal, SymTabValue, SymTabValue>;
27 inline void setParent(SymTabValue *parent) {
32 inline ConstPoolVal(const Type *Ty, const string &Name = "")
33 : User(Ty, Value::ConstantVal, Name) { Parent = 0; }
36 // Specialize setName to handle symbol table majik...
37 virtual void setName(const string &name);
39 // Static constructor to create a '0' constant of arbitrary type...
40 static ConstPoolVal *getNullConstant(const Type *Ty);
42 // clone() - Create a copy of 'this' value that is identical in all ways
43 // except the following:
44 // * The value has no parent
45 // * The value has no name
47 virtual ConstPoolVal *clone() const = 0;
49 virtual string getStrValue() const = 0;
50 virtual bool equals(const ConstPoolVal *V) const = 0;
52 inline const SymTabValue *getParent() const { return Parent; }
53 inline SymTabValue *getParent() { return Parent; }
54 inline const Value *getParentV() const { return Parent->getSTVParent(); }
55 inline Value *getParentV() { return Parent->getSTVParent(); }
60 //===----------------------------------------------------------------------===//
61 // Classes to represent constant pool variable defs
62 //===----------------------------------------------------------------------===//
64 //===---------------------------------------------------------------------------
65 // ConstPoolBool - Boolean Values
67 class ConstPoolBool : public ConstPoolVal {
69 ConstPoolBool(const ConstPoolBool &CP);
71 ConstPoolBool(const Type *Ty, bool V, const string &Name = "");
72 ConstPoolBool(bool V, const string &Name = "");
74 virtual string getStrValue() const;
75 virtual bool equals(const ConstPoolVal *V) const;
77 virtual ConstPoolVal *clone() const { return new ConstPoolBool(*this); }
79 inline bool getValue() const { return Val; }
81 // setValue - Be careful... if there is more than one 'use' of this node, then
82 // they will ALL see the value that you set...
84 inline void setValue(bool v) { Val = v; }
88 //===---------------------------------------------------------------------------
89 // ConstPoolInt - Superclass of ConstPoolSInt & ConstPoolUInt, to make dealing
90 // with integral constants easier.
92 class ConstPoolInt : public ConstPoolVal {
98 ConstPoolInt(const ConstPoolInt &CP);
100 ConstPoolInt(const Type *Ty, int64_t V, const string &Name = "");
101 ConstPoolInt(const Type *Ty, uint64_t V, const string &Name = "");
103 virtual bool equals(const ConstPoolVal *V) const;
105 // equalsInt - Provide a helper method that can be used to determine if the
106 // constant contained within is equal to a constant. This only works for very
107 // small values, because this is all that can be represented with all types.
109 bool equalsInt(unsigned char V) const {
110 assert(V <= 127 && "equals: Can only be used with very small constants!");
111 return Val.Unsigned == V;
114 // ConstPoolInt::get static method: return a constant pool int with the
115 // specified value. as above, we work only with very small values here.
117 static ConstPoolInt *get(const Type *Ty, unsigned char V);
121 //===---------------------------------------------------------------------------
122 // ConstPoolSInt - Signed Integer Values [sbyte, short, int, long]
124 class ConstPoolSInt : public ConstPoolInt {
125 ConstPoolSInt(const ConstPoolSInt &CP) : ConstPoolInt(CP) {}
127 ConstPoolSInt(const Type *Ty, int64_t V, const string &Name = "");
129 virtual ConstPoolVal *clone() const { return new ConstPoolSInt(*this); }
131 virtual string getStrValue() const;
133 static bool isValueValidForType(const Type *Ty, int64_t V);
134 inline int64_t getValue() const { return Val.Signed; }
138 //===---------------------------------------------------------------------------
139 // ConstPoolUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
141 class ConstPoolUInt : public ConstPoolInt {
142 ConstPoolUInt(const ConstPoolUInt &CP) : ConstPoolInt(CP) {}
144 ConstPoolUInt(const Type *Ty, uint64_t V, const string &Name = "");
146 virtual ConstPoolVal *clone() const { return new ConstPoolUInt(*this); }
148 virtual string getStrValue() const;
150 static bool isValueValidForType(const Type *Ty, uint64_t V);
151 inline uint64_t getValue() const { return Val.Unsigned; }
155 //===---------------------------------------------------------------------------
156 // ConstPoolFP - Floating Point Values [float, double]
158 class ConstPoolFP : public ConstPoolVal {
160 ConstPoolFP(const ConstPoolFP &CP);
162 ConstPoolFP(const Type *Ty, double V, const string &Name = "");
164 virtual ConstPoolVal *clone() const { return new ConstPoolFP(*this); }
165 virtual string getStrValue() const;
166 virtual bool equals(const ConstPoolVal *V) const;
168 static bool isValueValidForType(const Type *Ty, double V);
169 inline double getValue() const { return Val; }
173 //===---------------------------------------------------------------------------
174 // ConstPoolType - Type Declarations
176 class ConstPoolType : public ConstPoolVal {
178 ConstPoolType(const ConstPoolType &CPT);
180 ConstPoolType(const Type *V, const string &Name = "");
182 virtual ConstPoolVal *clone() const { return new ConstPoolType(*this); }
183 virtual string getStrValue() const;
184 virtual bool equals(const ConstPoolVal *V) const;
186 inline const Type *getValue() const { return Val; }
190 //===---------------------------------------------------------------------------
191 // ConstPoolArray - Constant Array Declarations
193 class ConstPoolArray : public ConstPoolVal {
194 ConstPoolArray(const ConstPoolArray &CPT);
196 ConstPoolArray(const ArrayType *T, vector<ConstPoolVal*> &V,
197 const string &Name = "");
199 virtual ConstPoolVal *clone() const { return new ConstPoolArray(*this); }
200 virtual string getStrValue() const;
201 virtual bool equals(const ConstPoolVal *V) const;
203 inline const vector<Use> &getValues() const { return Operands; }
207 //===---------------------------------------------------------------------------
208 // ConstPoolStruct - Constant Struct Declarations
210 class ConstPoolStruct : public ConstPoolVal {
211 ConstPoolStruct(const ConstPoolStruct &CPT);
213 ConstPoolStruct(const StructType *T, vector<ConstPoolVal*> &V,
214 const string &Name = "");
216 virtual ConstPoolVal *clone() const { return new ConstPoolStruct(*this); }
217 virtual string getStrValue() const;
218 virtual bool equals(const ConstPoolVal *V) const;
220 inline const vector<Use> &getValues() const { return Operands; }
223 //===---------------------------------------------------------------------------
224 // External functions
227 // Convenience functions to get the value of an integer constant, for an
228 // appropriate integer or non-integer type that can be held in an integer.
229 // The type of the argument must be the following:
230 // GetSignedIntConstantValue: signed integer or bool
231 // GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
232 // GetConstantValueAsSignedInt: any of the above, but the value
233 // must fit into a int64_t.
235 // isValidConstant is set to true if a valid constant was found.
237 int64_t GetSignedIntConstantValue (const Value* val, bool& isValidConst);
238 uint64_t GetUnsignedIntConstantValue (const Value* val, bool& isValidConst);
239 int64_t GetConstantValueAsSignedInt (const Value* val, bool& isValidConst);
241 //===---------------------------------------------------------------------------