Coverage Report

Created: 2019-02-23 12:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/CodeGen/SlotIndexes.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
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
// This file implements SlotIndex and related classes. The purpose of SlotIndex
10
// is to describe a position at which a register can become live, or cease to
11
// be live.
12
//
13
// SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which
14
// is held is LiveIntervals and provides the real numbering. This allows
15
// LiveIntervals to perform largely transparent renumbering.
16
//===----------------------------------------------------------------------===//
17
18
#ifndef LLVM_CODEGEN_SLOTINDEXES_H
19
#define LLVM_CODEGEN_SLOTINDEXES_H
20
21
#include "llvm/ADT/DenseMap.h"
22
#include "llvm/ADT/IntervalMap.h"
23
#include "llvm/ADT/PointerIntPair.h"
24
#include "llvm/ADT/SmallVector.h"
25
#include "llvm/ADT/ilist.h"
26
#include "llvm/CodeGen/MachineBasicBlock.h"
27
#include "llvm/CodeGen/MachineFunction.h"
28
#include "llvm/CodeGen/MachineFunctionPass.h"
29
#include "llvm/CodeGen/MachineInstr.h"
30
#include "llvm/CodeGen/MachineInstrBundle.h"
31
#include "llvm/Pass.h"
32
#include "llvm/Support/Allocator.h"
33
#include <algorithm>
34
#include <cassert>
35
#include <iterator>
36
#include <utility>
37
38
namespace llvm {
39
40
class raw_ostream;
41
42
  /// This class represents an entry in the slot index list held in the
43
  /// SlotIndexes pass. It should not be used directly. See the
44
  /// SlotIndex & SlotIndexes classes for the public interface to this
45
  /// information.
46
  class IndexListEntry : public ilist_node<IndexListEntry> {
47
    MachineInstr *mi;
48
    unsigned index;
49
50
  public:
51
62.6M
    IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
52
53
243M
    MachineInstr* getInstr() const { return mi; }
54
9.24M
    void setInstr(MachineInstr *mi) {
55
9.24M
      this->mi = mi;
56
9.24M
    }
57
58
7.39G
    unsigned getIndex() const { return index; }
59
123M
    void setIndex(unsigned index) {
60
123M
      this->index = index;
61
123M
    }
62
63
#ifdef EXPENSIVE_CHECKS
64
    // When EXPENSIVE_CHECKS is defined, "erased" index list entries will
65
    // actually be moved to a "graveyard" list, and have their pointers
66
    // poisoned, so that dangling SlotIndex access can be reliably detected.
67
    void setPoison() {
68
      intptr_t tmp = reinterpret_cast<intptr_t>(mi);
69
      assert(((tmp & 0x1) == 0x0) && "Pointer already poisoned?");
70
      tmp |= 0x1;
71
      mi = reinterpret_cast<MachineInstr*>(tmp);
72
    }
73
74
    bool isPoisoned() const { return (reinterpret_cast<intptr_t>(mi) & 0x1) == 0x1; }
75
#endif // EXPENSIVE_CHECKS
76
  };
77
78
  template <>
79
  struct ilist_alloc_traits<IndexListEntry>
80
      : public ilist_noalloc_traits<IndexListEntry> {};
81
82
  /// SlotIndex - An opaque wrapper around machine indexes.
83
0
  class SlotIndex {
Unexecuted instantiation: llvm::SlotIndex::operator=(llvm::SlotIndex const&)
Unexecuted instantiation: llvm::SlotIndex::operator=(llvm::SlotIndex&&)
84
    friend class SlotIndexes;
85
86
    enum Slot {
87
      /// Basic block boundary.  Used for live ranges entering and leaving a
88
      /// block without being live in the layout neighbor.  Also used as the
89
      /// def slot of PHI-defs.
90
      Slot_Block,
91
92
      /// Early-clobber register use/def slot.  A live range defined at
93
      /// Slot_EarlyClobber interferes with normal live ranges killed at
94
      /// Slot_Register.  Also used as the kill slot for live ranges tied to an
95
      /// early-clobber def.
96
      Slot_EarlyClobber,
97
98
      /// Normal register use/def slot.  Normal instructions kill and define
99
      /// register live ranges at this slot.
100
      Slot_Register,
101
102
      /// Dead def kill point.  Kill slot for a live range that is defined by
103
      /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
104
      /// used anywhere.
105
      Slot_Dead,
106
107
      Slot_Count
108
    };
109
110
    PointerIntPair<IndexListEntry*, 2, unsigned> lie;
111
112
    SlotIndex(IndexListEntry *entry, unsigned slot)
113
628M
      : lie(entry, slot) {}
114
115
8.06G
    IndexListEntry* listEntry() const {
116
8.06G
      assert(isValid() && "Attempt to compare reserved index.");
117
8.06G
#ifdef EXPENSIVE_CHECKS
118
8.06G
      assert(!lie.getPointer()->isPoisoned() &&
119
8.06G
             "Attempt to access deleted list-entry.");
120
8.06G
#endif // EXPENSIVE_CHECKS
121
8.06G
      return lie.getPointer();
122
8.06G
    }
123
124
7.11G
    unsigned getIndex() const {
125
7.11G
      return listEntry()->getIndex() | getSlot();
126
7.11G
    }
127
128
    /// Returns the slot for this SlotIndex.
129
7.47G
    Slot getSlot() const {
130
7.47G
      return static_cast<Slot>(lie.getInt());
131
7.47G
    }
132
133
  public:
134
    enum {
135
      /// The default distance between instructions as returned by distance().
136
      /// This may vary as instructions are inserted and removed.
137
      InstrDist = 4 * Slot_Count
138
    };
139
140
    /// Construct an invalid index.
141
683M
    SlotIndex() = default;
142
143
    // Construct a new slot index from the given one, and set the slot.
144
    SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
145
      assert(lie.getPointer() != nullptr &&
146
             "Attempt to construct index with 0 pointer.");
147
    }
148
149
    /// Returns true if this is a valid index. Invalid indices do
150
    /// not point into an index table, and cannot be compared.
151
942M
    bool isValid() const {
152
942M
      return lie.getPointer();
153
942M
    }
154
155
    /// Return true for a valid index.
156
5.78M
    explicit operator bool() const { return isValid(); }
157
158
    /// Print this index to the given raw_ostream.
159
    void print(raw_ostream &os) const;
160
161
    /// Dump this index to stderr.
162
    void dump() const;
163
164
    /// Compare two SlotIndex objects for equality.
165
123M
    bool operator==(SlotIndex other) const {
166
123M
      return lie == other.lie;
167
123M
    }
168
    /// Compare two SlotIndex objects for inequality.
169
66.1M
    bool operator!=(SlotIndex other) const {
170
66.1M
      return lie != other.lie;
171
66.1M
    }
172
173
    /// Compare two SlotIndex objects. Return true if the first index
174
    /// is strictly lower than the second.
175
1.28G
    bool operator<(SlotIndex other) const {
176
1.28G
      return getIndex() < other.getIndex();
177
1.28G
    }
178
    /// Compare two SlotIndex objects. Return true if the first index
179
    /// is lower than, or equal to, the second.
180
1.30G
    bool operator<=(SlotIndex other) const {
181
1.30G
      return getIndex() <= other.getIndex();
182
1.30G
    }
183
184
    /// Compare two SlotIndex objects. Return true if the first index
185
    /// is greater than the second.
186
300M
    bool operator>(SlotIndex other) const {
187
300M
      return getIndex() > other.getIndex();
188
300M
    }
189
190
    /// Compare two SlotIndex objects. Return true if the first index
191
    /// is greater than, or equal to, the second.
192
638M
    bool operator>=(SlotIndex other) const {
193
638M
      return getIndex() >= other.getIndex();
194
638M
    }
195
196
    /// isSameInstr - Return true if A and B refer to the same instruction.
197
40.7M
    static bool isSameInstr(SlotIndex A, SlotIndex B) {
198
40.7M
      return A.lie.getPointer() == B.lie.getPointer();
199
40.7M
    }
200
201
    /// isEarlierInstr - Return true if A refers to an instruction earlier than
202
    /// B. This is equivalent to A < B && !isSameInstr(A, B).
203
68.8M
    static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
204
68.8M
      return A.listEntry()->getIndex() < B.listEntry()->getIndex();
205
68.8M
    }
