summaryrefslogtreecommitdiffstats
path: root/build/build-clang/llvmorg-17-init-994-g1e72920c8859.patch
blob: 12d817fbe0ae38b4218c133879cd6bedb446ce60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
From 4cb60673a0a25a25d171716c5b90e7a3368d434f Mon Sep 17 00:00:00 2001
From: Alexey Lapshin <a.v.lapshin@mail.ru>
Date: Mon, 30 Jan 2023 15:05:53 +0100
Subject: [PATCH] [dsymutil] dsymutil produces broken lines info (probably)
 with LTO on mac

This patch fixes #60307 issue. The 8bb4451 introduces the possibility
to unite overlapped or adjacent address ranges to keep address ranges
in an unambiguous state. The AddressRangesMap is used to normalize
address ranges. The AddressRangesMap keeps address ranges and the value
of the relocated address. For intersected range, it creates a united
range that keeps the last inserted mapping value. The same for adjusted ranges.
While it is OK to use the last inserted mapping value for intersected ranges
(as there is no way how to resolve ambiguity) It is not OK to use the
last inserted value for adjacent address ranges. Currently, two following
address ranges are united into a single one:

{0,24,17e685c} {24,d8,55afe20} -> {0,d8,55afe20}

To avoid the problem, the AddressRangesMap should not unite adjacent address ranges
with different relocated addresses. Instead, it should leave adjacent address ranges
as separate ranges. So, the ranges should look like this:

{0,24,17e685c} {24,d8,55afe20}

Differential Revision: https://reviews.llvm.org/D142936
---
 llvm/include/llvm/ADT/AddressRanges.h         | 206 ++++++++-----
 .../llvm/DWARFLinker/DWARFLinkerCompileUnit.h |   2 +-
 llvm/lib/DWARFLinker/DWARFLinker.cpp          |  36 +--
 llvm/lib/DWARFLinker/DWARFStreamer.cpp        |   5 +-
 llvm/lib/Support/AddressRanges.cpp            |  70 -----
 llvm/lib/Support/CMakeLists.txt               |   1 -
 llvm/unittests/Support/AddressRangeTest.cpp   | 285 +++++++++++++++---
 7 files changed, 398 insertions(+), 207 deletions(-)
 delete mode 100644 llvm/lib/Support/AddressRanges.cpp

diff --git a/llvm/include/llvm/ADT/AddressRanges.h b/llvm/include/llvm/ADT/AddressRanges.h
index f2052d82e7c1..415d30bbb5cf 100644
--- a/llvm/include/llvm/ADT/AddressRanges.h
+++ b/llvm/include/llvm/ADT/AddressRanges.h
@@ -28,7 +28,11 @@ public:
   uint64_t start() const { return Start; }
   uint64_t end() const { return End; }
   uint64_t size() const { return End - Start; }
+  uint64_t empty() const { return size() == 0; }
   bool contains(uint64_t Addr) const { return Start <= Addr && Addr < End; }
+  bool contains(const AddressRange &R) const {
+    return Start <= R.Start && R.End <= End;
+  }
   bool intersects(const AddressRange &R) const {
     return Start < R.End && R.Start < End;
   }
@@ -45,101 +49,163 @@ private:
   uint64_t End = 0;
 };
 
