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 { 42ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void operator=(const Constant &) = delete; 43ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Constant(const Constant &) = delete; 4436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void anchor() override; 4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 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(); 5137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines void replaceUsesOfWithOnConstantImpl(Constant *Replacement); 5237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 5331bcdb822fe9133b1973de51519d34e5813a6184Chris Lattnerpublic: 5426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// isNullValue - Return true if this is the value that would be returned by 5526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getNullValue. 56032c6eb1c4d36a9e906f5efc0ada76c952225a4fChris Lattner bool isNullValue() const; 5731bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner 5837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief Returns true if the value is one. 5937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines bool isOneValue() const; 6037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 614c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem /// isAllOnesValue - Return true if this is the value that would be returned by 624c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem /// getAllOnesValue. 634c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem bool isAllOnesValue() const; 644c7c0f23533565c7e2ddf71e01bf50f2aede5f1bNadav Rotem 65fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson /// isNegativeZeroValue - Return true if the value is what would be returned 66fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson /// by getZeroValueForNegation. 67b447387726ff94ddb2a23408f39e22714c42f79bChris Lattner bool isNegativeZeroValue() const; 68fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson 69935e35d2b9f889566207b76a7026b63a1619742cShuxin Yang /// Return true if the value is negative zero or null value. 70935e35d2b9f889566207b76a7026b63a1619742cShuxin Yang bool isZeroValue() const; 71935e35d2b9f889566207b76a7026b63a1619742cShuxin Yang 7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines /// \brief Return true if the value is not the smallest signed value. 7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines bool isNotMinSignedValue() const; 7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 75c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines /// \brief Return true if the value is the smallest signed value. 76c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines bool isMinSignedValue() const; 77c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 7835b89fa63957c4d6468cd50af2a91ed92db44d49Chris Lattner /// canTrap - Return true if evaluation of this constant could trap. This is 7935b89fa63957c4d6468cd50af2a91ed92db44d49Chris Lattner /// true for things like constant expressions that could divide by zero. 8035b89fa63957c4d6468cd50af2a91ed92db44d49Chris Lattner bool canTrap() const; 8131bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner 821839858983dcbaf1808a6a37f5cd64d92981f27eHans Wennborg /// isThreadDependent - Return true if the value can vary between threads. 831839858983dcbaf1808a6a37f5cd64d92981f27eHans Wennborg bool isThreadDependent() const; 841839858983dcbaf1808a6a37f5cd64d92981f27eHans Wennborg 85c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines /// Return true if the value is dependent on a dllimport variable. 86c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines bool isDLLImportDependent() const; 87c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 884a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner /// isConstantUsed - Return true if the constant has users other than constant 894a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner /// exprs and other dangling things. 904a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner bool isConstantUsed() const; 914a7642ec9c33bcf8ed65231f382fb6d9a0f46f3aChris Lattner 92083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner enum PossibleRelocationsTy { 93083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner NoRelocation = 0, 94083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner LocalRelocation = 1, 95083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner GlobalRelocations = 2 96083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner }; 97083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner 987cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// getRelocationInfo - This method classifies the entry according to 997cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// whether or not it may generate a relocation entry. This must be 1007cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// conservative, so if it might codegen to a relocatable entry, it should say 1017cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// so. The return values are: 1027cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// 103083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// NoRelocation: This constant pool entry is guaranteed to never have a 104083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// relocation applied to it (because it holds a simple constant like 105083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// '4'). 106083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// LocalRelocation: This entry has relocations, but the entries are 107083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// guaranteed to be resolvable by the static linker, so the dynamic 108083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// linker will never see them. 109083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner /// GlobalRelocations: This entry may have arbitrary relocations. 1107cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// 1117cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner /// FIXME: This really should not be in VMCore. 112083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner PossibleRelocationsTy getRelocationInfo() const; 1137cf12c7efd37dc12c3ed536a3f4c373dddac2b85Chris Lattner 1143d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner /// getAggregateElement - For aggregates (struct/array/vector) return the 1153d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner /// constant that corresponds to the specified element if possible, or null if 1163d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner /// not. This can return null if the element index is a ConstantExpr, or if 1173d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner /// 'this' is a constant expr. 1183d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner Constant *getAggregateElement(unsigned Elt) const; 1193d5ed2250e78c3e849232398cb550238155dbb72Chris Lattner Constant *getAggregateElement(Constant *Elt) const; 1202333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands 1212333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands /// getSplatValue - If this is a splat vector constant, meaning that all of 1222333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands /// the elements have the same value, return that value. Otherwise return 0. 1232333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands Constant *getSplatValue() const; 1242333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands 1252333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands /// If C is a constant integer then return its value, otherwise C must be a 1262333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands /// vector of constant integers, all equal, and the common value is returned. 1272333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands const APInt &getUniqueInteger() const; 1282333e29be441d9d55920651e0b2add23ab0c1613Duncan Sands 12926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// destroyConstant - Called if some element of this constant is no longer 13026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// valid. At this point only other constants may be on the use_list for this 13126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// constant. Any constants on our Use list must also be destroy'd. The 13226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// implementation must be sure to remove the constant from the list of 13326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// available cached constants. Implementations should call 13426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// destroyConstantImpl as the last thing they do, to destroy all users and 13526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// delete this. 13650bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper virtual void destroyConstant() { llvm_unreachable("Not reached!"); } 1379769ab22265b313171d201b5928688524a01bd87Misha Brukman 138c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner //// Methods for support type inquiry through isa, cast, and dyn_cast: 13931bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner static inline bool classof(const Value *V) { 140a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman return V->getValueID() >= ConstantFirstVal && 141a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman V->getValueID() <= ConstantLastVal; 14231bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner } 1434a717d4a478ec40eb3b42078843af2291203ea21Vikram S. Adve 144c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// replaceUsesOfWithOnConstant - This method is a special form of 145c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// User::replaceUsesOfWith (which does not work on constants) that does work 146c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// on constants. Basically this method goes through the trouble of building 147c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// a new constant that is equivalent to the current one, with all uses of 148c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// From replaced with uses of To. After this construction is completed, all 149c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// of the users of 'this' are replaced to use the new constant, and then 150c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// 'this' is deleted. In general, you should not call this method, instead, 151c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// use Value::replaceAllUsesWith, which automatically dispatches to this 152c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// method as needed. 153c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner /// 15470cfe13f19e91a595808ed6c6ff7e87ff0dccd64Chris Lattner virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) { 155c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner // Provide a default implementation for constants (like integers) that 156c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner // cannot use any other values. This cannot be called at runtime, but needs 157c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner // to be here to avoid link errors. 158c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be " 159c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner "implemented for all constants that have operands!"); 16050bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper llvm_unreachable("Constants that do not have operands cannot be using " 16150bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper "'From'!"); 162c251f9e0ae46dafec666541b8311af878da27f06Chris Lattner } 16350bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper 164db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner static Constant *getNullValue(Type* Ty); 16550bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper 166a757a0d52aca0311576306a03865dacba5d12e56Duncan Sands /// @returns the value for an integer or vector of integer constant of the 167a757a0d52aca0311576306a03865dacba5d12e56Duncan Sands /// given type that has all its bits set to true. 168a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson /// @brief Get the all ones value 169db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner static Constant *getAllOnesValue(Type* Ty); 17043ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman 17143ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman /// getIntegerValue - Return the value for an integer or pointer constant, 17243ee5f7c08bd5a91cdd8681df20a7d1df26a1024Dan Gohman /// or a vector thereof, with the given scalar value. 173db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner static Constant *getIntegerValue(Type* Ty, const APInt &V); 17413fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner 17513fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner /// removeDeadConstantUsers - If there are any dead constant users dangling 17613fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner /// off of this constant, remove them. This method is useful for clients 17713fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner /// that want to check to see if a global is unused, but don't want to deal 17813fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner /// with potentially dead constants hanging off of the globals. 17913fb0db0c26ec498cf8ffb0f9943d28962d4ced7Chris Lattner void removeDeadConstantUsers() const; 180c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 181c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines Constant *stripPointerCasts() { 182c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines return cast<Constant>(Value::stripPointerCasts()); 183c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines } 184c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 185c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines const Constant *stripPointerCasts() const { 186c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines return const_cast<Constant*>(this)->stripPointerCasts(); 187c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines } 18831bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner}; 18931bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner 190d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 191d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 19231bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#endif 193