Constants.h revision 40bbeb5d077b1bc2b933d8c8628024dfa9b428c3
148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
26fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
56fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// This file was developed by the LLVM research group and is distributed under
66fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
76fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
1031bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner// This file contains the declarations for the subclasses of Constant, which
1131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner// represent the different type of constant pool values
12009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
14009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
1531bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#ifndef LLVM_CONSTANTS_H
1631bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#define LLVM_CONSTANTS_H
17009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
1831bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#include "llvm/Constant.h"
19360e17eaf1a2abda82b02235dc57d26d8f83c937Chris Lattner#include "Support/DataTypes.h"
20009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
21d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
22d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
23009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
24009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
254cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
26009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
276cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
286cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
295133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
305133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnerstruct ConvertConstantType;
315133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
32994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
33994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner//===---------------------------------------------------------------------------
3493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantIntegral - Shared superclass of boolean and integer constants.
3593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
3693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// This class just defines some common interfaces to be implemented.
3793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
38c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattnerclass ConstantIntegral : public Constant {
39994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerprotected:
40c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  ConstantIntegral(const Type *Ty) : Constant(Ty) {}
41994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
42994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
4393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
4493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
4593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
46994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isNullValue() const = 0;
47994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
4893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMaxValue - Return true if this is the largest value that may be
4993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
5093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
51994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const = 0;
52994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
5393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMinValue - Return true if this is the smallest value that may be
5493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
5593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
56994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const = 0;
57994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
5893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isAllOnesValue - Return true if every bit in this constant is set to true.
5993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
60994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isAllOnesValue() const = 0;
61994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
6293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Static constructor to get the maximum/minimum/allones constant of
6393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// specified (integral) type...
6493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
65c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static ConstantIntegral *getMaxValue(const Type *Ty);
66c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static ConstantIntegral *getMinValue(const Type *Ty);
67c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static ConstantIntegral *getAllOnesValue(const Type *Ty);
68c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner
6993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
70c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static inline bool classof(const ConstantIntegral *) { return true; }
71994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
72994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static inline bool classof(const Value *V) {
73994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
74994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
75994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner};
76994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
77994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
78009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
7993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantBool - Boolean Values
8093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
81c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattnerclass ConstantBool : public ConstantIntegral {
82009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  bool Val;
83e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantBool(bool V);
84009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
85e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantBool *True, *False;  // The True & False values
86009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
8793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
88e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantBool *get(bool Value) { return Value ? True : False; }
89e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
90009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
9193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// inverted - Return the opposite value of the current value.
92e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  inline ConstantBool *inverted() const { return (this==True) ? False : True; }
93009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
9493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValue - return the boolean value of this constant.
9593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
96009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  inline bool getValue() const { return Val; }
971d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner
9893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
9993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
10093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
1010eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return this == False; }
102994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const { return this == True; }
103994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const { return this == False; }
104994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isAllOnesValue() const { return this == True; }
1050eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
10693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
107e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantBool *) { return true; }
108e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static bool classof(const Constant *CPV) {
1091d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner    return (CPV == True) | (CPV == False);
1101d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner  }
111b00c582b6d40e6b9ff2d1ed4f5eaf7930e792aceChris Lattner  static inline bool classof(const Value *V) {
112e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
1131d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner  }
114009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
115009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
116009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
117009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
11893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
11993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// with integral constants easier.
12093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
121c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattnerclass ConstantInt : public ConstantIntegral {
1222ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattnerprotected:
1232ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  union {
1242ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner    int64_t  Signed;
1252ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner    uint64_t Unsigned;
1262ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  } Val;
127e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
128e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantInt(const Type *Ty, uint64_t V);
1292ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattnerpublic:
13093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// equalsInt - Provide a helper method that can be used to determine if the
13193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// constant contained within is equal to a constant.  This only works for
13293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// very small values, because this is all that can be represented with all
13393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// types.
13493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
135f98e88f7453df864a56bda6ca19cf70e09bf3e6eChris Lattner  bool equalsInt(unsigned char V) const {
1363e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattner    assert(V <= 127 &&
1373e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattner	   "equals: Can only be used with very small positive constants!");
1382ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner    return Val.Unsigned == V;
1392ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
1402ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
14193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// ConstantInt::get static method: return a ConstantInt with the specified
14293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// value.  as above, we work only with very small values here.
14393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
144e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantInt *get(const Type *Ty, unsigned char V);
1451d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner
146af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  /// getRawValue - return the underlying value of this constant as a 64-bit
147af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  /// unsigned integer value.
148af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
149af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  inline uint64_t getRawValue() const { return Val.Unsigned; }
150af28ac2201eafb90baa336975a3293467974acf7Chris Lattner
15193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
15293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
1530eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return Val.Unsigned == 0; }
154994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const = 0;
155994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const = 0;
1560eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
15793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
158e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantInt *) { return true; }
159994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
160b00c582b6d40e6b9ff2d1ed4f5eaf7930e792aceChris Lattner  static inline bool classof(const Value *V) {
161e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
1621d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner  }
1632ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner};
1642ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
1652ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
1662ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner//===---------------------------------------------------------------------------
16793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
16893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
169e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantSInt : public ConstantInt {
170e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
1716cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
1726cc89aad25155ecd93b5318414851aa46351196dChris Lattner
1733e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
174e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantSInt(const Type *Ty, int64_t V);
175009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
17693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
177af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
178e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantSInt *get(const Type *Ty, int64_t V);
179009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
18093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
181af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
182009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, int64_t V);
18393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
18493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValue - return the underlying value of this constant.
185af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
1862ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  inline int64_t getValue() const { return Val.Signed; }
1875ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
1881680312867fffeb9369800949b809e0b9e29a914Chris Lattner  virtual bool isAllOnesValue() const { return getValue() == -1; }
1891680312867fffeb9369800949b809e0b9e29a914Chris Lattner
19093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMaxValue - Return true if this is the largest value that may be
19193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
19293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
193994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const {
194994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    int64_t V = getValue();
195994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    if (V < 0) return false;    // Be careful about wrap-around on 'long's
196994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    ++V;
197994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    return !isValueValidForType(getType(), V) || V < 0;
198994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
199994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
20093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMinValue - Return true if this is the smallest value that may be
20193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
20293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
203994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const {
204994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    int64_t V = getValue();
205994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    if (V > 0) return false;    // Be careful about wrap-around on 'long's
206994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    --V;
207994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    return !isValueValidForType(getType(), V) || V > 0;
208994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
209994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
21093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
211af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
212e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantSInt *) { return true; }
213994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
2145ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
215e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
2165ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
217009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
218009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
219009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
22093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
22193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
222e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantUInt : public ConstantInt {
223e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
2246cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
2253e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
226e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantUInt(const Type *Ty, uint64_t V);
227009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
22893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
229af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
230e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantUInt *get(const Type *Ty, uint64_t V);
231009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
23293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
233af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
234009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, uint64_t V);
23593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
23693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValue - return the underlying value of this constant.
237af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
2382ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  inline uint64_t getValue() const { return Val.Unsigned; }
2395ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
24093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMaxValue - Return true if this is the largest value that may be
24193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
24293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
2431680312867fffeb9369800949b809e0b9e29a914Chris Lattner  virtual bool isAllOnesValue() const;
244994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const { return isAllOnesValue(); }
245994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const { return getValue() == 0; }
246994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
24793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
248e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantUInt *) { return true; }
249994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
2505ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
251e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
2525ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
253009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
254009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
255009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
256009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
25793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
25893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
259e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
260009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  double Val;
261cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
262cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
263e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
2643e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
265e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const Type *Ty, double V);
266009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
26793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
268e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantFP *get(const Type *Ty, double V);
269009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
27093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
271009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, double V);
272009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  inline double getValue() const { return Val; }
2735ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
27493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
275cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
276cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// considers -0.0 to be null as well as 0.0.  :(
277cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  virtual bool isNullValue() const {
278cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    union {
279cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner      double V;
280cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner      uint64_t I;
281cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    } T;
282cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    T.V = Val;
283cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    return T.I == 0;
284cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  }
2850eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
286f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
287f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
288f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
289f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// two floating point values.
290f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  bool isExactlyValue(double V) const {
291f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    union {
292f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      double V;
293f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      uint64_t I;
294f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    } T1;
295f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    T1.V = Val;
296f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    union {
297f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      double V;
298f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      uint64_t I;
299f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    } T2;
300c07cd132cb3c6032e837f4b432c8f895ca1f1182Chris Lattner    T2.V = V;
301f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    return T1.I == T2.I;
302f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  }
303f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner
30493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
305e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
306994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
3075ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
308e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
3095ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
310009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
311009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
31240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner//===---------------------------------------------------------------------------
31340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
31440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
31540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
31640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
31740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
31840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
31940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const Type *Ty) : Constant(Ty) {}
32040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
32140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// get() - static factory method for creating a null aggregate.  It is
32240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// illegal to call this method with a non-aggregate type.
32340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static Constant *get(const Type *Ty);
32440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
32540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
32640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.
32740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return true; }
32840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
32940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void destroyConstant();
33040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
33140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner                                           bool DisableChecking = false);
33240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
33340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
33440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
33540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static inline bool classof(const ConstantAggregateZero *) { return true; }
33640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static bool classof(const Constant *CPV);
33740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static inline bool classof(const Value *V) {
33840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
33940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
34040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
34140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
342009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
343009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
34493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
34593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
346e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3476cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
3486cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
349e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3503e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
351697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
352009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
35393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
354ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
355ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const std::string &Initializer);
356c9abc6528df2e50b9951fb45acedf7e3bcd4617bVikram S. Adve
35793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
35893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
35993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
360682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3618b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
362682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
363009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3649b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// isString - This method returns true if the array is an array of sbyte or
3659b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// ubyte, and if the elements of the array are all ConstantInt's.
3669b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3679b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
3689b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// getAsString - If this array is isString(), then this method converts the
3699b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// array to an std::string and returns it.  Otherwise, it asserts out.
37093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
37193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  std::string getAsString() const;
37293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
37393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValues - Return a vector of the component constants that make up this
37493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// array.
375697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  inline const std::vector<Use> &getValues() const { return Operands; }
3765ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
37793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
37840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero arrays are always
37940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
38040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return false; }
3810eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
382e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
3836ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
3846ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
385e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
38693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
387e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
388994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
3895ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
390e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
3915ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
392009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
393009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
394009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
395009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
396e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
397009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
398e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3996cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
4006cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
401e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
4023e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
403697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
404009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
40593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
406ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
407009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
40893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
409682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const StructType *getType() const {
4108b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const StructType*>(Value::getType());
411682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
412009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
41393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValues - Return a vector of the component constants that make up this
41493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// structure.
415697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  inline const std::vector<Use> &getValues() const { return Operands; }
4165ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
41793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
41840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero structs are always
41940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
420a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
42140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner    return false;
422a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
4230eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
424e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
4256ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
4266ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
42793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
42893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
429e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
430994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
4315ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
432e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
4335ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
434009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
435009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
4364cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner//===---------------------------------------------------------------------------
43793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerNull - a constant pointer value that points to null
43893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
43948babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerNull : public Constant {
4406cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
441e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
4425ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerprotected:
44348babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  ConstantPointerNull(const PointerType *T)
44448babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner    : Constant(reinterpret_cast<const Type*>(T)) {}
4456cc89aad25155ecd93b5318414851aa46351196dChris Lattner
4465ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
4475ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
44893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
449e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerNull *get(const PointerType *T);
4505ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
45193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
45293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
4530eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return true; }
4540eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
455e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
456e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
45793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
458e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
45948babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  static bool classof(const Constant *CPV);
4605ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
46148babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
4625ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
4634cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
4644cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
465f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
46648babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner//===---------------------------------------------------------------------------
46793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerRef - a constant pointer value that is initialized to
46893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// point to a global value, which lies at a constant, fixed address.
46993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
47048babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerRef : public Constant {
4715ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  friend class Module;   // Modules maintain these references
472e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerRef(const ConstantPointerRef &); // DNI!
4735ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
474f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattnerprotected:
475e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerRef(GlobalValue *GV);
476f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattnerpublic:
47793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
478e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerRef *get(GlobalValue *GV);
479f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
480ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner  const GlobalValue *getValue() const {
481ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner    return cast<GlobalValue>(Operands[0].get());
482f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner  }
48393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
484ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner  GlobalValue *getValue() {
485ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner    return cast<GlobalValue>(Operands[0].get());
486f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner  }
4875ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
48848babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
48948babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  /// getNullValue.
49048babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  virtual bool isNullValue() const { return false; }
49148babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner
492e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
4936ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
4946ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
495e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
49693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
497e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerRef *) { return true; }
49848babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  static bool classof(const Constant *CPV);
4995ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
50048babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
5015ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
502f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner};
503f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
504baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// ConstantExpr - a constant value that is initialized with an expression using
505baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// other constant values.  This is only used to represent values that cannot be
506baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// evaluated at compile-time (e.g., something derived from an address) because
507baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// it does not have a mechanism to store the actual value.  Use the appropriate
508baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// Constant subclass above for known constants.
50929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve//
51029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
5116cc89aad25155ecd93b5318414851aa46351196dChris Lattner  unsigned iType;      // Operation type (an Instruction opcode)
5126cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
5136cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
5145133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
51529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
5166cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
517baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // Cast creation ctor
518baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
519baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // Binary/Shift instruction creation ctor
520e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
521baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // GEP instruction creation ctor
522e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
523e8e4605021141d689493132a9c7c6fce6294937fChris Lattner               const Type *DestTy);
5245133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
5255133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
5265133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
5275133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
5285133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                         Constant *C1, Constant *C2);
5295133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getShiftTy(const Type *Ty,
5305133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                              unsigned Opcode, Constant *C1, Constant *C2);
5315133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
5325133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                                      const std::vector<Constant*> &IdxList);
53329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
53429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
535fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
536baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
537baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
538baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
539e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
54093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5415133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
542fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getCast(Constant *C, const Type *Ty);
543e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
5447e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
5457e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
5465133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
5475133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
5485133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner    return getTy(C1->getType(), Opcode, C1, C2);
5495133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  }
550e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
55193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Getelementptr form...
5525133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
553fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getGetElementPtr(Constant *C,
554fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner                                    const std::vector<Constant*> &IdxList);
55529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
55693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
55793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
55829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isNullValue() const { return false; }
55929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
56093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
56129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  unsigned getOpcode() const { return iType; }
56229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
56393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
564e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
56529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
56693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isConstantExpr - Return true if this is a ConstantExpr
56729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isConstantExpr() const { return true; }
568e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
569e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
5706ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
5716ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
572e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
57317aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  /// Override methods to provide more type information...
57417aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) {
57517aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return cast<Constant>(User::getOperand(i));
57617aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
57717aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) const {
57817aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
57917aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
58017aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
58117aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
58293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
58329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
58429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Constant *CPV) {
58529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve    return CPV->isConstantExpr();
58629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
58729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
58829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve    return isa<Constant>(V) && classof(cast<Constant>(V));
58929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
59029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
59129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
592d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
593d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
594009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
595