Coverage Report

Created: 2018-07-18 22:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/CodeGen/LiveInterval.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/CodeGen/LiveInterval.h - Interval representation ----*- 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
// This file implements the LiveRange and LiveInterval classes.  Given some
11
// numbering of each the machine instructions an interval [i, j) is said to be a
12
// live range for register v if there is no instruction with number j' >= j
13
// such that v is live at j' and there is no instruction with number i' < i such
14
// that v is live at i'. In this implementation ranges can have holes,
15
// i.e. a range might look like [1,20), [50,65), [1000,1001).  Each
16
// individual segment is represented as an instance of LiveRange::Segment,
17
// and the whole range is represented as an instance of LiveRange.
18
//
19
//===----------------------------------------------------------------------===//
20
21
#ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22
#define LLVM_CODEGEN_LIVEINTERVAL_H
23
24
#include "llvm/ADT/ArrayRef.h"
25
#include "llvm/ADT/IntEqClasses.h"
26
#include "llvm/ADT/STLExtras.h"
27
#include "llvm/ADT/SmallVector.h"
28
#include "llvm/ADT/iterator_range.h"
29
#include "llvm/CodeGen/SlotIndexes.h"
30
#include "llvm/MC/LaneBitmask.h"
31
#include "llvm/Support/Allocator.h"
32
#include "llvm/Support/MathExtras.h"
33
#include <algorithm>
34
#include <cassert>
35
#include <cstddef>
36
#include <functional>
37
#include <memory>
38
#include <set>
39
#include <tuple>
40
#include <utility>
41
42
namespace llvm {
43
44
  class CoalescerPair;
45
  class LiveIntervals;
46
  class MachineRegisterInfo;
47
  class raw_ostream;
48
49
  /// VNInfo - Value Number Information.
50
  /// This class holds information about a machine level values, including
51
  /// definition and use points.
52
  ///
53
  class VNInfo {
54
  public:
55
    using Allocator = BumpPtrAllocator;
56
57
    /// The ID number of this value.
58
    unsigned id;
59
60
    /// The index of the defining instruction.
61
    SlotIndex def;
62
63
    /// VNInfo constructor.
64
23.5M
    VNInfo(unsigned i, SlotIndex d) : id(i), def(d) {}
65
66
    /// VNInfo constructor, copies values from orig, except for the value number.
67
549k
    VNInfo(unsigned i, const VNInfo &orig) : id(i), def(orig.def) {}
68
69
    /// Copy from the parameter into this VNInfo.
70
13
    void copyFrom(VNInfo &src) {
71
13
      def = src.def;
72
13
    }
73
74
    /// Returns true if this value is defined by a PHI instruction (or was,
75
    /// PHI instructions may have been eliminated).
76
    /// PHI-defs begin at a block boundary, all other defs begin at register or
77
    /// EC slots.
78
53.5M
    bool isPHIDef() const { return def.isBlock(); }
79
80
    /// Returns true if this value is unused.
81
59.5M
    bool isUnused() const { return !def.isValid(); }
82
83
    /// Mark this value as unused.
84
68.9k
    void markUnused() { def = SlotIndex(); }
85
  };
86
87
  /// Result of a LiveRange query. This class hides the implementation details
88
  /// of live ranges, and it should be used as the primary interface for
89
  /// examining live ranges around instructions.
90
  class LiveQueryResult {
91
    VNInfo *const EarlyVal;
92
    VNInfo *const LateVal;
93
    const SlotIndex EndPoint;
94
    const bool Kill;
95
96
  public:
97
    LiveQueryResult(VNInfo *EarlyVal, VNInfo *LateVal, SlotIndex EndPoint,
98
                    bool Kill)
99
      : EarlyVal(EarlyVal), LateVal(LateVal), EndPoint(EndPoint), Kill(Kill)
100
43.7M
    {}
101
102
    /// Return the value that is live-in to the instruction. This is the value
103
    /// that will be read by the instruction's use operands. Return NULL if no
104
    /// value is live-in.
105
41.5M
    VNInfo *valueIn() const {
106
41.5M
      return EarlyVal;
107
41.5M
    }
108
109
    /// Return true if the live-in value is killed by this instruction. This
110
    /// means that either the live range ends at the instruction, or it changes
111
    /// value.
112
1.41M
    bool isKill() const {
113
1.41M
      return Kill;
114
1.41M
    }
115
116
    /// Return true if this instruction has a dead def.
117
2.37M
    bool isDeadDef() const {
118
2.37M
      return EndPoint.isDead();
119
2.37M
    }
120
121
    /// Return the value leaving the instruction, if any. This can be a
122
    /// live-through value, or a live def. A dead def returns NULL.
123
519k
    VNInfo *valueOut() const {
124
519k
      return isDeadDef() ? 
nullptr80
:
LateVal519k
;
125
519k
    }
126
127
    /// Returns the value alive at the end of the instruction, if any. This can
128
    /// be a live-through value, a live def or a dead def.
129
1.30M
    VNInfo *valueOutOrDead() const {
130
1.30M
      return LateVal;
131
1.30M
    }
132
133
    /// Return the value defined by this instruction, if any. This includes
134
    /// dead defs, it is the value created by the instruction's def operands.
135
22.5M
    VNInfo *valueDefined() const {
136
22.5M
      return EarlyVal == LateVal ? 
nullptr17.9M
:
LateVal4.56M
;
137
22.5M
    }
138
139
    /// Return the end point of the last live range segment to interact with
140
    /// the instruction, if any.
141
    ///
142
    /// The end point is an invalid SlotIndex only if the live range doesn't
143
    /// intersect the instruction at all.
144
    ///
145
    /// The end point may be at or past the end of the instruction's basic
146
    /// block. That means the value was live out of the block.
147
651k
    SlotIndex endPoint() const {
148
651k
      return EndPoint;
149
651k
    }
150
  };
151
152
  /// This class represents the liveness of a register, stack slot, etc.
153
  /// It manages an ordered list of Segment objects.
154
  /// The Segments are organized in a static single assignment form: At places
155
  /// where a new value is defined or different values reach a CFG join a new
156
  /// segment with a new value number is used.
157
  class LiveRange {
158
  public:
159
    /// This represents a simple continuous liveness interval for a value.
160
    /// The start point is inclusive, the end point exclusive. These intervals
161
    /// are rendered as [start,end).
162
    struct Segment {
163
      SlotIndex start;  // Start point of the interval (inclusive)
164
      SlotIndex end;    // End point of the interval (exclusive)
165
      VNInfo *valno = nullptr; // identifier for the value contained in this
166
                               // segment.
167
168
910k
      Segment() = default;
169
170
      Segment(SlotIndex S, SlotIndex E, VNInfo *V)
171
127M
        : start(S), end(E), valno(V) {
172
127M
        assert(S < E && "Cannot create empty or backwards segment");
173
127M
      }
174
175
      /// Return true if the index is covered by this segment.
176
156M
      bool contains(SlotIndex I) const {
177
156M
        return start <= I && 
I < end74.0k
;
178
156M
      }
179
180
      /// Return true if the given interval, [S, E), is covered by this segment.
181
0
      bool containsInterval(SlotIndex S, SlotIndex E) const {
182
0
        assert((S < E) && "Backwards interval?");
183
0
        return (start <= S && S < end) && (start < E && E <= end);
184
0
      }
185
186
64.3M
      bool operator<(const Segment &Other) const {
187
64.3M
        return std::tie(start, end) < std::tie(Other.start, Other.end);
188
64.3M
      }
189
0
      bool operator==(const Segment &Other) const {
190
0
        return start == Other.start && end == Other.end;
191
0
      }
192
193
      void dump() const;
194
    };
195
196
    using Segments = SmallVector<Segment, 2>;
197
    using VNInfoList = SmallVector<VNInfo *, 2>;
198
199
    Segments segments;   // the liveness segments
200
    VNInfoList valnos;   // value#'s
201
202
    // The segment set is used temporarily to accelerate initial computation
203
    // of live ranges of physical registers in computeRegUnitRange.
204
    // After that the set is flushed to the segment vector and deleted.
205
    using SegmentSet = std::set<Segment>;
206
    std::unique_ptr<SegmentSet> segmentSet;
207
208
    using iterator = Segments::iterator;
209
    using const_iterator = Segments::const_iterator;
210
211
275M
    iterator begin() { return segments.begin(); }
212
292M
    iterator end()   { return segments.end(); }
213
214
161M
    const_iterator begin() const { return segments.begin(); }
215
375M
    const_iterator end() const  { return segments.end(); }
216
217
    using vni_iterator = VNInfoList::iterator;
218
    using const_vni_iterator = VNInfoList::const_iterator;
219
220
765k
    vni_iterator vni_begin() { return valnos.begin(); }
221
765k
    vni_iterator vni_end()   { return valnos.end(); }
222
223
5.18M
    const_vni_iterator vni_begin() const { return valnos.begin(); }
224
5.18M
    const_vni_iterator vni_end() const   { return valnos.end(); }
225
226
    /// Constructs a new LiveRange object.
227
    LiveRange(bool UseSegmentSet = false)
228
        : segmentSet(UseSegmentSet ? llvm::make_unique<SegmentSet>()
229
17.7M
                                   : nullptr) {}
230
231
    /// Constructs a new LiveRange object by copying segments and valnos from
232
    /// another LiveRange.
233
354k
    LiveRange(const LiveRange &Other, BumpPtrAllocator &Allocator) {
234
354k
      assert(Other.segmentSet == nullptr &&
235
354k
             "Copying of LiveRanges with active SegmentSets is not supported");
236
354k
      assign(Other, Allocator);
237
354k
    }
238
239
    /// Copies values numbers and live segments from \p Other into this range.
240
483k
    void assign(const LiveRange &Other, BumpPtrAllocator &Allocator) {
241
483k
      if (this == &Other)
242
0
        return;
243
483k
244
483k
      assert(Other.segmentSet == nullptr &&
245
483k
             "Copying of LiveRanges with active SegmentSets is not supported");
246
483k
      // Duplicate valnos.
247
483k
      for (const VNInfo *VNI : Other.valnos)
248
549k
        createValueCopy(VNI, Allocator);
249
483k
      // Now we can copy segments and remap their valnos.
250
483k
      for (const Segment &S : Other.segments)
251
741k
        segments.push_back(Segment(S.start, S.end, valnos[S.valno->id]));
252
483k
    }
253
254
    /// advanceTo - Advance the specified iterator to point to the Segment
255
    /// containing the specified position, or end() if the position is past the
256
    /// end of the range.  If no Segment contains this position, but the
257
    /// position is in a hole, this method returns an iterator pointing to the
258
    /// Segment immediately after the hole.
259
14.6M
    iterator advanceTo(iterator I, SlotIndex Pos) {
260
14.6M
      assert(I != end());
261
14.6M
      if (Pos >= endIndex())
262
4.63M
        return end();
263
29.5M
      
while (9.98M
I->end <= Pos)
++I19.5M
;
264
9.98M
      return I;
265
9.98M
    }
266
267
201M
    const_iterator advanceTo(const_iterator I, SlotIndex Pos) const {
268
201M
      assert(I != end());
269
201M
      if (Pos >= endIndex())
270
9.48M
        return end();
271
228M
      
while (192M
I->end <= Pos)
++I36.3M
;
272
192M
      return I;
273
192M
    }
274
275
    /// find - Return an iterator pointing to the first segment that ends after
276
    /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
277
    /// when searching large ranges.
278
    ///
279
    /// If Pos is contained in a Segment, that segment is returned.
280
    /// If Pos is in a hole, the following Segment is returned.
281
    /// If Pos is beyond endIndex, end() is returned.
282
    iterator find(SlotIndex Pos);
283
284
145M
    const_iterator find(SlotIndex Pos) const {
285
145M
      return const_cast<LiveRange*>(this)->find(Pos);
286
145M
    }
287
288
540k
    void clear() {
289
540k
      valnos.clear();
290
540k
      segments.clear();
291
540k
    }
292
293
159M
    size_t size() const {
294
159M
      return segments.size();
295
159M
    }
296
297
54
    bool hasAtLeastOneValue() const { return !valnos.empty(); }
298
299
448k
    bool containsOneValue() const { return valnos.size() == 1; }
300
301
60.9M
    unsigned getNumValNums() const { return (unsigned)valnos.size(); }
302
303
    /// getValNumInfo - Returns pointer to the specified val#.
304
    ///
305
83.1M
    inline VNInfo *getValNumInfo(unsigned ValNo) {
306
83.1M
      return valnos[ValNo];
307
83.1M
    }
308
3.63M
    inline const VNInfo *getValNumInfo(unsigned ValNo) const {
309
3.63M
      return valnos[ValNo];
310
3.63M
    }
311
312
    /// containsValue - Returns true if VNI belongs to this range.
313
    bool containsValue(const VNInfo *VNI) const {
314
      return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id);
315
    }
316
317
    /// getNextValue - Create a new value number and return it.  MIIdx specifies
318
    /// the instruction that defines the value number.
319
23.4M
    VNInfo *getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator) {
320
23.4M
      VNInfo *VNI =
321
23.4M
        new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def);
