LLVM  16.0.0git
ExecutorAddress.h
Go to the documentation of this file.
1 //===------ ExecutorAddress.h - Executing process address -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Represents an address in the executing program.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
14 #define LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
15 
16 #include "llvm/ADT/DenseMapInfo.h"
17 #include "llvm/ADT/identity.h"
21 
22 #include <cassert>
23 #include <type_traits>
24 
25 namespace llvm {
26 namespace orc {
27 
29 
30 /// Represents an address in the executor process.
31 class ExecutorAddr {
32 public:
33  /// A wrap/unwrap function that leaves pointers unmodified.
34  template <typename T> using rawPtr = llvm::identity<T *>;
35 
36  /// Default wrap function to use on this host.
37  template <typename T> using defaultWrap = rawPtr<T>;
38 
39  /// Default unwrap function to use on this host.
40  template <typename T> using defaultUnwrap = rawPtr<T>;
41 
42  /// Merges a tag into the raw address value:
43  /// P' = P | (TagValue << TagOffset).
44  class Tag {
45  public:
46  constexpr Tag(uintptr_t TagValue, uintptr_t TagOffset)
47  : TagMask(TagValue << TagOffset) {}
48 
49  template <typename T> constexpr T *operator()(T *P) {
50  return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(P) | TagMask);
51  }
52 
53  private:
54  uintptr_t TagMask;
55  };
56 
57  /// Strips a tag of the given length from the given offset within the pointer:
58  /// P' = P & ~(((1 << TagLen) -1) << TagOffset)
59  class Untag {
60  public:
61  constexpr Untag(uintptr_t TagLen, uintptr_t TagOffset)
62  : UntagMask(~(((uintptr_t(1) << TagLen) - 1) << TagOffset)) {}
63 
64  template <typename T> constexpr T *operator()(T *P) {
65  return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(P) & UntagMask);
66  }
67 
68  private:
69  uintptr_t UntagMask;
70  };
71 
72  ExecutorAddr() = default;
73 
74  /// Create an ExecutorAddr from the given value.
75  explicit constexpr ExecutorAddr(uint64_t Addr) : Addr(Addr) {}
76 
77  /// Create an ExecutorAddr from the given pointer.
78  /// Warning: This should only be used when JITing in-process.
79  template <typename T, typename UnwrapFn = defaultUnwrap<T>>
80  static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap = UnwrapFn()) {
81  return ExecutorAddr(
82  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Unwrap(Ptr))));
83  }
84 
85  /// Cast this ExecutorAddr to a pointer of the given type.
86  /// Warning: This should only be used when JITing in-process.
87  template <typename T, typename WrapFn = defaultWrap<std::remove_pointer_t<T>>>
88  std::enable_if_t<std::is_pointer<T>::value, T>
89  toPtr(WrapFn &&Wrap = WrapFn()) const {
90  uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
91  assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t");
92  return Wrap(reinterpret_cast<T>(IntPtr));
93  }
94 
95  /// Cast this ExecutorAddr to a pointer of the given function type.
96  /// Warning: This should only be used when JITing in-process.
97  template <typename T, typename WrapFn = defaultWrap<T>>
98  std::enable_if_t<std::is_function<T>::value, T *>
99  toPtr(WrapFn &&Wrap = WrapFn()) const {
100  uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
101  assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t");
102  return Wrap(reinterpret_cast<T *>(IntPtr));
103  }
104 
105  uint64_t getValue() const { return Addr; }
106  void setValue(uint64_t Addr) { this->Addr = Addr; }
107  bool isNull() const { return Addr == 0; }
108 
109  explicit operator bool() const { return Addr != 0; }
110 
111  friend bool operator==(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
112  return LHS.Addr == RHS.Addr;
113  }
114 
115  friend bool operator!=(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
116  return LHS.Addr != RHS.Addr;
117  }
118 
119  friend bool operator<(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
120  return LHS.Addr < RHS.Addr;
121  }
122 
123  friend bool operator<=(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
124  return LHS.Addr <= RHS.Addr;
125  }
126 
127  friend bool operator>(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
128  return LHS.Addr > RHS.Addr;
129  }
130 
131  friend bool operator>=(const ExecutorAddr &LHS, const ExecutorAddr &RHS) {
132  return LHS.Addr >= RHS.Addr;
133  }
134 
136  ++Addr;
137  return *this;
138  }
140  --Addr;
141  return *this;
142  }
143  ExecutorAddr operator++(int) { return ExecutorAddr(Addr++); }
144  ExecutorAddr operator--(int) { return ExecutorAddr(Addr--); }
145 
147  Addr += Delta;
148  return *this;
149  }
150 
152  Addr -= Delta;
153  return *this;
154  }
155 
156 private:
157  uint64_t Addr = 0;
158 };
159 
160 /// Subtracting two addresses yields an offset.
162  const ExecutorAddr &RHS) {
163  return ExecutorAddrDiff(LHS.getValue() - RHS.getValue());
164 }
165 
166 /// Adding an offset and an address yields an address.
168  const ExecutorAddrDiff &RHS) {
169  return ExecutorAddr(LHS.getValue() + RHS);
170 }
171 
172 /// Adding an address and an offset yields an address.
174  const ExecutorAddr &RHS) {
175  return ExecutorAddr(LHS + RHS.getValue());
176 }
177 
178 /// Subtracting an offset from an address yields an address.
180  const ExecutorAddrDiff &RHS) {
181  return ExecutorAddr(LHS.getValue() - RHS);
182 }
183 
184 /// Taking the modulus of an address and a diff yields a diff.
186  const ExecutorAddrDiff &RHS) {
187  return ExecutorAddrDiff(LHS.getValue() % RHS);
188 }
189 
190 /// Represents an address range in the exceutor process.
192  ExecutorAddrRange() = default;
194  : Start(Start), End(End) {}
196  : Start(Start), End(Start + Size) {}
197 
198  bool empty() const { return Start == End; }
199  ExecutorAddrDiff size() const { return End - Start; }
200 
201  friend bool operator==(const ExecutorAddrRange &LHS,
202  const ExecutorAddrRange &RHS) {
203  return LHS.Start == RHS.Start && LHS.End == RHS.End;
204  }
205  friend bool operator!=(const ExecutorAddrRange &LHS,
206  const ExecutorAddrRange &RHS) {
207  return !(LHS == RHS);
208  }
209  bool contains(ExecutorAddr Addr) const { return Start <= Addr && Addr < End; }
210  bool overlaps(const ExecutorAddrRange &Other) {
211  return !(Other.End <= Start || End <= Other.Start);
212  }
213 
216 };
217 
219  return OS << formatv("{0:x}", A.getValue());
220 }
221 
223  return OS << formatv("{0:x} -- {1:x}", R.Start.getValue(), R.End.getValue());
224 }
225 
226 namespace shared {
227 
229 
230 /// SPS serializatior for ExecutorAddr.
232 public:
233  static size_t size(const ExecutorAddr &EA) {
235  }
236 
237  static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddr &EA) {
238  return SPSArgList<uint64_t>::serialize(BOB, EA.getValue());
239  }
240 
241  static bool deserialize(SPSInputBuffer &BIB, ExecutorAddr &EA) {
242  uint64_t Tmp;
243  if (!SPSArgList<uint64_t>::deserialize(BIB, Tmp))
244  return false;
245  EA = ExecutorAddr(Tmp);
246  return true;
247  }
248 };
249 
251 
252 /// Serialization traits for address ranges.
253 template <>
255 public:
256  static size_t size(const ExecutorAddrRange &Value) {
258  Value.End);
259  }
260 
261  static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddrRange &Value) {
263  BOB, Value.Start, Value.End);
264  }
265 
268  BIB, Value.Start, Value.End);
269  }
270 };
271 
273 
274 } // End namespace shared.
275 } // End namespace orc.
276 
277 // Provide DenseMapInfo for ExecutorAddrs.
278 template <> struct DenseMapInfo<orc::ExecutorAddr> {
279  static inline orc::ExecutorAddr getEmptyKey() {
281  }
284  }
285 
286  static unsigned getHashValue(const orc::ExecutorAddr &Addr) {
287  return DenseMapInfo<uint64_t>::getHashValue(Addr.getValue());
288  }
289 
290  static bool isEqual(const orc::ExecutorAddr &LHS,
291  const orc::ExecutorAddr &RHS) {
292  return DenseMapInfo<uint64_t>::isEqual(LHS.getValue(), RHS.getValue());
293  }
294 };
295 
296 } // End namespace llvm.
297 
298 #endif // LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:31
llvm::orc::ExecutorAddr::operator<=
friend bool operator<=(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Definition: ExecutorAddress.h:123
llvm::orc::ExecutorAddr::Tag
Merges a tag into the raw address value: P' = P | (TagValue << TagOffset).
Definition: ExecutorAddress.h:44
llvm::orc::operator<<
raw_ostream & operator<<(raw_ostream &OS, const SymbolStringPtr &Sym)
Render a SymbolStringPtr.
Definition: DebugUtils.cpp:141
llvm::orc::ExecutorAddr::fromPtr
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
Definition: ExecutorAddress.h:80
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::orc::shared::SPSSequence
SPS tag type for sequences.
Definition: SimplePackedSerialization.h:205
llvm::orc::ExecutorAddr::Untag::operator()
constexpr T * operator()(T *P)
Definition: ExecutorAddress.h:64
llvm::orc::shared::SPSSerializationTraits
Specialize to describe how to serialize/deserialize to/from the given concrete type.
Definition: SimplePackedSerialization.h:105
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::orc::shared::SPSOutputBuffer
Output char buffer with overflow check.
Definition: SimplePackedSerialization.h:54
llvm::orc::ExecutorAddr::getValue
uint64_t getValue() const
Definition: ExecutorAddress.h:105
llvm::orc::ExecutorAddrRange::ExecutorAddrRange
ExecutorAddrRange()=default
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::orc::shared::SPSInputBuffer
Input char buffer with underflow check.
Definition: SimplePackedSerialization.h:74
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::orc::shared::SPSSerializationTraits< SPSExecutorAddr, ExecutorAddr >::serialize
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddr &EA)
Definition: ExecutorAddress.h:237
llvm::orc::shared::SPSTuple
SPS tag type for tuples.
Definition: SimplePackedSerialization.h:195
llvm::orc::shared::SPSSerializationTraits< SPSExecutorAddr, ExecutorAddr >::size
static size_t size(const ExecutorAddr &EA)
Definition: ExecutorAddress.h:233
llvm::orc::ExecutorAddr::operator>
friend bool operator>(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Definition: ExecutorAddress.h:127
llvm::orc::operator+
ExecutorAddr operator+(const ExecutorAddr &LHS, const ExecutorAddrDiff &RHS)
Adding an offset and an address yields an address.
Definition: ExecutorAddress.h:167
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:251
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: APInt.h:34
llvm::orc::ExecutorAddr::toPtr
std::enable_if_t< std::is_pointer< T >::value, T > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given type.
Definition: ExecutorAddress.h:89
llvm::orc::ExecutorAddr::isNull
bool isNull() const
Definition: ExecutorAddress.h:107
llvm::orc::ExecutorAddr::Untag::Untag
constexpr Untag(uintptr_t TagLen, uintptr_t TagOffset)
Definition: ExecutorAddress.h:61
llvm::orc::operator-
ExecutorAddrDiff operator-(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Subtracting two addresses yields an offset.
Definition: ExecutorAddress.h:161
llvm::orc::ExecutorAddr::Tag::operator()
constexpr T * operator()(T *P)
Definition: ExecutorAddress.h:49
llvm::orc::shared::SPSExecutorAddr
Definition: ExecutorAddress.h:228
llvm::orc::ExecutorAddrDiff
uint64_t ExecutorAddrDiff
Definition: ExecutorAddress.h:28
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::orc::ExecutorAddrRange
Represents an address range in the exceutor process.
Definition: ExecutorAddress.h:191
llvm::orc::ExecutorAddr::setValue
void setValue(uint64_t Addr)
Definition: ExecutorAddress.h:106
FormatVariadic.h
llvm::DenseMapInfo< orc::ExecutorAddr >::isEqual
static bool isEqual(const orc::ExecutorAddr &LHS, const orc::ExecutorAddr &RHS)
Definition: ExecutorAddress.h:290
llvm::orc::ExecutorAddr::Untag
Strips a tag of the given length from the given offset within the pointer: P' = P & ~(((1 << TagLen) ...
Definition: ExecutorAddress.h:59
llvm::orc::ExecutorAddr::operator--
ExecutorAddr & operator--()
Definition: ExecutorAddress.h:139
llvm::orc::shared::SPSSerializationTraits< SPSExecutorAddrRange, ExecutorAddrRange >::serialize
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddrRange &Value)
Definition: ExecutorAddress.h:261
llvm::orc::ExecutorAddrRange::size
ExecutorAddrDiff size() const
Definition: ExecutorAddress.h:199
llvm::orc::ExecutorAddrRange::End
ExecutorAddr End
Definition: ExecutorAddress.h:215
llvm::orc::shared::SPSSerializationTraits< SPSExecutorAddrRange, ExecutorAddrRange >::size
static size_t size(const ExecutorAddrRange &Value)
Definition: ExecutorAddress.h:256
uint64_t
llvm::orc::ExecutorAddrRange::ExecutorAddrRange
ExecutorAddrRange(ExecutorAddr Start, ExecutorAddr End)
Definition: ExecutorAddress.h:193
llvm::DenseMapInfo< orc::ExecutorAddr >::getHashValue
static unsigned getHashValue(const orc::ExecutorAddr &Addr)
Definition: ExecutorAddress.h:286
llvm::orc::shared::SPSSerializationTraits< SPSExecutorAddr, ExecutorAddr >::deserialize
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddr &EA)
Definition: ExecutorAddress.h:241
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:78
size
i< reg-> size
Definition: README.txt:166
llvm::orc::ExecutorAddrRange::empty
bool empty() const
Definition: ExecutorAddress.h:198
llvm::orc::ExecutorAddr::operator++
ExecutorAddr & operator++()
Definition: ExecutorAddress.h:135
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::orc::ExecutorAddr::operator+=
ExecutorAddr & operator+=(const ExecutorAddrDiff &Delta)
Definition: ExecutorAddress.h:146
Ptr
@ Ptr
Definition: TargetLibraryInfo.cpp:60
llvm::orc::ExecutorAddrRange::overlaps
bool overlaps(const ExecutorAddrRange &Other)
Definition: ExecutorAddress.h:210
llvm::orc::ExecutorAddrRange::Start
ExecutorAddr Start
Definition: ExecutorAddress.h:214
SimplePackedSerialization.h
llvm::orc::ExecutorAddr::ExecutorAddr
constexpr ExecutorAddr(uint64_t Addr)
Create an ExecutorAddr from the given value.
Definition: ExecutorAddress.h:75
llvm::orc::ExecutorAddr::operator!=
friend bool operator!=(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Definition: ExecutorAddress.h:115
llvm::orc::ExecutorAddr::operator++
ExecutorAddr operator++(int)
Definition: ExecutorAddress.h:143
llvm::orc::ExecutorAddr::Tag::Tag
constexpr Tag(uintptr_t TagValue, uintptr_t TagOffset)
Definition: ExecutorAddress.h:46
llvm::orc::shared::SPSSerializationTraits< SPSExecutorAddrRange, ExecutorAddrRange >::deserialize
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddrRange &Value)
Definition: ExecutorAddress.h:266
llvm::orc::ExecutorAddr::operator==
friend bool operator==(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Definition: ExecutorAddress.h:111
llvm::orc::ExecutorAddr::operator--
ExecutorAddr operator--(int)
Definition: ExecutorAddress.h:144
llvm::identity
Definition: identity.h:22
llvm::orc::ExecutorAddr::ExecutorAddr
ExecutorAddr()=default
llvm::orc::ExecutorAddrRange::operator!=
friend bool operator!=(const ExecutorAddrRange &LHS, const ExecutorAddrRange &RHS)
Definition: ExecutorAddress.h:205
llvm::DenseMapInfo< orc::ExecutorAddr >::getTombstoneKey
static orc::ExecutorAddr getTombstoneKey()
Definition: ExecutorAddress.h:282
llvm::orc::ExecutorAddr::operator-=
ExecutorAddr & operator-=(const ExecutorAddrDiff &Delta)
Definition: ExecutorAddress.h:151
llvm::orc::ExecutorAddr::operator<
friend bool operator<(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Definition: ExecutorAddress.h:119
isEqual
static bool isEqual(const Function &Caller, const Function &Callee)
Definition: Attributes.cpp:1869
llvm::orc::ExecutorAddr::toPtr
std::enable_if_t< std::is_function< T >::value, T * > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given function type.
Definition: ExecutorAddress.h:99
identity.h
llvm::orc::ExecutorAddrRange::ExecutorAddrRange
ExecutorAddrRange(ExecutorAddr Start, ExecutorAddrDiff Size)
Definition: ExecutorAddress.h:195
llvm::orc::operator%
ExecutorAddrDiff operator%(const ExecutorAddr &LHS, const ExecutorAddrDiff &RHS)
Taking the modulus of an address and a diff yields a diff.
Definition: ExecutorAddress.h:185
DenseMapInfo.h
llvm::orc::ExecutorAddrRange::operator==
friend bool operator==(const ExecutorAddrRange &LHS, const ExecutorAddrRange &RHS)
Definition: ExecutorAddress.h:201
raw_ostream.h
llvm::DenseMapInfo< orc::ExecutorAddr >::getEmptyKey
static orc::ExecutorAddr getEmptyKey()
Definition: ExecutorAddress.h:279
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::orc::shared::SPSArgList
A utility class for serializing to a blob from a variadic list.
Definition: SimplePackedSerialization.h:108
llvm::orc::ExecutorAddrRange::contains
bool contains(ExecutorAddr Addr) const
Definition: ExecutorAddress.h:209
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1247
llvm::orc::ExecutorAddr::operator>=
friend bool operator>=(const ExecutorAddr &LHS, const ExecutorAddr &RHS)
Definition: ExecutorAddress.h:131