Coverage Report

Created: 2019-07-24 05:18

/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
60.8M
    IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
52
53
240M
    MachineInstr* getInstr() const { return mi; }
54
8.96M
    void setInstr(MachineInstr *mi) {
55
8.96M
      this->mi = mi;
56
8.96M
    }
57
58
7.25G
    unsigned getIndex() const { return index; }
59
125M
    void setIndex(unsigned index) {
60
125M
      this->index = index;
61
125M
    }
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
  class 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
622M
      : lie(entry, slot) {}
114
115
7.92G
    IndexListEntry* listEntry() const {
116
7.92G
      assert(isValid() && "Attempt to compare reserved index.");
117
#ifdef EXPENSIVE_CHECKS
118
      assert(!lie.getPointer()->isPoisoned() &&
119
             "Attempt to access deleted list-entry.");
120
#endif // EXPENSIVE_CHECKS
121
      return lie.getPointer();
122
7.92G
    }
123
124
6.98G
    unsigned getIndex() const {
125
6.98G
      return listEntry()->getIndex() | getSlot();
126
6.98G
    }
127
128
    /// Returns the slot for this SlotIndex.
129
7.35G
    Slot getSlot() const {
130
7.35G
      return static_cast<Slot>(lie.getInt());
131
7.35G
    }
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
676M
    SlotIndex() = default;
142
143
    // Construct a new slot index from the given one, and set the slot.
144
0
    SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
145
0
      assert(lie.getPointer() != nullptr &&
146
0
             "Attempt to construct index with 0 pointer.");
147
0
    }
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
852M
    bool isValid() const {
152
852M
      return lie.getPointer();
153
852M
    }
154
155
    /// Return true for a valid index.
156
5.05M
    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
120M
    bool operator==(SlotIndex other) const {
166
120M
      return lie == other.lie;
167
120M
    }
168
    /// Compare two SlotIndex objects for inequality.
169
63.3M
    bool operator!=(SlotIndex other) const {
170
63.3M
      return lie != other.lie;
171
63.3M
    }
172
173
    /// Compare two SlotIndex objects. Return true if the first index
174
    /// is strictly lower than the second.
175
1.26G
    bool operator<(SlotIndex other) const {
176
1.26G
      return getIndex() < other.getIndex();
177
1.26G
    }
178
    /// Compare two SlotIndex objects. Return true if the first index
179
    /// is lower than, or equal to, the second.
180
1.29G
    bool operator<=(SlotIndex other) const {
181
1.29G
      return getIndex() <= other.getIndex();
182
1.29G
    }
183
184
    /// Compare two SlotIndex objects. Return true if the first index
185
    /// is greater than the second.
186
298M
    bool operator>(SlotIndex other) const {
187
298M
      return getIndex() > other.getIndex();
188
298M
    }
189
190
    /// Compare two SlotIndex objects. Return true if the first index
191
    /// is greater than, or equal to, the second.
192
615M
    bool operator>=(SlotIndex other) const {
193
615M
      return getIndex() >= other.getIndex();
194
615M
    }
195
196
    /// isSameInstr - Return true if A and B refer to the same instruction.
197
41.6M
    static bool isSameInstr(SlotIndex A, SlotIndex B) {
198
41.6M
      return A.lie.getPointer() == B.lie.getPointer();
199
41.6M
    }
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
64.9M
    static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
204
64.9M
      return A.listEntry()->getIndex() < B.listEntry()->getIndex();
205
64.9M
    }
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
1.03M
    static bool isEarlierEqualInstr(SlotIndex A, SlotIndex B) {
210
1.03M
      return !isEarlierInstr(B, A);
211
1.03M
    }
212
213
    /// Return the distance from this index to the given one.
214
28.2M
    int distance(SlotIndex other) const {
215
28.2M
      return other.getIndex() - getIndex();
216
28.2M
    }
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.68M
    int getInstrDistance(SlotIndex other) const {
221
5.68M
      return (other.listEntry()->getIndex() - listEntry()->getIndex())
222
5.68M
        / Slot_Count;
223
5.68M
    }
224
225
    /// isBlock - Returns true if this is a block boundary slot.
226
175M
    bool isBlock() const { return getSlot() == Slot_Block; }
227
228
    /// isEarlyClobber - Returns true if this is an early-clobber slot.
229
7.07M
    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
4.69M
    bool isRegister() const { return getSlot() == Slot_Register; }
234
235
    /// isDead - Returns true if this is a dead def kill slot.
236
12.8M
    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
