148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/Constant.h - Constant class definition -------------*- 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//===----------------------------------------------------------------------===//
931bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner//
1031bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner// This file contains the declaration of the Constant class.
1131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner//
1231bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner//===----------------------------------------------------------------------===//
1331bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_CONSTANT_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_CONSTANT_H
1631bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/User.h"
1831bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
19d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
2043ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman  class APInt;
2143ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman
22cd4e5b0417aa729905da91cd7e43f0c4f704be3dChris Lattner  template<typename T> class SmallVectorImpl;
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
24bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// This is an important base class in LLVM. It provides the common facilities
25bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// of all constant values in an LLVM program. A constant is a value that is
26bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// immutable at runtime. Functions are constants because their address is
27bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// immutable. Same with global variables.
28bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer///
29bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// All constants share the capabilities provided in this class. All constants
30bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// can have a null value. They can have an operand list. Constants can be
31bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// simple (integer and floating point values), complex (arrays and structures),
32bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// or expression based (computations yielding a constant value composed of
33bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// only certain operators and other constant values).
34bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer///
35bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// Note that Constants are immutable (once created they never change)
36bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// and are fully shared by structural equivalence.  This means that two
37bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// structurally equivalent constants will always have the same address.
3863e0669b8eb660c793f56acc71a7424eee6c2b9fGabor Greif/// Constants are created on demand as needed and never deleted: thus clients
39bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// don't have to worry about the lifetime of the objects.
40bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// @brief LLVM Constant Representation
4131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattnerclass Constant : public User {
429f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper  void operator=(const Constant &) LLVM_DELETED_FUNCTION;
439f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper  Constant(const Constant &) LLVM_DELETED_FUNCTION;
442d24e2a396a1d211baaeedf32148a3b657240170David Blaikie  virtual void anchor();
45aad3fb7362aff151e97ad457005ea3f2872fe868Owen Anderson
4631bcdb822fe9133b1973de51519d34e5813a6184Chris Lattnerprotected:
47db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Constant(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
4813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling    : User(ty, vty, Ops, NumOps) {}
4931bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
5031bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner  void destroyConstantImpl();
5131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattnerpublic:
5226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// isNullValue - Return true if this is the value that would be returned by
5326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// getNullValue.
54032c6eb1c4d36a9e906f5efc0ada76c952225a4fChris Lattner  bool isNullValue() const;
5531bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
564c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem  /// isAllOnesValue - Return true if this is the value that would be returned by
574c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem  /// getAllOnesValue.
584c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem  bool isAllOnesValue() const;
594c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem
60fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson  /// isNegativeZeroValue - Return true if the value is what would be returned
61fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson  /// by getZeroValueForNegation.
62b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner  bool isNegativeZeroValue() const;
63fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson
64935e35d2b9f889566207b76a7026b63a1619742cShuxin Yang  /// Return true if the value is negative zero or null value.
65935e35d2b9f889566207b76a7026b63a1619742cShuxin Yang  bool isZeroValue() const;
66935e35d2b9f889566207b76a7026b63a1619742cShuxin Yang
6735b89fa63957c4d6468cd50af2a91ed92db44d49Chris Lattner  /// canTrap - Return true if evaluation of this constant could trap.  This is
6835b89fa63957c4d6468cd50af2a91ed92db44d49Chris Lattner  /// true for things like constant expressions that could divide by zero.
6935b89fa63957c4d6468cd50af2a91ed92db44d49Chris Lattner  bool canTrap() const;
7031bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
711839858983dcbaf1808a6a37f5cd64d92981f27eHans Wennborg  /// isThreadDependent - Return true if the value can vary between threads.
721839858983dcbaf1808a6a37f5cd64d92981f27eHans Wennborg  bool isThreadDependent() const;
731839858983dcbaf1808a6a37f5cd64d92981f27eHans Wennborg
744a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner  /// isConstantUsed - Return true if the constant has users other than constant
754a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner  /// exprs and other dangling things.
764a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner  bool isConstantUsed() const;
774a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner
78083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  enum PossibleRelocationsTy {
79083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner    NoRelocation = 0,
80083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner    LocalRelocation = 1,
81083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner    GlobalRelocations = 2
82083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  };
83083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner
847cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  /// getRelocationInfo - This method classifies the entry according to
857cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  /// whether or not it may generate a relocation entry.  This must be
867cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  /// conservative, so if it might codegen to a relocatable entry, it should say
877cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  /// so.  The return values are:
887cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  ///
89083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///  NoRelocation: This constant pool entry is guaranteed to never have a
90083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///     relocation applied to it (because it holds a simple constant like
91083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///     '4').
92083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///  LocalRelocation: This entry has relocations, but the entries are
93083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///     guaranteed to be resolvable by the static linker, so the dynamic
94083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///     linker will never see them.
95083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  ///  GlobalRelocations: This entry may have arbitrary relocations.
967cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  ///
977cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner  /// FIXME: This really should not be in VMCore.
98083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  PossibleRelocationsTy getRelocationInfo() const;
997cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner
1003d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  /// getAggregateElement - For aggregates (struct/array/vector) return the
1013d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  /// constant that corresponds to the specified element if possible, or null if
1023d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  /// not.  This can return null if the element index is a ConstantExpr, or if
1033d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  /// 'this' is a constant expr.
1043d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  Constant *getAggregateElement(unsigned Elt) const;
1053d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner  Constant *getAggregateElement(Constant *Elt) const;
1062333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands
1072333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands  /// getSplatValue - If this is a splat vector constant, meaning that all of
1082333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands  /// the elements have the same value, return that value. Otherwise return 0.
1092333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands  Constant *getSplatValue() const;
1102333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands
1112333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands  /// If C is a constant integer then return its value, otherwise C must be a
1122333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands  /// vector of constant integers, all equal, and the common value is returned.
1132333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands  const APInt &getUniqueInteger() const;
1142333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands
11526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// destroyConstant - Called if some element of this constant is no longer
11626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// valid.  At this point only other constants may be on the use_list for this
11726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// constant.  Any constants on our Use list must also be destroy'd.  The
11826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// implementation must be sure to remove the constant from the list of
11926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// available cached constants.  Implementations should call
12026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// destroyConstantImpl as the last thing they do, to destroy all users and
12126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// delete this.
12250bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper  virtual void destroyConstant() { llvm_unreachable("Not reached!"); }
1239769ab22265b313171d201b5928688524a01bd87Misha Brukman
124c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  //// Methods for support type inquiry through isa, cast, and dyn_cast:
12531bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner  static inline bool classof(const Value *V) {
126a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman    return V->getValueID() >= ConstantFirstVal &&
127a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman           V->getValueID() <= ConstantLastVal;
12831bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner  }
1294a717d4a478ec40eb3b42078843af2291203ea21Vikram S. Adve
130c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// replaceUsesOfWithOnConstant - This method is a special form of
131c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// User::replaceUsesOfWith (which does not work on constants) that does work
132c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// on constants.  Basically this method goes through the trouble of building
133c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// a new constant that is equivalent to the current one, with all uses of
134c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// From replaced with uses of To.  After this construction is completed, all
135c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// of the users of 'this' are replaced to use the new constant, and then
136c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// 'this' is deleted.  In general, you should not call this method, instead,
137c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// use Value::replaceAllUsesWith, which automatically dispatches to this
138c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  /// method as needed.
139c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  ///
14070cfe13f19e91a595808ed6c6ff7e87ff0dccd64Chris Lattner  virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
141c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner    // Provide a default implementation for constants (like integers) that
142c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner    // cannot use any other values.  This cannot be called at runtime, but needs
143c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner    // to be here to avoid link errors.
144c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner    assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
145c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner           "implemented for all constants that have operands!");
14650bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper    llvm_unreachable("Constants that do not have operands cannot be using "
14750bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper                     "'From'!");
148c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner  }
14950bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper
150db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getNullValue(Type* Ty);
15150bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper
152a757a0d52aca0311576306a03865dacba5d12e56Duncan Sands  /// @returns the value for an integer or vector of integer constant of the
153a757a0d52aca0311576306a03865dacba5d12e56Duncan Sands  /// given type that has all its bits set to true.
154a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson  /// @brief Get the all ones value
155db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getAllOnesValue(Type* Ty);
15643ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman
15743ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman  /// getIntegerValue - Return the value for an integer or pointer constant,
15843ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman  /// or a vector thereof, with the given scalar value.
159db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  static Constant *getIntegerValue(Type* Ty, const APInt &V);
16013fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner
16113fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner  /// removeDeadConstantUsers - If there are any dead constant users dangling
16213fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner  /// off of this constant, remove them.  This method is useful for clients
16313fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner  /// that want to check to see if a global is unused, but don't want to deal
16413fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner  /// with potentially dead constants hanging off of the globals.
16513fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner  void removeDeadConstantUsers() const;
16631bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner};
16731bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner
168d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
169d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
17031bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#endif
171