Coverage Report

Created: 2018-11-16 02:38

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