206
207
    /// Return true if A refers to the same instruction as B or an earlier one.
208
    /// This is equivalent to !isEarlierInstr(B, A).
209
910k
    static bool isEarlierEqualInstr(SlotIndex A, SlotIndex B) {
210
910k
      return !isEarlierInstr(B, A);
211
910k
    }
212
213
    /// Return the distance from this index to the given one.
214
28.5M
    int distance(SlotIndex other) const {
215
28.5M
      return other.getIndex() - getIndex();
216
28.5M
    }
217
218
    /// Return the scaled distance from this index to the given one, where all
219
    /// slots on the same instruction have zero distance.
220
5.72M
    int getInstrDistance(SlotIndex other) const {
221
5.72M
      return (other.listEntry()->getIndex() - listEntry()->getIndex())
222
5.72M
        / Slot_Count;
223
5.72M
    }
224
225
    /// isBlock - Returns true if this is a block boundary slot.
226
174M
    bool isBlock() const { return getSlot() == Slot_Block; }
227
228
    /// isEarlyClobber - Returns true if this is an early-clobber slot.
229
5.74M
    bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
230
231
    /// isRegister - Returns true if this is a normal register use/def slot.
232
    /// Note that early-clobber slots may also be used for uses and defs.
233
3.72M
    bool isRegister() const { return getSlot() == Slot_Register; }
