blob: 9fff873324d0d4493e9a956525ff51fd1d909616 [file] [log] [blame]
Lang Hames05fb9632009-11-03 23:52:08 +00001//===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Lang Hames05fb9632009-11-03 23:52:08 +00006//
7//===----------------------------------------------------------------------===//
8
Lang Hames05fb9632009-11-03 23:52:08 +00009#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesenb88f6ad2011-03-04 18:08:29 +000010#include "llvm/ADT/Statistic.h"
Lang Hames05fb9632009-11-03 23:52:08 +000011#include "llvm/CodeGen/MachineFunction.h"
Nico Weber432a3882018-04-30 14:59:11 +000012#include "llvm/Config/llvm-config.h"
Lang Hames05fb9632009-11-03 23:52:08 +000013#include "llvm/Support/Debug.h"
14#include "llvm/Support/raw_ostream.h"
15
16using namespace llvm;
17
Chandler Carruth1b9dde02014-04-22 02:02:50 +000018#define DEBUG_TYPE "slotindexes"
19
Lang Hames05fb9632009-11-03 23:52:08 +000020char SlotIndexes::ID = 0;
Matthias Braun1527baa2017-05-25 21:26:32 +000021INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE,
Owen Andersondf7a4f22010-10-07 22:25:06 +000022 "Slot index numbering", false, false)
Lang Hames05fb9632009-11-03 23:52:08 +000023
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +000024STATISTIC(NumLocalRenum, "Number of local renumberings");
Jakob Stoklund Olesenb88f6ad2011-03-04 18:08:29 +000025
Lang Hames05fb9632009-11-03 23:52:08 +000026void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
27 au.setPreservesAll();
28 MachineFunctionPass::getAnalysisUsage(au);
29}
30
31void SlotIndexes::releaseMemory() {
32 mi2iMap.clear();
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000033 MBBRanges.clear();
Lang Hames05fb9632009-11-03 23:52:08 +000034 idx2MBBMap.clear();
Lang Hamesaef91782012-04-17 04:15:51 +000035 indexList.clear();
36 ileAllocator.Reset();
Lang Hames05fb9632009-11-03 23:52:08 +000037}
38
39bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
40
41 // Compute numbering as follows:
42 // Grab an iterator to the start of the index list.
43 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
44 // iterator in lock-step (though skipping it over indexes which have
45 // null pointers in the instruction field).
46 // At each iteration assert that the instruction pointed to in the index
Lang Hamesaef91782012-04-17 04:15:51 +000047 // is the same one pointed to by the MI iterator. This
Lang Hames05fb9632009-11-03 23:52:08 +000048
49 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
50 // only need to be set up once after the first numbering is computed.
51
52 mf = &fn;
Lang Hames05fb9632009-11-03 23:52:08 +000053
Lang Hames05fb9632009-11-03 23:52:08 +000054 // Check that the list contains only the sentinal.
Lang Hamesaef91782012-04-17 04:15:51 +000055 assert(indexList.empty() && "Index list non-empty at initial numbering?");
Lang Hames05fb9632009-11-03 23:52:08 +000056 assert(idx2MBBMap.empty() &&
57 "Index -> MBB mapping non-empty at initial numbering?");
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000058 assert(MBBRanges.empty() &&
Lang Hames05fb9632009-11-03 23:52:08 +000059 "MBB -> Index mapping non-empty at initial numbering?");
60 assert(mi2iMap.empty() &&
61 "MachineInstr -> Index mapping non-empty at initial numbering?");
62
Lang Hames05fb9632009-11-03 23:52:08 +000063 unsigned index = 0;
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000064 MBBRanges.resize(mf->getNumBlockIDs());
65 idx2MBBMap.reserve(mf->size());
Lang Hames05fb9632009-11-03 23:52:08 +000066
Craig Topperc0196b12014-04-14 00:51:57 +000067 indexList.push_back(createEntry(nullptr, index));
Lang Hames4c052262009-12-22 00:11:50 +000068
Dan Gohman4a618822010-02-10 16:03:48 +000069 // Iterate over the function.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000070 for (MachineBasicBlock &MBB : *mf) {
Lang Hames05fb9632009-11-03 23:52:08 +000071 // Insert an index for the MBB start.
Lang Hamesaef91782012-04-17 04:15:51 +000072 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames05fb9632009-11-03 23:52:08 +000073
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000074 for (MachineInstr &MI : MBB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +000075 if (MI.isDebugInstr())
Dale Johannesen70487972010-01-22 22:38:21 +000076 continue;
Lang Hames05fb9632009-11-03 23:52:08 +000077
Lang Hames05fb9632009-11-03 23:52:08 +000078 // Insert a store index for the instr.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000079 indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
Lang Hames05fb9632009-11-03 23:52:08 +000080
81 // Save this base index in the maps.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000082 mi2iMap.insert(std::make_pair(
83 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
Lang Hames05fb9632009-11-03 23:52:08 +000084 }
85
Jakob Stoklund Olesen348d8e82011-03-04 18:51:09 +000086 // We insert one blank instructions between basic blocks.
Craig Topperc0196b12014-04-14 00:51:57 +000087 indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
Lang Hames4c052262009-12-22 00:11:50 +000088
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000089 MBBRanges[MBB.getNumber()].first = blockStartIndex;
90 MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +000091 SlotIndex::Slot_Block);
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000092 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
Lang Hames05fb9632009-11-03 23:52:08 +000093 }
94
Lang Hames05fb9632009-11-03 23:52:08 +000095 // Sort the Idx2MBBMap
Fangrui Song6620e3b2019-06-23 13:16:03 +000096 llvm::sort(idx2MBBMap, less_first());
Lang Hames05fb9632009-11-03 23:52:08 +000097
Nicola Zaghend34e60c2018-05-14 12:53:11 +000098 LLVM_DEBUG(mf->print(dbgs(), this));
Lang Hames05fb9632009-11-03 23:52:08 +000099
100 // And we're done!
101 return false;
102}
103
Matthias Braunfa289ec2017-03-17 00:41:33 +0000104void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI) {
105 assert(!MI.isBundledWithPred() &&
106 "Use removeSingleMachineInstrFromMaps() instread");
107 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
108 if (mi2iItr == mi2iMap.end())
109 return;
110
111 SlotIndex MIIndex = mi2iItr->second;
112 IndexListEntry &MIEntry = *MIIndex.listEntry();
113 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
114 mi2iMap.erase(mi2iItr);
115 // FIXME: Eventually we want to actually delete these indexes.
116 MIEntry.setInstr(nullptr);
117}
118
119void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
120 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
121 if (mi2iItr == mi2iMap.end())
122 return;
123
124 SlotIndex MIIndex = mi2iItr->second;
125 IndexListEntry &MIEntry = *MIIndex.listEntry();
126 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
127 mi2iMap.erase(mi2iItr);
128
129 // When removing the first instruction of a bundle update mapping to next
130 // instruction.
131 if (MI.isBundledWithSucc()) {
132 // Only the first instruction of a bundle should have an index assigned.
133 assert(!MI.isBundledWithPred() && "Should have first bundle isntruction");
134
135 MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
136 MachineInstr &NextMI = *Next;
137 MIEntry.setInstr(&NextMI);
138 mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
139 return;
140 } else {
141 // FIXME: Eventually we want to actually delete these indexes.
142 MIEntry.setInstr(nullptr);
143 }
144}
145
Lang Hamesaef91782012-04-17 04:15:51 +0000146// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000147// index.
Lang Hamesaef91782012-04-17 04:15:51 +0000148void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000149 // Number indexes with half the default spacing so we can catch up quickly.
150 const unsigned Space = SlotIndex::InstrDist/2;
Gabor Horvathfee04342015-03-16 09:53:42 +0000151 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000152
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000153 IndexList::iterator startItr = std::prev(curItr);
Lang Hamesaef91782012-04-17 04:15:51 +0000154 unsigned index = startItr->getIndex();
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000155 do {
Lang Hamesaef91782012-04-17 04:15:51 +0000156 curItr->setIndex(index += Space);
157 ++curItr;
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000158 // If the next index is bigger, we have caught up.
Lang Hamesaef91782012-04-17 04:15:51 +0000159 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000160
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000161 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
162 << '-' << index << " ***\n");
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000163 ++NumLocalRenum;
164}
165
Cameron Zwarich29414822013-02-20 06:46:41 +0000166// Repair indexes after adding and removing instructions.
167void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
168 MachineBasicBlock::iterator Begin,
169 MachineBasicBlock::iterator End) {
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000170 // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
171 // does the same thing.
172 // Find anchor points, which are at the beginning/end of blocks or at
173 // instructions that already have indexes.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000174 while (Begin != MBB->begin() && !hasIndex(*Begin))
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000175 --Begin;
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000176 while (End != MBB->end() && !hasIndex(*End))
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000177 ++End;
178
Cameron Zwarich29414822013-02-20 06:46:41 +0000179 bool includeStart = (Begin == MBB->begin());
180 SlotIndex startIdx;
181 if (includeStart)
182 startIdx = getMBBStartIdx(MBB);
183 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000184 startIdx = getInstructionIndex(*Begin);
Cameron Zwarich29414822013-02-20 06:46:41 +0000185
186 SlotIndex endIdx;
187 if (End == MBB->end())
188 endIdx = getMBBEndIdx(MBB);
189 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000190 endIdx = getInstructionIndex(*End);
Cameron Zwarich29414822013-02-20 06:46:41 +0000191
192 // FIXME: Conceptually, this code is implementing an iterator on MBB that
193 // optionally includes an additional position prior to MBB->begin(), indicated
194 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
195 // in parallel with SlotIndexes, but there should be a better way to do this.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000196 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
197 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
Cameron Zwarich29414822013-02-20 06:46:41 +0000198 MachineBasicBlock::iterator MBBI = End;
199 bool pastStart = false;
200 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
201 assert(ListI->getIndex() >= startIdx.getIndex() &&
202 (includeStart || !pastStart) &&
203 "Decremented past the beginning of region to repair.");
204
205 MachineInstr *SlotMI = ListI->getInstr();
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000206 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
Cameron Zwarich29414822013-02-20 06:46:41 +0000207 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
208
209 if (SlotMI == MI && !MBBIAtBegin) {
210 --ListI;
211 if (MBBI != Begin)
212 --MBBI;
213 else
214 pastStart = true;
215 } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
216 if (MBBI != Begin)
217 --MBBI;
218 else
219 pastStart = true;
220 } else {
221 --ListI;
222 if (SlotMI)
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000223 removeMachineInstrFromMaps(*SlotMI);
Cameron Zwarich29414822013-02-20 06:46:41 +0000224 }
225 }
226
227 // In theory this could be combined with the previous loop, but it is tricky
228 // to update the IndexList while we are iterating it.
229 for (MachineBasicBlock::iterator I = End; I != Begin;) {
230 --I;
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000231 MachineInstr &MI = *I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000232 if (!MI.isDebugInstr() && mi2iMap.find(&MI) == mi2iMap.end())
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000233 insertMachineInstrInMaps(MI);
Cameron Zwarich29414822013-02-20 06:46:41 +0000234 }
235}
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000236
Aaron Ballman615eb472017-10-15 14:32:27 +0000237#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000238LLVM_DUMP_METHOD void SlotIndexes::dump() const {
Lang Hamesaef91782012-04-17 04:15:51 +0000239 for (IndexList::const_iterator itr = indexList.begin();
240 itr != indexList.end(); ++itr) {
David Greene714520f2010-01-05 01:25:50 +0000241 dbgs() << itr->getIndex() << " ";
Lang Hames05fb9632009-11-03 23:52:08 +0000242
Craig Topperc0196b12014-04-14 00:51:57 +0000243 if (itr->getInstr()) {
David Greene714520f2010-01-05 01:25:50 +0000244 dbgs() << *itr->getInstr();
Lang Hames05fb9632009-11-03 23:52:08 +0000245 } else {
David Greene714520f2010-01-05 01:25:50 +0000246 dbgs() << "\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000247 }
248 }
249
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +0000250 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000251 dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +0000252 << MBBRanges[i].second << ")\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000253}
Manman Ren742534c2012-09-06 19:06:06 +0000254#endif
Lang Hames05fb9632009-11-03 23:52:08 +0000255
256// Print a SlotIndex to a raw_ostream.
257void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000258 if (isValid())
Lang Hamesaef91782012-04-17 04:15:51 +0000259 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000260 else
261 os << "invalid";
Lang Hames05fb9632009-11-03 23:52:08 +0000262}
263
Aaron Ballman615eb472017-10-15 14:32:27 +0000264#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Lang Hames05fb9632009-11-03 23:52:08 +0000265// Dump a SlotIndex to stderr.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000266LLVM_DUMP_METHOD void SlotIndex::dump() const {
David Greene714520f2010-01-05 01:25:50 +0000267 print(dbgs());
268 dbgs() << "\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000269}
Manman Ren742534c2012-09-06 19:06:06 +0000270#endif
Lang Hames05fb9632009-11-03 23:52:08 +0000271