148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
100f8b53f19d29013ab18f3d444cea1e6305405611Dan Gohman/// @file
110f8b53f19d29013ab18f3d444cea1e6305405611Dan Gohman/// This file contains the declarations for the subclasses of Constant,
120b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// which represent the different flavors of constant values that live in LLVM.
130b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// Note that Constants are immutable (once created they never change) and are
140b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// fully shared by structural equivalence.  This means that two structurally
150b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// equivalent constants will always have the same address.  Constant's are
160b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// created on demand as needed and never deleted: thus clients don't have to
170b5a504d105514178c80b886321221fbe5ac1131Reid Spencer/// worry about the lifetime of the objects.
18009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
19009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
20009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#ifndef LLVM_CONSTANTS_H
2231bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#define LLVM_CONSTANTS_H
23009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2431bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#include "llvm/Constant.h"
25efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif#include "llvm/OperandTraits.h"
26532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer#include "llvm/ADT/APInt.h"
27343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen#include "llvm/ADT/APFloat.h"
282ca5c8644e6c35b3a7910a576ed89cddb7b82c3bChris Lattner#include "llvm/ADT/ArrayRef.h"
29009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
31d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
32009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass ArrayType;
33174101e13a6f24f6f2737e043194f0ffae925bb3Benjamin Kramerclass IntegerType;
34009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerclass StructType;
354cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattnerclass PointerType;
369d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass VectorType;
3727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerclass SequentialType;
38009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
396cc89aad25155ecd93b5318414851aa46351196dChris Lattnertemplate<class ConstantClass, class TypeClass, class ValType>
406cc89aad25155ecd93b5318414851aa46351196dChris Lattnerstruct ConstantCreator;
415133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattnertemplate<class ConstantClass, class TypeClass>
422cb395eae71dacda49ca3fe758618fc3e0701659Talinstruct ConstantArrayCreator;
432cb395eae71dacda49ca3fe758618fc3e0701659Talintemplate<class ConstantClass, class TypeClass>
445f4ac848d94b0a92e19ac7f2b3d0284d7d323173Devang Patelstruct ConvertConstantType;
455133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
4686e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
4724d6da5fedcf39891f7d8c5b031c01324b3db545Reid Spencer/// This is the shared class of boolean and integer constants. This class
486b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// represents both boolean and integral constants.
496b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng/// @brief Class for constant integers.
506b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Shengclass ConstantInt : public Constant {
512d24e2a396a1d211baaeedf32148a3b657240170David Blaikie  virtual void anchor();
52051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
536b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
54db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  ConstantInt(IntegerType *Ty, const APInt& V);
55532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  APInt Val;
56051a950000e21935165db56695e35bade668193bGabor Greifprotected:
57051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
58051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
59051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
60051a950000e21935165db56695e35bade668193bGabor Greif  }
61994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattnerpublic:
622c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static ConstantInt *getTrue(LLVMContext &Context);
632c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static ConstantInt *getFalse(LLVMContext &Context);
64db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getTrue(Type *Ty);
65db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getFalse(Type *Ty);
665defacc6e605f4651c6300237cef8e9bb2eb6d0eOwen Anderson
67eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// If Ty is a vector type, return a Constant with a splat of the given
68eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// value. Otherwise return a ConstantInt for the given value.
69db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
70eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson
71eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// Return a ConstantInt with the specified integer value for the specified
72eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// type. If the type is wider than 64 bits, the value will be zero-extended
73eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// to fit the type, unless isSigned is true, in which case the value will
74eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// be interpreted as a 64-bit signed integer and sign-extended to fit
75eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// the type.
76eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// @brief Get a ConstantInt for a specific value.
77db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static ConstantInt *get(IntegerType *Ty, uint64_t V,
78eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson                          bool isSigned = false);
79eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson
80eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// Return a ConstantInt with the specified value for the specified type. The
81eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
82eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
83eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// signed value for the type Ty.
84eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// @brief Get a ConstantInt for a specific signed value.
85db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
86db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getSigned(Type *Ty, int64_t V);
87eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson
88eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// Return a ConstantInt with the specified value and an implied Type. The
89eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// type is the integer type that corresponds to the bit width of the value.
902c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static ConstantInt *get(LLVMContext &Context, const APInt &V);
910e81f660dbb7c437a3c95427bb495eb18822e42cErick Tryzelaar
920e81f660dbb7c437a3c95427bb495eb18822e42cErick Tryzelaar  /// Return a ConstantInt constructed from the string strStart with the given
930e81f660dbb7c437a3c95427bb495eb18822e42cErick Tryzelaar  /// radix.
94db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static ConstantInt *get(IntegerType *Ty, StringRef Str,
950e81f660dbb7c437a3c95427bb495eb18822e42cErick Tryzelaar                          uint8_t radix);
96eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson
97eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// If Ty is a vector type, return a Constant with a splat of the given
98eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  /// value. Otherwise return a ConstantInt for the given value.
99db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(Type* Ty, const APInt& V);
100eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson
101532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// Return the constant as an APInt value reference. This allows clients to
102532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// obtain a copy of the value, with all its precision in tact.
103532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @brief Return the constant's value.
1042c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  inline const APInt &getValue() const {
105532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val;
106532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  }
107e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner
108e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  /// getBitWidth - Return the bitwidth of this constant.
109e51f2a59d93336b2be46b9663af317b96a7e045bChris Lattner  unsigned getBitWidth() const { return Val.getBitWidth(); }
110532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
1110b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit unsigned integer value after it
112532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// has been zero extended as appropriate for the type of this constant. Note
113532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// that this method can assert if the value does not fit in 64 bits.
114532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
1150b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Return the zero extended value.
116a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline uint64_t getZExtValue() const {
117532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getZExtValue();
118a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
119c2dfb8bb909b0ba08733be94821513aef9467fa0Chris Lattner
1200b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// Return the constant as a 64-bit integer value after it has been sign
1217b048b391f9ded004170a67780d1c6a5eedeb604Bob Wilson  /// extended as appropriate for the type of this constant. Note that
122532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// this method can assert if the value does not fit in 64 bits.
123532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  /// @deprecated
1246c6b6a77c0f68d5a41e94d8ef0b1093089e683faChris Lattner  /// @brief Return the sign extended value.
125a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  inline int64_t getSExtValue() const {
126532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.getSExtValue();
127a58ef7b6526ee30f4793fe91686899c602b4f9b9Chris Lattner  }
128532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer
1290b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// A helper method that can be used to determine if the constant contained
1300b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// within is equal to a constant.  This only works for very small values,
1310b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// because this is all that can be represented with all types.
1320b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if this constant's value is same as an unsigned char.
133532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer  bool equalsInt(uint64_t V) const {
134b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    return Val == V;
1352ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner  }
1362ce0cbbbe1d53448ddbdaaedf0da28f68dd92ea0Chris Lattner
137c10305743c313558405079452138f03124e87581Reid Spencer  /// getType - Specialize the getType() method to always return an IntegerType,
138c10305743c313558405079452138f03124e87581Reid Spencer  /// which reduces the amount of casting needed in parts of the compiler.
139c10305743c313558405079452138f03124e87581Reid Spencer  ///
140db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  inline IntegerType *getType() const {
141db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    return reinterpret_cast<IntegerType*>(Value::getType());
142c10305743c313558405079452138f03124e87581Reid Spencer  }
143c10305743c313558405079452138f03124e87581Reid Spencer
1440b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// This static method returns true if the type Ty is big enough to
1450b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// represent the value V. This can be used to avoid having the get method
1466d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// assert when V is larger than Ty can represent. Note that there are two
1476d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// versions of this method, one for unsigned and one for signed integers.
1486d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// Although ConstantInt canonicalizes everything to an unsigned integer,
1496d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// the signed version avoids callers having to convert a signed quantity
1506d0483a7a365338e17537366c6c934edef2ef726Reid Spencer  /// to the appropriate unsigned type before calling the method.
1510b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if V is a valid value for type Ty
1520b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @brief Determine if the value is in range for the given type.
153db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static bool isValueValidForType(Type *Ty, uint64_t V);
154db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static bool isValueValidForType(Type *Ty, int64_t V);
15593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner
156b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner  bool isNegative() const { return Val.isNegative(); }
1575ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
15834da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// This is just a convenience method to make client code smaller for a
15934da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common code. It also correctly performs the comparison without the
16034da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
16134da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isZero() const {
16234da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer    return Val == 0;
16334da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  }
16434da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer
16537eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// This is just a convenience method to make client code smaller for a
16634da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// common case. It also correctly performs the comparison without the
16734da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  /// potential for an assertion from getZExtValue().
16837eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  /// @brief Determine if the value is one.
16934da0ac28f7bfe737f92d3a098697a6ebc09a25eReid Spencer  bool isOne() const {
17037eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer    return Val == 1;
17137eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer  }
17237eeaa729bc70258166a675ee00bbe95e608d519Reid Spencer
1736b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff every bit in this constant is set
1746b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// to true.
1750b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this constant's bits are all set to true.
1766b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is all ones.
1774c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem  bool isMinusOne() const {
178532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    return Val.isAllOnesValue();
1796b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  }
1801680312867fffeb9369800949b809e0b9e29a914Chris Lattner
1816b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the largest
1826b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by the constant's type.
1830b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true iff this is the largest value that may be represented
1840b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// by this type.
1856b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is maximal.
186579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMaxValue(bool isSigned) const {
187532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
188532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxSignedValue();
189532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
190532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMaxValue();
191994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
192994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
1936b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// This function will return true iff this constant represents the smallest
1946b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// value that may be represented by this constant's type.
1950b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// @returns true if this is the smallest value that may be represented by
1960b5a504d105514178c80b886321221fbe5ac1131Reid Spencer  /// this type.
1976b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng  /// @brief Determine if the value is minimal.
198579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer  bool isMinValue(bool isSigned) const {
199532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    if (isSigned)
200532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinSignedValue();
201532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer    else
202532d0ce208809545dc46d2de70c0384c036470d1Reid Spencer      return Val.isMinValue();
203994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner  }
204994b9f337ba9e15f3fa537c2a2d443b890c2a617Chris Lattner
205b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// This function will return true iff this constant represents a value with
206b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// active bits bigger than 64 bits or a value greater than the given uint64_t
207b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// value.
208b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @returns true iff this constant is greater or equal to the given number.
209b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  /// @brief Determine if the value is greater or equal to the given number.
21032c371f0dff32dc127f6701c8737aad46b207d1eDavid Greene  bool uge(uint64_t Num) const {
2110642f75836960afd547a6b98fad33026d0c355beZhou Sheng    return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
212b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
213b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
214edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// getLimitedValue - If the value is smaller than the specified limit,
215edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// return it, otherwise return the limit value.  This causes the value
216edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// to saturate to the limit.
217edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// @returns the min of the value of the constant and the specified value
218edf22b97d0d525ed4ec89eda1708c24c45c54b57Dan Gohman  /// @brief Get the constant's value with a saturation limit
21999b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
22099b1b38818e3a1628e39af40500bad47d5207d0dChris Lattner    return Val.getLimitedValue(Limit);
221b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng  }
222b422d2d9e5632585adb0c1855b217f3123cbdfefZhou Sheng
223b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
224b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer  static inline bool classof(const ConstantInt *) { return true; }
22552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
226a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantIntVal;
2275ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
228009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
229009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
230009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
23186e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
23293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantFP - Floating Point Values [float, double]
23393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
234e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantFP : public Constant {
235343e770983dcf53a1ea2dfca4e04d28ebc41138aDale Johannesen  APFloat Val;
2362d24e2a396a1d211baaeedf32148a3b657240170David Blaikie  virtual void anchor();
237051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
238e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
23912ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramer  friend class LLVMContextImpl;
2403e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
241db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  ConstantFP(Type *Ty, const APFloat& V);
242051a950000e21935165db56695e35bade668193bGabor Greifprotected:
243051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
244051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
245051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
246051a950000e21935165db56695e35bade668193bGabor Greif  }
247009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
2486f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
2496f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// method returns the negative zero constant for floating point or vector
2506f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// floating point types; for all other types, it returns the null value.
251db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getZeroValueForNegation(Type *Ty);
2526f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson
2536f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// get() - This returns a ConstantFP, or a vector containing a splat of a
2546f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// ConstantFP, for the specified value in the specified type.  This should
2556f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// only be used for simple constant values like 2.0/1.0 etc, that are
2566f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson  /// known-valid both as host double and as the target format.
257db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(Type* Ty, double V);
258db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(Type* Ty, StringRef Str);
2592c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static ConstantFP *get(LLVMContext &Context, const APFloat &V);
260db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static ConstantFP *getNegativeZero(Type* Ty);
261db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
2626f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson
26393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// isValueValidForType - return true if Ty is big enough to represent V.
264db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static bool isValueValidForType(Type *Ty, const APFloat &V);
265b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner  inline const APFloat &getValueAPF() const { return Val; }
2665ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
267e832693acbfc713bcaf44720efa8149e93a38027Dan Gohman  /// isZero - Return true if the value is positive or negative zero.
268e832693acbfc713bcaf44720efa8149e93a38027Dan Gohman  bool isZero() const { return Val.isZero(); }
269e832693acbfc713bcaf44720efa8149e93a38027Dan Gohman
270b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner  /// isNegative - Return true if the sign bit is set.
271b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner  bool isNegative() const { return Val.isNegative(); }
272b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner
273e832693acbfc713bcaf44720efa8149e93a38027Dan Gohman  /// isNaN - Return true if the value is a NaN.
274e832693acbfc713bcaf44720efa8149e93a38027Dan Gohman  bool isNaN() const { return Val.isNaN(); }
275e832693acbfc713bcaf44720efa8149e93a38027Dan Gohman
276f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// isExactlyValue - We don't rely on operator== working on double values, as
277f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
278f0fd6845eee799f62ac69297c9fd7086a0156cfaChris Lattner  /// As such, this method can be used to do an exact bit-for-bit comparison of
27943421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// two floating point values.  The version with a double operand is retained
28043421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen  /// because it's so convenient to write isExactlyValue(2.0), but please use
281def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner  /// it only for simple constants.
2822c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  bool isExactlyValue(const APFloat &V) const;
28343421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen
284f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  bool isExactlyValue(double V) const {
2858aaec36fa760c9dd50627e397ca85798d9eb6e79Dale Johannesen    bool ignored;
286beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    // convert is not supported on this type
287beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen    if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
288beb5be03320f9bbe6a4a92b0c7e1e0ebf3ef865dDale Johannesen      return false;
289def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    APFloat FV(V);
2908aaec36fa760c9dd50627e397ca85798d9eb6e79Dale Johannesen    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
291def286408f4ddd0511164d231dfeddcc5fe12008Chris Lattner    return isExactlyValue(FV);
292f04afdbb48568ef09f11fd10ac03426101f2dbf8Dale Johannesen  }
29393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
294e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantFP *) { return true; }
29552eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
296a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantFPVal;
2975ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
298009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
299009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
30086e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
30140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner/// ConstantAggregateZero - All zero aggregate value
30240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner///
30340bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerclass ConstantAggregateZero : public Constant {
304051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
30540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
30640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerprotected:
307db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  explicit ConstantAggregateZero(Type *ty)
30813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
309051a950000e21935165db56695e35bade668193bGabor Greifprotected:
310051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
311051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
312051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
313051a950000e21935165db56695e35bade668193bGabor Greif  }
31440bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattnerpublic:
315ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  static ConstantAggregateZero *get(Type *Ty);
3169e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson
31704fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
31840bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
319ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getSequentialElement - If this CAZ has array or vector type, return a zero
320ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// with the right element type.
3213d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  Constant *getSequentialElement() const;
322ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner
323ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getStructElement - If this CAZ has struct type, return a zero with the
324ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// right element type for the specified element.
3253d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  Constant *getStructElement(unsigned Elt) const;
326ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner
327ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getElementValue - Return a zero of the right value for the specified GEP
328ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// index.
3293d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  Constant *getElementValue(Constant *C) const;
330df39028607ca751f0a3f50a76144464b825ff97aChris Lattner
331df39028607ca751f0a3f50a76144464b825ff97aChris Lattner  /// getElementValue - Return a zero of the right value for the specified GEP
332df39028607ca751f0a3f50a76144464b825ff97aChris Lattner  /// index.
3333d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  Constant *getElementValue(unsigned Idx) const;
334df39028607ca751f0a3f50a76144464b825ff97aChris Lattner
33540bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
33640bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  ///
33752eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const ConstantAggregateZero *) { return true; }
33852eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
339a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantAggregateZeroVal;
34040bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner  }
34140bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner};
34240bbeb5d077b1bc2b933d8c8628024dfa9b428c3Chris Lattner
343009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
34486e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
34593aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantArray - Constant Array Declarations
34693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
347e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantArray : public Constant {
3482cb395eae71dacda49ca3fe758618fc3e0701659Talin  friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
349e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
3503e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
351166579e287a38d907acafc24243146e9f3ee9799Jay Foad  ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
352009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
3531fd7096407d5e598ed3366a1141548e71273f1c5Owen Anderson  // ConstantArray accessors
354db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
3551fd7096407d5e598ed3366a1141548e71273f1c5Owen Anderson
356efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
357efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
358efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
35993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
36093aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
36193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  ///
362db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  inline ArrayType *getType() const {
363db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    return reinterpret_cast<ArrayType*>(Value::getType());
364682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
365009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
36604fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
36740cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
368e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
36993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
370e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantArray *) { return true; }
37152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
372a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantArrayVal;
3735ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
374009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
375009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
376efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
37767c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foadstruct OperandTraits<ConstantArray> :
37867c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foad  public VariadicOperandTraits<ConstantArray> {
379efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
380efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
381ff7782bcc9235b1dc4c7fcb0497c52e4717eeffcJay FoadDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
382009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
38386e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
384e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner// ConstantStruct - Constant Struct Declarations
385009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
386e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattnerclass ConstantStruct : public Constant {
3872cb395eae71dacda49ca3fe758618fc3e0701659Talin  friend struct ConstantArrayCreator<ConstantStruct, StructType>;
388e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
3893e22bed21110e9ca591a476ff2feea8790c67b13Chris Lattnerprotected:
390166579e287a38d907acafc24243146e9f3ee9799Jay Foad  ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
391009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattnerpublic:
3928fa3338ed2400c1352b137613d2c2c70d1ead695Owen Anderson  // ConstantStruct accessors
393db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(StructType *T, ArrayRef<Constant*> V);
394db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *get(StructType *T, ...) END_WITH_NULL;
395b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner
396b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  /// getAnon - Return an anonymous struct that has the specified
397b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  /// elements.  If the struct is possibly empty, then you must specify a
398b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  /// context.
399b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
400b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner    return get(getTypeForElements(V, Packed), V);
401b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  }
402b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  static Constant *getAnon(LLVMContext &Ctx,
403b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner                           ArrayRef<Constant*> V, bool Packed = false) {
404b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner    return get(getTypeForElements(Ctx, V, Packed), V);
405b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  }
406c332fba8285e35a5a11463c34795af84f3960759Nick Lewycky
407b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  /// getTypeForElements - Return an anonymous struct type to use for a constant
408b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  /// with the specified set of elements.  The list must not be empty.
409b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  static StructType *getTypeForElements(ArrayRef<Constant*> V,
410b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner                                        bool Packed = false);
411b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  /// getTypeForElements - This version of the method allows an empty list.
412b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner  static StructType *getTypeForElements(LLVMContext &Ctx,
413b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner                                        ArrayRef<Constant*> V,
414b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner                                        bool Packed = false);
415b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner
416efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
417efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
418efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
41993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getType() specialization - Reduce amount of casting...
420c78631f0e2cfd7808233176c638c7f9a50ccaff2Chris Lattner  ///
421db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  inline StructType *getType() const {
422db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    return reinterpret_cast<StructType*>(Value::getType());
423682ea21397436e49eabbe13432e527869f07b0e0Chris Lattner  }
424009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
42504fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
42640cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
4279769ab22265b313171d201b5928688524a01bd87Misha Brukman
42893aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
429e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantStruct *) { return true; }
43052eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
431a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantStructVal;
4325ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
433009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
434009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
435efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
43667c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foadstruct OperandTraits<ConstantStruct> :
43767c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foad  public VariadicOperandTraits<ConstantStruct> {
438efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
439efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
440ff7782bcc9235b1dc4c7fcb0497c52e4717eeffcJay FoadDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
441efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
442fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
443fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner//===----------------------------------------------------------------------===//
4449d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer/// ConstantVector - Constant Vector Declarations
445715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke///
4469d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencerclass ConstantVector : public Constant {
4472cb395eae71dacda49ca3fe758618fc3e0701659Talin  friend struct ConstantArrayCreator<ConstantVector, VectorType>;
4489d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
449715c90ba524e736190a6380695ab337eeb5148beBrian Gaekeprotected:
450166579e287a38d907acafc24243146e9f3ee9799Jay Foad  ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
451715c90ba524e736190a6380695ab337eeb5148beBrian Gaekepublic:
452af7ec975870f92245f1f1484ac80a1e2db6a0afaOwen Anderson  // ConstantVector accessors
4532ca5c8644e6c35b3a7910a576ed89cddb7b82c3bChris Lattner  static Constant *get(ArrayRef<Constant*> V);
454af7ec975870f92245f1f1484ac80a1e2db6a0afaOwen Anderson
4553c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  /// getSplat - Return a ConstantVector with the specified constant in each
4563c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  /// element.
4573c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  static Constant *getSplat(unsigned NumElts, Constant *Elt);
4583c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner
459efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
460efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
461efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
462fa73ea2d9fd785a214256ca44488407b26c5a3dbDan Gohman  /// getType - Specialize the getType() method to always return a VectorType,
463715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// which reduces the amount of casting needed in parts of the compiler.
464715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  ///
465db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  inline VectorType *getType() const {
466db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    return reinterpret_cast<VectorType*>(Value::getType());
467715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
468fa30182eb84ee70d9f52120d653d63374b99cee6Jim Laskey
4693b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// getSplatValue - If this is a splat constant, meaning that all of the
4703b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman  /// elements have the same value, return that value. Otherwise return NULL.
4717681c6da606efcc392f76b8acea8301cb5fc7a0aDuncan Sands  Constant *getSplatValue() const;
4723b7cf0aabafc1c7a89a5a978d4a4307c891027ceDan Gohman
47304fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
47440cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
475715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
476715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  /// Methods for support type inquiry through isa, cast, and dyn_cast:
4779d6565a5b1fbc4286d6ee638d8f47a3171a9ed7eReid Spencer  static inline bool classof(const ConstantVector *) { return true; }
478715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  static bool classof(const Value *V) {
479a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantVectorVal;
480715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke  }
481715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke};
482715c90ba524e736190a6380695ab337eeb5148beBrian Gaeke
483efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
48467c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foadstruct OperandTraits<ConstantVector> :
48567c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foad  public VariadicOperandTraits<ConstantVector> {
486efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
487efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
488ff7782bcc9235b1dc4c7fcb0497c52e4717eeffcJay FoadDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
489efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
49086e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner//===----------------------------------------------------------------------===//
49193aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner/// ConstantPointerNull - a constant pointer value that points to null
49293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner///
49348babfa60d3ee1854f33ad93e07abb2b22cf8ab8Chris Lattnerclass ConstantPointerNull : public Constant {
494051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
495e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
4965ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerprotected:
497db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  explicit ConstantPointerNull(PointerType *T)
498db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    : Constant(reinterpret_cast<Type*>(T),
499225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner               Value::ConstantPointerNullVal, 0, 0) {}
500afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen
501051a950000e21935165db56695e35bade668193bGabor Greifprotected:
502051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
503051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
504051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
505051a950000e21935165db56695e35bade668193bGabor Greif  }
5065ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattnerpublic:
50793aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// get() - Static factory methods - Return objects of the specified value
508db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static ConstantPointerNull *get(PointerType *T);
5095ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner
51004fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
511e0fbb497ee44f86ec108e4ff8787a848c6ee8655Chris Lattner
5120f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// getType - Specialize the getType() method to always return an PointerType,
5130f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
5140f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  ///
515db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  inline PointerType *getType() const {
516db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    return reinterpret_cast<PointerType*>(Value::getType());
5170f42ba67771627453187e44fea935d6bab7a8152Chris Lattner  }
5180f42ba67771627453187e44fea935d6bab7a8152Chris Lattner
51993aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
520e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  static inline bool classof(const ConstantPointerNull *) { return true; }
52152eb13c58e63e277e83f94f3015dfc0e39a211eeChris Lattner  static bool classof(const Value *V) {
522a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantPointerNullVal;
5235ef35fdb72f7800e65e27311e38f028e3d400693Chris Lattner  }
5244cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner};
52527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
52627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner//===----------------------------------------------------------------------===//
5278cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// ConstantDataSequential - A vector or array constant whose element type is a
5288cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// simple 1/2/4/8-byte integer or float/double, and whose elements are just
5298cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// simple data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
5308cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// operands because it stores all of the elements of the constant as densely
5318cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// packed data, instead of as Value*'s.
5328cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner///
5338cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// This is the common base class of ConstantDataArray and ConstantDataVector.
53427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner///
53527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerclass ConstantDataSequential : public Constant {
53627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  friend class LLVMContextImpl;
53727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// DataElements - A pointer to the bytes underlying this constant (which is
53827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// owned by the uniquing StringMap).
53927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  const char *DataElements;
54027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
54127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// Next - This forms a link list of ConstantDataSequential nodes that have
54227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// the same value but different type.  For example, 0,0,0,1 could be a 4
54327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// element array of i8, or a 1-element array of i32.  They'll both end up in
54427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// the same StringMap bucket, linked up.
54527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ConstantDataSequential *Next;
54627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
54727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ConstantDataSequential(const ConstantDataSequential &);    // DO NOT IMPLEMENT
54827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerprotected:
54927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
55057af41e78c244951459c001dc37a92837a1c8f66Chris Lattner    : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
55127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ~ConstantDataSequential() { delete Next; }
55227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
55327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static Constant *getImpl(StringRef Bytes, Type *Ty);
55427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
55527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerprotected:
55627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  // allocate space for exactly zero operands.
55727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  void *operator new(size_t s) {
55827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return User::operator new(s, 0);
55927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
56027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerpublic:
56127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
562ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
563ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// formed with a vector or array of the specified element type.
564ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// ConstantDataArray only works with normal float and int types that are
565ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// stored densely in memory, not with things like i42 or x86_f80.
566ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  static bool isElementTypeCompatible(const Type *Ty);
567ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner
56827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getElementAsInteger - If this is a sequential container of integers (of
56927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// any size), return the specified element in the low bits of a uint64_t.
57027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  uint64_t getElementAsInteger(unsigned i) const;
57127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
57227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getElementAsAPFloat - If this is a sequential container of floating point
57327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// type, return the specified element as an APFloat.
57427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  APFloat getElementAsAPFloat(unsigned i) const;
57527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
57627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getElementAsFloat - If this is an sequential container of floats, return
57727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// the specified element as a float.
57827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  float getElementAsFloat(unsigned i) const;
57927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
58027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getElementAsDouble - If this is an sequential container of doubles, return
5818cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner  /// the specified element as a double.
58227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  double getElementAsDouble(unsigned i) const;
58327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
58427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getElementAsConstant - Return a Constant for a specified index's element.
58527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// Note that this has to compute a new constant to return, so it isn't as
58627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// efficient as getElementAsInteger/Float/Double.
58727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  Constant *getElementAsConstant(unsigned i) const;
58827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
58927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getType - Specialize the getType() method to always return a
59027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// SequentialType, which reduces the amount of casting needed in parts of the
59127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// compiler.
59227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  inline SequentialType *getType() const {
59327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return reinterpret_cast<SequentialType*>(Value::getType());
59427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
59545bb5c5333fba7e9909cca178d785bf2c1d2b0c1Chris Lattner
59645bb5c5333fba7e9909cca178d785bf2c1d2b0c1Chris Lattner  /// getElementType - Return the element type of the array/vector.
59745bb5c5333fba7e9909cca178d785bf2c1d2b0c1Chris Lattner  Type *getElementType() const;
5981ee0ecf84a07693c3a517ba030fac8ac1f9f3fbcChris Lattner
5991ee0ecf84a07693c3a517ba030fac8ac1f9f3fbcChris Lattner  /// getNumElements - Return the number of elements in the array or vector.
6001ee0ecf84a07693c3a517ba030fac8ac1f9f3fbcChris Lattner  unsigned getNumElements() const;
60145bb5c5333fba7e9909cca178d785bf2c1d2b0c1Chris Lattner
602ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getElementByteSize - Return the size (in bytes) of each element in the
603ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// array/vector.  The size of the elements is known to be a multiple of one
604ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// byte.
605ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  uint64_t getElementByteSize() const;
60627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
60762339073127df4579905f551f61c132cf21d2aadChris Lattner
60862339073127df4579905f551f61c132cf21d2aadChris Lattner  /// isString - This method returns true if this is an array of i8.
60962339073127df4579905f551f61c132cf21d2aadChris Lattner  bool isString() const;
61062339073127df4579905f551f61c132cf21d2aadChris Lattner
61162339073127df4579905f551f61c132cf21d2aadChris Lattner  /// isCString - This method returns true if the array "isString", ends with a
61262339073127df4579905f551f61c132cf21d2aadChris Lattner  /// nul byte, and does not contains any other nul bytes.
61362339073127df4579905f551f61c132cf21d2aadChris Lattner  bool isCString() const;
61462339073127df4579905f551f61c132cf21d2aadChris Lattner
61562339073127df4579905f551f61c132cf21d2aadChris Lattner  /// getAsString - If this array is isString(), then this method returns the
61662339073127df4579905f551f61c132cf21d2aadChris Lattner  /// array as a StringRef.  Otherwise, it asserts out.
61762339073127df4579905f551f61c132cf21d2aadChris Lattner  ///
6189e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner  StringRef getAsString() const {
6199e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner    assert(isString() && "Not a string");
6209e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner    return getRawDataValues();
6219e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner  }
62262339073127df4579905f551f61c132cf21d2aadChris Lattner
62362339073127df4579905f551f61c132cf21d2aadChris Lattner  /// getAsCString - If this array is isCString(), then this method returns the
62462339073127df4579905f551f61c132cf21d2aadChris Lattner  /// array (without the trailing null byte) as a StringRef. Otherwise, it
62562339073127df4579905f551f61c132cf21d2aadChris Lattner  /// asserts out.
62662339073127df4579905f551f61c132cf21d2aadChris Lattner  ///
62762339073127df4579905f551f61c132cf21d2aadChris Lattner  StringRef getAsCString() const {
62862339073127df4579905f551f61c132cf21d2aadChris Lattner    assert(isCString() && "Isn't a C string");
62962339073127df4579905f551f61c132cf21d2aadChris Lattner    StringRef Str = getAsString();
63062339073127df4579905f551f61c132cf21d2aadChris Lattner    return Str.substr(0, Str.size()-1);
63162339073127df4579905f551f61c132cf21d2aadChris Lattner  }
63262339073127df4579905f551f61c132cf21d2aadChris Lattner
6339e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner  /// getRawDataValues - Return the raw, underlying, bytes of this data.  Note
6349e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner  /// that this is an extremely tricky thing to work with, as it exposes the
6359e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner  /// host endianness of the data elements.
6369e631da253bff90d9da5ace21bd73f1c838e72ebChris Lattner  StringRef getRawDataValues() const;
63762339073127df4579905f551f61c132cf21d2aadChris Lattner
638ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  virtual void destroyConstant();
639ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner
64027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
64127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ///
64227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static bool classof(const ConstantDataSequential *) { return true; }
64327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static bool classof(const Value *V) {
64427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return V->getValueID() == ConstantDataArrayVal ||
64527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner           V->getValueID() == ConstantDataVectorVal;
64627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
64745bb5c5333fba7e9909cca178d785bf2c1d2b0c1Chris Lattnerprivate:
64845bb5c5333fba7e9909cca178d785bf2c1d2b0c1Chris Lattner  const char *getElementPointer(unsigned Elt) const;
64927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner};
65027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
65127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner//===----------------------------------------------------------------------===//
6528cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// ConstantDataArray - An array constant whose element type is a simple
6538cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// 1/2/4/8-byte integer or float/double, and whose elements are just simple
6548cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
6558cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// operands because it stores all of the elements of the constant as densely
6568cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// packed data, instead of as Value*'s.
65727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerclass ConstantDataArray : public ConstantDataSequential {
65827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  void *operator new(size_t, unsigned);            // DO NOT IMPLEMENT
65927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ConstantDataArray(const ConstantDataArray &);    // DO NOT IMPLEMENT
66027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  virtual void anchor();
66127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  friend class ConstantDataSequential;
66227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  explicit ConstantDataArray(Type *ty, const char *Data)
66327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
66427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerprotected:
66527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  // allocate space for exactly zero operands.
66627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  void *operator new(size_t s) {
66727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return User::operator new(s, 0);
66827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
66927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerpublic:
67027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
67127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// get() constructors - Return a constant with array type with an element
67227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// count and element type matching the ArrayRef passed in.  Note that this
67327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// can return a ConstantAggregateZero object.
67432100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
67532100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
67632100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
67732100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
67832100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
67932100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
68032100602432657b188db603ca3ab50b487938027Chris Lattner
68132100602432657b188db603ca3ab50b487938027Chris Lattner  /// getString - This method constructs a CDS and initializes it with a text
68232100602432657b188db603ca3ab50b487938027Chris Lattner  /// string. The default behavior (AddNull==true) causes a null terminator to
68332100602432657b188db603ca3ab50b487938027Chris Lattner  /// be placed at the end of the array (increasing the length of the string by
68432100602432657b188db603ca3ab50b487938027Chris Lattner  /// one more than the StringRef would normally indicate.  Pass AddNull=false
68532100602432657b188db603ca3ab50b487938027Chris Lattner  /// to disable this behavior.
68632100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *getString(LLVMContext &Context, StringRef Initializer,
68732100602432657b188db603ca3ab50b487938027Chris Lattner                             bool AddNull = true);
68832100602432657b188db603ca3ab50b487938027Chris Lattner
68927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getType - Specialize the getType() method to always return an ArrayType,
69027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
69127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ///
69227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  inline ArrayType *getType() const {
69327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return reinterpret_cast<ArrayType*>(Value::getType());
69427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
69527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
69627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
69727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ///
69827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static bool classof(const ConstantDataArray *) { return true; }
69927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static bool classof(const Value *V) {
70027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return V->getValueID() == ConstantDataArrayVal;
70127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
70227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner};
70327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
70427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner//===----------------------------------------------------------------------===//
7058cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// ConstantDataVector - A vector constant whose element type is a simple
7068cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// 1/2/4/8-byte integer or float/double, and whose elements are just simple
7078cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
7088cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// operands because it stores all of the elements of the constant as densely
7098cf27efe4000c194efec4004afa83d12031c6d55Chris Lattner/// packed data, instead of as Value*'s.
71027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerclass ConstantDataVector : public ConstantDataSequential {
71127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  void *operator new(size_t, unsigned);              // DO NOT IMPLEMENT
71227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ConstantDataVector(const ConstantDataVector &);    // DO NOT IMPLEMENT
71327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  virtual void anchor();
71427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  friend class ConstantDataSequential;
71527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  explicit ConstantDataVector(Type *ty, const char *Data)
71627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
71727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerprotected:
71827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  // allocate space for exactly zero operands.
71927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  void *operator new(size_t s) {
72027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return User::operator new(s, 0);
72127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
72227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattnerpublic:
72327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
72427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// get() constructors - Return a constant with vector type with an element
72527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// count and element type matching the ArrayRef passed in.  Note that this
72627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// can return a ConstantAggregateZero object.
72732100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
72832100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
72932100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
73032100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
73132100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
73232100602432657b188db603ca3ab50b487938027Chris Lattner  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
73327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
7343c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  /// getSplat - Return a ConstantVector with the specified constant in each
7353c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  /// element.  The specified constant has to be a of a compatible type (i8/i16/
7363c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
7373c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner  static Constant *getSplat(unsigned NumElts, Constant *Elt);
7383c2c954e0baf4c5029f87277d75cc6ca38916f71Chris Lattner
739e150e2df424b162710b36a1ec5ef90270ffb435bChris Lattner  /// getSplatValue - If this is a splat constant, meaning that all of the
740e150e2df424b162710b36a1ec5ef90270ffb435bChris Lattner  /// elements have the same value, return that value. Otherwise return NULL.
741e150e2df424b162710b36a1ec5ef90270ffb435bChris Lattner  Constant *getSplatValue() const;
742e150e2df424b162710b36a1ec5ef90270ffb435bChris Lattner
74327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// getType - Specialize the getType() method to always return a VectorType,
74427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// which reduces the amount of casting needed in parts of the compiler.
74527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ///
74627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  inline VectorType *getType() const {
74727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return reinterpret_cast<VectorType*>(Value::getType());
74827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
74927dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
75027dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
75127dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  ///
75227dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static bool classof(const ConstantDataVector *) { return true; }
75327dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  static bool classof(const Value *V) {
75427dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    return V->getValueID() == ConstantDataVectorVal;
75527dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner  }
75627dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner};
75727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
75827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner
7594cfb15331652f9a2f7e5f755485a2f6eb87a20c6Chris Lattner
7602ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner/// BlockAddress - The address of a basic block.
7612ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner///
7622ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattnerclass BlockAddress : public Constant {
7632ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
7642ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  void *operator new(size_t s) { return User::operator new(s, 2); }
7652ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  BlockAddress(Function *F, BasicBlock *BB);
7662ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattnerpublic:
7672ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  /// get - Return a BlockAddress for the specified function and basic block.
7682ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  static BlockAddress *get(Function *F, BasicBlock *BB);
7692ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7702ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  /// get - Return a BlockAddress for the specified basic block.  The basic
7712ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  /// block must be embedded into a function.
7722ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  static BlockAddress *get(BasicBlock *BB);
7732ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7742ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  /// Transparently provide more efficient getOperand methods.
7750eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
7762ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7772ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  Function *getFunction() const { return (Function*)Op<0>().get(); }
7782ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
7792ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7802ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  virtual void destroyConstant();
7812ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
7822ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7832ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
7842ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  static inline bool classof(const BlockAddress *) { return true; }
7852ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  static inline bool classof(const Value *V) {
7862ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner    return V->getValueID() == BlockAddressVal;
7872ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner  }
7882ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner};
7892ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7902ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattnertemplate <>
79167c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foadstruct OperandTraits<BlockAddress> :
79267c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foad  public FixedNumOperandTraits<BlockAddress, 2> {
7932ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner};
794f4ba6c710c298fe9b492b9cde82ce5efd46afd5dChris Lattner
795ff7782bcc9235b1dc4c7fcb0497c52e4717eeffcJay FoadDEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
7962ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner
7971c5730facaefbacd66badea74c83571a16611bd4Jin-Gu Kang
7982ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner//===----------------------------------------------------------------------===//
79986e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner/// ConstantExpr - a constant value that is initialized with an expression using
800eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// other constant values.
80186e100bd9bf1a1a838902c46de926c79c68cdde3Chris Lattner///
802eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// This class uses the standard Instruction opcodes to define the various
803eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// constant expressions.  The Opcode field for the ConstantExpr class is
804eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner/// maintained in the Value::SubclassData field.
80529ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adveclass ConstantExpr : public Constant {
8066cc89aad25155ecd93b5318414851aa46351196dChris Lattner  friend struct ConstantCreator<ConstantExpr,Type,
8076cc89aad25155ecd93b5318414851aa46351196dChris Lattner                            std::pair<unsigned, std::vector<Constant*> > >;
8085f4ac848d94b0a92e19ac7f2b3d0284d7d323173Devang Patel  friend struct ConvertConstantType<ConstantExpr, Type>;
8099769ab22265b313171d201b5928688524a01bd87Misha Brukman
8106cc89aad25155ecd93b5318414851aa46351196dChris Lattnerprotected:
811db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
81213d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : Constant(ty, ConstantExprVal, Ops, NumOps) {
813eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner    // Operation type (an Instruction opcode) is stored as the SubclassData.
814cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner    setValueSubclassData(Opcode);
815eecfea4da6771b01997d3dc9648e699b476a3079Chris Lattner  }
8165133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner
81729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Advepublic:
818fb242b6edc3d92daf49c7d5b2c19d81447aa61bcChris Lattner  // Static methods to construct a ConstantExpr of different kinds.  Note that
819baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // these methods may return a object that is not an instance of the
820baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // ConstantExpr class, because they will attempt to fold the constant
821baf850a51a253a39c60196343d756197bfbb27dbChris Lattner  // expression into something simpler if possible.
8229769ab22265b313171d201b5928688524a01bd87Misha Brukman
823baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson  /// getAlignOf constant expr - computes the alignment of a type in a target
82406ed3e788ab3d71350b7cc7d077f4bd9a2b57799Dan Gohman  /// independent way (Note: the return type is an i64).
825db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getAlignOf(Type *Ty);
826baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson
827a5a23046cbea6d011f84c97ac61743cb8f6a58c6Dan Gohman  /// getSizeOf constant expr - computes the (alloc) size of a type (in
828a5a23046cbea6d011f84c97ac61743cb8f6a58c6Dan Gohman  /// address-units, not bits) in a target independent way (Note: the return
829a5a23046cbea6d011f84c97ac61743cb8f6a58c6Dan Gohman  /// type is an i64).
830baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson  ///
831db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getSizeOf(Type *Ty);
8323778f21b17e675c2fd1600901361fa4a73888ebaDan Gohman
8332544a1de666ad7197c755b4dab6419f42df01f65Dan Gohman  /// getOffsetOf constant expr - computes the offset of a struct field in a
8342544a1de666ad7197c755b4dab6419f42df01f65Dan Gohman  /// target independent way (Note: the return type is an i64).
8352544a1de666ad7197c755b4dab6419f42df01f65Dan Gohman  ///
836db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
8372544a1de666ad7197c755b4dab6419f42df01f65Dan Gohman
8382544a1de666ad7197c755b4dab6419f42df01f65Dan Gohman  /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
8392544a1de666ad7197c755b4dab6419f42df01f65Dan Gohman  /// which supports any aggregate type, and any Constant index.
8403778f21b17e675c2fd1600901361fa4a73888ebaDan Gohman  ///
841db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
842baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson
84381baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
8442c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getFNeg(Constant *C);
8452c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getNot(Constant *C);
84681baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getAdd(Constant *C1, Constant *C2,
84781baf14fdfa29c22a08d609144c285169e23a247Chris Lattner                          bool HasNUW = false, bool HasNSW = false);
8482c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getFAdd(Constant *C1, Constant *C2);
84981baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getSub(Constant *C1, Constant *C2,
85081baf14fdfa29c22a08d609144c285169e23a247Chris Lattner                          bool HasNUW = false, bool HasNSW = false);
8512c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getFSub(Constant *C1, Constant *C2);
85281baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getMul(Constant *C1, Constant *C2,
85381baf14fdfa29c22a08d609144c285169e23a247Chris Lattner                          bool HasNUW = false, bool HasNSW = false);
8542c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getFMul(Constant *C1, Constant *C2);
85574f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
85674f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
8572c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getFDiv(Constant *C1, Constant *C2);
8582c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getURem(Constant *C1, Constant *C2);
8592c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getSRem(Constant *C1, Constant *C2);
8602c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getFRem(Constant *C1, Constant *C2);
8612c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getAnd(Constant *C1, Constant *C2);
8622c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getOr(Constant *C1, Constant *C2);
8632c5060dbde67422420cd81f3ce3f15127641c87dChris Lattner  static Constant *getXor(Constant *C1, Constant *C2);
86481baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getShl(Constant *C1, Constant *C2,
86581baf14fdfa29c22a08d609144c285169e23a247Chris Lattner                          bool HasNUW = false, bool HasNSW = false);
86674f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
86774f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
868db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getTrunc   (Constant *C, Type *Ty);
869db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getSExt    (Constant *C, Type *Ty);
870db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getZExt    (Constant *C, Type *Ty);
871db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getFPTrunc (Constant *C, Type *Ty);
872db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getFPExtend(Constant *C, Type *Ty);
873db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getUIToFP  (Constant *C, Type *Ty);
874db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getSIToFP  (Constant *C, Type *Ty);
875db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getFPToUI  (Constant *C, Type *Ty);
876db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getFPToSI  (Constant *C, Type *Ty);
877db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getPtrToInt(Constant *C, Type *Ty);
878db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getIntToPtr(Constant *C, Type *Ty);
879db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getBitCast (Constant *C, Type *Ty);
8803da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
88181baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
88281baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
88381baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNSWAdd(Constant *C1, Constant *C2) {
88481baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getAdd(C1, C2, false, true);
88581baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
88681baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNUWAdd(Constant *C1, Constant *C2) {
88781baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getAdd(C1, C2, true, false);
88881baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
88981baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNSWSub(Constant *C1, Constant *C2) {
89081baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getSub(C1, C2, false, true);
89181baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
89281baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNUWSub(Constant *C1, Constant *C2) {
89381baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getSub(C1, C2, true, false);
89481baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
89581baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNSWMul(Constant *C1, Constant *C2) {
89681baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getMul(C1, C2, false, true);
89781baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
89881baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNUWMul(Constant *C1, Constant *C2) {
89981baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getMul(C1, C2, true, false);
90081baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
90181baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNSWShl(Constant *C1, Constant *C2) {
90281baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getShl(C1, C2, false, true);
90381baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
90481baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  static Constant *getNUWShl(Constant *C1, Constant *C2) {
90581baf14fdfa29c22a08d609144c285169e23a247Chris Lattner    return getShl(C1, C2, true, false);
90681baf14fdfa29c22a08d609144c285169e23a247Chris Lattner  }
90774f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getExactSDiv(Constant *C1, Constant *C2) {
90874f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner    return getSDiv(C1, C2, true);
90974f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  }
91074f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getExactUDiv(Constant *C1, Constant *C2) {
91174f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner    return getUDiv(C1, C2, true);
91274f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  }
91374f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getExactAShr(Constant *C1, Constant *C2) {
91474f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner    return getAShr(C1, C2, true);
91574f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  }
91674f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  static Constant *getExactLShr(Constant *C1, Constant *C2) {
91774f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner    return getLShr(C1, C2, true);
91874f5c5abeafeaabefab470390ce0e7a36754c592Chris Lattner  }
9196e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman
920c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  /// getBinOpIdentity - Return the identity for the given binary operation,
921c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  /// i.e. a constant C such that X op C = X and C op X = X for every X.  It
922ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  /// returns null if the operator doesn't have an identity.
923c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
924c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
925ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  /// getBinOpAbsorber - Return the absorbing element for the given binary
926ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  /// operation, i.e. a constant C such that X op C = C and C op X = C for
927ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  /// every X.  For example, this returns zero for integer multiplication.
928ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  /// It returns null if the operator doesn't have an absorbing element.
929ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
930ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands
931efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  /// Transparently provide more efficient getOperand methods.
932efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
933efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
9343da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // @brief Convenience function for getting one of the casting operations
9353da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  // using a CastOps opcode.
9363da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  static Constant *getCast(
9373da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    unsigned ops,  ///< The opcode for the conversion
9383da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    Constant *C,   ///< The constant to be converted
939db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty ///< The type to which the constant is converted
9403da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  );
9413da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
942848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a ZExt or BitCast cast constant expression
943848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getZExtOrBitCast(
944848414e49c7600e3002a4366de52d03a9638b327Reid Spencer    Constant *C,   ///< The constant to zext or bitcast
945db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty ///< The type to zext or bitcast C to
946848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
947848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
948848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a SExt or BitCast cast constant expression
949848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getSExtOrBitCast(
95090fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to sext or bitcast
951db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty ///< The type to sext or bitcast C to
952848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
953848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
954848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  // @brief Create a Trunc or BitCast cast constant expression
955848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  static Constant *getTruncOrBitCast(
95690fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer    Constant *C,   ///< The constant to trunc or bitcast
957db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty ///< The type to trunc or bitcast C to
958848414e49c7600e3002a4366de52d03a9638b327Reid Spencer  );
959848414e49c7600e3002a4366de52d03a9638b327Reid Spencer
960887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  /// @brief Create a BitCast or a PtrToInt cast constant expression
961887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  static Constant *getPointerCast(
962887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer    Constant *C,   ///< The pointer value to be casted (operand 0)
963db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty ///< The type to which cast should be made
964887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer  );
965887ee06f94c5b36ddbe22f54ff816fbc13bb2703Reid Spencer
96684f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
96784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getIntegerCast(
96884f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
969db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty, ///< The integer type to cast to
97084f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    bool isSigned   ///< Whether C should be treated as signed or not
97184f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
97284f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
97384f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
97484f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  static Constant *getFPCast(
97584f3eab017d56b6854155f2350759dc77ac48aadReid Spencer    Constant *C,    ///< The integer constant to be casted
976db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Type *Ty ///< The integer type to cast to
97784f3eab017d56b6854155f2350759dc77ac48aadReid Spencer  );
97884f3eab017d56b6854155f2350759dc77ac48aadReid Spencer
9793da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  /// @brief Return true if this is a convert constant expression
9803da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer  bool isCast() const;
9813da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer
9824b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  /// @brief Return true if this is a compare constant expression
9834b94f49091be8e26ce6e49db1268458995299b82Reid Spencer  bool isCompare() const;
9844b94f49091be8e26ce6e49db1268458995299b82Reid Spencer
98581a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// @brief Return true if this is an insertvalue or extractvalue expression,
98681a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// and the getIndices() method may be used.
98781a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  bool hasIndices() const;
98881a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman
989e6992f728a94654e43269580a10a667f18dadba9Dan Gohman  /// @brief Return true if this is a getelementptr expression and all
990e6992f728a94654e43269580a10a667f18dadba9Dan Gohman  /// the index operands are compile-time known integers within the
991e6992f728a94654e43269580a10a667f18dadba9Dan Gohman  /// corresponding notional static array extents. Note that this is
992e6992f728a94654e43269580a10a667f18dadba9Dan Gohman  /// not equivalant to, a subset of, or a superset of the "inbounds"
993e6992f728a94654e43269580a10a667f18dadba9Dan Gohman  /// property.
994e6992f728a94654e43269580a10a667f18dadba9Dan Gohman  bool isGEPWithNoNotionalOverIndexing() const;
995e6992f728a94654e43269580a10a667f18dadba9Dan Gohman
99646a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  /// Select constant expr
99746a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner  ///
998eaf79809e8a78bf407bb26b9d7c59f32c142188aChris Lattner  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
99946a57d8f144af82be46ac925f1c53a3405bcfb3aChris Lattner
1000baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson  /// get - Return a binary or shift operator constant expression,
10017e208904ae8eeef888c3a6eecd38daa779ca36c9Chris Lattner  /// folding if possible.
10025133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
1003f8dbee7cea072eb63ae343759975109553697bcbDan Gohman  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1004f8dbee7cea072eb63ae343759975109553697bcbDan Gohman                       unsigned Flags = 0);
100590fdf9c6debbdcc61076a1e020c3dff4dc71d838Reid Spencer
10067f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky  /// @brief Return an ICmp or FCmp comparison operator constant expression.
1007e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
1008e8e4605021141d689493132a9c7c6fce6294937fChris Lattner
1009baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson  /// get* - Return some common constants without having to
10104dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  /// specify the full Instruction::OPCODE identifier.
10114dcb5401e4f6248eb546e4da444ba8e8cf306666Chris Lattner  ///
10124647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
10134647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
101402140b025d821ca8cefe5086f90f4d8e0c52ae5bChris Lattner
1015cda60cec1642fa7c4d9e34511c14fc37d8055733Jay Foad  /// Getelementptr form.  Value* is only accepted for convenience;
10167fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner  /// all elements must be Constant's.
10175133a5cf2ee42b5a4d4c7af2d90b41af769cc307Chris Lattner  ///
101804fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  static Constant *getGetElementPtr(Constant *C,
1019dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                    ArrayRef<Constant *> IdxList,
1020eaf79809e8a78bf407bb26b9d7c59f32c142188aChris Lattner                                    bool InBounds = false) {
1021dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1022dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                            IdxList.size()),
1023dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                            InBounds);
1024eaf79809e8a78bf407bb26b9d7c59f32c142188aChris Lattner  }
102504fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  static Constant *getGetElementPtr(Constant *C,
1026dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                    Constant *Idx,
1027dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                    bool InBounds = false) {
1028dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    // This form of the function only exists to avoid ambiguous overload
1029dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    // warnings about whether to convert Idx to ArrayRef<Constant *> or
1030dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    // ArrayRef<Value *>.
1031dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1032dab3d29605a5c83db41b28176273ef55961120c1Jay Foad  }
1033dab3d29605a5c83db41b28176273ef55961120c1Jay Foad  static Constant *getGetElementPtr(Constant *C,
1034dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                    ArrayRef<Value *> IdxList,
10351f78d51be69964b38c5fc76ceb2147b737d09857Chris Lattner                                    bool InBounds = false);
1036e2574d3215c412a15763d26aee9aa5d856764c2cDan Gohman
10376e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman  /// Create an "inbounds" getelementptr. See the documentation for the
10386e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman  /// "inbounds" flag in LangRef.html for details.
10396e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman  static Constant *getInBoundsGetElementPtr(Constant *C,
1040dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                            ArrayRef<Constant *> IdxList) {
1041dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    return getGetElementPtr(C, IdxList, true);
1042dab3d29605a5c83db41b28176273ef55961120c1Jay Foad  }
1043dab3d29605a5c83db41b28176273ef55961120c1Jay Foad  static Constant *getInBoundsGetElementPtr(Constant *C,
1044dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                            Constant *Idx) {
1045dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    // This form of the function only exists to avoid ambiguous overload
1046dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    // warnings about whether to convert Idx to ArrayRef<Constant *> or
1047dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    // ArrayRef<Value *>.
1048dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    return getGetElementPtr(C, Idx, true);
10491f78d51be69964b38c5fc76ceb2147b737d09857Chris Lattner  }
10506e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman  static Constant *getInBoundsGetElementPtr(Constant *C,
1051dab3d29605a5c83db41b28176273ef55961120c1Jay Foad                                            ArrayRef<Value *> IdxList) {
1052dab3d29605a5c83db41b28176273ef55961120c1Jay Foad    return getGetElementPtr(C, IdxList, true);
10531f78d51be69964b38c5fc76ceb2147b737d09857Chris Lattner  }
10546e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman
10559fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getExtractElement(Constant *Vec, Constant *Idx);
10569fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
10579fc18d24ae525a0047718d28e7a8735e8582ddb2Chris Lattner  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1058fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad  static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1059041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman  static Constant *getInsertValue(Constant *Agg, Constant *Val,
1060fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad                                  ArrayRef<unsigned> Idxs);
1061728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
106293aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcode - Return the opcode at the root of this constant expression
1063cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  unsigned getOpcode() const { return getSubclassDataFromValue(); }
106429ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
1065728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1066728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  /// not an ICMP or FCMP constant expression.
1067728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer  unsigned getPredicate() const;
1068728b6db6fbd3caee7fa25b377f4592160476bb9cReid Spencer
106981a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// getIndices - Assert that this is an insertvalue or exactvalue
107081a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman  /// expression and return the list of indices.
1071d30aa5a1edac5256573e8d76dd155df3d3fdec84Jay Foad  ArrayRef<unsigned> getIndices() const;
107281a0c0b44e582baca8b68754a7fcabfc3aef2e7aDan Gohman
107393aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// getOpcodeName - Return a string representation for an opcode.
1074e8e4605021141d689493132a9c7c6fce6294937fChris Lattner  const char *getOpcodeName() const;
10759769ab22265b313171d201b5928688524a01bd87Misha Brukman
107679ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// getWithOperandReplaced - Return a constant expression identical to this
107779ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  /// one, but with the specified operand set to the specified value.
107879ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
107979ce587cda552abf85f02282cf6cfe36afcaafd8Chris Lattner
10806b8408e9103328b82affbfdf3d3bca236c61f514Chris Lattner  /// getWithOperands - This returns the current constant expression with the
10811afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// operands replaced with the specified values.  The specified array must
10821afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// have the same number of operands as our current one.
10831afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
10841afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner    return getWithOperands(Ops, getType());
10851afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  }
10861afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner
10871afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// getWithOperands - This returns the current constant expression with the
10881afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// operands replaced with the specified values and with the specified result
10891afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// type.  The specified array must have the same number of operands as our
10901afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// current one.
1091db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
10921afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner
109304fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
109440cdedecf5d871a83363cb7d69a6f6eed525651cChris Lattner  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
10959769ab22265b313171d201b5928688524a01bd87Misha Brukman
109693aeea3748b11fa213b345edf3c86275a4936a31Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
109729ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const ConstantExpr *) { return true; }
109829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  static inline bool classof(const Value *V) {
1099a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == ConstantExprVal;
110029ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve  }
1101cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner
1102cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattnerprivate:
1103cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  // Shadow Value::setValueSubclassData with a private forwarding method so that
1104cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  // subclasses cannot accidentally use it.
1105cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  void setValueSubclassData(unsigned short D) {
1106cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner    Value::setValueSubclassData(D);
1107cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  }
110829ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve};
110929ab9f83481cd21abf3055c7c32ea1df953ae167Vikram S. Adve
1110efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greiftemplate <>
111167c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foadstruct OperandTraits<ConstantExpr> :
111267c619ba3eae68dcdb3f9340d82b33173aa0c256Jay Foad  public VariadicOperandTraits<ConstantExpr, 1> {
1113efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
1114efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
1115ff7782bcc9235b1dc4c7fcb0497c52e4717eeffcJay FoadDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1116e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
1117e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner//===----------------------------------------------------------------------===//
1118e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// UndefValue - 'undef' values are things that do not have specified contents.
1119e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// These are used for a variety of purposes, including global variable
1120e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner/// initializers and operands to instructions.  'undef' values can occur with
1121fd726176886b946ff4eaaf85bd254c5fbbacabfdDan Gohman/// any first-class type.
1122fd726176886b946ff4eaaf85bd254c5fbbacabfdDan Gohman///
1123fd726176886b946ff4eaaf85bd254c5fbbacabfdDan Gohman/// Undef values aren't exactly constants; if they have multiple uses, they
1124fd726176886b946ff4eaaf85bd254c5fbbacabfdDan Gohman/// can appear to have different bit patterns at each use. See
1125fd726176886b946ff4eaaf85bd254c5fbbacabfdDan Gohman/// LangRef.html#undefvalues for details.
1126e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner///
1127e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerclass UndefValue : public Constant {
1128051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1129e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
1130e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerprotected:
1131db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1132051a950000e21935165db56695e35bade668193bGabor Greifprotected:
1133051a950000e21935165db56695e35bade668193bGabor Greif  // allocate space for exactly zero operands
1134051a950000e21935165db56695e35bade668193bGabor Greif  void *operator new(size_t s) {
1135051a950000e21935165db56695e35bade668193bGabor Greif    return User::operator new(s, 0);
1136051a950000e21935165db56695e35bade668193bGabor Greif  }
1137e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattnerpublic:
1138e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// get() - Static factory methods - Return an 'undef' object of the specified
1139e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// type.
1140e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  ///
1141db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static UndefValue *get(Type *T);
1142e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
1143ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getSequentialElement - If this Undef has array or vector type, return a
1144ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// undef with the right element type.
11453d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  UndefValue *getSequentialElement() const;
1146ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner
1147ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getStructElement - If this undef has struct type, return a undef with the
1148ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// right element type for the specified element.
11493d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  UndefValue *getStructElement(unsigned Elt) const;
1150ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner
1151ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// getElementValue - Return an undef of the right value for the specified GEP
1152ff2b7f3cd66793e36a36500acb36b9d1a0489d4cChris Lattner  /// index.
11533d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  UndefValue *getElementValue(Constant *C) const;
1154df39028607ca751f0a3f50a76144464b825ff97aChris Lattner
1155df39028607ca751f0a3f50a76144464b825ff97aChris Lattner  /// getElementValue - Return an undef of the right value for the specified GEP
1156df39028607ca751f0a3f50a76144464b825ff97aChris Lattner  /// index.
11573d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  UndefValue *getElementValue(unsigned Idx) const;
1158df39028607ca751f0a3f50a76144464b825ff97aChris Lattner
115904fb7c36a9977127f32558dc01c39a9c2388bc39Owen Anderson  virtual void destroyConstant();
1160e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner
1161e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1162e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static inline bool classof(const UndefValue *) { return true; }
1163e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  static bool classof(const Value *V) {
1164a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() == UndefValueVal;
1165e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner  }
1166e1e922e09007f15aa01b438e5cf6a69c5a67afe4Chris Lattner};
1167fd726176886b946ff4eaaf85bd254c5fbbacabfdDan Gohman
1168d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
1169d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
1170009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
1171