LLVM  14.0.0git
Range.cpp
Go to the documentation of this file.
1 //===- Range.cpp ------------------------------------------------*- 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 
12 #include <algorithm>
13 #include <inttypes.h>
14 
15 using namespace llvm;
16 using namespace gsym;
17 
18 
20  if (Range.size() == 0)
21  return;
22 
23  auto It = llvm::upper_bound(Ranges, Range);
24  auto It2 = It;
25  while (It2 != Ranges.end() && It2->Start < Range.End)
26  ++It2;
27  if (It != It2) {
28  Range.End = std::max(Range.End, It2[-1].End);
29  It = Ranges.erase(It, It2);
30  }
31  if (It != Ranges.begin() && Range.Start < It[-1].End)
32  It[-1].End = std::max(It[-1].End, Range.End);
33  else
34  Ranges.insert(It, Range);
35 }
36 
38  auto It = std::partition_point(
39  Ranges.begin(), Ranges.end(),
40  [=](const AddressRange &R) { return R.Start <= Addr; });
41  return It != Ranges.begin() && Addr < It[-1].End;
42 }
43 
45  if (Range.size() == 0)
46  return false;
47  auto It = std::partition_point(
48  Ranges.begin(), Ranges.end(),
49  [=](const AddressRange &R) { return R.Start <= Range.Start; });
50  if (It == Ranges.begin())
51  return false;
52  return Range.End <= It[-1].End;
53 }
54 
57  auto It = std::partition_point(
58  Ranges.begin(), Ranges.end(),
59  [=](const AddressRange &R) { return R.Start <= Addr; });
60  if (It != Ranges.begin() && Addr < It[-1].End)
61  return It[-1];
62  return llvm::None;
63 }
64 
66  return OS << '[' << HEX64(R.Start) << " - " << HEX64(R.End) << ")";
67 }
68 
70  size_t Size = AR.size();
71  for (size_t I = 0; I < Size; ++I) {
72  if (I)
73  OS << ' ';
74  OS << AR[I];
75  }
76  return OS;
77 }
78 
79 void AddressRange::encode(FileWriter &O, uint64_t BaseAddr) const {
80  assert(Start >= BaseAddr);
81  O.writeULEB(Start - BaseAddr);
82  O.writeULEB(size());
83 }
84 
86  uint64_t &Offset) {
87  const uint64_t AddrOffset = Data.getULEB128(&Offset);
88  const uint64_t Size = Data.getULEB128(&Offset);
89  const uint64_t StartAddr = BaseAddr + AddrOffset;
90  Start = StartAddr;
91  End = StartAddr + Size;
92 }
93 
94 void AddressRanges::encode(FileWriter &O, uint64_t BaseAddr) const {
95  O.writeULEB(Ranges.size());
96  if (Ranges.empty())
97  return;
98  for (auto Range : Ranges)
99  Range.encode(O, BaseAddr);
100 }
101 
103  uint64_t &Offset) {
104  clear();
105  uint64_t NumRanges = Data.getULEB128(&Offset);
106  if (NumRanges == 0)
107  return;
108  Ranges.resize(NumRanges);
109  for (auto &Range : Ranges)
110  Range.decode(Data, BaseAddr, Offset);
111 }
112 
114  Data.getULEB128(&Offset);
115  Data.getULEB128(&Offset);
116 }
117 
119  uint64_t NumRanges = Data.getULEB128(&Offset);
120  for (uint64_t I=0; I<NumRanges; ++I)
122  return NumRanges;
123 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::gsym::AddressRange::decode
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:85
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::gsym::AddressRange::size
uint64_t size() const
Definition: Range.h:38
llvm::upper_bound
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:1674
llvm::gsym::AddressRanges::insert
void insert(AddressRange Range)
Definition: Range.cpp:19
llvm::Optional
Definition: APInt.h:33
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::gsym::AddressRange::Start
uint64_t Start
Definition: Range.h:34
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::gsym::AddressRanges::decode
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:102
llvm::gsym::operator<<
raw_ostream & operator<<(raw_ostream &OS, const FunctionInfo &R)
Definition: FunctionInfo.cpp:27
llvm::gsym::AddressRanges::size
size_t size() const
Definition: Range.h:95
llvm::gsym::AddressRanges::contains
bool contains(uint64_t Addr) const
Definition: Range.cpp:37
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::gsym::AddressRanges::clear
void clear()
Definition: Range.h:89
llvm::None
const NoneType None
Definition: None.h:23
llvm::gsym::AddressRanges::skip
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:118
llvm::gsym::AddressRanges
The AddressRanges class helps normalize address range collections.
Definition: Range.h:84
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:206
uint64_t
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::gsym::AddressRanges::getRangeThatContains
Optional< AddressRange > getRangeThatContains(uint64_t Addr) const
Definition: Range.cpp:56
FileWriter.h
llvm::gsym::FileWriter
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition: FileWriter.h:29
llvm::gsym::AddressRanges::Ranges
Collection Ranges
Definition: Range.h:87
Range.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::gsym::AddressRange::skip
static void skip(DataExtractor &Data, uint64_t &Offset)
Skip an address range object in the specified data a the specified offset.
Definition: Range.cpp:113
llvm::partition_point
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:1699
llvm::gsym::AddressRange
A class that represents an address range.
Definition: Range.h:33
DataExtractor.h
llvm::gsym::AddressRange::End
uint64_t End
Definition: Range.h:35
llvm::gsym::AddressRanges::encode
void encode(FileWriter &O, uint64_t BaseAddr) const
Definition: Range.cpp:94
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::gsym::AddressRange::encode
void encode(FileWriter &O, uint64_t BaseAddr) const
Definition: Range.cpp:79
HEX64
#define HEX64(v)
Definition: Range.h:21