Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / SlotIndexes.cpp
bloba55e74c4c063cc944a495f6a2ee36db957a6840d
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 //===----------------------------------------------------------------------===//
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"
16 using namespace llvm;
18 #define DEBUG_TYPE "slotindexes"
20 char SlotIndexes::ID = 0;
21 INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE,
22 "Slot index numbering", false, false)
24 STATISTIC(NumLocalRenum, "Number of local renumberings");
25 STATISTIC(NumGlobalRenum, "Number of global renumberings");
27 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
28 au.setPreservesAll();
29 MachineFunctionPass::getAnalysisUsage(au);
32 void SlotIndexes::releaseMemory() {
33 mi2iMap.clear();
34 MBBRanges.clear();
35 idx2MBBMap.clear();
36 indexList.clear();
37 ileAllocator.Reset();
40 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
42 // Compute numbering as follows:
43 // Grab an iterator to the start of the index list.
44 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
45 // iterator in lock-step (though skipping it over indexes which have
46 // null pointers in the instruction field).
47 // At each iteration assert that the instruction pointed to in the index
48 // is the same one pointed to by the MI iterator. This
50 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
51 // only need to be set up once after the first numbering is computed.
53 mf = &fn;
55 // Check that the list contains only the sentinal.
56 assert(indexList.empty() && "Index list non-empty at initial numbering?");
57 assert(idx2MBBMap.empty() &&
58 "Index -> MBB mapping non-empty at initial numbering?");
59 assert(MBBRanges.empty() &&
60 "MBB -> Index mapping non-empty at initial numbering?");
61 assert(mi2iMap.empty() &&
62 "MachineInstr -> Index mapping non-empty at initial numbering?");
64 unsigned index = 0;
65 MBBRanges.resize(mf->getNumBlockIDs());
66 idx2MBBMap.reserve(mf->size());
68 indexList.push_back(createEntry(nullptr, index));
70 // Iterate over the function.
71 for (MachineBasicBlock &MBB : *mf) {
72 // Insert an index for the MBB start.
73 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
75 for (MachineInstr &MI : MBB) {
76 if (MI.isDebugInstr())
77 continue;
79 // Insert a store index for the instr.
80 indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
82 // Save this base index in the maps.
83 mi2iMap.insert(std::make_pair(
84 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
87 // We insert one blank instructions between basic blocks.
88 indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
90 MBBRanges[MBB.getNumber()].first = blockStartIndex;
91 MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
92 SlotIndex::Slot_Block);
93 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
96 // Sort the Idx2MBBMap
97 llvm::sort(idx2MBBMap, Idx2MBBCompare());
99 LLVM_DEBUG(mf->print(dbgs(), this));
101 // And we're done!
102 return false;
105 void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI) {
106 assert(!MI.isBundledWithPred() &&
107 "Use removeSingleMachineInstrFromMaps() instread");
108 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
109 if (mi2iItr == mi2iMap.end())
110 return;
112 SlotIndex MIIndex = mi2iItr->second;
113 IndexListEntry &MIEntry = *MIIndex.listEntry();
114 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
115 mi2iMap.erase(mi2iItr);
116 // FIXME: Eventually we want to actually delete these indexes.
117 MIEntry.setInstr(nullptr);
120 void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
121 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
122 if (mi2iItr == mi2iMap.end())
123 return;
125 SlotIndex MIIndex = mi2iItr->second;
126 IndexListEntry &MIEntry = *MIIndex.listEntry();
127 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
128 mi2iMap.erase(mi2iItr);
130 // When removing the first instruction of a bundle update mapping to next
131 // instruction.
132 if (MI.isBundledWithSucc()) {
133 // Only the first instruction of a bundle should have an index assigned.
134 assert(!MI.isBundledWithPred() && "Should have first bundle isntruction");
136 MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
137 MachineInstr &NextMI = *Next;
138 MIEntry.setInstr(&NextMI);
139 mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
140 return;
141 } else {
142 // FIXME: Eventually we want to actually delete these indexes.
143 MIEntry.setInstr(nullptr);
147 void SlotIndexes::renumberIndexes() {
148 // Renumber updates the index of every element of the index list.
149 LLVM_DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
150 ++NumGlobalRenum;
152 unsigned index = 0;
154 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
155 I != E; ++I) {
156 I->setIndex(index);
157 index += SlotIndex::InstrDist;
161 // Renumber indexes locally after curItr was inserted, but failed to get a new
162 // index.
163 void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
164 // Number indexes with half the default spacing so we can catch up quickly.
165 const unsigned Space = SlotIndex::InstrDist/2;
166 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
168 IndexList::iterator startItr = std::prev(curItr);
169 unsigned index = startItr->getIndex();
170 do {
171 curItr->setIndex(index += Space);
172 ++curItr;
173 // If the next index is bigger, we have caught up.
174 } while (curItr != indexList.end() && curItr->getIndex() <= index);
176 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
177 << '-' << index << " ***\n");
178 ++NumLocalRenum;
181 // Repair indexes after adding and removing instructions.
182 void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
183 MachineBasicBlock::iterator Begin,
184 MachineBasicBlock::iterator End) {
185 // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
186 // does the same thing.
187 // Find anchor points, which are at the beginning/end of blocks or at
188 // instructions that already have indexes.
189 while (Begin != MBB->begin() && !hasIndex(*Begin))
190 --Begin;
191 while (End != MBB->end() && !hasIndex(*End))
192 ++End;
194 bool includeStart = (Begin == MBB->begin());
195 SlotIndex startIdx;
196 if (includeStart)
197 startIdx = getMBBStartIdx(MBB);
198 else
199 startIdx = getInstructionIndex(*Begin);
201 SlotIndex endIdx;
202 if (End == MBB->end())
203 endIdx = getMBBEndIdx(MBB);
204 else
205 endIdx = getInstructionIndex(*End);
207 // FIXME: Conceptually, this code is implementing an iterator on MBB that
208 // optionally includes an additional position prior to MBB->begin(), indicated
209 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
210 // in parallel with SlotIndexes, but there should be a better way to do this.
211 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
212 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
213 MachineBasicBlock::iterator MBBI = End;
214 bool pastStart = false;
215 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
216 assert(ListI->getIndex() >= startIdx.getIndex() &&
217 (includeStart || !pastStart) &&
218 "Decremented past the beginning of region to repair.");
220 MachineInstr *SlotMI = ListI->getInstr();
221 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
222 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
224 if (SlotMI == MI && !MBBIAtBegin) {
225 --ListI;
226 if (MBBI != Begin)
227 --MBBI;
228 else
229 pastStart = true;
230 } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
231 if (MBBI != Begin)
232 --MBBI;
233 else
234 pastStart = true;
235 } else {
236 --ListI;
237 if (SlotMI)
238 removeMachineInstrFromMaps(*SlotMI);
242 // In theory this could be combined with the previous loop, but it is tricky
243 // to update the IndexList while we are iterating it.
244 for (MachineBasicBlock::iterator I = End; I != Begin;) {
245 --I;
246 MachineInstr &MI = *I;
247 if (!MI.isDebugInstr() && mi2iMap.find(&MI) == mi2iMap.end())
248 insertMachineInstrInMaps(MI);
252 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
253 LLVM_DUMP_METHOD void SlotIndexes::dump() const {
254 for (IndexList::const_iterator itr = indexList.begin();
255 itr != indexList.end(); ++itr) {
256 dbgs() << itr->getIndex() << " ";
258 if (itr->getInstr()) {
259 dbgs() << *itr->getInstr();
260 } else {
261 dbgs() << "\n";
265 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
266 dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
267 << MBBRanges[i].second << ")\n";
269 #endif
271 // Print a SlotIndex to a raw_ostream.
272 void SlotIndex::print(raw_ostream &os) const {
273 if (isValid())
274 os << listEntry()->getIndex() << "Berd"[getSlot()];
275 else
276 os << "invalid";
279 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
280 // Dump a SlotIndex to stderr.
281 LLVM_DUMP_METHOD void SlotIndex::dump() const {
282 print(dbgs());
283 dbgs() << "\n";
285 #endif