llvm.org GIT mirror llvm / 613dfb2
SlotIndexes used to store the index list in a crufty custom linked-list. I can't for the life of me remember why I wrote it this way, but I can't see any good reason for it now. This patch replaces the custom linked list with an ilist. This change should preserve the existing numberings exactly, so no generated code should change (if it does, file a bug!). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154904 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 8 years ago
2 changed file(s) with 105 addition(s) and 169 deletion(s). Raw diff Collapse all Expand all
2222 #include "llvm/CodeGen/MachineFunction.h"
2323 #include "llvm/CodeGen/MachineFunctionPass.h"
2424 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/ilist.h"
2526 #include "llvm/ADT/SmallVector.h"
2627 #include "llvm/ADT/DenseMap.h"
2728 #include "llvm/Support/Allocator.h"
3233 /// SlotIndexes pass. It should not be used directly. See the
3334 /// SlotIndex & SlotIndexes classes for the public interface to this
3435 /// information.
35 class IndexListEntry {
36 IndexListEntry *next, *prev;
36 class IndexListEntry : public ilist_node {
3737 MachineInstr *mi;
3838 unsigned index;
3939
5050 void setIndex(unsigned index) {
5151 this->index = index;
5252 }
53
54 IndexListEntry* getNext() { return next; }
55 const IndexListEntry* getNext() const { return next; }
56 void setNext(IndexListEntry *next) {
57 this->next = next;
58 }
59
60 IndexListEntry* getPrev() { return prev; }
61 const IndexListEntry* getPrev() const { return prev; }
62 void setPrev(IndexListEntry *prev) {
63 this->prev = prev;
64 }
53
54 };
55
56 template <>
57 struct ilist_traits : public ilist_default_traits {
58 private:
59 mutable ilist_half_node Sentinel;
60 public:
61 IndexListEntry *createSentinel() const {
62 return static_cast(&Sentinel);
63 }
64 void destroySentinel(IndexListEntry *) const {}
65
66 IndexListEntry *provideInitialHead() const { return createSentinel(); }
67 IndexListEntry *ensureHead(IndexListEntry*) const { return createSentinel(); }
68 static void noteHead(IndexListEntry*, IndexListEntry*) {}
69 void deleteNode(IndexListEntry *N) {}
70
71 private:
72 void createNode(const IndexListEntry &);
6573 };
6674
6775 // Specialize PointerLikeTypeTraits for IndexListEntry.
6876 template <>
69 class PointerLikeTypeTraits {
77 class PointerLikeTypeTraits {
7078 public:
7179 static inline void* getAsVoidPointer(IndexListEntry *p) {
7280 return p;
111119 SlotIndex(IndexListEntry *entry, unsigned slot)
112120 : lie(entry, slot) {}
113121
114 IndexListEntry& entry() const {
122 IndexListEntry* listEntry() const {
115123 assert(isValid() && "Attempt to compare reserved index.");
116 return *lie.getPointer();
124 return lie.getPointer();
117125 }
118126
119127 int getIndex() const {
120 return entry().getIndex() | getSlot();
128 return listEntry()->getIndex() | getSlot();
121129 }
122130
123131 /// Returns the slot for this SlotIndex.
149157 SlotIndex() : lie(0, 0) {}
150158
151159 // Construct a new slot index from the given one, and set the slot.
152 SlotIndex(const SlotIndex &li, Slot s)
153 : lie(&li.entry(), unsigned(s)) {
160 SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
154161 assert(lie.getPointer() != 0 &&
155162 "Attempt to construct index with 0 pointer.");
156163 }
178185 bool operator!=(SlotIndex other) const {
179186 return lie != other.lie;
180187 }
181
188
182189 /// Compare two SlotIndex objects. Return true if the first index
183190 /// is strictly lower than the second.
184191 bool operator<(SlotIndex other) const {
210217 /// isEarlierInstr - Return true if A refers to an instruction earlier than
211218 /// B. This is equivalent to A < B && !isSameInstr(A, B).
212219 static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
213 return A.entry().getIndex() < B.entry().getIndex();
220 return A.listEntry()->getIndex() < B.listEntry()->getIndex();
214221 }
215222
216223 /// Return the distance from this index to the given one.
235242 /// is the one associated with the Slot_Block slot for the instruction
236243 /// pointed to by this index.
237244 SlotIndex getBaseIndex() const {
238 return SlotIndex(&entry(), Slot_Block);
245 return SlotIndex(listEntry(), Slot_Block);
239246 }
240247
241248 /// Returns the boundary index for associated with this index. The boundary
242249 /// index is the one associated with the Slot_Block slot for the instruction
243250 /// pointed to by this index.
244251 SlotIndex getBoundaryIndex() const {
245 return SlotIndex(&entry(), Slot_Dead);
252 return SlotIndex(listEntry(), Slot_Dead);
246253 }
247254
248255 /// Returns the register use/def slot in the current instruction for a
249256 /// normal or early-clobber def.
250257 SlotIndex getRegSlot(bool EC = false) const {
251 return SlotIndex(&entry(), EC ? Slot_EarlyClobber : Slot_Register);
258 return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
252259 }
253260
254261 /// Returns the dead def kill slot for the current instruction.
255262 SlotIndex getDeadSlot() const {
256 return SlotIndex(&entry(), Slot_Dead);
263 return SlotIndex(listEntry(), Slot_Dead);
257264 }
258265
259266 /// Returns the next slot in the index list. This could be either the
265272 SlotIndex getNextSlot() const {
266273 Slot s = getSlot();
267274 if (s == Slot_Dead) {
268 return SlotIndex(entry().getNext(), Slot_Block);
269 }
270 return SlotIndex(&entry(), s + 1);
275 return SlotIndex(listEntry()->getNextNode(), Slot_Block);
276 }
277 return SlotIndex(listEntry(), s + 1);
271278 }
272279
273280 /// Returns the next index. This is the index corresponding to the this
274281 /// index's slot, but for the next instruction.
275282 SlotIndex getNextIndex() const {
276 return SlotIndex(entry().getNext(), getSlot());
283 return SlotIndex(listEntry()->getNextNode(), getSlot());
277284 }
278285
279286 /// Returns the previous slot in the index list. This could be either the
285292 SlotIndex getPrevSlot() const {
286293 Slot s = getSlot();
287294 if (s == Slot_Block) {
288 return SlotIndex(entry().getPrev(), Slot_Dead);
289 }
290 return SlotIndex(&entry(), s - 1);
295 return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
296 }
297 return SlotIndex(listEntry(), s - 1);
291298 }
292299
293300 /// Returns the previous index. This is the index corresponding to this
294301 /// index's slot, but for the previous instruction.
295302 SlotIndex getPrevIndex() const {
296 return SlotIndex(entry().getPrev(), getSlot());
303 return SlotIndex(listEntry()->getPrevNode(), getSlot());
297304 }
298305
299306 };
314321 return (LHS == RHS);
315322 }
316323 };
317
324
318325 template <> struct isPodLike { static const bool value = true; };
319326
320327
345352 class SlotIndexes : public MachineFunctionPass {
346353 private:
347354
355 typedef ilist IndexList;
356 IndexList indexList;
357
348358 MachineFunction *mf;
349 IndexListEntry *indexListHead;
350359 unsigned functionSize;
351360
352361 typedef DenseMap Mi2IndexMap;
373382 return entry;
374383 }
375384
376 void initList() {
377 assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
378 indexListHead = createEntry(0, ~0U);
379 indexListHead->setNext(0);
380 indexListHead->setPrev(indexListHead);
381 }
382
383 void clearList() {
384 indexListHead = 0;
385 ileAllocator.Reset();
386 }
387
388 IndexListEntry* getTail() {
389 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
390 return indexListHead->getPrev();
391 }
392
393 const IndexListEntry* getTail() const {
394 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
395 return indexListHead->getPrev();
396 }
397
398 // Returns true if the index list is empty.
399 bool empty() const { return (indexListHead == getTail()); }
400
401 IndexListEntry* front() {
402 assert(!empty() && "front() called on empty index list.");
403 return indexListHead;
404 }
405
406 const IndexListEntry* front() const {
407 assert(!empty() && "front() called on empty index list.");
408 return indexListHead;
409 }
410
411 IndexListEntry* back() {
412 assert(!empty() && "back() called on empty index list.");
413 return getTail()->getPrev();
414 }
415
416 const IndexListEntry* back() const {
417 assert(!empty() && "back() called on empty index list.");
418 return getTail()->getPrev();
419 }
420
421 /// Insert a new entry before itr.
422 void insert(IndexListEntry *itr, IndexListEntry *val) {
423 assert(itr != 0 && "itr should not be null.");
424 IndexListEntry *prev = itr->getPrev();
425 val->setNext(itr);
426 val->setPrev(prev);
427
428 if (itr != indexListHead) {
429 prev->setNext(val);
430 }
431 else {
432 indexListHead = val;
433 }
434 itr->setPrev(val);
435 }
436
437 /// Push a new entry on to the end of the list.
438 void push_back(IndexListEntry *val) {
439 insert(getTail(), val);
440 }
441
442 /// Renumber locally after inserting newEntry.
443 void renumberIndexes(IndexListEntry *newEntry);
385 /// Renumber locally after inserting curItr.
386 void renumberIndexes(IndexList::iterator curItr);
444387
445388 public:
446389 static char ID;
447390
448 SlotIndexes() : MachineFunctionPass(ID), indexListHead(0) {
391 SlotIndexes() : MachineFunctionPass(ID) {
449392 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
450393 }
451394
452395 virtual void getAnalysisUsage(AnalysisUsage &au) const;
453 virtual void releaseMemory();
396 virtual void releaseMemory();
454397
455398 virtual bool runOnMachineFunction(MachineFunction &fn);
456399
462405
463406 /// Returns the zero index for this analysis.
464407 SlotIndex getZeroIndex() {
465 assert(front()->getIndex() == 0 && "First index is not 0?");
466 return SlotIndex(front(), 0);
408 assert(indexList.front().getIndex() == 0 && "First index is not 0?");
409 return SlotIndex(&indexList.front(), 0);
467410 }
468411
469412 /// Returns the base index of the last slot in this analysis.
470413 SlotIndex getLastIndex() {
471 return SlotIndex(back(), 0);
414 return SlotIndex(&indexList.back(), 0);
472415 }
473416
474417 /// Returns the distance between the highest and lowest indexes allocated
475418 /// so far.
476419 unsigned getIndexesLength() const {
477 assert(front()->getIndex() == 0 &&
420 assert(indexList.front().getIndex() == 0 &&
478421 "Initial index isn't zero?");
479
480 return back()->getIndex();
422 return indexList.back().getIndex();
481423 }
482424
483425 /// Returns the number of instructions in the function.
502444 /// Returns the instruction for the given index, or null if the given
503445 /// index has no instruction associated with it.
504446 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
505 return index.isValid() ? index.entry().getInstr() : 0;
447 return index.isValid() ? index.listEntry()->getInstr() : 0;
506448 }
507449
508450 /// Returns the next non-null index.
509451 SlotIndex getNextNonNullIndex(SlotIndex index) {
510 SlotIndex nextNonNull = index.getNextIndex();
511
512 while (&nextNonNull.entry() != getTail() &&
513 getInstructionFromIndex(nextNonNull) == 0) {
514 nextNonNull = nextNonNull.getNextIndex();
515 }
516
517 return nextNonNull;
452 IndexList::iterator itr(index.listEntry());
453 ++itr;
454 while (itr != indexList.end() && itr->getInstr() == 0) { ++itr; }
455 return SlotIndex(itr, index.getSlot());
518456 }
519457
520458 /// getIndexBefore - Returns the index of the last indexed instruction
658596 assert(mi->getParent() != 0 && "Instr must be added to function.");
659597
660598 // Get the entries where mi should be inserted.
661 IndexListEntry *prevEntry, *nextEntry;
599 IndexList::iterator prevItr, nextItr;
662600 if (Late) {
663601 // Insert mi's index immediately before the following instruction.
664 nextEntry = &getIndexAfter(mi).entry();
665 prevEntry = nextEntry->getPrev();
602 nextItr = getIndexAfter(mi).listEntry();
603 prevItr = prior(nextItr);
666604 } else {
667605 // Insert mi's index immediately after the preceeding instruction.
668 prevEntry = &getIndexBefore(mi).entry();
669 nextEntry = prevEntry->getNext();
606 prevItr = getIndexBefore(mi).listEntry();
607 nextItr = next(prevItr);
670608 }
671609
672610 // Get a number for the new instr, or 0 if there's no room currently.
673611 // In the latter case we'll force a renumber later.
674 unsigned dist = ((nextEntry->getIndex() - prevEntry->getIndex())/2) & ~3u;
675 unsigned newNumber = prevEntry->getIndex() + dist;
612 unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
613 unsigned newNumber = prevItr->getIndex() + dist;
676614
677615 // Insert a new list entry for mi.
678 IndexListEntry *newEntry = createEntry(mi, newNumber);
679 insert(nextEntry, newEntry);
616 IndexList::iterator newItr =
617 indexList.insert(nextItr, createEntry(mi, newNumber));
680618
681619 // Renumber locally if we need to.
682620 if (dist == 0)
683 renumberIndexes(newEntry);
684
685 SlotIndex newIndex(newEntry, SlotIndex::Slot_Block);
621 renumberIndexes(newItr);
622
623 SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
686624 mi2iMap.insert(std::make_pair(mi, newIndex));
687625 return newIndex;
688626 }
693631 // MachineInstr -> index mappings
694632 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
695633 if (mi2iItr != mi2iMap.end()) {
696 IndexListEntry *miEntry(&mi2iItr->second.entry());
634 IndexListEntry *miEntry(mi2iItr->second.listEntry());
697635 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
698636 // FIXME: Eventually we want to actually delete these indexes.
699637 miEntry->setInstr(0);
708646 if (mi2iItr == mi2iMap.end())
709647 return;
710648 SlotIndex replaceBaseIndex = mi2iItr->second;
711 IndexListEntry *miEntry(&replaceBaseIndex.entry());
649 IndexListEntry *miEntry(replaceBaseIndex.listEntry());
712650 assert(miEntry->getInstr() == mi &&
713651 "Mismatched instruction in index tables.");
714652 miEntry->setInstr(newMI);
725663 IndexListEntry *nextEntry = 0;
726664
727665 if (nextMBB == mbb->getParent()->end()) {
728 nextEntry = getTail();
666 nextEntry = indexList.end();
729667 } else {
730 nextEntry = &getMBBStartIdx(nextMBB).entry();
731 }
732
733 insert(nextEntry, startEntry);
734 insert(nextEntry, stopEntry);
668 nextEntry = getMBBStartIdx(nextMBB).listEntry();
669 }
670
671 indexList.insert(nextEntry, startEntry);
672 indexList.insert(nextEntry, stopEntry);
735673
736674 SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
737675 SlotIndex endIdx(nextEntry, SlotIndex::Slot_Block);
765703
766704 }
767705
768 #endif // LLVM_CODEGEN_LIVEINDEX_H
706 #endif // LLVM_CODEGEN_SLOTINDEXES_H
3333 mi2iMap.clear();
3434 MBBRanges.clear();
3535 idx2MBBMap.clear();
36 clearList();
36 indexList.clear();
37 ileAllocator.Reset();
3738 }
3839
3940 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
4445 // iterator in lock-step (though skipping it over indexes which have
4546 // null pointers in the instruction field).
4647 // At each iteration assert that the instruction pointed to in the index
47 // is the same one pointed to by the MI iterator. This
48 // is the same one pointed to by the MI iterator. This
4849
4950 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
5051 // only need to be set up once after the first numbering is computed.
5152
5253 mf = &fn;
53 initList();
5454
5555 // Check that the list contains only the sentinal.
56 assert(indexListHead->getNext() == 0 &&
57 "Index list non-empty at initial numbering?");
56 assert(indexList.empty() && "Index list non-empty at initial numbering?");
5857 assert(idx2MBBMap.empty() &&
5958 "Index -> MBB mapping non-empty at initial numbering?");
6059 assert(MBBRanges.empty() &&
6766 MBBRanges.resize(mf->getNumBlockIDs());
6867 idx2MBBMap.reserve(mf->size());
6968
70 push_back(createEntry(0, index));
69 indexList.push_back(createEntry(0, index));
7170
7271 // Iterate over the function.
7372 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
7574 MachineBasicBlock *mbb = &*mbbItr;
7675
7776 // Insert an index for the MBB start.
78 SlotIndex blockStartIndex(back(), SlotIndex::Slot_Block);
77 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
7978
8079 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
8180 miItr != miEnd; ++miItr) {
8483 continue;
8584
8685 // Insert a store index for the instr.
87 push_back(createEntry(mi, index += SlotIndex::InstrDist));
86 indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
8887
8988 // Save this base index in the maps.
90 mi2iMap.insert(std::make_pair(mi, SlotIndex(back(),
89 mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
9190 SlotIndex::Slot_Block)));
92
91
9392 ++functionSize;
9493 }
9594
9695 // We insert one blank instructions between basic blocks.
97 push_back(createEntry(0, index += SlotIndex::InstrDist));
96 indexList.push_back(createEntry(0, index += SlotIndex::InstrDist));
9897
9998 MBBRanges[mbb->getNumber()].first = blockStartIndex;
100 MBBRanges[mbb->getNumber()].second = SlotIndex(back(),
99 MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
101100 SlotIndex::Slot_Block);
102101 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
103102 }
118117
119118 unsigned index = 0;
120119
121 for (IndexListEntry *curEntry = front(); curEntry != getTail();
122 curEntry = curEntry->getNext()) {
123 curEntry->setIndex(index);
120 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
121 I != E; ++I) {
122 I->setIndex(index);
124123 index += SlotIndex::InstrDist;
125124 }
126125 }
127126
128 // Renumber indexes locally after curEntry was inserted, but failed to get a new
127 // Renumber indexes locally after curItr was inserted, but failed to get a new
129128 // index.
130 void SlotIndexes::renumberIndexes(IndexListEntry *curEntry) {
129 void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
131130 // Number indexes with half the default spacing so we can catch up quickly.
132131 const unsigned Space = SlotIndex::InstrDist/2;
133132 assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
134133
135 IndexListEntry *start = curEntry->getPrev();
136 unsigned index = start->getIndex();
137 IndexListEntry *tail = getTail();
134 IndexList::iterator startItr = prior(curItr);
135 unsigned index = startItr->getIndex();
138136 do {
139 curEntry->setIndex(index += Space);
140 curEntry = curEntry->getNext();
137 curItr->setIndex(index += Space);
138 ++curItr;
141139 // If the next index is bigger, we have caught up.
142 } while (curEntry != tail && curEntry->getIndex() <= index);
140 } while (curItr != indexList.end() && curItr->getIndex() <= index);
143141
144 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << start->getIndex() << '-'
142 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
145143 << index << " ***\n");
146144 ++NumLocalRenum;
147145 }
148146
149147
150148 void SlotIndexes::dump() const {
151 for (const IndexListEntry *itr = front(); itr != getTail();
152 itr = itr->getNext()) {
149 for (IndexList::const_iterator itr = indexList.begin();
150 itr != indexList.end(); ++itr) {
153151 dbgs() << itr->getIndex() << " ";
154152
155153 if (itr->getInstr() != 0) {
167165 // Print a SlotIndex to a raw_ostream.
168166 void SlotIndex::print(raw_ostream &os) const {
169167 if (isValid())
170 os << entry().getIndex() << "Berd"[getSlot()];
168 os << listEntry()->getIndex() << "Berd"[getSlot()];
171169 else
172170 os << "invalid";
173171 }