LLVM  11.0.0git
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 
58  auto It = std::partition_point(
59  Ranges.begin(), Ranges.end(),
60  [=](const AddressRange &R) { return R.Start <= Addr; });
61  if (It != Ranges.begin() && Addr < It[-1].End)
62  return It[-1];
63  return llvm::None;
64 }
65 
67  return OS << '[' << HEX64(R.Start) << " - " << HEX64(R.End) << ")";
68 }
69 
71  size_t Size = AR.size();
72  for (size_t I = 0; I < Size; ++I) {
73  if (I)
74  OS << ' ';
75  OS << AR[I];
76  }
77  return OS;
78 }
79 
80 void AddressRange::encode(FileWriter &O, uint64_t BaseAddr) const {
81  assert(Start >= BaseAddr);
82  O.writeULEB(Start - BaseAddr);
83  O.writeULEB(size());
84 }
85 
86 void AddressRange::decode(DataExtractor &Data, uint64_t BaseAddr,
87  uint64_t &Offset) {
88  const uint64_t AddrOffset = Data.getULEB128(&Offset);
89  const uint64_t Size = Data.getULEB128(&Offset);
90  const uint64_t StartAddr = BaseAddr + AddrOffset;
91  Start = StartAddr;
92  End = StartAddr + Size;
93 }
94 
95 void AddressRanges::encode(FileWriter &O, uint64_t BaseAddr) const {
96  O.writeULEB(Ranges.size());
97  if (Ranges.empty())
98  return;
99  for (auto Range : Ranges)
100  Range.encode(O, BaseAddr);
101 }
102 
103 void AddressRanges::decode(DataExtractor &Data, uint64_t BaseAddr,
104  uint64_t &Offset) {
105  clear();
106  uint64_t NumRanges = Data.getULEB128(&Offset);
107  if (NumRanges == 0)
108  return;
109  Ranges.resize(NumRanges);
110  for (auto &Range : Ranges)
111  Range.decode(Data, BaseAddr, Offset);
112 }
113 
114 void AddressRange::skip(DataExtractor &Data, uint64_t &Offset) {
115  Data.getULEB128(&Offset);
116  Data.getULEB128(&Offset);
117 }
118 
119 uint64_t AddressRanges::skip(DataExtractor &Data, uint64_t &Offset) {
120  uint64_t NumRanges = Data.getULEB128(&Offset);
121  for (uint64_t I=0; I<NumRanges; ++I)
122  AddressRange::skip(Data, Offset);
123  return NumRanges;
124 }
#define HEX64(v)
Definition: Range.h:21
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:119
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Collection Ranges
Definition: Range.h:87
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:80
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:86
The AddressRanges class helps normalize address range collections.
Definition: Range.h:84
Optional< AddressRange > getRangeThatContains(uint64_t Addr) const
Definition: Range.cpp:57
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1607
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:1632
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:95
A class that represents an address range.
Definition: Range.h:33
void insert(AddressRange Range)
Definition: Range.cpp:20
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
void encode(FileWriter &O, uint64_t BaseAddr) const
Definition: Range.cpp:95
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:103
#define I(x, y, z)
Definition: MD5.cpp:59
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:114
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t size() const
Definition: Range.h:38
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)