173M
    SlotIndex getBaseIndex() const {
242
173M
      return SlotIndex(listEntry(), Slot_Block);
243
173M
    }
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.0M
    SlotIndex getBoundaryIndex() const {
249
25.0M
      return SlotIndex(listEntry(), Slot_Dead);
250
25.0M
    }
251
252
    /// Returns the register use/def slot in the current instruction for a
253
    /// normal or early-clobber def.
254
113M
    SlotIndex getRegSlot(bool EC = false) const {
255
113M
      return SlotIndex(listEntry(), EC ? 
Slot_EarlyClobber4.40M
:
Slot_Register109M
);
256
113M
    }
257
258
    /// Returns the dead def kill slot for the current instruction.
259
73.8M
    SlotIndex getDeadSlot() const {
260
73.8M
      return SlotIndex(listEntry(), Slot_Dead);
261
73.8M
    }
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
8.97M
    SlotIndex getNextSlot() const {
270
8.97M
      Slot s = getSlot();
271
8.97M
      if (s == Slot_Dead) {
272
19.8k
        return SlotIndex(&*++listEntry()->getIterator(), Slot_Block);
273
19.8k
      }
274
8.95M
      return SlotIndex(listEntry(), s + 1);
275
8.95M
    }
276
277
    /// Returns the next index. This is the index corresponding to the this
278
    /// index's slot, but for the next instruction.
279
32.9k
    SlotIndex getNextIndex() const {
280
32.9k
      return SlotIndex(&*++listEntry()->getIterator(), getSlot());
281
32.9k
    }
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
148M
    SlotIndex getPrevSlot() const {
290
148M
      Slot s = getSlot();
291
148M
      if (s == Slot_Block) {
292
71.0M
        return SlotIndex(&*--listEntry()->getIterator(), Slot_Dead);
293
71.0M
      }
294
77.0M
      return SlotIndex(listEntry(), s - 1);
295
77.0M
    }
296
297
    /// Returns the previous index. This is the index corresponding to this
298
    /// index's slot, but for the previous instruction.
299
44.5k
    SlotIndex getPrevIndex() const {
300
44.5k
      return SlotIndex(&*--listEntry()->getIterator(), getSlot());
301
44.5k
    }
302
  };
303
304
15.9k
  inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
305
15.9k
    li.print(os);
306
15.9k
    return os;
307
15.9k
  }
308
309
  using IdxMBBPair = std::pair<SlotIndex, MachineBasicBlock *>;
310
311
  /// SlotIndexes pass.
312
  ///
313
  /// This pass assigns indexes to each instruction.
314
  class SlotIndexes : public MachineFunctionPass {
315
  private:
316
    // IndexListEntry allocator.
317
    BumpPtrAllocator ileAllocator;
318
319
    using IndexList = ilist<IndexListEntry>;
320
    IndexList indexList;
321
322
    MachineFunction *mf;
323
324
    using Mi2IndexMap = DenseMap<const MachineInstr *, SlotIndex>;
325
    Mi2IndexMap mi2iMap;
326
327
    /// MBBRanges - Map MBB number to (start, stop) indexes.
328
    SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
329
330
    /// Idx2MBBMap - Sorted list of pairs of index of first instruction
331
    /// and MBB id.
332
    SmallVector<IdxMBBPair, 8> idx2MBBMap;
333
334
60.8M
    IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
335
60.8M
      IndexListEntry *entry =
336
60.8M
          static_cast<IndexListEntry *>(ileAllocator.Allocate(
337
60.8M
              sizeof(IndexListEntry), alignof(IndexListEntry)));
338
60.8M
339
60.8M
      new (entry) IndexListEntry(mi, index);
340
60.8M
341
60.8M
      return entry;
342
60.8M
    }
343
344
    /// Renumber locally after inserting curItr.
345
    void renumberIndexes(IndexList::iterator curItr);
346
347
  public:
348
    static char ID;
349
350
77.1k
    SlotIndexes() : MachineFunctionPass(ID), mf(nullptr) {
351
77.1k
      initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
352
77.1k
    }
353
354
76.7k
    ~SlotIndexes() override {
355
76.7k
      // The indexList's nodes are all allocated in the BumpPtrAllocator.
356
76.7k
      indexList.clearAndLeakNodesUnsafely();
357
76.7k
    }
358
359
    void getAnalysisUsage(AnalysisUsage &au) const override;
360
    void releaseMemory() override;
361
362
    bool runOnMachineFunction(MachineFunction &fn) override;
363
364
    /// Dump the indexes.
365
    void dump() const;
366
367
    /// Repair indexes after adding and removing instructions.
