SlotIndexes.cpp revision e264f62ca09a8f65c87a46d562a4d0f9ec5d457e
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;
43static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering");
44
45IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
46  return &*IndexListEntryEmptyKey;
47}
48
49IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
50  return &*IndexListEntryTombstoneKey;
51}
52
53
54void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
55  au.setPreservesAll();
56  MachineFunctionPass::getAnalysisUsage(au);
57}
58
59void SlotIndexes::releaseMemory() {
60  mi2iMap.clear();
61  mbb2IdxMap.clear();
62  idx2MBBMap.clear();
63  terminatorGaps.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      if (miItr == mbb->getFirstTerminator()) {
115        push_back(createEntry(0, index));
116        terminatorGaps.insert(
117          std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
118        index += SlotIndex::NUM;
119      }
120
121      // Insert a store index for the instr.
122      push_back(createEntry(mi, index));
123
124      // Save this base index in the maps.
125      mi2iMap.insert(
126        std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
127
128      ++functionSize;
129
130      unsigned Slots = mi->getDesc().getNumDefs();
131      if (Slots == 0)
132        Slots = 1;
133
134      index += (Slots + 1) * SlotIndex::NUM;
135    }
136
137    if (mbb->getFirstTerminator() == mbb->end()) {
138      push_back(createEntry(0, index));
139      terminatorGaps.insert(
140        std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
141      index += SlotIndex::NUM;
142    }
143
144    // One blank instruction at the end.
145    push_back(createEntry(0, index));
146
147    SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
148    mbb2IdxMap.insert(
149      std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
150
151    idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
152  }
153
154  // Sort the Idx2MBBMap
155  std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
156
157  DEBUG(dump());
158
159  // And we're done!
160  return false;
161}
162
163void SlotIndexes::renumberIndexes() {
164
165  // Renumber updates the index of every element of the index list.
166  // If all instrs in the function have been allocated an index (which has been
167  // placed in the index list in the order of instruction iteration) then the
168  // resulting numbering will match what would have been generated by the
169  // pass during the initial numbering of the function if the new instructions
170  // had been present.
171
172  functionSize = 0;
173  unsigned index = 0;
174
175  for (IndexListEntry *curEntry = front(); curEntry != getTail();
176       curEntry = curEntry->getNext()) {
177
178    curEntry->setIndex(index);
179
180    if (curEntry->getInstr() == 0) {
181      // MBB start entry or terminator gap. Just step index by 1.
182      index += SlotIndex::NUM;
183    }
184    else {
185      ++functionSize;
186      unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
187      if (Slots == 0)
188        Slots = 1;
189
190      index += (Slots + 1) * SlotIndex::NUM;
191    }
192  }
193}
194
195void SlotIndexes::dump() const {
196  for (const IndexListEntry *itr = front(); itr != getTail();
197       itr = itr->getNext()) {
198    dbgs() << itr->getIndex() << " ";
199
200    if (itr->getInstr() != 0) {
201      dbgs() << *itr->getInstr();
202    } else {
203      dbgs() << "\n";
204    }
205  }
206
207  for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
208       itr != mbb2IdxMap.end(); ++itr) {
209    dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
210           << itr->second.first << ", " << itr->second.second << "]\n";
211  }
212}
213
214// Print a SlotIndex to a raw_ostream.
215void SlotIndex::print(raw_ostream &os) const {
216  os << getIndex();
217  if (isPHI())
218    os << "*";
219}
220
221// Dump a SlotIndex to stderr.
222void SlotIndex::dump() const {
223  print(dbgs());
224  dbgs() << "\n";
225}
226
227