#ifndef LLVM_IR_VALUE_H
#define LLVM_IR_VALUE_H
+#include "llvm-c/Core.h"
+#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Use.h"
-#include "llvm/Support/Casting.h"
#include "llvm/Support/CBindingWrapping.h"
+#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
-#include "llvm-c/Core.h"
namespace llvm {
-class Constant;
+class APInt;
class Argument;
-class Instruction;
+class AssemblyAnnotationWriter;
class BasicBlock;
-class GlobalValue;
+class Constant;
+class DataLayout;
class Function;
-class GlobalVariable;
class GlobalAlias;
+class GlobalValue;
+class GlobalVariable;
class InlineAsm;
-class ValueSymbolTable;
-template<typename ValueTy> class StringMapEntry;
-typedef StringMapEntry<Value*> ValueName;
-class raw_ostream;
-class AssemblyAnnotationWriter;
-class ValueHandleBase;
+class Instruction;
class LLVMContext;
-class Twine;
class MDNode;
-class Type;
+class Module;
class StringRef;
+class Twine;
+class Type;
+class ValueHandleBase;
+class ValueSymbolTable;
+class raw_ostream;
+
+template<typename ValueTy> class StringMapEntry;
+typedef StringMapEntry<Value*> ValueName;
//===----------------------------------------------------------------------===//
// Value Class
/// Every value has a "use list" that keeps track of which other Values are
/// using this Value. A Value can also have an arbitrary number of ValueHandle
/// objects that watch it and listen to RAUW and Destroy events. See
-/// llvm/Support/ValueHandle.h for details.
+/// llvm/IR/ValueHandle.h for details.
///
/// @brief LLVM Value Representation
class Value {
unsigned char SubclassOptionalData : 7;
private:
+ template <typename UseT> // UseT == 'Use' or 'const Use'
+ class use_iterator_impl
+ : public std::iterator<std::forward_iterator_tag, UseT *, ptrdiff_t> {
+ typedef std::iterator<std::forward_iterator_tag, UseT *, ptrdiff_t> super;
+
+ UseT *U;
+ explicit use_iterator_impl(UseT *u) : U(u) {}
+ friend class Value;
+
+ public:
+ typedef typename super::reference reference;
+ typedef typename super::pointer pointer;
+
+ use_iterator_impl() : U() {}
+
+ bool operator==(const use_iterator_impl &x) const { return U == x.U; }
+ bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
+
+ use_iterator_impl &operator++() { // Preincrement
+ assert(U && "Cannot increment end iterator!");
+ U = U->getNext();
+ return *this;
+ }
+ use_iterator_impl operator++(int) { // Postincrement
+ auto tmp = *this;
+ ++*this;
+ return tmp;
+ }
+
+ UseT &operator*() const {
+ assert(U && "Cannot dereference end iterator!");
+ return *U;
+ }
+
+ UseT *operator->() const { return &operator*(); }
+
+ operator use_iterator_impl<const UseT>() const {
+ return use_iterator_impl<const UseT>(U);
+ }
+ };
+
+ template <typename UserTy> // UserTy == 'User' or 'const User'
+ class user_iterator_impl
+ : public std::iterator<std::forward_iterator_tag, UserTy *, ptrdiff_t> {
+ typedef std::iterator<std::forward_iterator_tag, UserTy *, ptrdiff_t> super;
+
+ use_iterator_impl<Use> UI;
+ explicit user_iterator_impl(Use *U) : UI(U) {}
+ friend class Value;
+
+ public:
+ typedef typename super::reference reference;
+ typedef typename super::pointer pointer;
+
+ user_iterator_impl() {}
+
+ bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
+ bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
+
+ /// \brief Returns true if this iterator is equal to user_end() on the value.
+ bool atEnd() const { return *this == user_iterator_impl(); }
+
+ user_iterator_impl &operator++() { // Preincrement
+ ++UI;
+ return *this;
+ }
+ user_iterator_impl operator++(int) { // Postincrement
+ auto tmp = *this;
+ ++*this;
+ return tmp;
+ }
+
+ // Retrieve a pointer to the current User.
+ UserTy *operator*() const {
+ return UI->getUser();
+ }
+
+ UserTy *operator->() const { return operator*(); }
+
+ operator user_iterator_impl<const UserTy>() const {
+ return user_iterator_impl<const UserTy>(*UI);
+ }
+
+ Use &getUse() const { return *UI; }
+
+ /// \brief Return the operand # of this use in its User.
+ /// FIXME: Replace all callers with a direct call to Use::getOperandNo.
+ unsigned getOperandNo() const { return UI->getOperandNo(); }
+ };
+
/// SubclassData - This member is defined by this class, but is not used for
/// anything. Subclasses can use it to hold whatever state they find useful.
/// This field is initialized to zero by the ctor.
/// print - Implement operator<< on Value.
///
- void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
+ void print(raw_ostream &O) const;
+
+ /// \brief Print the name of this Value out to the specified raw_ostream.
+ /// This is useful when you just want to print 'int %reg126', not the
+ /// instruction that generated it. If you specify a Module for context, then
+ /// even constanst get pretty-printed; for example, the type of a null
+ /// pointer is printed symbolically.
+ void printAsOperand(raw_ostream &O, bool PrintType = true,
+ const Module *M = nullptr) const;
/// All values are typed, get the type of this value.
///
LLVMContext &getContext() const;
// All values can potentially be named.
- bool hasName() const { return Name != 0 && SubclassID != MDStringVal; }
+ bool hasName() const { return Name != nullptr && SubclassID != MDStringVal; }
ValueName *getValueName() const { return Name; }
void setValueName(ValueName *VN) { Name = VN; }
//----------------------------------------------------------------------
// Methods for handling the chain of uses of this Value.
//
- typedef value_use_iterator<User> use_iterator;
- typedef value_use_iterator<const User> const_use_iterator;
+ bool use_empty() const { return UseList == nullptr; }
- bool use_empty() const { return UseList == 0; }
+ typedef use_iterator_impl<Use> use_iterator;
+ typedef use_iterator_impl<const Use> const_use_iterator;
use_iterator use_begin() { return use_iterator(UseList); }
const_use_iterator use_begin() const { return const_use_iterator(UseList); }
- use_iterator use_end() { return use_iterator(0); }
- const_use_iterator use_end() const { return const_use_iterator(0); }
- User *use_back() { return *use_begin(); }
- const User *use_back() const { return *use_begin(); }
+ use_iterator use_end() { return use_iterator(); }
+ const_use_iterator use_end() const { return const_use_iterator(); }
+ iterator_range<use_iterator> uses() {
+ return iterator_range<use_iterator>(use_begin(), use_end());
+ }
+ iterator_range<const_use_iterator> uses() const {
+ return iterator_range<const_use_iterator>(use_begin(), use_end());
+ }
+
+ typedef user_iterator_impl<User> user_iterator;
+ typedef user_iterator_impl<const User> const_user_iterator;
+ user_iterator user_begin() { return user_iterator(UseList); }
+ const_user_iterator user_begin() const { return const_user_iterator(UseList); }
+ user_iterator user_end() { return user_iterator(); }
+ const_user_iterator user_end() const { return const_user_iterator(); }
+ User *user_back() { return *user_begin(); }
+ const User *user_back() const { return *user_begin(); }
+ iterator_range<user_iterator> users() {
+ return iterator_range<user_iterator>(user_begin(), user_end());
+ }
+ iterator_range<const_user_iterator> users() const {
+ return iterator_range<const_user_iterator>(user_begin(), user_end());
+ }
/// hasOneUse - Return true if there is exactly one user of this value. This
/// is specialized because it is a common request and does not require
MDNodeVal, // This is an instance of MDNode
MDStringVal, // This is an instance of MDString
InlineAsmVal, // This is an instance of InlineAsm
- PseudoSourceValueVal, // This is an instance of PseudoSourceValue
- FixedStackPseudoSourceValueVal, // This is an instance of
- // FixedStackPseudoSourceValue
InstructionVal, // This is an instance of Instruction
// Enum values starting at InstructionVal are used for Instructions;
// don't add new values here!
/// this value.
bool hasValueHandle() const { return HasValueHandle; }
- /// stripPointerCasts - This method strips off any unneeded pointer casts and
- /// all-zero GEPs from the specified value, returning the original uncasted
- /// value. If this is called on a non-pointer value, it returns 'this'.
+ /// \brief Strips off any unneeded pointer casts, all-zero GEPs and aliases
+ /// from the specified value, returning the original uncasted value.
+ ///
+ /// If this is called on a non-pointer value, it returns 'this'.
Value *stripPointerCasts();
const Value *stripPointerCasts() const {
return const_cast<Value*>(this)->stripPointerCasts();
}
- /// stripInBoundsConstantOffsets - This method strips off unneeded pointer casts and
- /// all-constant GEPs from the specified value, returning the original
- /// pointer value. If this is called on a non-pointer value, it returns
- /// 'this'.
+ /// \brief Strips off any unneeded pointer casts and all-zero GEPs from the
+ /// specified value, returning the original uncasted value.
+ ///
+ /// If this is called on a non-pointer value, it returns 'this'.
+ Value *stripPointerCastsNoFollowAliases();
+ const Value *stripPointerCastsNoFollowAliases() const {
+ return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
+ }
+
+ /// \brief Strips off unneeded pointer casts and all-constant GEPs from the
+ /// specified value, returning the original pointer value.
+ ///
+ /// If this is called on a non-pointer value, it returns 'this'.
Value *stripInBoundsConstantOffsets();
const Value *stripInBoundsConstantOffsets() const {
return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
}
- /// stripInBoundsOffsets - This method strips off unneeded pointer casts and
- /// any in-bounds Offsets from the specified value, returning the original
- /// pointer value. If this is called on a non-pointer value, it returns
- /// 'this'.
+ /// \brief Strips like \c stripInBoundsConstantOffsets but also accumulates
+ /// the constant offset stripped.
+ ///
+ /// Stores the resulting constant offset stripped into the APInt provided.
+ /// The provided APInt will be extended or truncated as needed to be the
+ /// correct bitwidth for an offset of this pointer type.
+ ///
+ /// If this is called on a non-pointer value, it returns 'this'.
+ Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
+ APInt &Offset);
+ const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
+ APInt &Offset) const {
+ return const_cast<Value *>(this)
+ ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
+ }
+
+ /// \brief Strips off unneeded pointer casts and any in-bounds offsets from
+ /// the specified value, returning the original pointer value.
+ ///
+ /// If this is called on a non-pointer value, it returns 'this'.
Value *stripInBoundsOffsets();
const Value *stripInBoundsOffsets() const {
return const_cast<Value*>(this)->stripInBoundsOffsets();