234
235
    /// isDead - Returns true if this is a dead def kill slot.
236
10.7M
    bool isDead() const { return getSlot() == Slot_Dead; }
237
238
    /// Returns the base index for associated with this index. The base index
239
    /// is the one associated with the Slot_Block slot for the instruction
240
    /// pointed to by this index.
241
171M
    SlotIndex getBaseIndex() const {
242
171M
      return SlotIndex(listEntry(), Slot_Block);
243
171M
    }
244
245
    /// Returns the boundary index for associated with this index. The boundary
246
    /// index is the one associated with the Slot_Block slot for the instruction
247
    /// pointed to by this index.
248
25.7M
    SlotIndex getBoundaryIndex() const {
249
25.7M
      return SlotIndex(listEntry(), Slot_Dead);
250
25.7M
    }
251
252
    /// Returns the register use/def slot in the current instruction for a
253
    /// normal or early-clobber def.
254
110M
    SlotIndex getRegSlot(bool EC = false) const {
255
110M
      return SlotIndex(listEntry(), EC ? 
Slot_EarlyClobber4.65M
:
Slot_Register106M
);
256
110M
    }
257
258
    /// Returns the dead def kill slot for the current instruction.
259
81.0M
    SlotIndex getDeadSlot() const {
260
81.0M
      return SlotIndex(listEntry(), Slot_Dead);
261
81.0M
    }
262
263
    /// Returns the next slot in the index list. This could be either the
264
    /// next slot for the instruction pointed to by this index or, if this
265
    /// index is a STORE, the first slot for the next instruction.
266
    /// WARNING: This method is considerably more expensive than the methods
267
    /// that return specific slots (getUseIndex(), etc). If you can - please
268
    /// use one of those methods.
269
9.19M
    SlotIndex getNextSlot() const {
270
9.19M
      Slot s = getSlot();
271
9.19M
      if (s == Slot_Dead) {
272
24.3k
        return SlotIndex(&*++listEntry()->getIterator(), Slot_Block);
273
24.3k
      }
274
9.17M
      return SlotIndex(listEntry(), s + 1);
275
9.17M
    }
276
277
    /// Returns the next index. This is the index corresponding to the this
278
    /// index's slot, but for the next instruction.
