blob: dba103e9bfb1586af90aa3f0d470da702f06bef5 [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"
Dale Johannesen70487972010-01-22 22:38:21 +000015#include "llvm/Target/TargetInstrInfo.h"
Lang Hames05fb9632009-11-03 23:52:08 +000016
17using namespace llvm;
18
Chandler Carruth1b9dde02014-04-22 02:02:50 +000019#define DEBUG_TYPE "slotindexes"
20
Lang Hames05fb9632009-11-03 23:52:08 +000021char SlotIndexes::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000022INITIALIZE_PASS(SlotIndexes, "slotindexes",
Owen Andersondf7a4f22010-10-07 22:25:06 +000023 "Slot index numbering", false, false)
Lang Hames05fb9632009-11-03 23:52:08 +000024
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +000025STATISTIC(NumLocalRenum, "Number of local renumberings");
26STATISTIC(NumGlobalRenum, "Number of global renumberings");
Jakob Stoklund Olesenb88f6ad2011-03-04 18:08:29 +000027
Lang Hames05fb9632009-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 Olesen36171282011-04-02 06:03:31 +000035 MBBRanges.clear();
Lang Hames05fb9632009-11-03 23:52:08 +000036 idx2MBBMap.clear();
Lang Hamesaef91782012-04-17 04:15:51 +000037 indexList.clear();
38 ileAllocator.Reset();
Lang Hames05fb9632009-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 Hamesaef91782012-04-17 04:15:51 +000049 // is the same one pointed to by the MI iterator. This
Lang Hames05fb9632009-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 Hames05fb9632009-11-03 23:52:08 +000055
Lang Hames05fb9632009-11-03 23:52:08 +000056 // Check that the list contains only the sentinal.
Lang Hamesaef91782012-04-17 04:15:51 +000057 assert(indexList.empty() && "Index list non-empty at initial numbering?");
Lang Hames05fb9632009-11-03 23:52:08 +000058 assert(idx2MBBMap.empty() &&
59 "Index -> MBB mapping non-empty at initial numbering?");
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000060 assert(MBBRanges.empty() &&
Lang Hames05fb9632009-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 Hames05fb9632009-11-03 23:52:08 +000065 unsigned index = 0;
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +000066 MBBRanges.resize(mf->getNumBlockIDs());
67 idx2MBBMap.reserve(mf->size());
Lang Hames05fb9632009-11-03 23:52:08 +000068
Craig Topperc0196b12014-04-14 00:51:57 +000069 indexList.push_back(createEntry(nullptr, index));
Lang Hames4c052262009-12-22 00:11:50 +000070
Dan Gohman4a618822010-02-10 16:03:48 +000071 // Iterate over the function.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000072 for (MachineBasicBlock &MBB : *mf) {
Lang Hames05fb9632009-11-03 23:52:08 +000073 // Insert an index for the MBB start.
Lang Hamesaef91782012-04-17 04:15:51 +000074 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames05fb9632009-11-03 23:52:08 +000075
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000076 for (MachineInstr &MI : MBB) {
77 if (MI.isDebugValue())
Dale Johannesen70487972010-01-22 22:38:21 +000078 continue;
Lang Hames05fb9632009-11-03 23:52:08 +000079
Lang Hames05fb9632009-11-03 23:52:08 +000080 // Insert a store index for the instr.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000081 indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
Lang Hames05fb9632009-11-03 23:52:08 +000082
83 // Save this base index in the maps.
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000084 mi2iMap.insert(std::make_pair(
85 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
Lang Hames05fb9632009-11-03 23:52:08 +000086 }
87
Jakob Stoklund Olesen348d8e82011-03-04 18:51:09 +000088 // We insert one blank instructions between basic blocks.
Craig Topperc0196b12014-04-14 00:51:57 +000089 indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
Lang Hames4c052262009-12-22 00:11:50 +000090
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000091 MBBRanges[MBB.getNumber()].first = blockStartIndex;
92 MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +000093 SlotIndex::Slot_Block);
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +000094 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
Lang Hames05fb9632009-11-03 23:52:08 +000095 }
96
Lang Hames05fb9632009-11-03 23:52:08 +000097 // Sort the Idx2MBBMap
98 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
99
Jakob Stoklund Olesen66ef9ad2012-01-24 23:28:38 +0000100 DEBUG(mf->print(dbgs(), this));
Lang Hames05fb9632009-11-03 23:52:08 +0000101
102 // And we're done!
103 return false;
104}
105
Lang Hames6b7233a2009-11-14 00:02:51 +0000106void SlotIndexes::renumberIndexes() {
Lang Hames933c5412009-11-05 22:20:57 +0000107 // Renumber updates the index of every element of the index list.
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000108 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000109 ++NumGlobalRenum;
Lang Hames05fb9632009-11-03 23:52:08 +0000110
Lang Hames933c5412009-11-05 22:20:57 +0000111 unsigned index = 0;
Lang Hames05fb9632009-11-03 23:52:08 +0000112
Lang Hamesaef91782012-04-17 04:15:51 +0000113 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
114 I != E; ++I) {
115 I->setIndex(index);
Jakob Stoklund Olesen348d8e82011-03-04 18:51:09 +0000116 index += SlotIndex::InstrDist;
Lang Hames933c5412009-11-05 22:20:57 +0000117 }
Lang Hames05fb9632009-11-03 23:52:08 +0000118}
119
Lang Hamesaef91782012-04-17 04:15:51 +0000120// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000121// index.
Lang Hamesaef91782012-04-17 04:15:51 +0000122void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000123 // Number indexes with half the default spacing so we can catch up quickly.
124 const unsigned Space = SlotIndex::InstrDist/2;
Gabor Horvathfee04342015-03-16 09:53:42 +0000125 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000126
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000127 IndexList::iterator startItr = std::prev(curItr);
Lang Hamesaef91782012-04-17 04:15:51 +0000128 unsigned index = startItr->getIndex();
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000129 do {
Lang Hamesaef91782012-04-17 04:15:51 +0000130 curItr->setIndex(index += Space);
131 ++curItr;
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000132 // If the next index is bigger, we have caught up.
Lang Hamesaef91782012-04-17 04:15:51 +0000133 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000134
Lang Hamesaef91782012-04-17 04:15:51 +0000135 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000136 << index << " ***\n");
137 ++NumLocalRenum;
138}
139
Cameron Zwarich29414822013-02-20 06:46:41 +0000140// Repair indexes after adding and removing instructions.
141void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
142 MachineBasicBlock::iterator Begin,
143 MachineBasicBlock::iterator End) {
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000144 // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
145 // does the same thing.
146 // Find anchor points, which are at the beginning/end of blocks or at
147 // instructions that already have indexes.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000148 while (Begin != MBB->begin() && !hasIndex(*Begin))
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000149 --Begin;
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000150 while (End != MBB->end() && !hasIndex(*End))
Cameron Zwarichcaad7e12013-02-20 22:10:00 +0000151 ++End;
152
Cameron Zwarich29414822013-02-20 06:46:41 +0000153 bool includeStart = (Begin == MBB->begin());
154 SlotIndex startIdx;
155 if (includeStart)
156 startIdx = getMBBStartIdx(MBB);
157 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000158 startIdx = getInstructionIndex(*Begin);
Cameron Zwarich29414822013-02-20 06:46:41 +0000159
160 SlotIndex endIdx;
161 if (End == MBB->end())
162 endIdx = getMBBEndIdx(MBB);
163 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000164 endIdx = getInstructionIndex(*End);
Cameron Zwarich29414822013-02-20 06:46:41 +0000165
166 // FIXME: Conceptually, this code is implementing an iterator on MBB that
167 // optionally includes an additional position prior to MBB->begin(), indicated
168 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
169 // in parallel with SlotIndexes, but there should be a better way to do this.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000170 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
171 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
Cameron Zwarich29414822013-02-20 06:46:41 +0000172 MachineBasicBlock::iterator MBBI = End;
173 bool pastStart = false;
174 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
175 assert(ListI->getIndex() >= startIdx.getIndex() &&
176 (includeStart || !pastStart) &&
177 "Decremented past the beginning of region to repair.");
178
179 MachineInstr *SlotMI = ListI->getInstr();
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000180 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
Cameron Zwarich29414822013-02-20 06:46:41 +0000181 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
182
183 if (SlotMI == MI && !MBBIAtBegin) {
184 --ListI;
185 if (MBBI != Begin)
186 --MBBI;
187 else
188 pastStart = true;
189 } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
190 if (MBBI != Begin)
191 --MBBI;
192 else
193 pastStart = true;
194 } else {
195 --ListI;
196 if (SlotMI)
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000197 removeMachineInstrFromMaps(*SlotMI);
Cameron Zwarich29414822013-02-20 06:46:41 +0000198 }
199 }
200
201 // In theory this could be combined with the previous loop, but it is tricky
202 // to update the IndexList while we are iterating it.
203 for (MachineBasicBlock::iterator I = End; I != Begin;) {
204 --I;
Duncan P. N. Exon Smithef105ca2016-07-01 15:08:52 +0000205 MachineInstr &MI = *I;
206 if (!MI.isDebugValue() && mi2iMap.find(&MI) == mi2iMap.end())
207 insertMachineInstrInMaps(MI);
Cameron Zwarich29414822013-02-20 06:46:41 +0000208 }
209}
Jakob Stoklund Olesenb8e6fdc2011-03-04 19:43:38 +0000210
Manman Ren19f49ac2012-09-11 22:23:19 +0000211#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000212LLVM_DUMP_METHOD void SlotIndexes::dump() const {
Lang Hamesaef91782012-04-17 04:15:51 +0000213 for (IndexList::const_iterator itr = indexList.begin();
214 itr != indexList.end(); ++itr) {
David Greene714520f2010-01-05 01:25:50 +0000215 dbgs() << itr->getIndex() << " ";
Lang Hames05fb9632009-11-03 23:52:08 +0000216
Craig Topperc0196b12014-04-14 00:51:57 +0000217 if (itr->getInstr()) {
David Greene714520f2010-01-05 01:25:50 +0000218 dbgs() << *itr->getInstr();
Lang Hames05fb9632009-11-03 23:52:08 +0000219 } else {
David Greene714520f2010-01-05 01:25:50 +0000220 dbgs() << "\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000221 }
222 }
223
Jakob Stoklund Olesen36171282011-04-02 06:03:31 +0000224 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
225 dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
226 << MBBRanges[i].second << ")\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000227}
Manman Ren742534c2012-09-06 19:06:06 +0000228#endif
Lang Hames05fb9632009-11-03 23:52:08 +0000229
230// Print a SlotIndex to a raw_ostream.
231void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000232 if (isValid())
Lang Hamesaef91782012-04-17 04:15:51 +0000233 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesendb4cf7e2011-02-03 20:29:41 +0000234 else
235 os << "invalid";
Lang Hames05fb9632009-11-03 23:52:08 +0000236}
237
Manman Ren19f49ac2012-09-11 22:23:19 +0000238#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Lang Hames05fb9632009-11-03 23:52:08 +0000239// Dump a SlotIndex to stderr.
Yaron Kereneb2a2542016-01-29 20:50:44 +0000240LLVM_DUMP_METHOD void SlotIndex::dump() const {
David Greene714520f2010-01-05 01:25:50 +0000241 print(dbgs());
242 dbgs() << "\n";
Lang Hames05fb9632009-11-03 23:52:08 +0000243}
Manman Ren742534c2012-09-06 19:06:06 +0000244#endif
Lang Hames05fb9632009-11-03 23:52:08 +0000245