Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/SlotIndexes.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SlotIndexes.cpp - Slot Indexes Pass  ------------------------------===//
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
#include "llvm/CodeGen/SlotIndexes.h"
10
#include "llvm/ADT/Statistic.h"
11
#include "llvm/CodeGen/MachineFunction.h"
12
#include "llvm/Config/llvm-config.h"
13
#include "llvm/Support/Debug.h"
14
#include "llvm/Support/raw_ostream.h"
15
16
using namespace llvm;
17
18
#define DEBUG_TYPE "slotindexes"
19
20
char SlotIndexes::ID = 0;
21
INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE,
22
                "Slot index numbering", false, false)
23
24
STATISTIC(NumLocalRenum,  "Number of local renumberings");
25
26
77.1k
void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
27
77.1k
  au.setPreservesAll();
28
77.1k
  MachineFunctionPass::getAnalysisUsage(au);
29
77.1k
}
30
31
1.05M
void SlotIndexes::releaseMemory() {
32
1.05M
  mi2iMap.clear();
33
1.05M
  MBBRanges.clear();
34
1.05M
  idx2MBBMap.clear();
35
1.05M
  indexList.clear();
36
1.05M
  ileAllocator.Reset();
37
1.05M
}
38
39
1.05M
bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
40
1.05M
41
1.05M
  // Compute numbering as follows:
42
1.05M
  // Grab an iterator to the start of the index list.
43
1.05M
  // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
44
1.05M
  // iterator in lock-step (though skipping it over indexes which have
45
1.05M
  // null pointers in the instruction field).
46
1.05M
  // At each iteration assert that the instruction pointed to in the index
47
1.05M
  // is the same one pointed to by the MI iterator. This
48
1.05M
49
1.05M
  // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
50
1.05M
  // only need to be set up once after the first numbering is computed.
51
1.05M
52
1.05M
  mf = &fn;
53
1.05M
54
1.05M
  // Check that the list contains only the sentinal.
55
1.05M
  assert(indexList.empty() && "Index list non-empty at initial numbering?");
56
1.05M
  assert(idx2MBBMap.empty() &&
57
1.05M
         "Index -> MBB mapping non-empty at initial numbering?");
58
1.05M
  assert(MBBRanges.empty() &&
59
1.05M
         "MBB -> Index mapping non-empty at initial numbering?");
60
1.05M
  assert(mi2iMap.empty() &&
61
1.05M
         "MachineInstr -> Index mapping non-empty at initial numbering?");
62
1.05M
63
1.05M
  unsigned index = 0;
64
1.05M
  MBBRanges.resize(mf->getNumBlockIDs());
65
1.05M
  idx2MBBMap.reserve(mf->size());
66
1.05M
67
1.05M
  indexList.push_back(createEntry(nullptr, index));
68
1.05M
69
1.05M
  // Iterate over the function.
70
5.50M
  for (MachineBasicBlock &MBB : *mf) {
71
5.50M
    // Insert an index for the MBB start.
72
5.50M
    SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
73
5.50M
74
52.5M
    for (MachineInstr &MI : MBB) {
75
52.5M
      if (MI.isDebugInstr())
76
10.3k
        continue;
77
52.5M
78
52.5M
      // Insert a store index for the instr.
79
52.5M
      indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
80
52.5M
81
52.5M
      // Save this base index in the maps.
82
52.5M
      mi2iMap.insert(std::make_pair(
83
52.5M
          &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
84
52.5M
    }
85
5.50M
86
5.50M
    // We insert one blank instructions between basic blocks.
87
5.50M
    indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
88
5.50M
89
5.50M
    MBBRanges[MBB.getNumber()].first = blockStartIndex;
90
5.50M
    MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
91
5.50M
                                                   SlotIndex::Slot_Block);
92
5.50M
    idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
93
5.50M
  }
94
1.05M
95
1.05M
  // Sort the Idx2MBBMap
96
1.05M
  llvm::sort(idx2MBBMap, less_first());
97
1.05M
98
1.05M
  LLVM_DEBUG(mf->print(dbgs(), this));
99
1.05M
100
1.05M
  // And we're done!
101
1.05M
  return false;
102
1.05M
}
103
104
5.42M
void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI) {
105
5.42M
  assert(!MI.isBundledWithPred() &&
106
5.42M
         "Use removeSingleMachineInstrFromMaps() instread");
107
5.42M
  Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
108
5.42M
  if (mi2iItr == mi2iMap.end())
109
251
    return;
110
5.42M
111
5.42M
  SlotIndex MIIndex = mi2iItr->second;
112
5.42M
  IndexListEntry &MIEntry = *MIIndex.listEntry();
113
5.42M
  assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
114
5.42M
  mi2iMap.erase(mi2iItr);
115
5.42M
  // FIXME: Eventually we want to actually delete these indexes.
116
5.42M
  MIEntry.setInstr(nullptr);
117
5.42M
}
118
119
2.65M
void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
120
2.65M
  Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
121
2.65M
  if (mi2iItr == mi2iMap.end())
122
0
    return;
123
2.65M
124
2.65M
  SlotIndex MIIndex = mi2iItr->second;
125
2.65M
  IndexListEntry &MIEntry = *MIIndex.listEntry();
126
2.65M
  assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
127
2.65M
  mi2iMap.erase(mi2iItr);
128
2.65M
129
2.65M
  // When removing the first instruction of a bundle update mapping to next
130
2.65M
  // instruction.
