1
1
package org .jruby .ir .passes ;
2
2
3
- import org .jruby .ir .IRClosure ;
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+ import java .util .HashMap ;
6
+ import java .util .List ;
7
+ import java .util .ListIterator ;
8
+ import java .util .Map ;
4
9
import org .jruby .ir .IRScope ;
5
10
import org .jruby .ir .instructions .*;
6
11
import org .jruby .ir .operands .ImmutableLiteral ;
7
12
import org .jruby .ir .operands .Operand ;
8
13
import org .jruby .ir .operands .TemporaryVariable ;
9
14
import org .jruby .ir .operands .Variable ;
10
15
11
- import java .util .*;
12
-
13
- public class OptimizeTempVarsPass extends CompilerPass {
14
- @ Override
15
- public String getLabel () {
16
- return "Temporary Variable Reduction" ;
17
- }
18
-
19
- @ Override
20
- public Object execute (IRScope s , Object ... data ) {
21
- optimizeTmpVars (s );
22
-
23
- return null ;
24
- }
25
-
26
- @ Override
27
- public boolean invalidate (IRScope s ) {
28
- // This runs after IR is built and before CFG is built.
29
- // Not reversible in the form it is written right now.
30
- return false ;
31
- }
32
16
17
+ /**
18
+ * Takes multiple single def-use temporary variables and reduces them to share the same temp variable.
19
+ * This ends up reducing the amount of allocation and most likely helps hotspot warm up in some way quicker.
20
+ *
21
+ * This traditionally was a compiler pass (extends CompilerPass) but it is special in that it is the only
22
+ * pass which does not require any supplementary datastructures. In fact, it cannot be run by the time
23
+ * a CFG is created. So it was de-CompilerPassed and called directly.
24
+ */
25
+ public class OptimizeTempVarsPass {
33
26
private static void allocVar (Operand oldVar , IRScope s , List <TemporaryVariable > freeVarsList , Map <Operand , Operand > newVarMap ) {
34
27
// If we dont have a var mapping, get a new var -- try the free list first
35
28
// and if none available, allocate a fresh one
@@ -43,8 +36,8 @@ private static void freeVar(TemporaryVariable newVar, List<TemporaryVariable> fr
43
36
if (!freeVarsList .contains (newVar )) freeVarsList .add (0 , newVar );
44
37
}
45
38
46
- private static void optimizeTmpVars (IRScope s ) {
47
- List <Instr > instructions = new ArrayList <>(Arrays .asList (s . getClonedInstrs () ));
39
+ public static Instr [] optimizeTmpVars (IRScope s , Instr [] initialInstrs ) {
40
+ List <Instr > instructions = new ArrayList <>(Arrays .asList (initialInstrs ));
48
41
49
42
// Pass 1: Analyze instructions and find use and def count of temporary variables
50
43
Map <TemporaryVariable , Instr > tmpVarUses = new HashMap <>();
@@ -208,7 +201,7 @@ else if (i instanceof CopyInstr) {
208
201
//
209
202
// NOTE: It is sufficient to just track last use for renaming purposes.
210
203
// At the first definition, we allocate a variable which then starts the live range
211
- Map <TemporaryVariable , Integer > lastVarUseOrDef = new HashMap <TemporaryVariable , Integer >();
204
+ Map <TemporaryVariable , Integer > lastVarUseOrDef = new HashMap <>();
212
205
int iCount = -1 ;
213
206
for (Instr i : instructions ) {
214
207
iCount ++;
@@ -238,8 +231,8 @@ else if (i instanceof CopyInstr) {
238
231
// Pass 4: Reallocate temporaries based on last uses to minimize # of unique vars.
239
232
// Replace all single use operands with constants they were assigned to.
240
233
// Using operand -> operand signature because simplifyOperands works on operands
241
- Map <Operand , Operand > newVarMap = new HashMap <Operand , Operand >();
242
- List <TemporaryVariable > freeVarsList = new ArrayList <TemporaryVariable >();
234
+ Map <Operand , Operand > newVarMap = new HashMap <>();
235
+ List <TemporaryVariable > freeVarsList = new ArrayList <>();
243
236
iCount = -1 ;
244
237
s .resetTemporaryVariables ();
245
238
@@ -273,6 +266,6 @@ else if (i instanceof CopyInstr) {
273
266
274
267
Instr [] newInstrs = new Instr [instructions .size ()];
275
268
instructions .toArray (newInstrs );
276
- s . setClonedInstrs ( newInstrs ) ;
269
+ return newInstrs ;
277
270
}
278
271
}
0 commit comments