Constants.h revision e51f2a59d93336b2be46b9663af317b96a7e045b
148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
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.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
100b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// @file This file contains the declarations for the subclasses of Constant,
110b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// which represent the different flavors of constant values that live in LLVM.
120b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// Note that Constants are immutable (once created they never change) and are
130b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// fully shared by structural equivalence.  This means that two structurally
140b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// equivalent constants will always have the same address.  Constant's are
150b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// created on demand as needed and never deleted: thus clients don't have to
160b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// worry about the lifetime of the objects.
17009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
18009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
19009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2031bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#ifndef LLVM_CONSTANTS_H
2131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#define LLVM_CONSTANTS_H
22009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2331bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#include "llvm/Constant.h"
2452eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner#include "llvm/Type.h"
25532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer#include "llvm/ADT/APInt.h"
26009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
27d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
28d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
29009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
30009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
314cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
329d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass VectorType;
33009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
346cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
356cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
365133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
375133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnerstruct ConvertConstantType;
385133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
3986e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4024d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer/// This is the shared class of boolean and integer constants. This class
416b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// represents both boolean and integral constants.
426b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// @brief Class for constant integers.
436b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Shengclass ConstantInt : public Constant {
44d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *TheTrueVal, *TheFalseVal;
456b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
46532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  ConstantInt(const IntegerType *Ty, const APInt& V);
47532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  APInt Val;
48994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
49532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// Return the constant as an APInt value reference. This allows clients to
50532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// obtain a copy of the value, with all its precision in tact.
51532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @brief Return the constant's value.
52532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  inline const APInt& getValue() const {
53532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val;
54532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  }
55e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner
56e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  /// getBitWidth - Return the bitwidth of this constant.
57e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  unsigned getBitWidth() const { return Val.getBitWidth(); }
58532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
590b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit unsigned integer value after it
60532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// has been zero extended as appropriate for the type of this constant. Note
61532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// that this method can assert if the value does not fit in 64 bits.
62532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
630b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Return the zero extended value.
64a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline uint64_t getZExtValue() const {
65532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getZExtValue();
66a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
67c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner
680b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit integer value after it has been sign
69532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// sign extended as appropriate for the type of this constant. Note that
70532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// this method can assert if the value does not fit in 64 bits.
71532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
726c6b6a77c0f68d5a41e94d8ef0b1093089e683faChris Lattner  /// @brief Return the sign extended value.
73a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline int64_t getSExtValue() const {
74532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getSExtValue();
75a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
76532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
770b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// A helper method that can be used to determine if the constant contained
780b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// within is equal to a constant.  This only works for very small values,
790b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// because this is all that can be represented with all types.
800b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if this constant's value is same as an unsigned char.
81532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  bool equalsInt(uint64_t V) const {
82b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    return Val == V;
832ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
842ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
856b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// getTrue/getFalse - Return the singleton true/false values.
866b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getTrue() {
87d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheTrueVal) return TheTrueVal;
88d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(true);
896b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
906b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getFalse() {
91d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheFalseVal) return TheFalseVal;
92d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(false);
936b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
946b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
95a0098386741949065f099b940819201f73cd86c2Reid Spencer  /// Return a ConstantInt with the specified value for the specified type. The
960050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
970050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
980050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// signed value for the type Ty.
990b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Get a ConstantInt for a specific value.
1007fc44c8c5fb0efd6c909f4e306ad516df88e93e1Reid Spencer  static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
1010b5a504d105514178c80b886321221fbe5ac1131Reid Spencer
102f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// Return a ConstantInt with the specified value and an implied Type. The
103f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// type is the integer type that corresponds to the bit width of the value.
104f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  static ConstantInt *get(const APInt &V);
105f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer
106c10305743c313558405079452138f03124e87581Reid Spencer  /// getType - Specialize the getType() method to always return an IntegerType,
107c10305743c313558405079452138f03124e87581Reid Spencer  /// which reduces the amount of casting needed in parts of the compiler.
108c10305743c313558405079452138f03124e87581Reid Spencer  ///
109c10305743c313558405079452138f03124e87581Reid Spencer  inline const IntegerType *getType() const {
110c10305743c313558405079452138f03124e87581Reid Spencer    return reinterpret_cast<const IntegerType*>(Value::getType());
111c10305743c313558405079452138f03124e87581Reid Spencer  }
112c10305743c313558405079452138f03124e87581Reid Spencer
1130b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// This static method returns true if the type Ty is big enough to
1140b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// represent the value V. This can be used to avoid having the get method
1156d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// assert when V is larger than Ty can represent. Note that there are two
1166d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// versions of this method, one for unsigned and one for signed integers.
1176d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// Although ConstantInt canonicalizes everything to an unsigned integer,
1186d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// the signed version avoids callers having to convert a signed quantity
1196d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// to the appropriate unsigned type before calling the method.
1200b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if V is a valid value for type Ty
1210b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if the value is in range for the given type.
1229b11d518aef8e191e5e94f3503dfddbe1c0a387aReid Spencer  static bool isValueValidForType(const Type *Ty, uint64_t V);
123009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, int64_t V);
12493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
1256b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the "null"
1266b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that would be returned by the getNullValue method.
127b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @returns true if this is the null integer value.
1286b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is null.
1296b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  virtual bool isNullValue() const {
1306b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng    return Val == 0;
1316b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1325ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
13334da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// This is just a convenience method to make client code smaller for a
13434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common code. It also correctly performs the comparison without the
13534da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
13634da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isZero() const {
13734da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer    return Val == 0;
13834da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  }
13934da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer
14037eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// This is just a convenience method to make client code smaller for a
14134da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common case. It also correctly performs the comparison without the
14234da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
14337eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// @brief Determine if the value is one.
14434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isOne() const {
14537eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer    return Val == 1;
14637eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  }
14737eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer
1486b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff every bit in this constant is set
1496b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// to true.
1500b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this constant's bits are all set to true.
1516b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is all ones.
152579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isAllOnesValue() const {
153532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.isAllOnesValue();
1546b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1551680312867fffeb9369800949b809e0b9e29a914Chris Lattner
1566b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the largest
1576b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by the constant's type.
1580b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this is the largest value that may be represented
1590b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// by this type.
1606b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is maximal.
161579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMaxValue(bool isSigned) const {
162532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
163532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxSignedValue();
164532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
165532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxValue();
166994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
167994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
1686b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the smallest
1696b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by this constant's type.
1700b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if this is the smallest value that may be represented by
1710b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// this type.
1726b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is minimal.
173579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMinValue(bool isSigned) const {
174532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
175532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinSignedValue();
176532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
177532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinValue();
178994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
179994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
180b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// This function will return true iff this constant represents a value with
181b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// active bits bigger than 64 bits or a value greater than the given uint64_t
182b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// value.
183b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns true iff this constant is greater or equal to the given number.
184b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Determine if the value is greater or equal to the given number.
1850642f75836960afd547a6b98fad33026d0c355beZhou Sheng  bool uge(uint64_t Num) {
1860642f75836960afd547a6b98fad33026d0c355beZhou Sheng    return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
187b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
188b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
189b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns the 64-bit value of this constant if its active bits number is
190b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// not greater than 64, otherwise, just return the given uint64_t number.
191b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Get the constant's value if possible.
192b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  uint64_t getLimitedValue(uint64_t Limit) {
1934ac93f9fa3b33511f3e1e5160a226204a1912981Zhou Sheng    return (Val.getActiveBits() > 64 || Val.getZExtValue() > Limit) ?
1944ac93f9fa3b33511f3e1e5160a226204a1912981Zhou Sheng           Limit : Val.getZExtValue();
195b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
196b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
1976b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @returns the value for an integer constant of the given type that has all
1986b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// its bits set to true.
1996b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Get the all ones value
2006b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static ConstantInt *getAllOnesValue(const Type *Ty);
2016b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
202b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
203b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  static inline bool classof(const ConstantInt *) { return true; }
20452eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
205b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    return V->getValueType() == ConstantIntVal;
2065ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
207d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
208d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattnerprivate:
209d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *CreateTrueFalseVals(bool WhichOne);
210009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
211009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
212009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
21386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
21493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
21593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
216e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
217009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  double Val;
218e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
2193e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
220e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const Type *Ty, double V);
221009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
22293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
223e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantFP *get(const Type *Ty, double V);
224009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
22593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
226009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, double V);
227009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  inline double getValue() const { return Val; }
2285ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
22993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
230cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
231cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// considers -0.0 to be null as well as 0.0.  :(
2323a1eff732b36a663cdb9f07f4a9ccae6452eadcbJim Laskey  virtual bool isNullValue() const;
2330eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
234f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
235f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
236f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
237f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// two floating point values.
2383a1eff732b36a663cdb9f07f4a9ccae6452eadcbJim Laskey  bool isExactlyValue(double V) const;
239f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner
24093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
241e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
24252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
243225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    return V->getValueType() == ConstantFPVal;
2445ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
245009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
246009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
24786e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
24840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
24940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
25040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
25140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
25240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
25340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
254423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit ConstantAggregateZero(const Type *Ty)
2555181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner    : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
25640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
25740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// get() - static factory method for creating a null aggregate.  It is
25840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// illegal to call this method with a non-aggregate type.
25940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static Constant *get(const Type *Ty);
26040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
26140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
26240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.
26340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return true; }
26440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
26540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void destroyConstant();
26640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
26740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
26840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
26952eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const ConstantAggregateZero *) { return true; }
27052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
27152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == ConstantAggregateZeroVal;
27240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
27340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
27440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
275009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
27686e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
27793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
27893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
279e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
2806cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
2816cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
282e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
2833e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
284697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
2855181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner  ~ConstantArray();
286009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
28793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
288ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
289df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const ArrayType *T,
290df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       Constant*const*Vals, unsigned NumVals) {
291df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
292df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
293df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
294efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer
295efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer  /// This method constructs a ConstantArray and initializes it with a text
296461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// string. The default behavior (AddNull==true) causes a null terminator to
297461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// be placed at the end of the array. This effectively increases the length
298461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// of the array by one (you've been warned).  However, in some situations
299461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// this is not desired so if AddNull==false then the string is copied without
300461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// null termination.
301461bed2b753c514ac15ff824befe585988408f45Reid Spencer  static Constant *get(const std::string &Initializer, bool AddNull = true);
3029769ab22265b313171d201b5928688524a01bd87Misha Brukman
30393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
30493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
30593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
306682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3078b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
308682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
309009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3109b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// isString - This method returns true if the array is an array of sbyte or
3119b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// ubyte, and if the elements of the array are all ConstantInt's.
3129b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3139b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
31422c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isCString - This method returns true if the array is a string (see
31522c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isString) and it ends in a null byte \0 and does not contains any other
31622c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// null bytes except its terminator.
31722c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  bool isCString() const;
31822c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng
3199b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// getAsString - If this array is isString(), then this method converts the
3209b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// array to an std::string and returns it.  Otherwise, it asserts out.
32193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
32293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  std::string getAsString() const;
32393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
32493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
32540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero arrays are always
32640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
32740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return false; }
3280eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
329e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
33040cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
331e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
33293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
333e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
33452eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
335225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    return V->getValueType() == ConstantArrayVal;
3365ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
337009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
338009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
339009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
34086e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
341e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
342009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
343e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3446cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
3456cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
346e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3473e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
348697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
3495181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner  ~ConstantStruct();
350009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
35193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
352c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
353ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
354df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
355df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals,
356df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       bool Packed = false) {
357df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
358df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
359df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
360df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
36193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
362c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
363682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const StructType *getType() const {
3648b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const StructType*>(Value::getType());
365682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
366009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
36793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
36840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero structs are always
36940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
370a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
37140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner    return false;
372a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
3730eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
374e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
37540cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
3769769ab22265b313171d201b5928688524a01bd87Misha Brukman
37793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
378e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
37952eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
380225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    return V->getValueType() == ConstantStructVal;
3815ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
382009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
383009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
38486e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
3859d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer/// ConstantVector - Constant Vector Declarations
386715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke///
3879d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass ConstantVector : public Constant {
3889d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  friend struct ConstantCreator<ConstantVector, VectorType,
389715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke                                    std::vector<Constant*> >;
3909d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
391715c90ba524e736190a6380695ab337eeb5148beBrian Gaekeprotected:
3929d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
3939d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ~ConstantVector();
394715c90ba524e736190a6380695ab337eeb5148beBrian Gaekepublic:
395715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// get() - Static factory methods - Return objects of the specified value
3969d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static Constant *get(const VectorType *T, const std::vector<Constant*> &);
397715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static Constant *get(const std::vector<Constant*> &V);
398df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals) {
399df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
400df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals));
401df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
402df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
4039d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  /// getType - Specialize the getType() method to always return an VectorType,
404715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// which reduces the amount of casting needed in parts of the compiler.
405715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  ///
4069d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  inline const VectorType *getType() const {
4079d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer    return reinterpret_cast<const VectorType*>(Value::getType());
408715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
409715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
41058513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// @returns the value for an packed integer constant of the given type that
41158513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// has all its bits set to true.
41258513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// @brief Get the all ones value
4139d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static ConstantVector *getAllOnesValue(const VectorType *Ty);
41458513aa1c22a08118734ac799d935ea2910db35aChris Lattner
415715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// isNullValue - Return true if this is the value that would be returned by
416715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// getNullValue.  This always returns false because zero arrays are always
417715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// created as ConstantAggregateZero objects.
418715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual bool isNullValue() const { return false; }
419715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
420fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// This function will return true iff every element in this packed constant
421fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// is set to all ones.
422fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @returns true iff this constant's emements are all set to all ones.
423fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @brief Determine if the value is all ones.
424569cc890e8822c265cec66745791edbd510dac1cJim Laskey  bool isAllOnesValue() const;
425fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey
426715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual void destroyConstant();
42740cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
428715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
429715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// Methods for support type inquiry through isa, cast, and dyn_cast:
4309d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static inline bool classof(const ConstantVector *) { return true; }
431715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static bool classof(const Value *V) {
4329d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer    return V->getValueType() == ConstantVectorVal;
433715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
434715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke};
435715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
43686e100bd9bf1a1a838902c46de926c79c68cdde3Chris 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:
443423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit ConstantPointerNull(const PointerType *T)
4445181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner    : Constant(reinterpret_cast<const Type*>(T),
445225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner               Value::ConstantPointerNullVal, 0, 0) {}
4466cc89aad25155ecd93b5318414851aa46351196dChris Lattner
4475ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
4485ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
44993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
450e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerNull *get(const PointerType *T);
4515ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
45293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
45393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
4540eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return true; }
4550eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
456e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
457e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
4580f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// getType - Specialize the getType() method to always return an PointerType,
4590f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
4600f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  ///
4610f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  inline const PointerType *getType() const {
4620f42ba67771627453187e44fea935d6bab7a8152Chris Lattner    return reinterpret_cast<const PointerType*>(Value::getType());
4630f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  }
4640f42ba67771627453187e44fea935d6bab7a8152Chris Lattner
46593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
466e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
46752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
468225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    return V->getValueType() == ConstantPointerNullVal;
4695ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
4704cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
4714cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
472f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
47386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner/// ConstantExpr - a constant value that is initialized with an expression using
474eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// other constant values.
47586e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner///
476eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// This class uses the standard Instruction opcodes to define the various
477eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// constant expressions.  The Opcode field for the ConstantExpr class is
478eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// maintained in the Value::SubclassData field.
47929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
4806cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
4816cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
4825133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
4839769ab22265b313171d201b5928688524a01bd87Misha Brukman
4846cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
4855181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner  ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
486eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    : Constant(Ty, ConstantExprVal, Ops, NumOps) {
487eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    // Operation type (an Instruction opcode) is stored as the SubclassData.
4889769ab22265b313171d201b5928688524a01bd87Misha Brukman    SubclassData = Opcode;
489eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  }
4905133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
4915133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
4925133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
4935133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
49490fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer                         Constant *C1, Constant *C2);
495e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompareTy(unsigned short pred, Constant *C1,
496e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer                                Constant *C2);
49746a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelectTy(const Type *Ty,
49846a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner                               Constant *C1, Constant *C2, Constant *C3);
4995133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
500fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                      Value* const *Idxs, unsigned NumIdxs);
50149b78a569609881811d905960baa7dd1ab801383Robert Bocchino  static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
50249b78a569609881811d905960baa7dd1ab801383Robert Bocchino                                       Constant *Idx);
503f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino  static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
504f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino                                      Constant *Elt, Constant *Idx);
5059fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
5069fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner                                      Constant *V2, Constant *Mask);
5079769ab22265b313171d201b5928688524a01bd87Misha Brukman
50829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
509fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
510baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
511baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
512baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
5139769ab22265b313171d201b5928688524a01bd87Misha Brukman
51493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5155133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
516d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getTrunc   (Constant *C, const Type *Ty);
517d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSExt    (Constant *C, const Type *Ty);
518d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getZExt    (Constant *C, const Type *Ty);
519d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPTrunc (Constant *C, const Type *Ty);
520d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPExtend(Constant *C, const Type *Ty);
521d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getUIToFP  (Constant *C, const Type *Ty);
522d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSIToFP  (Constant *C, const Type *Ty);
523d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToUI  (Constant *C, const Type *Ty);
524d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToSI  (Constant *C, const Type *Ty);
525d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getPtrToInt(Constant *C, const Type *Ty);
526d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getIntToPtr(Constant *C, const Type *Ty);
527d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getBitCast (Constant *C, const Type *Ty);
5283da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
5293da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // @brief Convenience function for getting one of the casting operations
5303da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // using a CastOps opcode.
5313da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  static Constant *getCast(
5323da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    unsigned ops,  ///< The opcode for the conversion
5333da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    Constant *C,   ///< The constant to be converted
5343da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    const Type *Ty ///< The type to which the constant is converted
5353da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  );
5363da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
537848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a ZExt or BitCast cast constant expression
538848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getZExtOrBitCast(
539848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    Constant *C,   ///< The constant to zext or bitcast
540848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    const Type *Ty ///< The type to zext or bitcast C to
541848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
542848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
543848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a SExt or BitCast cast constant expression
544848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getSExtOrBitCast(
54590fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to sext or bitcast
54690fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to sext or bitcast C to
547848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
548848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
549848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a Trunc or BitCast cast constant expression
550848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getTruncOrBitCast(
55190fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to trunc or bitcast
55290fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to trunc or bitcast C to
553848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
554848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
555887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Create a BitCast or a PtrToInt cast constant expression
556887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  static Constant *getPointerCast(
557887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    Constant *C,   ///< The pointer value to be casted (operand 0)
558887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    const Type *Ty ///< The type to which cast should be made
559887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  );
560887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer
56184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
56284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getIntegerCast(
56384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
56484f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty, ///< The integer type to cast to
56584f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    bool isSigned   ///< Whether C should be treated as signed or not
56684f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
56784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
56884f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
56984f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getFPCast(
57084f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
57184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty ///< The integer type to cast to
57284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
57384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
5743da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  /// @brief Return true if this is a convert constant expression
5753da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  bool isCast() const;
5763da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
5774b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  /// @brief Return true if this is a compare constant expression
5784b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  bool isCompare() const;
5794b94f49091be8e26ce6e49db1268458995299b82Reid Spencer
58046a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  /// Select constant expr
58146a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ///
58246a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
58346a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner    return getSelectTy(V1->getType(), C, V1, V2);
58446a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  }
58546a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
5867be2a120659cc61e554b04815ed3f1d4f234ecafAlkis Evlogimenos  /// getSizeOf constant expr - computes the size of a type in a target
587b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// independent way (Note: the return type is a ULong).
58860ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  ///
58960ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  static Constant *getSizeOf(const Type *Ty);
59046a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
5917e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
5927e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
5935133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
59490fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
59590fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer
596887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Return an ICmp or FCmp comparison operator constant expression.
597e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
598e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
5994dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// ConstantExpr::get* - Return some common constants without having to
6004dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// specify the full Instruction::OPCODE identifier.
6014dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  ///
6024dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNeg(Constant *C);
6034dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNot(Constant *C);
6044dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAdd(Constant *C1, Constant *C2);
6054dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSub(Constant *C1, Constant *C2);
6064dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getMul(Constant *C1, Constant *C2);
6071628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getUDiv(Constant *C1, Constant *C2);
6081628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getSDiv(Constant *C1, Constant *C2);
6091628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getFDiv(Constant *C1, Constant *C2);
6100a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
6110a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
6120a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getFRem(Constant *C1, Constant *C2);
6134dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAnd(Constant *C1, Constant *C2);
6144dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getOr(Constant *C1, Constant *C2);
6154dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getXor(Constant *C1, Constant *C2);
616728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  static Constant* getICmp(unsigned short pred, Constant* LHS, Constant* RHS);
617728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  static Constant* getFCmp(unsigned short pred, Constant* LHS, Constant* RHS);
6184dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getShl(Constant *C1, Constant *C2);
6193822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getLShr(Constant *C1, Constant *C2);
6203822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getAShr(Constant *C1, Constant *C2);
62102140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner
6227fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
6237fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// all elements must be Constant's.
6245133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
625fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getGetElementPtr(Constant *C,
626fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Constant* const *IdxList, unsigned NumIdx);
6277fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  static Constant *getGetElementPtr(Constant *C,
628fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Value* const *IdxList, unsigned NumIdx);
629df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
6309fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getExtractElement(Constant *Vec, Constant *Idx);
6319fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
6329fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
633728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
63424d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
63524d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// method returns the negative zero constant for floating point or packed
63624d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// floating point types; for all other types, it returns the null value.
63724d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  static Constant *getZeroValueForNegationExpr(const Type *Ty);
63824d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer
63993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
64093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
64129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isNullValue() const { return false; }
6429769ab22265b313171d201b5928688524a01bd87Misha Brukman
64393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
644eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  unsigned getOpcode() const { return SubclassData; }
64529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
646728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
647728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// not an ICMP or FCMP constant expression.
648728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  unsigned getPredicate() const;
649728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
65093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
651e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
6529769ab22265b313171d201b5928688524a01bd87Misha Brukman
65379ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// getWithOperandReplaced - Return a constant expression identical to this
65479ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// one, but with the specified operand set to the specified value.
65579ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
65679ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
6576b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// getWithOperands - This returns the current constant expression with the
6586b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// operands replaced with the specified values.  The specified operands must
6596b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// match count and type with the existing ones.
6606b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
66179ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
662e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
66340cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
6649769ab22265b313171d201b5928688524a01bd87Misha Brukman
66517aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  /// Override methods to provide more type information...
6669769ab22265b313171d201b5928688524a01bd87Misha Brukman  inline Constant *getOperand(unsigned i) {
66717aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return cast<Constant>(User::getOperand(i));
66817aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
66917aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  inline Constant *getOperand(unsigned i) const {
67017aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner    return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
67117aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner  }
6729769ab22265b313171d201b5928688524a01bd87Misha Brukman
67317aefb168fb2cfffcbd7131d1529a90ae1831b1cChris Lattner
67493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
67529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
67629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
67752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner    return V->getValueType() == ConstantExprVal;
67829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
67929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
68029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
681e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
682e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner//===----------------------------------------------------------------------===//
683e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// UndefValue - 'undef' values are things that do not have specified contents.
684e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// These are used for a variety of purposes, including global variable
685e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// initializers and operands to instructions.  'undef' values can occur with
686e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// any type.
687e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner///
688e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerclass UndefValue : public Constant {
689e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  friend struct ConstantCreator<UndefValue, Type, char>;
690e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
691e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerprotected:
692423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
693e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerpublic:
694e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// get() - Static factory methods - Return an 'undef' object of the specified
695e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// type.
696e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  ///
697e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static UndefValue *get(const Type *T);
698e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
699e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
700e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// getNullValue.
701e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual bool isNullValue() const { return false; }
702e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
703e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual void destroyConstant();
704e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
705e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
706e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static inline bool classof(const UndefValue *) { return true; }
707e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static bool classof(const Value *V) {
708e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner    return V->getValueType() == UndefValueVal;
709e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  }
710e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner};
711e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
712d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
713d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
714009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
715