322
23.4M
      valnos.push_back(VNI);
323
23.4M
      return VNI;
324
23.4M
    }
325
326
    /// createDeadDef - Make sure the range has a value defined at Def.
327
    /// If one already exists, return it. Otherwise allocate a new value and
328
    /// add liveness for a dead def.
329
    VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc);
330
331
    /// Create a def of value @p VNI. Return @p VNI. If there already exists
332
    /// a definition at VNI->def, the value defined there must be @p VNI.
333
    VNInfo *createDeadDef(VNInfo *VNI);
334
335
    /// Create a copy of the given value. The new value will be identical except
336
    /// for the Value number.
337
    VNInfo *createValueCopy(const VNInfo *orig,
338
549k
                            VNInfo::Allocator &VNInfoAllocator) {
339
549k
      VNInfo *VNI =
340
549k
        new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
341
549k
      valnos.push_back(VNI);
342
549k
      return VNI;
343
549k
    }
344
345
    /// RenumberValues - Renumber all values in order of appearance and remove
346
    /// unused values.
347
    void RenumberValues();
348
349
    /// MergeValueNumberInto - This method is called when two value numbers
350
    /// are found to be equivalent.  This eliminates V1, replacing all
351
    /// segments with the V1 value number with the V2 value number.  This can
352
    /// cause merging of V1/V2 values numbers and compaction of the value space.