279
27.8k
    SlotIndex getNextIndex() const {
280
27.8k
      return SlotIndex(&*++listEntry()->getIterator(), getSlot());
281
27.8k
    }
282
283
    /// Returns the previous slot in the index list. This could be either the
284
    /// previous slot for the instruction pointed to by this index or, if this
285
    /// index is a Slot_Block, the last slot for the previous instruction.
286
    /// WARNING: This method is considerably more expensive than the methods
287
    /// that return specific slots (getUseIndex(), etc). If you can - please
288
    /// use one of those methods.
289
147M
    SlotIndex getPrevSlot() const {
290
147M
      Slot s = getSlot();
291
147M
      if (s == Slot_Block) {
292
73.1M
        return SlotIndex(&*--listEntry()->getIterator(), Slot_Dead);
293
73.1M
      }
294
74.6M
      return SlotIndex(listEntry(), s - 1);
295
74.6M
    }
296
297
    /// Returns the previous index. This is the index corresponding to this
298
    /// index's slot, but for the previous instruction.
299
41.6k
    SlotIndex getPrevIndex() const {
300
41.6k
      return SlotIndex(&*--listEntry()->getIterator(), getSlot());
301
41.6k
    }
302
  };
303
304
8.07k
  inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
305
8.07k
    li.print(os);
306
8.07k
    return os;
307
8.07k
  }
308
309
  using IdxMBBPair = std::pair<SlotIndex, MachineBasicBlock *>;
310
311
0
  inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
312
0
    return V < IM.first;
313
0
  }
314
315
41.4M
  inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
316
41.4M
    return IM.first < V;
317
41.4M
  }
318
319
  struct Idx2MBBCompare {
320
8.91M
    bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
321
8.91M
      return LHS.first < RHS.first;
322
8.91M
    }
323
  };
324
325
  /// SlotIndexes pass.
326
  ///
327
  /// This pass assigns indexes to each instruction.
328
  class SlotIndexes : public MachineFunctionPass {
329
  private:
330
    // IndexListEntry allocator.
331
    BumpPtrAllocator ileAllocator;
332
333
    using IndexList = ilist<IndexListEntry>;
334
    IndexList indexList;
335
336
#ifdef EXPENSIVE_CHECKS
337
    IndexList graveyardList;
338
#endif // EXPENSIVE_CHECKS
339
340
    MachineFunction *mf;
341
342
    using Mi2IndexMap = DenseMap<const MachineInstr *, SlotIndex>;
343
    Mi2IndexMap mi2iMap;
344
345
    /// MBBRanges - Map MBB number to (start, stop) indexes.
346
    SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
347
348
    /// Idx2MBBMap - Sorted list of pairs of index of first instruction
349
    /// and MBB id.
350
    SmallVector<IdxMBBPair, 8> idx2MBBMap;
351
352
62.6M
    IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
353
62.6M
      IndexListEntry *entry =
354
62.6M
          static_cast<IndexListEntry *>(ileAllocator.Allocate(
355
62.6M
              sizeof(IndexListEntry), alignof(IndexListEntry)));
356
62.6M
357
62.6M
      new (entry) IndexListEntry(mi, index);
358
62.6M
359
62.6M
      return entry;
360
62.6M
    }
361
362
    /// Renumber locally after inserting curItr.
363
    void renumberIndexes(IndexList::iterator curItr);
364
365
  public:
366
    static char ID;
367
368
68.8k
    SlotIndexes() : MachineFunctionPass(ID) {
369
68.8k
      initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
370
68.8k
    }
371
372
68.5k
    ~SlotIndexes() override {
373
68.5k
      // The indexList's nodes are all allocated in the BumpPtrAllocator.
374
68.5k
      indexList.clearAndLeakNodesUnsafely();
375
68.5k
    }
376
377
    void getAnalysisUsage(AnalysisUsage &au) const override;
378
    void releaseMemory() override;
379
380
    bool runOnMachineFunction(MachineFunction &fn) override;
381
382
    /// Dump the indexes.
383
    void dump() const;