-/// The AddressRanges class helps normalize address range collections.
-/// This class keeps a sorted vector of AddressRange objects and can perform
-/// insertions and searches efficiently. The address ranges are always sorted
-/// and never contain any invalid or empty address ranges.
-/// Intersecting([100,200), [150,300)) and adjacent([100,200), [200,300))
-/// address ranges are combined during insertion.
-class AddressRanges {
+/// The AddressRangesBase class presents the base functionality for the
+/// normalized address ranges collection. This class keeps a sorted vector
+/// of AddressRange-like objects and can perform searches efficiently.
+/// The address ranges are always sorted and never contain any invalid,
+/// empty or intersected address ranges.
+
+template <typename T> class AddressRangesBase {
 protected:
-  using Collection = SmallVector<AddressRange>;
+  using Collection = SmallVector<T>;
   Collection Ranges;
 
 public:
   void clear() { Ranges.clear(); }
   bool empty() const { return Ranges.empty(); }
-  bool contains(uint64_t Addr) const { return find(Addr) != Ranges.end(); }
+  bool contains(uint64_t Addr) const {
+    return find(Addr, Addr + 1) != Ranges.end();
+  }
   bool contains(AddressRange Range) const {
-    return find(Range) != Ranges.end();
+    return find(Range.start(), Range.end()) != Ranges.end();
   }
-  std::optional<AddressRange> getRangeThatContains(uint64_t Addr) const {
-    Collection::const_iterator It = find(Addr);
+  void reserve(size_t Capacity) { Ranges.reserve(Capacity); }
+  size_t size() const { return Ranges.size(); }
+
+  std::optional<T> getRangeThatContains(uint64_t Addr) const {
+    typename Collection::const_iterator It = find(Addr, Addr + 1);
     if (It == Ranges.end())
       return std::nullopt;
 
     return *It;
   }
-  Collection::const_iterator insert(AddressRange Range);
-  void reserve(size_t Capacity) { Ranges.reserve(Capacity); }
-  size_t size() const { return Ranges.size(); }
-  bool operator==(const AddressRanges &RHS) const {
-    return Ranges == RHS.Ranges;
-  }
-  const AddressRange &operator[](size_t i) const {
+
+  typename Collection::const_iterator begin() const { return Ranges.begin(); }
+  typename Collection::const_iterator end() const { return Ranges.end(); }
+
+  const T &operator[](size_t i) const {
     assert(i < Ranges.size());
     return Ranges[i];
   }
-  Collection::const_iterator begin() const { return Ranges.begin(); }
-  Collection::const_iterator end() const { return Ranges.end(); }
+
+  bool operator==(const AddressRangesBase<T> &RHS) const {
+    return Ranges == RHS.Ranges;
+  }
 
 protected:
-  Collection::const_iterator find(uint64_t Addr) const;
-  Collection::const_iterator find(AddressRange Range) const;
+  typename Collection::const_iterator find(uint64_t Start, uint64_t End) const {
+    if (Start >= End)
+      return Ranges.end();
+
+    auto It =
+        std::partition_point(Ranges.begin(), Ranges.end(), [=](const T &R) {
+          return AddressRange(R).start() <= Start;
+        });
+
+    if (It == Ranges.begin())
+      return Ranges.end();
+
+    --It;
+    if (End > AddressRange(*It).end())
+      return Ranges.end();
+
+    return It;
+  }
 };
 
-/// AddressRangesMap class maps values to the address ranges.
-/// It keeps address ranges and corresponding values. If ranges
-/// are combined during insertion, then combined range keeps
-/// newly inserted value.
-template <typename T> class AddressRangesMap : protected AddressRanges {
+/// The AddressRanges class helps normalize address range collections.
+/// This class keeps a sorted vector of AddressRange objects and can perform
+/// insertions and searches efficiently. Intersecting([100,200), [150,300))
+/// and adjacent([100,200), [200,300)) address ranges are combined during
+/// insertion.
+class AddressRanges : public AddressRangesBase<AddressRange> {
 public:
-  void clear() {
-    Ranges.clear();
-    Values.clear();
+  Collection::const_iterator insert(AddressRange Range) {
+    if (Range.empty())
+      return Ranges.end();
+
+    auto It = llvm::upper_bound(Ranges, Range);
+    auto It2 = It;
+    while (It2 != Ranges.end() && It2->start() <= Range.end())
+      ++It2;
+    if (It != It2) {
+      Range = {Range.start(), std::max(Range.end(), std::prev(It2)->end())};
+      It = Ranges.erase(It, It2);
+    }
+    if (It != Ranges.begin() && Range.start() <= std::prev(It)->end()) {
+      --It;
+      *It = {It->start(), std::max(It->end(), Range.end())};
+      return It;
+    }
+
+    return Ranges.insert(It, Range);
   }
-  bool empty() const { return AddressRanges::empty(); }
-  bool contains(uint64_t Addr) const { return AddressRanges::contains(Addr); }
-  bool contains(AddressRange Range) const {
-    return AddressRanges::contains(Range);
-  }
-  void insert(AddressRange Range, T Value) {
-    size_t InputSize = Ranges.size();
-    Collection::const_iterator RangesIt = AddressRanges::insert(Range);
-    if (RangesIt == Ranges.end())
-      return;
+};
 
-    // make Values match to Ranges.
-    size_t Idx = RangesIt - Ranges.begin();
-    typename ValuesCollection::iterator ValuesIt = Values.begin() + Idx;
-    if (InputSize < Ranges.size())
-      Values.insert(ValuesIt, T());
-    else if (InputSize > Ranges.size())
-      Values.erase(ValuesIt, ValuesIt + InputSize - Ranges.size());
-    assert(Ranges.size() == Values.size());
-
-    // set value to the inserted or combined range.
-    Values[Idx] = Value;
-  }
-  size_t size() const {
-    assert(Ranges.size() == Values.size());
-    return AddressRanges::size();
-  }
-  std::optional<std::pair<AddressRange, T>>
-  getRangeValueThatContains(uint64_t Addr) const {
-    Collection::const_iterator It = find(Addr);
-    if (It == Ranges.end())
-      return std::nullopt;
+class AddressRangeValuePair {
+public:
+  operator AddressRange() const { return Range; }
 
-    return std::make_pair(*It, Values[It - Ranges.begin()]);
-  }
-  std::pair<AddressRange, T> operator[](size_t Idx) const {
-    return std::make_pair(Ranges[Idx], Values[Idx]);
-  }
+  AddressRange Range;
+  int64_t Value = 0;
+};
 
-protected:
-  using ValuesCollection = SmallVector<T>;
-  ValuesCollection Values;
+inline bool operator==(const AddressRangeValuePair &LHS,
+                       const AddressRangeValuePair &RHS) {
+  return LHS.Range == RHS.Range && LHS.Value == RHS.Value;
+}
+
+/// AddressRangesMap class maps values to the address ranges.
+/// It keeps normalized address ranges and corresponding values.
+/// This class keeps a sorted vector of AddressRangeValuePair objects
+/// and can perform insertions and searches efficiently.
+/// Intersecting([100,200), [150,300)) ranges splitted into non-conflicting
+/// parts([100,200), [200,300)). Adjacent([100,200), [200,300)) address
+/// ranges are not combined during insertion.
+class AddressRangesMap : public AddressRangesBase<AddressRangeValuePair> {
+public:
+  void insert(AddressRange Range, int64_t Value) {
+    if (Range.empty())
+      return;
+
+    // Search for range which is less than or equal incoming Range.
+    auto It = std::partition_point(Ranges.begin(), Ranges.end(),
+                                   [=](const AddressRangeValuePair &R) {
+                                     return R.Range.start() <= Range.start();
+                                   });
+
+    if (It != Ranges.begin())
+      It--;
+
+    while (!Range.empty()) {
+      // Inserted range does not overlap with any range.
+      // Store it into the Ranges collection.
+      if (It == Ranges.end() || Range.end() <= It->Range.start()) {
+        Ranges.insert(It, {Range, Value});
+        return;
+      }
+
+      // Inserted range partially overlaps with current range.
+      // Store not overlapped part of inserted range.
+      if (Range.start() < It->Range.start()) {
+        It = Ranges.insert(It, {{Range.start(), It->Range.start()}, Value});
+        It++;
+        Range = {It->Range.start(), Range.end()};
+        continue;
+      }
+
+      // Inserted range fully overlaps with current range.
+      if (Range.end() <= It->Range.end())
+        return;
+
+      // Inserted range partially overlaps with current range.
+      // Remove overlapped part from the inserted range.
+      if (Range.start() < It->Range.end())
+        Range = {It->Range.end(), Range.end()};
+
+      It++;
+    }
+  }
 };
 
 } // namespace llvm
diff --git a/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h b/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h
index 5b0ea339c4d6..9c7f24e69d48 100644
--- a/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h
+++ b/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h
@@ -21,7 +21,7 @@ class DeclContext;
 
 /// Mapped value in the address map is the offset to apply to the
 /// linked address.
-using RangesTy = AddressRangesMap<int64_t>;
+using RangesTy = AddressRangesMap;
 
 // FIXME: Delete this structure.
 struct PatchLocation {
diff --git a/llvm/lib/DWARFLinker/DWARFLinker.cpp b/llvm/lib/DWARFLinker/DWARFLinker.cpp
index 9f6e54377ede..d302d61894fa 100644
--- a/llvm/lib/DWARFLinker/DWARFLinker.cpp
+++ b/llvm/lib/DWARFLinker/DWARFLinker.cpp
@@ -1659,7 +1659,7 @@ void DWARFLinker::patchRangesForUnit(const CompileUnit &Unit,
   DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
                                     OrigDwarf.getDWARFObj().getRangesSection(),
                                     OrigDwarf.isLittleEndian(), AddressSize);
-  std::optional<std::pair<AddressRange, int64_t>> CachedRange;
+  std::optional<AddressRangeValuePair> CachedRange;
   DWARFUnit &OrigUnit = Unit.getOrigUnit();
   auto OrigUnitDie = OrigUnit.getUnitDIE(false);
   uint64_t UnitBaseAddress =
@@ -1687,9 +1687,9 @@ void DWARFLinker::patchRangesForUnit(const CompileUnit &Unit,
         }
 
         if (!CachedRange ||
-            !CachedRange->first.contains(Range.StartAddress + BaseAddress))
-          CachedRange = FunctionRanges.getRangeValueThatContains(
-              Range.StartAddress + BaseAddress);
+            !CachedRange->Range.contains(Range.StartAddress + BaseAddress))
+          CachedRange = FunctionRanges.getRangeThatContains(Range.StartAddress +
+                                                            BaseAddress);
 
         // All range entries should lie in the function range.
         if (!CachedRange) {
@@ -1698,8 +1698,8 @@ void DWARFLinker::patchRangesForUnit(const CompileUnit &Unit,
         }
 
         LinkedRanges.insert(
-            {Range.StartAddress + BaseAddress + CachedRange->second,
-             Range.EndAddress + BaseAddress + CachedRange->second});
+            {Range.StartAddress + BaseAddress + CachedRange->Value,
+             Range.EndAddress + BaseAddress + CachedRange->Value});
       }
     }
 
@@ -1802,7 +1802,7 @@ void DWARFLinker::patchLineTableForUnit(CompileUnit &Unit,
   // in NewRows.
   std::vector<DWARFDebugLine::Row> Seq;
   const auto &FunctionRanges = Unit.getFunctionRanges();
-  std::optional<std::pair<AddressRange, int64_t>> CurrRange;
+  std::optional<AddressRangeValuePair> CurrRange;
 
   // FIXME: This logic is meant to generate exactly the same output as
   // Darwin's classic dsymutil. There is a nicer way to implement this
@@ -1821,13 +1821,13 @@ void DWARFLinker::patchLineTableForUnit(CompileUnit &Unit,
     // it is marked as end_sequence in the input (because in that
     // case, the relocation offset is accurate and that entry won't
     // serve as the start of another function).
-    if (!CurrRange || !CurrRange->first.contains(Row.Address.Address) ||
-        (Row.Address.Address == CurrRange->first.end() && !Row.EndSequence)) {
+    if (!CurrRange || !CurrRange->Range.contains(Row.Address.Address) ||
+        (Row.Address.Address == CurrRange->Range.end() && !Row.EndSequence)) {
       // We just stepped out of a known range. Insert a end_sequence
       // corresponding to the end of the range.
       uint64_t StopAddress =
-          CurrRange ? CurrRange->first.end() + CurrRange->second : -1ULL;
-      CurrRange = FunctionRanges.getRangeValueThatContains(Row.Address.Address);
+          CurrRange ? CurrRange->Range.end() + CurrRange->Value : -1ULL;
+      CurrRange = FunctionRanges.getRangeThatContains(Row.Address.Address);
       if (!CurrRange) {
         if (StopAddress != -1ULL) {
           // Try harder by looking in the Address ranges map.
@@ -1836,9 +1836,9 @@ void DWARFLinker::patchLineTableForUnit(CompileUnit &Unit,
           // for now do as dsymutil.
           // FIXME: Understand exactly what cases this addresses and
           // potentially remove it along with the Ranges map.
-          if (std::optional<std::pair<AddressRange, int64_t>> Range =
-                  Ranges.getRangeValueThatContains(Row.Address.Address))
-            StopAddress = Row.Address.Address + (*Range).second;
+          if (std::optional<AddressRangeValuePair> Range =
+                  Ranges.getRangeThatContains(Row.Address.Address))
+            StopAddress = Row.Address.Address + (*Range).Value;
         }
       }
       if (StopAddress != -1ULL && !Seq.empty()) {
@@ -1863,7 +1863,7 @@ void DWARFLinker::patchLineTableForUnit(CompileUnit &Unit,
       continue;
 
     // Relocate row address and add it to the current sequence.
-    Row.Address.Address += CurrRange->second;
+    Row.Address.Address += CurrRange->Value;
     Seq.emplace_back(Row);
 
     if (Row.EndSequence)
@@ -2002,8 +2002,8 @@ void DWARFLinker::patchFrameInfoForObject(const DWARFFile &File,
     // the function entry point, thus we can't just lookup the address
     // in the debug map. Use the AddressInfo's range map to see if the FDE
     // describes something that we can relocate.
-    std::optional<std::pair<AddressRange, int64_t>> Range =
-        Ranges.getRangeValueThatContains(Loc);
+    std::optional<AddressRangeValuePair> Range =
+        Ranges.getRangeThatContains(Loc);
     if (!Range) {
       // The +4 is to account for the size of the InitialLength field itself.
       InputOffset = EntryOffset + InitialLength + 4;
@@ -2032,7 +2032,7 @@ void DWARFLinker::patchFrameInfoForObject(const DWARFFile &File,
     // fields that will get reconstructed by emitFDE().
     unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
     TheDwarfEmitter->emitFDE(IteratorInserted.first->getValue(), AddrSize,
-                             Loc + Range->second,
+                             Loc + Range->Value,
                              FrameData.substr(InputOffset, FDERemainingBytes));
     InputOffset += FDERemainingBytes;
   }
diff --git a/llvm/lib/DWARFLinker/DWARFStreamer.cpp b/llvm/lib/DWARFLinker/DWARFStreamer.cpp
index 5cad267fd845..ae79e8cb9066 100644
--- a/llvm/lib/DWARFLinker/DWARFStreamer.cpp
+++ b/llvm/lib/DWARFLinker/DWARFStreamer.cpp
@@ -402,10 +402,9 @@ void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
   // Linked addresses might end up in a different order.
   // Build linked address ranges.
   AddressRanges LinkedRanges;
-  for (size_t Idx = 0; Idx < FunctionRanges.size(); Idx++)
+  for (const AddressRangeValuePair &Range : FunctionRanges)
     LinkedRanges.insert(
-        {FunctionRanges[Idx].first.start() + FunctionRanges[Idx].second,
-         FunctionRanges[Idx].first.end() + FunctionRanges[Idx].second});
+        {Range.Range.start() + Range.Value, Range.Range.end() + Range.Value});
 
   if (!FunctionRanges.empty())
     emitDwarfDebugArangesTable(Unit, LinkedRanges);
diff --git a/llvm/lib/Support/AddressRanges.cpp b/llvm/lib/Support/AddressRanges.cpp
deleted file mode 100644
index 187d5be00dae..000000000000
--- a/llvm/lib/Support/AddressRanges.cpp
+++ /dev/null
@@ -1,70 +0,0 @@
-//===- AddressRanges.cpp ----------------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/ADT/AddressRanges.h"
-#include "llvm/ADT/STLExtras.h"
-#include <inttypes.h>
-
-using namespace llvm;
-
-AddressRanges::Collection::const_iterator
-AddressRanges::insert(AddressRange Range) {
-  if (Range.size() == 0)
-    return Ranges.end();
-
-  auto It = llvm::upper_bound(Ranges, Range);
-  auto It2 = It;
-  while (It2 != Ranges.end() && It2->start() <= Range.end())
-    ++It2;
-  if (It != It2) {
-    Range = {Range.start(), std::max(Range.end(), std::prev(It2)->end())};
-    It = Ranges.erase(It, It2);
-  }
-  if (It != Ranges.begin() && Range.start() <= std::prev(It)->end()) {
-    --It;
-    *It = {It->start(), std::max(It->end(), Range.end())};
-    return It;
-  }
-
-  return Ranges.insert(It, Range);
-}
-
-AddressRanges::Collection::const_iterator
-AddressRanges::find(uint64_t Addr) const {
-  auto It = std::partition_point(
-      Ranges.begin(), Ranges.end(),
-      [=](const AddressRange &R) { return R.start() <= Addr; });
-
-  if (It == Ranges.begin())
-    return Ranges.end();
-
-  --It;
-  if (Addr >= It->end())
-    return Ranges.end();
-
-  return It;
-}
-
-AddressRanges::Collection::const_iterator
-AddressRanges::find(AddressRange Range) const {
-  if (Range.size() == 0)
-    return Ranges.end();
-
-  auto It = std::partition_point(
-      Ranges.begin(), Ranges.end(),
-      [=](const AddressRange &R) { return R.start() <= Range.start(); });
-
-  if (It == Ranges.begin())
-    return Ranges.end();
-
-  --It;
-  if (Range.end() > It->end())
-    return Ranges.end();
-
-  return It;
-}
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 4cbc3b79f3bb..8fbb2ca4c164 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -117,7 +117,6 @@ endif()
 add_subdirectory(BLAKE3)
 
 add_llvm_component_library(LLVMSupport
-  AddressRanges.cpp
   ABIBreak.cpp
   AMDGPUMetadata.cpp
   APFixedPoint.cpp
diff --git a/llvm/unittests/Support/AddressRangeTest.cpp b/llvm/unittests/Support/AddressRangeTest.cpp
index 468f1e22ffa8..06b326678402 100644
--- a/llvm/unittests/Support/AddressRangeTest.cpp
+++ b/llvm/unittests/Support/AddressRangeTest.cpp
@@ -149,8 +149,31 @@ TEST(AddressRangeTest, TestRanges) {
   EXPECT_EQ(Ranges[0], AddressRange(0x1000, 0x5000));
 }
 
+TEST(AddressRangeTest, TestRangesRandom) {
+  AddressRanges Ranges;
+  size_t NumElements = 100;
+
+  std::srand(std::time(nullptr));
+
+  // Fill ranges.
+  for (size_t Idx = 0; Idx < NumElements; Idx++) {
+    uint64_t Start = static_cast<uint64_t>(std::rand() % 1000);
+    uint64_t End = Start + static_cast<uint64_t>(std::rand() % 1000);
+    Ranges.insert({Start, End});
+  }
+
+  // Check ranges.
+  for (size_t Idx = 0; Idx + 1 < Ranges.size(); Idx++) {
+    // Check that ranges are not intersected.
+    EXPECT_FALSE(Ranges[Idx].intersects(Ranges[Idx + 1]));
+
+    // Check that ranges are sorted and not adjusted.
+    EXPECT_TRUE(Ranges[Idx].end() < Ranges[Idx + 1].start());
+  }
+}
+
 TEST(AddressRangeTest, TestRangesMap) {
-  AddressRangesMap<int> Ranges;
+  AddressRangesMap Ranges;
 
   EXPECT_EQ(Ranges.size(), 0u);
   EXPECT_TRUE(Ranges.empty());
@@ -162,73 +185,247 @@ TEST(AddressRangeTest, TestRangesMap) {
   EXPECT_TRUE(Ranges.contains(0x1500));
   EXPECT_TRUE(Ranges.contains(AddressRange(0x1000, 0x2000)));
 
+  ///////////////////////////////////////
+  /// Check ranges with the same mapped value.
+
+  // Clear ranges.
+  Ranges.clear();
+  EXPECT_EQ(Ranges.size(), 0u);
+  EXPECT_TRUE(Ranges.empty());
+
+  // Add range and check mapped value.
+  Ranges.insert(AddressRange(0x1000, 0x2000), 0x11);
+  EXPECT_EQ(Ranges.size(), 1u);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0x11);
+
+  // Add adjacent range and check mapped value.
+  Ranges.insert(AddressRange(0x2000, 0x3000), 0x11);
+  EXPECT_EQ(Ranges.size(), 2u);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0x11);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x2000)->Value, 0x11);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x2900)->Value, 0x11);
+  EXPECT_FALSE(Ranges.getRangeThatContains(0x3000));
+
+  // Add intersecting range and check mapped value.
+  Ranges.insert(AddressRange(0x1000, 0x3000), 0x11);
+  EXPECT_EQ(Ranges.size(), 2u);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0x11);
+
+  // Add second range and check mapped values.
+  Ranges.insert(AddressRange(0x4000, 0x5000), 0x11);
+  EXPECT_EQ(Ranges.size(), 3u);
+  EXPECT_EQ(Ranges[0].Range, AddressRange(0x1000, 0x2000));
+  EXPECT_EQ(Ranges[0].Value, 0x11);
+  EXPECT_EQ(Ranges[1].Range, AddressRange(0x2000, 0x3000));
+  EXPECT_EQ(Ranges[1].Value, 0x11);
+  EXPECT_EQ(Ranges[2].Range, AddressRange(0x4000, 0x5000));
+  EXPECT_EQ(Ranges[2].Value, 0x11);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0x11);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x4000)->Value, 0x11);
+
+  // Add intersecting range and check mapped value.
+  Ranges.insert(AddressRange(0x0, 0x6000), 0x11);
+  EXPECT_EQ(Ranges.size(), 6u);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0x11);
+
+  // Check that mapped values are correctly preserved for combined ranges.
+  Ranges.clear();
+  Ranges.insert(AddressRange(0x0, 0xff), 0x11);
+  Ranges.insert(AddressRange(0x100, 0x1ff), 0x11);
+  Ranges.insert(AddressRange(0x200, 0x2ff), 0x11);
+  Ranges.insert(AddressRange(0x500, 0x5ff), 0x11);
+  Ranges.insert(AddressRange(0x300, 0x3ff), 0x11);
+  Ranges.insert(AddressRange(0x400, 0x4ff), 0x11);
+  Ranges.insert(AddressRange(0x600, 0x6ff), 0x11);
+  EXPECT_EQ(Ranges.size(), 7u);
+
+  Ranges.insert(AddressRange(0x150, 0x350), 0x11);
+  EXPECT_EQ(Ranges.size(), 9u);
+  EXPECT_EQ(Ranges[0].Range, AddressRange(0x0, 0xff));
+  EXPECT_EQ(Ranges[0].Value, 0x11);
+  EXPECT_EQ(Ranges[1].Range, AddressRange(0x100, 0x1ff));
+  EXPECT_EQ(Ranges[1].Value, 0x11);
+  EXPECT_EQ(Ranges[2].Range, AddressRange(0x1ff, 0x200));
+  EXPECT_EQ(Ranges[2].Value, 0x11);
+  EXPECT_EQ(Ranges[3].Range, AddressRange(0x200, 0x2ff));
+  EXPECT_EQ(Ranges[3].Value, 0x11);
+  EXPECT_EQ(Ranges[4].Range, AddressRange(0x2ff, 0x300));
+  EXPECT_EQ(Ranges[4].Value, 0x11);
+  EXPECT_EQ(Ranges[5].Range, AddressRange(0x300, 0x3ff));
+  EXPECT_EQ(Ranges[5].Value, 0x11);
+  EXPECT_EQ(Ranges[6].Range, AddressRange(0x400, 0x4ff));
+  EXPECT_EQ(Ranges[6].Value, 0x11);
+  EXPECT_EQ(Ranges[7].Range, AddressRange(0x500, 0x5ff));
+  EXPECT_EQ(Ranges[7].Value, 0x11);
+  EXPECT_EQ(Ranges[8].Range, AddressRange(0x600, 0x6ff));
+  EXPECT_EQ(Ranges[8].Value, 0x11);
+
+  Ranges.insert(AddressRange(0x3ff, 0x400), 0x11);
+  EXPECT_EQ(Ranges.size(), 10u);
+  EXPECT_EQ(Ranges[0].Range, AddressRange(0x0, 0xff));
+  EXPECT_EQ(Ranges[0].Value, 0x11);
+  EXPECT_EQ(Ranges[1].Range, AddressRange(0x100, 0x1ff));
+  EXPECT_EQ(Ranges[1].Value, 0x11);
+  EXPECT_EQ(Ranges[2].Range, AddressRange(0x1ff, 0x200));
+  EXPECT_EQ(Ranges[2].Value, 0x11);
+  EXPECT_EQ(Ranges[3].Range, AddressRange(0x200, 0x2ff));
+  EXPECT_EQ(Ranges[3].Value, 0x11);
+  EXPECT_EQ(Ranges[4].Range, AddressRange(0x2ff, 0x300));
+  EXPECT_EQ(Ranges[4].Value, 0x11);
+  EXPECT_EQ(Ranges[5].Range, AddressRange(0x300, 0x3ff));
+  EXPECT_EQ(Ranges[5].Value, 0x11);
+  EXPECT_EQ(Ranges[6].Range, AddressRange(0x3ff, 0x400));
+  EXPECT_EQ(Ranges[6].Value, 0x11);
+  EXPECT_EQ(Ranges[7].Range, AddressRange(0x400, 0x4ff));
+  EXPECT_EQ(Ranges[7].Value, 0x11);
+  EXPECT_EQ(Ranges[8].Range, AddressRange(0x500, 0x5ff));
+  EXPECT_EQ(Ranges[8].Value, 0x11);
+  EXPECT_EQ(Ranges[9].Range, AddressRange(0x600, 0x6ff));
+  EXPECT_EQ(Ranges[9].Value, 0x11);
+
+  /////////////////////////////////////////////
+  /// Check ranges with various mapped values.
+
   // Clear ranges.
   Ranges.clear();
   EXPECT_EQ(Ranges.size(), 0u);
   EXPECT_TRUE(Ranges.empty());
 
-  // Add range and check value.
+  // Add range and check mapped value.
   Ranges.insert(AddressRange(0x1000, 0x2000), 0xfe);
   EXPECT_EQ(Ranges.size(), 1u);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x1000)->second, 0xfe);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0xfe);
 