368
    void repairIndexesInRange(MachineBasicBlock *MBB,
369
                              MachineBasicBlock::iterator Begin,
370
                              MachineBasicBlock::iterator End);
371
372
    /// Returns the zero index for this analysis.
373
30.8k
    SlotIndex getZeroIndex() {
374
30.8k
      assert(indexList.front().getIndex() == 0 && "First index is not 0?");
375
30.8k
      return SlotIndex(&indexList.front(), 0);
376
30.8k
    }
377
378
    /// Returns the base index of the last slot in this analysis.
379
5.68M
    SlotIndex getLastIndex() {
380
5.68M
      return SlotIndex(&indexList.back(), 0);
381
5.68M
    }
382
383
    /// Returns true if the given machine instr is mapped to an index,
384
    /// otherwise returns false.
385
19.5M
    bool hasIndex(const MachineInstr &instr) const {
386
19.5M
      return mi2iMap.count(&instr);
387
19.5M
    }
388
389
    /// Returns the base index for the given instruction.
390
209M
    SlotIndex getInstructionIndex(const MachineInstr &MI) const {
391
209M
      // Instructions inside a bundle have the same number as the bundle itself.
392
209M
      auto BundleStart = getBundleStart(MI.getIterator());
393
209M
      auto BundleEnd = getBundleEnd(MI.getIterator());
394
209M
      // Use the first non-debug instruction in the bundle to get SlotIndex.
395
209M
      const MachineInstr &BundleNonDebug =
396
209M
          *skipDebugInstructionsForward(BundleStart, BundleEnd);
397
209M
      assert(!BundleNonDebug.isDebugInstr() &&
398
209M
             "Could not use a debug instruction to query mi2iMap.");
399
209M
      Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleNonDebug);
400
209M
      assert(itr != mi2iMap.end() && "Instruction not found in maps.");
401
209M
      return itr->second;
402
209M
    }
403
404
    /// Returns the instruction for the given index, or null if the given
405
    /// index has no instruction associated with it.
406
228M
    MachineInstr* getInstructionFromIndex(SlotIndex index) const {
407
18.4E
      return index.isValid() ? 
index.listEntry()->getInstr()228M
: nullptr;
408
228M
    }
409
410
    /// Returns the next non-null index, if one exists.
411
    /// Otherwise returns getLastIndex().
412
8.62M
    SlotIndex getNextNonNullIndex(SlotIndex Index) {
413
8.62M
      IndexList::iterator I = Index.listEntry()->getIterator();
414
8.62M
      IndexList::iterator E = indexList.end();
415
11.4M
      while (++I != E)
416
11.4M
        if (I->getInstr())
417
8.62M
          return SlotIndex(&*I, Index.getSlot());
418
8.62M
      // We reached the end of the function.
419
8.62M
      
return getLastIndex()3
;
420
8.62M
    }
421
422
    /// getIndexBefore - Returns the index of the last indexed instruction
423
    /// before MI, or the start index of its basic block.
424
    /// MI is not required to have an index.
425
1.51M
    SlotIndex getIndexBefore(const MachineInstr &MI) const {
426
1.51M
      const MachineBasicBlock *MBB = MI.getParent();
427
1.51M
      assert(MBB && "MI must be inserted in a basic block");
428
1.51M
      MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
429
1.51M
      while (true) {
430
1.51M
        if (I == B)
431
203k
          return getMBBStartIdx(MBB);
432
1.30M
        --I;
433
1.30M
        Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I);
434
1.30M
        if (MapItr != mi2iMap.end())
435
1.30M
          return MapItr->second;
436
1.30M
      }
437
1.51M
    }
438
439
    /// getIndexAfter - Returns the index of the first indexed instruction
440
    /// after MI, or the end index of its basic block.
441
    /// MI is not required to have an index.
442
262k
    SlotIndex getIndexAfter(const MachineInstr &MI) const {
443
262k
      const MachineBasicBlock *MBB = MI.getParent();
444
262k
      assert(MBB && "MI must be inserted in a basic block");
445
262k
      MachineBasicBlock::const_iterator I = MI, E = MBB->end();
446
262k
      while (true) {
447
262k
        ++I;
448
262k
        if (I == E)
449
41.6k
          return getMBBEndIdx(MBB);
450
220k
        Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I);
451
220k
        if (MapItr != mi2iMap.end())
452
220k
          return MapItr->second;
453
220k
      }
454
262k
    }
455
456
    /// Return the (start,end) range of the given basic block number.
457
    const std::pair<SlotIndex, SlotIndex> &
458
283M
    getMBBRange(unsigned Num) const {
459
283M
      return MBBRanges[Num];
460
283M
    }
