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