-  // Add adjacent range and check value.
+  // Add adjacent range and check mapped value.
   Ranges.insert(AddressRange(0x2000, 0x3000), 0xfc);
-  EXPECT_EQ(Ranges.size(), 1u);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x1000)->second, 0xfc);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x2000)->second, 0xfc);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x2900)->second, 0xfc);
-  EXPECT_FALSE(Ranges.getRangeValueThatContains(0x3000));
+  EXPECT_EQ(Ranges.size(), 2u);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0xfe);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x2000)->Value, 0xfc);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x2900)->Value, 0xfc);
+  EXPECT_FALSE(Ranges.getRangeThatContains(0x3000));
 
-  // Add intersecting range and check value.
-  Ranges.insert(AddressRange(0x2000, 0x3000), 0xff);
-  EXPECT_EQ(Ranges.size(), 1u);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x1000)->second, 0xff);
+  // Add intersecting range and check mapped value.
+  Ranges.insert(AddressRange(0x1000, 0x3000), 0xff);
+  EXPECT_EQ(Ranges.size(), 2u);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0xfe);
 
-  // Add second range and check values.
+  // Add one more range and check mapped values.
   Ranges.insert(AddressRange(0x4000, 0x5000), 0x0);
-  EXPECT_EQ(Ranges.size(), 2u);
-  EXPECT_EQ(Ranges[0].second, 0xff);
-  EXPECT_EQ(Ranges[1].second, 0x0);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x1000)->second, 0xff);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x4000)->second, 0x0);
+  EXPECT_EQ(Ranges.size(), 3u);
+  EXPECT_EQ(Ranges[0].Value, 0xfe);
+  EXPECT_EQ(Ranges[1].Value, 0xfc);
+  EXPECT_EQ(Ranges[2].Value, 0x0);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0xfe);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x4000)->Value, 0x0);
 
