LLVM  10.0.0
Range.cpp
Go to the documentation of this file.
1 //===- Range.cpp ------------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
13 #include <algorithm>
14 #include <inttypes.h>
15 
16 using namespace llvm;
17 using namespace gsym;
18 
19 
21  if (Range.size() == 0)
22  return;
23 
24  auto It = llvm::upper_bound(Ranges, Range);
25  auto It2 = It;
26  while (It2 != Ranges.end() && It2->Start < Range.End)
27  ++It2;
28  if (It != It2) {
29  Range.End = std::max(Range.End, It2[-1].End);
30  It = Ranges.erase(It, It2);
31  }
32  if (It != Ranges.begin() && Range.Start < It[-1].End)
33  It[-1].End = std::max(It[-1].End, Range.End);
34  else
35  Ranges.insert(It, Range);
36 }
37 
38 bool AddressRanges::contains(uint64_t Addr) const {
39  auto It = std::partition_point(
40  Ranges.begin(), Ranges.end(),
41  [=](const AddressRange &R) { return R.Start <= Addr; });
42  return It != Ranges.begin() && Addr < It[-1].End;
43 }
44 
46  if (Range.size() == 0)
47  return false;
48  auto It = std::partition_point(
49  Ranges.begin(), Ranges.end(),
50  [=](const AddressRange &R) { return R.Start <= Range.Start; });
51  if (It == Ranges.begin())
52  return false;
53  return Range.End <= It[-1].End;
54 }
55 
57  return OS << '[' << HEX64(R.Start) << " - " << HEX64(R.End) << ")";
58 }
59 
61  size_t Size = AR.size();
62  for (size_t I = 0; I < Size; ++I) {
63  if (I)
64  OS << ' ';
65  OS << AR[I];
66  }
67  return OS;
68 }
69 
70 void AddressRange::encode(FileWriter &O, uint64_t BaseAddr) const {
71  assert(Start >= BaseAddr);
72  O.writeULEB(Start - BaseAddr);
73  O.writeULEB(size());
74 }
75 
76 void AddressRange::decode(DataExtractor &Data, uint64_t BaseAddr,
77  uint64_t &Offset) {
78  const uint64_t AddrOffset = Data.getULEB128(&Offset);
79  const uint64_t Size = Data.getULEB128(&Offset);
80  const uint64_t StartAddr = BaseAddr + AddrOffset;
81  Start = StartAddr;
82  End = StartAddr + Size;
83 }
84 
85 void AddressRanges::encode(FileWriter &O, uint64_t BaseAddr) const {
86  O.writeULEB(Ranges.size());
87  if (Ranges.empty())
88  return;
89  for (auto Range : Ranges)
90  Range.encode(O, BaseAddr);
91 }
92 
93 void AddressRanges::decode(DataExtractor &Data, uint64_t BaseAddr,
94  uint64_t &Offset) {
95  clear();
96  uint64_t NumRanges = Data.getULEB128(&Offset);
97  if (NumRanges == 0)
98  return;
99  Ranges.resize(NumRanges);
100  for (auto &Range : Ranges)
101  Range.decode(Data, BaseAddr, Offset);
102 }
103 
104 void AddressRange::skip(DataExtractor &Data, uint64_t &Offset) {
105  Data.getULEB128(&Offset);
106  Data.getULEB128(&Offset);
107 }
108 
109 uint64_t AddressRanges::skip(DataExtractor &Data, uint64_t &Offset) {
110  uint64_t NumRanges = Data.getULEB128(&Offset);
111  for (uint64_t I=0; I<NumRanges; ++I)
112  AddressRange::skip(Data, Offset);
113  return NumRanges;
114 }
#define HEX64(v)
Definition: Range.h:20
static uint64_t skip(DataExtractor &Data, uint64_t &Offset)
Skip an address range object in the specified data a the specified offset.
Definition: Range.cpp:109
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Collection Ranges
Definition: Range.h:86
void writeULEB(uint64_t Value)
Write the value into the stream encoded using unsigned LEB128 at the current file position...
Definition: FileWriter.cpp:27
void encode(FileWriter &O, uint64_t BaseAddr) const
Definition: Range.cpp:70
bool contains(uint64_t Addr) const
Definition: Range.cpp:38
void decode(DataExtractor &Data, uint64_t BaseAddr, uint64_t &Offset)
AddressRange objects are encoded and decoded to be relative to a base address.
Definition: Range.cpp:76
The AddressRanges class helps normalize address range collections.
Definition: Range.h:83
auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range))
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:1319
A simplified binary data writer class that doesn&#39;t require targets, target definitions, architectures, or require any other optional compile time libraries to be enabled via the build process.
Definition: FileWriter.h:29
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
size_t size() const
Definition: Range.h:93
A class that represents an address range.
Definition: Range.h:32
void insert(AddressRange Range)
Definition: Range.cpp:20
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
void encode(FileWriter &O, uint64_t BaseAddr) const
Definition: Range.cpp:85
void decode(DataExtractor &Data, uint64_t BaseAddr, uint64_t &Offset)
Address ranges are decoded and encoded to be relative to a base address.
Definition: Range.cpp:93
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:46
static void skip(DataExtractor &Data, uint64_t &Offset)
Skip an address range object in the specified data a the specified offset.
Definition: Range.cpp:104
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t size() const
Definition: Range.h:37
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
raw_ostream & operator<<(raw_ostream &OS, const FunctionInfo &R)
auto upper_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1293