1 //===- DWARFAddressRange.h --------------------------------------*- 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 #ifndef LLVM_DEBUGINFO_DWARF_DWARFADDRESSRANGE_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFADDRESSRANGE_H
11 
12 #include "llvm/DebugInfo/DIContext.h"
13 #include <cstdint>
14 #include <tuple>
15 #include <vector>
16 
17 namespace llvm {
18 
19 class raw_ostream;
20 class DWARFObject;
21 
22 struct DWARFAddressRange {
23   uint64_t LowPC;
24   uint64_t HighPC;
25   uint64_t SectionIndex;
26 
27   DWARFAddressRange() = default;
28 
29   /// Used for unit testing.
30   DWARFAddressRange(
31       uint64_t LowPC, uint64_t HighPC,
32       uint64_t SectionIndex = object::SectionedAddress::UndefSection)
33       : LowPC(LowPC), HighPC(HighPC), SectionIndex(SectionIndex) {}
34 
35   /// Returns true if LowPC is smaller or equal to HighPC. This accounts for
36   /// dead-stripped ranges.
37   bool valid() const { return LowPC <= HighPC; }
38 
39   /// Returns true if [LowPC, HighPC) intersects with [RHS.LowPC, RHS.HighPC).
40   bool intersects(const DWARFAddressRange &RHS) const {
41     assert(valid() && RHS.valid());
42     if (SectionIndex != RHS.SectionIndex)
43       return false;
44     // Empty ranges can't intersect.
45     if (LowPC == HighPC || RHS.LowPC == RHS.HighPC)
46       return false;
47     return LowPC < RHS.HighPC && RHS.LowPC < HighPC;
48   }
49 
50   /// Union two address ranges if they intersect.
51   ///
52   /// This function will union two address ranges if they intersect by
53   /// modifying this range to be the union of both ranges. If the two ranges
54   /// don't intersect this range will be left alone.
55   ///
56   /// \param RHS Another address range to combine with.
57   ///
58   /// \returns false if the ranges don't intersect, true if they do and the
59   /// ranges were combined.
60   bool merge(const DWARFAddressRange &RHS) {
61     if (!intersects(RHS))
62       return false;
63     LowPC = std::min<uint64_t>(LowPC, RHS.LowPC);
64     HighPC = std::max<uint64_t>(HighPC, RHS.HighPC);
65     return true;
66   }
67 
68   void dump(raw_ostream &OS, uint32_t AddressSize, DIDumpOptions DumpOpts = {},
69             const DWARFObject *Obj = nullptr) const;
70 };
71 
72 inline bool operator<(const DWARFAddressRange &LHS,
73                       const DWARFAddressRange &RHS) {
74   return std::tie(LHS.SectionIndex, LHS.LowPC, LHS.HighPC) < std::tie(RHS.SectionIndex, RHS.LowPC, RHS.HighPC);
75 }
76 
77 inline bool operator==(const DWARFAddressRange &LHS,
78                        const DWARFAddressRange &RHS) {
79   return std::tie(LHS.SectionIndex, LHS.LowPC, LHS.HighPC) == std::tie(RHS.SectionIndex, RHS.LowPC, RHS.HighPC);
80 }
81 
82 raw_ostream &operator<<(raw_ostream &OS, const DWARFAddressRange &R);
83 
84 /// DWARFAddressRangesVector - represents a set of absolute address ranges.
85 using DWARFAddressRangesVector = std::vector<DWARFAddressRange>;
86 
87 } // end namespace llvm
88 
89 #endif // LLVM_DEBUGINFO_DWARF_DWARFADDRESSRANGE_H
90