1202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner//===-- llvm/Use.h - Definition of the Use 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//===----------------------------------------------------------------------===//
936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \file
1036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
1136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// This defines the Use class.  The Use class represents the operand of an
1236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// instruction or some other User instance which refers to a Value.  The Use
1336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// class keeps the "use list" of the referenced value up to date.
1436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
1536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Pointer tagging is used to efficiently find the User corresponding to a Use
1636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// without having to store a User pointer in every Use. A User is preceded in
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// memory by all the Uses corresponding to its operands, and the low bits of
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// one of the fields (Prev) of the Use class are used to encode offsets to be
1936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// able to find that User given a pointer to any Use. For details, see:
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
23202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner//===----------------------------------------------------------------------===//
24202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
25674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_USE_H
26674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_USE_H
27202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
2836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm-c/Core.h"
29fd095b6389ec7c794e094f2a5dc8851bdc108999Gabor Greif#include "llvm/ADT/PointerIntPair.h"
3040be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo#include "llvm/Support/CBindingWrapping.h"
31daca73f9bec4ce02045aa5a631a60221c33262cfCraig Topper#include "llvm/Support/Compiler.h"
32ae479aa13ed344876680f7d4dc08e3cf4b512da9Nick Lewycky#include <cstddef>
33f0891be8bdbeeadb39da5575273b6645755fa383Gabor Greif#include <iterator>
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
35d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
36d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
37202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattnerclass Value;
38202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattnerclass User;
39e30173ac3396510bd0bb26a66fd615ff9083436dChris Lattnerclass Use;
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestemplate <typename> struct simplify_type;
41202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
42b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattner// Use** is only 4-byte aligned.
4336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestemplate <> class PointerLikeTypeTraits<Use **> {
44b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattnerpublic:
4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  static inline void *getAsVoidPointer(Use **P) { return P; }
46b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattner  static inline Use **getFromVoidPointer(void *P) {
4736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return static_cast<Use **>(P);
48b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattner  }
49b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattner  enum { NumLowBitsAvailable = 2 };
50b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattner};
51b7a00daa1165576dd2bb9d17970c249d536f4a82Chris Lattner
5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \brief A Use represents the edge between a Value definition and its users.
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// This is notionally a two-dimensional linked list. It supports traversing
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// all of the uses for a particular value definition. It also supports jumping
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// directly to the used value when we arrive from the User's operands, and
5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// jumping directly to the User when we arrive from the Value's uses.
5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// The pointer to the used Value is explicit, and the pointer to the User is
6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// implicit. The implicit pointer is found via a waymarking algorithm
6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// described in the programmer's manual:
6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
63dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines///   http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
6436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
6536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// This is essentially the single most memory intensive object in LLVM because
6636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// of the number of uses in the system. At the same time, the constant time
6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// operations it allows are essential to many optimizations having reasonable
6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// time complexity.
69202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattnerclass Use {
706f4266506bca785828bacda55bd5db9172f990c6Gabor Greifpublic:
7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Provide a fast substitute to std::swap<Use>
7294fb68ba217975d2d99ae86d15993402158ac655Gabor Greif  /// that also works with less standard-compliant compilers
7394fb68ba217975d2d99ae86d15993402158ac655Gabor Greif  void swap(Use &RHS);
74b8d5b1211f982d65546d855130d99c42780a76a0Chris Lattner
75691c05bb29d3e2ec9c2ed6b1c082ce5d484b75daJay Foad  // A type for the word following an array of hung-off Uses in memory, which is
76691c05bb29d3e2ec9c2ed6b1c082ce5d484b75daJay Foad  // a pointer back to their User with the bottom bit set.
7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef PointerIntPair<User *, 1, unsigned> UserRef;
78691c05bb29d3e2ec9c2ed6b1c082ce5d484b75daJay Foad
79efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifprivate:
80f630e49efc7bf3f1716b6daab3c2cc11a908754aCraig Topper  Use(const Use &U) LLVM_DELETED_FUNCTION;
81efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
8294fb68ba217975d2d99ae86d15993402158ac655Gabor Greif  /// Destructor - Only for zap()
831ed26acc58a13f125bc9e1d5e5aa22fd479654ffJay Foad  ~Use() {
8436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (Val)
8536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      removeFromList();
8659dc98de2f79c027eb6860443daee260710b1405Gabor Greif  }
87202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
8836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
896f4266506bca785828bacda55bd5db9172f990c6Gabor Greif
901ed26acc58a13f125bc9e1d5e5aa22fd479654ffJay Foad  /// Constructor
91dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Use(PrevPtrTag tag) : Val(nullptr) { Prev.setInt(tag); }
921ed26acc58a13f125bc9e1d5e5aa22fd479654ffJay Foad
93efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greifpublic:
9436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  operator Value *() const { return Val; }
956f4266506bca785828bacda55bd5db9172f990c6Gabor Greif  Value *get() const { return Val; }
9636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Returns the User that contains this Use.
9836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// For an instruction operand, for example, this will return the
10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// instruction.
101efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif  User *getUser() const;
102202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
103202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  inline void set(Value *Val);
104202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
105202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  Value *operator=(Value *RHS) {
106202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner    set(RHS);
107202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner    return RHS;
108202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  }
109202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  const Use &operator=(const Use &RHS) {
1106f4266506bca785828bacda55bd5db9172f990c6Gabor Greif    set(RHS.Val);
111202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner    return *this;
112202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner  }
113202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Value *operator->() { return Val; }
1156f4266506bca785828bacda55bd5db9172f990c6Gabor Greif  const Value *operator->() const { return Val; }
11602accaeb172fbe179fe20eafa6172606d0c9eae1Chris Lattner
1176f4266506bca785828bacda55bd5db9172f990c6Gabor Greif  Use *getNext() const { return Next; }
118a870039277d26d3d8f3ff774345db90096b544adChris Lattner
11936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Return the operand # of this use in its User.
12036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned getOperandNo() const;
12136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
12236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Initializes the waymarking tags on an array of Uses.
12336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
12436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// This sets up the array of Uses such that getUser() can find the User from
12536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// any of those Uses.
12695c3e48f9557adb6064d580684bb14cacec2f826Jay Foad  static Use *initTags(Use *Start, Use *Stop);
12795c3e48f9557adb6064d580684bb14cacec2f826Jay Foad
12836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Destroys Use operands when the number of operands of
129a870039277d26d3d8f3ff774345db90096b544adChris Lattner  /// a User changes.
130a870039277d26d3d8f3ff774345db90096b544adChris Lattner  static void zap(Use *Start, const Use *Stop, bool del = false);
131a870039277d26d3d8f3ff774345db90096b544adChris Lattner
13202accaeb172fbe179fe20eafa6172606d0c9eae1Chris Lattnerprivate:
13336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const Use *getImpliedUser() const;
13436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1356f4266506bca785828bacda55bd5db9172f990c6Gabor Greif  Value *Val;
136fd095b6389ec7c794e094f2a5dc8851bdc108999Gabor Greif  Use *Next;
13736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  PointerIntPair<Use **, 2, PrevPtrTag> Prev;
138202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
13936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
140a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  void addToList(Use **List) {
141a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner    Next = *List;
14236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (Next)
14336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Next->setPrev(&Next);
144efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif    setPrev(List);
145a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner    *List = this;
146a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  }
147a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  void removeFromList() {
148fd095b6389ec7c794e094f2a5dc8851bdc108999Gabor Greif    Use **StrippedPrev = Prev.getPointer();
149efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif    *StrippedPrev = Next;
15036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (Next)
15136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Next->setPrev(StrippedPrev);
152a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  }
153202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
154a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner  friend class Value;
155a26619748932b146a09773d51465d7b7dcdb7dd2Chris Lattner};
156202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
15736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \brief Allow clients to treat uses just like values when using
15836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// casting operators.
15936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestemplate <> struct simplify_type<Use> {
16036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef Value *SimpleType;
16136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
162202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner};
16336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestemplate <> struct simplify_type<const Use> {
16436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef /*const*/ Value *SimpleType;
16536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
166202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner};
167202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner
16840be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h).
16940be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
17040be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
17136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
172d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
173202884fd9b575396f7ca220755cc4e3dac9b5a97Chris Lattner#endif
174