2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/mempool.h>
13 #include <mono/metadata/mempool-internals.h>
22 #define USE_ORIGINAL_VARS
23 #define CREATE_PRUNED_SSA
27 #define NEW_PHI(cfg,dest,val) do { \
28 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
29 (dest)->opcode = OP_PHI; \
30 (dest)->inst_c0 = (val); \
31 (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
40 unlink_target (MonoBasicBlock
*bb
, MonoBasicBlock
*target
)
44 for (i
= 0; i
< bb
->out_count
; i
++) {
45 if (bb
->out_bb
[i
] == target
) {
46 bb
->out_bb
[i
] = bb
->out_bb
[--bb
->out_count
];
50 for (i
= 0; i
< target
->in_count
; i
++) {
51 if (target
->in_bb
[i
] == bb
) {
52 target
->in_bb
[i
] = target
->in_bb
[--target
->in_count
];
60 unlink_unused_bblocks (MonoCompile
*cfg
)
65 g_assert (cfg
->comp_done
& MONO_COMP_REACHABILITY
);
67 if (G_UNLIKELY (cfg
->verbose_level
> 1))
68 printf ("\nUNLINK UNUSED BBLOCKS:\n");
70 for (bb
= cfg
->bb_entry
; bb
&& bb
->next_bb
;) {
71 if (!(bb
->next_bb
->flags
& BB_REACHABLE
)) {
72 bb
->next_bb
= bb
->next_bb
->next_bb
;
77 for (i
= 1; i
< cfg
->num_bblocks
; i
++) {
78 bb
= cfg
->bblocks
[i
];
80 if (!(bb
->flags
& BB_REACHABLE
)) {
81 for (j
= 0; j
< bb
->in_count
; j
++) {
82 unlink_target (bb
->in_bb
[j
], bb
);
84 for (j
= 0; j
< bb
->out_count
; j
++) {
85 unlink_target (bb
, bb
->out_bb
[j
]);
87 if (G_UNLIKELY (cfg
->verbose_level
> 1))
88 printf ("\tUnlinked BB%d\n", bb
->block_num
);
95 * remove_bb_from_phis:
97 * Remove BB from the PHI statements in TARGET.
100 remove_bb_from_phis (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoBasicBlock
*target
)
105 for (i
= 0; i
< target
->in_count
; i
++) {
106 if (target
->in_bb
[i
] == bb
) {
110 g_assert (i
< target
->in_count
);
112 for (ins
= target
->code
; ins
; ins
= ins
->next
) {
113 if (MONO_IS_PHI (ins
)) {
114 for (j
= i
; j
< ins
->inst_phi_args
[0] - 1; ++j
)
115 ins
->inst_phi_args
[j
+ 1] = ins
->inst_phi_args
[j
+ 2];
116 ins
->inst_phi_args
[0] --;
124 op_phi_to_move (int opcode
)
136 g_assert_not_reached ();
143 record_use (MonoCompile
*cfg
, MonoInst
*var
, MonoBasicBlock
*bb
, MonoInst
*ins
)
146 MonoVarUsageInfo
*ui
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoVarUsageInfo
));
148 info
= MONO_VARINFO (cfg
, var
->inst_c0
);
152 info
->uses
= g_list_prepend_mempool (cfg
->mempool
, info
->uses
, ui
);
161 * mono_ssa_rename_vars:
163 * Implement renaming of SSA variables. Also compute def-use information in parallel.
164 * @stack_history points to an area of memory which can be used for storing changes
165 * made to the stack, so they can be reverted later.
168 mono_ssa_rename_vars (MonoCompile
*cfg
, int max_vars
, MonoBasicBlock
*bb
, gboolean
*originals_used
, MonoInst
**stack
, guint32
*lvreg_stack
, gboolean
*lvreg_defined
, RenameInfo
*stack_history
, int stack_history_size
)
170 MonoInst
*ins
, *new_var
;
173 int stack_history_len
= 0;
175 if (cfg
->verbose_level
>= 4)
176 printf ("\nRENAME VARS BLOCK %d:\n", bb
->block_num
);
178 /* First pass: Create new vars */
179 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
180 const char *spec
= INS_INFO (ins
->opcode
);
182 int sregs
[MONO_MAX_SRC_REGS
];
185 printf ("\tProcessing "); mono_print_ins (ins
);
187 if (ins
->opcode
== OP_NOP
)
191 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
192 for (i
= 0; i
< num_sregs
; ++i
) {
193 if (spec
[MONO_INST_SRC1
+ i
] != ' ') {
194 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
195 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
196 int idx
= var
->inst_c0
;
198 if (var
->opcode
!= OP_ARG
)
199 g_assert (stack
[idx
]);
200 sregs
[i
] = stack
[idx
]->dreg
;
201 record_use (cfg
, stack
[idx
], bb
, ins
);
204 record_use (cfg
, var
, bb
, ins
);
206 else if (G_UNLIKELY (!var
&& lvreg_stack
[sregs
[i
]]))
207 sregs
[i
] = lvreg_stack
[sregs
[i
]];
210 mono_inst_set_src_registers (ins
, sregs
);
212 if (MONO_IS_STORE_MEMBASE (ins
)) {
213 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
214 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
215 int idx
= var
->inst_c0
;
217 if (var
->opcode
!= OP_ARG
)
218 g_assert (stack
[idx
]);
219 ins
->dreg
= stack
[idx
]->dreg
;
220 record_use (cfg
, stack
[idx
], bb
, ins
);
223 record_use (cfg
, var
, bb
, ins
);
225 else if (G_UNLIKELY (!var
&& lvreg_stack
[ins
->dreg
]))
226 ins
->dreg
= lvreg_stack
[ins
->dreg
];
230 if ((spec
[MONO_INST_DEST
] != ' ') && !MONO_IS_STORE_MEMBASE (ins
)) {
231 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
234 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
236 g_assert (idx
< max_vars
);
238 if (var
->opcode
== OP_ARG
)
239 originals_used
[idx
] = TRUE
;
242 g_assert (stack_history_len
< stack_history_size
);
243 stack_history
[stack_history_len
].var
= stack
[idx
];
244 stack_history
[stack_history_len
].idx
= idx
;
245 stack_history_len
++;
247 if (originals_used
[idx
]) {
248 new_var
= mono_compile_create_var (cfg
, var
->inst_vtype
, OP_LOCAL
);
249 new_var
->flags
= var
->flags
;
250 MONO_VARINFO (cfg
, new_var
->inst_c0
)->reg
= idx
;
252 if (cfg
->verbose_level
>= 4)
253 printf (" R%d -> R%d\n", var
->dreg
, new_var
->dreg
);
255 stack
[idx
] = new_var
;
257 ins
->dreg
= new_var
->dreg
;
262 originals_used
[idx
] = TRUE
;
265 info
= MONO_VARINFO (cfg
, var
->inst_c0
);
269 else if (G_UNLIKELY (!var
&& lvreg_defined
[ins
->dreg
] && (ins
->dreg
>= MONO_MAX_IREGS
))) {
270 /* Perform renaming for local vregs */
271 lvreg_stack
[ins
->dreg
] = mono_alloc_preg (cfg
);
272 ins
->dreg
= lvreg_stack
[ins
->dreg
];
275 lvreg_defined
[ins
->dreg
] = TRUE
;
279 printf ("\tAfter processing "); mono_print_ins (ins
);
284 /* Rename PHI arguments in succeeding bblocks */
285 for (i
= 0; i
< bb
->out_count
; i
++) {
286 MonoBasicBlock
*n
= bb
->out_bb
[i
];
288 for (j
= 0; j
< n
->in_count
; j
++)
289 if (n
->in_bb
[j
] == bb
)
292 for (ins
= n
->code
; ins
; ins
= ins
->next
) {
293 if (MONO_IS_PHI (ins
)) {
296 new_var
= stack
[idx
];
298 new_var
= cfg
->varinfo
[idx
];
300 printf ("FOUND PHI %d (%d, %d)\n", idx
, j
, new_var
->inst_c0
);
302 ins
->inst_phi_args
[j
+ 1] = new_var
->dreg
;
303 record_use (cfg
, new_var
, n
, ins
);
304 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
305 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins
->dreg
, new_var
->dreg
, n
->block_num
);
308 /* The phi nodes are at the beginning of the bblock */
314 for (tmp
= bb
->dominated
; tmp
; tmp
= tmp
->next
) {
315 mono_ssa_rename_vars (cfg
, max_vars
, (MonoBasicBlock
*)tmp
->data
, originals_used
, stack
, lvreg_stack
, lvreg_defined
, stack_history
+ stack_history_len
, stack_history_size
- stack_history_len
);
320 for (i
= stack_history_len
- 1; i
>= 0; i
--) {
321 stack
[stack_history
[i
].idx
] = stack_history
[i
].var
;
324 cfg
->comp_done
|= MONO_COMP_SSA_DEF_USE
;
328 mono_ssa_compute (MonoCompile
*cfg
)
330 int i
, j
, idx
, bitsize
;
332 MonoMethodVar
*vinfo
= g_new0 (MonoMethodVar
, cfg
->num_varinfo
);
333 MonoInst
*ins
, **stack
;
334 guint8
*buf
, *buf_start
;
335 RenameInfo
*stack_history
;
336 int stack_history_size
;
338 guint32
*lvreg_stack
;
339 gboolean
*lvreg_defined
;
341 g_assert (!(cfg
->comp_done
& MONO_COMP_SSA
));
343 /* we dont support methods containing exception clauses */
344 g_assert (mono_method_get_header (cfg
->method
)->num_clauses
== 0);
345 g_assert (!cfg
->disable_ssa
);
347 if (cfg
->verbose_level
>= 4)
348 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg
->num_varinfo
, cfg
->next_vreg
);
350 #ifdef CREATE_PRUNED_SSA
351 /* we need liveness for pruned SSA */
352 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
353 mono_analyze_liveness (cfg
);
356 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
| MONO_COMP_DFRONTIER
);
358 bitsize
= mono_bitset_alloc_size (cfg
->num_bblocks
, 0);
359 buf
= buf_start
= g_malloc0 (mono_bitset_alloc_size (cfg
->num_bblocks
, 0) * cfg
->num_varinfo
);
361 for (i
= 0; i
< cfg
->num_varinfo
; ++i
) {
362 vinfo
[i
].def_in
= mono_bitset_mem_new (buf
, cfg
->num_bblocks
, 0);
365 /* implicit reference at start */
366 if (cfg
->varinfo
[i
]->opcode
== OP_ARG
)
367 mono_bitset_set_fast (vinfo
[i
].def_in
, 0);
370 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
371 MONO_BB_FOR_EACH_INS (cfg
->bblocks
[i
], ins
) {
372 if (ins
->opcode
== OP_NOP
)
375 if (!MONO_IS_STORE_MEMBASE (ins
) && get_vreg_to_inst (cfg
, ins
->dreg
)) {
376 mono_bitset_set_fast (vinfo
[get_vreg_to_inst (cfg
, ins
->dreg
)->inst_c0
].def_in
, i
);
381 /* insert phi functions */
382 for (i
= 0; i
< cfg
->num_varinfo
; ++i
) {
383 MonoInst
*var
= cfg
->varinfo
[i
];
385 #if SIZEOF_REGISTER == 4
386 if (var
->type
== STACK_I8
&& !COMPILE_LLVM (cfg
))
389 if (var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
392 /* Most variables have only one definition */
393 if (mono_bitset_count (vinfo
[i
].def_in
) <= 1)
396 set
= mono_compile_iterated_dfrontier (cfg
, vinfo
[i
].def_in
);
398 if (cfg
->verbose_level
>= 4) {
399 if (mono_bitset_count (set
) > 0) {
400 printf ("\tR%d needs PHI functions in ", var
->dreg
);
401 mono_blockset_print (cfg
, set
, "", -1);
405 mono_bitset_foreach_bit (set
, idx
, cfg
->num_bblocks
) {
406 MonoBasicBlock
*bb
= cfg
->bblocks
[idx
];
408 /* fixme: create pruned SSA? we would need liveness information for that */
410 if (bb
== cfg
->bb_exit
&& !COMPILE_LLVM (cfg
))
413 if ((cfg
->comp_done
& MONO_COMP_LIVENESS
) && !mono_bitset_test_fast (bb
->live_in_set
, i
)) {
414 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
418 NEW_PHI (cfg
, ins
, i
);
426 ins
->opcode
= OP_PHI
;
429 ins
->opcode
= OP_FPHI
;
432 ins
->opcode
= MONO_CLASS_IS_SIMD (cfg
, var
->klass
) ? OP_XPHI
: OP_VPHI
;
436 if (var
->inst_vtype
->byref
)
437 ins
->klass
= mono_defaults
.int_class
;
439 ins
->klass
= var
->klass
;
441 ins
->inst_phi_args
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (int) * (cfg
->bblocks
[idx
]->in_count
+ 1));
442 ins
->inst_phi_args
[0] = cfg
->bblocks
[idx
]->in_count
;
445 for (j
= 0; j
< cfg
->bblocks
[idx
]->in_count
; ++j
)
446 ins
->inst_phi_args
[j
+ 1] = -1;
448 ins
->dreg
= cfg
->varinfo
[i
]->dreg
;
450 mono_bblock_insert_before_ins (bb
, bb
->code
, ins
);
453 printf ("ADD PHI BB%d %s\n", cfg
->bblocks
[idx
]->block_num
, mono_method_full_name (cfg
->method
, TRUE
));
464 stack
= alloca (sizeof (MonoInst
*) * cfg
->num_varinfo
);
465 memset (stack
, 0, sizeof (MonoInst
*) * cfg
->num_varinfo
);
467 lvreg_stack
= g_new0 (guint32
, cfg
->next_vreg
);
468 lvreg_defined
= g_new0 (gboolean
, cfg
->next_vreg
);
469 stack_history_size
= 10240;
470 stack_history
= g_new (RenameInfo
, stack_history_size
);
471 originals
= g_new0 (gboolean
, cfg
->num_varinfo
);
472 mono_ssa_rename_vars (cfg
, cfg
->num_varinfo
, cfg
->bb_entry
, originals
, stack
, lvreg_stack
, lvreg_defined
, stack_history
, stack_history_size
);
473 g_free (stack_history
);
475 g_free (lvreg_stack
);
476 g_free (lvreg_defined
);
478 if (cfg
->verbose_level
>= 4)
479 printf ("\nEND COMPUTE SSA.\n\n");
481 cfg
->comp_done
|= MONO_COMP_SSA
;
485 mono_ssa_remove (MonoCompile
*cfg
)
487 MonoInst
*ins
, *var
, *move
;
490 g_assert (cfg
->comp_done
& MONO_COMP_SSA
);
492 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
493 MonoBasicBlock
*bb
= cfg
->bblocks
[i
];
495 if (cfg
->verbose_level
>= 4)
496 printf ("\nREMOVE SSA %d:\n", bb
->block_num
);
498 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
499 if (MONO_IS_PHI (ins
)) {
500 g_assert (ins
->inst_phi_args
[0] == bb
->in_count
);
501 var
= get_vreg_to_inst (cfg
, ins
->dreg
);
503 /* Check for PHI nodes where all the inputs are the same */
504 first
= ins
->inst_phi_args
[1];
506 for (j
= 1; j
< bb
->in_count
; ++j
)
507 if (first
!= ins
->inst_phi_args
[j
+ 1])
510 if ((bb
->in_count
> 1) && (j
== bb
->in_count
)) {
511 ins
->opcode
= op_phi_to_move (ins
->opcode
);
512 if (ins
->opcode
== OP_VMOVE
)
513 g_assert (ins
->klass
);
516 for (j
= 0; j
< bb
->in_count
; j
++) {
517 MonoBasicBlock
*pred
= bb
->in_bb
[j
];
518 int sreg
= ins
->inst_phi_args
[j
+ 1];
520 if (cfg
->verbose_level
>= 4)
521 printf ("\tADD R%d <- R%d in BB%d\n", var
->dreg
, sreg
, pred
->block_num
);
522 if (var
->dreg
!= sreg
) {
523 MONO_INST_NEW (cfg
, move
, op_phi_to_move (ins
->opcode
));
524 if (move
->opcode
== OP_VMOVE
) {
525 g_assert (ins
->klass
);
526 move
->klass
= ins
->klass
;
528 move
->dreg
= var
->dreg
;
530 mono_add_ins_to_end (pred
, move
);
534 ins
->opcode
= OP_NOP
;
541 if (cfg
->verbose_level
>= 4) {
542 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
543 MonoBasicBlock
*bb
= cfg
->bblocks
[i
];
545 mono_print_bb (bb
, "AFTER REMOVE SSA:");
550 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
551 * the variables if possible. Since the newly introduced SSA variables don't
552 * have overlapping live ranges (because we don't do agressive optimization), we
553 * can coalesce them into the original variable.
556 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
557 MonoBasicBlock
*bb
= cfg
->bblocks
[i
];
559 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
560 const char *spec
= INS_INFO (ins
->opcode
);
562 int sregs
[MONO_MAX_SRC_REGS
];
564 if (ins
->opcode
== OP_NOP
)
567 if (spec
[MONO_INST_DEST
] != ' ') {
568 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
571 MonoMethodVar
*vmv
= MONO_VARINFO (cfg
, var
->inst_c0
);
574 * The third condition avoids coalescing with variables eliminated
577 if ((vmv
->reg
!= -1) && (vmv
->idx
!= vmv
->reg
) && (MONO_VARINFO (cfg
, vmv
->reg
)->reg
!= -1)) {
578 printf ("COALESCE: R%d -> R%d\n", ins
->dreg
, cfg
->varinfo
[vmv
->reg
]->dreg
);
579 ins
->dreg
= cfg
->varinfo
[vmv
->reg
]->dreg
;
584 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
585 for (j
= 0; j
< num_sregs
; ++j
) {
586 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
589 MonoMethodVar
*vmv
= MONO_VARINFO (cfg
, var
->inst_c0
);
591 if ((vmv
->reg
!= -1) && (vmv
->idx
!= vmv
->reg
) && (MONO_VARINFO (cfg
, vmv
->reg
)->reg
!= -1)) {
592 printf ("COALESCE: R%d -> R%d\n", sregs
[i
], cfg
->varinfo
[vmv
->reg
]->dreg
);
593 sregs
[i
] = cfg
->varinfo
[vmv
->reg
]->dreg
;
597 mono_inst_set_src_registers (ins
, sregs
);
601 for (i
= 0; i
< cfg
->num_varinfo
; ++i
) {
602 MONO_VARINFO (cfg
, i
)->reg
= -1;
605 if (cfg
->comp_done
& MONO_COMP_REACHABILITY
)
606 unlink_unused_bblocks (cfg
);
608 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
610 cfg
->comp_done
&= ~MONO_COMP_SSA
;
614 mono_ssa_create_def_use (MonoCompile
*cfg
)
620 g_assert (!(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
));
622 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
623 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
624 const char *spec
= INS_INFO (ins
->opcode
);
627 int sregs
[MONO_MAX_SRC_REGS
];
629 if (ins
->opcode
== OP_NOP
)
633 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
634 for (i
= 0; i
< num_sregs
; ++i
) {
635 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
636 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
637 record_use (cfg
, var
, bb
, ins
);
640 if (MONO_IS_STORE_MEMBASE (ins
)) {
641 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
642 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
643 record_use (cfg
, var
, bb
, ins
);
646 if (MONO_IS_PHI (ins
)) {
647 for (i
= ins
->inst_phi_args
[0]; i
> 0; i
--) {
648 g_assert (ins
->inst_phi_args
[i
] != -1);
649 record_use (cfg
, get_vreg_to_inst (cfg
, ins
->inst_phi_args
[i
]), bb
, ins
);
654 if ((spec
[MONO_INST_DEST
] != ' ') && !MONO_IS_STORE_MEMBASE (ins
)) {
655 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
657 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
658 info
= MONO_VARINFO (cfg
, var
->inst_c0
);
666 cfg
->comp_done
|= MONO_COMP_SSA_DEF_USE
;
670 mono_ssa_copyprop (MonoCompile
*cfg
)
675 g_assert ((cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
));
677 for (index
= 0; index
< cfg
->num_varinfo
; ++index
) {
678 MonoInst
*var
= cfg
->varinfo
[index
];
679 MonoMethodVar
*info
= MONO_VARINFO (cfg
, index
);
681 if (info
->def
&& (MONO_IS_MOVE (info
->def
))) {
682 MonoInst
*var2
= get_vreg_to_inst (cfg
, info
->def
->sreg1
);
684 if (var2
&& !(var2
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)) && MONO_VARINFO (cfg
, var2
->inst_c0
)->def
&& (!MONO_IS_PHI (MONO_VARINFO (cfg
, var2
->inst_c0
)->def
))) {
685 /* Rewrite all uses of var to be uses of var2 */
686 int dreg
= var
->dreg
;
687 int sreg1
= var2
->dreg
;
692 MonoVarUsageInfo
*u
= (MonoVarUsageInfo
*)l
->data
;
693 MonoInst
*ins
= u
->inst
;
694 GList
*next
= l
->next
;
696 int sregs
[MONO_MAX_SRC_REGS
];
698 spec
= INS_INFO (ins
->opcode
);
700 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
701 for (i
= 0; i
< num_sregs
; ++i
) {
702 if (sregs
[i
] == dreg
)
706 g_assert (sregs
[i
] == dreg
);
708 mono_inst_set_src_registers (ins
, sregs
);
709 } else if (MONO_IS_STORE_MEMBASE (ins
) && ins
->dreg
== dreg
) {
711 } else if (MONO_IS_PHI (ins
)) {
712 for (i
= ins
->inst_phi_args
[0]; i
> 0; i
--) {
713 int sreg
= ins
->inst_phi_args
[i
];
714 if (sreg
== var
->dreg
)
718 ins
->inst_phi_args
[i
] = sreg1
;
721 g_assert_not_reached ();
723 record_use (cfg
, var2
, u
->bb
, ins
);
733 if (cfg
->verbose_level
>= 4) {
736 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
737 mono_print_bb (bb
, "AFTER SSA COPYPROP");
742 evaluate_ins (MonoCompile
*cfg
, MonoInst
*ins
, MonoInst
**res
, MonoInst
**carray
)
744 MonoInst
*args
[MONO_MAX_SRC_REGS
];
745 int rs
[MONO_MAX_SRC_REGS
];
747 gboolean const_args
= TRUE
;
748 const char *spec
= INS_INFO (ins
->opcode
);
750 int sregs
[MONO_MAX_SRC_REGS
];
752 /* Short-circuit this */
753 if (ins
->opcode
== OP_ICONST
) {
758 if (ins
->opcode
== OP_NOP
)
761 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
762 for (i
= 0; i
< MONO_MAX_SRC_REGS
; ++i
)
764 for (i
= 0; i
< num_sregs
; ++i
) {
765 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
768 args
[i
] = carray
[sregs
[i
]];
771 else if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
772 rs
[i
] = MONO_VARINFO (cfg
, var
->inst_c0
)->cpstate
;
779 if (num_sregs
> 0 && const_args
) {
780 g_assert (num_sregs
<= 2);
781 if ((spec
[MONO_INST_DEST
] != ' ') && carray
[ins
->dreg
]) {
783 *res
= carray
[ins
->dreg
];
786 c0
= mono_constant_fold_ins (cfg
, ins
, args
[0], args
[1], FALSE
);
788 if (G_UNLIKELY (cfg
->verbose_level
> 1)) {
789 printf ("\t cfold -> ");
796 /* Can't cfold this ins */
802 for (i
= 0; i
< num_sregs
; ++i
) {
810 change_varstate (MonoCompile
*cfg
, GList
**cvars
, MonoMethodVar
*info
, int state
, MonoInst
*c0
, MonoInst
**carray
)
812 if (info
->cpstate
>= state
)
815 info
->cpstate
= state
;
817 if (G_UNLIKELY (cfg
->verbose_level
> 1))
818 printf ("\tState of R%d set to %d\n", cfg
->varinfo
[info
->idx
]->dreg
, info
->cpstate
);
823 carray
[cfg
->varinfo
[info
->idx
]->dreg
] = c0
;
825 if (!g_list_find (*cvars
, info
)) {
826 *cvars
= g_list_prepend (*cvars
, info
);
831 add_cprop_bb (MonoCompile
*cfg
, MonoBasicBlock
*bb
, GList
**bblist
)
833 if (G_UNLIKELY (cfg
->verbose_level
> 1))
834 printf ("\tAdd BB%d to worklist\n", bb
->block_num
);
836 if (!(bb
->flags
& BB_REACHABLE
)) {
837 bb
->flags
|= BB_REACHABLE
;
838 *bblist
= g_list_prepend (*bblist
, bb
);
843 visit_inst (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, GList
**cvars
, GList
**bblist
, MonoInst
**carray
)
845 const char *spec
= INS_INFO (ins
->opcode
);
847 if (ins
->opcode
== OP_NOP
)
850 if (cfg
->verbose_level
> 1)
851 mono_print_ins (ins
);
853 /* FIXME: Support longs/floats */
854 /* FIXME: Work on vregs as well */
856 if (MONO_IS_PHI (ins
)) {
857 MonoMethodVar
*info
= MONO_VARINFO (cfg
, get_vreg_to_inst (cfg
, ins
->dreg
)->inst_c0
);
861 for (j
= 1; j
<= ins
->inst_phi_args
[0]; j
++) {
862 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->inst_phi_args
[j
]);
863 MonoMethodVar
*mv
= MONO_VARINFO (cfg
, var
->inst_c0
);
864 MonoInst
*src
= mv
->def
;
866 if (mv
->def_bb
&& !(mv
->def_bb
->flags
& BB_REACHABLE
))
869 if (!mv
->def
|| !src
|| mv
->cpstate
== 2) {
870 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
874 if (mv
->cpstate
== 0)
877 g_assert (carray
[var
->dreg
]);
880 c0
= carray
[var
->dreg
];
883 if (c0
->opcode
!= OP_ICONST
) {
884 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
888 if (carray
[var
->dreg
]->inst_c0
!= c0
->inst_c0
) {
889 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
894 if (c0
&& info
->cpstate
< 1) {
895 change_varstate (cfg
, cvars
, info
, 1, c0
, carray
);
897 g_assert (c0
->opcode
== OP_ICONST
);
900 else if (!MONO_IS_STORE_MEMBASE (ins
) && ((spec
[MONO_INST_SRC1
] != ' ') || (spec
[MONO_INST_SRC2
] != ' ') || (spec
[MONO_INST_DEST
] != ' '))) {
904 if (spec
[MONO_INST_DEST
] != ' ')
905 var
= get_vreg_to_inst (cfg
, ins
->dreg
);
910 state
= evaluate_ins (cfg
, ins
, &c0
, carray
);
912 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
913 MonoMethodVar
*info
= MONO_VARINFO (cfg
, var
->inst_c0
);
915 if (info
->cpstate
< 2) {
917 change_varstate (cfg
, cvars
, info
, 1, c0
, carray
);
919 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
922 else if (!var
&& (ins
->dreg
!= -1)) {
924 * We don't record def-use information for local vregs since it would be
925 * expensive. Instead, we depend on the fact that all uses of the vreg are in
926 * the same bblock, so they will be examined after the definition.
927 * FIXME: This isn't true if the ins is visited through an SSA edge.
930 carray
[ins
->dreg
] = c0
;
932 if (carray
[ins
->dreg
]) {
934 * The state of the vreg changed from constant to non-constant
935 * -> need to rescan the whole bblock.
937 carray
[ins
->dreg
] = NULL
;
938 /* FIXME: Speed this up */
940 if (!g_list_find (*bblist
, bb
))
941 *bblist
= g_list_prepend (*bblist
, bb
);
946 if (MONO_IS_JUMP_TABLE (ins
)) {
948 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (ins
);
950 if (!ins
->next
|| ins
->next
->opcode
!= OP_PADD
) {
951 /* The PADD was optimized away */
952 /* FIXME: handle this as well */
953 for (i
= 0; i
< table
->table_size
; i
++)
954 if (table
->table
[i
])
955 add_cprop_bb (cfg
, table
->table
[i
], bblist
);
959 g_assert (ins
->next
->opcode
== OP_PADD
);
960 g_assert (ins
->next
->sreg1
== ins
->dreg
);
962 if (carray
[ins
->next
->sreg2
]) {
963 #if SIZEOF_REGISTER == 8
964 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 3;
966 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 2;
968 if ((idx
< 0) || (idx
>= table
->table_size
))
969 /* Out-of-range, no branch is executed */
972 if (table
->table
[idx
])
973 add_cprop_bb (cfg
, table
->table
[idx
], bblist
);
976 for (i
= 0; i
< table
->table_size
; i
++)
977 if (table
->table
[i
])
978 add_cprop_bb (cfg
, table
->table
[i
], bblist
);
982 if (ins
->opcode
== OP_SWITCH
) {
984 MonoJumpInfoBBTable
*table
= ins
->inst_p0
;
986 for (i
= 0; i
< table
->table_size
; i
++)
987 if (table
->table
[i
])
988 add_cprop_bb (cfg
, table
->table
[i
], bblist
);
991 /* Handle COMPARE+BRCOND pairs */
992 if (ins
->next
&& MONO_IS_COND_BRANCH_OP (ins
->next
)) {
994 g_assert (c0
->opcode
== OP_ICONST
);
997 ins
->next
->flags
|= MONO_INST_CFOLD_TAKEN
;
999 ins
->next
->flags
|= MONO_INST_CFOLD_NOT_TAKEN
;
1002 ins
->next
->flags
&= ~(MONO_INST_CFOLD_TAKEN
| MONO_INST_CFOLD_NOT_TAKEN
);
1005 visit_inst (cfg
, bb
, ins
->next
, cvars
, bblist
, carray
);
1007 } else if (ins
->opcode
== OP_BR
) {
1008 add_cprop_bb (cfg
, ins
->inst_target_bb
, bblist
);
1009 } else if (MONO_IS_COND_BRANCH_OP (ins
)) {
1010 if (ins
->flags
& MONO_INST_CFOLD_TAKEN
) {
1011 add_cprop_bb (cfg
, ins
->inst_true_bb
, bblist
);
1012 } else if (ins
->flags
& MONO_INST_CFOLD_NOT_TAKEN
) {
1013 if (ins
->inst_false_bb
)
1014 add_cprop_bb (cfg
, ins
->inst_false_bb
, bblist
);
1016 add_cprop_bb (cfg
, ins
->inst_true_bb
, bblist
);
1017 if (ins
->inst_false_bb
)
1018 add_cprop_bb (cfg
, ins
->inst_false_bb
, bblist
);
1026 * Replace INS with its constant value, if it exists
1029 fold_ins (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
**carray
)
1031 const char *spec
= INS_INFO (ins
->opcode
);
1033 int num_sregs
= mono_inst_get_num_src_registers (ins
);
1035 if ((ins
->opcode
!= OP_NOP
) && (ins
->dreg
!= -1) && !MONO_IS_STORE_MEMBASE (ins
)) {
1036 if (carray
[ins
->dreg
] && (spec
[MONO_INST_DEST
] == 'i') && (ins
->dreg
>= MONO_MAX_IREGS
)) {
1037 /* Perform constant folding */
1039 g_assert (carray
[ins
->dreg
]->opcode
== OP_ICONST
);
1040 ins
->opcode
= OP_ICONST
;
1041 ins
->inst_c0
= carray
[ins
->dreg
]->inst_c0
;
1042 MONO_INST_NULLIFY_SREGS (ins
);
1043 } else if (num_sregs
== 2 && carray
[ins
->sreg2
]) {
1044 /* Perform op->op_imm conversion */
1045 opcode2
= mono_op_to_op_imm (ins
->opcode
);
1046 if (opcode2
!= -1) {
1047 ins
->opcode
= opcode2
;
1048 ins
->inst_imm
= carray
[ins
->sreg2
]->inst_c0
;
1051 if ((opcode2
== OP_VOIDCALL
) || (opcode2
== OP_CALL
) || (opcode2
== OP_LCALL
) || (opcode2
== OP_FCALL
))
1052 ((MonoCallInst
*)ins
)->fptr
= (gpointer
)ins
->inst_imm
;
1055 /* FIXME: Handle 3 op insns */
1058 if (MONO_IS_JUMP_TABLE (ins
)) {
1060 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (ins
);
1062 if (!ins
->next
|| ins
->next
->opcode
!= OP_PADD
) {
1063 /* The PADD was optimized away */
1064 /* FIXME: handle this as well */
1068 g_assert (ins
->next
->opcode
== OP_PADD
);
1069 g_assert (ins
->next
->sreg1
== ins
->dreg
);
1070 g_assert (ins
->next
->next
->opcode
== OP_LOAD_MEMBASE
);
1072 if (carray
[ins
->next
->sreg2
]) {
1073 /* Convert to a simple branch */
1074 #if SIZEOF_REGISTER == 8
1075 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 3;
1077 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 2;
1080 if (!((idx
>= 0) && (idx
< table
->table_size
))) {
1081 /* Out of range, eliminate the whole switch */
1082 for (i
= 0; i
< table
->table_size
; ++i
) {
1083 remove_bb_from_phis (cfg
, bb
, table
->table
[i
]);
1084 mono_unlink_bblock (cfg
, bb
, table
->table
[i
]);
1088 NULLIFY_INS (ins
->next
);
1089 NULLIFY_INS (ins
->next
->next
);
1090 if (ins
->next
->next
->next
)
1091 NULLIFY_INS (ins
->next
->next
->next
);
1096 if (!ins
->next
->next
->next
|| ins
->next
->next
->next
->opcode
!= OP_BR_REG
) {
1097 /* A one-way switch which got optimized away */
1098 if (G_UNLIKELY (cfg
->verbose_level
> 1)) {
1099 printf ("\tNo cfold on ");
1100 mono_print_ins (ins
);
1105 if (G_UNLIKELY (cfg
->verbose_level
> 1)) {
1106 printf ("\tcfold on ");
1107 mono_print_ins (ins
);
1110 /* Unlink target bblocks */
1111 for (i
= 0; i
< table
->table_size
; ++i
) {
1113 remove_bb_from_phis (cfg
, bb
, table
->table
[i
]);
1114 mono_unlink_bblock (cfg
, bb
, table
->table
[i
]);
1118 /* Change the OP_BR_REG to a simple branch */
1119 ins
->next
->next
->next
->opcode
= OP_BR
;
1120 ins
->next
->next
->next
->inst_target_bb
= table
->table
[idx
];
1121 ins
->next
->next
->next
->sreg1
= -1;
1123 /* Nullify the other instructions */
1125 NULLIFY_INS (ins
->next
);
1126 NULLIFY_INS (ins
->next
->next
);
1130 else if (MONO_IS_COND_BRANCH_OP (ins
)) {
1131 if (ins
->flags
& MONO_INST_CFOLD_TAKEN
) {
1132 remove_bb_from_phis (cfg
, bb
, ins
->inst_false_bb
);
1133 mono_unlink_bblock (cfg
, bb
, ins
->inst_false_bb
);
1134 ins
->opcode
= OP_BR
;
1135 ins
->inst_target_bb
= ins
->inst_true_bb
;
1136 } else if (ins
->flags
& MONO_INST_CFOLD_NOT_TAKEN
) {
1137 remove_bb_from_phis (cfg
, bb
, ins
->inst_true_bb
);
1138 mono_unlink_bblock (cfg
, bb
, ins
->inst_true_bb
);
1139 ins
->opcode
= OP_BR
;
1140 ins
->inst_target_bb
= ins
->inst_false_bb
;
1146 mono_ssa_cprop (MonoCompile
*cfg
)
1150 GList
*bblock_list
, *cvars
;
1153 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1155 carray
= g_new0 (MonoInst
*, cfg
->next_vreg
);
1157 if (!(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
))
1158 mono_ssa_create_def_use (cfg
);
1160 bblock_list
= g_list_prepend (NULL
, cfg
->bb_entry
);
1161 cfg
->bb_entry
->flags
|= BB_REACHABLE
;
1163 memset (carray
, 0, sizeof (MonoInst
*) * cfg
->num_varinfo
);
1165 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1166 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1173 while (bblock_list
) {
1176 bb
= (MonoBasicBlock
*)bblock_list
->data
;
1178 bblock_list
= g_list_delete_link (bblock_list
, bblock_list
);
1180 g_assert (bb
->flags
& BB_REACHABLE
);
1182 if (bb
->out_count
== 1) {
1183 if (!(bb
->out_bb
[0]->flags
& BB_REACHABLE
)) {
1184 bb
->out_bb
[0]->flags
|= BB_REACHABLE
;
1185 bblock_list
= g_list_prepend (bblock_list
, bb
->out_bb
[0]);
1189 if (cfg
->verbose_level
> 1)
1190 printf ("\nSSA CONSPROP BB%d:\n", bb
->block_num
);
1192 for (inst
= bb
->code
; inst
; inst
= inst
->next
) {
1193 visit_inst (cfg
, bb
, inst
, &cvars
, &bblock_list
, carray
);
1197 MonoMethodVar
*info
= (MonoMethodVar
*)cvars
->data
;
1198 cvars
= g_list_delete_link (cvars
, cvars
);
1200 for (tmp
= info
->uses
; tmp
; tmp
= tmp
->next
) {
1201 MonoVarUsageInfo
*ui
= (MonoVarUsageInfo
*)tmp
->data
;
1202 if (!(ui
->bb
->flags
& BB_REACHABLE
))
1204 visit_inst (cfg
, ui
->bb
, ui
->inst
, &cvars
, &bblock_list
, carray
);
1209 for (bb
= cfg
->bb_entry
->next_bb
; bb
; bb
= bb
->next_bb
) {
1211 for (inst
= bb
->code
; inst
; inst
= inst
->next
) {
1212 fold_ins (cfg
, bb
, inst
, carray
);
1218 cfg
->comp_done
|= MONO_COMP_REACHABILITY
;
1220 /* fixme: we should update usage infos during cprop, instead of computing it again */
1221 cfg
->comp_done
&= ~MONO_COMP_SSA_DEF_USE
;
1222 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1223 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1230 add_to_dce_worklist (MonoCompile
*cfg
, MonoMethodVar
*var
, MonoMethodVar
*use
, GList
**wl
)
1234 *wl
= g_list_prepend_mempool (cfg
->mempool
, *wl
, use
);
1236 for (tmp
= use
->uses
; tmp
; tmp
= tmp
->next
) {
1237 MonoVarUsageInfo
*ui
= (MonoVarUsageInfo
*)tmp
->data
;
1238 if (ui
->inst
== var
->def
) {
1239 /* from the mempool */
1240 use
->uses
= g_list_remove_link (use
->uses
, tmp
);
1247 mono_ssa_deadce (MonoCompile
*cfg
)
1252 g_assert (cfg
->comp_done
& MONO_COMP_SSA
);
1254 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1256 if (!(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
))
1257 mono_ssa_create_def_use (cfg
);
1259 mono_ssa_copyprop (cfg
);
1262 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1263 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1264 work_list
= g_list_prepend_mempool (cfg
->mempool
, work_list
, info
);
1268 MonoMethodVar
*info
= (MonoMethodVar
*)work_list
->data
;
1269 work_list
= g_list_remove_link (work_list
, work_list
);
1272 * The second part of the condition happens often when PHI nodes have their dreg
1273 * as one of their arguments due to the fact that we use the original vars.
1275 if (info
->def
&& (!info
->uses
|| ((info
->uses
->next
== NULL
) && (((MonoVarUsageInfo
*)info
->uses
->data
)->inst
== info
->def
)))) {
1276 MonoInst
*def
= info
->def
;
1278 /* Eliminating FMOVE could screw up the fp stack */
1279 if (MONO_IS_MOVE (def
) && (!MONO_ARCH_USE_FPSTACK
|| (def
->opcode
!= OP_FMOVE
))) {
1280 MonoInst
*src_var
= get_vreg_to_inst (cfg
, def
->sreg1
);
1281 if (src_var
&& !(src_var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
1282 add_to_dce_worklist (cfg
, info
, MONO_VARINFO (cfg
, src_var
->inst_c0
), &work_list
);
1283 def
->opcode
= OP_NOP
;
1284 def
->dreg
= def
->sreg1
= def
->sreg2
= -1;
1286 } else if ((def
->opcode
== OP_ICONST
) || (def
->opcode
== OP_I8CONST
) || MONO_IS_ZERO (def
)) {
1287 def
->opcode
= OP_NOP
;
1288 def
->dreg
= def
->sreg1
= def
->sreg2
= -1;
1290 } else if (MONO_IS_PHI (def
)) {
1292 for (j
= def
->inst_phi_args
[0]; j
> 0; j
--) {
1293 MonoMethodVar
*u
= MONO_VARINFO (cfg
, get_vreg_to_inst (cfg
, def
->inst_phi_args
[j
])->inst_c0
);
1294 add_to_dce_worklist (cfg
, info
, u
, &work_list
);
1296 def
->opcode
= OP_NOP
;
1297 def
->dreg
= def
->sreg1
= def
->sreg2
= -1;
1300 else if (def
->opcode
== OP_NOP
) {
1303 //mono_print_ins (def);
1311 mono_ssa_strength_reduction (MonoCompile
*cfg
)
1316 g_assert (cfg
->comp_done
& MONO_COMP_SSA
);
1317 g_assert (cfg
->comp_done
& MONO_COMP_LOOPS
);
1318 g_assert (cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
);
1320 for (bb
= cfg
->bb_entry
->next_bb
; bb
; bb
= bb
->next_bb
) {
1321 GList
*lp
= bb
->loop_blocks
;
1324 MonoBasicBlock
*h
= (MonoBasicBlock
*)lp
->data
;
1326 /* we only consider loops with 2 in bblocks */
1327 if (!h
->in_count
== 2)
1330 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1331 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1333 if (info
->def
&& info
->def
->ssa_op
== MONO_SSA_STORE
&&
1334 info
->def
->inst_i0
->opcode
== OP_LOCAL
&& g_list_find (lp
, info
->def_bb
)) {
1335 MonoInst
*v
= info
->def
->inst_i1
;
1338 printf ("FOUND %d in %s\n", info
->idx
, mono_method_full_name (cfg
->method
, TRUE
));
1346 #endif /* DISABLE_JIT */