-  // Add intersecting range and check value.
+  // Add intersecting range and check mapped value.
   Ranges.insert(AddressRange(0x0, 0x6000), 0x1);
-  EXPECT_EQ(Ranges.size(), 1u);
-  EXPECT_EQ(Ranges.getRangeValueThatContains(0x1000)->second, 0x1);
+  EXPECT_EQ(Ranges.size(), 6u);
+  EXPECT_EQ(Ranges[0].Value, 0x1);
+  EXPECT_EQ(Ranges[1].Value, 0xfe);
+  EXPECT_EQ(Ranges[2].Value, 0xfc);
+  EXPECT_EQ(Ranges[3].Value, 0x1);
+  EXPECT_EQ(Ranges[4].Value, 0x0);
+  EXPECT_EQ(Ranges[5].Value, 0x1);
+  EXPECT_EQ(Ranges.getRangeThatContains(0x1000)->Value, 0xfe);
 
-  // Check that values are correctly preserved for combined ranges.
+  // Check that mapped values are correctly preserved for combined ranges.
   Ranges.clear();
   Ranges.insert(AddressRange(0x0, 0xff), 0x1);
   Ranges.insert(AddressRange(0x100, 0x1ff), 0x2);
   Ranges.insert(AddressRange(0x200, 0x2ff), 0x3);
   Ranges.insert(AddressRange(0x300, 0x3ff), 0x4);
