Constants.h revision f0fd6845eee799f62ac69297c9fd7086a0156cfa
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;
300f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    T2.V = Val;
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
312009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
313009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
31493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
31593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
316e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3176cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
3186cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
319e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3203e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
321697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
322009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
32393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
324697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
325697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  static ConstantArray *get(const std::string &Initializer);
326c9abc6528df2e50b9951fb45acedf7e3bcd4617bVikram S. Adve
32793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
32893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
32993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
330682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3318b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
332682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
333009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3349b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// isString - This method returns true if the array is an array of sbyte or
3359b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// ubyte, and if the elements of the array are all ConstantInt's.
3369b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3379b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
3389b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// getAsString - If this array is isString(), then this method converts the
3399b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// array to an std::string and returns it.  Otherwise, it asserts out.
34093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
34193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  std::string getAsString() const;
34293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
34393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValues - Return a vector of the component constants that make up this
34493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// array.
345697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  inline const std::vector<Use> &getValues() const { return Operands; }
3465ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
34793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
34893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
349a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
350a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    // FIXME: This should be made to be MUCH faster.  Just check against well
351a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    // known null value!
3529b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner    if (getNumOperands()) {
3539b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner      const Constant *First = cast<Constant>(getOperand(0));
3549b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner      if (!First->isNullValue()) return false;
3559b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner      for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
3569b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner        if (cast<Constant>(getOperand(i)) != First)
3579b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner          return false;
3589b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner    }
359a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    return true;
360a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
3610eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
362e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
3636ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
3646ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
365e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
36693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
367e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
368994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
3695ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
370e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
3715ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
372009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
373009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
374009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
375009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
376e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
377009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
378e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3796cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
3806cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
381e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3823e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
383697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
384009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
38593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
386e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantStruct *get(const StructType *T,
387697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner                             const std::vector<Constant*> &V);
388009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
38993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
390682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const StructType *getType() const {
3918b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const StructType*>(Value::getType());
392682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
393009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
39493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValues - Return a vector of the component constants that make up this
39593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// structure.
396697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  inline const std::vector<Use> &getValues() const { return Operands; }
3975ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
39893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
39993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
400a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
401a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    // FIXME: This should be made to be MUCH faster.  Just check against well
402a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    // known null value!
403a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
404a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner      if (!cast<Constant>(getOperand(i))->isNullValue())
405a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner        return false;
406a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner    return true;
407a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
4080eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
409e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
4106ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
4116ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
41293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
41393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
414e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
415994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  static bool classof(const Constant *CPV);  // defined in Constants.cpp
4165ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
417e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
4185ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
419009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
420009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
4214cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner//===---------------------------------------------------------------------------
42293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerNull - a constant pointer value that points to null
42393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
42448babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerNull : public Constant {
4256cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
426e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
4275ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerprotected:
42848babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  ConstantPointerNull(const PointerType *T)
42948babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner    : Constant(reinterpret_cast<const Type*>(T)) {}
4306cc89aad25155ecd93b5318414851aa46351196dChris Lattner
4315ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
4325ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
43393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
434e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerNull *get(const PointerType *T);
4355ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
43693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
43793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
4380eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return true; }
4390eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
440e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
441e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
44293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
443e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
44448babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  static bool classof(const Constant *CPV);
4455ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
44648babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
4475ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
4484cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
4494cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
450f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
45148babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner//===---------------------------------------------------------------------------
45293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerRef - a constant pointer value that is initialized to
45393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// point to a global value, which lies at a constant, fixed address.
45493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
45548babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerRef : public Constant {
4565ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  friend class Module;   // Modules maintain these references
457e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerRef(const ConstantPointerRef &); // DNI!
4585ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
459f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattnerprotected:
460e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerRef(GlobalValue *GV);
461f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattnerpublic:
46293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
463e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerRef *get(GlobalValue *GV);
464f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
465ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner  const GlobalValue *getValue() const {
466ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner    return cast<GlobalValue>(Operands[0].get());
467f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner  }
46893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
469ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner  GlobalValue *getValue() {
470ef9c23f2812322ae5c5f3140bfbcf92629d7ff47Chris Lattner    return cast<GlobalValue>(Operands[0].get());
471f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner  }
4725ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
47348babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
47448babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  /// getNullValue.
47548babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  virtual bool isNullValue() const { return false; }
47648babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner
477e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
4786ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
4796ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
480e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
48193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
482e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerRef *) { return true; }
48348babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner  static bool classof(const Constant *CPV);
4845ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  static inline bool classof(const Value *V) {
48548babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattner    return isa<Constant>(V) && classof(cast<Constant>(V));
4865ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
487f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner};
488f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
489baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// ConstantExpr - a constant value that is initialized with an expression using
490baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// other constant values.  This is only used to represent values that cannot be
491baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// evaluated at compile-time (e.g., something derived from an address) because
492baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// it does not have a mechanism to store the actual value.  Use the appropriate
493baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// Constant subclass above for known constants.
49429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve//
49529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
4966cc89aad25155ecd93b5318414851aa46351196dChris Lattner  unsigned iType;      // Operation type (an Instruction opcode)
4976cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
4986cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
4995133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
50029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
5016cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
502baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // Cast creation ctor
503baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
504baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // Binary/Shift instruction creation ctor
505e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
506baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // GEP instruction creation ctor
507e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
508e8e4605021141d689493132a9c7c6fce6294937fChris Lattner               const Type *DestTy);
5095133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
5105133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
5115133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
5125133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
5135133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                         Constant *C1, Constant *C2);
5145133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getShiftTy(const Type *Ty,
5155133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                              unsigned Opcode, Constant *C1, Constant *C2);
5165133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
5175133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                                      const std::vector<Constant*> &IdxList);
51829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
51929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
520fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
521baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
522baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
523baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
524e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
52593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5265133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
527fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getCast(Constant *C, const Type *Ty);
528e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
5297e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
5307e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
5315133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
5325133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
5335133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner    return getTy(C1->getType(), Opcode, C1, C2);
5345133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  }
535e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
53693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Getelementptr form...
5375133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
538fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getGetElementPtr(Constant *C,
539fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner                                    const std::vector<Constant*> &IdxList);
54029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
54193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
54293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
54329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isNullValue() const { return false; }
54429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
54593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
54629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  unsigned getOpcode() const { return iType; }
54729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
54893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
549e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
55029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
55193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isConstantExpr - Return true if this is a ConstantExpr
55229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isConstantExpr() const { return true; }
553e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
554e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
5556ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
5566ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
557e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
55817aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  /// Override methods to provide more type information...
55917aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) {
56017aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return cast<Constant>(User::getOperand(i));
56117aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
56217aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) const {
56317aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
56417aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
56517aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
56617aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
56793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
56829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
56929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Constant *CPV) {
57029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve    return CPV->isConstantExpr();
57129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
57229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
57329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve    return isa<Constant>(V) && classof(cast<Constant>(V));
57429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
57529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
57629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
577d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
578d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
579009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
580