@@ -57,14 +57,31 @@ object LivenessAnalysis {
57
57
}
58
58
}
59
59
60
+ // May be used for debug purposes.
61
+ @Suppress(" unused" )
62
+ private fun BitSet.format () = buildString {
63
+ append(' [' )
64
+ var first = true
65
+ forEachBit {
66
+ if (! first) append(" , " )
67
+ first = false
68
+ append(variables[it].name)
69
+ }
70
+ append(' ]' )
71
+ }
72
+
60
73
private fun getVariableId (variable : IrVariable ) = variableIds.getOrPut(variable) {
61
74
variables.add(variable)
62
75
variables.lastIndex
63
76
}
64
77
65
78
private inline fun <T : IrElement > saveAndCompute (element : T , liveVariables : BitSet , compute : () -> BitSet ): BitSet {
66
- if (filter(element))
67
- filteredElementEndsLV[element] = liveVariables.copy().also { it.or (catchesLV) }
79
+ if (filter(element)) {
80
+ // Merge with the previous because of the loops (see the comment there).
81
+ val elementLV = filteredElementEndsLV.getOrPut(element) { BitSet () }
82
+ elementLV.or (liveVariables)
83
+ elementLV.or (catchesLV)
84
+ }
68
85
return compute()
69
86
}
70
87
@@ -217,13 +234,15 @@ object LivenessAnalysis {
217
234
loopEndsLV[loop] = data
218
235
var bodyEndLV = loop.condition.accept(this , data)
219
236
val body = loop.body ? : return bodyEndLV
220
- var bodyStartLV: BitSet
237
+ val bodyStartLV = BitSet ()
221
238
// In practice, only one or two iterations seem to be enough, but the classic algorithm
222
239
// loops until "saturation" (when nothing changes anymore).
223
240
do {
224
241
loopStartsLV[loop] = bodyEndLV
225
- bodyStartLV = body.accept(this , bodyEndLV)
226
- val nextBodyEndLV = loop.condition.accept(this , bodyStartLV)
242
+ val curBodyStartLV = body.accept(this , bodyEndLV)
243
+ // Since it's unknown how many iterations the loop will execute, merge the live variables at each iteration.
244
+ bodyStartLV.or (curBodyStartLV)
245
+ val nextBodyEndLV = loop.condition.accept(this , curBodyStartLV)
227
246
val lvHaveChanged = nextBodyEndLV != bodyEndLV
228
247
bodyEndLV = nextBodyEndLV
229
248
} while (lvHaveChanged)
0 commit comments