461
462
    /// Return the (start,end) range of the given basic block.
463
    const std::pair<SlotIndex, SlotIndex> &
464
156M
    getMBBRange(const MachineBasicBlock *MBB) const {
465
156M
      return getMBBRange(MBB->getNumber());
466
156M
    }
467
468
    /// Returns the first index in the given basic block number.
469
32.6M
    SlotIndex getMBBStartIdx(unsigned Num) const {
470
32.6M
      return getMBBRange(Num).first;
471
32.6M
    }
472
473
    /// Returns the first index in the given basic block.
474
81.4M
    SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
475
81.4M
      return getMBBRange(mbb).first;
476
81.4M
    }
477
478
    /// Returns the last index in the given basic block number.
479
0
    SlotIndex getMBBEndIdx(unsigned Num) const {
480
0
      return getMBBRange(Num).second;
481
0
    }
482
483
    /// Returns the last index in the given basic block.
484
29.5M
    SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
485
29.5M
      return getMBBRange(mbb).second;
486
29.5M
    }
487
488
    /// Iterator over the idx2MBBMap (sorted pairs of slot index of basic block
489
    /// begin and basic block)
490
    using MBBIndexIterator = SmallVectorImpl<IdxMBBPair>::const_iterator;
491
492
    /// Move iterator to the next IdxMBBPair where the SlotIndex is greater or
493
    /// equal to \p To.
494
8.92M
    MBBIndexIterator advanceMBBIndex(MBBIndexIterator I, SlotIndex To) const {
495
8.92M
      return std::partition_point(
496
8.92M
          I, idx2MBBMap.end(),
497
41.1M
          [=](const IdxMBBPair &IM) { return IM.first < To; });
498
8.92M
    }
499
500
    /// Get an iterator pointing to the IdxMBBPair with the biggest SlotIndex
501
    /// that is greater or equal to \p Idx.
502
3.49M
    MBBIndexIterator findMBBIndex(SlotIndex Idx) const {
503
3.49M
      return advanceMBBIndex(idx2MBBMap.begin(), Idx);
504
3.49M
    }
505
506
    /// Returns an iterator for the begin of the idx2MBBMap.
507
2.05M
    MBBIndexIterator MBBIndexBegin() const {
508
2.05M
      return idx2MBBMap.begin();
509
2.05M
    }
510
511
    /// Return an iterator for the end of the idx2MBBMap.
512
25.2M
    MBBIndexIterator MBBIndexEnd() const {
513
25.2M
      return idx2MBBMap.end();
514
25.2M
    }
515
516
    /// Returns the basic block which the given index falls in.
517
145M
    MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
518
145M
      if (MachineInstr *MI = getInstructionFromIndex(index))
519
142M
        return MI->getParent();
520
3.49M
521
3.49M
      MBBIndexIterator I = findMBBIndex(index);
522
3.49M
      // Take the pair containing the index
523
3.49M
      MBBIndexIterator J =
524
3.49M
        ((I != MBBIndexEnd() && 
I->first > index3.41M
) ||
525
3.49M
         
(2.88M
I == MBBIndexEnd()2.88M
&&
!idx2MBBMap.empty()77.6k
)) ?
std::prev(I)689k
:
I2.80M
;
526
3.49M
527
3.49M
      assert(J != MBBIndexEnd() && J->first <= index &&
528
3.49M
             index < getMBBEndIdx(J->second) &&
529
3.49M
             "index does not correspond to an MBB");
530
3.49M
      return J->second;
531
3.49M
    }
532
533
    /// Insert the given machine instruction into the mapping. Returns the
534
    /// assigned index.
535
    /// If Late is set and there are null indexes between mi's neighboring
536
    /// instructions, create the new index after the null indexes instead of
537
    /// before them.
