Skip to content

Commit 26921ec

Browse files
committed
Add comments. Improve index assign algorithm.
1 parent b6a7bbc commit 26921ec

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

llvm/lib/CodeGen/RegAllocFast.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ class InstrPosIndexes {
8686
return false;
8787
}
8888

89+
// Distance is the number of consecutive unassigned instructions including
90+
// MI. Start is the first instruction of them. End is the next of last
91+
// instruction of them.
92+
// e.g.
93+
// |Instruction| A | B | C | MI | D | E |
94+
// | Index | 1024 | | | | | 2048 |
95+
//
96+
// In this case, B, C, MI, D are unassigned. Distance is 4, Start is B, End
97+
// is E.
8998
unsigned Distance = 1;
9099
MachineBasicBlock::const_iterator Start = MI.getIterator(),
91100
End = std::next(Start);
@@ -99,11 +108,38 @@ class InstrPosIndexes {
99108
++Distance;
100109
}
101110

111+
// LastIndex is initialized to last used index prior to MI or zero.
112+
// In previous example, LastIndex is 1024, EndIndex is 2048;
102113
uint64_t LastIndex =
103114
Start == CurMBB->begin() ? 0 : Instr2PosIndex.at(&*std::prev(Start));
104-
uint64_t Step = End == CurMBB->end()
105-
? static_cast<uint64_t>(InstrDist)
106-
: (Instr2PosIndex.at(&*End) - LastIndex - 1) / Distance;
115+
uint64_t Step;
116+
if (End == CurMBB->end())
117+
Step = static_cast<uint64_t>(InstrDist);
118+
else {
119+
// No instruction uses index zero.
120+
uint64_t EndIndex = Instr2PosIndex.at(&*End);
121+
assert(EndIndex > LastIndex && "Index must be ascending order");
122+
unsigned NumAvailableIndexes = EndIndex - LastIndex - 1;
123+
// We want index gap between two adjacent MI is as same as possible. Given
124+
// total A available indexes, D is number of consecutive unassigned
125+
// instructions, S is the step.
126+
// |<- S-1 -> MI <- S-1 -> MI <- A-S*D ->|
127+
// There're S-1 available indexes between unassigned instruction and its
128+
// predecessor. There're A-S*D available indexes between the last
129+
// unassigned instruction and its successor.
130+
// Ideally, we want
131+
// S-1 = A-S*D
132+
// then
133+
// S = (A+1)/(D+1)
134+
// An valid S must be integer greater than zero, so
135+
// S <= (A+1)/(D+1)
136+
// =>
137+
// A-S*D >= 0
138+
// That means we can safely use (A+1)/(D+1) as step.
139+
// In previous example, Step is 204, Index of B, C, MI, D is 1228, 1432,
140+
// 1636, 1840.
141+
Step = (NumAvailableIndexes + 1) / (Distance + 1);
142+
}
107143

108144
// Reassign index for all instructions if number of new inserted
109145
// instructions exceed slot or all instructions are new.

0 commit comments

Comments
 (0)