2008-07-22 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / local-propagation.c
1 /*
2  * local-propagation.c: Local constant, copy and tree propagation.
3  *
4  * To make some sense of the tree mover, read mono/docs/tree-mover.txt
5  *
6  * Author:
7  *   Paolo Molaro (lupus@ximian.com)
8  *   Dietmar Maurer (dietmar@ximian.com)
9  *   Massimiliano Mantione (massi@ximian.com)
10  *
11  * (C) 2006 Novell, Inc.  http://www.novell.com
12  */
13
14
15 #include <string.h>
16 #include <stdio.h>
17
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mempool.h>
20 #include <mono/metadata/opcodes.h>
21 #include "mini.h"
22
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); \
29         } while (0)
30
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); \
36         } while (0)
37
38 #ifndef MONO_ARCH_IS_OP_MEMBASE
39 #define MONO_ARCH_IS_OP_MEMBASE(opcode) FALSE
40 #endif
41
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
47
48 struct TreeMoverActSlot;
49 /*
50  * A node describing one dependency between a tree and a local
51  */
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 */
64         guchar use_is_direct;
65 } TreeMoverDependencyNode;
66
67 struct TreeMoverTreeMove;
68 /*
69  * A node in a list of affected TreeMoverTreeMove
70  */
71 typedef struct TreeMoverAffectedMove {
72         struct TreeMoverTreeMove *affected_move;
73         struct TreeMoverAffectedMove *next_affected_move;
74 } TreeMoverAffectedMove;
75
76 /*
77  * A node in a list of TreeMoverDependencyFromDeadDefinition
78  */
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;
87
88 /*
89  * A "tree move"
90  */
91 typedef struct TreeMoverTreeMove {
92         /* ACT slot of the defined local */
93         struct TreeMoverActSlot *defined_slot;
94         /* Code location of the definition */
95         MonoInst *definition;
96         /* Code location where the tree must be replaced with the local */
97         MonoInst **use;
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 */
107         guchar move_is_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;
112 } TreeMoverTreeMove;
113
114 /*
115  * An ACT slot (there is one for each local in the ACT array)
116  */
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;
128         /* See [W] flag */
129         guchar waiting_flag;
130         /* See [X] flag */
131         guchar unsafe_flag;
132         /* A "tree forwarding" is in progress */
133         guchar pending_move_is_forwarded;
134 } TreeMoverActSlot;
135
136 /*
137  * Main tree mover work area
138  */
139 typedef struct TreeMover {
140         /* Pool used for allocating everything */
141         MonoMemPool *pool;
142         /* Current method */
143         MonoCompile *cfg;
144         
145         /* Free (recycled) TreeMoverDependencyNode structs */
146         TreeMoverDependencyNode *free_nodes;
147         /* Free (recycled) TreeMoverTreeMove structs */
148         TreeMoverTreeMove *free_moves;
149
150         /* ACT array */
151         TreeMoverActSlot *ACT;
152         /* List of tree moves that could be performed */
153         TreeMoverTreeMove *scheduled_moves;
154
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;
164 } TreeMover;
165
166 inline static TreeMoverDependencyNode*
167 tree_mover_new_node (TreeMover *tree_mover) {
168         TreeMoverDependencyNode *node;
169         
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;
176         } else {
177                 node = (TreeMoverDependencyNode*) mono_mempool_alloc0 (tree_mover->pool, sizeof (TreeMoverDependencyNode));
178         }
179         
180         return node;
181 }
182
183 inline static void
184 tree_mover_new_slot_move (TreeMover *tree_mover, TreeMoverActSlot *slot) {
185         TreeMoverTreeMove *move;
186         
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));
191         } else {
192                 move = (TreeMoverTreeMove*) mono_mempool_alloc0 (tree_mover->pool, sizeof (TreeMoverTreeMove));
193         }
194         
195         slot->pending_move = move;
196 }
197
198 inline static void
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;
204 }
205
206 inline static void
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;
212 }
213
214 inline static void
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;
219 }
220
221 inline static TreeMoverActSlot*
222 tree_mover_slot_from_index (TreeMover *tree_mover, int index) {
223         return & (tree_mover->ACT [index]);
224 }
225
226 inline static int
227 tree_mover_slot_to_index (TreeMover *tree_mover, TreeMoverActSlot *slot) {
228         return slot - tree_mover->ACT;
229 }
230
231 inline static void
232 tree_mover_add_used_node (TreeMover *tree_mover, TreeMoverActSlot *slot, gboolean use_is_direct) {
233         TreeMoverDependencyNode *node;
234         
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;
241         } else {\
242                 tree_mover->used_nodes = node;
243         }\
244         tree_mover->last_used_node = node;
245 }
246
247 inline static void
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;
255         }
256         node->previous_affected_local = NULL;
257 }
258
259 inline static void
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;
263         }
264         if (node->previous_affected_local != NULL) {
265                 node->previous_affected_local->next_affected_local = node->next_affected_local;
266         } else {
267                 TreeMoverActSlot *slot = node->used_slot;
268                 slot->affected_locals = node->next_affected_local;
269         }
270         node->next_affected_local = NULL;
271         node->previous_affected_local = NULL;
272         node->affected_slot = NULL;
273 }
274
275 inline static void
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;
281 }
282
283
284 inline static void
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;
292         }
293 }
294
295 inline static void
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;
302                         }
303                         dependency = dependency->next;
304                 }
305                 slot->pending_move = NULL;
306         }
307 }
308
309 inline static void
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;
315         }
316 }
317
318 inline static void
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;
324         }
325         if (slot->used_locals != NULL) {
326                 tree_mover_dispose_slot_nodes (tree_mover, slot);
327         }
328 }
329
330 inline static void
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);
335                 } else {\
336                         slot->pending_move = NULL;
337                 }
338         }
339         slot->pending_move_is_ready = FALSE;
340         slot->pending_move_is_forwarded = FALSE;
341 }
342
343 inline static void
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);
347 }
348
349 inline static void
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);
354 }
355
356 inline static void
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);
360         }
361 }
362
363 inline static void
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);
367 }
368
369 inline static void
370 tree_mover_kill_act_for_indirect_local_definition (TreeMover *tree_mover, int size) {
371         int i;
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;
376                 }
377                 tree_mover_kill_act_slot_because_it_is_affected (tree_mover, slot);
378         }
379 }
380
381 inline static void
382 tree_mover_kill_act_for_indirect_global_definition (TreeMover *tree_mover, int size) {
383         int i;
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);
388                 }
389         }
390 }
391
392 inline static void
393 tree_mover_kill_act_for_indirect_use (TreeMover *tree_mover, int size) {
394         int i;
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);
398         }
399 }
400
401 inline static void
402 tree_mover_clear_act_recording_moves (TreeMover *tree_mover, int size) {
403         int i;
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);
408         }
409 }
410
411 inline static void
412 tree_mover_set_waiting_flags (TreeMover *tree_mover, int size) {
413         int i;
414         for (i = 0; i < size; i++) {
415                 TreeMoverActSlot *slot = &(tree_mover->ACT [i]);
416                 slot->waiting_flag = TRUE;
417         }
418 }
419
420 inline static void
421 tree_mover_verify_dependency_nodes_are_clear (TreeMover *tree_mover, int size) {
422         int i;
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 ();
428                 }
429                 if (slot->used_locals != NULL) {
430                         printf ("Slot %d has still used variables\n", i); 
431                         g_assert_not_reached ();
432                 }
433                 if (slot->last_used_local != NULL) {
434                         printf ("Slot %d has still a last used variable\n", i); 
435                         g_assert_not_reached ();
436                 }
437         }
438 }
439
440
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  */
450 };
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 */
465 };
466
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)])
470
471 static void
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));
477         }
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));
481         }
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);
487         }
488         printf ("\n");
489 }
490
491 static TreeMoverTreeMove*
492 mono_cprop_copy_values (MonoCompile *cfg, TreeMover *tree_mover, MonoInst *tree, MonoInst **acp)
493 {
494         MonoInst *cp;
495         int arity;
496         TreeMoverTreeMove *pending_move = NULL;
497
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) {
500
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);
509                                         printf ("\n");
510                                 }
511                                 *tree = *cp;
512                         }
513                 } else {
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);
525                                                 printf ("\n");
526                                         }
527                                         tree->inst_i0 = cp;
528                                 }
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);
534                                 printf (" and cp ");
535                                 mono_print_tree (cp);
536                                 printf (" have incompatible types in tree ");
537                                 mono_print_tree (tree);
538                                 printf ("\n");
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);
543                         }
544                 }
545         } else {
546                 if (MONO_DEBUG_LOCAL_PROP) {
547                         printf ("Propagation SKIPPED for inst ");
548                         mono_print_tree (tree);
549                         printf ("\n");
550                 }
551                 if ((tree_mover != NULL) && (cfg->opt & MONO_OPT_CFOLD))
552                         mono_constant_fold_inst (tree, NULL);
553
554                 arity = mono_burg_arity [tree->opcode];
555
556                 if (arity) {
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);
563
564                         }
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);
573                                 }
574                         }
575                         mono_constant_fold_inst (tree, NULL);
576                 }
577         }
578         
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]);
584                 
585                 /* First, handle waiting flag */
586                 if (used_slot->waiting_flag) {
587                         used_slot->unsafe_flag = TRUE;
588                         used_slot->waiting_flag = FALSE;
589                 }
590
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);
594
595                         /* Start working on the pending move... */
596                         pending_move = used_slot->pending_move;
597
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);
608                                         }
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 */
612                                         pending_move = NULL;
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);
620                                         }
621                                         /* ...kill it, and clear the pending_move */
622                                         tree_mover_kill_act_slot_for_use (tree_mover, used_slot);
623                                         pending_move = NULL;
624                                 } else {
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);
630                                         }
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;
635                                         }
636
637                                         /* Setup tree as movable */
638                                         used_slot->pending_move_is_ready = TRUE;
639                                 }
640                         }
641                 } else {
642                         if (MONO_DEBUG_TREE_MOVER) {
643                                 printf ("Tree has side effects, killing slot %d: ", used_index);
644                                 mono_print_tree_nl (tree);
645                         }
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);
650                 }
651 #if MONO_DUMP_TREE_MOVER
652                 tree_mover_print_act_slot ("USE", tree_mover, used_slot);
653 #endif
654         }       
655         return pending_move;
656 }
657
658 static void
659 mono_cprop_invalidate_values (MonoInst *tree, TreeMover *tree_mover, MonoInst **acp, int acp_size)
660 {
661         int arity;
662
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);
668                         }
669                         tree_mover->tree_has_side_effects = TRUE;
670                 }
671         }
672
673         switch (tree->opcode) {
674         case CEE_LDIND_REF:
675         case CEE_LDIND_I1:
676         case CEE_LDIND_I2:
677         case CEE_LDIND_I4:
678         case CEE_LDIND_U1:
679         case CEE_LDIND_U2:
680         case CEE_LDIND_U4:
681         case CEE_LDIND_I8:
682         case CEE_LDIND_R4:
683         case CEE_LDIND_R8:
684         case CEE_LDIND_I:
685         case CEE_LDOBJ:
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);
690                         }
691                         tree_mover->tree_reads_memory = TRUE;
692                 }
693                 break;
694         case CEE_STIND_I:
695         case CEE_STIND_I1:
696         case CEE_STIND_I2:
697         case CEE_STIND_I4:
698         case CEE_STIND_REF:
699         case CEE_STIND_I8:
700         case CEE_STIND_R4:
701         case CEE_STIND_R8:
702         case CEE_STOBJ:
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);
707                                 printf ("\n");
708                         }
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);
714                                 }
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;
718                         }
719                         return;
720                 }
721
722                 break;
723         case OP_CALL:
724         case OP_CALL_REG:
725         case OP_CALLVIRT:
726         case OP_LCALL_REG:
727         case OP_LCALLVIRT:
728         case OP_LCALL:
729         case OP_FCALL_REG:
730         case OP_FCALLVIRT:
731         case OP_FCALL:
732         case OP_VCALL_REG:
733         case OP_VCALLVIRT:
734         case OP_VCALL:
735         case OP_VOIDCALL_REG:
736         case OP_VOIDCALLVIRT:
737         case OP_VOIDCALL:
738         case OP_TRAMPCALL_VTABLE:
739         case OP_CALL_RGCTX:
740         case OP_FCALL_RGCTX:
741         case OP_VOIDCALL_RGCTX:
742         case OP_LCALL_RGCTX:
743         case OP_VCALL_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;
757
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);
762                         }
763                         tree_mover->tree_has_side_effects = TRUE;
764                 }
765
766                 for (i = 0; i < sig->param_count; i++) {
767                         if (sig->params [i]->byref) {
768                                 byref = TRUE;
769                                 break;
770                         }
771                 }
772
773                 if (byref) {
774                         if (MONO_DEBUG_LOCAL_PROP) {
775                                 printf ("Call with byref parameter clears ACP at tree ");
776                                 mono_print_tree (tree);
777                                 printf ("\n");
778                         }
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);
784                                 }
785                                 tree_mover_kill_act_for_indirect_use (tree_mover, acp_size);
786                         }
787                 } else {
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);
792                                 }
793                                 tree_mover_kill_act_for_indirect_global_definition (tree_mover, acp_size);
794                         }
795                 }
796                 return;
797         }
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"
801 #undef OPDEF
802 #define MINI_OP(a1,a2) case a1:
803 #include "simple-mini-ops.h"
804 #undef MINI_OP
805 #undef TREEMOVE_SPECIFIC_OPS
806                 break;
807         default:
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);
812                         }
813                         tree_mover->tree_has_side_effects = TRUE;
814                 }
815                 break;
816         }
817
818         arity = mono_burg_arity [tree->opcode];
819
820         switch (arity) {
821         case 0:
822                 break;
823         case 1:
824                 mono_cprop_invalidate_values (tree->inst_i0, tree_mover, acp, acp_size);
825                 break;
826         case 2:
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);
829                 break;
830         default:
831                 g_assert_not_reached ();
832         }
833 }
834
835 static void
836 mono_local_cprop_bb (MonoCompile *cfg, TreeMover *tree_mover, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
837 {
838         MonoInst *tree = bb->code;
839         int i;
840
841         if (!tree)
842                 return;
843
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);
848                 }
849         }
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);
855                         }
856                         tree_mover->tree_has_side_effects = FALSE;
857                         tree_mover->tree_reads_memory = FALSE;
858                 }
859
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);
865                         }
866                 }
867
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;
873
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);
878                                 printf ("\n");
879                         }
880
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) {
884                                         acp [i] = NULL;
885                                         if (MONO_DEBUG_LOCAL_PROP) {
886                                                 printf ("  Consequently, ACP[%d] is cleared\n", i);
887                                         }
888                                 }
889                         }
890                         
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);
897                                         printf ("\n");
898                                 }
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);
905                                         printf ("\n");
906                                 }
907                         }
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);
916                                         printf ("\n");
917                                 }
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;
925                                         }
926                                 }
927                                 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
928                         }
929                         
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;
935                                 
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);
942                                 }
943
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);
955                                                 }
956
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;
962
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);
972                                                 }
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);
977                                         }
978                                 } else {
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);
984                                         }
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;
997                                 }
998
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;
1004                                         
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);
1010                                                 }
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));
1020                                                 }
1021                                                 tree_mover_link_affected_moves (tree_mover, defined_slot, affected_slot);
1022                                                 tree_mover_link_affected_moves (tree_mover, affected_slot, defined_slot);
1023                                         }
1024                                         tree_mover_unlink_affecting_node (affected_node);
1025                                         
1026                                         if ((next_affected_node != NULL) && (next_affected_node->affected_slot != NULL)) {
1027                                                 affected_node = next_affected_node;
1028                                         } else {
1029                                                 affected_node = defined_slot->affected_locals;
1030                                         }
1031                                 }
1032                                 if (MONO_DUMP_TREE_MOVER) {
1033                                         tree_mover_print_act_slot ("DEFINITION", tree_mover, defined_slot);
1034                                 }
1035                         }
1036                 }
1037
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);
1041                 }
1042
1043                 /*
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) {
1048
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;
1052                   } else {
1053                   tree->inst_target_bb = tree->inst_false_bb;
1054                   }
1055                   }
1056                   }
1057                 */
1058         }
1059         
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);
1065                 }
1066         }
1067 }
1068
1069
1070 #if (MONO_APPLY_TREE_MOVER_TO_SINGLE_METHOD)
1071 static char*
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");
1076         }
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);
1081                         return TRUE;
1082                 } else {
1083                         g_free (method_name);
1084                         return FALSE;
1085                 }
1086         } else {
1087                 return TRUE;
1088         }
1089 }
1090 #endif
1091
1092 #if (MONO_APPLY_TREE_MOVER_TO_COUNTED_METHODS)
1093 static int
1094 mono_tree_mover_method_limit = -1;
1095 static int
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);
1102                 } else {
1103                         mono_tree_mover_method_limit = -2;
1104                 }
1105         }
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);
1112                         
1113                 }
1114                 return (mono_tree_mover_method_count <= mono_tree_mover_method_limit);
1115         } else {
1116                 return TRUE;
1117         }
1118 }
1119 #endif
1120
1121 static void
1122 apply_tree_mover (TreeMover *tree_mover, TreeMoverTreeMove *move) {
1123         TreeMoverDependencyFromDeadDefinition *dependency;
1124         TreeMoverAffectedMove *affected_move;
1125
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);
1131                 }
1132                 return;
1133         }
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);
1139                 }
1140                 return;
1141         }
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);
1148                         }
1149                         return;
1150                 }
1151         }
1152
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);
1156         }
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;
1161
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));
1167                 }
1168                 affected_move->affected_move->skip_this_move = TRUE;
1169                 affected_move = affected_move->next_affected_move;
1170         }
1171
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);
1178                         }
1179                         dependency->dead_definition->opcode = OP_NOP;
1180                         dependency->dead_definition->ssa_op = MONO_SSA_NOP;
1181                 }
1182         }
1183 }
1184
1185 void
1186 mono_local_cprop (MonoCompile *cfg) {
1187         MonoBasicBlock *bb;
1188         MonoInst **acp;
1189         TreeMover *tree_mover;
1190
1191         acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
1192         
1193         if (cfg->opt & MONO_OPT_TREEPROP) {
1194                 MonoMemPool *pool = mono_mempool_new();
1195                 tree_mover = mono_mempool_alloc0(pool, sizeof (TreeMover));
1196                 
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);
1203                         tree_mover = NULL;
1204                 }
1205 #endif
1206 #if (MONO_APPLY_TREE_MOVER_TO_COUNTED_METHODS)
1207                 if (! check_tree_mover_method_count (cfg)) {
1208                         mono_mempool_destroy(tree_mover->pool);
1209                         tree_mover = NULL;
1210                 }
1211 #endif
1212         } else {
1213                 tree_mover = NULL;
1214         }
1215
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);
1219                 }
1220                 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
1221                 mono_local_cprop_bb (cfg, tree_mover, bb, acp, cfg->num_varinfo);
1222         }
1223         
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");
1230                 }
1231                 for (move = tree_mover->scheduled_moves; move != NULL; move = move->next) {
1232                         apply_tree_mover (tree_mover, move);
1233                 }
1234                 if (MONO_DEBUG_TREE_MOVER) {
1235                         mono_print_code (cfg, "AFTER TREE MOVER");
1236                 }
1237                 
1238                 /* Global cleanup of tree mover memory */
1239                 mono_mempool_destroy(tree_mover->pool);
1240         }
1241 }
1242
1243 static inline MonoBitSet* 
1244 mono_bitset_mp_new_noinit (MonoMemPool *mp,  guint32 max_size)
1245 {
1246         int size = mono_bitset_alloc_size (max_size, 0);
1247         gpointer mem;
1248
1249         mem = mono_mempool_alloc (mp, size);
1250         return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
1251 }
1252
1253 /*
1254  * mono_local_cprop2:
1255  *
1256  *  A combined local copy and constant propagation pass.
1257  */
1258 void
1259 mono_local_cprop2 (MonoCompile *cfg)
1260 {
1261         MonoBasicBlock *bb;
1262         MonoInst **defs;
1263         gint32 *def_index;
1264         int max;
1265
1266 restart:
1267
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));
1271
1272         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1273                 MonoInst *ins;
1274                 int ins_index;
1275                 int last_call_index;
1276
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;
1283 #endif
1284                         }
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;
1289 #endif
1290                         }
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;
1295 #endif
1296                         }
1297                 }
1298
1299                 ins_index = 0;
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;
1304
1305                         if (ins->opcode == OP_NOP) {
1306                                 MONO_DELETE_INS (bb, ins);
1307                                 continue;
1308                         }
1309
1310                         g_assert (ins->opcode > MONO_CEE_LAST);
1311
1312                         /* FIXME: Optimize this */
1313                         if (ins->opcode == OP_LDADDR) {
1314                                 MonoInst *var = ins->inst_p0;
1315
1316                                 defs [var->dreg] = NULL;
1317                                 /*
1318                                 if (!MONO_TYPE_ISSTRUCT (var->inst_vtype))
1319                                         break;
1320                                 */
1321                         }
1322
1323                         if (MONO_IS_STORE_MEMBASE (ins)) {
1324                                 sreg = ins->dreg;
1325                                 regtype = 'i';
1326
1327                                 if ((regtype == 'i') && (sreg != -1) && defs [sreg]) {
1328                                         MonoInst *def = defs [sreg];
1329
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);
1333                                                 ins->dreg = vreg;
1334                                         }
1335                                 }
1336                         }
1337
1338                         for (srcindex = 0; srcindex < 2; ++srcindex) {
1339                                 MonoInst *def;
1340
1341                                 regtype = srcindex == 0 ? spec [MONO_INST_SRC1] : spec [MONO_INST_SRC2];
1342                                 sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
1343
1344                                 if ((regtype == ' ') || (sreg == -1) || (!defs [sreg]))
1345                                         continue;
1346
1347                                 def = defs [sreg];
1348
1349                                 /* Copy propagation */
1350                                 /* 
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.
1360                                  */
1361                                 /* Enabling this for floats trips up the fp stack */
1362                                 /* 
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 
1365                                  * conversions.
1366                                  */
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;
1376
1377                                         //printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
1378                                         if (srcindex == 0)
1379                                                 ins->sreg1 = vreg;
1380                                         else
1381                                                 ins->sreg2 = vreg;
1382
1383                                         /* Allow further iterations */
1384                                         srcindex = -1;
1385                                         continue;
1386                                 }
1387
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))) {
1395                                         guint32 opcode2;
1396
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 */
1407                                                         srcindex = -1;
1408                                                         continue;
1409                                                 }
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);
1413                                         }
1414
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;
1421                                                 } else {
1422                                                         ins->inst_imm = def->inst_c0;
1423                                                 }
1424                                                 if (srcindex == 0)
1425                                                         ins->sreg1 = -1;
1426                                                 else
1427                                                         ins->sreg2 = -1;
1428
1429                                                 if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
1430                                                         ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
1431
1432                                                 /* Allow further iterations */
1433                                                 srcindex = -1;
1434                                                 continue;
1435                                         }
1436                                         else {
1437                                                 /* Special cases */
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;
1443 #else
1444                                                         ins->opcode = OP_ADD_IMM;
1445 #endif
1446                                                         ins->inst_imm += def->inst_c0 << ins->backend.shift_amount;
1447                                                         ins->sreg2 = -1;
1448                                                 }
1449 #endif
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;
1454                                                         ins->sreg1 = -1;
1455                                                 }
1456                                         }
1457                                 }
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 */
1460                                         /* 
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.
1463                                          */
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;
1467                                         }
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;
1484                                 }
1485                         }
1486
1487                         /* Do strength reduction here */
1488                         /* FIXME: Add long/float */
1489                         switch (ins->opcode) {
1490                         case OP_MOVE:
1491                                 if (ins->dreg == ins->sreg1) {
1492                                         MONO_DELETE_INS (bb, ins);
1493                                         spec = INS_INFO (ins->opcode);
1494                                 }
1495                                 break;
1496                         case OP_ADD_IMM:
1497                         case OP_IADD_IMM:
1498                         case OP_SUB_IMM:
1499                         case OP_ISUB_IMM:
1500 #if SIZEOF_VOID_P == 8
1501                         case OP_LADD_IMM:
1502                         case OP_LSUB_IMM:
1503 #endif
1504                                 if (ins->inst_imm == 0) {
1505                                         ins->opcode = OP_MOVE;
1506                                         spec = INS_INFO (ins->opcode);
1507                                 }
1508                                 break;
1509                         case OP_MUL_IMM:
1510                         case OP_IMUL_IMM:
1511 #if SIZEOF_VOID_P == 8
1512                         case OP_LMUL_IMM:
1513 #endif
1514                                 if (ins->inst_imm == 0) {
1515                                         ins->opcode = (ins->opcode == OP_LMUL_IMM) ? OP_I8CONST : OP_ICONST;
1516                                         ins->inst_c0 = 0;
1517                                         ins->sreg1 = -1;
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;
1524                                 } else {
1525                                         int power2 = mono_is_power_of_two (ins->inst_imm);
1526                                         if (power2 >= 0) {
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;
1529                                         }
1530                                 }
1531                                 spec = INS_INFO (ins->opcode);
1532                                 break;
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);
1537
1538                                 if (power2 >= 0) {
1539                                         if (ins->opcode == OP_IREM_UN_IMM) {
1540                                                 ins->opcode = OP_IAND_IMM;
1541                                                 ins->sreg2 = -1;
1542                                                 ins->inst_imm = (1 << power2) - 1;
1543                                         } else if (ins->opcode == OP_IDIV_UN_IMM) {
1544                                                 ins->opcode = OP_ISHR_UN_IMM;
1545                                                 ins->sreg2 = -1;
1546                                                 ins->inst_imm = power2;
1547                                         }
1548                                 }
1549                                 spec = INS_INFO (ins->opcode);
1550                                 break;
1551                         }
1552                         case OP_IDIV_IMM: {
1553                                 int c = ins->inst_imm;
1554                                 int power2 = mono_is_power_of_two (c);
1555                                 MonoInst *tmp1, *tmp2, *tmp3, *tmp4;
1556
1557                                 /* FIXME: Move this elsewhere cause its hard to implement it here */
1558                                 if (power2 == 1) {
1559                                         int r1 = mono_alloc_ireg (cfg);
1560
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);
1567
1568                                         NULLIFY_INS (ins);
1569
1570                                         // We allocated a new vreg, so need to restart
1571                                         goto restart;
1572                                 } else if (power2 > 0) {
1573                                         int r1 = mono_alloc_ireg (cfg);
1574
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);
1583
1584                                         NULLIFY_INS (ins);
1585
1586                                         // We allocated a new vreg, so need to restart
1587                                         goto restart;
1588                                 }
1589                                 break;
1590                         }
1591                         }
1592                         
1593                         if (spec [MONO_INST_DEST] != ' ') {
1594                                 MonoInst *def = defs [ins->dreg];
1595
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;
1601                                 }
1602                         }
1603                         
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;
1607                         }
1608
1609                         if (MONO_IS_CALL (ins))
1610                                 last_call_index = ins_index;
1611
1612                         ins_index ++;
1613                 }
1614         }
1615 }
1616
1617 /**
1618  * mono_local_deadce:
1619  *
1620  *   Get rid of the dead assignments to local vregs like the ones created by the 
1621  * copyprop pass.
1622  */
1623 void
1624 mono_local_deadce (MonoCompile *cfg)
1625 {
1626         MonoBasicBlock *bb;
1627         MonoInst *ins, *prev;
1628         MonoBitSet *used, *defined;
1629
1630         //mono_print_code (cfg, "BEFORE LOCAL-DEADCE");
1631
1632         /*
1633          * Assignments to global vregs can't be eliminated so this pass must come
1634          * after the handle_global_vregs () pass.
1635          */
1636
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);
1639
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);
1645
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
1650                                 /* Regpairs */
1651                                 mono_bitset_clear_fast (used, ins->dreg + 1);
1652                                 mono_bitset_clear_fast (defined, ins->dreg + 1);
1653 #endif
1654                         }
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);
1659 #endif
1660                         }
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);
1665 #endif
1666                         }
1667                 }
1668
1669                 /*
1670                  * Make a reverse pass over the instruction list
1671                  */
1672                 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1673                         const char *spec = INS_INFO (ins->opcode);
1674
1675                         if (ins->opcode == OP_NOP) {
1676                                 MONO_DELETE_INS (bb, ins);
1677                                 continue;
1678                         }
1679
1680                         g_assert (ins->opcode > MONO_CEE_LAST);
1681
1682                         if (((ins->opcode == OP_MOVE) || (ins->opcode == OP_VMOVE)) && ins->prev) {
1683                                 MonoInst *def;
1684                                 const char *spec2;
1685
1686                                 def = ins->prev;
1687                                 while (def->prev && (def->opcode == OP_NOP))
1688                                         def = def->prev;
1689                                 spec2 = INS_INFO (def->opcode);
1690
1691                                 /* 
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.
1696                                  */
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);
1701                                         }
1702
1703                                         def->dreg = ins->dreg;
1704                                         MONO_DELETE_INS (bb, ins);
1705                                         spec = INS_INFO (ins->opcode);
1706                                 }
1707                         }
1708
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')) {
1713                                 /* 
1714                                  * Assignments to global vregs can only be eliminated if there is another
1715                                  * assignment to the same vreg later in the same bblock.
1716                                  */
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);
1724                                         }
1725                                         //printf ("DEADCE: "); mono_print_ins (ins);
1726                                         MONO_DELETE_INS (bb, ins);
1727                                         spec = INS_INFO (ins->opcode);
1728                                 }
1729
1730                                 if (spec [MONO_INST_DEST] != ' ')
1731                                         mono_bitset_clear_fast (used, ins->dreg);
1732                         }
1733
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);
1742
1743                         if (MONO_IS_CALL (ins)) {
1744                                 MonoCallInst *call = (MonoCallInst*)ins;
1745                                 GSList *l;
1746
1747                                 if (call->out_ireg_args) {
1748                                         for (l = call->out_ireg_args; l; l = l->next) {
1749                                                 guint32 regpair, reg;
1750
1751                                                 regpair = (guint32)(gssize)(l->data);
1752                                                 reg = regpair & 0xffffff;
1753                                         
1754                                                 mono_bitset_set_fast (used, reg);
1755                                         }
1756                                 }
1757
1758                                 if (call->out_freg_args) {
1759                                         for (l = call->out_freg_args; l; l = l->next) {
1760                                                 guint32 regpair, reg;
1761
1762                                                 regpair = (guint32)(gssize)(l->data);
1763                                                 reg = regpair & 0xffffff;
1764                                         
1765                                                 mono_bitset_set_fast (used, reg);
1766                                         }
1767                                 }
1768                         }
1769                 }
1770         }
1771
1772         //mono_print_code (cfg, "AFTER LOCAL-DEADCE");
1773 }