Constants.h revision 8aaec36fa760c9dd50627e397ca85798d9eb6e79
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"
2881a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman#include "llvm/ADT/SmallVector.h"
29009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
31d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
32009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
33009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
344cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
359d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass VectorType;
36009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
376cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
386cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
395133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
405133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnerstruct ConvertConstantType;
415133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
4286e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4324d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer/// This is the shared class of boolean and integer constants. This class
446b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// represents both boolean and integral constants.
456b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// @brief Class for constant integers.
466b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Shengclass ConstantInt : public Constant {
47d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *TheTrueVal, *TheFalseVal;
48051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
496b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
50532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  ConstantInt(const IntegerType *Ty, const APInt& V);
51532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  APInt Val;
52051a950000e21935165db56695e35bade668193bGabor Greifprotected:
53051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
54051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
55051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
56051a950000e21935165db56695e35bade668193bGabor Greif  }
57994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
58532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// Return the constant as an APInt value reference. This allows clients to
59532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// obtain a copy of the value, with all its precision in tact.
60532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @brief Return the constant's value.
61532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  inline const APInt& getValue() const {
62532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val;
63532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  }
64e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner
65e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  /// getBitWidth - Return the bitwidth of this constant.
66e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  unsigned getBitWidth() const { return Val.getBitWidth(); }
67532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
680b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit unsigned integer value after it
69532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// has been zero extended as appropriate for the type of this constant. Note
70532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// that this method can assert if the value does not fit in 64 bits.
71532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
720b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Return the zero extended value.
73a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline uint64_t getZExtValue() const {
74532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getZExtValue();
75a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
76c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner
770b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit integer value after it has been sign
78532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// sign extended as appropriate for the type of this constant. Note that
79532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// this method can assert if the value does not fit in 64 bits.
80532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
816c6b6a77c0f68d5a41e94d8ef0b1093089e683faChris Lattner  /// @brief Return the sign extended value.
82a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline int64_t getSExtValue() const {
83532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getSExtValue();
84a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
85532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
860b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// A helper method that can be used to determine if the constant contained
870b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// within is equal to a constant.  This only works for very small values,
880b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// because this is all that can be represented with all types.
890b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if this constant's value is same as an unsigned char.
90532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  bool equalsInt(uint64_t V) const {
91b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    return Val == V;
922ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
932ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
946b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// getTrue/getFalse - Return the singleton true/false values.
956b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getTrue() {
96d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheTrueVal) return TheTrueVal;
97d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(true);
986b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
996b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static inline ConstantInt *getFalse() {
100d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    if (TheFalseVal) return TheFalseVal;
101d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner    return CreateTrueFalseVals(false);
1026b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1036b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
104a0098386741949065f099b940819201f73cd86c2Reid Spencer  /// Return a ConstantInt with the specified value for the specified type. The
1050050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
1060050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
1070050c737c549b89f5df6743dc81646a3b7f4a94fReid Spencer  /// signed value for the type Ty.
1080b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Get a ConstantInt for a specific value.
1097fc44c8c5fb0efd6c909f4e306ad516df88e93e1Reid Spencer  static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
1100b5a504d105514178c80b886321221fbe5ac1131Reid Spencer
111f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// Return a ConstantInt with the specified value and an implied Type. The
112f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  /// type is the integer type that corresponds to the bit width of the value.
113f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer  static ConstantInt *get(const APInt &V);
114f57fc81faebf3eb81fb30fe17c4295d46060e03fReid Spencer
115c10305743c313558405079452138f03124e87581Reid Spencer  /// getType - Specialize the getType() method to always return an IntegerType,
116c10305743c313558405079452138f03124e87581Reid Spencer  /// which reduces the amount of casting needed in parts of the compiler.
117c10305743c313558405079452138f03124e87581Reid Spencer  ///
118c10305743c313558405079452138f03124e87581Reid Spencer  inline const IntegerType *getType() const {
119c10305743c313558405079452138f03124e87581Reid Spencer    return reinterpret_cast<const IntegerType*>(Value::getType());
120c10305743c313558405079452138f03124e87581Reid Spencer  }
121c10305743c313558405079452138f03124e87581Reid Spencer
1220b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// This static method returns true if the type Ty is big enough to
1230b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// represent the value V. This can be used to avoid having the get method
1246d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// assert when V is larger than Ty can represent. Note that there are two
1256d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// versions of this method, one for unsigned and one for signed integers.
1266d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// Although ConstantInt canonicalizes everything to an unsigned integer,
1276d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// the signed version avoids callers having to convert a signed quantity
1286d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// to the appropriate unsigned type before calling the method.
1290b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if V is a valid value for type Ty
1300b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if the value is in range for the given type.
1319b11d518aef8e191e5e94f3503dfddbe1c0a387aReid Spencer  static bool isValueValidForType(const Type *Ty, uint64_t V);
132009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  static bool isValueValidForType(const Type *Ty, int64_t V);
13393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
1346b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the "null"
1356b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that would be returned by the getNullValue method.
136b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @returns true if this is the null integer value.
1376b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is null.
1386b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  virtual bool isNullValue() const {
1396b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng    return Val == 0;
1406b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1415ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
14234da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// This is just a convenience method to make client code smaller for a
14334da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common code. It also correctly performs the comparison without the
14434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
14534da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isZero() const {
14634da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer    return Val == 0;
14734da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  }
14834da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer
14937eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// This is just a convenience method to make client code smaller for a
15034da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common case. It also correctly performs the comparison without the
15134da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
15237eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// @brief Determine if the value is one.
15334da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isOne() const {
15437eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer    return Val == 1;
15537eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  }
15637eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer
1576b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff every bit in this constant is set
1586b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// to true.
1590b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this constant's bits are all set to true.
1606b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is all ones.
161579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isAllOnesValue() const {
162532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.isAllOnesValue();
1636b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1641680312867fffeb9369800949b809e0b9e29a914Chris Lattner
1656b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the largest
1666b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by the constant's type.
1670b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this is the largest value that may be represented
1680b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// by this type.
1696b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is maximal.
170579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMaxValue(bool isSigned) const {
171532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
172532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxSignedValue();
173532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
174532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxValue();
175994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
176994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
1776b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the smallest
1786b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by this constant's type.
1790b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if this is the smallest value that may be represented by
1800b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// this type.
1816b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is minimal.
182579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMinValue(bool isSigned) const {
183532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
184532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinSignedValue();
185532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
186532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinValue();
187994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
188994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
189b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// This function will return true iff this constant represents a value with
190b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// active bits bigger than 64 bits or a value greater than the given uint64_t
191b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// value.
192b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns true iff this constant is greater or equal to the given number.
193b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Determine if the value is greater or equal to the given number.
1940642f75836960afd547a6b98fad33026d0c355beZhou Sheng  bool uge(uint64_t Num) {
1950642f75836960afd547a6b98fad33026d0c355beZhou Sheng    return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
196b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
197b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
198b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns the 64-bit value of this constant if its active bits number is
199b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// not greater than 64, otherwise, just return the given uint64_t number.
200b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Get the constant's value if possible.
20199b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
20299b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner    return Val.getLimitedValue(Limit);
203b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
204b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
2056b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @returns the value for an integer constant of the given type that has all
2066b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// its bits set to true.
2076b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Get the all ones value
2086b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  static ConstantInt *getAllOnesValue(const Type *Ty);
2096b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng
210b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
211b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  static inline bool classof(const ConstantInt *) { return true; }
21252eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
213a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantIntVal;
2145ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
215d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
216d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattnerprivate:
217d1afbd02ef9405fb5c1575bf1d3656f434c4a956Chris Lattner  static ConstantInt *CreateTrueFalseVals(bool WhichOne);
218009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
219009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
220009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
22186e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
22293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
22393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
224e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
225343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen  APFloat Val;
226051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
227e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
2283e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
229f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  ConstantFP(const Type *Ty, const APFloat& V);
230051a950000e21935165db56695e35bade668193bGabor Greifprotected:
231051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
232051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
233051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
234051a950000e21935165db56695e35bade668193bGabor Greif  }
235009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
23693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
2374592230265384bebc2879303310f89dee4d9d7a8Chris Lattner  static ConstantFP *get(const APFloat &V);
238009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
239a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// get() - This returns a constant fp for the specified value in the
240a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// specified type.  This should only be used for simple constant values like
241a541931ddb43191daea4217faa4762934d8bc169Evan Cheng  /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
242d6d48c4e5b60aa4acc02c7a8700cb79949503e93Chris Lattner  static ConstantFP *get(const Type *Ty, double V);
243a541931ddb43191daea4217faa4762934d8bc169Evan Cheng
24493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
245f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  static bool isValueValidForType(const Type *Ty, const APFloat& V);
246f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  inline const APFloat& getValueAPF() const { return Val; }
2475ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
24893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
249cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
250cf2c4f8ae52d5f0a01ab40eda6d165f91b8de441Chris Lattner  /// considers -0.0 to be null as well as 0.0.  :(
2513a1eff732b36a663cdb9f07f4a9ccae6452eadcbJim Laskey  virtual bool isNullValue() const;
2520eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
2539e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  // Get a negative zero.
2549e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen  static ConstantFP *getNegativeZero(const Type* Ty);
2559e3d3abd937c9bb79d56d25ec0e0724c7cbba67cDale Johannesen
256f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
257f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
258f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
25943421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// two floating point values.  The version with a double operand is retained
26043421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// because it's so convenient to write isExactlyValue(2.0), but please use
261def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner  /// it only for simple constants.
262f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(const APFloat& V) const;
26343421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen
264f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(double V) const {
2658aaec36fa760c9dd50627e397ca85798d9eb6e79Dale Johannesen    bool ignored;
266beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    // convert is not supported on this type
267beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
268beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen      return false;
269def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    APFloat FV(V);
2708aaec36fa760c9dd50627e397ca85798d9eb6e79Dale Johannesen    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
271def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    return isExactlyValue(FV);
272f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  }
27393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
274e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
27552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
276a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantFPVal;
2775ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
278009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
279009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
28086e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
28140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
28240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
28340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
28440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
285051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
28640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
28740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
28813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  explicit ConstantAggregateZero(const Type *ty)
28913d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
290051a950000e21935165db56695e35bade668193bGabor Greifprotected:
291051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
292051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
293051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
294051a950000e21935165db56695e35bade668193bGabor Greif  }
29540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
29640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// get() - static factory method for creating a null aggregate.  It is
29740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// illegal to call this method with a non-aggregate type.
29840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  static Constant *get(const Type *Ty);
29940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
30140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.
30240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return true; }
30340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual void destroyConstant();
30540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
30640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
30740bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
30852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const ConstantAggregateZero *) { return true; }
30952eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
310a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantAggregateZeroVal;
31140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
31240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
31340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
314009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
31586e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
31693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
31793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
318e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3196cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantArray, ArrayType,
3206cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
321e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3223e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
323697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
324009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
32593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
326ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
327df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const ArrayType *T,
328df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       Constant*const*Vals, unsigned NumVals) {
329df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
330df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
331df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
332efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer
333efcaa42c282ae6c6ba9652c48738c334878afbc6Reid Spencer  /// This method constructs a ConstantArray and initializes it with a text
334461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// string. The default behavior (AddNull==true) causes a null terminator to
335461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// be placed at the end of the array. This effectively increases the length
336461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// of the array by one (you've been warned).  However, in some situations
337461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// this is not desired so if AddNull==false then the string is copied without
338461bed2b753c514ac15ff824befe585988408f45Reid Spencer  /// null termination.
339461bed2b753c514ac15ff824befe585988408f45Reid Spencer  static Constant *get(const std::string &Initializer, bool AddNull = true);
3409769ab22265b313171d201b5928688524a01bd87Misha Brukman
341efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
342efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
343efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
34493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
34593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
34693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
347682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const ArrayType *getType() const {
3488b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const ArrayType*>(Value::getType());
349682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
350009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3516eabd74d1a236effc6757e7c81182fa1ecd52ac6Nick Lewycky  /// isString - This method returns true if the array is an array of i8 and
3526eabd74d1a236effc6757e7c81182fa1ecd52ac6Nick Lewycky  /// the elements of the array are all ConstantInt's.
3539b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  bool isString() const;
3549b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner
35522c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isCString - This method returns true if the array is a string (see
356181b6c9cb5def44658d15848e34c5c45d973f065Reid Spencer  /// @verbatim
35722c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// isString) and it ends in a null byte \0 and does not contains any other
358181b6c9cb5def44658d15848e34c5c45d973f065Reid Spencer  /// @endverbatim
35922c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  /// null bytes except its terminator.
36022c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng  bool isCString() const;
36122c7030a0c535ed48d8465994f8f8aaf8fa76813Evan Cheng
3629b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// getAsString - If this array is isString(), then this method converts the
3639b0a5ee5bb77b5f74320f3603750f4521d16d196Chris Lattner  /// array to an std::string and returns it.  Otherwise, it asserts out.
36493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
36593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  std::string getAsString() const;
36693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
36793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
36840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero arrays are always
36940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
37040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  virtual bool isNullValue() const { return false; }
3710eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
372e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
37340cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
374e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
37593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
376e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
37752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
378a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantArrayVal;
3795ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
380009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
381009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
382efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
383efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
384efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
385efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
386efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
387009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
38886e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
389e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
390009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
391e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3926cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantStruct, StructType,
3936cc89aad25155ecd93b5318414851aa46351196dChris Lattner                                    std::vector<Constant*> >;
394e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3953e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
396697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner  ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
397009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
39893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
399c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
400ca705fa31d53469d2e6d0e52fa9e40d3e13a088aChris Lattner  static Constant *get(const StructType *T, const std::vector<Constant*> &V);
401df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
402df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals,
403df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner                       bool Packed = false) {
404df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
405df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
406df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
407df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
408efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
409efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
410efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
41193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
412c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
413682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  inline const StructType *getType() const {
4148b70b78ba489b090d9866e6a4084ab1e8613b527Chris Lattner    return reinterpret_cast<const StructType*>(Value::getType());
415682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
416009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
41793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
41840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// getNullValue.  This always returns false because zero structs are always
41940bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// created as ConstantAggregateZero objects.
420a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  virtual bool isNullValue() const {
42140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner    return false;
422a5ae71a137b9c6f458cc6c9064be787dbc5f4311Chris Lattner  }
4230eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
424e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
42540cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
4269769ab22265b313171d201b5928688524a01bd87Misha Brukman
42793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
428e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
42952eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
430a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantStructVal;
4315ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
432009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
433009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
434efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
435efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
436efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
437efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
438efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
439efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
44086e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4419d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer/// ConstantVector - Constant Vector Declarations
442715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke///
4439d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass ConstantVector : public Constant {
4449d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  friend struct ConstantCreator<ConstantVector, VectorType,
445715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke                                    std::vector<Constant*> >;
4469d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
447715c90ba524e736190a6380695ab337eeb5148beBrian Gaekeprotected:
4489d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
449715c90ba524e736190a6380695ab337eeb5148beBrian Gaekepublic:
450715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// get() - Static factory methods - Return objects of the specified value
4519d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static Constant *get(const VectorType *T, const std::vector<Constant*> &);
452715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static Constant *get(const std::vector<Constant*> &V);
453df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  static Constant *get(Constant*const* Vals, unsigned NumVals) {
454df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    // FIXME: make this the primary ctor method.
455df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner    return get(std::vector<Constant*>(Vals, Vals+NumVals));
456df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner  }
457df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
458efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
459efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
460efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
461fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// getType - Specialize the getType() method to always return a VectorType,
462715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// which reduces the amount of casting needed in parts of the compiler.
463715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  ///
4649d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  inline const VectorType *getType() const {
4659d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer    return reinterpret_cast<const VectorType*>(Value::getType());
466715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
467715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
468fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// @returns the value for a vector integer constant of the given type that
46958513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// has all its bits set to true.
47058513aa1c22a08118734ac799d935ea2910db35aChris Lattner  /// @brief Get the all ones value
4719d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static ConstantVector *getAllOnesValue(const VectorType *Ty);
47258513aa1c22a08118734ac799d935ea2910db35aChris Lattner
473715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// isNullValue - Return true if this is the value that would be returned by
47407a96765daedf180a7102d39fe56c499878312b7Dan Gohman  /// getNullValue.  This always returns false because zero vectors are always
475715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// created as ConstantAggregateZero objects.
476715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual bool isNullValue() const { return false; }
477715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
478fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// This function will return true iff every element in this vector constant
479fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// is set to all ones.
480fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @returns true iff this constant's emements are all set to all ones.
481fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey  /// @brief Determine if the value is all ones.
482569cc890e8822c265cec66745791edbd510dac1cJim Laskey  bool isAllOnesValue() const;
483fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey
4843b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// getSplatValue - If this is a splat constant, meaning that all of the
4853b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// elements have the same value, return that value. Otherwise return NULL.
4863b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  Constant *getSplatValue();
4873b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman
488715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  virtual void destroyConstant();
48940cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
490715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
491715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// Methods for support type inquiry through isa, cast, and dyn_cast:
4929d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static inline bool classof(const ConstantVector *) { return true; }
493715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static bool classof(const Value *V) {
494a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantVectorVal;
495715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
496715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke};
497715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
498efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
499efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
500efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
501efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
502efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
503efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
50486e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
50593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerNull - a constant pointer value that points to null
50693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
50748babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerNull : public Constant {
5086cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
509051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
510e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
5115ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerprotected:
512423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit ConstantPointerNull(const PointerType *T)
5135181ac8081dd6c7d78613a5da8829f81a81e9e63Chris Lattner    : Constant(reinterpret_cast<const Type*>(T),
514225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner               Value::ConstantPointerNullVal, 0, 0) {}
515afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen
516051a950000e21935165db56695e35bade668193bGabor Greifprotected:
517051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
518051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
519051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
520051a950000e21935165db56695e35bade668193bGabor Greif  }
5215ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
52293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
523e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static ConstantPointerNull *get(const PointerType *T);
5245ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
52593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
52693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
5270eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner  virtual bool isNullValue() const { return true; }
5280eca13bf58e0a1d6b492ca257d01abc19c4024daChris Lattner
529e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
530e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
5310f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// getType - Specialize the getType() method to always return an PointerType,
5320f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
5330f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  ///
5340f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  inline const PointerType *getType() const {
5350f42ba67771627453187e44fea935d6bab7a8152Chris Lattner    return reinterpret_cast<const PointerType*>(Value::getType());
5360f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  }
5370f42ba67771627453187e44fea935d6bab7a8152Chris Lattner
53893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
539e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
54052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
541a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantPointerNullVal;
5425ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
5434cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
5444cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
545f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
54686e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner/// ConstantExpr - a constant value that is initialized with an expression using
547eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// other constant values.
54886e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner///
549eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// This class uses the standard Instruction opcodes to define the various
550eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// constant expressions.  The Opcode field for the ConstantExpr class is
551eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// maintained in the Value::SubclassData field.
55229ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
5536cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
5546cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
5555133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  friend struct ConvertConstantType<ConstantExpr, Type>;
5569769ab22265b313171d201b5928688524a01bd87Misha Brukman
5576cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
55813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
55913d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : Constant(ty, ConstantExprVal, Ops, NumOps) {
560eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    // Operation type (an Instruction opcode) is stored as the SubclassData.
5619769ab22265b313171d201b5928688524a01bd87Misha Brukman    SubclassData = Opcode;
562eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  }
5635133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
5645133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // These private methods are used by the type resolution code to create
5655133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  // ConstantExprs in intermediate forms.
5665133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getTy(const Type *Ty, unsigned Opcode,
56790fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer                         Constant *C1, Constant *C2);
568ff795a80a35dc99a1971646de11f088e71d0a2c6Nate Begeman  static Constant *getCompareTy(unsigned short pred, Constant *C1,
569ff795a80a35dc99a1971646de11f088e71d0a2c6Nate Begeman                                Constant *C2);
57046a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelectTy(const Type *Ty,
57146a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner                               Constant *C1, Constant *C2, Constant *C3);
5725133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
573fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                      Value* const *Idxs, unsigned NumIdxs);
57449b78a569609881811d905960baa7dd1ab801383Robert Bocchino  static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
57549b78a569609881811d905960baa7dd1ab801383Robert Bocchino                                       Constant *Idx);
576f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino  static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
577f999344fa74199f5acefbc492af2b60e67d0ba24Robert Bocchino                                      Constant *Elt, Constant *Idx);
5789fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
5799fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner                                      Constant *V2, Constant *Mask);
580041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
58181a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                     const unsigned *Idxs, unsigned NumIdxs);
582041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
583041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman                                    Constant *Val,
58481a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                    const unsigned *Idxs, unsigned NumIdxs);
5859769ab22265b313171d201b5928688524a01bd87Misha Brukman
58629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
587fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
588baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
589baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
590baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
5919769ab22265b313171d201b5928688524a01bd87Misha Brukman
59293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Cast constant expr
5935133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
594d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getTrunc   (Constant *C, const Type *Ty);
595d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSExt    (Constant *C, const Type *Ty);
596d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getZExt    (Constant *C, const Type *Ty);
597d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPTrunc (Constant *C, const Type *Ty);
598d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPExtend(Constant *C, const Type *Ty);
599d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getUIToFP  (Constant *C, const Type *Ty);
600d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getSIToFP  (Constant *C, const Type *Ty);
601d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToUI  (Constant *C, const Type *Ty);
602d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getFPToSI  (Constant *C, const Type *Ty);
603d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getPtrToInt(Constant *C, const Type *Ty);
604d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getIntToPtr(Constant *C, const Type *Ty);
605d977d8651a5cd26a3e1088267f31cade405f2adfReid Spencer  static Constant *getBitCast (Constant *C, const Type *Ty);
6063da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
607efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
608efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
609efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
6103da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // @brief Convenience function for getting one of the casting operations
6113da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // using a CastOps opcode.
6123da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  static Constant *getCast(
6133da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    unsigned ops,  ///< The opcode for the conversion
6143da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    Constant *C,   ///< The constant to be converted
6153da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    const Type *Ty ///< The type to which the constant is converted
6163da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  );
6173da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
618848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a ZExt or BitCast cast constant expression
619848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getZExtOrBitCast(
620848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    Constant *C,   ///< The constant to zext or bitcast
621848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    const Type *Ty ///< The type to zext or bitcast C to
622848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
623848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
624848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a SExt or BitCast cast constant expression
625848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getSExtOrBitCast(
62690fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to sext or bitcast
62790fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to sext or bitcast C to
628848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
629848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
630848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a Trunc or BitCast cast constant expression
631848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getTruncOrBitCast(
63290fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to trunc or bitcast
63390fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    const Type *Ty ///< The type to trunc or bitcast C to
634848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
635848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
636887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Create a BitCast or a PtrToInt cast constant expression
637887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  static Constant *getPointerCast(
638887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    Constant *C,   ///< The pointer value to be casted (operand 0)
639887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    const Type *Ty ///< The type to which cast should be made
640887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  );
641887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer
64284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
64384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getIntegerCast(
64484f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
64584f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty, ///< The integer type to cast to
64684f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    bool isSigned   ///< Whether C should be treated as signed or not
64784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
64884f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
64984f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
65084f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getFPCast(
65184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
65284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    const Type *Ty ///< The integer type to cast to
65384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
65484f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
6553da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  /// @brief Return true if this is a convert constant expression
6563da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  bool isCast() const;
6573da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
6584b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  /// @brief Return true if this is a compare constant expression
6594b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  bool isCompare() const;
6604b94f49091be8e26ce6e49db1268458995299b82Reid Spencer
66181a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// @brief Return true if this is an insertvalue or extractvalue expression,
66281a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// and the getIndices() method may be used.
66381a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  bool hasIndices() const;
66481a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman
66546a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  /// Select constant expr
66646a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ///
66746a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
66846a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner    return getSelectTy(V1->getType(), C, V1, V2);
66946a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  }
67046a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
6717be2a120659cc61e554b04815ed3f1d4f234ecafAlkis Evlogimenos  /// getSizeOf constant expr - computes the size of a type in a target
6724647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  /// independent way (Note: the return type is an i64).
67360ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  ///
67460ab1402981a757c5ee785de540a5d0f85839c5dAlkis Evlogimenos  static Constant *getSizeOf(const Type *Ty);
67546a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
6767e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// ConstantExpr::get - Return a binary or shift operator constant expression,
6777e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
6785133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
67990fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
68090fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer
681b5557abcf13a7b375cae683e9ec200d499645d02Nate Begeman  /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
682b5557abcf13a7b375cae683e9ec200d499645d02Nate Begeman  /// expression.
683e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
684e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
6854dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// ConstantExpr::get* - Return some common constants without having to
6864dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// specify the full Instruction::OPCODE identifier.
6874dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  ///
6884dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNeg(Constant *C);
6894dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getNot(Constant *C);
6904dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAdd(Constant *C1, Constant *C2);
6914dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getSub(Constant *C1, Constant *C2);
6924dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getMul(Constant *C1, Constant *C2);
6931628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getUDiv(Constant *C1, Constant *C2);
6941628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getSDiv(Constant *C1, Constant *C2);
6951628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer  static Constant *getFDiv(Constant *C1, Constant *C2);
6960a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
6970a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
6980a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer  static Constant *getFRem(Constant *C1, Constant *C2);
6994dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getAnd(Constant *C1, Constant *C2);
7004dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getOr(Constant *C1, Constant *C2);
7014dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getXor(Constant *C1, Constant *C2);
7024647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
7034647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
704ac80ade1580378e484e24c9f66d2fa5b058e5891Nate Begeman  static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
705ac80ade1580378e484e24c9f66d2fa5b058e5891Nate Begeman  static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
7064dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  static Constant *getShl(Constant *C1, Constant *C2);
7073822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getLShr(Constant *C1, Constant *C2);
7083822ff5c71478c7c90a50ca57045fb676fcb5005Reid Spencer  static Constant *getAShr(Constant *C1, Constant *C2);
70902140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner
7107fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
7117fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// all elements must be Constant's.
7125133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
713fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  static Constant *getGetElementPtr(Constant *C,
714fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Constant* const *IdxList, unsigned NumIdx);
7157fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  static Constant *getGetElementPtr(Constant *C,
716fb11053815ee4b3c6593c12aff06fefea96d7d0aChris Lattner                                    Value* const *IdxList, unsigned NumIdx);
717df7490a27323f194b63db75c0b16708fc7c6cfbeChris Lattner
7189fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getExtractElement(Constant *Vec, Constant *Idx);
7199fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
7209fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
721041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getExtractValue(Constant *Agg,
72281a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                   const unsigned *IdxList, unsigned NumIdx);
723041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValue(Constant *Agg, Constant *Val,
72481a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman                                  const unsigned *IdxList, unsigned NumIdx);
725728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
72624d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
727fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// method returns the negative zero constant for floating point or vector
72824d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  /// floating point types; for all other types, it returns the null value.
72924d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer  static Constant *getZeroValueForNegationExpr(const Type *Ty);
73024d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer
73193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
73293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getNullValue.
73329ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  virtual bool isNullValue() const { return false; }
7349769ab22265b313171d201b5928688524a01bd87Misha Brukman
73593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
736eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  unsigned getOpcode() const { return SubclassData; }
73729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
738728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
739728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// not an ICMP or FCMP constant expression.
740728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  unsigned getPredicate() const;
741728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
74281a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// getIndices - Assert that this is an insertvalue or exactvalue
74381a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// expression and return the list of indices.
74481a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  const SmallVector<unsigned, 4> &getIndices() const;
74581a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman
74693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
747e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
7489769ab22265b313171d201b5928688524a01bd87Misha Brukman
74979ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// getWithOperandReplaced - Return a constant expression identical to this
75079ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// one, but with the specified operand set to the specified value.
75179ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
75279ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
7536b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// getWithOperands - This returns the current constant expression with the
7546b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// operands replaced with the specified values.  The specified operands must
7556b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// match count and type with the existing ones.
756b054bfd3ea50d599fe14bce1b74e39b686724dd9Chris Lattner  Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
757135ccbd74f041caccfbc26a0c935006dfa828e84Bill Wendling    return getWithOperands(&Ops[0], (unsigned)Ops.size());
758b054bfd3ea50d599fe14bce1b74e39b686724dd9Chris Lattner  }
759b054bfd3ea50d599fe14bce1b74e39b686724dd9Chris Lattner  Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
76079ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
761e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner  virtual void destroyConstant();
76240cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
7639769ab22265b313171d201b5928688524a01bd87Misha Brukman
76493aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
76529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
76629ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
767a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantExprVal;
76829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
76929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
77029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
771efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
772efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifstruct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
773efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
774efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
775efe65369a74871c3140a540a6c95ce5d1f080954Gabor GreifDEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
776e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
777e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner//===----------------------------------------------------------------------===//
778e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// UndefValue - 'undef' values are things that do not have specified contents.
779e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// These are used for a variety of purposes, including global variable
780e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// initializers and operands to instructions.  'undef' values can occur with
781e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// any type.
782e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner///
783e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerclass UndefValue : public Constant {
784e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  friend struct ConstantCreator<UndefValue, Type, char>;
785051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
786e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
787e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerprotected:
788423c2260f95883f7c84ac962e58ac66c3a11efacDan Gohman  explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
789051a950000e21935165db56695e35bade668193bGabor Greifprotected:
790051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
791051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
792051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
793051a950000e21935165db56695e35bade668193bGabor Greif  }
794e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerpublic:
795e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// get() - Static factory methods - Return an 'undef' object of the specified
796e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// type.
797e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  ///
798e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static UndefValue *get(const Type *T);
799e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
800e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// isNullValue - Return true if this is the value that would be returned by
801e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// getNullValue.
802e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual bool isNullValue() const { return false; }
803e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
804e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  virtual void destroyConstant();
805e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
806e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
807e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static inline bool classof(const UndefValue *) { return true; }
808e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static bool classof(const Value *V) {
809a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == UndefValueVal;
810e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  }
811e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner};
812e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
813d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
814d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
815009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
816