384
385
    /// Renumber the index list, providing space for new instructions.
386
    void renumberIndexes();
387
388
    /// Repair indexes after adding and removing instructions.
389
    void repairIndexesInRange(MachineBasicBlock *MBB,
390
                              MachineBasicBlock::iterator Begin,
391
                              MachineBasicBlock::iterator End);
392
393
    /// Returns the zero index for this analysis.
394
40.0k
    SlotIndex getZeroIndex() {
395
40.0k
      assert(indexList.front().getIndex() == 0 && "First index is not 0?");
396
40.0k
      return SlotIndex(&indexList.front(), 0);
397
40.0k
    }
398
399
    /// Returns the base index of the last slot in this analysis.
400
5.72M
    SlotIndex getLastIndex() {
401
5.72M
      return SlotIndex(&indexList.back(), 0);
402
5.72M
    }
403
404
    /// Returns true if the given machine instr is mapped to an index,
405
    /// otherwise returns false.
406
15.2M
    bool hasIndex(const MachineInstr &instr) const {
407
15.2M
      return mi2iMap.count(&instr);
408
15.2M
    }
409
410
    /// Returns the base index for the given instruction.
411
212M
    SlotIndex getInstructionIndex(const MachineInstr &MI) const {
412
212M
      // Instructions inside a bundle have the same number as the bundle itself.
413
212M
      auto BundleStart = getBundleStart(MI.getIterator());
414
212M
      auto BundleEnd = getBundleEnd(MI.getIterator());
415
212M
      // Use the first non-debug instruction in the bundle to get SlotIndex.
416
212M
      const MachineInstr &BundleNonDebug =
417
212M
          *skipDebugInstructionsForward(BundleStart, BundleEnd);
418
212M
      assert(!BundleNonDebug.isDebugInstr() &&
419
212M
             "Could not use a debug instruction to query mi2iMap.");
420
212M
      Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleNonDebug);
421
212M
      assert(itr != mi2iMap.end() && "Instruction not found in maps.");
422
212M
      return itr->second;
423
212M
    }
424
425
    /// Returns the instruction for the given index, or null if the given
426
    /// index has no instruction associated with it.
427
231M
    MachineInstr* getInstructionFromIndex(SlotIndex index) const {
428
18.4E
      return index.isValid() ? 
index.listEntry()->getInstr()231M
: nullptr;
429
231M
    }
430
431
    /// Returns the next non-null index, if one exists.
432
    /// Otherwise returns getLastIndex().
433
8.76M
    SlotIndex getNextNonNullIndex(SlotIndex Index) {
434
8.76M
      IndexList::iterator I = Index.listEntry()->getIterator();
435
8.76M
      IndexList::iterator E = indexList.end();
436
11.9M
      while (++I != E)
437
11.9M
        if (I->getInstr())
438
8.76M
          return SlotIndex(&*I, Index.getSlot());
439
8.76M
      // We reached the end of the function.
440
8.76M
      
return getLastIndex()12
;
441
8.76M
    }
442
443
    /// getIndexBefore - Returns the index of the last indexed instruction
444
    /// before MI, or the start index of its basic block.
445
    /// MI is not required to have an index.
446
1.47M
    SlotIndex getIndexBefore(const MachineInstr &MI) const {
447
1.47M
      const MachineBasicBlock *MBB = MI.getParent();
448
1.47M
      assert(MBB && "MI must be inserted in a basic block");
449
1.47M
      MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
450
1.47M
      while (true) {
451
1.47M
        if (I == B)
452
224k
          return getMBBStartIdx(MBB);
453
1.24M
        --I;
454
1.24M
        Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I);
455
1.24M
        if (MapItr != mi2iMap.end())
456
1.24M
          return MapItr->second;
457
1.24M
      }
458
1.47M
    }
459
460
    /// getIndexAfter - Returns the index of the first indexed instruction
461
    /// after MI, or the end index of its basic block.
462
    /// MI is not required to have an index.