-  Ranges.insert(AddressRange(0x400, 0x4ff), 0x5);
   Ranges.insert(AddressRange(0x500, 0x5ff), 0x6);
+  Ranges.insert(AddressRange(0x400, 0x4ff), 0x5);
   Ranges.insert(AddressRange(0x600, 0x6ff), 0x7);
+  EXPECT_EQ(Ranges.size(), 7u);
 
   Ranges.insert(AddressRange(0x150, 0x350), 0xff);
-  EXPECT_EQ(Ranges.size(), 5u);
-  EXPECT_EQ(Ranges[0].first, AddressRange(0x0, 0xff));
-  EXPECT_EQ(Ranges[0].second, 0x1);
-  EXPECT_EQ(Ranges[1].first, AddressRange(0x100, 0x3ff));
-  EXPECT_EQ(Ranges[1].second, 0xff);
-  EXPECT_EQ(Ranges[2].first, AddressRange(0x400, 0x4ff));
-  EXPECT_EQ(Ranges[2].second, 0x5);
-  EXPECT_EQ(Ranges[3].first, AddressRange(0x500, 0x5ff));
-  EXPECT_EQ(Ranges[3].second, 0x6);
-  EXPECT_EQ(Ranges[4].first, AddressRange(0x600, 0x6ff));
-  EXPECT_EQ(Ranges[4].second, 0x7);
+  EXPECT_EQ(Ranges.size(), 9u);
+  EXPECT_EQ(Ranges[0].Range, AddressRange(0x0, 0xff));
+  EXPECT_EQ(Ranges[0].Value, 0x1);
+  EXPECT_EQ(Ranges[1].Range, AddressRange(0x100, 0x1ff));
+  EXPECT_EQ(Ranges[1].Value, 0x2);
+  EXPECT_EQ(Ranges[2].Range, AddressRange(0x1ff, 0x200));
+  EXPECT_EQ(Ranges[2].Value, 0xff);
+  EXPECT_EQ(Ranges[3].Range, AddressRange(0x200, 0x2ff));
+  EXPECT_EQ(Ranges[3].Value, 0x3);
+  EXPECT_EQ(Ranges[4].Range, AddressRange(0x2ff, 0x300));
+  EXPECT_EQ(Ranges[4].Value, 0xff);
+  EXPECT_EQ(Ranges[5].Range, AddressRange(0x300, 0x3ff));
+  EXPECT_EQ(Ranges[5].Value, 0x4);
+  EXPECT_EQ(Ranges[6].Range, AddressRange(0x400, 0x4ff));
+  EXPECT_EQ(Ranges[6].Value, 0x5);
+  EXPECT_EQ(Ranges[7].Range, AddressRange(0x500, 0x5ff));
+  EXPECT_EQ(Ranges[7].Value, 0x6);
+  EXPECT_EQ(Ranges[8].Range, AddressRange(0x600, 0x6ff));
+  EXPECT_EQ(Ranges[8].Value, 0x7);
 
