Constants.h revision 049c48f1adf5cd7862cc4a48a3d3935a0e98ebb0
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"
1952eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner#include "llvm/Type.h"
20360e17eaf1a2abda82b02235dc57d26d8f83c937Chris Lattner#include "Support/DataTypes.h"
21009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
22d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
24009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
25009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
264cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
27009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
286cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
296cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
305133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
315133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnerstruct ConvertConstantType;
325133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
33994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
34994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner//===---------------------------------------------------------------------------
3593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantIntegral - Shared superclass of boolean and integer constants.
3693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
3793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// This class just defines some common interfaces to be implemented.
3893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
39c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattnerclass ConstantIntegral : public Constant {
40994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerprotected:
41c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  union {
42c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner    int64_t  Signed;
43c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner    uint64_t Unsigned;
44c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  } Val;
45c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  ConstantIntegral(const Type *Ty, uint64_t V);
46994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
47994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
48c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  /// getRawValue - return the underlying value of this constant as a 64-bit
49c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  /// unsigned integer value.
50c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  ///
51c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  inline uint64_t getRawValue() const { return Val.Unsigned; }
52c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner
5393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
5493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
5593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
56994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isNullValue() const = 0;
57994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
5893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMaxValue - Return true if this is the largest value that may be
5993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
6093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
61994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const = 0;
62994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
6393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMinValue - Return true if this is the smallest value that may be
6493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
6593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
66994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const = 0;
67994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
6893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isAllOnesValue - Return true if every bit in this constant is set to true.
6993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
70994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isAllOnesValue() const = 0;
71994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
7293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Static constructor to get the maximum/minimum/allones constant of
7393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// specified (integral) type...
7493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
75c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static ConstantIntegral *getMaxValue(const Type *Ty);
76c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static ConstantIntegral *getMinValue(const Type *Ty);
77c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static ConstantIntegral *getAllOnesValue(const Type *Ty);
78c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner
7993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
80c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattner  static inline bool classof(const ConstantIntegral *) { return true; }
8152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
8252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
8352eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->isIntegral();
84994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
85994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner};
86994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
87994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
88009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
8993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantBool - Boolean Values
9093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
91c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattnerclass ConstantBool : public ConstantIntegral {
92e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantBool(bool V);
93009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
94e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantBool *True, *False;  // The True & False values
95009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
9693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
97e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantBool *get(bool Value) { return Value ? True : False; }
98e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
99009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
10093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// inverted - Return the opposite value of the current value.
101e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  inline ConstantBool *inverted() const { return (this==True) ? False : True; }
102009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
10393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValue - return the boolean value of this constant.
10493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
105c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner  inline bool getValue() const { return static_cast<bool>(getRawValue()); }
1061d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner
10793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
10893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
10993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
1100eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return this == False; }
111994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const { return this == True; }
112994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const { return this == False; }
113994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isAllOnesValue() const { return this == True; }
1140eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
11593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
116e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantBool *) { return true; }
11752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
11852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return (V == True) | (V == False);
1191d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner  }
120009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
121009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
122009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
123009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
12493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
12593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// with integral constants easier.
12693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
127c75071c3bff982c6d83e1274060ec5fcf5fa5922Chris Lattnerclass ConstantInt : public ConstantIntegral {
1282ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattnerprotected:
129e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
130e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantInt(const Type *Ty, uint64_t V);
1312ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattnerpublic:
13293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// equalsInt - Provide a helper method that can be used to determine if the
13393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// constant contained within is equal to a constant.  This only works for
13493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// very small values, because this is all that can be represented with all
13593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// types.
13693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
137f98e88f7453df864a56bda6ca19cf70e09bf3e6eChris Lattner  bool equalsInt(unsigned char V) const {
1383e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattner    assert(V <= 127 &&
139049c48f1adf5cd7862cc4a48a3d3935a0e98ebb0Misha Brukman           "equalsInt: Can only be used with very small positive constants!");
1402ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner    return Val.Unsigned == V;
1412ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
1422ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
14393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// ConstantInt::get static method: return a ConstantInt with the specified
14493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// value.  as above, we work only with very small values here.
14593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
146e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantInt *get(const Type *Ty, unsigned char V);
1471d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner
14893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
14993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
1500eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return Val.Unsigned == 0; }
151994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const = 0;
152994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const = 0;
1530eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
15493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
155e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantInt *) { return true; }
15652eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
15752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
15852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->isInteger();
1591d87bcf4909b06dcd86320722653341f08b8b396Chris Lattner  }
1602ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner};
1612ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
1622ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
1632ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner//===---------------------------------------------------------------------------
16493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
16593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
166e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantSInt : public ConstantInt {
167e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
1686cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
1696cc89aad25155ecd93b5318414851aa46351196dChris Lattner
1703e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
171e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantSInt(const Type *Ty, int64_t V);
172009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
17393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
174af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
175e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantSInt *get(const Type *Ty, int64_t V);
176009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
17793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
178af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
179009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, int64_t V);
18093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
18193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValue - return the underlying value of this constant.
182af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
1832ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  inline int64_t getValue() const { return Val.Signed; }
1845ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
1851680312867fffeb9369800949b809e0b9e29a914Chris Lattner  virtual bool isAllOnesValue() const { return getValue() == -1; }
1861680312867fffeb9369800949b809e0b9e29a914Chris Lattner
18793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMaxValue - Return true if this is the largest value that may be
18893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
18993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
190994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const {
191994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    int64_t V = getValue();
192994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    if (V < 0) return false;    // Be careful about wrap-around on 'long's
193994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    ++V;
194994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    return !isValueValidForType(getType(), V) || V < 0;
195994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
196994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
19793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMinValue - Return true if this is the smallest value that may be
19893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
19993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
200994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const {
201994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    int64_t V = getValue();
202994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    if (V > 0) return false;    // Be careful about wrap-around on 'long's
203994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    --V;
204994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner    return !isValueValidForType(getType(), V) || V > 0;
205994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
206994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
20793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
208af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
209e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantSInt *) { return true; }
21052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
21152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
21252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->isSigned();
2135ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
214009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
215009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
216009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
21793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
21893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
219e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantUInt : public ConstantInt {
220e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
2216cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
2223e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
223e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantUInt(const Type *Ty, uint64_t V);
224009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
22593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
226af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
227e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantUInt *get(const Type *Ty, uint64_t V);
228009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
22993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
230af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
231009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, uint64_t V);
23293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
23393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValue - return the underlying value of this constant.
234af28ac2201eafb90baa336975a3293467974acf7Chris Lattner  ///
2352ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  inline uint64_t getValue() const { return Val.Unsigned; }
2365ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
23793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isMaxValue - Return true if this is the largest value that may be
23893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// represented by this type.
23993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
2401680312867fffeb9369800949b809e0b9e29a914Chris Lattner  virtual bool isAllOnesValue() const;
241994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMaxValue() const { return isAllOnesValue(); }
242994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  virtual bool isMinValue() const { return getValue() == 0; }
243994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
24493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
245e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantUInt *) { return true; }
24652eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
24752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
24852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->isUnsigned();
2495ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
250009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
251009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
252009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
253009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
25493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
25593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
256e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
257009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  double Val;
258cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
259cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
260e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
2613e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
262e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const Type *Ty, double V);
263009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
26493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
265e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantFP *get(const Type *Ty, double V);
266009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
26793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
268009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, double V);
269009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  inline double getValue() const { return Val; }
2705ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
27193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
272cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
273cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// considers -0.0 to be null as well as 0.0.  :(
274cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  virtual bool isNullValue() const {
275cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    union {
276cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner      double V;
277cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner      uint64_t I;
278cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    } T;
279cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    T.V = Val;
280cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner    return T.I == 0;
281cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  }
2820eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
283f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
284f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
285f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
286f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// two floating point values.
287f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  bool isExactlyValue(double V) const {
288f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    union {
289f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      double V;
290f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      uint64_t I;
291f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    } T1;
292f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    T1.V = Val;
293f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    union {
294f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      double V;
295f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner      uint64_t I;
296f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    } T2;
297c07cd132cb3c6032e837f4b432c8f895ca1f1182Chris Lattner    T2.V = V;
298f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner    return T1.I == T2.I;
299f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  }
300f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner
30193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
302e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
30352eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
30452eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
30552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->isFloatingPoint();
3065ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
307009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
308009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
30940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner//===---------------------------------------------------------------------------
31040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
31140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
31240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
31340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
31440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
31540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
31652eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  ConstantAggregateZero(const Type *Ty)
31752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    : Constant(Ty, ConstantAggregateZeroVal) {}
31840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
31940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// get() - static factory method for creating a null aggregate.  It is
32040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// illegal to call this method with a non-aggregate type.
32140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static Constant *get(const Type *Ty);
32240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
32340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
32440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.
32540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return true; }
32640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
32740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void destroyConstant();
32840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
32940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner                                           bool DisableChecking = false);
33040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
33140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
33240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
33352eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const ConstantAggregateZero *) { return true; }
33452eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
33552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == ConstantAggregateZeroVal;
33640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
33740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
33840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
339009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
340009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
34193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
34293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
343e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3446cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
3456cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
346e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3473e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
348697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
349009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
35093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
351ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
352ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const std::string &Initializer);
353c9abc6528df2e50b9951fb45acedf7e3bcd4617bVikram S. Adve
35493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
35593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
35693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
357682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3588b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
359682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
360009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3619b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// isString - This method returns true if the array is an array of sbyte or
3629b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// ubyte, and if the elements of the array are all ConstantInt's.
3639b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3649b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
3659b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// getAsString - If this array is isString(), then this method converts the
3669b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// array to an std::string and returns it.  Otherwise, it asserts out.
36793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
36893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  std::string getAsString() const;
36993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
37093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getValues - Return a vector of the component constants that make up this
37193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// array.
372697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  inline const std::vector<Use> &getValues() const { return Operands; }
3735ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
37493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
37540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero arrays are always
37640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
37740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return false; }
3780eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
379e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
3806ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
3816ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
382e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
38393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
384e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
38552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
38652eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
38752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->getTypeID() == Type::ArrayTyID;
3885ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
389009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
390009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
391009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
392009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===---------------------------------------------------------------------------
393e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
394009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
395e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3966cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
3976cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
398e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3993e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
400697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
401009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
40293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
403c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
404ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
405c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  static Constant *get(const std::vector<Constant*> &V);
406009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
40793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
408c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
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; }
43052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
43152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
43252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           V->getType()->getTypeID() == Type::StructTyID;
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; }
45952eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
46052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == SimpleConstantVal &&
46152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner           isa<PointerType>(V->getType());
4625ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
4634cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
4644cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
465f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
466baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// ConstantExpr - a constant value that is initialized with an expression using
467baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// other constant values.  This is only used to represent values that cannot be
468baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// evaluated at compile-time (e.g., something derived from an address) because
469baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// it does not have a mechanism to store the actual value.  Use the appropriate
470baf850a51a253a39c60196343d756197bfbb27dbChris Lattner// Constant subclass above for known constants.
47129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve//
47229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
4736cc89aad25155ecd93b5318414851aa46351196dChris Lattner  unsigned iType;      // Operation type (an Instruction opcode)
4746cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
4756cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
4765133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
47729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
4786cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
479baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // Cast creation ctor
480baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
481baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // Binary/Shift instruction creation ctor
482e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
48346a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  // Select instruction creation ctor
48446a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ConstantExpr(Constant *C, Constant *V1, Constant *V2);
485baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // GEP instruction creation ctor
486e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
487e8e4605021141d689493132a9c7c6fce6294937fChris Lattner               const Type *DestTy);
4885133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
4895133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
4905133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
4915133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
4925133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                         Constant *C1, Constant *C2);
4935133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getShiftTy(const Type *Ty,
4945133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                              unsigned Opcode, Constant *C1, Constant *C2);
49546a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelectTy(const Type *Ty,
49646a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner                               Constant *C1, Constant *C2, Constant *C3);
4975133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
4985133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner                                      const std::vector<Constant*> &IdxList);
49929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
50029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
501fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
502baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
503baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
504baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
505e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
50693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5075133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
508fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getCast(Constant *C, const Type *Ty);
509d144f42a5aa0e9c41b0bf478ea20742c09e9a015Chris Lattner  static Constant *getSignExtend(Constant *C, const Type *Ty);
510d144f42a5aa0e9c41b0bf478ea20742c09e9a015Chris Lattner  static Constant *getZeroExtend(Constant *C, const Type *Ty);
511e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
51246a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  /// Select constant expr
51346a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ///
51446a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
51546a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner    return getSelectTy(V1->getType(), C, V1, V2);
51646a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  }
51746a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
51846a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
5197e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
5207e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
5215133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
5225133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
5235133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner    return getTy(C1->getType(), Opcode, C1, C2);
5245133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  }
525e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
5264dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// ConstantExpr::get* - Return some common constants without having to
5274dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// specify the full Instruction::OPCODE identifier.
5284dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  ///
5294dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNeg(Constant *C);
5304dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNot(Constant *C);
5314dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAdd(Constant *C1, Constant *C2);
5324dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSub(Constant *C1, Constant *C2);
5334dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getMul(Constant *C1, Constant *C2);
5344dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getDiv(Constant *C1, Constant *C2);
5354dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getRem(Constant *C1, Constant *C2);
5364dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAnd(Constant *C1, Constant *C2);
5374dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getOr(Constant *C1, Constant *C2);
5384dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getXor(Constant *C1, Constant *C2);
5394dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSetEQ(Constant *C1, Constant *C2);
5404dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSetNE(Constant *C1, Constant *C2);
5414dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSetLT(Constant *C1, Constant *C2);
5424dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSetGT(Constant *C1, Constant *C2);
5434dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSetLE(Constant *C1, Constant *C2);
5444dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSetGE(Constant *C1, Constant *C2);
5454dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getShl(Constant *C1, Constant *C2);
5464dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getShr(Constant *C1, Constant *C2);
5474dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner
54802140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner  static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
54902140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner  static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
55002140b025d821ca8cefe5086f90f4d8e0c52ae5bChris 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
566e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
5676ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
5686ac02a909256c6c0481e57c4a41e53e4007e69b2Chris Lattner                                           bool DisableChecking = false);
569e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
57017aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  /// Override methods to provide more type information...
57117aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) {
57217aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return cast<Constant>(User::getOperand(i));
57317aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
57417aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) const {
57517aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
57617aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
57717aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
57817aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
57993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
58029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
58129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
58252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == ConstantExprVal;
58329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
58429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
58529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
586d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
587d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
588009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
589