[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / lib / DebugInfo / DWARF / DWARFDebugAranges.cpp
blobca6043109cdb862f99edb9242c74f836d1f8c3e8
1 //===- DWARFDebugAranges.cpp ----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
10 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
13 #include "llvm/Support/DataExtractor.h"
14 #include "llvm/Support/WithColor.h"
15 #include <algorithm>
16 #include <cassert>
17 #include <cstdint>
18 #include <set>
19 #include <vector>
21 using namespace llvm;
23 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
24 if (!DebugArangesData.isValidOffset(0))
25 return;
26 uint64_t Offset = 0;
27 DWARFDebugArangeSet Set;
29 while (Set.extract(DebugArangesData, &Offset)) {
30 uint64_t CUOffset = Set.getCompileUnitDIEOffset();
31 for (const auto &Desc : Set.descriptors()) {
32 uint64_t LowPC = Desc.Address;
33 uint64_t HighPC = Desc.getEndAddress();
34 appendRange(CUOffset, LowPC, HighPC);
36 ParsedCUOffsets.insert(CUOffset);
40 void DWARFDebugAranges::generate(DWARFContext *CTX) {
41 clear();
42 if (!CTX)
43 return;
45 // Extract aranges from .debug_aranges section.
46 DataExtractor ArangesData(CTX->getDWARFObj().getArangesSection(),
47 CTX->isLittleEndian(), 0);
48 extract(ArangesData);
50 // Generate aranges from DIEs: even if .debug_aranges section is present,
51 // it may describe only a small subset of compilation units, so we need to
52 // manually build aranges for the rest of them.
53 for (const auto &CU : CTX->compile_units()) {
54 uint64_t CUOffset = CU->getOffset();
55 if (ParsedCUOffsets.insert(CUOffset).second) {
56 Expected<DWARFAddressRangesVector> CURanges = CU->collectAddressRanges();
57 if (!CURanges)
58 WithColor::error() << toString(CURanges.takeError()) << '\n';
59 else
60 for (const auto &R : *CURanges)
61 appendRange(CUOffset, R.LowPC, R.HighPC);
65 construct();
68 void DWARFDebugAranges::clear() {
69 Endpoints.clear();
70 Aranges.clear();
71 ParsedCUOffsets.clear();
74 void DWARFDebugAranges::appendRange(uint64_t CUOffset, uint64_t LowPC,
75 uint64_t HighPC) {
76 if (LowPC >= HighPC)
77 return;
78 Endpoints.emplace_back(LowPC, CUOffset, true);
79 Endpoints.emplace_back(HighPC, CUOffset, false);
82 void DWARFDebugAranges::construct() {
83 std::multiset<uint64_t> ValidCUs; // Maintain the set of CUs describing
84 // a current address range.
85 llvm::sort(Endpoints);
86 uint64_t PrevAddress = -1ULL;
87 for (const auto &E : Endpoints) {
88 if (PrevAddress < E.Address && !ValidCUs.empty()) {
89 // If the address range between two endpoints is described by some
90 // CU, first try to extend the last range in Aranges. If we can't
91 // do it, start a new range.
92 if (!Aranges.empty() && Aranges.back().HighPC() == PrevAddress &&
93 ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()) {
94 Aranges.back().setHighPC(E.Address);
95 } else {
96 Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
99 // Update the set of valid CUs.
100 if (E.IsRangeStart) {
101 ValidCUs.insert(E.CUOffset);
102 } else {
103 auto CUPos = ValidCUs.find(E.CUOffset);
104 assert(CUPos != ValidCUs.end());
105 ValidCUs.erase(CUPos);
107 PrevAddress = E.Address;
109 assert(ValidCUs.empty());
111 // Endpoints are not needed now.
112 Endpoints.clear();
113 Endpoints.shrink_to_fit();
116 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
117 RangeCollIterator It =
118 partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
119 if (It != Aranges.end() && It->LowPC <= Address)
120 return It->CUOffset;
121 return -1U;