Constants.h revision 041e2eb51721bcfecee5d9c9fc409ff185526e47
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//
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"
25efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif#include "llvm/OperandTraits.h"
26532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer#include "llvm/ADT/APInt.h"
27343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen#include "llvm/ADT/APFloat.h"
28009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
29d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
31009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
32009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
334cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
349d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass VectorType;
35009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
366cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
376cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
385133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
395133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnerstruct ConvertConstantType;
405133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
4186e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4224d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer/// This is the shared class of boolean and integer constants. This class
436b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// represents both boolean and integral constants.
446b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// @brief Class for constant integers.
456b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Shengclass ConstantInt : public Constant {
46d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *TheTrueVal, *TheFalseVal;
47051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
486b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
49532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  ConstantInt(const IntegerType *Ty, const APInt& V);
50532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  APInt Val;
51051a950000e21935165db56695e35bade668193bGabor Greifprotected:
52051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
53051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
54051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
55051a950000e21935165db56695e35bade668193bGabor Greif  }
56994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
57532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// Return the constant as an APInt value reference. This allows clients to
58532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// obtain a copy of the value, with all its precision in tact.
59532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @brief Return the constant's value.
60532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  inline const APInt& getValue() const {
61532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val;
62532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  }
63e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner
64e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  /// getBitWidth - Return the bitwidth of this constant.
65e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  unsigned getBitWidth() const { return Val.getBitWidth(); }
66532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
670b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit unsigned integer value after it
68532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// has been zero extended as appropriate for the type of this constant. Note
69532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// that this method can assert if the value does not fit in 64 bits.
70532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
710b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Return the zero extended value.
72a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline uint64_t getZExtValue() const {
73532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getZExtValue();
74a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
75c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner
760b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit integer value after it has been sign
77532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// sign extended as appropriate for the type of this constant. Note that
78532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// this method can assert if the value does not fit in 64 bits.
79532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
806c6b6a77c0f68d5a41e94d8ef0b1093089e683faChris Lattner  /// @brief Return the sign extended value.
81a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline int64_t getSExtValue() const {
82532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getSExtValue();
83a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
84532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
850b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// A helper method that can be used to determine if the constant contained
860b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// within is equal to a constant.  This only works for very small values,
870b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// because this is all that can be represented with all types.
880b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if this constant's value is same as an unsigned char.
89532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  bool equalsInt(uint64_t V) const {
90b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    return Val == V;
912ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
922ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
936b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// getTrue/getFalse - Return the singleton true/false values.
946b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getTrue() {
95d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheTrueVal) return TheTrueVal;
96d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(true);
976b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
986b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getFalse() {
99d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheFalseVal) return TheFalseVal;
100d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(false);
1016b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1026b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
103a0098386741949065f099b940819201f73cd86c2Reid Spencer  /// Return a ConstantInt with the specified value for the specified type. The
1040050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
1050050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
1060050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// signed value for the type Ty.
1070b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Get a ConstantInt for a specific value.
1087fc44c8c5fb0efd6c909f4e306ad516df88e93e1Reid Spencer  static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
1090b5a504d105514178c80b886321221fbe5ac1131Reid Spencer
110f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// Return a ConstantInt with the specified value and an implied Type. The
111f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// type is the integer type that corresponds to the bit width of the value.
112f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  static ConstantInt *get(const APInt &V);
113f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer
114c10305743c313558405079452138f03124e87581Reid Spencer  /// getType - Specialize the getType() method to always return an IntegerType,
115c10305743c313558405079452138f03124e87581Reid Spencer  /// which reduces the amount of casting needed in parts of the compiler.
116c10305743c313558405079452138f03124e87581Reid Spencer  ///
117c10305743c313558405079452138f03124e87581Reid Spencer  inline const IntegerType *getType() const {
118c10305743c313558405079452138f03124e87581Reid Spencer    return reinterpret_cast<const IntegerType*>(Value::getType());
119c10305743c313558405079452138f03124e87581Reid Spencer  }
120c10305743c313558405079452138f03124e87581Reid Spencer
1210b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// This static method returns true if the type Ty is big enough to
1220b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// represent the value V. This can be used to avoid having the get method
1236d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// assert when V is larger than Ty can represent. Note that there are two
1246d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// versions of this method, one for unsigned and one for signed integers.
1256d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// Although ConstantInt canonicalizes everything to an unsigned integer,
1266d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// the signed version avoids callers having to convert a signed quantity
1276d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// to the appropriate unsigned type before calling the method.
1280b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if V is a valid value for type Ty
1290b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if the value is in range for the given type.
1309b11d518aef8e191e5e94f3503dfddbe1c0a387aReid Spencer  static bool isValueValidForType(const Type *Ty, uint64_t V);
131009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, int64_t V);
13293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
1336b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the "null"
1346b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that would be returned by the getNullValue method.
135b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @returns true if this is the null integer value.
1366b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is null.
1376b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  virtual bool isNullValue() const {
1386b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng    return Val == 0;
1396b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1405ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
14134da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// This is just a convenience method to make client code smaller for a
14234da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common code. It also correctly performs the comparison without the
14334da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
14434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isZero() const {
14534da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer    return Val == 0;
14634da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  }
14734da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer
14837eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// This is just a convenience method to make client code smaller for a
14934da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common case. It also correctly performs the comparison without the
15034da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
15137eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// @brief Determine if the value is one.
15234da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isOne() const {
15337eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer    return Val == 1;
15437eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  }
15537eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer
1566b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff every bit in this constant is set
1576b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// to true.
1580b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this constant's bits are all set to true.
1596b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is all ones.
160579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isAllOnesValue() const {
161532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.isAllOnesValue();
1626b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1631680312867fffeb9369800949b809e0b9e29a914Chris Lattner
1646b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the largest
1656b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by the constant's type.
1660b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this is the largest value that may be represented
1670b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// by this type.
1686b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is maximal.
169579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMaxValue(bool isSigned) const {
170532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
171532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxSignedValue();
172532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
173532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxValue();
174994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
175994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
1766b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the smallest
1776b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by this constant's type.
1780b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if this is the smallest value that may be represented by
1790b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// this type.
1806b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is minimal.
181579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMinValue(bool isSigned) const {
182532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
183532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinSignedValue();
184532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
185532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinValue();
186994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
187994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
188b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// This function will return true iff this constant represents a value with
189b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// active bits bigger than 64 bits or a value greater than the given uint64_t
190b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// value.
191b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns true iff this constant is greater or equal to the given number.
192b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Determine if the value is greater or equal to the given number.
1930642f75836960afd547a6b98fad33026d0c355beZhou Sheng  bool uge(uint64_t Num) {
1940642f75836960afd547a6b98fad33026d0c355beZhou Sheng    return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
195b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
196b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
197b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns the 64-bit value of this constant if its active bits number is
198b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// not greater than 64, otherwise, just return the given uint64_t number.
199b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Get the constant's value if possible.
20099b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
20199b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner    return Val.getLimitedValue(Limit);
202b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
203b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
2046b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @returns the value for an integer constant of the given type that has all
2056b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// its bits set to true.
2066b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Get the all ones value
2076b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static ConstantInt *getAllOnesValue(const Type *Ty);
2086b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
209b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
210b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  static inline bool classof(const ConstantInt *) { return true; }
21152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
212a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantIntVal;
2135ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
214d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
215d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattnerprivate:
216d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *CreateTrueFalseVals(bool WhichOne);
217009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
218009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
219009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
22086e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
22193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
22293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
223e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
224343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen  APFloat Val;
225051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
226e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
2273e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
228f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  ConstantFP(const Type *Ty, const APFloat& V);
229051a950000e21935165db56695e35bade668193bGabor Greifprotected:
230051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
231051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
232051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
233051a950000e21935165db56695e35bade668193bGabor Greif  }
234009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
23593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
2364592230265384bebc2879303310f89dee4d9d7a8Chris Lattner  static ConstantFP *get(const APFloat &V);
237009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
238a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// get() - This returns a constant fp for the specified value in the
239a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// specified type.  This should only be used for simple constant values like
240a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
241d6d48c4e5b60aa4acc02c7a8700cb79949503e93Chris Lattner  static ConstantFP *get(const Type *Ty, double V);
242a541931ddb43191daea4217faa4762934d8bc169Evan Cheng
24393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
244f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  static bool isValueValidForType(const Type *Ty, const APFloat& V);
245f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  inline const APFloat& getValueAPF() const { return Val; }
2465ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
24793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
248cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
249cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// considers -0.0 to be null as well as 0.0.  :(
2503a1eff732b36a663cdb9f07f4a9ccae6452eadcbJim Laskey  virtual bool isNullValue() const;
2510eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
2529e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  // Get a negative zero.
2539e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  static ConstantFP *getNegativeZero(const Type* Ty);
2549e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen
255f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
256f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
257f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
25843421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// two floating point values.  The version with a double operand is retained
25943421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// because it's so convenient to write isExactlyValue(2.0), but please use
260def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner  /// it only for simple constants.
261f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(const APFloat& V) const;
26243421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen
263f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(double V) const {
264beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    // convert is not supported on this type
265beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
266beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen      return false;
267def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    APFloat FV(V);
268def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven);
269def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    return isExactlyValue(FV);
270f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  }
27193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
272e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
27352eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
274a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantFPVal;
2755ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
276009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
277009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
27886e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
27940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
28040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
28140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
28240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
283051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
28440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
28540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
286423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit ConstantAggregateZero(const Type *Ty)
2875181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner    : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
288051a950000e21935165db56695e35bade668193bGabor Greifprotected:
289051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
290051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
291051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
292051a950000e21935165db56695e35bade668193bGabor Greif  }
29340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
29440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// get() - static factory method for creating a null aggregate.  It is
29540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// illegal to call this method with a non-aggregate type.
29640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static Constant *get(const Type *Ty);
29740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
29840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
29940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.
30040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return true; }
30140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void destroyConstant();
30340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
30540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
30652eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const ConstantAggregateZero *) { return true; }
30752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
308a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantAggregateZeroVal;
30940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
31040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
31140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
312009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
31386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
31493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
31593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
316e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3176cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
3186cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
319e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3203e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
321697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
322009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
32393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
324ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
325df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const ArrayType *T,
326df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       Constant*const*Vals, unsigned NumVals) {
327df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
328df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
329df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
330efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer
331efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer  /// This method constructs a ConstantArray and initializes it with a text
332461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// string. The default behavior (AddNull==true) causes a null terminator to
333461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// be placed at the end of the array. This effectively increases the length
334461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// of the array by one (you've been warned).  However, in some situations
335461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// this is not desired so if AddNull==false then the string is copied without
336461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// null termination.
337461bed2b753c514ac15ff824befe585988408f45Reid Spencer  static Constant *get(const std::string &Initializer, bool AddNull = true);
3389769ab22265b313171d201b5928688524a01bd87Misha Brukman
339efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
340efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
341efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
34293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
34393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
34493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
345682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3468b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
347682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
348009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3496eabd74d1a236effc6757e7c81182fa1ecd52ac6Nick Lewycky  /// isString - This method returns true if the array is an array of i8 and
3506eabd74d1a236effc6757e7c81182fa1ecd52ac6Nick Lewycky  /// the elements of the array are all ConstantInt's.
3519b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3529b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
35322c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isCString - This method returns true if the array is a string (see
354181b6c9cb5def44658d15848e34c5c45d973f065Reid Spencer  /// @verbatim
35522c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isString) and it ends in a null byte \0 and does not contains any other
356181b6c9cb5def44658d15848e34c5c45d973f065Reid Spencer  /// @endverbatim
35722c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// null bytes except its terminator.
35822c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  bool isCString() const;
35922c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng
3609b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// getAsString - If this array is isString(), then this method converts the
3619b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// array to an std::string and returns it.  Otherwise, it asserts out.
36293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
36393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  std::string getAsString() const;
36493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
36593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
36640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero arrays are always
36740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
36840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return false; }
3690eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
370e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
37140cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
372e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
37393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
374e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
37552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
376a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantArrayVal;
3775ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
378009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
379009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
380efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
381efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
382efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
383efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
384efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
385009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
38686e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
387e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
388009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
389e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3906cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
3916cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
392e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3933e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
394697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
395009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
39693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
397c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
398ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
399df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
400df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals,
401df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       bool Packed = false) {
402df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
403df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
404df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
405df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
406efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
407efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
408efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
40993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
410c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
411682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const StructType *getType() const {
4128b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const StructType*>(Value::getType());
413682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
414009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
41593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
41640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero structs are always
41740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
418a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
41940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner    return false;
420a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
4210eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
422e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
42340cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
4249769ab22265b313171d201b5928688524a01bd87Misha Brukman
42593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
426e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
42752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
428a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantStructVal;
4295ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
430009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
431009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
432efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
433efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
434efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
435efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
436efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
437efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
43886e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4399d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer/// ConstantVector - Constant Vector Declarations
440715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke///
4419d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass ConstantVector : public Constant {
4429d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  friend struct ConstantCreator<ConstantVector, VectorType,
443715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke                                    std::vector<Constant*> >;
4449d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
445715c90ba524e736190a6380695ab337eeb5148beBrian Gaekeprotected:
4469d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
447715c90ba524e736190a6380695ab337eeb5148beBrian Gaekepublic:
448715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// get() - Static factory methods - Return objects of the specified value
4499d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static Constant *get(const VectorType *T, const std::vector<Constant*> &);
450715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static Constant *get(const std::vector<Constant*> &V);
451df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals) {
452df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
453df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals));
454df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
455df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
456efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
457efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
458efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
459fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// getType - Specialize the getType() method to always return a VectorType,
460715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// which reduces the amount of casting needed in parts of the compiler.
461715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  ///
4629d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  inline const VectorType *getType() const {
4639d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer    return reinterpret_cast<const VectorType*>(Value::getType());
464715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
465715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
466fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// @returns the value for a vector integer constant of the given type that
46758513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// has all its bits set to true.
46858513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// @brief Get the all ones value
4699d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static ConstantVector *getAllOnesValue(const VectorType *Ty);
47058513aa1c22a08118734ac799d935ea2910db35aChris Lattner
471715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// isNullValue - Return true if this is the value that would be returned by
47207a96765daedf180a7102d39fe56c499878312b7Dan Gohman  /// getNullValue.  This always returns false because zero vectors are always
473715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// created as ConstantAggregateZero objects.
474715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual bool isNullValue() const { return false; }
475715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
476fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// This function will return true iff every element in this vector constant
477fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// is set to all ones.
478fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @returns true iff this constant's emements are all set to all ones.
479fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @brief Determine if the value is all ones.
480569cc890e8822c265cec66745791edbd510dac1cJim Laskey  bool isAllOnesValue() const;
481fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey
4823b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// getSplatValue - If this is a splat constant, meaning that all of the
4833b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// elements have the same value, return that value. Otherwise return NULL.
4843b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  Constant *getSplatValue();
4853b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman
486715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual void destroyConstant();
48740cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
488715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
489715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// Methods for support type inquiry through isa, cast, and dyn_cast:
4909d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static inline bool classof(const ConstantVector *) { return true; }
491715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static bool classof(const Value *V) {
492a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantVectorVal;
493715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
494715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke};
495715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
496efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
497efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
498efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
499efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
500efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
501efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
50286e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
50393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerNull - a constant pointer value that points to null
50493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
50548babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerNull : public Constant {
5066cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
507051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
508e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
5095ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerprotected:
510423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit ConstantPointerNull(const PointerType *T)
5115181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner    : Constant(reinterpret_cast<const Type*>(T),
512225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner               Value::ConstantPointerNullVal, 0, 0) {}
513afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen
514051a950000e21935165db56695e35bade668193bGabor Greifprotected:
515051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
516051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
517051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
518051a950000e21935165db56695e35bade668193bGabor Greif  }
5195ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
52093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
521e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerNull *get(const PointerType *T);
5225ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
52393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
52493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
5250eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return true; }
5260eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
527e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
528e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
5290f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// getType - Specialize the getType() method to always return an PointerType,
5300f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
5310f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  ///
5320f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  inline const PointerType *getType() const {
5330f42ba67771627453187e44fea935d6bab7a8152Chris Lattner    return reinterpret_cast<const PointerType*>(Value::getType());
5340f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  }
5350f42ba67771627453187e44fea935d6bab7a8152Chris Lattner
53693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
537e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
53852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
539a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantPointerNullVal;
5405ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
5414cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
5424cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
543f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
54486e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner/// ConstantExpr - a constant value that is initialized with an expression using
545eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// other constant values.
54686e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner///
547eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// This class uses the standard Instruction opcodes to define the various
548eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// constant expressions.  The Opcode field for the ConstantExpr class is
549eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// maintained in the Value::SubclassData field.
55029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
5516cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
5526cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
5535133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
5549769ab22265b313171d201b5928688524a01bd87Misha Brukman
5556cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
5565181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner  ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
557eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    : Constant(Ty, ConstantExprVal, Ops, NumOps) {
558eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    // Operation type (an Instruction opcode) is stored as the SubclassData.
5599769ab22265b313171d201b5928688524a01bd87Misha Brukman    SubclassData = Opcode;
560eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  }
5615133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
5625133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
5635133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
5645133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
56590fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer                         Constant *C1, Constant *C2);
566e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompareTy(unsigned short pred, Constant *C1,
567e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer                                Constant *C2);
56846a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelectTy(const Type *Ty,
56946a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner                               Constant *C1, Constant *C2, Constant *C3);
5705133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
571fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                      Value* const *Idxs, unsigned NumIdxs);
57249b78a569609881811d905960baa7dd1ab801383Robert Bocchino  static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
57349b78a569609881811d905960baa7dd1ab801383Robert Bocchino                                       Constant *Idx);
574f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino  static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
575f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino                                      Constant *Elt, Constant *Idx);
5769fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
5779fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner                                      Constant *V2, Constant *Mask);
578041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
579041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                     Constant * const *Idxs, unsigned NumIdxs);
580041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
581041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                    Constant *Val,
582041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                    Constant * const *Idxs, unsigned NumIdxs);
5839769ab22265b313171d201b5928688524a01bd87Misha Brukman
58429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
585fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
586baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
587baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
588baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
5899769ab22265b313171d201b5928688524a01bd87Misha Brukman
59093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5915133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
592d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getTrunc   (Constant *C, const Type *Ty);
593d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSExt    (Constant *C, const Type *Ty);
594d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getZExt    (Constant *C, const Type *Ty);
595d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPTrunc (Constant *C, const Type *Ty);
596d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPExtend(Constant *C, const Type *Ty);
597d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getUIToFP  (Constant *C, const Type *Ty);
598d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSIToFP  (Constant *C, const Type *Ty);
599d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToUI  (Constant *C, const Type *Ty);
600d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToSI  (Constant *C, const Type *Ty);
601d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getPtrToInt(Constant *C, const Type *Ty);
602d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getIntToPtr(Constant *C, const Type *Ty);
603d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getBitCast (Constant *C, const Type *Ty);
6043da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
605efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
606efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
607efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
6083da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // @brief Convenience function for getting one of the casting operations
6093da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // using a CastOps opcode.
6103da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  static Constant *getCast(
6113da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    unsigned ops,  ///< The opcode for the conversion
6123da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    Constant *C,   ///< The constant to be converted
6133da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    const Type *Ty ///< The type to which the constant is converted
6143da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  );
6153da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
616848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a ZExt or BitCast cast constant expression
617848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getZExtOrBitCast(
618848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    Constant *C,   ///< The constant to zext or bitcast
619848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    const Type *Ty ///< The type to zext or bitcast C to
620848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
621848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
622848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a SExt or BitCast cast constant expression
623848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getSExtOrBitCast(
62490fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to sext or bitcast
62590fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to sext or bitcast C to
626848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
627848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
628848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a Trunc or BitCast cast constant expression
629848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getTruncOrBitCast(
63090fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to trunc or bitcast
63190fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to trunc or bitcast C to
632848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
633848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
634887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Create a BitCast or a PtrToInt cast constant expression
635887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  static Constant *getPointerCast(
636887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    Constant *C,   ///< The pointer value to be casted (operand 0)
637887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    const Type *Ty ///< The type to which cast should be made
638887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  );
639887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer
64084f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
64184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getIntegerCast(
64284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
64384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty, ///< The integer type to cast to
64484f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    bool isSigned   ///< Whether C should be treated as signed or not
64584f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
64684f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
64784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
64884f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getFPCast(
64984f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
65084f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty ///< The integer type to cast to
65184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
65284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
6533da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  /// @brief Return true if this is a convert constant expression
6543da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  bool isCast() const;
6553da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
6564b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  /// @brief Return true if this is a compare constant expression
6574b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  bool isCompare() const;
6584b94f49091be8e26ce6e49db1268458995299b82Reid Spencer
65946a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  /// Select constant expr
66046a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ///
66146a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
66246a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner    return getSelectTy(V1->getType(), C, V1, V2);
66346a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  }
66446a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
6657be2a120659cc61e554b04815ed3f1d4f234ecafAlkis Evlogimenos  /// getSizeOf constant expr - computes the size of a type in a target
6664647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  /// independent way (Note: the return type is an i64).
66760ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  ///
66860ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  static Constant *getSizeOf(const Type *Ty);
66946a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
6707e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
6717e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
6725133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
67390fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
67490fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer
675887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Return an ICmp or FCmp comparison operator constant expression.
676e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
677e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
6784dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// ConstantExpr::get* - Return some common constants without having to
6794dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// specify the full Instruction::OPCODE identifier.
6804dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  ///
6814dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNeg(Constant *C);
6824dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNot(Constant *C);
6834dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAdd(Constant *C1, Constant *C2);
6844dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSub(Constant *C1, Constant *C2);
6854dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getMul(Constant *C1, Constant *C2);
6861628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getUDiv(Constant *C1, Constant *C2);
6871628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getSDiv(Constant *C1, Constant *C2);
6881628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getFDiv(Constant *C1, Constant *C2);
6890a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
6900a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
6910a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getFRem(Constant *C1, Constant *C2);
6924dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAnd(Constant *C1, Constant *C2);
6934dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getOr(Constant *C1, Constant *C2);
6944dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getXor(Constant *C1, Constant *C2);
6954647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
6964647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
697ac80ade1580378e484e24c9f66d2fa5b058e5891Nate Begeman  static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
698ac80ade1580378e484e24c9f66d2fa5b058e5891Nate Begeman  static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
6994dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getShl(Constant *C1, Constant *C2);
7003822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getLShr(Constant *C1, Constant *C2);
7013822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getAShr(Constant *C1, Constant *C2);
70202140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner
7037fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
7047fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// all elements must be Constant's.
7055133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
706fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getGetElementPtr(Constant *C,
707fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Constant* const *IdxList, unsigned NumIdx);
7087fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  static Constant *getGetElementPtr(Constant *C,
709fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Value* const *IdxList, unsigned NumIdx);
710df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
7119fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getExtractElement(Constant *Vec, Constant *Idx);
7129fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
7139fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
714041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getExtractValue(Constant *Agg,
715041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                   Constant* const *IdxList, unsigned NumIdx);
716041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValue(Constant *Agg, Constant *Val,
717041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                  Constant* const *IdxList, unsigned NumIdx);
718728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
71924d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
720fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// method returns the negative zero constant for floating point or vector
72124d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// floating point types; for all other types, it returns the null value.
72224d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  static Constant *getZeroValueForNegationExpr(const Type *Ty);
72324d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer
72493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
72593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
72629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isNullValue() const { return false; }
7279769ab22265b313171d201b5928688524a01bd87Misha Brukman
72893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
729eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  unsigned getOpcode() const { return SubclassData; }
73029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
731728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
732728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// not an ICMP or FCMP constant expression.
733728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  unsigned getPredicate() const;
734728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
73593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
736e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
7379769ab22265b313171d201b5928688524a01bd87Misha Brukman
73879ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// getWithOperandReplaced - Return a constant expression identical to this
73979ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// one, but with the specified operand set to the specified value.
74079ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
74179ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
7426b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// getWithOperands - This returns the current constant expression with the
7436b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// operands replaced with the specified values.  The specified operands must
7446b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// match count and type with the existing ones.
7456b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
74679ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
747e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
74840cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
7499769ab22265b313171d201b5928688524a01bd87Misha Brukman
75093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
75129ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
75229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
753a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantExprVal;
75429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
75529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
75629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
757efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
758efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
759efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
760efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
761efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
762e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
763e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner//===----------------------------------------------------------------------===//
764e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// UndefValue - 'undef' values are things that do not have specified contents.
765e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// These are used for a variety of purposes, including global variable
766e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// initializers and operands to instructions.  'undef' values can occur with
767e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// any type.
768e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner///
769e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerclass UndefValue : public Constant {
770e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  friend struct ConstantCreator<UndefValue, Type, char>;
771051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
772e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
773e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerprotected:
774423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
775051a950000e21935165db56695e35bade668193bGabor Greifprotected:
776051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
777051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
778051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
779051a950000e21935165db56695e35bade668193bGabor Greif  }
780e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerpublic:
781e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// get() - Static factory methods - Return an 'undef' object of the specified
782e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// type.
783e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  ///
784e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static UndefValue *get(const Type *T);
785e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
786e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
787e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// getNullValue.
788e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual bool isNullValue() const { return false; }
789e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
790e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual void destroyConstant();
791e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
792e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
793e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static inline bool classof(const UndefValue *) { return true; }
794e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static bool classof(const Value *V) {
795a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == UndefValueVal;
796e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  }
797e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner};
798e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
799d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
800d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
801009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
802