SlotIndexes.cpp revision bfd9a9590cf1f4fedd927c0f34e83ff7311bac16
1//===-- 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"
13#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/Support/Debug.h"
15#include "llvm/Support/raw_ostream.h"
16#include "llvm/Support/ManagedStatic.h"
17#include "llvm/Target/TargetInstrInfo.h"
18
19using namespace llvm;
20
21
22// Yep - these are thread safe. See the header for details.
23namespace {
24
25
26  class EmptyIndexListEntry : public IndexListEntry {
27  public:
28    EmptyIndexListEntry() : IndexListEntry(EMPTY_KEY) {}
29  };
30
31  class TombstoneIndexListEntry : public IndexListEntry {
32  public:
33    TombstoneIndexListEntry() : IndexListEntry(TOMBSTONE_KEY) {}
34  };
35
36  // The following statics are thread safe. They're read only, and you
37  // can't step from them to any other list entries.
38  ManagedStatic<EmptyIndexListEntry> IndexListEntryEmptyKey;
39  ManagedStatic<TombstoneIndexListEntry> IndexListEntryTombstoneKey;
40}
41
42char SlotIndexes::ID = 0;
43INITIALIZE_PASS(SlotIndexes, "slotindexes",
44                "Slot index numbering", false, false);
45
46IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
47  return &*IndexListEntryEmptyKey;
48}
49
50IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
51  return &*IndexListEntryTombstoneKey;
52}
53
54
55void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
56  au.setPreservesAll();
57  MachineFunctionPass::getAnalysisUsage(au);
58}
59
60void SlotIndexes::releaseMemory() {
61  mi2iMap.clear();
62  mbb2IdxMap.clear();
63  idx2MBBMap.clear();
64  clearList();
65}
66
67bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
68
69  // Compute numbering as follows:
70  // Grab an iterator to the start of the index list.
71  // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
72  // iterator in lock-step (though skipping it over indexes which have
73  // null pointers in the instruction field).
74  // At each iteration assert that the instruction pointed to in the index
75  // is the same one pointed to by the MI iterator. This
76
77  // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
78  // only need to be set up once after the first numbering is computed.
79
80  mf = &fn;
81  initList();
82
83  // Check that the list contains only the sentinal.
84  assert(indexListHead->getNext() == 0 &&
85         "Index list non-empty at initial numbering?");
86  assert(idx2MBBMap.empty() &&
87         "Index -> MBB mapping non-empty at initial numbering?");
88  assert(mbb2IdxMap.empty() &&
89         "MBB -> Index mapping non-empty at initial numbering?");
90  assert(mi2iMap.empty() &&
91         "MachineInstr -> Index mapping non-empty at initial numbering?");
92
93  functionSize = 0;
94  unsigned index = 0;
95
96  push_back(createEntry(0, index));
97
98  // Iterate over the function.
99  for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
100       mbbItr != mbbEnd; ++mbbItr) {
101    MachineBasicBlock *mbb = &*mbbItr;
102
103    // Insert an index for the MBB start.
104    SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
105
106    index += SlotIndex::NUM;
107
108    for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
109         miItr != miEnd; ++miItr) {
110      MachineInstr *mi = miItr;
111      if (mi->isDebugValue())
112        continue;
113
114      // Insert a store index for the instr.
115      push_back(createEntry(mi, index));
116
117      // Save this base index in the maps.
118      mi2iMap.insert(
119        std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
120
121      ++functionSize;
122
123      unsigned Slots = mi->getDesc().getNumDefs();
124      if (Slots == 0)
125        Slots = 1;
126
127      index += (Slots + 1) * SlotIndex::NUM;
128    }
129
130    // One blank instruction at the end.
131    push_back(createEntry(0, index));
132
133    SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
134    mbb2IdxMap.insert(
135      std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
136
137    idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
138  }
139
140  // Sort the Idx2MBBMap
141  std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
142
143  DEBUG(dump());
144
145  // And we're done!
146  return false;
147}
148
149void SlotIndexes::renumberIndexes() {
150
151  // Renumber updates the index of every element of the index list.
152  // If all instrs in the function have been allocated an index (which has been
153  // placed in the index list in the order of instruction iteration) then the
154  // resulting numbering will match what would have been generated by the
155  // pass during the initial numbering of the function if the new instructions
156  // had been present.
157
158  functionSize = 0;
159  unsigned index = 0;
160
161  for (IndexListEntry *curEntry = front(); curEntry != getTail();
162       curEntry = curEntry->getNext()) {
163
164    curEntry->setIndex(index);
165
166    if (curEntry->getInstr() == 0) {
167      // MBB start entry. Just step index by 1.
168      index += SlotIndex::NUM;
169    }
170    else {
171      ++functionSize;
172      unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
173      if (Slots == 0)
174        Slots = 1;
175
176      index += (Slots + 1) * SlotIndex::NUM;
177    }
178  }
179}
180
181void SlotIndexes::dump() const {
182  for (const IndexListEntry *itr = front(); itr != getTail();
183       itr = itr->getNext()) {
184    dbgs() << itr->getIndex() << " ";
185
186    if (itr->getInstr() != 0) {
187      dbgs() << *itr->getInstr();
188    } else {
189      dbgs() << "\n";
190    }
191  }
192
193  for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
194       itr != mbb2IdxMap.end(); ++itr) {
195    dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
196           << itr->second.first << ", " << itr->second.second << "]\n";
197  }
198}
199
200// Print a SlotIndex to a raw_ostream.
201void SlotIndex::print(raw_ostream &os) const {
202  os << entry().getIndex() << "LudS"[getSlot()];
203}
204
205// Dump a SlotIndex to stderr.
206void SlotIndex::dump() const {
207  print(dbgs());
208  dbgs() << "\n";
209}
210
211