538
1.77M
    SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late = false) {
539
1.77M
      assert(!MI.isInsideBundle() &&
540
1.77M
             "Instructions inside bundles should use bundle start's slot.");
541
1.77M
      assert(mi2iMap.find(&MI) == mi2iMap.end() && "Instr already indexed.");
542
1.77M
      // Numbering debug instructions could cause code generation to be
543
1.77M
      // affected by debug information.
544
1.77M
      assert(!MI.isDebugInstr() && "Cannot number debug instructions.");
545
1.77M
546
1.77M
      assert(MI.getParent() != nullptr && "Instr must be added to function.");
547
1.77M
548
1.77M
      // Get the entries where MI should be inserted.
549
1.77M
      IndexList::iterator prevItr, nextItr;
550
1.77M
      if (Late) {
551
262k
        // Insert MI's index immediately before the following instruction.
552
262k
        nextItr = getIndexAfter(MI).listEntry()->getIterator();
553
262k
        prevItr = std::prev(nextItr);
554
1.51M
      } else {
555
1.51M
        // Insert MI's index immediately after the preceding instruction.
556
1.51M
        prevItr = getIndexBefore(MI).listEntry()->getIterator();
557
1.51M
        nextItr = std::next(prevItr);
558
1.51M
      }
559
1.77M
560
1.77M
      // Get a number for the new instr, or 0 if there's no room currently.
561
1.77M
      // In the latter case we'll force a renumber later.
562
1.77M
      unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
563
1.77M
      unsigned newNumber = prevItr->getIndex() + dist;
564
1.77M
565
1.77M
      // Insert a new list entry for MI.
566
1.77M
      IndexList::iterator newItr =
567
1.77M
          indexList.insert(nextItr, createEntry(&MI, newNumber));
568
1.77M
569
1.77M
      // Renumber locally if we need to.
570
1.77M
      if (dist == 0)
571
273k
        renumberIndexes(newItr);
572
1.77M
573
1.77M
      SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
574
1.77M
      mi2iMap.insert(std::make_pair(&MI, newIndex));
575
1.77M
      return newIndex;
576
1.77M
    }
577
578
    /// Removes machine instruction (bundle) \p MI from the mapping.
579
    /// This should be called before MachineInstr::eraseFromParent() is used to
580
    /// remove a whole bundle or an unbundled instruction.
581
    void removeMachineInstrFromMaps(MachineInstr &MI);
582
583
    /// Removes a single machine instruction \p MI from the mapping.
584
    /// This should be called before MachineInstr::eraseFromBundle() is used to
585
    /// remove a single instruction (out of a bundle).
586
    void removeSingleMachineInstrFromMaps(MachineInstr &MI);
587
588
    /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
589
    /// maps used by register allocator. \returns the index where the new
590
    /// instruction was inserted.
591
882k
    SlotIndex replaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI) {
592
882k
      Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
593
882k
      if (mi2iItr == mi2iMap.end())
594
0
        return SlotIndex();
595
882k
      SlotIndex replaceBaseIndex = mi2iItr->second;
596
882k
      IndexListEntry *miEntry(replaceBaseIndex.listEntry());
597
882k
      assert(miEntry->getInstr() == &MI &&
598
882k
             "Mismatched instruction in index tables.");
599
882k
      miEntry->setInstr(&NewMI);
600
882k
      mi2iMap.erase(mi2iItr);
601
882k
      mi2iMap.insert(std::make_pair(&NewMI, replaceBaseIndex));
602
882k
      return replaceBaseIndex;
603
882k
    }
604
605
    /// Add the given MachineBasicBlock into the maps.
606
0
    void insertMBBInMaps(MachineBasicBlock *mbb) {
607
0
      MachineFunction::iterator nextMBB =
608
0
        std::next(MachineFunction::iterator(mbb));
609
0
610
0
      IndexListEntry *startEntry = nullptr;
611
0
      IndexListEntry *endEntry = nullptr;
612
0
      IndexList::iterator newItr;
613
0
      if (nextMBB == mbb->getParent()->end()) {
614
0
        startEntry = &indexList.back();
615
0
        endEntry = createEntry(nullptr, 0);
616
0
        newItr = indexList.insertAfter(startEntry->getIterator(), endEntry);
617
0
      } else {
618
0
        startEntry = createEntry(nullptr, 0);
619
0
        endEntry = getMBBStartIdx(&*nextMBB).listEntry();
620
0
        newItr = indexList.insert(endEntry->getIterator(), startEntry);
621
0
      }
622
0
623
0
      SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
624
0
      SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
625
0
626
0
      MachineFunction::iterator prevMBB(mbb);
627
0
      assert(prevMBB != mbb->getParent()->end() &&
628
0
             "Can't insert a new block at the beginning of a function.");
629
0
      --prevMBB;
630
0
      MBBRanges[prevMBB->getNumber()].second = startIdx;
631
0
632
0
      assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
633
0
             "Blocks must be added in order");
634
0
      MBBRanges.push_back(std::make_pair(startIdx, endIdx));
635
0
      idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
636
0
637
0
      renumberIndexes(newItr);
638
0
      llvm::sort(idx2MBBMap, less_first());
639
0
    }
640
  };
641
642
  // Specialize IntervalMapInfo for half-open slot index intervals.
643
  template <>
644
  struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
645
  };
646
647
} // end namespace llvm
648
649
#endif // LLVM_CODEGEN_SLOTINDEXES_H