Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/LiveIntervalUnion.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
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
// LiveIntervalUnion represents a coalesced set of live intervals. This may be
10
// used during coalescing to represent a congruence class, or during register
11
// allocation to model liveness of a physical register.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/CodeGen/LiveIntervalUnion.h"
16
#include "llvm/ADT/STLExtras.h"
17
#include "llvm/ADT/SparseBitVector.h"
18
#include "llvm/CodeGen/LiveInterval.h"
19
#include "llvm/CodeGen/TargetRegisterInfo.h"
20
#include "llvm/Support/raw_ostream.h"
21
#include <cassert>
22
#include <cstdlib>
23
24
using namespace llvm;
25
26
#define DEBUG_TYPE "regalloc"
27
28
// Merge a LiveInterval's segments. Guarantee no overlaps.
29
9.95M
void LiveIntervalUnion::unify(LiveInterval &VirtReg, const LiveRange &Range) {
30
9.95M
  if (Range.empty())
31
14.8k
    return;
32
9.94M
  ++Tag;
33
9.94M
34
9.94M
  // Insert each of the virtual register's live segments into the map.
35
9.94M
  LiveRange::const_iterator RegPos = Range.begin();
36
9.94M
  LiveRange::const_iterator RegEnd = Range.end();
37
9.94M
  SegmentIter SegPos = Segments.find(RegPos->start);
38
9.94M
39
11.7M
  while (SegPos.valid()) {
40
5.02M
    SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
41
5.02M
    if (++RegPos == RegEnd)
42
3.24M
      return;
43
1.78M
    SegPos.advanceTo(RegPos->start);
44
1.78M
  }
45
9.94M
46
9.94M
  // We have reached the end of Segments, so it is no longer necessary to search
47
9.94M
  // for the insertion position.
48
9.94M
  // It is faster to insert the end first.
49
9.94M
  --RegEnd;
50
6.70M
  SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
51
11.0M
  for (; RegPos != RegEnd; 
++RegPos, ++SegPos4.39M
)
52
4.39M
    SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
53
6.70M
}
54
55
// Remove a live virtual register's segments from this union.
56
811k
void LiveIntervalUnion::extract(LiveInterval &VirtReg, const LiveRange &Range) {
57
811k
  if (Range.empty())
58
0
    return;
59
811k
  ++Tag;
60
811k
61
811k
  // Remove each of the virtual register's live segments from the map.
62
811k
  LiveRange::const_iterator RegPos = Range.begin();
63
811k
  LiveRange::const_iterator RegEnd = Range.end();
64
811k
  SegmentIter SegPos = Segments.find(RegPos->start);
65
811k
66
1.60M
  while (true) {
67
1.60M
    assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
68
1.60M
    SegPos.erase();
69
1.60M
    if (!SegPos.valid())
70
561k
      return;
71
1.04M
72
1.04M
    // Skip all segments that may have been coalesced.
73
1.04M
    RegPos = Range.advanceTo(RegPos, SegPos.start());
74
1.04M
    if (RegPos == RegEnd)
75
250k
      return;
76
793k
77
793k
    SegPos.advanceTo(RegPos->start);
78
793k
  }
79
811k
}
80
81
void
82
0
LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
83
0
  if (empty()) {
84
0
    OS << " empty\n";
85
0
    return;
86
0
  }
87
0
  for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
88
0
    OS << " [" << SI.start() << ' ' << SI.stop() << "):"
89
0
       << printReg(SI.value()->reg, TRI);
90
0
  }
91
0
  OS << '\n';
92
0
}
93
94
#ifndef NDEBUG
95
// Verify the live intervals in this union and add them to the visited set.
96
void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
97
  for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
98
    VisitedVRegs.set(SI.value()->reg);
99
}
100
#endif //!NDEBUG
101
102
// Scan the vector of interfering virtual registers in this union. Assume it's
103
// quite small.
104
156M
bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
105
156M
  return is_contained(InterferingVRegs, VirtReg);