463
289k
    SlotIndex getIndexAfter(const MachineInstr &MI) const {
464
289k
      const MachineBasicBlock *MBB = MI.getParent();
465
289k
      assert(MBB && "MI must be inserted in a basic block");
466
289k
      MachineBasicBlock::const_iterator I = MI, E = MBB->end();
467
289k
      while (true) {
468
289k
        ++I;
469
289k
        if (I == E)
470
54.6k
          return getMBBEndIdx(MBB);
471
235k
        Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I);
472
235k
        if (MapItr != mi2iMap.end())
473
235k
          return MapItr->second;
474
235k
      }
475
289k
    }
476
477
    /// Return the (start,end) range of the given basic block number.
478
    const std::pair<SlotIndex, SlotIndex> &
479
313M
    getMBBRange(unsigned Num) const {
480
313M
      return MBBRanges[Num];
481
313M
    }
482
483
    /// Return the (start,end) range of the given basic block.
484
    const std::pair<SlotIndex, SlotIndex> &
485
163M
    getMBBRange(const MachineBasicBlock *MBB) const {
486
163M
      return getMBBRange(MBB->getNumber());
487
163M
    }
488
489
    /// Returns the first index in the given basic block number.
490
38.9M
    SlotIndex getMBBStartIdx(unsigned Num) const {
491
38.9M
      return getMBBRange(Num).first;
492
38.9M
    }
493
494
    /// Returns the first index in the given basic block.
495
84.9M
    SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
496
84.9M
      return getMBBRange(mbb).first;
497
84.9M
    }
498
499
    /// Returns the last index in the given basic block number.
500
0
    SlotIndex getMBBEndIdx(unsigned Num) const {
501
0
      return getMBBRange(Num).second;
502
0
    }
503
504
    /// Returns the last index in the given basic block.
505
28.4M
    SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
506
28.4M
      return getMBBRange(mbb).second;
507
28.4M
    }
508
509
    /// Iterator over the idx2MBBMap (sorted pairs of slot index of basic block
510
    /// begin and basic block)
511
    using MBBIndexIterator = SmallVectorImpl<IdxMBBPair>::const_iterator;
512
513
    /// Move iterator to the next IdxMBBPair where the SlotIndex is greater or
514
    /// equal to \p To.
515
8.75M
    MBBIndexIterator advanceMBBIndex(MBBIndexIterator I, SlotIndex To) const {
516
8.75M
      return std::lower_bound(I, idx2MBBMap.end(), To);
517
8.75M
    }
518
519
    /// Get an iterator pointing to the IdxMBBPair with the biggest SlotIndex
520
    /// that is greater or equal to \p Idx.
521
3.14M
    MBBIndexIterator findMBBIndex(SlotIndex Idx) const {
522
3.14M
      return advanceMBBIndex(idx2MBBMap.begin(), Idx);
523
3.14M
    }
524
525
    /// Returns an iterator for the begin of the idx2MBBMap.
526
2.02M
    MBBIndexIterator MBBIndexBegin() const {
527
2.02M
      return idx2MBBMap.begin();
528
2.02M
    }
529
530
    /// Return an iterator for the end of the idx2MBBMap.
531
25.3M
    MBBIndexIterator MBBIndexEnd() const {
532
25.3M
      return idx2MBBMap.end();
533
25.3M
    }
534
535
    /// Returns the basic block which the given index falls in.
536
143M
    MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
537
143M
      if (MachineInstr *MI = getInstructionFromIndex(index))
538
140M
        return MI->getParent();
539
3.13M
540
3.13M
      MBBIndexIterator I = findMBBIndex(index);
541
3.13M
      // Take the pair containing the index
542
3.13M
      MBBIndexIterator J =
543
3.13M
        ((I != MBBIndexEnd() && 
I->first > index3.07M
) ||
544
3.13M
         
(2.51M
I == MBBIndexEnd()2.51M
&&
!idx2MBBMap.empty()68.5k
)) ?
std::prev(I)688k
:
I2.45M
;
545
3.13M
546
3.13M
      assert(J != MBBIndexEnd() && J->first <= index &&
547
3.13M
             index < getMBBEndIdx(J->second) &&
548
3.13M
             "index does not correspond to an MBB");
