blob: ea74c777e1e2f5a985c69a81d96458bd7c90d99c [file] [log] [blame]
Lang Hames05fb9632009-11-03 23:52:08 +00001//===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
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
Lang Hames05fb9632009-11-03 23:52:08 +000010#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesenb88f6ad2011-03-04 18:08:29 +000011#include "llvm/ADT/Statistic.h"
Lang Hames05fb9632009-11-03 23:52:08 +000012#include "llvm/CodeGen/MachineFunction.h"
13#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");
25STATISTIC(NumGlobalRenum, "Number of global renumberings");
Jakob Stoklund Olesenb88f6ad2011-03-04 18:08:29 +000026
Lang Hames05fb9632009-11-03 23:52:08 +000027void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
28 au.setPreservesAll();
29 MachineFunctionPass::getAnalysisUsage(au);
30}
31
32void SlotIndexes::releaseMemory() {
33 mi2iMap.clear();
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000034 MBBRanges.clear();
Lang Hames05fb9632009-11-03 23:52:08 +000035 idx2MBBMap.clear();
Lang Hamesaef91782012-04-17 04:15:51 +000036 indexList.clear();
37 ileAllocator.Reset();
Lang Hames05fb9632009-11-03 23:52:08 +000038}
39
40bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
41
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
Lang Hamesaef91782012-04-17 04:15:51 +000048 // is the same one pointed to by the MI iterator. This
Lang Hames05fb9632009-11-03 23:52:08 +000049
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.
52
53 mf = &fn;
Lang Hames05fb9632009-11-03 23:52:08 +000054
Lang Hames05fb9632009-11-03 23:52:08 +000055 // Check that the list contains only the sentinal.
Lang Hamesaef91782012-04-17 04:15:51 +000056 assert(indexList.empty() && "Index list non-empty at initial numbering?");
Lang Hames05fb9632009-11-03 23:52:08 +000057 assert(idx2MBBMap.empty() &&
58 "Index -> MBB mapping non-empty at initial numbering?");
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000059 assert(MBBRanges.empty() &&
Lang Hames05fb9632009-11-03 23:52:08 +000060 "MBB -> Index mapping non-empty at initial numbering?");
61 assert(mi2iMap.empty() &&
62 "MachineInstr -> Index mapping non-empty at initial numbering?");
63
Lang Hames05fb9632009-11-03 23:52:08 +000064 unsigned index = 0;
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000065 MBBRanges.resize(mf->getNumBlockIDs());
66 idx2MBBMap.reserve(mf->size());
Lang Hames05fb9632009-11-03 23:52:08 +000067
Craig Topperc0196b12014-04-14 00:51:57 +000068 indexList.push_back(createEntry(nullptr, index));
Lang Hames4c052262009-12-22 00:11:50 +000069
Dan Gohman4a618822010-02-10 16:03:48 +000070 // Iterate over the function.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000071 for (MachineBasicBlock &MBB : *mf) {
Lang Hames05fb9632009-11-03 23:52:08 +000072 // Insert an index for the MBB start.
Lang Hamesaef91782012-04-17 04:15:51 +000073 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames05fb9632009-11-03 23:52:08 +000074
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000075 for (MachineInstr &MI : MBB) {
76 if (MI.isDebugValue())
Dale Johannesen70487972010-01-22 22:38:21 +000077 continue;
Lang Hames05fb9632009-11-03 23:52:08 +000078
Lang Hames05fb9632009-11-03 23:52:08 +000079 // Insert a store index for the instr.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000080 indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
Lang Hames05fb9632009-11-03 23:52:08 +000081
82 // Save this base index in the maps.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000083 mi2iMap.insert(std::make_pair(
84 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
Lang Hames05fb9632009-11-03 23:52:08 +000085 }
86
Jakob Stoklund Olesen348d8e82011-03-04 18:51:09 +000087 // We insert one blank instructions between basic blocks.
Craig Topperc0196b12014-04-14 00:51:57 +000088 indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
Lang Hames4c052262009-12-22 00:11:50 +000089
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000090 MBBRanges[MBB.getNumber()].first = blockStartIndex;
91 MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +000092 SlotIndex::Slot_Block);
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000093 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
Lang Hames05fb9632009-11-03 23:52:08 +000094 }
95
Lang Hames05fb9632009-11-03 23:52:08 +000096 // Sort the Idx2MBBMap
97 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
98
Jakob Stoklund Olesen66ef9ad2012-01-24 23:28:38 +000099 DEBUG(mf->print(dbgs(), this));
Lang Hames05fb9632009-11-03 23:52:08 +0000100
101 // And we're done!
102 return false;
103}
104
Matthias Braunfa289ec2017-03-17 00:41:33 +0000105void 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;
111
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);
118}
119
120void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
121 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
122 if (mi2iItr == mi2iMap.end())
123 return;
124
125 SlotIndex MIIndex = mi2iItr->second;
126 IndexListEntry &MIEntry = *MIIndex.listEntry();
127 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
128 mi2iMap.erase(mi2iItr);
129
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");
135
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);
144 }
145}
146
Lang Hames6b7233a2009-11-14 00:02:51 +0000147void SlotIndexes::renumberIndexes() {
Lang Hames933c5412009-11-05 22:20:57 +0000148 // Renumber updates the index of every element of the index list.
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000149 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000150 ++NumGlobalRenum;
Lang Hames05fb9632009-11-03 23:52:08 +0000151
Lang Hames933c5412009-11-05 22:20:57 +0000152 unsigned index = 0;
Lang Hames05fb9632009-11-03 23:52:08 +0000153
Lang Hamesaef91782012-04-17 04:15:51 +0000154 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
155 I != E; ++I) {
156 I->setIndex(index);
Jakob Stoklund Olesen348d8e82011-03-04 18:51:09 +0000157 index += SlotIndex::InstrDist;
Lang Hames933c5412009-11-05 22:20:57 +0000158 }
Lang Hames05fb9632009-11-03 23:52:08 +0000159}
160
Lang Hamesaef91782012-04-17 04:15:51 +0000161// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000162// index.
Lang Hamesaef91782012-04-17 04:15:51 +0000163void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000164 // Number indexes with half the default spacing so we can catch up quickly.
165 const unsigned Space = SlotIndex::InstrDist/2;
Gabor Horvathfee04342015-03-16 09:53:42 +0000166 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000167
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000168 IndexList::iterator startItr = std::prev(curItr);
Lang Hamesaef91782012-04-17 04:15:51 +0000169 unsigned index = startItr->getIndex();
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000170 do {
Lang Hamesaef91782012-04-17 04:15:51 +0000171 curItr->setIndex(index += Space);
172 ++curItr;
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000173 // If the next index is bigger, we have caught up.
Lang Hamesaef91782012-04-17 04:15:51 +0000174 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000175
Lang Hamesaef91782012-04-17 04:15:51 +0000176 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000177 << index << " ***\n");
178 ++NumLocalRenum;
179}
180
Cameron Zwarich29414822013-02-20 06:46:41 +0000181// Repair indexes after adding and removing instructions.
182void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
183 MachineBasicBlock::iterator Begin,
184 MachineBasicBlock::iterator End) {
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000185 // 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.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000189 while (Begin != MBB->begin() && !hasIndex(*Begin))
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000190 --Begin;
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000191 while (End != MBB->end() && !hasIndex(*End))
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000192 ++End;
193
Cameron Zwarich29414822013-02-20 06:46:41 +0000194 bool includeStart = (Begin == MBB->begin());
195 SlotIndex startIdx;
196 if (includeStart)
197 startIdx = getMBBStartIdx(MBB);
198 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000199 startIdx = getInstructionIndex(*Begin);
Cameron Zwarich29414822013-02-20 06:46:41 +0000200
201 SlotIndex endIdx;
202 if (End == MBB->end())
203 endIdx = getMBBEndIdx(MBB);
204 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000205 endIdx = getInstructionIndex(*End);
Cameron Zwarich29414822013-02-20 06:46:41 +0000206
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.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000211 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
212 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
Cameron Zwarich29414822013-02-20 06:46:41 +0000213 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.");
219
220 MachineInstr *SlotMI = ListI->getInstr();
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000221 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
Cameron Zwarich29414822013-02-20 06:46:41 +0000222 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
223
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)
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000238 removeMachineInstrFromMaps(*SlotMI);
Cameron Zwarich29414822013-02-20 06:46:41 +0000239 }
240 }
241
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;
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000246 MachineInstr &MI = *I;
247 if (!MI.isDebugValue() && mi2iMap.find(&MI) == mi2iMap.end())
248 insertMachineInstrInMaps(MI);
Cameron Zwarich29414822013-02-20 06:46:41 +0000249 }
250}
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000251
Aaron Ballman615eb472017-10-15 14:32:27 +0000252#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000253LLVM_DUMP_METHOD void SlotIndexes::dump() const {
Lang Hamesaef91782012-04-17 04:15:51 +0000254 for (IndexList::const_iterator itr = indexList.begin();
255 itr != indexList.end(); ++itr) {
David Greene714520f2010-01-05 01:25:50 +0000256 dbgs() << itr->getIndex() << " ";
Lang Hames05fb9632009-11-03 23:52:08 +0000257
Craig Topperc0196b12014-04-14 00:51:57 +0000258 if (itr->getInstr()) {
David Greene714520f2010-01-05 01:25:50 +0000259 dbgs() << *itr->getInstr();
Lang Hames05fb9632009-11-03 23:52:08 +0000260 } else {
David Greene714520f2010-01-05 01:25:50 +0000261 dbgs() << "\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000262 }
263 }
264
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +0000265 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000266 dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +0000267 << MBBRanges[i].second << ")\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000268}
Manman Ren742534c2012-09-06 19:06:06 +0000269#endif
Lang Hames05fb9632009-11-03 23:52:08 +0000270
271// Print a SlotIndex to a raw_ostream.
272void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000273 if (isValid())
Lang Hamesaef91782012-04-17 04:15:51 +0000274 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000275 else
276 os << "invalid";
Lang Hames05fb9632009-11-03 23:52:08 +0000277}
278
Aaron Ballman615eb472017-10-15 14:32:27 +0000279#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Lang Hames05fb9632009-11-03 23:52:08 +0000280// Dump a SlotIndex to stderr.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000281LLVM_DUMP_METHOD void SlotIndex::dump() const {
David Greene714520f2010-01-05 01:25:50 +0000282 print(dbgs());
283 dbgs() << "\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000284}
Manman Ren742534c2012-09-06 19:06:06 +0000285#endif
Lang Hames05fb9632009-11-03 23:52:08 +0000286