148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- llvm/Value.h - Definition of the Value class ------------*- 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//
10dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// This file declares the Value class.
117295eb4ea3e3a81e697600cbca681674e4b35a20Chris Lattner//
12009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_VALUE_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_VALUE_H
16009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm-c/Core.h"
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/ADT/iterator_range.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Use.h"
2040be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo#include "llvm/Support/CBindingWrapping.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/Support/Casting.h"
22daca73f9bec4ce02045aa5a631a60221c33262cfCraig Topper#include "llvm/Support/Compiler.h"
23009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
26f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass APInt;
27e06e9146eeb48e3ebf0f30bdaf7d86a6e03946adChris Lattnerclass Argument;
28f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass AssemblyAnnotationWriter;
291fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass BasicBlock;
30f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass Constant;
31f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass DataLayout;
32e7506a366e8bd56c97d10beb68e4db953aebaecaChris Lattnerclass Function;
338b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikovclass GlobalAlias;
34dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass GlobalObject;
35f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass GlobalValue;
36f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass GlobalVariable;
376dbca0b6bcf1a53ad30850448e662b5c77985a62Chris Lattnerclass InlineAsm;
38f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass Instruction;
3912ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext;
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass Module;
41f1d0f7781e766df878bec4e7977fa3204374f394Craig Topperclass StringRef;
42f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass Twine;
43f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass Type;
44f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass ValueHandleBase;
45f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass ValueSymbolTable;
46f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthclass raw_ostream;
47f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth
48f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthtemplate<typename ValueTy> class StringMapEntry;
49f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruthtypedef StringMapEntry<Value*> ValueName;
50009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
51009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
52009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//                                 Value Class
53009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
54009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
5537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief LLVM Value Representation
5637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
57dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines/// This is a very important LLVM class. It is the base class of all values
58bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// computed by a program that may be used as operands to other values. Value is
59bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// the super class of other important classes such as Instruction and Function.
60db04b81014cadc6ecdbaaa918a89a180511c6855Chris Lattner/// All Values have a Type. Type is not a subclass of Value. Some values can
61db04b81014cadc6ecdbaaa918a89a180511c6855Chris Lattner/// have a name and they belong to some Module.  Setting the name on the Value
6259dc98de2f79c027eb6860443daee260710b1405Gabor Greif/// automatically updates the module's symbol table.
6326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///
64bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer/// Every value has a "use list" that keeps track of which other Values are
65722272df41d8de9c7683811b7bd8e901ee2f2785Chris Lattner/// using this Value.  A Value can also have an arbitrary number of ValueHandle
66cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner/// objects that watch it and listen to RAUW and Destroy events.  See
6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// llvm/IR/ValueHandle.h for details.
68530036b5e89e41787056120666bf0128ea8addd8Reid Spencerclass Value {
69c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  Type *VTy;
70c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  Use *UseList;
71c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
72ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  friend class ValueAsMetadata; // Allow access to NameAndIsUsedByMD.
73c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  friend class ValueHandleBase;
74ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  PointerIntPair<ValueName *, 1> NameAndIsUsedByMD;
75c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
76c4ef55119285fa2f966ee6578a58371c3c368774Chris Lattner  const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
77722272df41d8de9c7683811b7bd8e901ee2f2785Chris Lattner  unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
788f0d40392658fd1ccc057198bd5a6f1c85be5425Chris Lattnerprotected:
7937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Hold subclass data that can be dropped.
8037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
8137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This member is similar to SubclassData, however it is for holding
8237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// information which may be used to aid optimization, but which may be
8337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// cleared to zero without affecting conservative interpretation.
849491c16e14f9fb56d5d71cd855c2d8c315fbb5aeDan Gohman  unsigned char SubclassOptionalData : 7;
859491c16e14f9fb56d5d71cd855c2d8c315fbb5aeDan Gohman
86cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattnerprivate:
8737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Hold arbitrary subclass data.
8837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
8937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This member is defined by this class, but is not used for anything.
9037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Subclasses can use it to hold whatever state they find useful.  This
9137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// field is initialized to zero by the ctor.
92c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  unsigned short SubclassData;
93c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
9437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesprotected:
9537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief The number of operands in the subclass.
9637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
9737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This member is defined by this class, but not used for anything.
9837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Subclasses can use it to store their number of operands, if they have
9937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// any.
10037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
10137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is stored here to save space in User on 64-bit hosts.  Since most
10237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// instances of Value have operands, 32-bit hosts aren't significantly
10337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// affected.
10437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  unsigned NumOperands;
10537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
10637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesprivate:
10736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  template <typename UseT> // UseT == 'Use' or 'const Use'
10836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  class use_iterator_impl
109ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      : public std::iterator<std::forward_iterator_tag, UseT *> {
11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    UseT *U;
11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    explicit use_iterator_impl(UseT *u) : U(u) {}
11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    friend class Value;
11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  public:
11536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    use_iterator_impl() : U() {}
11636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
11736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool operator==(const use_iterator_impl &x) const { return U == x.U; }
11836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
11936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
12036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    use_iterator_impl &operator++() { // Preincrement
12136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      assert(U && "Cannot increment end iterator!");
12236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      U = U->getNext();
12336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return *this;
12436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
12536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    use_iterator_impl operator++(int) { // Postincrement
12636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      auto tmp = *this;
12736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      ++*this;
12836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return tmp;
12936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
13036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
13136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    UseT &operator*() const {
13236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      assert(U && "Cannot dereference end iterator!");
13336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return *U;
13436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
13536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
13636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    UseT *operator->() const { return &operator*(); }
13736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
13836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    operator use_iterator_impl<const UseT>() const {
13936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return use_iterator_impl<const UseT>(U);
14036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
14136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  };
14236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
14336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  template <typename UserTy> // UserTy == 'User' or 'const User'
14436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  class user_iterator_impl
145ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      : public std::iterator<std::forward_iterator_tag, UserTy *> {
14636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    use_iterator_impl<Use> UI;
14736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    explicit user_iterator_impl(Use *U) : UI(U) {}
14836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    friend class Value;
14936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
15036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  public:
15136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    user_iterator_impl() {}
15236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
15336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
15436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
15536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
15636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    /// \brief Returns true if this iterator is equal to user_end() on the value.
15736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool atEnd() const { return *this == user_iterator_impl(); }
15836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
15936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    user_iterator_impl &operator++() { // Preincrement
16036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      ++UI;
16136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return *this;
16236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
16336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    user_iterator_impl operator++(int) { // Postincrement
16436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      auto tmp = *this;
16536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      ++*this;
16636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return tmp;
16736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
16836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
16936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Retrieve a pointer to the current User.
17036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    UserTy *operator*() const {
17136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return UI->getUser();
17236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
17336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
17436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    UserTy *operator->() const { return operator*(); }
17536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
17636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    operator user_iterator_impl<const UserTy>() const {
17736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return user_iterator_impl<const UserTy>(*UI);
17836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
17936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
18036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    Use &getUse() const { return *UI; }
18136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  };
18236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
183ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void operator=(const Value &) = delete;
184ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Value(const Value &) = delete;
185f6b784410103cd0da2de5797463da5830f8ac6bcChris Lattner
186cd26ec5f3c089b3b24f80ff200e94e681eb9e1eeDan Gohmanprotected:
187db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Value(Type *Ty, unsigned scid);
18844781a0a79e0d3b9f32c2fed4bf0971ff082e525Chris Lattnerpublic:
189009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  virtual ~Value();
1909769ab22265b313171d201b5928688524a01bd87Misha Brukman
19137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Support for debugging, callable in GDB: V->dump()
192cd26ec5f3c089b3b24f80ff200e94e681eb9e1eeDan Gohman  void dump() const;
19342a695c2f2627a04b1fc8e2160d608c39f1dd6acChris Lattner
19437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Implement operator<< on Value.
195dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void print(raw_ostream &O) const;
1969769ab22265b313171d201b5928688524a01bd87Misha Brukman
19736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Print the name of this Value out to the specified raw_ostream.
19837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
19936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// This is useful when you just want to print 'int %reg126', not the
20036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// instruction that generated it. If you specify a Module for context, then
20136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// even constanst get pretty-printed; for example, the type of a null
20236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// pointer is printed symbolically.
203dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void printAsOperand(raw_ostream &O, bool PrintType = true,
204dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                      const Module *M = nullptr) const;
20536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
20637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief All values are typed, get the type of this value.
2071afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  Type *getType() const { return VTy; }
2089769ab22265b313171d201b5928688524a01bd87Misha Brukman
20937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief All values hold a context through their type.
210e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  LLVMContext &getContext() const;
211e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson
21237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // \brief All values can potentially be named.
213ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool hasName() const { return getValueName() != nullptr; }
214ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  ValueName *getValueName() const { return NameAndIsUsedByMD.getPointer(); }
215ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void setValueName(ValueName *VN) { NameAndIsUsedByMD.setPointer(VN); }
216ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
217ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesprivate:
218ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void destroyValueName();
219dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
220ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinespublic:
22137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return a constant reference to the value's name.
22237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
22337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is cheap and guaranteed to return the same reference as long as the
22437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// value is not modified.
225499027fb4826e25919f2ea154ca4db73842560afDaniel Dunbar  StringRef getName() const;
226499027fb4826e25919f2ea154ca4db73842560afDaniel Dunbar
22737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Change the name of the value.
22837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
22937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Choose a new unique name if the provided name is taken.
2303f53fa9a51c4ce7ba81170ca7ab2e49bd37281b0Daniel Dunbar  ///
2312d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko  /// \param Name The new name; or "" if the value's name should be removed.
2326e0d1cb30957a636c53158d3089e6fb88348a57aDaniel Dunbar  void setName(const Twine &Name);
233ec79b3da13ae9ba09f52e177bd71fcc5669d61a5Chris Lattner
234dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
23537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Transfer the name from V to this value.
23637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
23737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// After taking V's name, sets V's name to empty.
23837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
23937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \note It is an error to call V->takeName(V).
2407216811ea22d68cbf8df092cda8e64e13e394ac8Chris Lattner  void takeName(Value *V);
2419769ab22265b313171d201b5928688524a01bd87Misha Brukman
24237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Change all uses of this to point to a new Value.
24326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
24437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Go through the uses list for this definition and make each use point to
24537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// "V" instead of "this".  After this completes, 'this's use list is
24637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// guaranteed to be empty.
24726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  void replaceAllUsesWith(Value *V);
248009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
249ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// replaceUsesOutsideBlock - Go through the uses list for this definition and
250ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// make each use point to "V" instead of "this" when the use is outside the
251ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// block. 'This's use list is expected to have at least one element.
252ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// Unlike replaceAllUsesWith this function does not support basic block
253ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// values or constant users.
254ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
255ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
256009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  //----------------------------------------------------------------------
257bfe2f407db4cfb27f7e0cebbffcd3db8d53f864cGabor Greif  // Methods for handling the chain of uses of this Value.
258009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  //
259dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  bool               use_empty() const { return UseList == nullptr; }
26036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
26136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef use_iterator_impl<Use>       use_iterator;
26236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef use_iterator_impl<const Use> const_use_iterator;
263a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  use_iterator       use_begin()       { return use_iterator(UseList); }
26460ad781c61815ca5b8dc2a45a102e1c8af65992fGabor Greif  const_use_iterator use_begin() const { return const_use_iterator(UseList); }
26536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  use_iterator       use_end()         { return use_iterator();   }
26636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const_use_iterator use_end()   const { return const_use_iterator();   }
26736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  iterator_range<use_iterator> uses() {
26836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return iterator_range<use_iterator>(use_begin(), use_end());
26936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
27036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  iterator_range<const_use_iterator> uses() const {
27136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return iterator_range<const_use_iterator>(use_begin(), use_end());
27236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
27336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
274ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool               user_empty() const { return UseList == nullptr; }
275ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
27636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef user_iterator_impl<User>       user_iterator;
27736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef user_iterator_impl<const User> const_user_iterator;
27836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  user_iterator       user_begin()       { return user_iterator(UseList); }
27936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const_user_iterator user_begin() const { return const_user_iterator(UseList); }
28036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  user_iterator       user_end()         { return user_iterator();   }
28136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const_user_iterator user_end()   const { return const_user_iterator();   }
28236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  User               *user_back()        { return *user_begin(); }
28336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const User         *user_back()  const { return *user_begin(); }
28436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  iterator_range<user_iterator> users() {
28536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return iterator_range<user_iterator>(user_begin(), user_end());
28636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
28736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  iterator_range<const_user_iterator> users() const {
28836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return iterator_range<const_user_iterator>(user_begin(), user_end());
28936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
29014d9ce7892c7c0008c7cc38894ee3f71e4104d94Chris Lattner
29137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return true if there is exactly one user of this value.
29214d9ce7892c7c0008c7cc38894ee3f71e4104d94Chris Lattner  ///
29337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is specialized because it is a common request and does not require
29437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// traversing the whole use list.
295202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  bool hasOneUse() const {
29660ad781c61815ca5b8dc2a45a102e1c8af65992fGabor Greif    const_use_iterator I = use_begin(), E = use_end();
297202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner    if (I == E) return false;
298202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner    return ++I == E;
299202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  }
300009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
30137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return true if this Value has exactly N users.
302a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  bool hasNUses(unsigned N) const;
303a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner
30437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return true if this value has N users or more.
305977a39570fcade4dfed60124f67eabc51efa76c6Chris Lattner  ///
30637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is logically equivalent to getNumUses() >= N.
307977a39570fcade4dfed60124f67eabc51efa76c6Chris Lattner  bool hasNUsesOrMore(unsigned N) const;
308977a39570fcade4dfed60124f67eabc51efa76c6Chris Lattner
30937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Check if this value is used in the specified basic block.
310c4f72dd6e759a170808ecfc6be784f8598367484Bill Wendling  bool isUsedInBasicBlock(const BasicBlock *BB) const;
311502a4f5162498ec420e3cb22f667808d726dd7daEvan Cheng
31237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief This method computes the number of uses of this Value.
31337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
31437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is a linear time operation.  Use hasOneUse, hasNUses, or
31537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// hasNUsesOrMore to check for specific values.
316a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  unsigned getNumUses() const;
317a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner
31837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief This method should only be used by the Use class.
3193b77f56194952bc3767c11e8b216906d404fcb2cOwen Anderson  void addUse(Use &U) { U.addToList(&UseList); }
320f6b784410103cd0da2de5797463da5830f8ac6bcChris Lattner
32137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Concrete subclass of this.
32237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
323bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer  /// An enumeration for keeping track of the concrete subclass of Value that
324dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// is actually instantiated. Values of this enumeration are kept in the
325bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer  /// Value classes SubclassID field. They are used for concrete type
326bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer  /// identification.
327f6b784410103cd0da2de5797463da5830f8ac6bcChris Lattner  enum ValueTy {
32866a434692b63ac76c99a0d259c7fedc0b75a1d6aChris Lattner    ArgumentVal,              // This is an instance of Argument
32966a434692b63ac76c99a0d259c7fedc0b75a1d6aChris Lattner    BasicBlockVal,            // This is an instance of BasicBlock
33066a434692b63ac76c99a0d259c7fedc0b75a1d6aChris Lattner    FunctionVal,              // This is an instance of Function
3318b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov    GlobalAliasVal,           // This is an instance of GlobalAlias
33266a434692b63ac76c99a0d259c7fedc0b75a1d6aChris Lattner    GlobalVariableVal,        // This is an instance of GlobalVariable
3337eb8a523320d05d9318c957c9840cfbde3cf5b6dChris Lattner    UndefValueVal,            // This is an instance of UndefValue
3342ee11eccdde14c95c78773be76b02bb5fd09d7eeChris Lattner    BlockAddressVal,          // This is an instance of BlockAddress
33566a434692b63ac76c99a0d259c7fedc0b75a1d6aChris Lattner    ConstantExprVal,          // This is an instance of ConstantExpr
3366ed9d40aa113bba1101a9f74c661d0d11e229f50Nick Lewycky    ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
33727dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    ConstantDataArrayVal,     // This is an instance of ConstantDataArray
33827dd9cf5d1ec831a1cd0766580e6d1177a9800a3Chris Lattner    ConstantDataVectorVal,    // This is an instance of ConstantDataVector
339b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer    ConstantIntVal,           // This is an instance of ConstantInt
340225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    ConstantFPVal,            // This is an instance of ConstantFP
341225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    ConstantArrayVal,         // This is an instance of ConstantArray
342225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    ConstantStructVal,        // This is an instance of ConstantStruct
343ac9dcb94dde5f166ee29372385c0e3b695227ab4Reid Spencer    ConstantVectorVal,        // This is an instance of ConstantVector
344225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
345ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    MetadataAsValueVal,       // This is an instance of MetadataAsValue
346cc041ba03aed685400197fb938b7a583713d25afChris Lattner    InlineAsmVal,             // This is an instance of InlineAsm
347ce8447ccfeb8e90fc37f2eeb504d391a59c44a66Dan Gohman    InstructionVal,           // This is an instance of Instruction
348ce8447ccfeb8e90fc37f2eeb504d391a59c44a66Dan Gohman    // Enum values starting at InstructionVal are used for Instructions;
349ce8447ccfeb8e90fc37f2eeb504d391a59c44a66Dan Gohman    // don't add new values here!
350d7a18e420e1a95c06f551ded98531eb3ae97eaa9David Greene
351225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    // Markers:
352225e8dd2f512e3e6840ba7cb1570fdc4d56a853fChris Lattner    ConstantFirstVal = FunctionVal,
353104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel    ConstantLastVal  = ConstantPointerNullVal
354f6b784410103cd0da2de5797463da5830f8ac6bcChris Lattner  };
355bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4Reid Spencer
35637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return an ID for the concrete type of this object.
35737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
35837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is used to implement the classof checks.  This should not be used
35937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// for any other purpose, as the values may change as LLVM evolves.  Also,
36037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// note that for instructions, the Instruction's opcode is added to
36137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// InstructionVal. So this means three things:
36253201869cdb92cfe298126ea9e6f5b763038d494Reid Spencer  /// # there is no value with code InstructionVal (no opcode==0).
36353201869cdb92cfe298126ea9e6f5b763038d494Reid Spencer  /// # there are more possible values for the value type than in ValueTy enum.
36453201869cdb92cfe298126ea9e6f5b763038d494Reid Spencer  /// # the InstructionVal enumerator must be the highest valued enumerator in
36553201869cdb92cfe298126ea9e6f5b763038d494Reid Spencer  ///   the ValueTy enum.
366a1a702cdd23221e6e3f36632be91150138958e9dDan Gohman  unsigned getValueID() const {
367f6b784410103cd0da2de5797463da5830f8ac6bcChris Lattner    return SubclassID;
368f6b784410103cd0da2de5797463da5830f8ac6bcChris Lattner  }
3697da38ec915010576685200089c75a2135bd3fa97Chris Lattner
37037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return the raw optional flags value contained in this value.
37137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
37237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This should only be used when testing two Values for equivalence.
373f8dbee7cea072eb63ae343759975109553697bcbDan Gohman  unsigned getRawSubclassOptionalData() const {
374f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    return SubclassOptionalData;
375f8dbee7cea072eb63ae343759975109553697bcbDan Gohman  }
376f8dbee7cea072eb63ae343759975109553697bcbDan Gohman
37737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Clear the optional flags contained in this value.
37846985a14409486293b689ca07dd07d7482734795Dan Gohman  void clearSubclassOptionalData() {
37946985a14409486293b689ca07dd07d7482734795Dan Gohman    SubclassOptionalData = 0;
38046985a14409486293b689ca07dd07d7482734795Dan Gohman  }
38146985a14409486293b689ca07dd07d7482734795Dan Gohman
38237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Check the optional flags for equality.
38358cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman  bool hasSameSubclassOptionalData(const Value *V) const {
38458cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman    return SubclassOptionalData == V->SubclassOptionalData;
38558cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman  }
38658cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman
38737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Clear any optional flags not set in the given Value.
38858cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman  void intersectOptionalDataWith(const Value *V) {
38958cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman    SubclassOptionalData &= V->SubclassOptionalData;
39058cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman  }
39158cfa3b13752579c86cf85270d49f9ced0942f2fDan Gohman
39237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return true if there is a value handle associated with this value.
3931ad25b605c9a0384998a63d72042a7a3caf83912Chris Lattner  bool hasValueHandle() const { return HasValueHandle; }
394ff8c1b3dc6766d534878613f6db511edb33ba44fChris Lattner
395ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// \brief Return true if there is metadata referencing this value.
396ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool isUsedByMetadata() const { return NameAndIsUsedByMD.getInt(); }
397ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
39837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
399f6db9425648e9d9b965dc0aae9f63afbb3e7a5b8Chandler Carruth  ///
40037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Returns the original uncasted value.  If this is called on a non-pointer
40137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// value, it returns 'this'.
4020b12ecf6ff6b5d3a144178257b6206f0c4788792Anton Korobeynikov  Value *stripPointerCasts();
4030ad4d9be0310b79e47561abeef39daf322d06760Chris Lattner  const Value *stripPointerCasts() const {
4040ad4d9be0310b79e47561abeef39daf322d06760Chris Lattner    return const_cast<Value*>(this)->stripPointerCasts();
4050ad4d9be0310b79e47561abeef39daf322d06760Chris Lattner  }
4065d0392c6b370758750b397e254a6c6f028479969Duncan Sands
40737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Strip off pointer casts and all-zero GEPs.
408f6db9425648e9d9b965dc0aae9f63afbb3e7a5b8Chandler Carruth  ///
40937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Returns the original uncasted value.  If this is called on a non-pointer
41037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// value, it returns 'this'.
411eaf14786ca3975266ed7041ac242122c02baf1cfRafael Espindola  Value *stripPointerCastsNoFollowAliases();
412eaf14786ca3975266ed7041ac242122c02baf1cfRafael Espindola  const Value *stripPointerCastsNoFollowAliases() const {
413eaf14786ca3975266ed7041ac242122c02baf1cfRafael Espindola    return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
414eaf14786ca3975266ed7041ac242122c02baf1cfRafael Espindola  }
415eaf14786ca3975266ed7041ac242122c02baf1cfRafael Espindola
41637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Strip off pointer casts and all-constant inbounds GEPs.
417f6db9425648e9d9b965dc0aae9f63afbb3e7a5b8Chandler Carruth  ///
41837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Returns the original pointer value.  If this is called on a non-pointer
41937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// value, it returns 'this'.
420274d377ea68195989c3238fe96ce2ca812a12fafChandler Carruth  Value *stripInBoundsConstantOffsets();
421274d377ea68195989c3238fe96ce2ca812a12fafChandler Carruth  const Value *stripInBoundsConstantOffsets() const {
422274d377ea68195989c3238fe96ce2ca812a12fafChandler Carruth    return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
42384dfc32ff906271c373819595e60a173624e1184Chandler Carruth  }
42484dfc32ff906271c373819595e60a173624e1184Chandler Carruth
42537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
426f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  ///
427f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  /// Stores the resulting constant offset stripped into the APInt provided.
428f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  /// The provided APInt will be extended or truncated as needed to be the
429f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  /// correct bitwidth for an offset of this pointer type.
430f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  ///
431f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  /// If this is called on a non-pointer value, it returns 'this'.
432f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
433f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth                                                   APInt &Offset);
434f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
435f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth                                                         APInt &Offset) const {
436f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth    return const_cast<Value *>(this)
437f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth        ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
438f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth  }
439f73826bef09fcc38d2db7b69baf0b8a45c9788f8Chandler Carruth
44037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Strip off pointer casts and inbounds GEPs.
441f6db9425648e9d9b965dc0aae9f63afbb3e7a5b8Chandler Carruth  ///
44237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Returns the original pointer value.  If this is called on a non-pointer
44337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// value, it returns 'this'.
44484dfc32ff906271c373819595e60a173624e1184Chandler Carruth  Value *stripInBoundsOffsets();
44584dfc32ff906271c373819595e60a173624e1184Chandler Carruth  const Value *stripInBoundsOffsets() const {
44684dfc32ff906271c373819595e60a173624e1184Chandler Carruth    return const_cast<Value*>(this)->stripInBoundsOffsets();
44784dfc32ff906271c373819595e60a173624e1184Chandler Carruth  }
44884dfc32ff906271c373819595e60a173624e1184Chandler Carruth
44937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Check if this is always a dereferenceable pointer.
45037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
45137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Test if this value is always a pointer to allocated and suitably aligned
45237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// memory for a simple load or store.
4534c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  bool isDereferenceablePointer(const DataLayout &DL) const;
454dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
45537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Translate PHI node to its predecessor from the given basic block.
45637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
45737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// If this value is a PHI node with CurBB as its parent, return the value in
45837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// the PHI node corresponding to PredBB.  If not, return ourself.  This is
45937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// useful if you want to know the value something has in a predecessor
46037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// block.
461c7f7c1dc5067a36c8ae337610dcbbe55d525c80cChris Lattner  Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
462c7f7c1dc5067a36c8ae337610dcbbe55d525c80cChris Lattner
463c7f7c1dc5067a36c8ae337610dcbbe55d525c80cChris Lattner  const Value *DoPHITranslation(const BasicBlock *CurBB,
464c7f7c1dc5067a36c8ae337610dcbbe55d525c80cChris Lattner                                const BasicBlock *PredBB) const{
465c7f7c1dc5067a36c8ae337610dcbbe55d525c80cChris Lattner    return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
466c7f7c1dc5067a36c8ae337610dcbbe55d525c80cChris Lattner  }
467dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
46837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief The maximum alignment for instructions.
46937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
47037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// This is the greatest alignment value supported by load, store, and alloca
47137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// instructions, and global values.
472ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static const unsigned MaxAlignmentExponent = 29;
473ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
474dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
47537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Mutate the type of this Value to be of the specified type.
47637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
4771afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// Note that this is an extremely dangerous operation which can create
4781afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// completely invalid IR very easily.  It is strongly recommended that you
4791afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// recreate IR objects with the right types instead of mutating them in
4801afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  /// place.
4811afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  void mutateType(Type *Ty) {
4821afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner    VTy = Ty;
4831afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  }
484dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
48537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Sort the use-list.
48637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
48737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Sorts the Value's use-list by Cmp using a stable mergesort.  Cmp is
48837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// expected to compare two \a Use references.
48937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  template <class Compare> void sortUseList(Compare Cmp);
49037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
49137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Reverse the use-list.
49237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void reverseUseList();
49337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
49437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesprivate:
49537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Merge two lists together.
49637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
49737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Merges \c L and \c R using \c Cmp.  To enable stable sorts, always pushes
49837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// "equal" items from L before items from R.
49937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
50037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \return the first element in the list.
50137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
50237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
50337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  template <class Compare>
50437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
50537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Use *Merged;
50637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    mergeUseListsImpl(L, R, &Merged, Cmp);
50737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Merged;
50837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
50937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
51037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Tail-recursive helper for \a mergeUseLists().
51137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
51237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \param[out] Next the first element in the list.
51337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  template <class Compare>
51437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  static void mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp);
51537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
516cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattnerprotected:
517cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  unsigned short getSubclassDataFromValue() const { return SubclassData; }
518cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  void setValueSubclassData(unsigned short D) { SubclassData = D; }
519009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner};
520009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
521944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattnerinline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
522944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  V.print(OS);
523944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  return OS;
524944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner}
525dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
5269769ab22265b313171d201b5928688524a01bd87Misha Brukmanvoid Use::set(Value *V) {
5276f4266506bca785828bacda55bd5db9172f990c6Gabor Greif  if (Val) removeFromList();
5286f4266506bca785828bacda55bd5db9172f990c6Gabor Greif  Val = V;
529202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  if (V) V->addUse(*this);
530202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner}
531009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
53237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinestemplate <class Compare> void Value::sortUseList(Compare Cmp) {
53337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!UseList || !UseList->Next)
53437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // No need to sort 0 or 1 uses.
53537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return;
53637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
53737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Note: this function completely ignores Prev pointers until the end when
53837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // they're fixed en masse.
53937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
54037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Create a binomial vector of sorted lists, visiting uses one at a time and
54137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // merging lists as necessary.
54237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const unsigned MaxSlots = 32;
54337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Use *Slots[MaxSlots];
54437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
54537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Collect the first use, turning it into a single-item list.
54637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Use *Next = UseList->Next;
54737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  UseList->Next = nullptr;
54837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  unsigned NumSlots = 1;
54937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Slots[0] = UseList;
55037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
55137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Collect all but the last use.
55237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  while (Next->Next) {
55337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Use *Current = Next;
55437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Next = Current->Next;
55537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
55637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // Turn Current into a single-item list.
55737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Current->Next = nullptr;
55837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
55937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // Save Current in the first available slot, merging on collisions.
56037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    unsigned I;
56137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    for (I = 0; I < NumSlots; ++I) {
56237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (!Slots[I])
56337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        break;
56437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
56537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // Merge two lists, doubling the size of Current and emptying slot I.
56637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      //
56737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // Since the uses in Slots[I] originally preceded those in Current, send
56837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // Slots[I] in as the left parameter to maintain a stable sort.
56937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Current = mergeUseLists(Slots[I], Current, Cmp);
57037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Slots[I] = nullptr;
57137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
57237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // Check if this is a new slot.
57337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (I == NumSlots) {
57437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      ++NumSlots;
57537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
57637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
57737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
57837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // Found an open slot.
57937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Slots[I] = Current;
58037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
58137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
58237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Merge all the lists together.
58337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  assert(Next && "Expected one more Use");
58437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  assert(!Next->Next && "Expected only one Use");
58537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  UseList = Next;
58637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  for (unsigned I = 0; I < NumSlots; ++I)
58737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (Slots[I])
58837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // Since the uses in Slots[I] originally preceded those in UseList, send
58937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // Slots[I] in as the left parameter to maintain a stable sort.
59037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      UseList = mergeUseLists(Slots[I], UseList, Cmp);
59137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
59237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Fix the Prev pointers.
59337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
59437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    I->setPrev(Prev);
59537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Prev = &I->Next;
59637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
59737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
59837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
59937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinestemplate <class Compare>
60037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesvoid Value::mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp) {
60137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!L) {
60237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    *Next = R;
60337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return;
60437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
60537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!R) {
60637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    *Next = L;
60737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return;
60837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
60937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (Cmp(*R, *L)) {
61037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    *Next = R;
61137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    mergeUseListsImpl(L, R->Next, &R->Next, Cmp);
61237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return;
61337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
61437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  *Next = L;
61537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  mergeUseListsImpl(L->Next, R, &L->Next, Cmp);
61637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
617cfe26c930ae691ff3012736555846c45087e1a9eChris Lattner
61842a695c2f2627a04b1fc8e2160d608c39f1dd6acChris Lattner// isa - Provide some specializations of isa so that we don't have to include
61942a695c2f2627a04b1fc8e2160d608c39f1dd6acChris Lattner// the subtype header files to test to see if the value is a subclass...
620cfe26c930ae691ff3012736555846c45087e1a9eChris Lattner//
6210179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregortemplate <> struct isa_impl<Constant, Value> {
6220179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6230179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() >= Value::ConstantFirstVal &&
6240179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor      Val.getValueID() <= Value::ConstantLastVal;
6250179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6260179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6270179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
6280179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregortemplate <> struct isa_impl<Argument, Value> {
6290179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit (const Value &Val) {
6300179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() == Value::ArgumentVal;
6310179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6320179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6330179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
634dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<InlineAsm, Value> {
6350179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6360179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() == Value::InlineAsmVal;
6370179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6380179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6390179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
640dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<Instruction, Value> {
6410179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6420179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() >= Value::InstructionVal;
6430179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6440179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6450179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
646dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<BasicBlock, Value> {
6470179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6480179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() == Value::BasicBlockVal;
6490179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6500179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6510179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
652dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<Function, Value> {
6530179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6540179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() == Value::FunctionVal;
6550179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6560179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6570179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
658dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<GlobalVariable, Value> {
6590179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6600179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() == Value::GlobalVariableVal;
6610179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6620179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6630179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
664dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<GlobalAlias, Value> {
6650179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
6660179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor    return Val.getValueID() == Value::GlobalAliasVal;
6670179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6680179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6690179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
670dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<GlobalValue, Value> {
671dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  static inline bool doit(const Value &Val) {
672dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val);
673dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
674dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines};
675dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
676dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestemplate <> struct isa_impl<GlobalObject, Value> {
6770179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  static inline bool doit(const Value &Val) {
678dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return isa<GlobalVariable>(Val) || isa<Function>(Val);
6790179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor  }
6800179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor};
6810179e977234fef45b1877eb93a3c7565cdd1862dDouglas Gregor
682aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner// Value* is only 4-byte aligned.
683aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattnertemplate<>
684aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattnerclass PointerLikeTypeTraits<Value*> {
685aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner  typedef Value* PT;
686aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattnerpublic:
687aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner  static inline void *getAsVoidPointer(PT P) { return P; }
688aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner  static inline PT getFromVoidPointer(void *P) {
689aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner    return static_cast<PT>(P);
690aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner  }
691aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner  enum { NumLowBitsAvailable = 2 };
692aab3d88bc481fdc3081d2280dab4f7efd7e74a61Chris Lattner};
693cfe26c930ae691ff3012736555846c45087e1a9eChris Lattner
69440be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h).
69540be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
69640be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
69740be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo/* Specialized opaque value conversions.
698dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines */
69940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizloinline Value **unwrap(LLVMValueRef *Vals) {
70040be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  return reinterpret_cast<Value**>(Vals);
70140be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo}
70240be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
70340be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlotemplate<typename T>
70440be1e85665d10f5444186f0e7106e368dd735b8Filip Pizloinline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
70540be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo#ifdef DEBUG
70640be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
70740be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo    cast<T>(*I);
70840be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo#endif
70940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  (void)Length;
71040be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  return reinterpret_cast<T**>(Vals);
71140be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo}
71240be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
71340be1e85665d10f5444186f0e7106e368dd735b8Filip Pizloinline LLVMValueRef *wrap(const Value **Vals) {
71440be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
71540be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo}
71640be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
717d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
718d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
719009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner#endif
720