131
2.65M
  if (MI.isBundledWithSucc()) {
132
6
    // Only the first instruction of a bundle should have an index assigned.
133
6
    assert(!MI.isBundledWithPred() && "Should have first bundle isntruction");
134
6
135
6
    MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
136
6
    MachineInstr &NextMI = *Next;
137
6
    MIEntry.setInstr(&NextMI);
138
6
    mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
139
6
    return;
140
2.65M
  } else {
141
2.65M
    // FIXME: Eventually we want to actually delete these indexes.
142
2.65M
    MIEntry.setInstr(nullptr);
143
2.65M
  }
144
2.65M
}
145
146
// Renumber indexes locally after curItr was inserted, but failed to get a new
147
// index.
148
273k
void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
149
273k
  // Number indexes with half the default spacing so we can catch up quickly.
150
273k
  const unsigned Space = SlotIndex::InstrDist/2;
151
273k
  static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
152
273k
153
273k
  IndexList::iterator startItr = std::prev(curItr);
154
273k
  unsigned index = startItr->getIndex();
155
125M
  do {
156
125M
    curItr->setIndex(index += Space);
157
125M
    ++curItr;
158
125M
    // If the next index is bigger, we have caught up.
159
125M
  } while (curItr != indexList.end() && 
curItr->getIndex() <= index125M
);
160
273k
161
273k
  LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
162
273k
                    << '-' << index << " ***\n");
163
273k
  ++NumLocalRenum;
164
273k
}
165
166
// Repair indexes after adding and removing instructions.
167
void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
168
                                       MachineBasicBlock::iterator Begin,
169
29
                                       MachineBasicBlock::iterator End) {
170
29
  // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
171
29
  // does the same thing.
172
29
  // Find anchor points, which are at the beginning/end of blocks or at
173
29
  // instructions that already have indexes.
174
29
  while (Begin != MBB->begin() && 
!hasIndex(*Begin)18
)
175
0
    --Begin;
176
29
  while (End != MBB->end() && !hasIndex(*End))
177
0
    ++End;
178
29
179
29
  bool includeStart = (Begin == MBB->begin());
180
29
  SlotIndex startIdx;
181
29
  if (includeStart)
182
11
    startIdx = getMBBStartIdx(MBB);
183
18
  else
184
18
    startIdx = getInstructionIndex(*Begin);
185
29
186
29
  SlotIndex endIdx;
187
29
  if (End == MBB->end())
188
0
    endIdx = getMBBEndIdx(MBB);
189
29
  else
190
29
    endIdx = getInstructionIndex(*End);
191
29
192
29
  // FIXME: Conceptually, this code is implementing an iterator on MBB that
193
29
  // optionally includes an additional position prior to MBB->begin(), indicated
194
29
  // by the includeStart flag. This is done so that we can iterate MIs in a MBB
195
29
  // in parallel with SlotIndexes, but there should be a better way to do this.
196
29
  IndexList::iterator ListB = startIdx.listEntry()->getIterator();
197
29
  IndexList::iterator ListI = endIdx.listEntry()->getIterator();
198
29
  MachineBasicBlock::iterator MBBI = End;
199
29
  bool pastStart = false;
200
241
  while (ListI != ListB || 
MBBI != Begin29
||
(29
includeStart29
&&
!pastStart11
)) {
201
212
    assert(ListI->getIndex() >= startIdx.getIndex() &&
202
212
           (includeStart || !pastStart) &&
203
212
           "Decremented past the beginning of region to repair.");
204
212
205
212
    MachineInstr *SlotMI = ListI->getInstr();
206
212
    MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : 
nullptr0
;
207
212
    bool MBBIAtBegin = MBBI == Begin && 
(40
!includeStart40
||
pastStart22
);
208
212
209
212
    if (SlotMI == MI && 
!MBBIAtBegin40
) {
210
40
      --ListI;
211
40
      if (MBBI != Begin)
212
29
        --MBBI;
213
11
      else
214
11
        pastStart = true;
215
172
    } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
216
143
      if (MBBI != Begin)
217
143
        --MBBI;
218
0
      else
219
0
        pastStart = true;
220
143
    } else {
221
29
      --ListI;
222
29
      if (SlotMI)
223
29
        removeMachineInstrFromMaps(*SlotMI);
224
29
    }
225
212
  }
226
29
227
29
  // In theory this could be combined with the previous loop, but it is tricky
228
29
  // to update the IndexList while we are iterating it.
229
201
  for (MachineBasicBlock::iterator I = End; I != Begin;) {
230
172
    --I;
231
172
    MachineInstr &MI = *I;
232
172
    if (!MI.isDebugInstr() && mi2iMap.find(&MI) == mi2iMap.end())
233
143
      insertMachineInstrInMaps(MI);
234
172
  }
235
29
}
236
237
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
238
LLVM_DUMP_METHOD void SlotIndexes::dump() const {
239
  for (IndexList::const_iterator itr = indexList.begin();
240
       itr != indexList.end(); ++itr) {
241
    dbgs() << itr->getIndex() << " ";
242
243
    if (itr->getInstr()) {
244
      dbgs() << *itr->getInstr();
245
    } else {
246
      dbgs() << "\n";
247
    }
248
  }
249
250
  for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
251
    dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
252
           << MBBRanges[i].second << ")\n";
253
}
254
#endif
255
256
// Print a SlotIndex to a raw_ostream.
257
15.9k
void SlotIndex::print(raw_ostream &os) const {
258
15.9k
  if (isValid())
259
15.9k
    os << listEntry()->getIndex() << "Berd"[getSlot()];
260
0
  else
261
0
    os << "invalid";
262
15.9k
}
263
264
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
265
// Dump a SlotIndex to stderr.
266
LLVM_DUMP_METHOD void SlotIndex::dump() const {
267
  print(dbgs());
268
  dbgs() << "\n";
269
}
270
#endif
271