Constants.h revision 0f8b53f19d29013ab18f3d444cea1e6305405611
148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
100f8b53f19d29013ab18f3d444cea1e6305405611Dan Gohman/// @file
110f8b53f19d29013ab18f3d444cea1e6305405611Dan Gohman/// This file contains the declarations for the subclasses of Constant,
120b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// which represent the different flavors of constant values that live in LLVM.
130b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// Note that Constants are immutable (once created they never change) and are
140b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// fully shared by structural equivalence.  This means that two structurally
150b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// equivalent constants will always have the same address.  Constant's are
160b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// created on demand as needed and never deleted: thus clients don't have to
170b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// worry about the lifetime of the objects.
18009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
19009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
20009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#ifndef LLVM_CONSTANTS_H
2231bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#define LLVM_CONSTANTS_H
23009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2431bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#include "llvm/Constant.h"
2552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner#include "llvm/Type.h"
26efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif#include "llvm/OperandTraits.h"
27532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer#include "llvm/ADT/APInt.h"
28343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen#include "llvm/ADT/APFloat.h"
2981a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman#include "llvm/ADT/SmallVector.h"
30009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
31d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
32d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
33009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
34009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
354cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
369d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass VectorType;
37009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
386cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
396cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
405133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
415133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnerstruct ConvertConstantType;
425133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
4386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4424d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer/// This is the shared class of boolean and integer constants. This class
456b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// represents both boolean and integral constants.
466b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// @brief Class for constant integers.
476b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Shengclass ConstantInt : public Constant {
48d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *TheTrueVal, *TheFalseVal;
49051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
506b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
51532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  ConstantInt(const IntegerType *Ty, const APInt& V);
52532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  APInt Val;
53051a950000e21935165db56695e35bade668193bGabor Greifprotected:
54051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
55051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
56051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
57051a950000e21935165db56695e35bade668193bGabor Greif  }
58994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
59532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// Return the constant as an APInt value reference. This allows clients to
60532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// obtain a copy of the value, with all its precision in tact.
61532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @brief Return the constant's value.
62532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  inline const APInt& getValue() const {
63532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val;
64532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  }
65e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner
66e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  /// getBitWidth - Return the bitwidth of this constant.
67e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  unsigned getBitWidth() const { return Val.getBitWidth(); }
68532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
690b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit unsigned integer value after it
70532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// has been zero extended as appropriate for the type of this constant. Note
71532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// that this method can assert if the value does not fit in 64 bits.
72532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
730b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Return the zero extended value.
74a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline uint64_t getZExtValue() const {
75532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getZExtValue();
76a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
77c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner
780b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit integer value after it has been sign
797b048b391f9ded004170a67780d1c6a5eedeb604Bob Wilson  /// extended as appropriate for the type of this constant. Note that
80532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// this method can assert if the value does not fit in 64 bits.
81532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
826c6b6a77c0f68d5a41e94d8ef0b1093089e683faChris Lattner  /// @brief Return the sign extended value.
83a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline int64_t getSExtValue() const {
84532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getSExtValue();
85a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
86532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
870b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// A helper method that can be used to determine if the constant contained
880b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// within is equal to a constant.  This only works for very small values,
890b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// because this is all that can be represented with all types.
900b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if this constant's value is same as an unsigned char.
91532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  bool equalsInt(uint64_t V) const {
92b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    return Val == V;
932ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
942ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
956b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// getTrue/getFalse - Return the singleton true/false values.
966b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getTrue() {
97d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheTrueVal) return TheTrueVal;
98d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(true);
996b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1006b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getFalse() {
101d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheFalseVal) return TheFalseVal;
102d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(false);
1036b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1046b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
105a0098386741949065f099b940819201f73cd86c2Reid Spencer  /// Return a ConstantInt with the specified value for the specified type. The
1060050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
1070050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
1080050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// signed value for the type Ty.
1090b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Get a ConstantInt for a specific value.
1107fc44c8c5fb0efd6c909f4e306ad516df88e93e1Reid Spencer  static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
1110b5a504d105514178c80b886321221fbe5ac1131Reid Spencer
112f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// Return a ConstantInt with the specified value and an implied Type. The
113f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// type is the integer type that corresponds to the bit width of the value.
114f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  static ConstantInt *get(const APInt &V);
115f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer
116c10305743c313558405079452138f03124e87581Reid Spencer  /// getType - Specialize the getType() method to always return an IntegerType,
117c10305743c313558405079452138f03124e87581Reid Spencer  /// which reduces the amount of casting needed in parts of the compiler.
118c10305743c313558405079452138f03124e87581Reid Spencer  ///
119c10305743c313558405079452138f03124e87581Reid Spencer  inline const IntegerType *getType() const {
120c10305743c313558405079452138f03124e87581Reid Spencer    return reinterpret_cast<const IntegerType*>(Value::getType());
121c10305743c313558405079452138f03124e87581Reid Spencer  }
122c10305743c313558405079452138f03124e87581Reid Spencer
1230b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// This static method returns true if the type Ty is big enough to
1240b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// represent the value V. This can be used to avoid having the get method
1256d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// assert when V is larger than Ty can represent. Note that there are two
1266d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// versions of this method, one for unsigned and one for signed integers.
1276d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// Although ConstantInt canonicalizes everything to an unsigned integer,
1286d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// the signed version avoids callers having to convert a signed quantity
1296d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// to the appropriate unsigned type before calling the method.
1300b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if V is a valid value for type Ty
1310b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if the value is in range for the given type.
1329b11d518aef8e191e5e94f3503dfddbe1c0a387aReid Spencer  static bool isValueValidForType(const Type *Ty, uint64_t V);
133009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, int64_t V);
13493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
1356b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the "null"
1366b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that would be returned by the getNullValue method.
137b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @returns true if this is the null integer value.
1386b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is null.
1396b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  virtual bool isNullValue() const {
1406b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng    return Val == 0;
1416b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1425ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
14334da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// This is just a convenience method to make client code smaller for a
14434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common code. It also correctly performs the comparison without the
14534da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
14634da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isZero() const {
14734da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer    return Val == 0;
14834da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  }
14934da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer
15037eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// This is just a convenience method to make client code smaller for a
15134da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common case. It also correctly performs the comparison without the
15234da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
15337eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// @brief Determine if the value is one.
15434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isOne() const {
15537eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer    return Val == 1;
15637eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  }
15737eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer
1586b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff every bit in this constant is set
1596b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// to true.
1600b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this constant's bits are all set to true.
1616b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is all ones.
162579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isAllOnesValue() const {
163532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.isAllOnesValue();
1646b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1651680312867fffeb9369800949b809e0b9e29a914Chris Lattner
1666b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the largest
1676b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by the constant's type.
1680b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this is the largest value that may be represented
1690b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// by this type.
1706b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is maximal.
171579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMaxValue(bool isSigned) const {
172532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
173532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxSignedValue();
174532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
175532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxValue();
176994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
177994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
1786b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the smallest
1796b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by this constant's type.
1800b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if this is the smallest value that may be represented by
1810b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// this type.
1826b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is minimal.
183579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMinValue(bool isSigned) const {
184532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
185532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinSignedValue();
186532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
187532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinValue();
188994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
189994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
190b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// This function will return true iff this constant represents a value with
191b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// active bits bigger than 64 bits or a value greater than the given uint64_t
192b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// value.
193b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns true iff this constant is greater or equal to the given number.
194b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Determine if the value is greater or equal to the given number.
1950642f75836960afd547a6b98fad33026d0c355beZhou Sheng  bool uge(uint64_t Num) {
1960642f75836960afd547a6b98fad33026d0c355beZhou Sheng    return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
197b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
198b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
199edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// getLimitedValue - If the value is smaller than the specified limit,
200edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// return it, otherwise return the limit value.  This causes the value
201edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// to saturate to the limit.
202edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// @returns the min of the value of the constant and the specified value
203edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// @brief Get the constant's value with a saturation limit
20499b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
20599b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner    return Val.getLimitedValue(Limit);
206b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
207b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
2086b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @returns the value for an integer constant of the given type that has all
2096b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// its bits set to true.
2106b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Get the all ones value
2116b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static ConstantInt *getAllOnesValue(const Type *Ty);
2126b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
213b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
214b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  static inline bool classof(const ConstantInt *) { return true; }
21552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
216a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantIntVal;
2175ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
218d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
219d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattnerprivate:
220d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *CreateTrueFalseVals(bool WhichOne);
221009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
222009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
223009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
22486e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
22593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
22693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
227e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
228343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen  APFloat Val;
229051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
230e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
2313e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
232f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  ConstantFP(const Type *Ty, const APFloat& V);
233051a950000e21935165db56695e35bade668193bGabor Greifprotected:
234051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
235051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
236051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
237051a950000e21935165db56695e35bade668193bGabor Greif  }
238009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
23993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
2404592230265384bebc2879303310f89dee4d9d7a8Chris Lattner  static ConstantFP *get(const APFloat &V);
241009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
242a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// get() - This returns a constant fp for the specified value in the
243a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// specified type.  This should only be used for simple constant values like
244a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
245d6d48c4e5b60aa4acc02c7a8700cb79949503e93Chris Lattner  static ConstantFP *get(const Type *Ty, double V);
246a541931ddb43191daea4217faa4762934d8bc169Evan Cheng
24793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
248f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  static bool isValueValidForType(const Type *Ty, const APFloat& V);
249f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  inline const APFloat& getValueAPF() const { return Val; }
2505ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
25193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
252cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
253cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// considers -0.0 to be null as well as 0.0.  :(
2543a1eff732b36a663cdb9f07f4a9ccae6452eadcbJim Laskey  virtual bool isNullValue() const;
2550eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
2569e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  // Get a negative zero.
2579e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  static ConstantFP *getNegativeZero(const Type* Ty);
2589e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen
259f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
260f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
261f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
26243421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// two floating point values.  The version with a double operand is retained
26343421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// because it's so convenient to write isExactlyValue(2.0), but please use
264def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner  /// it only for simple constants.
265f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(const APFloat& V) const;
26643421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen
267f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(double V) const {
2688aaec36fa760c9dd50627e397ca85798d9eb6e79Dale Johannesen    bool ignored;
269beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    // convert is not supported on this type
270beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
271beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen      return false;
272def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    APFloat FV(V);
2738aaec36fa760c9dd50627e397ca85798d9eb6e79Dale Johannesen    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
274def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    return isExactlyValue(FV);
275f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  }
27693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
277e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
27852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
279a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantFPVal;
2805ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
281009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
282009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
28386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
28440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
28540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
28640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
28740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
288051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
28940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
29040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
29113d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  explicit ConstantAggregateZero(const Type *ty)
29213d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
293051a950000e21935165db56695e35bade668193bGabor Greifprotected:
294051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
295051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
296051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
297051a950000e21935165db56695e35bade668193bGabor Greif  }
29840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
29940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// get() - static factory method for creating a null aggregate.  It is
30040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// illegal to call this method with a non-aggregate type.
301149cfc35197229d229c5737046210b5ade06e557Dan Gohman  static ConstantAggregateZero *get(const Type *Ty);
30240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
30440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.
30540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return true; }
30640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void destroyConstant();
30840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
31040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
31152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const ConstantAggregateZero *) { return true; }
31252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
313a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantAggregateZeroVal;
31440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
31540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
31640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
317009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
31886e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
31993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
32093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
321e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3226cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
3236cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
324e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3253e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
326697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
327009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
32893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
329ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
330df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const ArrayType *T,
331df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       Constant*const*Vals, unsigned NumVals) {
332df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
333df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
334df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
335efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer
336efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer  /// This method constructs a ConstantArray and initializes it with a text
337461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// string. The default behavior (AddNull==true) causes a null terminator to
338461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// be placed at the end of the array. This effectively increases the length
339461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// of the array by one (you've been warned).  However, in some situations
340461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// this is not desired so if AddNull==false then the string is copied without
341461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// null termination.
342461bed2b753c514ac15ff824befe585988408f45Reid Spencer  static Constant *get(const std::string &Initializer, bool AddNull = true);
3439769ab22265b313171d201b5928688524a01bd87Misha Brukman
344efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
345efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
346efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
34793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
34893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
34993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
350682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3518b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
352682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
353009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3546eabd74d1a236effc6757e7c81182fa1ecd52ac6Nick Lewycky  /// isString - This method returns true if the array is an array of i8 and
3556eabd74d1a236effc6757e7c81182fa1ecd52ac6Nick Lewycky  /// the elements of the array are all ConstantInt's.
3569b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3579b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
35822c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isCString - This method returns true if the array is a string (see
359181b6c9cb5def44658d15848e34c5c45d973f065Reid Spencer  /// @verbatim
36022c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isString) and it ends in a null byte \0 and does not contains any other
361181b6c9cb5def44658d15848e34c5c45d973f065Reid Spencer  /// @endverbatim
36222c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// null bytes except its terminator.
36322c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  bool isCString() const;
36422c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng
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  /// isNullValue - Return true if this is the value that would be returned by
37140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero arrays are always
37240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
37340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return false; }
3740eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
375e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
37640cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
377e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
37893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
379e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
38052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
381a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantArrayVal;
3825ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
383009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
384009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
385efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
386efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
387efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
388efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
389efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
390009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
39186e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
392e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
393009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
394e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3956cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
3966cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
397e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3983e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
399697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
400009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
40193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
402c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
403ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
404df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
405df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals,
406df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       bool Packed = false) {
407df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
408df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
409df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
410df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
411efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
412efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
413efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
41493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
415c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
416682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const StructType *getType() const {
4178b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const StructType*>(Value::getType());
418682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
419009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
42093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
42140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero structs are always
42240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
423a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
42440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner    return false;
425a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
4260eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
427e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
42840cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
4299769ab22265b313171d201b5928688524a01bd87Misha Brukman
43093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
431e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
43252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
433a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantStructVal;
4345ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
435009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
436009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
437efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
438efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
439efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
440efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
441efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
442efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
44386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4449d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer/// ConstantVector - Constant Vector Declarations
445715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke///
4469d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass ConstantVector : public Constant {
4479d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  friend struct ConstantCreator<ConstantVector, VectorType,
448715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke                                    std::vector<Constant*> >;
4499d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
450715c90ba524e736190a6380695ab337eeb5148beBrian Gaekeprotected:
4519d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
452715c90ba524e736190a6380695ab337eeb5148beBrian Gaekepublic:
453715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// get() - Static factory methods - Return objects of the specified value
4549d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static Constant *get(const VectorType *T, const std::vector<Constant*> &);
455715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static Constant *get(const std::vector<Constant*> &V);
456df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals) {
457df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
458df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals));
459df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
460df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
461efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
462efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
463efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
464fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// getType - Specialize the getType() method to always return a VectorType,
465715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// which reduces the amount of casting needed in parts of the compiler.
466715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  ///
4679d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  inline const VectorType *getType() const {
4689d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer    return reinterpret_cast<const VectorType*>(Value::getType());
469715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
470715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
471fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// @returns the value for a vector integer constant of the given type that
47258513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// has all its bits set to true.
47358513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// @brief Get the all ones value
4749d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static ConstantVector *getAllOnesValue(const VectorType *Ty);
47558513aa1c22a08118734ac799d935ea2910db35aChris Lattner
476715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// isNullValue - Return true if this is the value that would be returned by
47707a96765daedf180a7102d39fe56c499878312b7Dan Gohman  /// getNullValue.  This always returns false because zero vectors are always
478715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// created as ConstantAggregateZero objects.
479715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual bool isNullValue() const { return false; }
480715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
481fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// This function will return true iff every element in this vector constant
482fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// is set to all ones.
483fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @returns true iff this constant's emements are all set to all ones.
484fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @brief Determine if the value is all ones.
485569cc890e8822c265cec66745791edbd510dac1cJim Laskey  bool isAllOnesValue() const;
486fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey
4873b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// getSplatValue - If this is a splat constant, meaning that all of the
4883b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// elements have the same value, return that value. Otherwise return NULL.
4893b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  Constant *getSplatValue();
4903b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman
491715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual void destroyConstant();
49240cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
493715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
494715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// Methods for support type inquiry through isa, cast, and dyn_cast:
4959d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static inline bool classof(const ConstantVector *) { return true; }
496715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static bool classof(const Value *V) {
497a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantVectorVal;
498715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
499715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke};
500715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
501efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
502efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
503efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
504efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
505efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
506efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
50786e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
50893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerNull - a constant pointer value that points to null
50993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
51048babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerNull : public Constant {
5116cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
512051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
513e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
5145ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerprotected:
515423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit ConstantPointerNull(const PointerType *T)
5165181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner    : Constant(reinterpret_cast<const Type*>(T),
517225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner               Value::ConstantPointerNullVal, 0, 0) {}
518afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen
519051a950000e21935165db56695e35bade668193bGabor Greifprotected:
520051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
521051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
522051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
523051a950000e21935165db56695e35bade668193bGabor Greif  }
5245ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
52593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
526e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerNull *get(const PointerType *T);
5275ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
52893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
52993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
5300eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return true; }
5310eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
532e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
533e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
5340f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// getType - Specialize the getType() method to always return an PointerType,
5350f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
5360f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  ///
5370f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  inline const PointerType *getType() const {
5380f42ba67771627453187e44fea935d6bab7a8152Chris Lattner    return reinterpret_cast<const PointerType*>(Value::getType());
5390f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  }
5400f42ba67771627453187e44fea935d6bab7a8152Chris Lattner
54193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
542e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
54352eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
544a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantPointerNullVal;
5455ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
5464cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
5474cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
548f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
54986e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner/// ConstantExpr - a constant value that is initialized with an expression using
550eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// other constant values.
55186e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner///
552eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// This class uses the standard Instruction opcodes to define the various
553eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// constant expressions.  The Opcode field for the ConstantExpr class is
554eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// maintained in the Value::SubclassData field.
55529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
5566cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
5576cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
5585133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
5599769ab22265b313171d201b5928688524a01bd87Misha Brukman
5606cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
56113d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
56213d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : Constant(ty, ConstantExprVal, Ops, NumOps) {
563eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    // Operation type (an Instruction opcode) is stored as the SubclassData.
5649769ab22265b313171d201b5928688524a01bd87Misha Brukman    SubclassData = Opcode;
565eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  }
5665133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
5675133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
5685133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
5695133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
57090fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer                         Constant *C1, Constant *C2);
571ff795a80a35dc99a1971646de11f088e71d0a2c6Nate Begeman  static Constant *getCompareTy(unsigned short pred, Constant *C1,
572ff795a80a35dc99a1971646de11f088e71d0a2c6Nate Begeman                                Constant *C2);
57346a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelectTy(const Type *Ty,
57446a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner                               Constant *C1, Constant *C2, Constant *C3);
5755133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
576fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                      Value* const *Idxs, unsigned NumIdxs);
57749b78a569609881811d905960baa7dd1ab801383Robert Bocchino  static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
57849b78a569609881811d905960baa7dd1ab801383Robert Bocchino                                       Constant *Idx);
579f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino  static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
580f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino                                      Constant *Elt, Constant *Idx);
5819fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
5829fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner                                      Constant *V2, Constant *Mask);
583041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
58481a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                     const unsigned *Idxs, unsigned NumIdxs);
585041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
586041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                    Constant *Val,
58781a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                    const unsigned *Idxs, unsigned NumIdxs);
5889769ab22265b313171d201b5928688524a01bd87Misha Brukman
58929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
590fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
591baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
592baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
593baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
5949769ab22265b313171d201b5928688524a01bd87Misha Brukman
59593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5965133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
597d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getTrunc   (Constant *C, const Type *Ty);
598d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSExt    (Constant *C, const Type *Ty);
599d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getZExt    (Constant *C, const Type *Ty);
600d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPTrunc (Constant *C, const Type *Ty);
601d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPExtend(Constant *C, const Type *Ty);
602d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getUIToFP  (Constant *C, const Type *Ty);
603d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSIToFP  (Constant *C, const Type *Ty);
604d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToUI  (Constant *C, const Type *Ty);
605d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToSI  (Constant *C, const Type *Ty);
606d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getPtrToInt(Constant *C, const Type *Ty);
607d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getIntToPtr(Constant *C, const Type *Ty);
608d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getBitCast (Constant *C, const Type *Ty);
6093da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
610efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
611efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
612efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
6133da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // @brief Convenience function for getting one of the casting operations
6143da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // using a CastOps opcode.
6153da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  static Constant *getCast(
6163da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    unsigned ops,  ///< The opcode for the conversion
6173da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    Constant *C,   ///< The constant to be converted
6183da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    const Type *Ty ///< The type to which the constant is converted
6193da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  );
6203da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
621848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a ZExt or BitCast cast constant expression
622848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getZExtOrBitCast(
623848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    Constant *C,   ///< The constant to zext or bitcast
624848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    const Type *Ty ///< The type to zext or bitcast C to
625848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
626848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
627848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a SExt or BitCast cast constant expression
628848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getSExtOrBitCast(
62990fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to sext or bitcast
63090fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to sext or bitcast C to
631848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
632848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
633848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a Trunc or BitCast cast constant expression
634848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getTruncOrBitCast(
63590fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to trunc or bitcast
63690fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to trunc or bitcast C to
637848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
638848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
639887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Create a BitCast or a PtrToInt cast constant expression
640887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  static Constant *getPointerCast(
641887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    Constant *C,   ///< The pointer value to be casted (operand 0)
642887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    const Type *Ty ///< The type to which cast should be made
643887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  );
644887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer
64584f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
64684f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getIntegerCast(
64784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
64884f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty, ///< The integer type to cast to
64984f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    bool isSigned   ///< Whether C should be treated as signed or not
65084f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
65184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
65284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
65384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getFPCast(
65484f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
65584f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty ///< The integer type to cast to
65684f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
65784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
6583da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  /// @brief Return true if this is a convert constant expression
6593da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  bool isCast() const;
6603da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
6614b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  /// @brief Return true if this is a compare constant expression
6624b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  bool isCompare() const;
6634b94f49091be8e26ce6e49db1268458995299b82Reid Spencer
66481a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// @brief Return true if this is an insertvalue or extractvalue expression,
66581a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// and the getIndices() method may be used.
66681a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  bool hasIndices() const;
66781a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman
66846a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  /// Select constant expr
66946a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ///
67046a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
67146a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner    return getSelectTy(V1->getType(), C, V1, V2);
67246a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  }
67346a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
6747be2a120659cc61e554b04815ed3f1d4f234ecafAlkis Evlogimenos  /// getSizeOf constant expr - computes the size of a type in a target
6754647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  /// independent way (Note: the return type is an i64).
67660ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  ///
67760ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  static Constant *getSizeOf(const Type *Ty);
67846a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
6797e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
6807e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
6815133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
68290fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
68390fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer
684b5557abcf13a7b375cae683e9ec200d499645d02Nate Begeman  /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
685b5557abcf13a7b375cae683e9ec200d499645d02Nate Begeman  /// expression.
686e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
687e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
6884dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// ConstantExpr::get* - Return some common constants without having to
6894dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// specify the full Instruction::OPCODE identifier.
6904dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  ///
6914dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNeg(Constant *C);
6924dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNot(Constant *C);
6934dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAdd(Constant *C1, Constant *C2);
6944dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSub(Constant *C1, Constant *C2);
6954dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getMul(Constant *C1, Constant *C2);
6961628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getUDiv(Constant *C1, Constant *C2);
6971628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getSDiv(Constant *C1, Constant *C2);
6981628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getFDiv(Constant *C1, Constant *C2);
6990a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
7000a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
7010a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getFRem(Constant *C1, Constant *C2);
7024dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAnd(Constant *C1, Constant *C2);
7034dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getOr(Constant *C1, Constant *C2);
7044dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getXor(Constant *C1, Constant *C2);
7054647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
7064647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
707ac80ade1580378e484e24c9f66d2fa5b058e5891Nate Begeman  static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
708ac80ade1580378e484e24c9f66d2fa5b058e5891Nate Begeman  static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
7094dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getShl(Constant *C1, Constant *C2);
7103822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getLShr(Constant *C1, Constant *C2);
7113822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getAShr(Constant *C1, Constant *C2);
71202140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner
7137fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
7147fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// all elements must be Constant's.
7155133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
716fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getGetElementPtr(Constant *C,
717fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Constant* const *IdxList, unsigned NumIdx);
7187fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  static Constant *getGetElementPtr(Constant *C,
719fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Value* const *IdxList, unsigned NumIdx);
720df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
7219fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getExtractElement(Constant *Vec, Constant *Idx);
7229fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
7239fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
724041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getExtractValue(Constant *Agg,
72581a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                   const unsigned *IdxList, unsigned NumIdx);
726041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValue(Constant *Agg, Constant *Val,
72781a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                  const unsigned *IdxList, unsigned NumIdx);
728728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
72924d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
730fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// method returns the negative zero constant for floating point or vector
73124d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// floating point types; for all other types, it returns the null value.
73224d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  static Constant *getZeroValueForNegationExpr(const Type *Ty);
73324d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer
73493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
73593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
73629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isNullValue() const { return false; }
7379769ab22265b313171d201b5928688524a01bd87Misha Brukman
73893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
739eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  unsigned getOpcode() const { return SubclassData; }
74029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
741728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
742728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// not an ICMP or FCMP constant expression.
743728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  unsigned getPredicate() const;
744728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
74581a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// getIndices - Assert that this is an insertvalue or exactvalue
74681a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// expression and return the list of indices.
74781a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  const SmallVector<unsigned, 4> &getIndices() const;
74881a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman
74993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
750e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
7519769ab22265b313171d201b5928688524a01bd87Misha Brukman
75279ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// getWithOperandReplaced - Return a constant expression identical to this
75379ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// one, but with the specified operand set to the specified value.
75479ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
75579ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
7566b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// getWithOperands - This returns the current constant expression with the
7576b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// operands replaced with the specified values.  The specified operands must
7586b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// match count and type with the existing ones.
759b054bfd3ea50d599fe14bce1b74e39b686724dd9Chris Lattner  Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
760135ccbd74f041caccfbc26a0c935006dfa828e84Bill Wendling    return getWithOperands(&Ops[0], (unsigned)Ops.size());
761b054bfd3ea50d599fe14bce1b74e39b686724dd9Chris Lattner  }
762b054bfd3ea50d599fe14bce1b74e39b686724dd9Chris Lattner  Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
76379ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
764e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
76540cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
7669769ab22265b313171d201b5928688524a01bd87Misha Brukman
76793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
76829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
76929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
770a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantExprVal;
77129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
77229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
77329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
774efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
775efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
776efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
777efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
778efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
779e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
780e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner//===----------------------------------------------------------------------===//
781e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// UndefValue - 'undef' values are things that do not have specified contents.
782e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// These are used for a variety of purposes, including global variable
783e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// initializers and operands to instructions.  'undef' values can occur with
784e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// any type.
785e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner///
786e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerclass UndefValue : public Constant {
787e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  friend struct ConstantCreator<UndefValue, Type, char>;
788051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
789e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
790e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerprotected:
791423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
792051a950000e21935165db56695e35bade668193bGabor Greifprotected:
793051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
794051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
795051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
796051a950000e21935165db56695e35bade668193bGabor Greif  }
797e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerpublic:
798e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// get() - Static factory methods - Return an 'undef' object of the specified
799e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// type.
800e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  ///
801e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static UndefValue *get(const Type *T);
802e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
803e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
804e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// getNullValue.
805e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual bool isNullValue() const { return false; }
806e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
807e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual void destroyConstant();
808e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
809e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
810e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static inline bool classof(const UndefValue *) { return true; }
811e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static bool classof(const Value *V) {
812a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == UndefValueVal;
813e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  }
814e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner};
815e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
816d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
817d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
818009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
819