@@ -86,6 +86,15 @@ class InstrPosIndexes {
86
86
return false ;
87
87
}
88
88
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.
89
98
unsigned Distance = 1 ;
90
99
MachineBasicBlock::const_iterator Start = MI.getIterator (),
91
100
End = std::next (Start);
@@ -99,11 +108,38 @@ class InstrPosIndexes {
99
108
++Distance;
100
109
}
101
110
111
+ // LastIndex is initialized to last used index prior to MI or zero.
112
+ // In previous example, LastIndex is 1024, EndIndex is 2048;
102
113
uint64_t LastIndex =
103
114
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
+ }
107
143
108
144
// Reassign index for all instructions if number of new inserted
109
145
// instructions exceed slot or all instructions are new.
0 commit comments