blob: fccbb8ec91cb9494669e4638770f3845ea99d033 [file] [log] [blame]
Lang Hames233a60e2009-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 Hames233a60e2009-11-03 23:52:08 +000010#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesen10c5f2d2011-03-04 18:08:29 +000011#include "llvm/ADT/Statistic.h"
Lang Hames233a60e2009-11-03 23:52:08 +000012#include "llvm/CodeGen/MachineFunction.h"
Nico Weber0f38c602018-04-30 14:59:11 +000013#include "llvm/Config/llvm-config.h"
Lang Hames233a60e2009-11-03 23:52:08 +000014#include "llvm/Support/Debug.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace llvm;
18
Chandler Carruth8677f2f2014-04-22 02:02:50 +000019#define DEBUG_TYPE "slotindexes"
20
Lang Hames233a60e2009-11-03 23:52:08 +000021char SlotIndexes::ID = 0;
Matthias Braun94c49042017-05-25 21:26:32 +000022INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE,
Owen Andersonce665bd2010-10-07 22:25:06 +000023 "Slot index numbering", false, false)
Lang Hames233a60e2009-11-03 23:52:08 +000024
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +000025STATISTIC(NumLocalRenum, "Number of local renumberings");
26STATISTIC(NumGlobalRenum, "Number of global renumberings");
Jakob Stoklund Olesen10c5f2d2011-03-04 18:08:29 +000027
Lang Hames233a60e2009-11-03 23:52:08 +000028void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
29 au.setPreservesAll();
30 MachineFunctionPass::getAnalysisUsage(au);
31}
32
33void SlotIndexes::releaseMemory() {
34 mi2iMap.clear();
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000035 MBBRanges.clear();
Lang Hames233a60e2009-11-03 23:52:08 +000036 idx2MBBMap.clear();
Lang Hames613dfb22012-04-17 04:15:51 +000037 indexList.clear();
38 ileAllocator.Reset();
Lang Hames233a60e2009-11-03 23:52:08 +000039}
40
41bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
42
43 // Compute numbering as follows:
44 // Grab an iterator to the start of the index list.
45 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
46 // iterator in lock-step (though skipping it over indexes which have
47 // null pointers in the instruction field).
48 // At each iteration assert that the instruction pointed to in the index
Lang Hames613dfb22012-04-17 04:15:51 +000049 // is the same one pointed to by the MI iterator. This
Lang Hames233a60e2009-11-03 23:52:08 +000050
51 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
52 // only need to be set up once after the first numbering is computed.
53
54 mf = &fn;
Lang Hames233a60e2009-11-03 23:52:08 +000055
Lang Hames233a60e2009-11-03 23:52:08 +000056 // Check that the list contains only the sentinal.
Lang Hames613dfb22012-04-17 04:15:51 +000057 assert(indexList.empty() && "Index list non-empty at initial numbering?");
Lang Hames233a60e2009-11-03 23:52:08 +000058 assert(idx2MBBMap.empty() &&
59 "Index -> MBB mapping non-empty at initial numbering?");
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000060 assert(MBBRanges.empty() &&
Lang Hames233a60e2009-11-03 23:52:08 +000061 "MBB -> Index mapping non-empty at initial numbering?");
62 assert(mi2iMap.empty() &&
63 "MachineInstr -> Index mapping non-empty at initial numbering?");
64
Lang Hames233a60e2009-11-03 23:52:08 +000065 unsigned index = 0;
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000066 MBBRanges.resize(mf->getNumBlockIDs());
67 idx2MBBMap.reserve(mf->size());
Lang Hames233a60e2009-11-03 23:52:08 +000068
Craig Topper4ba84432014-04-14 00:51:57 +000069 indexList.push_back(createEntry(nullptr, index));
Lang Hames74ab5ee2009-12-22 00:11:50 +000070
Dan Gohmanf451cb82010-02-10 16:03:48 +000071 // Iterate over the function.
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +000072 for (MachineBasicBlock &MBB : *mf) {
Lang Hames233a60e2009-11-03 23:52:08 +000073 // Insert an index for the MBB start.
Lang Hames613dfb22012-04-17 04:15:51 +000074 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +000075
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +000076 for (MachineInstr &MI : MBB) {
Shiva Chen24abe712018-05-09 02:42:00 +000077 if (MI.isDebugInstr())
Dale Johannesen1caedd02010-01-22 22:38:21 +000078 continue;
Lang Hames233a60e2009-11-03 23:52:08 +000079
Lang Hames233a60e2009-11-03 23:52:08 +000080 // Insert a store index for the instr.
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +000081 indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
Lang Hames233a60e2009-11-03 23:52:08 +000082
83 // Save this base index in the maps.
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +000084 mi2iMap.insert(std::make_pair(
85 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
Lang Hames233a60e2009-11-03 23:52:08 +000086 }
87
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +000088 // We insert one blank instructions between basic blocks.
Craig Topper4ba84432014-04-14 00:51:57 +000089 indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
Lang Hames74ab5ee2009-12-22 00:11:50 +000090
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +000091 MBBRanges[MBB.getNumber()].first = blockStartIndex;
92 MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +000093 SlotIndex::Slot_Block);
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +000094 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
Lang Hames233a60e2009-11-03 23:52:08 +000095 }
96
Lang Hames233a60e2009-11-03 23:52:08 +000097 // Sort the Idx2MBBMap
Fangrui Song3b35e172018-09-27 02:13:45 +000098 llvm::sort(idx2MBBMap, Idx2MBBCompare());
Lang Hames233a60e2009-11-03 23:52:08 +000099
Nicola Zaghen0818e782018-05-14 12:53:11 +0000100 LLVM_DEBUG(mf->print(dbgs(), this));
Lang Hames233a60e2009-11-03 23:52:08 +0000101
102 // And we're done!
103 return false;
104}
105
Matthias Braun852989f2017-03-17 00:41:33 +0000106void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI) {
107 assert(!MI.isBundledWithPred() &&
108 "Use removeSingleMachineInstrFromMaps() instread");
109 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
110 if (mi2iItr == mi2iMap.end())
111 return;
112
113 SlotIndex MIIndex = mi2iItr->second;
114 IndexListEntry &MIEntry = *MIIndex.listEntry();
115 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
116 mi2iMap.erase(mi2iItr);
117 // FIXME: Eventually we want to actually delete these indexes.
118 MIEntry.setInstr(nullptr);
119}
120
121void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
122 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
123 if (mi2iItr == mi2iMap.end())
124 return;
125
126 SlotIndex MIIndex = mi2iItr->second;
127 IndexListEntry &MIEntry = *MIIndex.listEntry();
128 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
129 mi2iMap.erase(mi2iItr);
130
131 // When removing the first instruction of a bundle update mapping to next
132 // instruction.
133 if (MI.isBundledWithSucc()) {
134 // Only the first instruction of a bundle should have an index assigned.
135 assert(!MI.isBundledWithPred() && "Should have first bundle isntruction");
136
137 MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
138 MachineInstr &NextMI = *Next;
139 MIEntry.setInstr(&NextMI);
140 mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
141 return;
142 } else {
143 // FIXME: Eventually we want to actually delete these indexes.
144 MIEntry.setInstr(nullptr);
145 }
146}
147
Lang Hamesb3661582009-11-14 00:02:51 +0000148void SlotIndexes::renumberIndexes() {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000149 // Renumber updates the index of every element of the index list.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000150 LLVM_DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000151 ++NumGlobalRenum;
Lang Hames233a60e2009-11-03 23:52:08 +0000152
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000153 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000154
Lang Hames613dfb22012-04-17 04:15:51 +0000155 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
156 I != E; ++I) {
157 I->setIndex(index);
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +0000158 index += SlotIndex::InstrDist;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000159 }
Lang Hames233a60e2009-11-03 23:52:08 +0000160}
161
Lang Hames613dfb22012-04-17 04:15:51 +0000162// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000163// index.
Lang Hames613dfb22012-04-17 04:15:51 +0000164void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000165 // Number indexes with half the default spacing so we can catch up quickly.
166 const unsigned Space = SlotIndex::InstrDist/2;
Gabor Horvath1fc0a8d2015-03-16 09:53:42 +0000167 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000168
Benjamin Kramerd628f192014-03-02 12:27:27 +0000169 IndexList::iterator startItr = std::prev(curItr);
Lang Hames613dfb22012-04-17 04:15:51 +0000170 unsigned index = startItr->getIndex();
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000171 do {
Lang Hames613dfb22012-04-17 04:15:51 +0000172 curItr->setIndex(index += Space);
173 ++curItr;
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000174 // If the next index is bigger, we have caught up.
Lang Hames613dfb22012-04-17 04:15:51 +0000175 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000176
Nicola Zaghen0818e782018-05-14 12:53:11 +0000177 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
178 << '-' << index << " ***\n");
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000179 ++NumLocalRenum;
180}
181
Cameron Zwarich349cf342013-02-20 06:46:41 +0000182// Repair indexes after adding and removing instructions.
183void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
184 MachineBasicBlock::iterator Begin,
185 MachineBasicBlock::iterator End) {
Cameron Zwarichc5b61352013-02-20 22:10:00 +0000186 // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
187 // does the same thing.
188 // Find anchor points, which are at the beginning/end of blocks or at
189 // instructions that already have indexes.
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000190 while (Begin != MBB->begin() && !hasIndex(*Begin))
Cameron Zwarichc5b61352013-02-20 22:10:00 +0000191 --Begin;
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000192 while (End != MBB->end() && !hasIndex(*End))
Cameron Zwarichc5b61352013-02-20 22:10:00 +0000193 ++End;
194
Cameron Zwarich349cf342013-02-20 06:46:41 +0000195 bool includeStart = (Begin == MBB->begin());
196 SlotIndex startIdx;
197 if (includeStart)
198 startIdx = getMBBStartIdx(MBB);
199 else
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000200 startIdx = getInstructionIndex(*Begin);
Cameron Zwarich349cf342013-02-20 06:46:41 +0000201
202 SlotIndex endIdx;
203 if (End == MBB->end())
204 endIdx = getMBBEndIdx(MBB);
205 else
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000206 endIdx = getInstructionIndex(*End);
Cameron Zwarich349cf342013-02-20 06:46:41 +0000207
208 // FIXME: Conceptually, this code is implementing an iterator on MBB that
209 // optionally includes an additional position prior to MBB->begin(), indicated
210 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
211 // in parallel with SlotIndexes, but there should be a better way to do this.
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +0000212 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
213 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
Cameron Zwarich349cf342013-02-20 06:46:41 +0000214 MachineBasicBlock::iterator MBBI = End;
215 bool pastStart = false;
216 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
217 assert(ListI->getIndex() >= startIdx.getIndex() &&
218 (includeStart || !pastStart) &&
219 "Decremented past the beginning of region to repair.");
220
221 MachineInstr *SlotMI = ListI->getInstr();
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +0000222 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
Cameron Zwarich349cf342013-02-20 06:46:41 +0000223 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
224
225 if (SlotMI == MI && !MBBIAtBegin) {
226 --ListI;
227 if (MBBI != Begin)
228 --MBBI;
229 else
230 pastStart = true;
231 } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
232 if (MBBI != Begin)
233 --MBBI;
234 else
235 pastStart = true;
236 } else {
237 --ListI;
238 if (SlotMI)
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000239 removeMachineInstrFromMaps(*SlotMI);
Cameron Zwarich349cf342013-02-20 06:46:41 +0000240 }
241 }
242
243 // In theory this could be combined with the previous loop, but it is tricky
244 // to update the IndexList while we are iterating it.
245 for (MachineBasicBlock::iterator I = End; I != Begin;) {
246 --I;
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +0000247 MachineInstr &MI = *I;
Shiva Chen24abe712018-05-09 02:42:00 +0000248 if (!MI.isDebugInstr() && mi2iMap.find(&MI) == mi2iMap.end())
Duncan P. N. Exon Smitheffa4cc2016-07-01 15:08:52 +0000249 insertMachineInstrInMaps(MI);
Cameron Zwarich349cf342013-02-20 06:46:41 +0000250 }
251}
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000252
Aaron Ballman1d03d382017-10-15 14:32:27 +0000253#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000254LLVM_DUMP_METHOD void SlotIndexes::dump() const {
Lang Hames613dfb22012-04-17 04:15:51 +0000255 for (IndexList::const_iterator itr = indexList.begin();
256 itr != indexList.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000257 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000258
Craig Topper4ba84432014-04-14 00:51:57 +0000259 if (itr->getInstr()) {
David Greene87b0efc2010-01-05 01:25:50 +0000260 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000261 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000262 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000263 }
264 }
265
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +0000266 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +0000267 dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +0000268 << MBBRanges[i].second << ")\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000269}
Manman Ren77e300e2012-09-06 19:06:06 +0000270#endif
Lang Hames233a60e2009-11-03 23:52:08 +0000271
272// Print a SlotIndex to a raw_ostream.
273void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000274 if (isValid())
Lang Hames613dfb22012-04-17 04:15:51 +0000275 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000276 else
277 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000278}
279
Aaron Ballman1d03d382017-10-15 14:32:27 +0000280#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Lang Hames233a60e2009-11-03 23:52:08 +0000281// Dump a SlotIndex to stderr.
Yaron Keren55307982016-01-29 20:50:44 +0000282LLVM_DUMP_METHOD void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000283 print(dbgs());
284 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000285}
Manman Ren77e300e2012-09-06 19:06:06 +0000286#endif
Lang Hames233a60e2009-11-03 23:52:08 +0000287