353
    VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
354
355
    /// Merge all of the live segments of a specific val# in RHS into this live
356
    /// range as the specified value number. The segments in RHS are allowed
357
    /// to overlap with segments in the current range, it will replace the
358
    /// value numbers of the overlaped live segments with the specified value
359
    /// number.
360
    void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo);
361
362
    /// MergeValueInAsValue - Merge all of the segments of a specific val#
363
    /// in RHS into this live range as the specified value number.
364
    /// The segments in RHS are allowed to overlap with segments in the
365
    /// current range, but only if the overlapping segments have the
366
    /// specified value number.
367
    void MergeValueInAsValue(const LiveRange &RHS,
368
                             const VNInfo *RHSValNo, VNInfo *LHSValNo);
369
370
591M
    bool empty() const { return segments.empty(); }
371
372
    /// beginIndex - Return the lowest numbered slot covered.
373
58.1M
    SlotIndex beginIndex() const {
374
58.1M
      assert(!empty() && "Call to beginIndex() on empty range.");
375
58.1M
      return segments.front().start;
376
58.1M
    }
377
378
    /// endNumber - return the maximum point of the range of the whole,
379
    /// exclusive.
380
434M
    SlotIndex endIndex() const {
381
434M
      assert(!empty() && "Call to endIndex() on empty range.");
382
434M
      return segments.back().end;
383
434M
    }