549
3.13M
      return J->second;
550
3.13M
    }
551
552
    /// Returns the MBB covering the given range, or null if the range covers
553
    /// more than one basic block.
554
0
    MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
555
0
556
0
      assert(start < end && "Backwards ranges not allowed.");
557
0
      MBBIndexIterator itr = findMBBIndex(start);
558
0
      if (itr == MBBIndexEnd()) {
559
0
        itr = std::prev(itr);
560
0
        return itr->second;
561
0
      }
562
0
563
0
      // Check that we don't cross the boundary into this block.
564
0
      if (itr->first < end)
565
0
        return nullptr;
566
0
567
0
      itr = std::prev(itr);
568
0
569
0
      if (itr->first <= start)
570
0
        return itr->second;
571
0
572
0
      return nullptr;
573
0
    }
574
575
    /// Insert the given machine instruction into the mapping. Returns the
576
    /// assigned index.
577
    /// If Late is set and there are null indexes between mi's neighboring
578
    /// instructions, create the new index after the null indexes instead of
579
    /// before them.
580
1.76M
    SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late = false) {
581
1.76M
      assert(!MI.isInsideBundle() &&
582
1.76M
             "Instructions inside bundles should use bundle start's slot.");
583
1.76M
      assert(mi2iMap.find(&MI) == mi2iMap.end() && "Instr already indexed.");
584
1.76M
      // Numbering debug instructions could cause code generation to be
585
1.76M
      // affected by debug information.
586
1.76M
      assert(!MI.isDebugInstr() && "Cannot number debug instructions.");
587
1.76M
588
1.76M
      assert(MI.getParent() != nullptr && "Instr must be added to function.");
589
1.76M
590
1.76M
      // Get the entries where MI should be inserted.
591
1.76M
      IndexList::iterator prevItr, nextItr;
592
1.76M
      if (Late) {
593
289k
        // Insert MI's index immediately before the following instruction.
594
289k
        nextItr = getIndexAfter(MI).listEntry()->getIterator();
595
289k
        prevItr = std::prev(nextItr);
596
1.47M
      } else {
597
1.47M
        // Insert MI's index immediately after the preceding instruction.
598
1.47M
        prevItr = getIndexBefore(MI).listEntry()->getIterator();
599
1.47M
        nextItr = std::next(prevItr);
600
1.47M
      }
601
1.76M
602
1.76M
      // Get a number for the new instr, or 0 if there's no room currently.
603
1.76M
      // In the latter case we'll force a renumber later.
604
1.76M
      unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
605
1.76M
      unsigned newNumber = prevItr->getIndex() + dist;
606
1.76M
607
1.76M
      // Insert a new list entry for MI.
608
1.76M
      IndexList::iterator newItr =
609
1.76M
          indexList.insert(nextItr, createEntry(&MI, newNumber));
610
1.76M
611
1.76M
      // Renumber locally if we need to.
612
1.76M
      if (dist == 0)
613
238k
        renumberIndexes(newItr);
614
1.76M
615
1.76M
      SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
616
1.76M
      mi2iMap.insert(std::make_pair(&MI, newIndex));
617
1.76M
      return newIndex;
618
1.76M
    }
619
620
    /// Removes machine instruction (bundle) \p MI from the mapping.
621
    /// This should be called before MachineInstr::eraseFromParent() is used to
622
    /// remove a whole bundle or an unbundled instruction.
623
    void removeMachineInstrFromMaps(MachineInstr &MI);
624
625
    /// Removes a single machine instruction \p MI from the mapping.
626
    /// This should be called before MachineInstr::eraseFromBundle() is used to
627
    /// remove a single instruction (out of a bundle).
628
    void removeSingleMachineInstrFromMaps(MachineInstr &MI);
629
630
    /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
631
    /// maps used by register allocator. \returns the index where the new
632
    /// instruction was inserted.