+  Ranges.insert(AddressRange(0x650, 0x700), 0x8);
   Ranges.insert(AddressRange(0x3ff, 0x400), 0x5);
-  EXPECT_EQ(Ranges.size(), 4u);
-  EXPECT_EQ(Ranges[0].first, AddressRange(0x0, 0xff));
-  EXPECT_EQ(Ranges[0].second, 0x1);
-  EXPECT_EQ(Ranges[1].first, AddressRange(0x100, 0x4ff));
-  EXPECT_EQ(Ranges[1].second, 0x5);
-  EXPECT_EQ(Ranges[2].first, AddressRange(0x500, 0x5ff));
-  EXPECT_EQ(Ranges[2].second, 0x6);
-  EXPECT_EQ(Ranges[3].first, AddressRange(0x600, 0x6ff));
-  EXPECT_EQ(Ranges[3].second, 0x7);
+  Ranges.insert(AddressRange(0x0, 0x40), 0xee);
+  EXPECT_EQ(Ranges.size(), 11u);
+  EXPECT_EQ(Ranges[0].Range, AddressRange(0x0, 0xff));
+  EXPECT_EQ(Ranges[0].Value, 0x1);
+  EXPECT_EQ(Ranges[1].Range, AddressRange(0x100, 0x1ff));
+  EXPECT_EQ(Ranges[1].Value, 0x2);
+  EXPECT_EQ(Ranges[2].Range, AddressRange(0x1ff, 0x200));
+  EXPECT_EQ(Ranges[2].Value, 0xff);
+  EXPECT_EQ(Ranges[3].Range, AddressRange(0x200, 0x2ff));
+  EXPECT_EQ(Ranges[3].Value, 0x3);
+  EXPECT_EQ(Ranges[4].Range, AddressRange(0x2ff, 0x300));
+  EXPECT_EQ(Ranges[4].Value, 0xff);
+  EXPECT_EQ(Ranges[5].Range, AddressRange(0x300, 0x3ff));
+  EXPECT_EQ(Ranges[5].Value, 0x4);
+  EXPECT_EQ(Ranges[6].Range, AddressRange(0x3ff, 0x400));
+  EXPECT_EQ(Ranges[6].Value, 0x5);
+  EXPECT_EQ(Ranges[7].Range, AddressRange(0x400, 0x4ff));
+  EXPECT_EQ(Ranges[7].Value, 0x5);
+  EXPECT_EQ(Ranges[8].Range, AddressRange(0x500, 0x5ff));
+  EXPECT_EQ(Ranges[8].Value, 0x6);
+  EXPECT_EQ(Ranges[9].Range, AddressRange(0x600, 0x6ff));
+  EXPECT_EQ(Ranges[9].Value, 0x7);
+  EXPECT_EQ(Ranges[10].Range, AddressRange(0x6ff, 0x700));
+  EXPECT_EQ(Ranges[10].Value, 0x8);
+}
+
+TEST(AddressRangeTest, TestRangesMapRandom) {
+  AddressRangesMap Ranges;
+  size_t NumElements = 100;
+
+  std::srand(std::time(nullptr));
+
+  // Fill ranges. Use the same mapped value.
+  for (size_t Idx = 0; Idx < NumElements; Idx++) {
+    uint64_t Start = static_cast<uint64_t>(std::rand() % 1000);
+    uint64_t End = Start + static_cast<uint64_t>(std::rand() % 1000);
+    Ranges.insert({Start, End}, 0xffLL);
+  }
+
+  // Check ranges.
+  for (size_t Idx = 0; Idx + 1 < Ranges.size(); Idx++) {
+    // Check that ranges are not intersected.
+    EXPECT_FALSE(Ranges[Idx].Range.intersects(Ranges[Idx + 1].Range));
+
+    // Check that ranges are sorted and not adjusted.
+    EXPECT_TRUE(Ranges[Idx].Range.end() <= Ranges[Idx + 1].Range.start());
+  }
+
+  Ranges.clear();
+  // Fill ranges. Use the various mapped value.
+  for (size_t Idx = 0; Idx < NumElements; Idx++) {
+    uint64_t Start = static_cast<uint64_t>(std::rand() % 1000);
+    uint64_t End = Start + static_cast<uint64_t>(std::rand() % 1000);
+    int64_t Value = static_cast<int64_t>(std::rand() % 10);
+    Ranges.insert({Start, End}, Value);
+  }
+
+  // Check ranges.
+  for (size_t Idx = 0; Idx + 1 < Ranges.size(); Idx++) {
+    // Check that ranges are not intersected.
+    EXPECT_FALSE(Ranges[Idx].Range.intersects(Ranges[Idx + 1].Range));
+
+    // Check that ranges are sorted and not adjusted.
+    EXPECT_TRUE(Ranges[Idx].Range.end() <= Ranges[Idx + 1].Range.start());
+  }
 }
-- 
2.39.0.1.g6739ec1790