blob: 20049a89d15d8e8d9493198ac60bd81512f11ce4 [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
10#define DEBUG_TYPE "slotindexes"
11
12#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesen10c5f2d2011-03-04 18:08:29 +000013#include "llvm/ADT/Statistic.h"
Lang Hames233a60e2009-11-03 23:52:08 +000014#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/raw_ostream.h"
Dale Johannesen1caedd02010-01-22 22:38:21 +000017#include "llvm/Target/TargetInstrInfo.h"
Lang Hames233a60e2009-11-03 23:52:08 +000018
19using namespace llvm;
20
Lang Hames233a60e2009-11-03 23:52:08 +000021char SlotIndexes::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000022INITIALIZE_PASS(SlotIndexes, "slotindexes",
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
Lang Hames613dfb22012-04-17 04:15:51 +000069 indexList.push_back(createEntry(0, index));
Lang Hames74ab5ee2009-12-22 00:11:50 +000070
Dan Gohmanf451cb82010-02-10 16:03:48 +000071 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +000072 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
73 mbbItr != mbbEnd; ++mbbItr) {
74 MachineBasicBlock *mbb = &*mbbItr;
75
76 // Insert an index for the MBB start.
Lang Hames613dfb22012-04-17 04:15:51 +000077 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +000078
Lang Hames233a60e2009-11-03 23:52:08 +000079 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
80 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +000081 MachineInstr *mi = miItr;
82 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +000083 continue;
Lang Hames233a60e2009-11-03 23:52:08 +000084
Lang Hames233a60e2009-11-03 23:52:08 +000085 // Insert a store index for the instr.
Lang Hames613dfb22012-04-17 04:15:51 +000086 indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
Lang Hames233a60e2009-11-03 23:52:08 +000087
88 // Save this base index in the maps.
Lang Hames613dfb22012-04-17 04:15:51 +000089 mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +000090 SlotIndex::Slot_Block)));
Lang Hames233a60e2009-11-03 23:52:08 +000091 }
92
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +000093 // We insert one blank instructions between basic blocks.
Lang Hames613dfb22012-04-17 04:15:51 +000094 indexList.push_back(createEntry(0, index += SlotIndex::InstrDist));
Lang Hames74ab5ee2009-12-22 00:11:50 +000095
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000096 MBBRanges[mbb->getNumber()].first = blockStartIndex;
Lang Hames613dfb22012-04-17 04:15:51 +000097 MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +000098 SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +000099 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
100 }
101
Lang Hames233a60e2009-11-03 23:52:08 +0000102 // Sort the Idx2MBBMap
103 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
104
Jakob Stoklund Olesen1ce6a362012-01-24 23:28:38 +0000105 DEBUG(mf->print(dbgs(), this));
Lang Hames233a60e2009-11-03 23:52:08 +0000106
107 // And we're done!
108 return false;
109}
110
Lang Hamesb3661582009-11-14 00:02:51 +0000111void SlotIndexes::renumberIndexes() {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000112 // Renumber updates the index of every element of the index list.
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000113 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000114 ++NumGlobalRenum;
Lang Hames233a60e2009-11-03 23:52:08 +0000115
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000116 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000117
Lang Hames613dfb22012-04-17 04:15:51 +0000118 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
119 I != E; ++I) {
120 I->setIndex(index);
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +0000121 index += SlotIndex::InstrDist;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000122 }
Lang Hames233a60e2009-11-03 23:52:08 +0000123}
124
Lang Hames613dfb22012-04-17 04:15:51 +0000125// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000126// index.
Lang Hames613dfb22012-04-17 04:15:51 +0000127void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000128 // Number indexes with half the default spacing so we can catch up quickly.
129 const unsigned Space = SlotIndex::InstrDist/2;
130 assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
131
Lang Hames613dfb22012-04-17 04:15:51 +0000132 IndexList::iterator startItr = prior(curItr);
133 unsigned index = startItr->getIndex();
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000134 do {
Lang Hames613dfb22012-04-17 04:15:51 +0000135 curItr->setIndex(index += Space);
136 ++curItr;
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000137 // If the next index is bigger, we have caught up.
Lang Hames613dfb22012-04-17 04:15:51 +0000138 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000139
Lang Hames613dfb22012-04-17 04:15:51 +0000140 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000141 << index << " ***\n");
142 ++NumLocalRenum;
143}
144
Cameron Zwarich349cf342013-02-20 06:46:41 +0000145// Repair indexes after adding and removing instructions.
146void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
147 MachineBasicBlock::iterator Begin,
148 MachineBasicBlock::iterator End) {
Cameron Zwarichc5b61352013-02-20 22:10:00 +0000149 // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
150 // does the same thing.
151 // Find anchor points, which are at the beginning/end of blocks or at
152 // instructions that already have indexes.
153 while (Begin != MBB->begin() && !hasIndex(Begin))
154 --Begin;
155 while (End != MBB->end() && !hasIndex(End))
156 ++End;
157
Cameron Zwarich349cf342013-02-20 06:46:41 +0000158 bool includeStart = (Begin == MBB->begin());
159 SlotIndex startIdx;
160 if (includeStart)
161 startIdx = getMBBStartIdx(MBB);
162 else
163 startIdx = getInstructionIndex(Begin);
164
165 SlotIndex endIdx;
166 if (End == MBB->end())
167 endIdx = getMBBEndIdx(MBB);
168 else
169 endIdx = getInstructionIndex(End);
170
171 // FIXME: Conceptually, this code is implementing an iterator on MBB that
172 // optionally includes an additional position prior to MBB->begin(), indicated
173 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
174 // in parallel with SlotIndexes, but there should be a better way to do this.
175 IndexList::iterator ListB = startIdx.listEntry();
176 IndexList::iterator ListI = endIdx.listEntry();
177 MachineBasicBlock::iterator MBBI = End;
178 bool pastStart = false;
179 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
180 assert(ListI->getIndex() >= startIdx.getIndex() &&
181 (includeStart || !pastStart) &&
182 "Decremented past the beginning of region to repair.");
183
184 MachineInstr *SlotMI = ListI->getInstr();
185 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? MBBI : 0;
186 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
187
188 if (SlotMI == MI && !MBBIAtBegin) {
189 --ListI;
190 if (MBBI != Begin)
191 --MBBI;
192 else
193 pastStart = true;
194 } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
195 if (MBBI != Begin)
196 --MBBI;
197 else
198 pastStart = true;
199 } else {
200 --ListI;
201 if (SlotMI)
202 removeMachineInstrFromMaps(SlotMI);
203 }
204 }
205
206 // In theory this could be combined with the previous loop, but it is tricky
207 // to update the IndexList while we are iterating it.
208 for (MachineBasicBlock::iterator I = End; I != Begin;) {
209 --I;
210 MachineInstr *MI = I;
Cameron Zwarich79f5ab12013-02-23 10:25:25 +0000211 if (!MI->isDebugValue() && mi2iMap.find(MI) == mi2iMap.end())
Cameron Zwarich349cf342013-02-20 06:46:41 +0000212 insertMachineInstrInMaps(MI);
213 }
214}
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000215
Manman Renb720be62012-09-11 22:23:19 +0000216#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Lang Hames233a60e2009-11-03 23:52:08 +0000217void SlotIndexes::dump() const {
Lang Hames613dfb22012-04-17 04:15:51 +0000218 for (IndexList::const_iterator itr = indexList.begin();
219 itr != indexList.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000220 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000221
222 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000223 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000224 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000225 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000226 }
227 }
228
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +0000229 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
230 dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
231 << MBBRanges[i].second << ")\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000232}
Manman Ren77e300e2012-09-06 19:06:06 +0000233#endif
Lang Hames233a60e2009-11-03 23:52:08 +0000234
235// Print a SlotIndex to a raw_ostream.
236void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000237 if (isValid())
Lang Hames613dfb22012-04-17 04:15:51 +0000238 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000239 else
240 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000241}
242
Manman Renb720be62012-09-11 22:23:19 +0000243#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Lang Hames233a60e2009-11-03 23:52:08 +0000244// Dump a SlotIndex to stderr.
245void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000246 print(dbgs());
247 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000248}
Manman Ren77e300e2012-09-06 19:06:06 +0000249#endif
Lang Hames233a60e2009-11-03 23:52:08 +0000250