384
385
132
    bool expiredAt(SlotIndex index) const {
386
132
      return index >= endIndex();
387
132
    }
388
389
36.4M
    bool liveAt(SlotIndex index) const {
390
36.4M
      const_iterator r = find(index);
391
36.4M
      return r != end() && 
r->start <= index32.4M
;
392
36.4M
    }
393
394
    /// Return the segment that contains the specified index, or null if there
395
    /// is none.
396
18.1M
    const Segment *getSegmentContaining(SlotIndex Idx) const {
397
18.1M
      const_iterator I = FindSegmentContaining(Idx);
398
18.1M
      return I == end() ? 
nullptr3.69M
:
&*I14.4M
;
399
18.1M
    }
400
401
    /// Return the live segment that contains the specified index, or null if
402
    /// there is none.
403
11.4k
    Segment *getSegmentContaining(SlotIndex Idx) {
404
11.4k
      iterator I = FindSegmentContaining(Idx);
405
11.4k
      return I == end() ? 
nullptr526
:
&*I10.9k
;
406
11.4k
    }
407
408
    /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
409
12.3M
    VNInfo *getVNInfoAt(SlotIndex Idx) const {
410
12.3M
      const_iterator I = FindSegmentContaining(Idx);
411
12.3M
      return I == end() ? 
nullptr234k
:
I->valno12.0M
;
412
12.3M
    }
413
414
    /// getVNInfoBefore - Return the VNInfo that is live up to but not
415
    /// necessarilly including Idx, or NULL. Use this to find the reaching def
416
    /// used by an instruction at this SlotIndex position.
417
10.0M
    VNInfo *getVNInfoBefore(SlotIndex Idx) const {
418
10.0M
      const_iterator I = FindSegmentContaining(Idx.getPrevSlot());
419
10.0M
      return I == end() ? 
nullptr2.07M
:
I->valno7.99M
;
420
10.0M
    }
421
422
    /// Return an iterator to the segment that contains the specified index, or
423
    /// end() if there is none.
424
15.6M
    iterator FindSegmentContaining(SlotIndex Idx) {
425
15.6M
      iterator I = find(Idx);
426
15.6M
      return I != end() && 
I->start <= Idx15.6M
?
I15.6M
:
end()3.25k
;
427
15.6M
    }
428
429
40.5M
    const_iterator FindSegmentContaining(SlotIndex Idx) const {
430
40.5M
      const_iterator I = find(Idx);
431
40.5M
      return I != end() && 
I->start <= Idx37.0M
?
I34.5M
:
end()6.01M
;
432
40.5M
    }
433
434
    /// overlaps - Return true if the intersection of the two live ranges is
435
    /// not empty.
436
3.75M
    bool overlaps(const LiveRange &other) const {
437
3.75M
      if (other.empty())
438
4.39k
        return false;
439
3.75M
      return overlapsFrom(other, other.begin());
440
3.75M
    }
441
442
    /// overlaps - Return true if the two ranges have overlapping segments
443
    /// that are not coalescable according to CP.
444
    ///
445
    /// Overlapping segments where one range is defined by a coalescable
446
    /// copy are allowed.
447
    bool overlaps(const LiveRange &Other, const CoalescerPair &CP,
448
                  const SlotIndexes&) const;
449
450
    /// overlaps - Return true if the live range overlaps an interval specified
451
    /// by [Start, End).
452
    bool overlaps(SlotIndex Start, SlotIndex End) const;
453
454
    /// overlapsFrom - Return true if the intersection of the two live ranges
455
    /// is not empty.  The specified iterator is a hint that we can begin
456
    /// scanning the Other range starting at I.
457
    bool overlapsFrom(const LiveRange &Other, const_iterator StartPos) const;
458
459
    /// Returns true if all segments of the @p Other live range are completely
460
    /// covered by this live range.
461
    /// Adjacent live ranges do not affect the covering:the liverange
462
    /// [1,5](5,10] covers (3,7].
463
    bool covers(const LiveRange &Other) const;
464
465
    /// Add the specified Segment to this range, merging segments as
466
    /// appropriate.  This returns an iterator to the inserted segment (which
467
    /// may have grown since it was inserted).
468
    iterator addSegment(Segment S);
469
470
    /// Attempt to extend a value defined after @p StartIdx to include @p Use.
471
    /// Both @p StartIdx and @p Use should be in the same basic block. In case
472
    /// of subranges, an extension could be prevented by an explicit "undef"
473
    /// caused by a <def,read-undef> on a non-overlapping lane. The list of
474
    /// location of such "undefs" should be provided in @p Undefs.
475
    /// The return value is a pair: the first element is VNInfo of the value
476
    /// that was extended (possibly nullptr), the second is a boolean value
477
    /// indicating whether an "undef" was encountered.
478
    /// If this range is live before @p Use in the basic block that starts at
479
    /// @p StartIdx, and there is no intervening "undef", extend it to be live
480
    /// up to @p Use, and return the pair {value, false}. If there is no
481
    /// segment before @p Use and there is no "undef" between @p StartIdx and
482
    /// @p Use, return {nullptr, false}. If there is an "undef" before @p Use,
483
    /// return {nullptr, true}.
