2 * local-propagation.c: Local constant, copy and tree propagation.
4 * To make some sense of the tree mover, read mono/docs/tree-mover.txt
7 * Paolo Molaro (lupus@ximian.com)
8 * Dietmar Maurer (dietmar@ximian.com)
9 * Massimiliano Mantione (massi@ximian.com)
11 * (C) 2006 Novell, Inc. http://www.novell.com
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mempool.h>
20 #include <mono/metadata/opcodes.h>
23 /* FIXME: Get rid of these */
24 #define NEW_BIALU(cfg,dest,op,dr,sr1,sr2) do { \
25 MONO_INST_NEW ((cfg), (dest), (op)); \
26 (dest)->dreg = (dr); \
27 (dest)->sreg1 = (sr1); \
28 (dest)->sreg2 = (sr2); \
31 #define NEW_BIALU_IMM(cfg,dest,op,dr,sr,imm) do { \
32 MONO_INST_NEW ((cfg), (dest), (op)); \
33 (dest)->dreg = (dr); \
34 (dest)->sreg1 = (sr); \
35 (dest)->inst_p1 = (gpointer)(gssize)(imm); \
38 #ifndef MONO_ARCH_IS_OP_MEMBASE
39 #define MONO_ARCH_IS_OP_MEMBASE(opcode) FALSE
42 #define MONO_DEBUG_LOCAL_PROP 0
43 #define MONO_DEBUG_TREE_MOVER 0
44 #define MONO_DUMP_TREE_MOVER 0
45 #define MONO_APPLY_TREE_MOVER_TO_SINGLE_METHOD 0
46 #define MONO_APPLY_TREE_MOVER_TO_COUNTED_METHODS 0
48 struct TreeMoverActSlot;
50 * A node describing one dependency between a tree and a local
52 typedef struct TreeMoverDependencyNode {
53 /* The local used in the tree */
54 struct TreeMoverActSlot *used_slot;
55 /* The local defined by the tree */
56 struct TreeMoverActSlot *affected_slot;
57 /* Next in the list of used locals */
58 struct TreeMoverDependencyNode *next_used_local;
59 /* Next in the list of affected locals */
60 struct TreeMoverDependencyNode *next_affected_local;
61 /* Previous in the list of affected locals */
62 struct TreeMoverDependencyNode *previous_affected_local;
63 /* False if the local is used in a tree that defines a used local */
65 } TreeMoverDependencyNode;
67 struct TreeMoverTreeMove;
69 * A node in a list of affected TreeMoverTreeMove
71 typedef struct TreeMoverAffectedMove {
72 struct TreeMoverTreeMove *affected_move;
73 struct TreeMoverAffectedMove *next_affected_move;
74 } TreeMoverAffectedMove;
77 * A node in a list of TreeMoverDependencyFromDeadDefinition
79 typedef struct TreeMoverDependencyFromDeadDefinition {
80 /* The ACT slot of the defined local */
81 struct TreeMoverActSlot *defined_slot;
82 /* The definition that will hopefully be dead */
83 MonoInst *dead_definition;
84 /* Next in the list */
85 struct TreeMoverDependencyFromDeadDefinition *next;
86 } TreeMoverDependencyFromDeadDefinition;
91 typedef struct TreeMoverTreeMove {
92 /* ACT slot of the defined local */
93 struct TreeMoverActSlot *defined_slot;
94 /* Code location of the definition */
96 /* Code location where the tree must be replaced with the local */
98 /* Moves that must not be performed of we perform this one */
99 TreeMoverAffectedMove *affected_moves;
100 /* Definitions that must be dead to be allowed to perform this move */
101 struct TreeMoverDependencyFromDeadDefinition *slots_that_must_be_safe;
102 /* Next in the list of scheduled moves */
103 struct TreeMoverTreeMove *next;
104 /* The used tree accesses heap memory */
105 guchar tree_reads_memory;
106 /* A subsequent definitions makes this move globally safe */
108 /* This move has been affected by something, ignore it */
109 guchar skip_this_move;
110 /* "tree forwarding" cannot continue for this definition */
111 guchar prevent_forwarding;
115 * An ACT slot (there is one for each local in the ACT array)
117 typedef struct TreeMoverActSlot {
118 /* List of used locals (directly and indirectly) */
119 TreeMoverDependencyNode *used_locals;
120 /* Last element (so that we can move all the nodes quickly) */
121 TreeMoverDependencyNode *last_used_local;
122 /* List of affected locals (definitions that use this local) */
123 TreeMoverDependencyNode *affected_locals;
124 /* The current pending move */
125 TreeMoverTreeMove *pending_move;
126 /* True if the move has already met its use use */
127 guchar pending_move_is_ready;
132 /* A "tree forwarding" is in progress */
133 guchar pending_move_is_forwarded;
137 * Main tree mover work area
139 typedef struct TreeMover {
140 /* Pool used for allocating everything */
145 /* Free (recycled) TreeMoverDependencyNode structs */
146 TreeMoverDependencyNode *free_nodes;
147 /* Free (recycled) TreeMoverTreeMove structs */
148 TreeMoverTreeMove *free_moves;
151 TreeMoverActSlot *ACT;
152 /* List of tree moves that could be performed */
153 TreeMoverTreeMove *scheduled_moves;
155 /* The following fields are reset at each tree traversal */
156 /* List of used locals */
157 TreeMoverDependencyNode *used_nodes;
158 /* Last node in the list (to free it in one block) */
159 TreeMoverDependencyNode *last_used_node;
160 /* The current tree cannot be moved (it can still receive moves!) */
161 guchar tree_has_side_effects;
162 /* The current tree reads heap locations */
163 guchar tree_reads_memory;
166 inline static TreeMoverDependencyNode*
167 tree_mover_new_node (TreeMover *tree_mover) {
168 TreeMoverDependencyNode *node;
170 if (tree_mover->free_nodes != NULL) {
171 node = tree_mover->free_nodes;
172 tree_mover->free_nodes = tree_mover->free_nodes->next_used_local;
173 node->next_used_local = NULL;
174 node->next_affected_local = NULL;
175 node->previous_affected_local = NULL;
177 node = (TreeMoverDependencyNode*) mono_mempool_alloc0 (tree_mover->pool, sizeof (TreeMoverDependencyNode));
184 tree_mover_new_slot_move (TreeMover *tree_mover, TreeMoverActSlot *slot) {
185 TreeMoverTreeMove *move;
187 if (tree_mover->free_moves != NULL) {
188 move = tree_mover->free_moves;
189 tree_mover->free_moves = tree_mover->free_moves->next;
190 memset (move, 0, sizeof (TreeMoverTreeMove));
192 move = (TreeMoverTreeMove*) mono_mempool_alloc0 (tree_mover->pool, sizeof (TreeMoverTreeMove));
195 slot->pending_move = move;
199 tree_mover_dispose_used_nodes (TreeMover *tree_mover) {
200 tree_mover->last_used_node->next_used_local = tree_mover->free_nodes;
201 tree_mover->free_nodes = tree_mover->used_nodes;
202 tree_mover->used_nodes = NULL;
203 tree_mover->last_used_node = NULL;
207 tree_mover_dispose_slot_nodes (TreeMover *tree_mover, TreeMoverActSlot *slot) {
208 slot->last_used_local->next_used_local = tree_mover->free_nodes;
209 tree_mover->free_nodes = slot->used_locals;
210 slot->used_locals = NULL;
211 slot->last_used_local = NULL;
215 tree_mover_dispose_slot_move (TreeMover *tree_mover, TreeMoverActSlot *slot) {
216 slot->pending_move->next = tree_mover->free_moves;
217 tree_mover->free_moves = slot->pending_move;
218 slot->pending_move = NULL;
221 inline static TreeMoverActSlot*
222 tree_mover_slot_from_index (TreeMover *tree_mover, int index) {
223 return & (tree_mover->ACT [index]);
227 tree_mover_slot_to_index (TreeMover *tree_mover, TreeMoverActSlot *slot) {
228 return slot - tree_mover->ACT;
232 tree_mover_add_used_node (TreeMover *tree_mover, TreeMoverActSlot *slot, gboolean use_is_direct) {
233 TreeMoverDependencyNode *node;
235 node = tree_mover_new_node (tree_mover);
236 node->used_slot = slot;
237 node->affected_slot = NULL;
238 node->use_is_direct = use_is_direct;
239 if (tree_mover->last_used_node != NULL) {
240 tree_mover->last_used_node->next_used_local = node;
242 tree_mover->used_nodes = node;
244 tree_mover->last_used_node = node;
248 tree_mover_link_affecting_node (TreeMoverDependencyNode *node, TreeMoverActSlot *affected_slot) {
249 TreeMoverActSlot *affecting_slot = node->used_slot;
250 node->affected_slot = affected_slot;
251 node->next_affected_local = affecting_slot->affected_locals;
252 affecting_slot->affected_locals = node;
253 if (node->next_affected_local != NULL) {
254 node->next_affected_local->previous_affected_local = node;
256 node->previous_affected_local = NULL;
260 tree_mover_unlink_affecting_node (TreeMoverDependencyNode *node) {
261 if (node->next_affected_local != NULL) {
262 node->next_affected_local->previous_affected_local = node->previous_affected_local;
264 if (node->previous_affected_local != NULL) {
265 node->previous_affected_local->next_affected_local = node->next_affected_local;
267 TreeMoverActSlot *slot = node->used_slot;
268 slot->affected_locals = node->next_affected_local;
270 node->next_affected_local = NULL;
271 node->previous_affected_local = NULL;
272 node->affected_slot = NULL;
276 tree_mover_link_affected_moves (TreeMover *tree_mover, TreeMoverActSlot *source_slot, TreeMoverActSlot *destination_slot) {
277 TreeMoverAffectedMove *node = (TreeMoverAffectedMove*) mono_mempool_alloc0 (tree_mover->pool, sizeof (TreeMoverAffectedMove));
278 node->affected_move = destination_slot->pending_move;
279 node->next_affected_move = source_slot->pending_move->affected_moves;
280 source_slot->pending_move->affected_moves = node;
285 tree_mover_record_pending_move (TreeMover *tree_mover, TreeMoverActSlot *slot, gboolean move_is_safe) {
286 if (slot->pending_move_is_ready) {
287 slot->pending_move->move_is_safe = move_is_safe;
288 slot->pending_move->next = tree_mover->scheduled_moves;
289 tree_mover->scheduled_moves = slot->pending_move;
290 slot->pending_move = NULL;
291 slot->pending_move_is_ready = FALSE;
296 tree_mover_clear_forwarding_dependency (TreeMoverActSlot *slot) {
297 if (slot->pending_move_is_forwarded) {
298 TreeMoverDependencyFromDeadDefinition *dependency = slot->pending_move->slots_that_must_be_safe;
299 while (dependency != NULL) {
300 if (dependency->defined_slot == slot) {
301 dependency->defined_slot = NULL;
303 dependency = dependency->next;
305 slot->pending_move = NULL;
310 tree_mover_enforce_forwarding_dependency (TreeMoverActSlot *slot) {
311 if (slot->pending_move_is_forwarded) {
312 slot->pending_move->skip_this_move = TRUE;
313 slot->pending_move_is_forwarded = FALSE;
314 slot->pending_move = NULL;
319 tree_mover_clean_act_slot_dependency_nodes (TreeMover *tree_mover, TreeMoverActSlot *slot) {
320 TreeMoverDependencyNode *current_node = slot->used_locals;
321 while (current_node != NULL) {
322 tree_mover_unlink_affecting_node (current_node);
323 current_node = current_node->next_used_local;
325 if (slot->used_locals != NULL) {
326 tree_mover_dispose_slot_nodes (tree_mover, slot);
331 tree_mover_clean_act_slot_pending_move (TreeMover *tree_mover, TreeMoverActSlot *slot) {
332 if (slot->pending_move != NULL) {
333 if (! slot->pending_move_is_forwarded) {
334 tree_mover_dispose_slot_move (tree_mover, slot);
336 slot->pending_move = NULL;
339 slot->pending_move_is_ready = FALSE;
340 slot->pending_move_is_forwarded = FALSE;
344 tree_mover_clean_act_slot (TreeMover *tree_mover, TreeMoverActSlot *slot) {
345 tree_mover_clean_act_slot_dependency_nodes (tree_mover, slot);
346 tree_mover_clean_act_slot_pending_move (tree_mover, slot);
350 tree_mover_kill_act_slot_for_definition (TreeMover *tree_mover, TreeMoverActSlot *slot) {
351 tree_mover_record_pending_move (tree_mover, slot, TRUE);
352 tree_mover_clear_forwarding_dependency (slot);
353 tree_mover_clean_act_slot (tree_mover, slot);
357 tree_mover_kill_act_slot_because_it_is_affected (TreeMover *tree_mover, TreeMoverActSlot *slot) {
358 if ((! slot->pending_move_is_ready) && (! slot->pending_move_is_forwarded)) {
359 tree_mover_clean_act_slot (tree_mover, slot);
364 tree_mover_kill_act_slot_for_use (TreeMover *tree_mover, TreeMoverActSlot *slot) {
365 tree_mover_enforce_forwarding_dependency (slot);
366 tree_mover_clean_act_slot (tree_mover, slot);
370 tree_mover_kill_act_for_indirect_local_definition (TreeMover *tree_mover, int size) {
372 for (i = 0; i < size; i++) {
373 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
374 if (slot->pending_move != NULL) {
375 slot->pending_move->prevent_forwarding = TRUE;
377 tree_mover_kill_act_slot_because_it_is_affected (tree_mover, slot);
382 tree_mover_kill_act_for_indirect_global_definition (TreeMover *tree_mover, int size) {
384 for (i = 0; i < size; i++) {
385 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
386 if ((slot->pending_move != NULL) && slot->pending_move->tree_reads_memory) {
387 tree_mover_kill_act_slot_because_it_is_affected (tree_mover, slot);
393 tree_mover_kill_act_for_indirect_use (TreeMover *tree_mover, int size) {
395 for (i = 0; i < size; i++) {
396 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
397 tree_mover_kill_act_slot_for_use (tree_mover, slot);
402 tree_mover_clear_act_recording_moves (TreeMover *tree_mover, int size) {
404 for (i = 0; i < size; i++) {
405 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
406 tree_mover_record_pending_move (tree_mover, slot, FALSE);
407 tree_mover_clean_act_slot (tree_mover, slot);
412 tree_mover_set_waiting_flags (TreeMover *tree_mover, int size) {
414 for (i = 0; i < size; i++) {
415 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
416 slot->waiting_flag = TRUE;
421 tree_mover_verify_dependency_nodes_are_clear (TreeMover *tree_mover, int size) {
423 for (i = 0; i < size; i++) {
424 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
425 if (slot->affected_locals != NULL) {
426 printf ("Slot %d has still affected variables\n", i);
427 g_assert_not_reached ();
429 if (slot->used_locals != NULL) {
430 printf ("Slot %d has still used variables\n", i);
431 g_assert_not_reached ();
433 if (slot->last_used_local != NULL) {
434 printf ("Slot %d has still a last used variable\n", i);
435 g_assert_not_reached ();
441 static const guchar stind_needs_conversion[(CEE_STIND_R8-CEE_STIND_REF)+1][STACK_MAX] = {
442 /* INV I4 I8 PTR R8 MP OBJ VTYPE */
443 {TRUE ,TRUE, TRUE, FALSE,TRUE, FALSE,FALSE,TRUE}, /* CEE_STIND_REF */
444 {TRUE ,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_STIND_I1 */
445 {TRUE ,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_STIND_I2 */
446 {TRUE ,FALSE,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_STIND_I4 */
447 {TRUE ,TRUE, FALSE,TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_STIND_I8 */
448 {TRUE ,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_STIND_R4 */
449 {TRUE ,TRUE, TRUE, TRUE, FALSE,TRUE, TRUE, TRUE} /* CEE_STIND_R8 */
451 static const guchar stind_i_needs_conversion[STACK_MAX] = {TRUE ,TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE};
452 static const guchar ldind_needs_conversion[(CEE_LDIND_REF-CEE_LDIND_I1)+1][STACK_MAX] = {
453 /* INV I4 I8 PTR R8 MP OBJ VTYPE */
454 {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_I1 */
455 {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_U1 */
456 {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_I2 */
457 {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_U2 */
458 {TRUE, FALSE,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_I4 */
459 {TRUE, FALSE,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_U4 */
460 {TRUE, TRUE, FALSE,TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_I8 */
461 {TRUE, TRUE, TRUE, FALSE,TRUE, FALSE,FALSE,TRUE}, /* CEE_LDIND_I */
462 {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE}, /* CEE_LDIND_R4 */
463 {TRUE, TRUE, TRUE, TRUE, FALSE,TRUE, TRUE, TRUE}, /* CEE_LDIND_R8 */
464 {TRUE, TRUE, TRUE, FALSE,TRUE, FALSE,FALSE,TRUE} /* CEE_LDIND_REF */
467 #define TREE_MOVER_LDIND_TO_CONV(__opcode) (ldind_to_conv [(__opcode) - CEE_LDIND_I1])
468 #define TREE_MOVER_STIND_NEEDS_CONVERSION(__opcode,__type) (((__opcode) != CEE_STIND_I) ? (stind_needs_conversion [(__opcode) - CEE_STIND_REF][(__type)]) : (stind_i_needs_conversion [(__type)]))
469 #define TREE_MOVER_LDIND_NEEDS_CONVERSION(__opcode,__type) (ldind_needs_conversion [(__opcode) - CEE_LDIND_I1][(__type)])
472 tree_mover_print_act_slot (const char* message, TreeMover *tree_mover, TreeMoverActSlot *slot) {
473 TreeMoverDependencyNode *node;
474 printf (" [%s] Slot %d uses {", message, tree_mover_slot_to_index (tree_mover, slot));
475 for (node = slot->used_locals; node != NULL; node = node->next_used_local) {
476 printf (" %d", tree_mover_slot_to_index (tree_mover, node->used_slot));
478 printf (" } affects {");
479 for (node = slot->affected_locals; node != NULL; node = node->next_affected_local) {
480 printf (" %d", tree_mover_slot_to_index (tree_mover, node->affected_slot));
482 printf (" } R%d F%d W%d U%d", slot->pending_move_is_ready, slot->pending_move_is_forwarded, slot->waiting_flag, slot->unsafe_flag);
483 if (slot->pending_move != NULL) {
484 printf (" DEFINITION:");
485 //printf (" DEFINITION[%p]:", slot->pending_move->definition);
486 mono_print_tree (slot->pending_move->definition);
491 static TreeMoverTreeMove*
492 mono_cprop_copy_values (MonoCompile *cfg, TreeMover *tree_mover, MonoInst *tree, MonoInst **acp)
496 TreeMoverTreeMove *pending_move = NULL;
498 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
499 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
501 if (cp->opcode == OP_ICONST) {
502 if (cfg->opt & MONO_OPT_CONSPROP) {
503 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
504 if (MONO_DEBUG_LOCAL_PROP) {
505 printf ("Propagating constant, tree ");
506 mono_print_tree (tree);
507 printf (" becomes ");
508 mono_print_tree (cp);
514 MonoType *inst_i0_underlying_type = mono_type_get_underlying_type (tree->inst_i0->inst_vtype);
515 MonoType *cp_underlying_type = mono_type_get_underlying_type (cp->inst_vtype);
516 if ((inst_i0_underlying_type->type == cp_underlying_type->type) ||
517 (tree->type == STACK_OBJ) || (tree->type == STACK_MP)) {
518 if (cfg->opt & MONO_OPT_COPYPROP) {
519 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
520 if (MONO_DEBUG_LOCAL_PROP) {
521 printf ("Propagating value, tree->inst_i0 ");
522 mono_print_tree (tree->inst_i0);
523 printf (" becomes ");
524 mono_print_tree (cp);
529 } else if (MONO_DEBUG_LOCAL_PROP) {
530 char* tree_type_name = mono_type_full_name (tree->inst_i0->inst_vtype);
531 char* cp_type_name = mono_type_full_name (cp->inst_vtype);
532 printf ("Values of tree->inst_i0 ");
533 mono_print_tree (tree->inst_i0);
535 mono_print_tree (cp);
536 printf (" have incompatible types in tree ");
537 mono_print_tree (tree);
539 printf (" MonoType of tree->inst_i0 is: %s\n", tree_type_name);
540 printf (" MonoType of cp is: %s\n", cp_type_name);
541 g_free (tree_type_name);
542 g_free (cp_type_name);
546 if (MONO_DEBUG_LOCAL_PROP) {
547 printf ("Propagation SKIPPED for inst ");
548 mono_print_tree (tree);
551 if ((tree_mover != NULL) && (cfg->opt & MONO_OPT_CFOLD))
552 mono_constant_fold_inst (tree, NULL);
554 arity = mono_burg_arity [tree->opcode];
557 TreeMoverTreeMove *result = mono_cprop_copy_values (cfg, tree_mover, tree->inst_i0, acp);
558 if (cfg->opt & MONO_OPT_CFOLD)
559 mono_constant_fold_inst (tree, NULL);
560 if (result != NULL) {
561 result->use = &(tree->inst_i0);
562 //printf (" SETTING inst_i0[%p] USE to %p (definition is %p)\n", tree, result->use, result->definition);
565 /* The opcode may have changed */
566 if (mono_burg_arity [tree->opcode] > 1) {
567 if (cfg->opt & MONO_OPT_CFOLD)
568 mono_constant_fold_inst (tree, NULL);
569 result = mono_cprop_copy_values (cfg, tree_mover, tree->inst_i1, acp);
570 if (result != NULL) {
571 result->use = &(tree->inst_i1);
572 //printf (" SETTING inst_i1[%p] USE to %p (definition is %p)\n", tree, result->use, result->definition);
575 mono_constant_fold_inst (tree, NULL);
579 /* Apply the tree mover after after propagation has been done */
580 if ((tree_mover != NULL) && (tree->ssa_op == MONO_SSA_LOAD) &&
581 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
582 guint used_index = tree->inst_i0->inst_c0;
583 TreeMoverActSlot *used_slot = &(tree_mover->ACT [used_index]);
585 /* First, handle waiting flag */
586 if (used_slot->waiting_flag) {
587 used_slot->unsafe_flag = TRUE;
588 used_slot->waiting_flag = FALSE;
591 if (!tree->inst_i0->flags) {
592 /* Record local use (the tree that contains this use might be movable) */
593 tree_mover_add_used_node (tree_mover, used_slot, TRUE);
595 /* Start working on the pending move... */
596 pending_move = used_slot->pending_move;
598 /* If there *is* a pending move... (otherwise, do nothing) */
599 if (pending_move != NULL) {
600 /* Check slot state */
601 if (used_slot->pending_move_is_forwarded) {
602 /* If the slot was a "hopefully dead" definition because of a forwarding... */
603 if (MONO_DEBUG_TREE_MOVER) {
604 printf ("Use should have been dead, killing slot %d: ", used_index);
605 mono_print_tree_nl (tree);
606 printf ("Also disabling forwarded definition at slot %d: ", tree_mover_slot_to_index (tree_mover, pending_move->defined_slot));
607 mono_print_tree_nl (pending_move->definition);
609 /* ...clear the slot (which also disables the forwarded definition), and... */
610 tree_mover_kill_act_slot_for_use (tree_mover, used_slot);
611 /* ...clear the pending_move */
613 } else if (used_slot->pending_move_is_ready ||
614 TREE_MOVER_STIND_NEEDS_CONVERSION (pending_move->definition->opcode, pending_move->definition->inst_i1->type) ||
615 TREE_MOVER_LDIND_NEEDS_CONVERSION (tree->opcode, pending_move->definition->inst_i1->type)) {
616 /* If the move was already in state [U], or if there are type problems... */
617 if (MONO_DEBUG_TREE_MOVER) {
618 printf ("Definition has too many, wrong or misplaced uses, killing slot %d: ", used_index);
619 mono_print_tree_nl (tree);
621 /* ...kill it, and clear the pending_move */
622 tree_mover_kill_act_slot_for_use (tree_mover, used_slot);
625 /* All goes well: set slot state to [U] */
626 TreeMoverDependencyNode *node = used_slot->used_locals;
627 if (MONO_DEBUG_TREE_MOVER) {
628 printf ("Setting tree move for slot %d as ready: ", used_index);
629 mono_print_tree_nl (tree);
631 /* Record indirect uses generated by this move */
632 while (node != NULL) {
633 tree_mover_add_used_node (tree_mover, node->used_slot, FALSE);
634 node = node->next_used_local;
637 /* Setup tree as movable */
638 used_slot->pending_move_is_ready = TRUE;
642 if (MONO_DEBUG_TREE_MOVER) {
643 printf ("Tree has side effects, killing slot %d: ", used_index);
644 mono_print_tree_nl (tree);
646 /* The whole tree is unmovable (it uses a flagged local) */
647 tree_mover->tree_has_side_effects = TRUE;
648 /* Moreover, the use of a flagged local kills the definition */
649 tree_mover_kill_act_slot_for_use (tree_mover, used_slot);
651 #if MONO_DUMP_TREE_MOVER
652 tree_mover_print_act_slot ("USE", tree_mover, used_slot);
659 mono_cprop_invalidate_values (MonoInst *tree, TreeMover *tree_mover, MonoInst **acp, int acp_size)
663 if (tree_mover != NULL) {
664 if ((tree->opcode == CEE_NEWARR) || (mono_find_jit_opcode_emulation (tree->opcode) != NULL)) {
665 if (MONO_DEBUG_TREE_MOVER) {
666 printf ("Recording side effect because emulated opcode cannot be moved: ");
667 mono_print_tree_nl (tree);
669 tree_mover->tree_has_side_effects = TRUE;
673 switch (tree->opcode) {
686 if ((tree_mover != NULL) && ((tree->ssa_op == MONO_SSA_NOP) || (tree->ssa_op & MONO_SSA_ADDRESS_TAKEN))) {
687 if (MONO_DEBUG_TREE_MOVER) {
688 printf ("Recording memory read at inst: ");
689 mono_print_tree_nl (tree);
691 tree_mover->tree_reads_memory = TRUE;
703 if ((tree->ssa_op == MONO_SSA_NOP) || (tree->ssa_op & MONO_SSA_ADDRESS_TAKEN)) {
704 if (MONO_DEBUG_LOCAL_PROP) {
705 printf ("Indirect store clears ACP at tree ");
706 mono_print_tree (tree);
709 memset (acp, 0, sizeof (MonoInst *) * acp_size);
710 if (tree_mover != NULL) {
711 if (MONO_DEBUG_TREE_MOVER) {
712 printf ("Killing all active slots (and recording side effect) because of inst ");
713 mono_print_tree_nl (tree);
715 /* Note that this does *not* dispose ready moves (state [U]) */
716 tree_mover_kill_act_for_indirect_local_definition (tree_mover, acp_size);
717 tree_mover->tree_has_side_effects = TRUE;
735 case OP_VOIDCALL_REG:
736 case OP_VOIDCALLVIRT:
738 case OP_TRAMPCALL_VTABLE:
741 case OP_VOIDCALL_RGCTX:
744 case OP_CALL_REG_RGCTX:
745 case OP_FCALL_REG_RGCTX:
746 case OP_VOIDCALL_REG_RGCTX:
747 case OP_LCALL_REG_RGCTX:
748 case OP_VCALL_REG_RGCTX:
749 case OP_CALLVIRT_IMT:
750 case OP_VOIDCALLVIRT_IMT:
751 case OP_FCALLVIRT_IMT:
752 case OP_LCALLVIRT_IMT:
753 case OP_VCALLVIRT_IMT: {
754 MonoCallInst *call = (MonoCallInst *)tree;
755 MonoMethodSignature *sig = call->signature;
756 int i, byref = FALSE;
758 if (tree_mover != NULL) {
759 if (MONO_DEBUG_TREE_MOVER) {
760 printf ("Recording side effect because of inst ");
761 mono_print_tree_nl (tree);
763 tree_mover->tree_has_side_effects = TRUE;
766 for (i = 0; i < sig->param_count; i++) {
767 if (sig->params [i]->byref) {
774 if (MONO_DEBUG_LOCAL_PROP) {
775 printf ("Call with byref parameter clears ACP at tree ");
776 mono_print_tree (tree);
779 memset (acp, 0, sizeof (MonoInst *) * acp_size);
780 if (tree_mover != NULL) {
781 if (MONO_DEBUG_TREE_MOVER) {
782 printf ("Killing all active slots because of inst ");
783 mono_print_tree_nl (tree);
785 tree_mover_kill_act_for_indirect_use (tree_mover, acp_size);
788 if (tree_mover != NULL) {
789 if (MONO_DEBUG_TREE_MOVER) {
790 printf ("Killing all active slots reading memory because of inst ");
791 mono_print_tree_nl (tree);
793 tree_mover_kill_act_for_indirect_global_definition (tree_mover, acp_size);
798 #define TREEMOVE_SPECIFIC_OPS 1
799 #define OPDEF(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) case a1:
800 #include "simple-cee-ops.h"
802 #define MINI_OP(a1,a2) case a1:
803 #include "simple-mini-ops.h"
805 #undef TREEMOVE_SPECIFIC_OPS
808 if (tree_mover != NULL) {
809 if (MONO_DEBUG_TREE_MOVER) {
810 printf ("Recording side effect because of inst ");
811 mono_print_tree_nl (tree);
813 tree_mover->tree_has_side_effects = TRUE;
818 arity = mono_burg_arity [tree->opcode];
824 mono_cprop_invalidate_values (tree->inst_i0, tree_mover, acp, acp_size);
827 mono_cprop_invalidate_values (tree->inst_i0, tree_mover, acp, acp_size);
828 mono_cprop_invalidate_values (tree->inst_i1, tree_mover, acp, acp_size);
831 g_assert_not_reached ();
836 mono_local_cprop_bb (MonoCompile *cfg, TreeMover *tree_mover, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
838 MonoInst *tree = bb->code;
844 if (tree_mover != NULL) {
845 tree_mover_set_waiting_flags (tree_mover, acp_size);
846 if (MONO_DEBUG_TREE_MOVER) {
847 printf ("Running tree mover on BB%d\n", bb->block_num);
850 MONO_BB_FOR_EACH_INS (bb, tree) {
851 if (tree_mover != NULL) {
852 if (MONO_DEBUG_TREE_MOVER) {
853 printf ("Running tree mover on tree ");
854 mono_print_tree_nl (tree);
856 tree_mover->tree_has_side_effects = FALSE;
857 tree_mover->tree_reads_memory = FALSE;
860 mono_cprop_copy_values (cfg, tree_mover, tree, acp);
861 mono_cprop_invalidate_values (tree, tree_mover, acp, acp_size);
862 if (MONO_DEBUG_TREE_MOVER) {
863 if (tree_mover != NULL) {
864 printf ("After the tree walk, tree_mover->tree_has_side_effects is %d\n", tree_mover->tree_has_side_effects);
868 if (tree->ssa_op == MONO_SSA_STORE &&
869 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
870 MonoInst *i1 = tree->inst_i1;
871 TreeMoverActSlot *forwarding_source = NULL;
872 gboolean tree_can_be_moved = TRUE;
874 acp [tree->inst_i0->inst_c0] = NULL;
875 if (MONO_DEBUG_TREE_MOVER) {
876 printf ("Assignment clears ACP[%d] at tree ", (int)tree->inst_i0->inst_c0);
877 mono_print_tree (tree);
881 for (i = 0; i < acp_size; i++) {
882 if (acp [i] && acp [i]->opcode != OP_ICONST &&
883 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
885 if (MONO_DEBUG_LOCAL_PROP) {
886 printf (" Consequently, ACP[%d] is cleared\n", i);
891 if (i1->opcode == OP_ICONST) {
892 acp [tree->inst_i0->inst_c0] = i1;
893 tree_can_be_moved = FALSE;
894 if (MONO_DEBUG_LOCAL_PROP) {
895 printf (" Consequently, ACP[%ld] becomes constant ", (long)tree->inst_i0->inst_c0);
896 mono_print_tree (i1);
899 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
900 } else if ((i1->type==STACK_I8) || (i1->opcode==OP_I8CONST) || (i1->opcode==OP_R4CONST) || (i1->opcode==OP_R8CONST) || (i1->opcode==OP_AOTCONST)) {
901 tree_can_be_moved = FALSE;
902 if (MONO_DEBUG_TREE_MOVER) {
903 printf ("Preventing move of constant or long value ");
904 mono_print_tree (i1);
908 if (i1->ssa_op == MONO_SSA_LOAD &&
909 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
910 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
911 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
912 tree_can_be_moved = FALSE;
913 if (MONO_DEBUG_LOCAL_PROP) {
914 printf (" Consequently, ACP[%d] becomes local ", (int)tree->inst_i0->inst_c0);
915 mono_print_tree (i1->inst_i0);
918 if (tree_mover != NULL) {
919 /* Examine the variable *used* in this definition (the "source") */
920 forwarding_source = tree_mover_slot_from_index (tree_mover, i1->inst_i0->inst_c0);
921 /* Check if source slot is ready to be forwarded */
922 if ((! forwarding_source->pending_move_is_ready) || (forwarding_source->pending_move->prevent_forwarding)) {
923 /* no forwarding is possible, do nothing */
924 forwarding_source = NULL;
927 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
930 /* Apply tree mover */
931 if (tree_mover != NULL) {
932 guint defined_index = tree->inst_i0->inst_c0;
933 TreeMoverActSlot *defined_slot = tree_mover_slot_from_index (tree_mover, defined_index);
934 TreeMoverDependencyNode *affected_node;
936 /* First clear the waiting flag... */
937 defined_slot->waiting_flag = FALSE;
938 /* ...and kill this slot (but recording any pending move)*/
939 tree_mover_kill_act_slot_for_definition (tree_mover, defined_slot);
940 if (MONO_DEBUG_TREE_MOVER) {
941 printf ("Definition is clearing slot %d\n", defined_index);
944 /* Handle "used" nodes... */
945 /* Check if this is a forwarding */
946 if (forwarding_source == NULL) {
947 /* Normal case, no forwarding: */
948 /* Check that consprop or copyprop did not already do the job, */
949 /* and that the tree has no side effects */
950 if (tree_can_be_moved && ! tree_mover->tree_has_side_effects) {
951 TreeMoverDependencyNode *affecting_node;
952 if (MONO_DEBUG_TREE_MOVER) {
953 printf ("Recording definition of slot %d by tree: ", defined_index);
954 mono_print_tree_nl (tree);
957 /* Then apply the definition */
958 tree_mover_new_slot_move (tree_mover, defined_slot);
959 defined_slot->pending_move->definition = tree;
960 defined_slot->pending_move->defined_slot = defined_slot;
961 defined_slot->pending_move->tree_reads_memory = tree_mover->tree_reads_memory;
963 /* Setup "used nodes" list */
964 defined_slot->used_locals = tree_mover->used_nodes;
965 defined_slot->last_used_local = tree_mover->last_used_node;
966 tree_mover->used_nodes = NULL;
967 tree_mover->last_used_node = NULL;
968 /* Link used nodes to "affecting" slots (so affected variables are linked) */
969 /* This is needed *now* so that circular definitions are detected */
970 for (affecting_node = defined_slot->used_locals; affecting_node != NULL; affecting_node = affecting_node->next_used_local) {
971 tree_mover_link_affecting_node (affecting_node, defined_slot);
973 } else if (MONO_DEBUG_TREE_MOVER) {
974 /* otherwise, do nothing */
975 printf ("Skipping definition of slot %d by tree: ", defined_index);
976 mono_print_tree_nl (tree);
979 TreeMoverDependencyFromDeadDefinition *dependency;
980 /* forwarding previous definition: */
981 if (MONO_DEBUG_TREE_MOVER) {
982 printf ("Handling forwarding in slot %d for tree: ", defined_index);
983 mono_print_tree_nl (tree);
985 /* Setup slot for forwarding */
986 defined_slot->pending_move = forwarding_source->pending_move;
987 defined_slot->pending_move_is_forwarded = TRUE;
988 /* Setup forwarding dependency node */
989 dependency = mono_mempool_alloc0 (tree_mover->pool, sizeof (TreeMoverDependencyFromDeadDefinition));
990 dependency->defined_slot = defined_slot;
991 dependency->dead_definition = tree;
992 dependency->next = defined_slot->pending_move->slots_that_must_be_safe;
993 defined_slot->pending_move->slots_that_must_be_safe = dependency;
994 /* Clear use (put slot back to state [D]) */
995 defined_slot->pending_move->use = NULL;
996 defined_slot->pending_move->defined_slot->pending_move_is_ready = FALSE;
999 /* Then kill all affected definitions waiting for a use */
1000 affected_node = defined_slot->affected_locals;
1001 while (affected_node != NULL) {
1002 TreeMoverDependencyNode *next_affected_node = affected_node->next_affected_local;
1003 TreeMoverActSlot *affected_slot = affected_node->affected_slot;
1005 if (affected_node->use_is_direct) {
1006 /* Direct use: kill affected slot */
1007 if (MONO_DEBUG_TREE_MOVER) {
1008 printf (" Direct use, killing slot %d with definition:", tree_mover_slot_to_index (tree_mover, affected_node->affected_slot));
1009 mono_print_tree_nl (affected_slot->pending_move->definition);
1011 tree_mover_kill_act_slot_because_it_is_affected (tree_mover, affected_slot);
1012 } else if ((defined_slot->pending_move!= NULL) &&
1013 (! defined_slot->pending_move_is_ready) &&
1014 (! defined_slot->pending_move_is_forwarded) &&
1015 (affected_slot->pending_move!= NULL) &&
1016 (! affected_slot->pending_move_is_ready) &&
1017 (! affected_slot->pending_move_is_forwarded)) {
1018 if (MONO_DEBUG_TREE_MOVER) {
1019 printf (" Indirect use, linking slots %d and %d\n", tree_mover_slot_to_index (tree_mover, affected_node->used_slot), tree_mover_slot_to_index (tree_mover, affected_node->affected_slot));
1021 tree_mover_link_affected_moves (tree_mover, defined_slot, affected_slot);
1022 tree_mover_link_affected_moves (tree_mover, affected_slot, defined_slot);
1024 tree_mover_unlink_affecting_node (affected_node);
1026 if ((next_affected_node != NULL) && (next_affected_node->affected_slot != NULL)) {
1027 affected_node = next_affected_node;
1029 affected_node = defined_slot->affected_locals;
1032 if (MONO_DUMP_TREE_MOVER) {
1033 tree_mover_print_act_slot ("DEFINITION", tree_mover, defined_slot);
1038 /* After we are done with this tree, clear the tree mover area */
1039 if ((tree_mover != NULL) && (tree_mover->used_nodes != NULL)) {
1040 tree_mover_dispose_used_nodes (tree_mover);
1044 if (tree->opcode == CEE_BEQ) {
1045 g_assert (tree->inst_i0->opcode == OP_COMPARE);
1046 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
1047 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
1049 tree->opcode = OP_BR;
1050 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
1051 tree->inst_target_bb = tree->inst_true_bb;
1053 tree->inst_target_bb = tree->inst_false_bb;
1060 if (tree_mover != NULL) {
1061 /* At BB end, kill all definitions still waiting for a use */
1062 tree_mover_clear_act_recording_moves (tree_mover, acp_size);
1063 if (MONO_DEBUG_TREE_MOVER) {
1064 tree_mover_verify_dependency_nodes_are_clear (tree_mover, acp_size);
1070 #if (MONO_APPLY_TREE_MOVER_TO_SINGLE_METHOD)
1072 mono_tree_mover_method_name = NULL;
1073 static gboolean check_tree_mover_method_name (MonoCompile *cfg) {
1074 if (mono_tree_mover_method_name == NULL) {
1075 mono_tree_mover_method_name = getenv ("MONO_TREE_MOVER_METHOD_NAME");
1077 if (mono_tree_mover_method_name != NULL) {
1078 char *method_name = mono_method_full_name (cfg->method, TRUE);
1079 if (strstr (method_name, mono_tree_mover_method_name) != NULL) {
1080 g_free (method_name);
1083 g_free (method_name);
1092 #if (MONO_APPLY_TREE_MOVER_TO_COUNTED_METHODS)
1094 mono_tree_mover_method_limit = -1;
1096 mono_tree_mover_method_count = 0;
1097 static gboolean check_tree_mover_method_count (MonoCompile *cfg) {
1098 if (mono_tree_mover_method_limit == -1) {
1099 char *limit_string = getenv ("MONO_TREE_MOVER_METHOD_LIMIT");
1100 if (limit_string != NULL) {
1101 mono_tree_mover_method_limit = atoi (limit_string);
1103 mono_tree_mover_method_limit = -2;
1106 if (mono_tree_mover_method_limit > -1) {
1107 mono_tree_mover_method_count ++;
1108 if (mono_tree_mover_method_count == mono_tree_mover_method_limit) {
1109 char *method_name = mono_method_full_name (cfg->method, TRUE);
1110 printf ("Last method compiled with treeprop: %s\n", method_name);
1111 g_free (method_name);
1114 return (mono_tree_mover_method_count <= mono_tree_mover_method_limit);
1122 apply_tree_mover (TreeMover *tree_mover, TreeMoverTreeMove *move) {
1123 TreeMoverDependencyFromDeadDefinition *dependency;
1124 TreeMoverAffectedMove *affected_move;
1126 /* Test if this move has been explicitly disabled */
1127 if (move->skip_this_move) {
1128 if (MONO_DEBUG_TREE_MOVER) {
1129 printf ("Move of slot %d must be skipped: ", tree_mover_slot_to_index (tree_mover, move->defined_slot));
1130 mono_print_tree_nl (move->definition);
1134 /* Test if this move is safe */
1135 if ((! move->move_is_safe) && move->defined_slot->unsafe_flag) {
1136 if (MONO_DEBUG_TREE_MOVER) {
1137 printf ("Move of slot %d is unsafe: ", tree_mover_slot_to_index (tree_mover, move->defined_slot));
1138 mono_print_tree_nl (move->definition);
1142 /* Test if this move depends from a definition that should have been dead */
1143 for (dependency = move->slots_that_must_be_safe; dependency != NULL; dependency = dependency->next) {
1144 if ((dependency->defined_slot != NULL) && dependency->defined_slot->unsafe_flag) {
1145 if (MONO_DEBUG_TREE_MOVER) {
1146 printf ("Move of slot %d depended from unsafe slot %d: ", tree_mover_slot_to_index (tree_mover, move->defined_slot), tree_mover_slot_to_index (tree_mover, dependency->defined_slot));
1147 mono_print_tree_nl (move->definition);
1153 if (MONO_DEBUG_TREE_MOVER) {
1154 printf ("Performing move of slot %d: ", tree_mover_slot_to_index (tree_mover, move->defined_slot));
1155 mono_print_tree_nl (move->definition);
1157 /* All tests passed, apply move */
1158 *(move->use) = move->definition->inst_i1;
1159 move->definition->opcode = OP_NOP;
1160 move->definition->ssa_op = MONO_SSA_NOP;
1162 /* Then disable moves affected by this move */
1163 affected_move = move->affected_moves;
1164 while (affected_move != NULL) {
1165 if (MONO_DEBUG_TREE_MOVER) {
1166 printf (" Consequently, disabling slot %d\n", tree_mover_slot_to_index (tree_mover, affected_move->affected_move->defined_slot));
1168 affected_move->affected_move->skip_this_move = TRUE;
1169 affected_move = affected_move->next_affected_move;
1172 /* Also kill dead dependency definitions */
1173 for (dependency = move->slots_that_must_be_safe; dependency != NULL; dependency = dependency->next) {
1174 if (dependency->defined_slot != NULL) {
1175 if (MONO_DEBUG_TREE_MOVER) {
1176 printf (" Consequently, kill dependent definition %d: ", tree_mover_slot_to_index (tree_mover, dependency->defined_slot));
1177 mono_print_tree_nl (dependency->dead_definition);
1179 dependency->dead_definition->opcode = OP_NOP;
1180 dependency->dead_definition->ssa_op = MONO_SSA_NOP;
1186 mono_local_cprop (MonoCompile *cfg) {
1189 TreeMover *tree_mover;
1191 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
1193 if (cfg->opt & MONO_OPT_TREEPROP) {
1194 MonoMemPool *pool = mono_mempool_new();
1195 tree_mover = mono_mempool_alloc0(pool, sizeof (TreeMover));
1197 tree_mover->cfg = cfg;
1198 tree_mover->pool = pool;
1199 tree_mover->ACT = mono_mempool_alloc0 (pool, sizeof (TreeMoverActSlot) * (cfg->num_varinfo));
1200 #if (MONO_APPLY_TREE_MOVER_TO_SINGLE_METHOD)
1201 if (! check_tree_mover_method_name (cfg)) {
1202 mono_mempool_destroy(tree_mover->pool);
1206 #if (MONO_APPLY_TREE_MOVER_TO_COUNTED_METHODS)
1207 if (! check_tree_mover_method_count (cfg)) {
1208 mono_mempool_destroy(tree_mover->pool);
1216 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1217 if (MONO_DEBUG_LOCAL_PROP||MONO_DEBUG_TREE_MOVER) {
1218 printf ("Applying mono_local_cprop to BB%d\n", bb->block_num);
1220 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1221 mono_local_cprop_bb (cfg, tree_mover, bb, acp, cfg->num_varinfo);
1224 if (tree_mover != NULL) {
1225 TreeMoverTreeMove *move;
1226 /* Move the movable trees */
1227 if (MONO_DEBUG_TREE_MOVER) {
1228 mono_print_code (cfg, "BEFORE TREE MOVER");
1229 printf ("Applying tree mover...\n");
1231 for (move = tree_mover->scheduled_moves; move != NULL; move = move->next) {
1232 apply_tree_mover (tree_mover, move);
1234 if (MONO_DEBUG_TREE_MOVER) {
1235 mono_print_code (cfg, "AFTER TREE MOVER");
1238 /* Global cleanup of tree mover memory */
1239 mono_mempool_destroy(tree_mover->pool);
1243 static inline MonoBitSet*
1244 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 max_size)
1246 int size = mono_bitset_alloc_size (max_size, 0);
1249 mem = mono_mempool_alloc (mp, size);
1250 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
1254 * mono_local_cprop2:
1256 * A combined local copy and constant propagation pass.
1259 mono_local_cprop2 (MonoCompile *cfg)
1268 max = cfg->next_vreg;
1269 defs = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * (cfg->next_vreg + 1));
1270 def_index = mono_mempool_alloc (cfg->mempool, sizeof (guint32) * (cfg->next_vreg + 1));
1272 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1275 int last_call_index;
1277 /* Manually init the defs entries used by the bblock */
1278 MONO_BB_FOR_EACH_INS (bb, ins) {
1279 if ((ins->dreg != -1) && (ins->dreg < max)) {
1280 defs [ins->dreg] = NULL;
1281 #if SIZEOF_VOID_P == 4
1282 defs [ins->dreg + 1] = NULL;
1285 if ((ins->sreg1 != -1) && (ins->sreg1 < max)) {
1286 defs [ins->sreg1] = NULL;
1287 #if SIZEOF_VOID_P == 4
1288 defs [ins->sreg1 + 1] = NULL;
1291 if ((ins->sreg2 != -1) && (ins->sreg2 < max)) {
1292 defs [ins->sreg2] = NULL;
1293 #if SIZEOF_VOID_P == 4
1294 defs [ins->sreg2 + 1] = NULL;
1300 last_call_index = -1;
1301 MONO_BB_FOR_EACH_INS (bb, ins) {
1302 const char *spec = INS_INFO (ins->opcode);
1303 int regtype, srcindex, sreg;
1305 if (ins->opcode == OP_NOP) {
1306 MONO_DELETE_INS (bb, ins);
1310 g_assert (ins->opcode > MONO_CEE_LAST);
1312 /* FIXME: Optimize this */
1313 if (ins->opcode == OP_LDADDR) {
1314 MonoInst *var = ins->inst_p0;
1316 defs [var->dreg] = NULL;
1318 if (!MONO_TYPE_ISSTRUCT (var->inst_vtype))
1323 if (MONO_IS_STORE_MEMBASE (ins)) {
1327 if ((regtype == 'i') && (sreg != -1) && defs [sreg]) {
1328 MonoInst *def = defs [sreg];
1330 if ((def->opcode == OP_MOVE) && (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) && !vreg_is_volatile (cfg, def->sreg1)) {
1331 int vreg = def->sreg1;
1332 //printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
1338 for (srcindex = 0; srcindex < 2; ++srcindex) {
1341 regtype = srcindex == 0 ? spec [MONO_INST_SRC1] : spec [MONO_INST_SRC2];
1342 sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
1344 if ((regtype == ' ') || (sreg == -1) || (!defs [sreg]))
1349 /* Copy propagation */
1351 * The first check makes sure the source of the copy did not change since
1352 * the copy was made.
1353 * The second check avoids volatile variables.
1354 * The third check avoids copy propagating local vregs through a call,
1355 * since the lvreg will be spilled
1356 * The fourth check avoids copy propagating a vreg in cases where
1357 * it would be eliminated anyway by reverse copy propagation later,
1358 * because propagating it would create another use for it, thus making
1359 * it impossible to use reverse copy propagation.
1361 /* Enabling this for floats trips up the fp stack */
1363 * Enabling this for floats on amd64 seems to cause a failure in
1364 * basic-math.cs, most likely because it gets rid of some r8->r4
1367 if (MONO_IS_MOVE (def) &&
1368 (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) &&
1369 !vreg_is_volatile (cfg, def->sreg1) &&
1370 /* This avoids propagating local vregs across calls */
1371 ((get_vreg_to_inst (cfg, def->sreg1) || !defs [def->sreg1] || (def_index [def->sreg1] >= last_call_index) || (def->opcode == OP_VMOVE))) &&
1372 !(defs [def->sreg1] && defs [def->sreg1]->next == def) &&
1373 (!MONO_ARCH_USE_FPSTACK || (def->opcode != OP_FMOVE)) &&
1374 (def->opcode != OP_FMOVE)) {
1375 int vreg = def->sreg1;
1377 //printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
1383 /* Allow further iterations */
1388 /* Constant propagation */
1389 /* FIXME: Make is_inst_imm a macro */
1390 /* FIXME: Make is_inst_imm take an opcode argument */
1391 /* is_inst_imm is only needed for binops */
1392 if ((((def->opcode == OP_ICONST) || ((sizeof (gpointer) == 8) && (def->opcode == OP_I8CONST))) &&
1393 (((srcindex == 0) && (ins->sreg2 == -1)) || mono_arch_is_inst_imm (def->inst_c0))) ||
1394 (!MONO_ARCH_USE_FPSTACK && (def->opcode == OP_R8CONST))) {
1397 /* srcindex == 1 -> binop, ins->sreg2 == -1 -> unop */
1398 if ((srcindex == 1) && (ins->sreg1 != -1) && defs [ins->sreg1] && (defs [ins->sreg1]->opcode == OP_ICONST) && defs [ins->sreg2]) {
1399 /* Both arguments are constants, perform cfold */
1400 mono_constant_fold_ins2 (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
1401 } else if ((srcindex == 0) && (ins->sreg2 != -1) && defs [ins->sreg2]) {
1402 /* Arg 1 is constant, swap arguments if possible */
1403 int opcode = ins->opcode;
1404 mono_constant_fold_ins2 (cfg, ins, defs [ins->sreg1], defs [ins->sreg2], TRUE);
1405 if (ins->opcode != opcode) {
1406 /* Allow further iterations */
1410 } else if ((srcindex == 0) && (ins->sreg2 == -1)) {
1411 /* Constant unop, perform cfold */
1412 mono_constant_fold_ins2 (cfg, ins, defs [ins->sreg1], NULL, TRUE);
1415 opcode2 = mono_op_to_op_imm (ins->opcode);
1416 if ((opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0) && ((srcindex == 1) || (ins->sreg2 == -1))) {
1417 ins->opcode = opcode2;
1418 if ((def->opcode == OP_I8CONST) && (sizeof (gpointer) == 4)) {
1419 ins->inst_ls_word = def->inst_ls_word;
1420 ins->inst_ms_word = def->inst_ms_word;
1422 ins->inst_imm = def->inst_c0;
1429 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1430 ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1432 /* Allow further iterations */
1438 #if defined(__i386__) || defined(__x86__64__)
1439 if ((ins->opcode == OP_X86_LEA) && (srcindex == 1)) {
1440 #if SIZEOF_VOID_P == 8
1441 /* FIXME: Use OP_PADD_IMM when the new JIT is done */
1442 ins->opcode = OP_LADD_IMM;
1444 ins->opcode = OP_ADD_IMM;
1446 ins->inst_imm += def->inst_c0 << ins->backend.shift_amount;
1450 opcode2 = mono_load_membase_to_load_mem (ins->opcode);
1451 if ((srcindex == 0) && (opcode2 != -1) && mono_arch_is_inst_imm (def->inst_c0)) {
1452 ins->opcode = opcode2;
1453 ins->inst_imm = def->inst_c0 + ins->inst_offset;
1458 else if (((def->opcode == OP_ADD_IMM) || (def->opcode == OP_LADD_IMM)) && (MONO_IS_LOAD_MEMBASE (ins) || MONO_ARCH_IS_OP_MEMBASE (ins->opcode))) {
1459 /* ADD_IMM is created by spill_global_vars */
1461 * We have to guarantee that def->sreg1 haven't changed since def->dreg
1462 * was defined. cfg->frame_reg is assumed to remain constant.
1464 if ((def->sreg1 == cfg->frame_reg) || ((def->next == ins) && (def->dreg != def->sreg1))) {
1465 ins->inst_basereg = def->sreg1;
1466 ins->inst_offset += def->inst_imm;
1468 } else if ((ins->opcode == OP_ISUB_IMM) && (def->opcode == OP_IADD_IMM) && (def->next == ins)) {
1469 ins->sreg1 = def->sreg1;
1470 ins->inst_imm -= def->inst_imm;
1471 } else if ((ins->opcode == OP_IADD_IMM) && (def->opcode == OP_ISUB_IMM) && (def->next == ins)) {
1472 ins->sreg1 = def->sreg1;
1473 ins->inst_imm -= def->inst_imm;
1474 } else if (ins->opcode == OP_STOREI1_MEMBASE_REG &&
1475 (def->opcode == OP_ICONV_TO_U1 || def->opcode == OP_ICONV_TO_I1 || def->opcode == OP_SEXT_I4 || (SIZEOF_VOID_P == 8 && def->opcode == OP_LCONV_TO_U1)) &&
1476 (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
1477 /* Avoid needless sign extension */
1478 ins->sreg1 = def->sreg1;
1479 } else if (ins->opcode == OP_STOREI2_MEMBASE_REG &&
1480 (def->opcode == OP_ICONV_TO_U2 || def->opcode == OP_ICONV_TO_I2 || def->opcode == OP_SEXT_I4 || (SIZEOF_VOID_P == 8 && def->opcode == OP_LCONV_TO_I2)) &&
1481 (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg]))) {
1482 /* Avoid needless sign extension */
1483 ins->sreg1 = def->sreg1;
1487 /* Do strength reduction here */
1488 /* FIXME: Add long/float */
1489 switch (ins->opcode) {
1491 if (ins->dreg == ins->sreg1) {
1492 MONO_DELETE_INS (bb, ins);
1493 spec = INS_INFO (ins->opcode);
1500 #if SIZEOF_VOID_P == 8
1504 if (ins->inst_imm == 0) {
1505 ins->opcode = OP_MOVE;
1506 spec = INS_INFO (ins->opcode);
1511 #if SIZEOF_VOID_P == 8
1514 if (ins->inst_imm == 0) {
1515 ins->opcode = (ins->opcode == OP_LMUL_IMM) ? OP_I8CONST : OP_ICONST;
1518 } else if (ins->inst_imm == 1) {
1519 ins->opcode = OP_MOVE;
1520 } else if ((ins->opcode == OP_IMUL_IMM) && (ins->inst_imm == -1)) {
1521 ins->opcode = OP_INEG;
1522 } else if ((ins->opcode == OP_LMUL_IMM) && (ins->inst_imm == -1)) {
1523 ins->opcode = OP_LNEG;
1525 int power2 = mono_is_power_of_two (ins->inst_imm);
1527 ins->opcode = (ins->opcode == OP_MUL_IMM) ? OP_SHL_IMM : ((ins->opcode == OP_LMUL_IMM) ? OP_LSHL_IMM : OP_ISHL_IMM);
1528 ins->inst_imm = power2;
1531 spec = INS_INFO (ins->opcode);
1533 case OP_IREM_UN_IMM:
1534 case OP_IDIV_UN_IMM: {
1535 int c = ins->inst_imm;
1536 int power2 = mono_is_power_of_two (c);
1539 if (ins->opcode == OP_IREM_UN_IMM) {
1540 ins->opcode = OP_IAND_IMM;
1542 ins->inst_imm = (1 << power2) - 1;
1543 } else if (ins->opcode == OP_IDIV_UN_IMM) {
1544 ins->opcode = OP_ISHR_UN_IMM;
1546 ins->inst_imm = power2;
1549 spec = INS_INFO (ins->opcode);
1553 int c = ins->inst_imm;
1554 int power2 = mono_is_power_of_two (c);
1555 MonoInst *tmp1, *tmp2, *tmp3, *tmp4;
1557 /* FIXME: Move this elsewhere cause its hard to implement it here */
1559 int r1 = mono_alloc_ireg (cfg);
1561 NEW_BIALU_IMM (cfg, tmp1, OP_ISHR_UN_IMM, r1, ins->sreg1, 31);
1562 mono_bblock_insert_after_ins (bb, ins, tmp1);
1563 NEW_BIALU (cfg, tmp2, OP_IADD, r1, r1, ins->sreg1);
1564 mono_bblock_insert_after_ins (bb, tmp1, tmp2);
1565 NEW_BIALU_IMM (cfg, tmp3, OP_ISHR_IMM, ins->dreg, r1, 1);
1566 mono_bblock_insert_after_ins (bb, tmp2, tmp3);
1570 // We allocated a new vreg, so need to restart
1572 } else if (power2 > 0) {
1573 int r1 = mono_alloc_ireg (cfg);
1575 NEW_BIALU_IMM (cfg, tmp1, OP_ISHR_IMM, r1, ins->sreg1, 31);
1576 mono_bblock_insert_after_ins (bb, ins, tmp1);
1577 NEW_BIALU_IMM (cfg, tmp2, OP_ISHR_UN_IMM, r1, r1, (32 - power2));
1578 mono_bblock_insert_after_ins (bb, tmp1, tmp2);
1579 NEW_BIALU (cfg, tmp3, OP_IADD, r1, r1, ins->sreg1);
1580 mono_bblock_insert_after_ins (bb, tmp2, tmp3);
1581 NEW_BIALU_IMM (cfg, tmp4, OP_ISHR_IMM, ins->dreg, r1, power2);
1582 mono_bblock_insert_after_ins (bb, tmp3, tmp4);
1586 // We allocated a new vreg, so need to restart
1593 if (spec [MONO_INST_DEST] != ' ') {
1594 MonoInst *def = defs [ins->dreg];
1596 if (def && (def->opcode == OP_ADD_IMM) && (def->sreg1 == cfg->frame_reg) && (MONO_IS_STORE_MEMBASE (ins))) {
1597 /* ADD_IMM is created by spill_global_vars */
1598 /* cfg->frame_reg is assumed to remain constant */
1599 ins->inst_destbasereg = def->sreg1;
1600 ins->inst_offset += def->inst_imm;
1604 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins) && !vreg_is_volatile (cfg, ins->dreg)) {
1605 defs [ins->dreg] = ins;
1606 def_index [ins->dreg] = ins_index;
1609 if (MONO_IS_CALL (ins))
1610 last_call_index = ins_index;
1618 * mono_local_deadce:
1620 * Get rid of the dead assignments to local vregs like the ones created by the
1624 mono_local_deadce (MonoCompile *cfg)
1627 MonoInst *ins, *prev;
1628 MonoBitSet *used, *defined;
1630 //mono_print_code (cfg, "BEFORE LOCAL-DEADCE");
1633 * Assignments to global vregs can't be eliminated so this pass must come
1634 * after the handle_global_vregs () pass.
1637 used = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
1638 defined = mono_bitset_mp_new_noinit (cfg->mempool, cfg->next_vreg + 1);
1640 /* First pass: collect liveness info */
1641 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1642 /* Manually init the defs entries used by the bblock */
1643 MONO_BB_FOR_EACH_INS (bb, ins) {
1644 const char *spec = INS_INFO (ins->opcode);
1646 if (spec [MONO_INST_DEST] != ' ') {
1647 mono_bitset_clear_fast (used, ins->dreg);
1648 mono_bitset_clear_fast (defined, ins->dreg);
1649 #if SIZEOF_VOID_P == 4
1651 mono_bitset_clear_fast (used, ins->dreg + 1);
1652 mono_bitset_clear_fast (defined, ins->dreg + 1);
1655 if (spec [MONO_INST_SRC1] != ' ') {
1656 mono_bitset_clear_fast (used, ins->sreg1);
1657 #if SIZEOF_VOID_P == 4
1658 mono_bitset_clear_fast (used, ins->sreg1 + 1);
1661 if (spec [MONO_INST_SRC2] != ' ') {
1662 mono_bitset_clear_fast (used, ins->sreg2);
1663 #if SIZEOF_VOID_P == 4
1664 mono_bitset_clear_fast (used, ins->sreg2 + 1);
1670 * Make a reverse pass over the instruction list
1672 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1673 const char *spec = INS_INFO (ins->opcode);
1675 if (ins->opcode == OP_NOP) {
1676 MONO_DELETE_INS (bb, ins);
1680 g_assert (ins->opcode > MONO_CEE_LAST);
1682 if (((ins->opcode == OP_MOVE) || (ins->opcode == OP_VMOVE)) && ins->prev) {
1687 while (def->prev && (def->opcode == OP_NOP))
1689 spec2 = INS_INFO (def->opcode);
1692 * Perform a limited kind of reverse copy propagation, i.e.
1693 * transform B <- FOO; A <- B into A <- FOO
1694 * This isn't copyprop, not deadce, but it can only be performed
1695 * after handle_global_vregs () has run.
1697 if (!get_vreg_to_inst (cfg, ins->sreg1) && (spec2 [MONO_INST_DEST] != ' ') && (def->dreg == ins->sreg1) && !mono_bitset_test_fast (used, ins->sreg1) && !MONO_IS_STORE_MEMBASE (def) && ((spec [MONO_INST_DEST] == 'f' && ins->sreg1 > MONO_MAX_FREGS) || (spec [MONO_INST_DEST] == 'i' && ins->sreg1 > MONO_MAX_IREGS) || (spec [MONO_INST_DEST] == 'v'))) {
1698 if (cfg->verbose_level > 2) {
1699 printf ("\tReverse copyprop in BB%d on ", bb->block_num);
1700 mono_print_ins (ins);
1703 def->dreg = ins->dreg;
1704 MONO_DELETE_INS (bb, ins);
1705 spec = INS_INFO (ins->opcode);
1709 /* Enabling this on x86 could screw up the fp stack */
1710 if (((spec [MONO_INST_DEST] == 'i') && (ins->dreg >= MONO_MAX_IREGS)) ||
1711 ((spec [MONO_INST_DEST] == 'f') && (ins->dreg >= MONO_MAX_FREGS) && !MONO_ARCH_USE_FPSTACK) ||
1712 (spec [MONO_INST_DEST] == 'v')) {
1714 * Assignments to global vregs can only be eliminated if there is another
1715 * assignment to the same vreg later in the same bblock.
1717 if (!mono_bitset_test_fast (used, ins->dreg) &&
1718 (!get_vreg_to_inst (cfg, ins->dreg) || (!bb->extended && !vreg_is_volatile (cfg, ins->dreg) && mono_bitset_test_fast (defined, ins->dreg))) &&
1719 MONO_INS_HAS_NO_SIDE_EFFECT (ins)) {
1720 /* Happens with CMOV instructions */
1721 if (ins->prev && ins->prev->opcode == OP_ICOMPARE_IMM) {
1722 MonoInst *prev = ins->prev;
1723 MONO_DELETE_INS (bb, prev);
1725 //printf ("DEADCE: "); mono_print_ins (ins);
1726 MONO_DELETE_INS (bb, ins);
1727 spec = INS_INFO (ins->opcode);
1730 if (spec [MONO_INST_DEST] != ' ')
1731 mono_bitset_clear_fast (used, ins->dreg);
1734 if (spec [MONO_INST_DEST] != ' ')
1735 mono_bitset_set_fast (defined, ins->dreg);
1736 if (spec [MONO_INST_SRC1] != ' ')
1737 mono_bitset_set_fast (used, ins->sreg1);
1738 if (spec [MONO_INST_SRC2] != ' ')
1739 mono_bitset_set_fast (used, ins->sreg2);
1740 if (MONO_IS_STORE_MEMBASE (ins))
1741 mono_bitset_set_fast (used, ins->dreg);
1743 if (MONO_IS_CALL (ins)) {
1744 MonoCallInst *call = (MonoCallInst*)ins;
1747 if (call->out_ireg_args) {
1748 for (l = call->out_ireg_args; l; l = l->next) {
1749 guint32 regpair, reg;
1751 regpair = (guint32)(gssize)(l->data);
1752 reg = regpair & 0xffffff;
1754 mono_bitset_set_fast (used, reg);
1758 if (call->out_freg_args) {
1759 for (l = call->out_freg_args; l; l = l->next) {
1760 guint32 regpair, reg;
1762 regpair = (guint32)(gssize)(l->data);
1763 reg = regpair & 0xffffff;
1765 mono_bitset_set_fast (used, reg);
1772 //mono_print_code (cfg, "AFTER LOCAL-DEADCE");