106
156M
}
107
108
// Collect virtual registers in this union that interfere with this
109
// query's live virtual register.
110
//
111
// The query state is one of:
112
//
113
// 1. CheckedFirstInterference == false: Iterators are uninitialized.
114
// 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
115
// 3. Iterators left at the last seen intersection.
116
//
117
unsigned LiveIntervalUnion::Query::
118
168M
collectInterferingVRegs(unsigned MaxInterferingRegs) {
119
168M
  // Fast path return if we already have the desired information.
120
168M
  if (SeenAllInterferences || 
InterferingVRegs.size() >= MaxInterferingRegs167M
)
121
15.3M
    return InterferingVRegs.size();
122
153M
123
153M
  // Set up iterators on the first call.
124
153M
  if (!CheckedFirstInterference) {
125
139M
    CheckedFirstInterference = true;
126
139M
127
139M
    // Quickly skip interference check for empty sets.
128
139M
    if (
LR->empty()139M
|| LiveUnion->empty()) {
129
3.79M
      SeenAllInterferences = true;
130
3.79M
      return 0;
131
3.79M
    }
132
135M
133
135M
    // In most cases, the union will start before LR.
134
135M
    LRI = LR->begin();
135
135M
    LiveUnionI.setMap(LiveUnion->getMap());
136
135M
    LiveUnionI.find(LRI->start);
137
135M
  }
138
153M
139
153M
  LiveRange::const_iterator LREnd = LR->end();
140
149M
  LiveInterval *RecentReg = nullptr;
141
186M
  while (LiveUnionI.valid()) {
142
183M
    assert(LRI != LREnd && "Reached end of LR");
143
183M
144
183M
    // Check for overlapping interference.
145
203M
    while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) {
146
157M
      // This is an overlap, record the interfering register.
147
157M
      LiveInterval *VReg = LiveUnionI.value();
148
157M
      if (VReg != RecentReg && 
!isSeenInterference(VReg)156M
) {
149
133M
        RecentReg = VReg;
150
133M
        InterferingVRegs.push_back(VReg);
151
133M
        if (InterferingVRegs.size() >= MaxInterferingRegs)
152
129M
          return InterferingVRegs.size();
153
28.7M
      }
154
28.7M
      // This LiveUnion segment is no longer interesting.
155
28.7M
      if (!(++LiveUnionI).valid()) {
156
9.02M
        SeenAllInterferences = true;
157
9.02M
        return InterferingVRegs.size();
158
9.02M
      }
159
28.7M
    }
160
183M
161
183M
    // The iterators are now not overlapping, LiveUnionI has been advanced
162
183M
    // beyond LRI.
163
183M
    assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap");
164
45.6M
165
45.6M
    // Advance the iterator that ends first.
166
45.6M
    LRI = LR->advanceTo(LRI, LiveUnionI.start());
167
45.6M
    if (LRI == LREnd)
168
8.49M
      break;
169
37.1M
170
37.1M
    // Detect overlap, handle above.
171
37.1M
    if (LRI->start < LiveUnionI.stop())
172
35.7M
      continue;
173
1.35M
174
1.35M
    // Still not overlapping. Catch up LiveUnionI.
175
1.35M
    LiveUnionI.advanceTo(LRI->start);
176
1.35M
  }
177
149M
  SeenAllInterferences = true;
178
11.4M
  return InterferingVRegs.size();
179
149M
}
180
181
void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
182
509k
                                    unsigned NSize) {
183
509k
  // Reuse existing allocation.
184
509k
  if (NSize == Size)
185
475k
    return;
186
33.9k
  clear();
187
33.9k
  Size = NSize;
188
33.9k
  LIUs = static_cast<LiveIntervalUnion*>(
189
33.9k
      safe_malloc(sizeof(LiveIntervalUnion)*NSize));
190
7.75M
  for (unsigned i = 0; i != Size; 
++i7.72M
)
191
7.72M
    new(LIUs + i) LiveIntervalUnion(Alloc);
192
33.9k
}
193
194
69.8k
void LiveIntervalUnion::Array::clear() {
195
69.8k
  if (!LIUs)
196
35.8k
    return;
197
7.74M
  
for (unsigned i = 0; 33.9k
i != Size;
++i7.71M
)
198
7.71M
    LIUs[i].~LiveIntervalUnion();
199
33.9k
  free(LIUs);
200
33.9k
  Size =  0;
201
33.9k
  LIUs = nullptr;
202
33.9k
}