484
    std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs,
485
        SlotIndex StartIdx, SlotIndex Kill);
486
487
    /// Simplified version of the above "extendInBlock", which assumes that
488
    /// no register lanes are undefined by <def,read-undef> operands.
489
    /// If this range is live before @p Use in the basic block that starts
490
    /// at @p StartIdx, extend it to be live up to @p Use, and return the
491
    /// value. If there is no segment before @p Use, return nullptr.
492
    VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill);
493
494
    /// join - Join two live ranges (this, and other) together.  This applies
495
    /// mappings to the value numbers in the LHS/RHS ranges as specified.  If
496
    /// the ranges are not joinable, this aborts.
497
    void join(LiveRange &Other,
498
              const int *ValNoAssignments,
499
              const int *RHSValNoAssignments,
500
              SmallVectorImpl<VNInfo *> &NewVNInfo);
501
502
    /// True iff this segment is a single segment that lies between the
503
    /// specified boundaries, exclusively. Vregs live across a backedge are not
504
    /// considered local. The boundaries are expected to lie within an extended
505
    /// basic block, so vregs that are not live out should contain no holes.
506
281k
    bool isLocal(SlotIndex Start, SlotIndex End) const {
507
281k
      return beginIndex() > Start.getBaseIndex() &&
508
281k
        
endIndex() < End.getBoundaryIndex()139k
;
509
281k
    }
510
511
    /// Remove the specified segment from this range.  Note that the segment
512
    /// must be a single Segment in its entirety.
513
    void removeSegment(SlotIndex Start, SlotIndex End,
514
                       bool RemoveDeadValNo = false);
515
516
613
    void removeSegment(Segment S, bool RemoveDeadValNo = false) {
517
613
      removeSegment(S.start, S.end, RemoveDeadValNo);
518
613
    }
519
520
    /// Remove segment pointed to by iterator @p I from this range.  This does
521
    /// not remove dead value numbers.
522
4.66k
    iterator removeSegment(iterator I) {
523
4.66k
      return segments.erase(I);
524
4.66k
    }
525
526
    /// Query Liveness at Idx.
527
    /// The sub-instruction slot of Idx doesn't matter, only the instruction
528
    /// it refers to is considered.
529
43.7M
    LiveQueryResult Query(SlotIndex Idx) const {
530
43.7M
      // Find the segment that enters the instruction.
531
43.7M
      const_iterator I = find(Idx.getBaseIndex());
532
43.7M
      const_iterator E = end();
533
43.7M
      if (I == E)
534
4.69M
        return LiveQueryResult(nullptr, nullptr, SlotIndex(), false);
535
39.0M
536
39.0M
      // Is this an instruction live-in segment?
537
39.0M
      // If Idx is the start index of a basic block, include live-in segments
538
39.0M
      // that start at Idx.getBaseIndex().
539
39.0M
      VNInfo *EarlyVal = nullptr;
540
39.0M
      VNInfo *LateVal  = nullptr;
541
39.0M
      SlotIndex EndPoint;
542
39.0M
      bool Kill = false;
543
39.0M
      if (I->start <= Idx.getBaseIndex()) {
544
26.7M
        EarlyVal = I->valno;
545
26.7M
        EndPoint = I->end;
546
26.7M
        // Move to the potentially live-out segment.
547
26.7M
        if (SlotIndex::isSameInstr(Idx, I->end)) {
548
17.0M
          Kill = true;
549
17.0M
          if (++I == E)
550
12.9M
            return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
551
13.7M
        }
552
13.7M
        // Special case: A PHIDef value can have its def in the middle of a
553
13.7M
        // segment if the value happens to be live out of the layout
554
13.7M
        // predecessor.
555
13.7M
        // Such a value is not live-in.
556
13.7M
        if (EarlyVal->def == Idx.getBaseIndex())
557
40.4k
          EarlyVal = nullptr;
558
13.7M
      }
559
39.0M
      // I now points to the segment that may be live-through, or defined by
560
39.0M
      // this instr. Ignore segments starting after the current instr.
561
39.0M
      
if (26.0M
!SlotIndex::isEarlierInstr(Idx, I->start)26.0M
) {
562
13.5M
        LateVal = I->valno;
563
13.5M
        EndPoint = I->end;
564
13.5M
      }
565
26.0M
      return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
566
39.0M
    }
567
568
    /// removeValNo - Remove all the segments defined by the specified value#.
569
    /// Also remove the value# from value# list.
570
    void removeValNo(VNInfo *ValNo);
571
572
    /// Returns true if the live range is zero length, i.e. no live segments
573
    /// span instructions. It doesn't pay to spill such a range.
574
7.71M
    bool isZeroLength(SlotIndexes *Indexes) const {
575
7.71M
      for (const Segment &S : segments)
576
7.95M
        if (Indexes->getNextNonNullIndex(S.start).getBaseIndex() <
577
7.95M
            S.end.getBaseIndex())
578
5.17M
          return false;
579
7.71M
      
return true2.53M
;
580
7.71M
    }
581
582
    // Returns true if any segment in the live range contains any of the
583
    // provided slot indexes.  Slots which occur in holes between
584
    // segments will not cause the function to return true.
585
    bool isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const;
