[DebugInfo] Remove unused functions from DWARFDebugAranges and fix code style.
[oota-llvm.git] / lib / DebugInfo / DWARFDebugAranges.cpp
1 //===-- DWARFDebugAranges.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
10 #include "DWARFDebugAranges.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <algorithm>
16 #include <cassert>
17 using namespace llvm;
18
19 // Compare function DWARFDebugAranges::Range structures
20 static bool RangeLessThan(const DWARFDebugAranges::Range &range1,
21                           const DWARFDebugAranges::Range &range2) {
22   return range1.LoPC < range2.LoPC;
23 }
24
25 namespace {
26   class CountArangeDescriptors {
27   public:
28     CountArangeDescriptors(uint32_t &count_ref) : Count(count_ref) {}
29     void operator()(const DWARFDebugArangeSet &Set) {
30       Count += Set.getNumDescriptors();
31     }
32     uint32_t &Count;
33   };
34
35   class AddArangeDescriptors {
36   public:
37     AddArangeDescriptors(DWARFDebugAranges::RangeColl &Ranges,
38                          DWARFDebugAranges::ParsedCUOffsetColl &CUOffsets)
39       : RangeCollection(Ranges),
40         CUOffsetCollection(CUOffsets) {}
41     void operator()(const DWARFDebugArangeSet &Set) {
42       DWARFDebugAranges::Range Range;
43       Range.Offset = Set.getCompileUnitDIEOffset();
44       CUOffsetCollection.insert(Range.Offset);
45
46       for (uint32_t i = 0, n = Set.getNumDescriptors(); i < n; ++i) {
47         const DWARFDebugArangeSet::Descriptor *ArangeDescPtr =
48             Set.getDescriptor(i);
49         Range.LoPC = ArangeDescPtr->Address;
50         Range.Length = ArangeDescPtr->Length;
51
52         // Insert each item in increasing address order so binary searching
53         // can later be done!
54         DWARFDebugAranges::RangeColl::iterator InsertPos =
55           std::lower_bound(RangeCollection.begin(), RangeCollection.end(),
56                            Range, RangeLessThan);
57         RangeCollection.insert(InsertPos, Range);
58       }
59
60     }
61     DWARFDebugAranges::RangeColl &RangeCollection;
62     DWARFDebugAranges::ParsedCUOffsetColl &CUOffsetCollection;
63   };
64 }
65
66 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
67   if (!DebugArangesData.isValidOffset(0))
68     return;
69   uint32_t offset = 0;
70
71   typedef std::vector<DWARFDebugArangeSet> SetCollection;
72   SetCollection sets;
73
74   DWARFDebugArangeSet set;
75   Range range;
76   while (set.extract(DebugArangesData, &offset))
77     sets.push_back(set);
78
79   uint32_t count = 0;
80
81   std::for_each(sets.begin(), sets.end(), CountArangeDescriptors(count));
82
83   if (count > 0) {
84     Aranges.reserve(count);
85     AddArangeDescriptors range_adder(Aranges, ParsedCUOffsets);
86     std::for_each(sets.begin(), sets.end(), range_adder);
87   }
88 }
89
90 void DWARFDebugAranges::generate(DWARFContext *CTX) {
91   if (CTX) {
92     const uint32_t num_compile_units = CTX->getNumCompileUnits();
93     for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
94       if (DWARFCompileUnit *cu = CTX->getCompileUnitAtIndex(cu_idx)) {
95         uint32_t CUOffset = cu->getOffset();
96         if (ParsedCUOffsets.insert(CUOffset).second)
97           cu->buildAddressRangeTable(this, true, CUOffset);
98       }
99     }
100   }
101   sort(true, /* overlap size */ 0);
102 }
103
104 void DWARFDebugAranges::dump(raw_ostream &OS) const {
105   for (RangeCollIterator I = Aranges.begin(), E = Aranges.end(); I != E; ++I) {
106     I->dump(OS);
107   }
108 }
109
110 void DWARFDebugAranges::Range::dump(raw_ostream &OS) const {
111   OS << format("{0x%8.8x}: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n",
112                Offset, LoPC, HiPC());
113 }
114
115 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
116                                     uint64_t HighPC) {
117   if (!Aranges.empty()) {
118     if (Aranges.back().Offset == CUOffset && Aranges.back().HiPC() == LowPC) {
119       Aranges.back().setHiPC(HighPC);
120       return;
121     }
122   }
123   Aranges.push_back(Range(LowPC, HighPC, CUOffset));
124 }
125
126 void DWARFDebugAranges::sort(bool Minimize, uint32_t OverlapSize) {
127   const size_t orig_arange_size = Aranges.size();
128   // Size of one? If so, no sorting is needed
129   if (orig_arange_size <= 1)
130     return;
131   // Sort our address range entries
132   std::stable_sort(Aranges.begin(), Aranges.end(), RangeLessThan);
133
134   if (!Minimize)
135     return;
136
137   // Most address ranges are contiguous from function to function
138   // so our new ranges will likely be smaller. We calculate the size
139   // of the new ranges since although std::vector objects can be resized,
140   // the will never reduce their allocated block size and free any excesss
141   // memory, so we might as well start a brand new collection so it is as
142   // small as possible.
143
144   // First calculate the size of the new minimal arange vector
145   // so we don't have to do a bunch of re-allocations as we
146   // copy the new minimal stuff over to the new collection.
147   size_t minimal_size = 1;
148   for (size_t i = 1; i < orig_arange_size; ++i) {
149     if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i], OverlapSize))
150       ++minimal_size;
151   }
152
153   // If the sizes are the same, then no consecutive aranges can be
154   // combined, we are done.
155   if (minimal_size == orig_arange_size)
156     return;
157
158   // Else, make a new RangeColl that _only_ contains what we need.
159   RangeColl minimal_aranges;
160   minimal_aranges.resize(minimal_size);
161   uint32_t j = 0;
162   minimal_aranges[j] = Aranges[0];
163   for (size_t i = 1; i < orig_arange_size; ++i) {
164     if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i],
165                                   OverlapSize)) {
166       minimal_aranges[j].setHiPC (Aranges[i].HiPC());
167     } else {
168       // Only increment j if we aren't merging
169       minimal_aranges[++j] = Aranges[i];
170     }
171   }
172   assert (j+1 == minimal_size);
173
174   // Now swap our new minimal aranges into place. The local
175   // minimal_aranges will then contian the old big collection
176   // which will get freed.
177   minimal_aranges.swap(Aranges);
178 }
179
180 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
181   if (!Aranges.empty()) {
182     Range range(Address);
183     RangeCollIterator begin = Aranges.begin();
184     RangeCollIterator end = Aranges.end();
185     RangeCollIterator pos = std::lower_bound(begin, end, range, RangeLessThan);
186
187     if (pos != end && pos->LoPC <= Address && Address < pos->HiPC()) {
188       return pos->Offset;
189     } else if (pos != begin) {
190       --pos;
191       if (pos->LoPC <= Address && Address < pos->HiPC())
192         return (*pos).Offset;
193     }
194   }
195   return -1U;
196 }