633
929k
    SlotIndex replaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI) {
634
929k
      Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
635
929k
      if (mi2iItr == mi2iMap.end())
636
0
        return SlotIndex();
637
929k
      SlotIndex replaceBaseIndex = mi2iItr->second;
638
929k
      IndexListEntry *miEntry(replaceBaseIndex.listEntry());
639
929k
      assert(miEntry->getInstr() == &MI &&
640
929k
             "Mismatched instruction in index tables.");
641
929k
      miEntry->setInstr(&NewMI);
642
929k
      mi2iMap.erase(mi2iItr);
643
929k
      mi2iMap.insert(std::make_pair(&NewMI, replaceBaseIndex));
644
929k
      return replaceBaseIndex;
645
929k
    }
646
647
    /// Add the given MachineBasicBlock into the maps.
648
0
    void insertMBBInMaps(MachineBasicBlock *mbb) {
649
0
      MachineFunction::iterator nextMBB =
650
0
        std::next(MachineFunction::iterator(mbb));
651
0
652
0
      IndexListEntry *startEntry = nullptr;
653
0
      IndexListEntry *endEntry = nullptr;
654
0
      IndexList::iterator newItr;
655
0
      if (nextMBB == mbb->getParent()->end()) {
656
0
        startEntry = &indexList.back();
657
0
        endEntry = createEntry(nullptr, 0);
658
0
        newItr = indexList.insertAfter(startEntry->getIterator(), endEntry);
659
0
      } else {
660
0
        startEntry = createEntry(nullptr, 0);
661
0
        endEntry = getMBBStartIdx(&*nextMBB).listEntry();
662
0
        newItr = indexList.insert(endEntry->getIterator(), startEntry);
663
0
      }
664
0
665
0
      SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
666
0
      SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
667
0
668
0
      MachineFunction::iterator prevMBB(mbb);
669
0
      assert(prevMBB != mbb->getParent()->end() &&
670
0
             "Can't insert a new block at the beginning of a function.");
671
0
      --prevMBB;
672
0
      MBBRanges[prevMBB->getNumber()].second = startIdx;
673
0
674
0
      assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
675
0
             "Blocks must be added in order");
676
0
      MBBRanges.push_back(std::make_pair(startIdx, endIdx));
677
0
      idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
678
0
679
0
      renumberIndexes(newItr);
680
0
      llvm::sort(idx2MBBMap, Idx2MBBCompare());
681
0
    }
682
683
    /// Free the resources that were required to maintain a SlotIndex.
684
    ///
685
    /// Once an index is no longer needed (for instance because the instruction
686
    /// at that index has been moved), the resources required to maintain the
687
    /// index can be relinquished to reduce memory use and improve renumbering
688
    /// performance. Any remaining SlotIndex objects that point to the same
689
    /// index are left 'dangling' (much the same as a dangling pointer to a
690
    /// freed object) and should not be accessed, except to destruct them.
691
    ///
692
    /// Like dangling pointers, access to dangling SlotIndexes can cause
693
    /// painful-to-track-down bugs, especially if the memory for the index
694
    /// previously pointed to has been re-used. To detect dangling SlotIndex
695
    /// bugs, build with EXPENSIVE_CHECKS=1. This will cause "erased" indexes to
696
    /// be retained in a graveyard instead of being freed. Operations on indexes
697
    /// in the graveyard will trigger an assertion.
698
0
    void eraseIndex(SlotIndex index) {
699
0
      IndexListEntry *entry = index.listEntry();
700
0
#ifdef EXPENSIVE_CHECKS
701
0
      indexList.remove(entry);
702
0
      graveyardList.push_back(entry);
703
0
      entry->setPoison();
704
0
#else
705
0
      indexList.erase(entry);
706
0
#endif
707
0
    }
708
  };
709
710
  // Specialize IntervalMapInfo for half-open slot index intervals.
711
  template <>
712
  struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
713
  };
714
715
} // end namespace llvm
716
717
#endif // LLVM_CODEGEN_SLOTINDEXES_H