586
587
0
    bool operator<(const LiveRange& other) const {
588
0
      const SlotIndex &thisIndex = beginIndex();
589
0
      const SlotIndex &otherIndex = other.beginIndex();
590
0
      return thisIndex < otherIndex;
591
0
    }
592
593
    /// Returns true if there is an explicit "undef" between @p Begin
594
    /// @p End.
595
    bool isUndefIn(ArrayRef<SlotIndex> Undefs, SlotIndex Begin,
596
50.4M
                   SlotIndex End) const {
597
50.4M
      return std::any_of(Undefs.begin(), Undefs.end(),
598
50.4M
                [Begin,End] (SlotIndex Idx) -> bool {
599
276k
                  return Begin <= Idx && 
Idx < End266
;
600
276k
                });
601
50.4M
    }
602
603
    /// Flush segment set into the regular segment vector.
604
    /// The method is to be called after the live range
605
    /// has been created, if use of the segment set was
606
    /// activated in the constructor of the live range.
607
    void flushSegmentSet();
608
609
    void print(raw_ostream &OS) const;
610
    void dump() const;
611
612
    /// Walk the range and assert if any invariants fail to hold.
613
    ///
614
    /// Note that this is a no-op when asserts are disabled.
615
#ifdef NDEBUG
616
20.3M
    void verify() const {}
617
#else
618
    void verify() const;
619
#endif
620
621
  protected:
622
    /// Append a segment to the list of segments.
623
    void append(const LiveRange::Segment S);
624
625
  private:
626
    friend class LiveRangeUpdater;
627
    void addSegmentToSet(Segment S);
628
    void markValNoForDeletion(VNInfo *V);
629
  };
630
631
0
  inline raw_ostream &operator<<(raw_ostream &OS, const LiveRange &LR) {
632
0
    LR.print(OS);
633
0
    return OS;
634
0
  }
635
636
  /// LiveInterval - This class represents the liveness of a register,
637
  /// or stack slot.
