2 * ssa.c: Static single assign form support for the JIT compiler.
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2003 Ximian, Inc.
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mempool.h>
14 #include <mono/metadata/mempool-internals.h>
23 #define CREATE_PRUNED_SSA
27 #define NEW_PHI(cfg,dest,val) do { \
28 MONO_INST_NEW ((cfg), (dest), OP_PHI); \
29 (dest)->inst_c0 = (val); \
38 unlink_target (MonoBasicBlock
*bb
, MonoBasicBlock
*target
)
42 for (i
= 0; i
< bb
->out_count
; i
++) {
43 if (bb
->out_bb
[i
] == target
) {
44 bb
->out_bb
[i
] = bb
->out_bb
[--bb
->out_count
];
48 for (i
= 0; i
< target
->in_count
; i
++) {
49 if (target
->in_bb
[i
] == bb
) {
50 target
->in_bb
[i
] = target
->in_bb
[--target
->in_count
];
58 unlink_unused_bblocks (MonoCompile
*cfg
)
63 g_assert (cfg
->comp_done
& MONO_COMP_REACHABILITY
);
65 if (G_UNLIKELY (cfg
->verbose_level
> 1))
66 printf ("\nUNLINK UNUSED BBLOCKS:\n");
68 for (bb
= cfg
->bb_entry
; bb
&& bb
->next_bb
;) {
69 if (!(bb
->next_bb
->flags
& BB_REACHABLE
)) {
70 bb
->next_bb
= bb
->next_bb
->next_bb
;
75 for (i
= 1; i
< cfg
->num_bblocks
; i
++) {
76 bb
= cfg
->bblocks
[i
];
78 if (!(bb
->flags
& BB_REACHABLE
)) {
79 for (j
= 0; j
< bb
->in_count
; j
++) {
80 unlink_target (bb
->in_bb
[j
], bb
);
82 for (j
= 0; j
< bb
->out_count
; j
++) {
83 unlink_target (bb
, bb
->out_bb
[j
]);
85 if (G_UNLIKELY (cfg
->verbose_level
> 1))
86 printf ("\tUnlinked BB%d\n", bb
->block_num
);
93 * remove_bb_from_phis:
95 * Remove BB from the PHI statements in TARGET.
98 remove_bb_from_phis (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoBasicBlock
*target
)
103 for (i
= 0; i
< target
->in_count
; i
++) {
104 if (target
->in_bb
[i
] == bb
) {
108 g_assert (i
< target
->in_count
);
110 for (ins
= target
->code
; ins
; ins
= ins
->next
) {
111 if (MONO_IS_PHI (ins
)) {
112 for (j
= i
; j
< ins
->inst_phi_args
[0] - 1; ++j
)
113 ins
->inst_phi_args
[j
+ 1] = ins
->inst_phi_args
[j
+ 2];
114 ins
->inst_phi_args
[0] --;
122 op_phi_to_move (int opcode
)
134 g_assert_not_reached ();
141 record_use (MonoCompile
*cfg
, MonoInst
*var
, MonoBasicBlock
*bb
, MonoInst
*ins
)
144 MonoVarUsageInfo
*ui
= mono_mempool_alloc (cfg
->mempool
, sizeof (MonoVarUsageInfo
));
146 info
= MONO_VARINFO (cfg
, var
->inst_c0
);
150 info
->uses
= g_list_prepend_mempool (cfg
->mempool
, info
->uses
, ui
);
159 * mono_ssa_rename_vars:
161 * Implement renaming of SSA variables. Also compute def-use information in parallel.
162 * @stack_history points to an area of memory which can be used for storing changes
163 * made to the stack, so they can be reverted later.
166 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
)
168 MonoInst
*ins
, *new_var
;
171 int stack_history_len
= 0;
173 if (cfg
->verbose_level
>= 4)
174 printf ("\nRENAME VARS BLOCK %d:\n", bb
->block_num
);
176 /* First pass: Create new vars */
177 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
178 const char *spec
= INS_INFO (ins
->opcode
);
180 int sregs
[MONO_MAX_SRC_REGS
];
183 printf ("\tProcessing "); mono_print_ins (ins
);
185 if (ins
->opcode
== OP_NOP
)
189 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
190 for (i
= 0; i
< num_sregs
; ++i
) {
191 if (spec
[MONO_INST_SRC1
+ i
] != ' ') {
192 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
193 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
194 int idx
= var
->inst_c0
;
196 if (var
->opcode
!= OP_ARG
)
197 g_assert (stack
[idx
]);
198 sregs
[i
] = stack
[idx
]->dreg
;
199 record_use (cfg
, stack
[idx
], bb
, ins
);
202 record_use (cfg
, var
, bb
, ins
);
204 else if (G_UNLIKELY (!var
&& lvreg_stack
[sregs
[i
]]))
205 sregs
[i
] = lvreg_stack
[sregs
[i
]];
208 mono_inst_set_src_registers (ins
, sregs
);
210 if (MONO_IS_STORE_MEMBASE (ins
)) {
211 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
212 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
213 int idx
= var
->inst_c0
;
215 if (var
->opcode
!= OP_ARG
)
216 g_assert (stack
[idx
]);
217 ins
->dreg
= stack
[idx
]->dreg
;
218 record_use (cfg
, stack
[idx
], bb
, ins
);
221 record_use (cfg
, var
, bb
, ins
);
223 else if (G_UNLIKELY (!var
&& lvreg_stack
[ins
->dreg
]))
224 ins
->dreg
= lvreg_stack
[ins
->dreg
];
228 if ((spec
[MONO_INST_DEST
] != ' ') && !MONO_IS_STORE_MEMBASE (ins
)) {
229 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
232 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
234 g_assert (idx
< max_vars
);
236 if (var
->opcode
== OP_ARG
)
237 originals_used
[idx
] = TRUE
;
240 g_assert (stack_history_len
< stack_history_size
);
241 stack_history
[stack_history_len
].var
= stack
[idx
];
242 stack_history
[stack_history_len
].idx
= idx
;
243 stack_history_len
++;
245 if (originals_used
[idx
]) {
246 new_var
= mono_compile_create_var (cfg
, var
->inst_vtype
, OP_LOCAL
);
247 new_var
->flags
= var
->flags
;
248 MONO_VARINFO (cfg
, new_var
->inst_c0
)->reg
= idx
;
250 if (cfg
->verbose_level
>= 4)
251 printf (" R%d -> R%d\n", var
->dreg
, new_var
->dreg
);
253 stack
[idx
] = new_var
;
255 ins
->dreg
= new_var
->dreg
;
260 originals_used
[idx
] = TRUE
;
263 info
= MONO_VARINFO (cfg
, var
->inst_c0
);
267 else if (G_UNLIKELY (!var
&& lvreg_defined
[ins
->dreg
] && (ins
->dreg
>= MONO_MAX_IREGS
))) {
268 /* Perform renaming for local vregs */
269 lvreg_stack
[ins
->dreg
] = vreg_is_ref (cfg
, ins
->dreg
) ? mono_alloc_ireg_ref (cfg
) : mono_alloc_preg (cfg
);
270 ins
->dreg
= lvreg_stack
[ins
->dreg
];
273 lvreg_defined
[ins
->dreg
] = TRUE
;
277 printf ("\tAfter processing "); mono_print_ins (ins
);
282 /* Rename PHI arguments in succeeding bblocks */
283 for (i
= 0; i
< bb
->out_count
; i
++) {
284 MonoBasicBlock
*n
= bb
->out_bb
[i
];
286 for (j
= 0; j
< n
->in_count
; j
++)
287 if (n
->in_bb
[j
] == bb
)
290 for (ins
= n
->code
; ins
; ins
= ins
->next
) {
291 if (MONO_IS_PHI (ins
)) {
294 new_var
= stack
[idx
];
296 new_var
= cfg
->varinfo
[idx
];
298 printf ("FOUND PHI %d (%d, %d)\n", idx
, j
, new_var
->inst_c0
);
300 ins
->inst_phi_args
[j
+ 1] = new_var
->dreg
;
301 record_use (cfg
, new_var
, n
, ins
);
302 if (G_UNLIKELY (cfg
->verbose_level
>= 4))
303 printf ("\tAdd PHI R%d <- R%d to BB%d\n", ins
->dreg
, new_var
->dreg
, n
->block_num
);
306 /* The phi nodes are at the beginning of the bblock */
312 for (tmp
= bb
->dominated
; tmp
; tmp
= tmp
->next
) {
313 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
);
318 for (i
= stack_history_len
- 1; i
>= 0; i
--) {
319 stack
[stack_history
[i
].idx
] = stack_history
[i
].var
;
322 cfg
->comp_done
|= MONO_COMP_SSA_DEF_USE
;
326 mono_ssa_compute (MonoCompile
*cfg
)
328 int i
, j
, idx
, bitsize
;
330 MonoMethodVar
*vinfo
= g_new0 (MonoMethodVar
, cfg
->num_varinfo
);
331 MonoInst
*ins
, **stack
;
332 guint8
*buf
, *buf_start
;
333 RenameInfo
*stack_history
;
334 int stack_history_size
;
336 guint32
*lvreg_stack
;
337 gboolean
*lvreg_defined
;
339 g_assert (!(cfg
->comp_done
& MONO_COMP_SSA
));
341 g_assert (!cfg
->disable_ssa
);
343 if (cfg
->verbose_level
>= 4)
344 printf ("\nCOMPUTE SSA %d (R%d-)\n\n", cfg
->num_varinfo
, cfg
->next_vreg
);
346 #ifdef CREATE_PRUNED_SSA
347 /* we need liveness for pruned SSA */
348 if (!(cfg
->comp_done
& MONO_COMP_LIVENESS
))
349 mono_analyze_liveness (cfg
);
352 mono_compile_dominator_info (cfg
, MONO_COMP_DOM
| MONO_COMP_IDOM
| MONO_COMP_DFRONTIER
);
354 bitsize
= mono_bitset_alloc_size (cfg
->num_bblocks
, 0);
355 buf
= buf_start
= g_malloc0 (mono_bitset_alloc_size (cfg
->num_bblocks
, 0) * cfg
->num_varinfo
);
357 for (i
= 0; i
< cfg
->num_varinfo
; ++i
) {
358 vinfo
[i
].def_in
= mono_bitset_mem_new (buf
, cfg
->num_bblocks
, 0);
361 /* implicit reference at start */
362 if (cfg
->varinfo
[i
]->opcode
== OP_ARG
)
363 mono_bitset_set_fast (vinfo
[i
].def_in
, 0);
366 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
367 MONO_BB_FOR_EACH_INS (cfg
->bblocks
[i
], ins
) {
368 if (ins
->opcode
== OP_NOP
)
371 if (!MONO_IS_STORE_MEMBASE (ins
) && get_vreg_to_inst (cfg
, ins
->dreg
)) {
372 mono_bitset_set_fast (vinfo
[get_vreg_to_inst (cfg
, ins
->dreg
)->inst_c0
].def_in
, i
);
377 /* insert phi functions */
378 for (i
= 0; i
< cfg
->num_varinfo
; ++i
) {
379 MonoInst
*var
= cfg
->varinfo
[i
];
381 #if SIZEOF_REGISTER == 4
382 if (var
->type
== STACK_I8
&& !COMPILE_LLVM (cfg
))
385 if (var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))
388 /* Most variables have only one definition */
389 if (mono_bitset_count (vinfo
[i
].def_in
) <= 1)
392 set
= mono_compile_iterated_dfrontier (cfg
, vinfo
[i
].def_in
);
394 if (cfg
->verbose_level
>= 4) {
395 if (mono_bitset_count (set
) > 0) {
396 printf ("\tR%d needs PHI functions in ", var
->dreg
);
397 mono_blockset_print (cfg
, set
, "", -1);
401 mono_bitset_foreach_bit (set
, idx
, cfg
->num_bblocks
) {
402 MonoBasicBlock
*bb
= cfg
->bblocks
[idx
];
404 /* fixme: create pruned SSA? we would need liveness information for that */
406 if (bb
== cfg
->bb_exit
&& !COMPILE_LLVM (cfg
))
409 if ((cfg
->comp_done
& MONO_COMP_LIVENESS
) && !mono_bitset_test_fast (bb
->live_in_set
, i
)) {
410 //printf ("%d is not live in BB%d %s\n", i, bb->block_num, mono_method_full_name (cfg->method, TRUE));
414 NEW_PHI (cfg
, ins
, i
);
422 ins
->opcode
= OP_PHI
;
425 ins
->opcode
= OP_FPHI
;
428 ins
->opcode
= MONO_CLASS_IS_SIMD (cfg
, var
->klass
) ? OP_XPHI
: OP_VPHI
;
432 if (var
->inst_vtype
->byref
)
433 ins
->klass
= mono_defaults
.int_class
;
435 ins
->klass
= var
->klass
;
437 ins
->inst_phi_args
= mono_mempool_alloc0 (cfg
->mempool
, sizeof (int) * (cfg
->bblocks
[idx
]->in_count
+ 1));
438 ins
->inst_phi_args
[0] = cfg
->bblocks
[idx
]->in_count
;
441 for (j
= 0; j
< cfg
->bblocks
[idx
]->in_count
; ++j
)
442 ins
->inst_phi_args
[j
+ 1] = -1;
444 ins
->dreg
= cfg
->varinfo
[i
]->dreg
;
446 mono_bblock_insert_before_ins (bb
, bb
->code
, ins
);
449 printf ("ADD PHI BB%d %s\n", cfg
->bblocks
[idx
]->block_num
, mono_method_full_name (cfg
->method
, TRUE
));
460 stack
= alloca (sizeof (MonoInst
*) * cfg
->num_varinfo
);
461 memset (stack
, 0, sizeof (MonoInst
*) * cfg
->num_varinfo
);
463 lvreg_stack
= g_new0 (guint32
, cfg
->next_vreg
);
464 lvreg_defined
= g_new0 (gboolean
, cfg
->next_vreg
);
465 stack_history_size
= 10240;
466 stack_history
= g_new (RenameInfo
, stack_history_size
);
467 originals
= g_new0 (gboolean
, cfg
->num_varinfo
);
468 mono_ssa_rename_vars (cfg
, cfg
->num_varinfo
, cfg
->bb_entry
, originals
, stack
, lvreg_stack
, lvreg_defined
, stack_history
, stack_history_size
);
469 g_free (stack_history
);
471 g_free (lvreg_stack
);
472 g_free (lvreg_defined
);
474 if (cfg
->verbose_level
>= 4)
475 printf ("\nEND COMPUTE SSA.\n\n");
477 cfg
->comp_done
|= MONO_COMP_SSA
;
481 mono_ssa_remove (MonoCompile
*cfg
)
483 MonoInst
*ins
, *var
, *move
;
484 int bbindex
, i
, j
, first
;
486 g_assert (cfg
->comp_done
& MONO_COMP_SSA
);
488 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
489 MonoBasicBlock
*bb
= cfg
->bblocks
[i
];
491 if (cfg
->verbose_level
>= 4)
492 printf ("\nREMOVE SSA %d:\n", bb
->block_num
);
494 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
495 if (MONO_IS_PHI (ins
)) {
496 g_assert (ins
->inst_phi_args
[0] == bb
->in_count
);
497 var
= get_vreg_to_inst (cfg
, ins
->dreg
);
499 /* Check for PHI nodes where all the inputs are the same */
500 first
= ins
->inst_phi_args
[1];
502 for (j
= 1; j
< bb
->in_count
; ++j
)
503 if (first
!= ins
->inst_phi_args
[j
+ 1])
506 if ((bb
->in_count
> 1) && (j
== bb
->in_count
)) {
507 ins
->opcode
= op_phi_to_move (ins
->opcode
);
508 if (ins
->opcode
== OP_VMOVE
)
509 g_assert (ins
->klass
);
512 for (j
= 0; j
< bb
->in_count
; j
++) {
513 MonoBasicBlock
*pred
= bb
->in_bb
[j
];
514 int sreg
= ins
->inst_phi_args
[j
+ 1];
516 if (cfg
->verbose_level
>= 4)
517 printf ("\tADD R%d <- R%d in BB%d\n", var
->dreg
, sreg
, pred
->block_num
);
518 if (var
->dreg
!= sreg
) {
519 MONO_INST_NEW (cfg
, move
, op_phi_to_move (ins
->opcode
));
520 if (move
->opcode
== OP_VMOVE
) {
521 g_assert (ins
->klass
);
522 move
->klass
= ins
->klass
;
524 move
->dreg
= var
->dreg
;
526 mono_add_ins_to_end (pred
, move
);
536 if (cfg
->verbose_level
>= 4) {
537 for (i
= 0; i
< cfg
->num_bblocks
; ++i
) {
538 MonoBasicBlock
*bb
= cfg
->bblocks
[i
];
540 mono_print_bb (bb
, "AFTER REMOVE SSA:");
545 * Removal of SSA form introduces many copies. To avoid this, we tyry to coalesce
546 * the variables if possible. Since the newly introduced SSA variables don't
547 * have overlapping live ranges (because we don't do agressive optimization), we
548 * can coalesce them into the original variable.
551 for (bbindex
= 0; bbindex
< cfg
->num_bblocks
; ++bbindex
) {
552 MonoBasicBlock
*bb
= cfg
->bblocks
[bbindex
];
554 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
555 const char *spec
= INS_INFO (ins
->opcode
);
557 int sregs
[MONO_MAX_SRC_REGS
];
559 if (ins
->opcode
== OP_NOP
)
562 if (spec
[MONO_INST_DEST
] != ' ') {
563 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
566 MonoMethodVar
*vmv
= MONO_VARINFO (cfg
, var
->inst_c0
);
569 * The third condition avoids coalescing with variables eliminated
572 if ((vmv
->reg
!= -1) && (vmv
->idx
!= vmv
->reg
) && (MONO_VARINFO (cfg
, vmv
->reg
)->reg
!= -1)) {
573 printf ("COALESCE: R%d -> R%d\n", ins
->dreg
, cfg
->varinfo
[vmv
->reg
]->dreg
);
574 ins
->dreg
= cfg
->varinfo
[vmv
->reg
]->dreg
;
579 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
580 for (i
= 0; i
< num_sregs
; ++i
) {
581 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
584 MonoMethodVar
*vmv
= MONO_VARINFO (cfg
, var
->inst_c0
);
586 if ((vmv
->reg
!= -1) && (vmv
->idx
!= vmv
->reg
) && (MONO_VARINFO (cfg
, vmv
->reg
)->reg
!= -1)) {
587 printf ("COALESCE: R%d -> R%d\n", sregs
[i
], cfg
->varinfo
[vmv
->reg
]->dreg
);
588 sregs
[i
] = cfg
->varinfo
[vmv
->reg
]->dreg
;
592 mono_inst_set_src_registers (ins
, sregs
);
596 for (i
= 0; i
< cfg
->num_varinfo
; ++i
) {
597 MONO_VARINFO (cfg
, i
)->reg
= -1;
600 if (cfg
->comp_done
& MONO_COMP_REACHABILITY
)
601 unlink_unused_bblocks (cfg
);
603 cfg
->comp_done
&= ~MONO_COMP_LIVENESS
;
605 cfg
->comp_done
&= ~MONO_COMP_SSA
;
609 mono_ssa_create_def_use (MonoCompile
*cfg
)
615 g_assert (!(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
));
617 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
618 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
619 const char *spec
= INS_INFO (ins
->opcode
);
622 int sregs
[MONO_MAX_SRC_REGS
];
624 if (ins
->opcode
== OP_NOP
)
628 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
629 for (i
= 0; i
< num_sregs
; ++i
) {
630 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
631 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
632 record_use (cfg
, var
, bb
, ins
);
635 if (MONO_IS_STORE_MEMBASE (ins
)) {
636 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
637 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
638 record_use (cfg
, var
, bb
, ins
);
641 if (MONO_IS_PHI (ins
)) {
642 for (i
= ins
->inst_phi_args
[0]; i
> 0; i
--) {
643 g_assert (ins
->inst_phi_args
[i
] != -1);
644 record_use (cfg
, get_vreg_to_inst (cfg
, ins
->inst_phi_args
[i
]), bb
, ins
);
649 if ((spec
[MONO_INST_DEST
] != ' ') && !MONO_IS_STORE_MEMBASE (ins
)) {
650 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->dreg
);
652 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
653 info
= MONO_VARINFO (cfg
, var
->inst_c0
);
661 cfg
->comp_done
|= MONO_COMP_SSA_DEF_USE
;
665 mono_ssa_copyprop (MonoCompile
*cfg
)
670 g_assert ((cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
));
672 for (index
= 0; index
< cfg
->num_varinfo
; ++index
) {
673 MonoInst
*var
= cfg
->varinfo
[index
];
674 MonoMethodVar
*info
= MONO_VARINFO (cfg
, index
);
676 if (info
->def
&& (MONO_IS_MOVE (info
->def
))) {
677 MonoInst
*var2
= get_vreg_to_inst (cfg
, info
->def
->sreg1
);
679 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
))) {
680 /* Rewrite all uses of var to be uses of var2 */
681 int dreg
= var
->dreg
;
682 int sreg1
= var2
->dreg
;
686 MonoVarUsageInfo
*u
= (MonoVarUsageInfo
*)l
->data
;
687 MonoInst
*ins
= u
->inst
;
688 GList
*next
= l
->next
;
690 int sregs
[MONO_MAX_SRC_REGS
];
692 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
693 for (i
= 0; i
< num_sregs
; ++i
) {
694 if (sregs
[i
] == dreg
)
698 g_assert (sregs
[i
] == dreg
);
700 mono_inst_set_src_registers (ins
, sregs
);
701 } else if (MONO_IS_STORE_MEMBASE (ins
) && ins
->dreg
== dreg
) {
703 } else if (MONO_IS_PHI (ins
)) {
704 for (i
= ins
->inst_phi_args
[0]; i
> 0; i
--) {
705 int sreg
= ins
->inst_phi_args
[i
];
706 if (sreg
== var
->dreg
)
710 ins
->inst_phi_args
[i
] = sreg1
;
713 g_assert_not_reached ();
715 record_use (cfg
, var2
, u
->bb
, ins
);
725 if (cfg
->verbose_level
>= 4) {
728 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
)
729 mono_print_bb (bb
, "AFTER SSA COPYPROP");
734 evaluate_ins (MonoCompile
*cfg
, MonoInst
*ins
, MonoInst
**res
, MonoInst
**carray
)
736 MonoInst
*args
[MONO_MAX_SRC_REGS
];
737 int rs
[MONO_MAX_SRC_REGS
];
739 gboolean const_args
= TRUE
;
740 const char *spec
= INS_INFO (ins
->opcode
);
742 int sregs
[MONO_MAX_SRC_REGS
];
744 /* Short-circuit this */
745 if (ins
->opcode
== OP_ICONST
) {
750 if (ins
->opcode
== OP_NOP
)
753 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
754 for (i
= 0; i
< MONO_MAX_SRC_REGS
; ++i
)
756 for (i
= 0; i
< num_sregs
; ++i
) {
757 MonoInst
*var
= get_vreg_to_inst (cfg
, sregs
[i
]);
760 args
[i
] = carray
[sregs
[i
]];
763 else if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
764 rs
[i
] = MONO_VARINFO (cfg
, var
->inst_c0
)->cpstate
;
771 if (num_sregs
> 0 && const_args
) {
772 g_assert (num_sregs
<= 2);
773 if ((spec
[MONO_INST_DEST
] != ' ') && carray
[ins
->dreg
]) {
775 *res
= carray
[ins
->dreg
];
778 c0
= mono_constant_fold_ins (cfg
, ins
, args
[0], args
[1], FALSE
);
780 if (G_UNLIKELY (cfg
->verbose_level
> 1)) {
781 printf ("\t cfold -> ");
788 /* Can't cfold this ins */
794 for (i
= 0; i
< num_sregs
; ++i
) {
802 change_varstate (MonoCompile
*cfg
, GList
**cvars
, MonoMethodVar
*info
, int state
, MonoInst
*c0
, MonoInst
**carray
)
804 if (info
->cpstate
>= state
)
807 info
->cpstate
= state
;
809 if (G_UNLIKELY (cfg
->verbose_level
> 1))
810 printf ("\tState of R%d set to %d\n", cfg
->varinfo
[info
->idx
]->dreg
, info
->cpstate
);
815 carray
[cfg
->varinfo
[info
->idx
]->dreg
] = c0
;
817 if (!g_list_find (*cvars
, info
)) {
818 *cvars
= g_list_prepend (*cvars
, info
);
823 add_cprop_bb (MonoCompile
*cfg
, MonoBasicBlock
*bb
, GList
**bblist
)
825 if (G_UNLIKELY (cfg
->verbose_level
> 1))
826 printf ("\tAdd BB%d to worklist\n", bb
->block_num
);
828 if (!(bb
->flags
& BB_REACHABLE
)) {
829 bb
->flags
|= BB_REACHABLE
;
830 *bblist
= g_list_prepend (*bblist
, bb
);
835 visit_inst (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, GList
**cvars
, GList
**bblist
, MonoInst
**carray
)
837 const char *spec
= INS_INFO (ins
->opcode
);
839 if (ins
->opcode
== OP_NOP
)
842 if (cfg
->verbose_level
> 1)
843 mono_print_ins (ins
);
845 /* FIXME: Support longs/floats */
846 /* FIXME: Work on vregs as well */
848 if (MONO_IS_PHI (ins
)) {
849 MonoMethodVar
*info
= MONO_VARINFO (cfg
, get_vreg_to_inst (cfg
, ins
->dreg
)->inst_c0
);
853 for (j
= 1; j
<= ins
->inst_phi_args
[0]; j
++) {
854 MonoInst
*var
= get_vreg_to_inst (cfg
, ins
->inst_phi_args
[j
]);
855 MonoMethodVar
*mv
= MONO_VARINFO (cfg
, var
->inst_c0
);
856 MonoInst
*src
= mv
->def
;
858 if (mv
->def_bb
&& !(mv
->def_bb
->flags
& BB_REACHABLE
))
861 if (!mv
->def
|| !src
|| mv
->cpstate
== 2) {
862 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
866 if (mv
->cpstate
== 0)
869 g_assert (carray
[var
->dreg
]);
872 c0
= carray
[var
->dreg
];
875 if (c0
->opcode
!= OP_ICONST
) {
876 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
880 if (carray
[var
->dreg
]->inst_c0
!= c0
->inst_c0
) {
881 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
886 if (c0
&& info
->cpstate
< 1) {
887 change_varstate (cfg
, cvars
, info
, 1, c0
, carray
);
889 g_assert (c0
->opcode
== OP_ICONST
);
892 else if (!MONO_IS_STORE_MEMBASE (ins
) && ((spec
[MONO_INST_SRC1
] != ' ') || (spec
[MONO_INST_SRC2
] != ' ') || (spec
[MONO_INST_DEST
] != ' '))) {
896 if (spec
[MONO_INST_DEST
] != ' ')
897 var
= get_vreg_to_inst (cfg
, ins
->dreg
);
902 state
= evaluate_ins (cfg
, ins
, &c0
, carray
);
904 if (var
&& !(var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
))) {
905 MonoMethodVar
*info
= MONO_VARINFO (cfg
, var
->inst_c0
);
907 if (info
->cpstate
< 2) {
909 change_varstate (cfg
, cvars
, info
, 1, c0
, carray
);
911 change_varstate (cfg
, cvars
, info
, 2, NULL
, carray
);
914 else if (!var
&& (ins
->dreg
!= -1)) {
916 * We don't record def-use information for local vregs since it would be
917 * expensive. Instead, we depend on the fact that all uses of the vreg are in
918 * the same bblock, so they will be examined after the definition.
919 * FIXME: This isn't true if the ins is visited through an SSA edge.
922 carray
[ins
->dreg
] = c0
;
924 if (carray
[ins
->dreg
]) {
926 * The state of the vreg changed from constant to non-constant
927 * -> need to rescan the whole bblock.
929 carray
[ins
->dreg
] = NULL
;
930 /* FIXME: Speed this up */
932 if (!g_list_find (*bblist
, bb
))
933 *bblist
= g_list_prepend (*bblist
, bb
);
938 if (MONO_IS_JUMP_TABLE (ins
)) {
940 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (ins
);
942 if (!ins
->next
|| ins
->next
->opcode
!= OP_PADD
) {
943 /* The PADD was optimized away */
944 /* FIXME: handle this as well */
945 for (i
= 0; i
< table
->table_size
; i
++)
946 if (table
->table
[i
])
947 add_cprop_bb (cfg
, table
->table
[i
], bblist
);
951 g_assert (ins
->next
->opcode
== OP_PADD
);
952 g_assert (ins
->next
->sreg1
== ins
->dreg
);
954 if (carray
[ins
->next
->sreg2
]) {
955 #if SIZEOF_REGISTER == 8
956 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 3;
958 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 2;
960 if ((idx
< 0) || (idx
>= table
->table_size
))
961 /* Out-of-range, no branch is executed */
964 if (table
->table
[idx
])
965 add_cprop_bb (cfg
, table
->table
[idx
], bblist
);
968 for (i
= 0; i
< table
->table_size
; i
++)
969 if (table
->table
[i
])
970 add_cprop_bb (cfg
, table
->table
[i
], bblist
);
974 if (ins
->opcode
== OP_SWITCH
) {
976 MonoJumpInfoBBTable
*table
= ins
->inst_p0
;
978 for (i
= 0; i
< table
->table_size
; i
++)
979 if (table
->table
[i
])
980 add_cprop_bb (cfg
, table
->table
[i
], bblist
);
983 /* Handle COMPARE+BRCOND pairs */
984 if (ins
->next
&& MONO_IS_COND_BRANCH_OP (ins
->next
)) {
986 g_assert (c0
->opcode
== OP_ICONST
);
989 ins
->next
->flags
|= MONO_INST_CFOLD_TAKEN
;
991 ins
->next
->flags
|= MONO_INST_CFOLD_NOT_TAKEN
;
994 ins
->next
->flags
&= ~(MONO_INST_CFOLD_TAKEN
| MONO_INST_CFOLD_NOT_TAKEN
);
997 visit_inst (cfg
, bb
, ins
->next
, cvars
, bblist
, carray
);
999 } else if (ins
->opcode
== OP_BR
) {
1000 add_cprop_bb (cfg
, ins
->inst_target_bb
, bblist
);
1001 } else if (MONO_IS_COND_BRANCH_OP (ins
)) {
1002 if (ins
->flags
& MONO_INST_CFOLD_TAKEN
) {
1003 add_cprop_bb (cfg
, ins
->inst_true_bb
, bblist
);
1004 } else if (ins
->flags
& MONO_INST_CFOLD_NOT_TAKEN
) {
1005 if (ins
->inst_false_bb
)
1006 add_cprop_bb (cfg
, ins
->inst_false_bb
, bblist
);
1008 add_cprop_bb (cfg
, ins
->inst_true_bb
, bblist
);
1009 if (ins
->inst_false_bb
)
1010 add_cprop_bb (cfg
, ins
->inst_false_bb
, bblist
);
1018 * Replace INS with its constant value, if it exists
1021 fold_ins (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
**carray
)
1023 const char *spec
= INS_INFO (ins
->opcode
);
1025 int num_sregs
= mono_inst_get_num_src_registers (ins
);
1027 if ((ins
->opcode
!= OP_NOP
) && (ins
->dreg
!= -1) && !MONO_IS_STORE_MEMBASE (ins
)) {
1028 if (carray
[ins
->dreg
] && (spec
[MONO_INST_DEST
] == 'i') && (ins
->dreg
>= MONO_MAX_IREGS
)) {
1029 /* Perform constant folding */
1031 g_assert (carray
[ins
->dreg
]->opcode
== OP_ICONST
);
1032 ins
->opcode
= OP_ICONST
;
1033 ins
->inst_c0
= carray
[ins
->dreg
]->inst_c0
;
1034 MONO_INST_NULLIFY_SREGS (ins
);
1035 } else if (num_sregs
== 2 && carray
[ins
->sreg2
]) {
1036 /* Perform op->op_imm conversion */
1037 opcode2
= mono_op_to_op_imm (ins
->opcode
);
1038 if (opcode2
!= -1) {
1039 ins
->opcode
= opcode2
;
1040 ins
->inst_imm
= carray
[ins
->sreg2
]->inst_c0
;
1043 if ((opcode2
== OP_VOIDCALL
) || (opcode2
== OP_CALL
) || (opcode2
== OP_LCALL
) || (opcode2
== OP_FCALL
))
1044 ((MonoCallInst
*)ins
)->fptr
= (gpointer
)ins
->inst_imm
;
1047 /* FIXME: Handle 3 op insns */
1050 if (MONO_IS_JUMP_TABLE (ins
)) {
1052 MonoJumpInfoBBTable
*table
= MONO_JUMP_TABLE_FROM_INS (ins
);
1054 if (!ins
->next
|| ins
->next
->opcode
!= OP_PADD
) {
1055 /* The PADD was optimized away */
1056 /* FIXME: handle this as well */
1060 g_assert (ins
->next
->opcode
== OP_PADD
);
1061 g_assert (ins
->next
->sreg1
== ins
->dreg
);
1062 g_assert (ins
->next
->next
->opcode
== OP_LOAD_MEMBASE
);
1064 if (carray
[ins
->next
->sreg2
]) {
1065 /* Convert to a simple branch */
1066 #if SIZEOF_REGISTER == 8
1067 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 3;
1069 int idx
= carray
[ins
->next
->sreg2
]->inst_c0
>> 2;
1072 if (!((idx
>= 0) && (idx
< table
->table_size
))) {
1073 /* Out of range, eliminate the whole switch */
1074 for (i
= 0; i
< table
->table_size
; ++i
) {
1075 remove_bb_from_phis (cfg
, bb
, table
->table
[i
]);
1076 mono_unlink_bblock (cfg
, bb
, table
->table
[i
]);
1080 NULLIFY_INS (ins
->next
);
1081 NULLIFY_INS (ins
->next
->next
);
1082 if (ins
->next
->next
->next
)
1083 NULLIFY_INS (ins
->next
->next
->next
);
1088 if (!ins
->next
->next
->next
|| ins
->next
->next
->next
->opcode
!= OP_BR_REG
) {
1089 /* A one-way switch which got optimized away */
1090 if (G_UNLIKELY (cfg
->verbose_level
> 1)) {
1091 printf ("\tNo cfold on ");
1092 mono_print_ins (ins
);
1097 if (G_UNLIKELY (cfg
->verbose_level
> 1)) {
1098 printf ("\tcfold on ");
1099 mono_print_ins (ins
);
1102 /* Unlink target bblocks */
1103 for (i
= 0; i
< table
->table_size
; ++i
) {
1104 if (table
->table
[i
] != table
->table
[idx
]) {
1105 remove_bb_from_phis (cfg
, bb
, table
->table
[i
]);
1106 mono_unlink_bblock (cfg
, bb
, table
->table
[i
]);
1110 /* Change the OP_BR_REG to a simple branch */
1111 ins
->next
->next
->next
->opcode
= OP_BR
;
1112 ins
->next
->next
->next
->inst_target_bb
= table
->table
[idx
];
1113 ins
->next
->next
->next
->sreg1
= -1;
1115 /* Nullify the other instructions */
1117 NULLIFY_INS (ins
->next
);
1118 NULLIFY_INS (ins
->next
->next
);
1122 else if (MONO_IS_COND_BRANCH_OP (ins
)) {
1123 if (ins
->flags
& MONO_INST_CFOLD_TAKEN
) {
1124 remove_bb_from_phis (cfg
, bb
, ins
->inst_false_bb
);
1125 mono_unlink_bblock (cfg
, bb
, ins
->inst_false_bb
);
1126 ins
->opcode
= OP_BR
;
1127 ins
->inst_target_bb
= ins
->inst_true_bb
;
1128 } else if (ins
->flags
& MONO_INST_CFOLD_NOT_TAKEN
) {
1129 remove_bb_from_phis (cfg
, bb
, ins
->inst_true_bb
);
1130 mono_unlink_bblock (cfg
, bb
, ins
->inst_true_bb
);
1131 ins
->opcode
= OP_BR
;
1132 ins
->inst_target_bb
= ins
->inst_false_bb
;
1138 mono_ssa_cprop (MonoCompile
*cfg
)
1142 GList
*bblock_list
, *cvars
;
1145 //printf ("SIMPLE OPTS BB%d %s\n", bb->block_num, mono_method_full_name (cfg->method, TRUE));
1147 carray
= g_new0 (MonoInst
*, cfg
->next_vreg
);
1149 if (!(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
))
1150 mono_ssa_create_def_use (cfg
);
1152 bblock_list
= g_list_prepend (NULL
, cfg
->bb_entry
);
1153 cfg
->bb_entry
->flags
|= BB_REACHABLE
;
1155 memset (carray
, 0, sizeof (MonoInst
*) * cfg
->num_varinfo
);
1157 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1158 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1163 for (bb
= cfg
->bb_entry
->next_bb
; bb
; bb
= bb
->next_bb
) {
1165 * FIXME: This should be bb->flags & BB_FLAG_EXCEPTION_HANDLER, but
1166 * that would still allow unreachable try's to be removed.
1169 add_cprop_bb (cfg
, bb
, &bblock_list
);
1174 while (bblock_list
) {
1177 bb
= (MonoBasicBlock
*)bblock_list
->data
;
1179 bblock_list
= g_list_delete_link (bblock_list
, bblock_list
);
1181 g_assert (bb
->flags
& BB_REACHABLE
);
1184 * Some bblocks are linked to 2 others even through they fall through to the
1187 if (!(bb
->last_ins
&& MONO_IS_BRANCH_OP (bb
->last_ins
))) {
1188 for (i
= 0; i
< bb
->out_count
; ++i
)
1189 add_cprop_bb (cfg
, bb
->out_bb
[i
], &bblock_list
);
1192 if (cfg
->verbose_level
> 1)
1193 printf ("\nSSA CONSPROP BB%d:\n", bb
->block_num
);
1195 for (inst
= bb
->code
; inst
; inst
= inst
->next
) {
1196 visit_inst (cfg
, bb
, inst
, &cvars
, &bblock_list
, carray
);
1200 MonoMethodVar
*info
= (MonoMethodVar
*)cvars
->data
;
1201 cvars
= g_list_delete_link (cvars
, cvars
);
1203 for (tmp
= info
->uses
; tmp
; tmp
= tmp
->next
) {
1204 MonoVarUsageInfo
*ui
= (MonoVarUsageInfo
*)tmp
->data
;
1205 if (!(ui
->bb
->flags
& BB_REACHABLE
))
1207 visit_inst (cfg
, ui
->bb
, ui
->inst
, &cvars
, &bblock_list
, carray
);
1212 for (bb
= cfg
->bb_entry
->next_bb
; bb
; bb
= bb
->next_bb
) {
1214 for (inst
= bb
->code
; inst
; inst
= inst
->next
) {
1215 fold_ins (cfg
, bb
, inst
, carray
);
1221 cfg
->comp_done
|= MONO_COMP_REACHABILITY
;
1223 /* fixme: we should update usage infos during cprop, instead of computing it again */
1224 cfg
->comp_done
&= ~MONO_COMP_SSA_DEF_USE
;
1225 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1226 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1233 add_to_dce_worklist (MonoCompile
*cfg
, MonoMethodVar
*var
, MonoMethodVar
*use
, GList
**wl
)
1237 *wl
= g_list_prepend_mempool (cfg
->mempool
, *wl
, use
);
1239 for (tmp
= use
->uses
; tmp
; tmp
= tmp
->next
) {
1240 MonoVarUsageInfo
*ui
= (MonoVarUsageInfo
*)tmp
->data
;
1241 if (ui
->inst
== var
->def
) {
1242 /* from the mempool */
1243 use
->uses
= g_list_remove_link (use
->uses
, tmp
);
1250 mono_ssa_deadce (MonoCompile
*cfg
)
1255 g_assert (cfg
->comp_done
& MONO_COMP_SSA
);
1257 //printf ("DEADCE %s\n", mono_method_full_name (cfg->method, TRUE));
1259 if (!(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
))
1260 mono_ssa_create_def_use (cfg
);
1262 mono_ssa_copyprop (cfg
);
1265 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1266 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1267 work_list
= g_list_prepend_mempool (cfg
->mempool
, work_list
, info
);
1271 MonoMethodVar
*info
= (MonoMethodVar
*)work_list
->data
;
1272 work_list
= g_list_remove_link (work_list
, work_list
);
1275 * The second part of the condition happens often when PHI nodes have their dreg
1276 * as one of their arguments due to the fact that we use the original vars.
1278 if (info
->def
&& (!info
->uses
|| ((info
->uses
->next
== NULL
) && (((MonoVarUsageInfo
*)info
->uses
->data
)->inst
== info
->def
)))) {
1279 MonoInst
*def
= info
->def
;
1281 /* Eliminating FMOVE could screw up the fp stack */
1282 if (MONO_IS_MOVE (def
) && (!MONO_ARCH_USE_FPSTACK
|| (def
->opcode
!= OP_FMOVE
))) {
1283 MonoInst
*src_var
= get_vreg_to_inst (cfg
, def
->sreg1
);
1284 if (src_var
&& !(src_var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
1285 add_to_dce_worklist (cfg
, info
, MONO_VARINFO (cfg
, src_var
->inst_c0
), &work_list
);
1288 } else if ((def
->opcode
== OP_ICONST
) || (def
->opcode
== OP_I8CONST
) || MONO_IS_ZERO (def
)) {
1291 } else if (MONO_IS_PHI (def
)) {
1293 for (j
= def
->inst_phi_args
[0]; j
> 0; j
--) {
1294 MonoMethodVar
*u
= MONO_VARINFO (cfg
, get_vreg_to_inst (cfg
, def
->inst_phi_args
[j
])->inst_c0
);
1295 add_to_dce_worklist (cfg
, info
, u
, &work_list
);
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
));
1347 mono_ssa_loop_invariant_code_motion (MonoCompile
*cfg
)
1349 MonoBasicBlock
*bb
, *h
, *idom
;
1350 MonoInst
*ins
, *n
, *tins
;
1353 g_assert (cfg
->comp_done
& MONO_COMP_SSA
);
1354 if (!(cfg
->comp_done
& MONO_COMP_LOOPS
) || !(cfg
->comp_done
& MONO_COMP_SSA_DEF_USE
))
1357 for (bb
= cfg
->bb_entry
->next_bb
; bb
; bb
= bb
->next_bb
) {
1358 GList
*lp
= bb
->loop_blocks
;
1362 h
= (MonoBasicBlock
*)lp
->data
;
1365 MONO_BB_FOR_EACH_INS_SAFE (bb
, n
, ins
) {
1366 gboolean is_class_init
= FALSE
;
1369 * Try to move instructions out of loop headers into the preceeding bblock.
1371 if (ins
->opcode
== OP_VOIDCALL
) {
1372 MonoCallInst
*call
= (MonoCallInst
*)ins
;
1374 if (call
->fptr_is_patch
) {
1375 MonoJumpInfo
*ji
= (MonoJumpInfo
*)call
->fptr
;
1377 if (ji
->type
== MONO_PATCH_INFO_CLASS_INIT
)
1378 is_class_init
= TRUE
;
1381 if (ins
->opcode
== OP_LDLEN
|| ins
->opcode
== OP_STRLEN
|| ins
->opcode
== OP_CHECK_THIS
|| ins
->opcode
== OP_AOTCONST
|| is_class_init
) {
1387 * h->nesting is needed to work around:
1388 * http://llvm.org/bugs/show_bug.cgi?id=17868
1390 if (!(idom
&& idom
->last_ins
&& idom
->last_ins
->opcode
== OP_BR
&& idom
->last_ins
->inst_target_bb
== h
&& h
->nesting
== 1)) {
1395 * Make sure there are no instructions with side effects before ins.
1398 MONO_BB_FOR_EACH_INS (bb
, tins
) {
1401 if (!MONO_INS_HAS_NO_SIDE_EFFECT (tins
)) {
1408 printf ("%s\n", mono_method_full_name (cfg->method, TRUE));
1409 mono_print_ins (tins);
1414 /* Make sure we don't move the instruction before the def of its sreg */
1415 if (ins
->opcode
== OP_LDLEN
|| ins
->opcode
== OP_STRLEN
|| ins
->opcode
== OP_CHECK_THIS
)
1420 MonoInst
*tins
, *var
;
1423 for (tins
= ins
->prev
; tins
; tins
= tins
->prev
) {
1424 const char *spec
= INS_INFO (tins
->opcode
);
1426 if (tins
->opcode
== OP_MOVE
&& tins
->dreg
== sreg
) {
1428 } if (spec
[MONO_INST_DEST
] != ' ' && tins
->dreg
== sreg
) {
1435 var
= get_vreg_to_inst (cfg
, sreg
);
1436 if (var
&& (var
->flags
& (MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)))
1441 if (cfg
->verbose_level
> 1) {
1442 printf ("licm in BB%d on ", bb
->block_num
);
1443 mono_print_ins (ins
);
1445 //{ static int count = 0; count ++; printf ("%d\n", count); }
1446 MONO_REMOVE_INS (bb
, ins
);
1447 mono_bblock_insert_before_ins (idom
, idom
->last_ins
, ins
);
1448 if (ins
->opcode
== OP_LDLEN
|| ins
->opcode
== OP_STRLEN
)
1449 idom
->has_array_access
= TRUE
;
1454 cfg
->comp_done
&= ~MONO_COMP_SSA_DEF_USE
;
1455 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
1456 MonoMethodVar
*info
= MONO_VARINFO (cfg
, i
);
1462 #endif /* DISABLE_JIT */