638
  class LiveInterval : public LiveRange {
639
  public:
640
    using super = LiveRange;
641
642
    /// A live range for subregisters. The LaneMask specifies which parts of the
643
    /// super register are covered by the interval.
644
    /// (@sa TargetRegisterInfo::getSubRegIndexLaneMask()).
645
    class SubRange : public LiveRange {
646
    public:
647
      SubRange *Next = nullptr;
648
      LaneBitmask LaneMask;
649
650
      /// Constructs a new SubRange object.
651
146k
      SubRange(LaneBitmask LaneMask) : LaneMask(LaneMask) {}
652
653
      /// Constructs a new SubRange object by copying liveness from @p Other.
654
      SubRange(LaneBitmask LaneMask, const LiveRange &Other,
655
               BumpPtrAllocator &Allocator)
656
179k
        : LiveRange(Other, Allocator), LaneMask(LaneMask) {}
657
658
      void print(raw_ostream &OS) const;
659
      void dump() const;
660
    };
661
662
  private:
663
    SubRange *SubRanges = nullptr; ///< Single linked list of subregister live
664
                                   /// ranges.
665
666
  public:
667
    const unsigned reg;  // the register or stack slot of this interval.
668
    float weight;        // weight of this interval
669
670
13.0M
    LiveInterval(unsigned Reg, float Weight) : reg(Reg), weight(Weight) {}
671
672
13.0M
    ~LiveInterval() {
673
13.0M
      clearSubRanges();
674
13.0M
    }
675
676
    template<typename T>
677
    class SingleLinkedListIterator {
678
      T *P;
679
680
    public:
681
31.5M
      SingleLinkedListIterator<T>(T *P) : P(P) {}
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange const>::SingleLinkedListIterator(llvm::LiveInterval::SubRange const*)
Line
Count
Source
681
17.9M
      SingleLinkedListIterator<T>(T *P) : P(P) {}
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange>::SingleLinkedListIterator(llvm::LiveInterval::SubRange*)
Line
Count
Source
681
13.6M
      SingleLinkedListIterator<T>(T *P) : P(P) {}
682
683
35.6M
      SingleLinkedListIterator<T> &operator++() {
684
35.6M
        P = P->Next;
685
35.6M
        return *this;
686
35.6M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange const>::operator++()
Line
Count
Source
683
27.8M
      SingleLinkedListIterator<T> &operator++() {
684
27.8M
        P = P->Next;
685
27.8M
        return *this;
686
27.8M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange>::operator++()
Line
Count
Source
683
7.80M
      SingleLinkedListIterator<T> &operator++() {
684
7.80M
        P = P->Next;
685
7.80M
        return *this;
686
7.80M
      }
687
      SingleLinkedListIterator<T> operator++(int) {
688
        SingleLinkedListIterator res = *this;
689
        ++*this;
690
        return res;
691
      }
692
51.3M
      bool operator!=(const SingleLinkedListIterator<T> &Other) {
693
51.3M
        return P != Other.operator->();
694
51.3M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange const>::operator!=(llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange const> const&)
Line
Count
Source
692
36.7M
      bool operator!=(const SingleLinkedListIterator<T> &Other) {
693
36.7M
        return P != Other.operator->();
694
36.7M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange>::operator!=(llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange> const&)
Line
Count
Source
692
14.6M
      bool operator!=(const SingleLinkedListIterator<T> &Other) {
693
14.6M
        return P != Other.operator->();
694
14.6M
      }
695
      bool operator==(const SingleLinkedListIterator<T> &Other) {
696
        return P == Other.operator->();
697
      }
698
40.3M
      T &operator*() const {
699
40.3M
        return *P;
700
40.3M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange const>::operator*() const
Line
Count
Source
698
28.5M
      T &operator*() const {
699
28.5M
        return *P;
700
28.5M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange>::operator*() const
Line
Count
Source
698
11.8M
      T &operator*() const {
699
11.8M
        return *P;
700
11.8M
      }
701
51.3M
      T *operator->() const {
702
51.3M
        return P;
703
51.3M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange const>::operator->() const
Line
Count
Source
701
36.7M
      T *operator->() const {
702
36.7M
        return P;
703
36.7M
      }
llvm::LiveInterval::SingleLinkedListIterator<llvm::LiveInterval::SubRange>::operator->() const
Line
Count
Source
701
14.6M
      T *operator->() const {
702
14.6M
        return P;
703
14.6M
      }
704
    };
705
706
    using subrange_iterator = SingleLinkedListIterator<SubRange>;
707
    using const_subrange_iterator = SingleLinkedListIterator<const SubRange>;
708
709
6.81M
    subrange_iterator subrange_begin() {
710
6.81M
      return subrange_iterator(SubRanges);
711
6.81M
    }
712
6.81M
    subrange_iterator subrange_end() {
713
6.81M
      return subrange_iterator(nullptr);
714
6.81M
    }
715
716
8.95M
    const_subrange_iterator subrange_begin() const {
717
8.95M
      return const_subrange_iterator(SubRanges);
718
8.95M
    }
719
8.95M
    const_subrange_iterator subrange_end() const {
720
8.95M
      return const_subrange_iterator(nullptr);
721
8.95M
    }
722
723
6.81M
    iterator_range<subrange_iterator> subranges() {
724
6.81M
      return make_range(subrange_begin(), subrange_end());
725
6.81M
    }
726
727
8.95M
    iterator_range<const_subrange_iterator> subranges() const {
728
8.95M
      return make_range(subrange_begin(), subrange_end());
729
8.95M
    }
730
731
    /// Creates a new empty subregister live range. The range is added at the
732
    /// beginning of the subrange list; subrange iterators stay valid.
733
    SubRange *createSubRange(BumpPtrAllocator &Allocator,
734
146k
                             LaneBitmask LaneMask) {
735
146k
      SubRange *Range = new (Allocator) SubRange(LaneMask);
736
146k
      appendSubRange(Range);
737
146k
      return Range;
738
146k
    }
739
740
    /// Like createSubRange() but the new range is filled with a copy of the
741
    /// liveness information in @p CopyFrom.
742
    SubRange *createSubRangeFrom(BumpPtrAllocator &Allocator,
743
                                 LaneBitmask LaneMask,
744
179k
                                 const LiveRange &CopyFrom) {
745
179k
      SubRange *Range = new (Allocator) SubRange(LaneMask, CopyFrom, Allocator);
746
179k
      appendSubRange(Range);
747
179k
      return Range;
748
179k
    }
749
750
    /// Returns true if subregister liveness information is available.
751
205M
    bool hasSubRanges() const {
752
205M
      return SubRanges != nullptr;
753
205M
    }
754
755
    /// Removes all subregister liveness information.
756
    void clearSubRanges();
757
758
    /// Removes all subranges without any segments (subranges without segments
759
    /// are not considered valid and should only exist temporarily).
760
    void removeEmptySubRanges();
761
762
    /// getSize - Returns the sum of sizes of all the LiveRange's.
763
    ///
764
    unsigned getSize() const;
765
766
    /// isSpillable - Can this interval be spilled?
767
21.5M
    bool isSpillable() const {
768
21.5M
      return weight != huge_valf;
769
21.5M
    }
770
771
    /// markNotSpillable - Mark interval as not spillable
772
2.53M
    void markNotSpillable() {
773
2.53M
      weight = huge_valf;
774
2.53M
    }
775
776
    /// For a given lane mask @p LaneMask, compute indexes at which the
777
    /// lane is marked undefined by subregister <def,read-undef> definitions.
778
    void computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs,
779
                               LaneBitmask LaneMask,
780
                               const MachineRegisterInfo &MRI,
781
                               const SlotIndexes &Indexes) const;
782
783
    /// Refines the subranges to support \p LaneMask. This may only be called
784
    /// for LI.hasSubrange()==true. Subregister ranges are split or created
785
    /// until \p LaneMask can be matched exactly. \p Mod is executed on the
786
    /// matching subranges.
787
    ///
788
    /// Example:
789
    ///    Given an interval with subranges with lanemasks L0F00, L00F0 and
790
    ///    L000F, refining for mask L0018. Will split the L00F0 lane into
791
    ///    L00E0 and L0010 and the L000F lane into L0007 and L0008. The Mod
792
    ///    function will be applied to the L0010 and L0008 subranges.
793
    void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask,
794
                         std::function<void(LiveInterval::SubRange&)> Apply);
795
796
    bool operator<(const LiveInterval& other) const {
797
      const SlotIndex &thisIndex = beginIndex();
798
      const SlotIndex &otherIndex = other.beginIndex();
799
      return std::tie(thisIndex, reg) < std::tie(otherIndex, other.reg);
800
    }
801
802
    void print(raw_ostream &OS) const;
803
    void dump() const;
804
805
    /// Walks the interval and assert if any invariants fail to hold.
806
    ///
807
    /// Note that this is a no-op when asserts are disabled.
808
#ifdef NDEBUG
809
1.35k
    void verify(const MachineRegisterInfo *MRI = nullptr) const {}
810
#else
811
    void verify(const MachineRegisterInfo *MRI = nullptr) const;
812
#endif
813
814
  private:
815
    /// Appends @p Range to SubRanges list.
816
325k
    void appendSubRange(SubRange *Range) {
817
325k
      Range->Next = SubRanges;
818
325k
      SubRanges = Range;
819
325k
    }
820
821
    /// Free memory held by SubRange.
822
    void freeSubRange(SubRange *S);
823
  };
824
825
  inline raw_ostream &operator<<(raw_ostream &OS,
826
0
                                 const LiveInterval::SubRange &SR) {
827
0
    SR.print(OS);
828
0
    return OS;
829
0
  }
830
831
0
  inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
832
0
    LI.print(OS);
833
0
    return OS;
834
0
  }
835
836
  raw_ostream &operator<<(raw_ostream &OS, const LiveRange::Segment &S);
837
838
114M
  inline bool operator<(SlotIndex V, const LiveRange::Segment &S) {
839
114M
    return V < S.start;
840
114M
  }
841
842
625k
  inline bool operator<(const LiveRange::Segment &S, SlotIndex V) {
843
625k
    return S.start < V;
844
625k
  }
845
846
  /// Helper class for performant LiveRange bulk updates.
847
  ///
848
  /// Calling LiveRange::addSegment() repeatedly can be expensive on large
849
  /// live ranges because segments after the insertion point may need to be
850
  /// shifted. The LiveRangeUpdater class can defer the shifting when adding
851
  /// many segments in order.
852
  ///
853
  /// The LiveRange will be in an invalid state until flush() is called.
854
  class LiveRangeUpdater {
855
    LiveRange *LR;
856
    SlotIndex LastStart;
857
    LiveRange::iterator WriteI;
858
    LiveRange::iterator ReadI;
859
    SmallVector<LiveRange::Segment, 16> Spills;
860
    void mergeSpills();
861
862
  public:
863
    /// Create a LiveRangeUpdater for adding segments to LR.
864
    /// LR will temporarily be in an invalid state until flush() is called.
865
8.46M
    LiveRangeUpdater(LiveRange *lr = nullptr) : LR(lr) {}
866
867
8.46M
    ~LiveRangeUpdater() { flush(); }
868
869
    /// Add a segment to LR and coalesce when possible, just like
870
    /// LR.addSegment(). Segments should be added in increasing start order for
871
    /// best performance.
872
    void add(LiveRange::Segment);
873
874
25.8M
    void add(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
875
25.8M
      add(LiveRange::Segment(Start, End, VNI));
876
25.8M
    }
877
878
    /// Return true if the LR is currently in an invalid state, and flush()
879
    /// needs to be called.
880
19.9M
    bool isDirty() const { return LastStart.isValid(); }
881
882
    /// Flush the updater state to LR so it is valid and contains all added
883
    /// segments.
884
    void flush();
885
886
    /// Select a different destination live range.
887
954k
    void setDest(LiveRange *lr) {
888
954k
      if (LR != lr && 
isDirty()56.6k
)
889
481
        flush();
890
954k
      LR = lr;
891
954k
    }
892
893
    /// Get the current destination live range.
894
    LiveRange *getDest() const { return LR; }
895
896
    void dump() const;
897
    void print(raw_ostream&) const;
898
  };
899
900
0
  inline raw_ostream &operator<<(raw_ostream &OS, const LiveRangeUpdater &X) {
901
0
    X.print(OS);
902
0
    return OS;
903
0
  }
904
905
  /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
906
  /// LiveInterval into equivalence clases of connected components. A
907
  /// LiveInterval that has multiple connected components can be broken into
908
  /// multiple LiveIntervals.
909
  ///
910
  /// Given a LiveInterval that may have multiple connected components, run:
911
  ///
912
  ///   unsigned numComps = ConEQ.Classify(LI);
913
  ///   if (numComps > 1) {
914
  ///     // allocate numComps-1 new LiveIntervals into LIS[1..]
915
  ///     ConEQ.Distribute(LIS);
916
  /// }
917
918
  class ConnectedVNInfoEqClasses {
919
    LiveIntervals &LIS;
920
    IntEqClasses EqClass;
921
922
  public:
923
2.05M
    explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
924
925
    /// Classify the values in \p LR into connected components.
926
    /// Returns the number of connected components.
927
    unsigned Classify(const LiveRange &LR);
928
929
    /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
930
    /// the equivalence class assigned the VNI.
931
605k
    unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
932
933
    /// Distribute values in \p LI into a separate LiveIntervals
934
    /// for each connected component. LIV must have an empty LiveInterval for
935
    /// each additional connected component. The first connected component is
936
    /// left in \p LI.
937
    void Distribute(LiveInterval &LI, LiveInterval *LIV[],
938
                    MachineRegisterInfo &MRI);
939
  };
940
941
} // end namespace llvm
942
943
#endif // LLVM_CODEGEN_LIVEINTERVAL_H