2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Dataflow driven Load/Store optimizations, uses some ideas from
10 * @author Michael Beck
25 #include "iroptimize.h"
26 #include "irnodehashmap.h"
27 #include "raw_bitset.h"
32 /* maximum number of output Proj's */
33 #define MAX_PROJ ((long)pn_Load_max > (long)pn_Store_max ? (long)pn_Load_max : (long)pn_Store_max)
36 * Mapping an address to an dense ID.
38 typedef struct address_entry_t {
39 unsigned id; /**< The ID */
46 FLAG_KILL_ALL = 1, /**< KILL all addresses */
47 FLAG_KILLED_NODE = 2, /**< this node was killed */
48 FLAG_EXCEPTION = 4, /**< this node has exception flow */
49 FLAG_IGNORE = 8, /**< ignore this node (volatile or other) */
53 * A value: This represents a value stored at a given address in
54 * memory. Do not confuse with values from value numbering.
56 typedef struct value_t value_t;
58 ir_node *address; /**< the address of this value */
59 ir_node *value; /**< the value itself */
60 ir_mode *mode; /**< the mode of the value */
61 unsigned id; /**< address id */
65 * A memop describes an memory-related operation.
66 * These are Loads/Store and all other ops that might modify
67 * memory (Calls, CopyB) or causing exceptions.
69 typedef struct memop_t memop_t;
71 value_t value; /**< the value of this memop: only defined for Load/Store */
72 ir_node *node; /**< the memory op itself */
73 ir_node *mem; /**< the memory FROM this node */
74 ir_node *replace; /**< the replacement node if this memop is replaced */
75 memop_t *next; /**< links to the next memory op in the block in forward order. */
76 memop_t *prev; /**< links to the previous memory op in the block in forward order. */
77 unsigned flags; /**< memop flags */
78 ir_node *projs[MAX_PROJ+1]; /**< Projs of this memory op */
82 * Additional data for every basic block.
84 typedef struct block_t block_t;
86 memop_t *memop_forward; /**< topologically sorted list of memory ops in this block */
87 memop_t *memop_backward; /**< last memop in the list */
88 unsigned *avail_out; /**< out-set of available addresses */
89 memop_t **id_2_memop_avail; /**< maps avail address ids to memops */
90 unsigned *anticL_in; /**< in-set of anticipated Load addresses */
91 memop_t **id_2_memop_antic; /**< maps anticipated address ids to memops */
92 ir_node *block; /**< the associated block */
93 block_t *forward_next; /**< next block entry for forward iteration */
94 block_t *backward_next; /**< next block entry for backward iteration */
95 memop_t *avail; /**< used locally for the avail map */
96 memop_t **trans_results; /**< used to cached translated nodes due antic calculation. */
100 * Metadata for this pass.
102 typedef struct ldst_env_t {
103 struct obstack obst; /**< obstack for temporary data */
104 ir_nodehashmap_t adr_map; /**< Map addresses to */
105 block_t *forward; /**< Inverse post-order list of all blocks Start->End */
106 block_t *backward; /**< Inverse post-order list of all blocks End->Start */
107 ir_node *end_bl; /**< end block of the current graph */
108 unsigned *curr_set; /**< current set of addresses */
109 memop_t **curr_id_2_memop; /**< current map of address ids to memops */
110 unsigned curr_adr_id; /**< number for address mapping */
111 unsigned n_mem_ops; /**< number of memory operations (Loads/Stores) */
112 size_t rbs_size; /**< size of all bitsets in bytes */
113 int max_cfg_preds; /**< maximum number of block cfg predecessors */
114 int changed; /**< Flags for changed graph state */
116 ir_node **id_2_address; /**< maps an id to the used address */
120 /* the one and only environment */
125 static firm_dbg_module_t *dbg;
128 * Dumps the block list.
130 * @param ldst environment
132 static void dump_block_list(ldst_env *env)
138 for (entry = env->forward; entry != NULL; entry = entry->forward_next) {
139 DB((dbg, LEVEL_2, "%+F {", entry->block));
142 for (op = entry->memop_forward; op != NULL; op = op->next) {
144 DB((dbg, LEVEL_2, "\n\t"));
146 DB((dbg, LEVEL_2, "%+F", op->node));
147 if ((op->flags & FLAG_KILL_ALL) == FLAG_KILL_ALL)
148 DB((dbg, LEVEL_2, "X"));
149 else if (op->flags & FLAG_KILL_ALL)
150 DB((dbg, LEVEL_2, "K"));
151 DB((dbg, LEVEL_2, ", "));
155 DB((dbg, LEVEL_2, "\n}\n\n"));
160 * Dumps the current set.
162 * @param bl current block
163 * @param s name of the set
165 static void dump_curr(block_t *bl, const char *s)
167 size_t end = env.rbs_size - 1;
171 DB((dbg, LEVEL_2, "%s[%+F] = {", s, bl->block));
173 for (pos = rbitset_next(env.curr_set, 0, 1); pos < end; pos = rbitset_next(env.curr_set, pos + 1, 1)) {
174 memop_t *op = env.curr_id_2_memop[pos];
177 DB((dbg, LEVEL_2, "\n\t"));
180 DB((dbg, LEVEL_2, "<%+F, %+F>, ", op->value.address, op->value.value));
183 DB((dbg, LEVEL_2, "\n}\n"));
187 static void dump_block_list(ldst_env *env)
191 static void dump_curr(block_t *bl, const char *s)
196 #endif /* DEBUG_libfirm */
198 /** Get the block entry for a block node */
199 static block_t *get_block_entry(const ir_node *block)
201 assert(is_Block(block));
203 return (block_t*)get_irn_link(block);
206 /** Get the memop entry for a memory operation node */
207 static memop_t *get_irn_memop(const ir_node *irn)
209 assert(! is_Block(irn));
210 return (memop_t*)get_irn_link(irn);
214 * Walk over the memory edges from definition to users.
215 * This ensures, that even operation without memory output are found.
217 * @param irn start node
218 * @param pre pre walker function
219 * @param post post walker function
220 * @param ctx context parameter for the walker functions
222 static void walk_memory(ir_node *irn, irg_walk_func *pre, irg_walk_func *post, void *ctx)
226 mark_irn_visited(irn);
231 mode = get_irn_mode(irn);
232 if (mode == mode_M) {
233 /* every successor uses memory */
234 for (unsigned i = get_irn_n_outs(irn); i-- > 0; ) {
235 ir_node *succ = get_irn_out(irn, i);
237 if (! irn_visited(succ))
238 walk_memory(succ, pre, post, ctx);
240 } else if (mode == mode_T) {
241 /* only some Proj's uses memory */
242 for (unsigned i = get_irn_n_outs(irn); i-- > 0; ) {
243 ir_node *proj = get_irn_out(irn, i);
245 if (get_irn_mode(proj) == mode_M && ! irn_visited(proj))
246 walk_memory(proj, pre, post, ctx);
254 * Walks over all memory nodes of a graph.
257 * @param pre pre walker function
258 * @param post post walker function
259 * @param ctx context parameter for the walker functions
261 static void walk_memory_irg(ir_graph *irg, irg_walk_func pre, irg_walk_func post, void *ctx)
263 inc_irg_visited(irg);
265 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
268 * there are two possible sources for memory: initial_mem and nomem
269 * we ignore nomem as this should NOT change the memory
271 walk_memory(get_irg_initial_mem(irg), pre, post, ctx);
273 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
277 * Register an address and allocate a (sparse, 0..n) ID for it.
279 * @param adr the IR-node representing the address
281 * @return the allocated id
283 static unsigned register_address(ir_node *adr)
285 address_entry *entry;
289 if (is_Confirm(adr)) {
290 adr = get_Confirm_value(adr);
294 entry = ir_nodehashmap_get(address_entry, &env.adr_map, adr);
298 entry = OALLOC(&env.obst, address_entry);
300 entry->id = env.curr_adr_id++;
301 ir_nodehashmap_insert(&env.adr_map, adr, entry);
303 DB((dbg, LEVEL_3, "ADDRESS %+F has ID %u\n", adr, entry->id));
305 ARR_APP1(ir_node *, env.id_2_address, adr);
313 * translate an address through a Phi node into a given predecessor
316 * @param address the address
317 * @param block the block
318 * @param pos the position of the predecessor in block
320 static ir_node *phi_translate(ir_node *address, const ir_node *block, int pos)
322 if (is_Phi(address) && get_nodes_block(address) == block)
323 address = get_Phi_pred(address, pos);
328 * Walker: allocate an block entry for every block
329 * and register all potential addresses.
331 static void prepare_blocks(ir_node *irn, void *ctx)
336 block_t *entry = OALLOC(&env.obst, block_t);
339 entry->memop_forward = NULL;
340 entry->memop_backward = NULL;
341 entry->avail_out = NULL;
342 entry->id_2_memop_avail = NULL;
343 entry->anticL_in = NULL;
344 entry->id_2_memop_antic = NULL;
346 entry->forward_next = NULL;
347 entry->backward_next = NULL;
349 entry->trans_results = NULL;
350 set_irn_link(irn, entry);
352 set_Block_phis(irn, NULL);
354 /* use block marks to track unreachable blocks */
355 set_Block_mark(irn, 0);
357 n = get_Block_n_cfgpreds(irn);
358 if (n > env.max_cfg_preds)
359 env.max_cfg_preds = n;
361 ir_mode *mode = get_irn_mode(irn);
363 if (mode_is_reference(mode)) {
365 * Register ALL possible addresses: this is overkill yet but
366 * simpler then doing it for all possible translated addresses
367 * (which would be sufficient in the moment.
369 (void)register_address(irn);
375 * Post-Walker, link in all Phi's
377 static void link_phis(ir_node *irn, void *ctx)
382 ir_node *block = get_nodes_block(irn);
383 add_Block_phi(block, irn);
388 * Block walker: creates the inverse post-order list for the CFG.
390 static void inverse_post_order(ir_node *block, void *ctx)
392 block_t *entry = get_block_entry(block);
396 /* mark this block IS reachable from start */
397 set_Block_mark(block, 1);
399 /* create the list in inverse order */
400 entry->forward_next = env.forward;
403 /* remember the first visited (last in list) entry, needed for later */
404 if (env.backward == NULL)
405 env.backward = entry;
409 * Block walker: create backward links for the memops of a block.
411 static void collect_backward(ir_node *block, void *ctx)
413 block_t *entry = get_block_entry(block);
419 * Do NOT link in the end block yet. We want it to be
420 * the first in the list. This is NOT guaranteed by the walker
421 * if we have endless loops.
423 if (block != env.end_bl) {
424 entry->backward_next = env.backward;
426 /* create the list in inverse order */
427 env.backward = entry;
430 /* create backward links for all memory ops */
432 for (op = entry->memop_forward; op != NULL; op = op->next) {
436 entry->memop_backward = last;
442 * @param irn the IR-node representing the memop or NULL
443 * if this is a translated (virtual) memop
445 * @return the allocated memop
447 static memop_t *alloc_memop(ir_node *irn)
449 memop_t *m = OALLOC(&env.obst, memop_t);
451 m->value.address = NULL;
452 m->value.value = NULL;
453 m->value.mode = NULL;
461 memset(m->projs, 0, sizeof(m->projs));
464 set_irn_link(irn, m);
469 * Create a memop for a Phi-replacement.
471 * @param op the memop to clone
472 * @param phi the Phi-node representing the new value
474 static memop_t *clone_memop_phi(memop_t *op, ir_node *phi)
476 memop_t *m = OALLOC(&env.obst, memop_t);
478 m->value = op->value;
479 m->value.value = phi;
486 set_irn_link(phi, m);
491 * Return the memory properties of a call node.
493 * @param call the call node
495 * return a bitset of mtp_property_const and mtp_property_pure
497 static unsigned get_Call_memory_properties(ir_node *call)
499 ir_type *call_tp = get_Call_type(call);
500 unsigned prop = get_method_additional_properties(call_tp);
502 /* check first the call type */
503 if ((prop & (mtp_property_const|mtp_property_pure)) == 0) {
504 /* try the called entity */
505 ir_node *ptr = get_Call_ptr(call);
507 if (is_SymConst_addr_ent(ptr)) {
508 ir_entity *ent = get_SymConst_entity(ptr);
510 prop = get_entity_additional_properties(ent);
513 return prop & (mtp_property_const|mtp_property_pure);
517 * Returns an entity if the address ptr points to a constant one.
519 * @param ptr the address
521 * @return an entity or NULL
523 static ir_entity *find_constant_entity(ir_node *ptr)
526 if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
527 return get_SymConst_entity(ptr);
528 } else if (is_Sel(ptr)) {
529 ir_entity *ent = get_Sel_entity(ptr);
530 ir_type *tp = get_entity_owner(ent);
532 /* Do not fiddle with polymorphism. */
533 if (is_Class_type(tp) &&
534 ((get_entity_n_overwrites(ent) != 0) ||
535 (get_entity_n_overwrittenby(ent) != 0) ) )
538 if (is_Array_type(tp)) {
542 for (i = 0, n = get_Sel_n_indexs(ptr); i < n; ++i) {
544 ir_tarval *tlower, *tupper;
545 ir_node *index = get_Sel_index(ptr, i);
546 ir_tarval *tv = computed_value(index);
548 /* check if the index is constant */
549 if (tv == tarval_bad)
552 bound = get_array_lower_bound(tp, i);
553 tlower = computed_value(bound);
554 bound = get_array_upper_bound(tp, i);
555 tupper = computed_value(bound);
557 if (tlower == tarval_bad || tupper == tarval_bad)
560 if (tarval_cmp(tv, tlower) == ir_relation_less)
562 if (tarval_cmp(tupper, tv) == ir_relation_less)
565 /* ok, bounds check finished */
569 if (get_entity_linkage(ent) == IR_LINKAGE_CONSTANT)
573 ptr = get_Sel_ptr(ptr);
574 } else if (is_Add(ptr)) {
575 ir_node *l = get_Add_left(ptr);
576 ir_node *r = get_Add_right(ptr);
578 if (get_irn_mode(l) == get_irn_mode(ptr) && is_Const(r))
580 else if (get_irn_mode(r) == get_irn_mode(ptr) && is_Const(l))
585 /* for now, we support only one addition, reassoc should fold all others */
586 if (! is_SymConst(ptr) && !is_Sel(ptr))
588 } else if (is_Sub(ptr)) {
589 ir_node *l = get_Sub_left(ptr);
590 ir_node *r = get_Sub_right(ptr);
592 if (get_irn_mode(l) == get_irn_mode(ptr) && is_Const(r))
596 /* for now, we support only one subtraction, reassoc should fold all others */
597 if (! is_SymConst(ptr) && !is_Sel(ptr))
605 * Return the Selection index of a Sel node from dimension n
607 static long get_Sel_array_index_long(ir_node *n, int dim)
609 ir_node *index = get_Sel_index(n, dim);
610 return get_tarval_long(get_Const_tarval(index));
613 typedef struct path_entry {
615 struct path_entry *next;
619 static ir_node *rec_find_compound_ent_value(ir_node *ptr, path_entry *next)
621 path_entry entry, *p;
622 ir_entity *ent, *field;
623 ir_initializer_t *initializer;
629 if (is_SymConst(ptr)) {
631 ent = get_SymConst_entity(ptr);
632 initializer = get_entity_initializer(ent);
633 for (p = next; p != NULL;) {
634 if (initializer->kind != IR_INITIALIZER_COMPOUND)
636 n = get_initializer_compound_n_entries(initializer);
637 tp = get_entity_type(ent);
639 if (is_Array_type(tp)) {
640 ent = get_array_element_entity(tp);
645 initializer = get_initializer_compound_value(initializer, 0);
651 initializer = get_initializer_compound_value(initializer, p->index);
656 tp = get_entity_type(ent);
657 while (is_Array_type(tp)) {
658 ent = get_array_element_entity(tp);
659 tp = get_entity_type(ent);
661 n = get_initializer_compound_n_entries(initializer);
664 initializer = get_initializer_compound_value(initializer, 0);
667 switch (initializer->kind) {
668 case IR_INITIALIZER_CONST:
669 return get_initializer_const_value(initializer);
670 case IR_INITIALIZER_TARVAL:
671 case IR_INITIALIZER_NULL:
675 } else if (is_Sel(ptr)) {
676 entry.ent = field = get_Sel_entity(ptr);
677 tp = get_entity_owner(field);
678 if (is_Array_type(tp)) {
679 assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
680 entry.index = get_Sel_array_index_long(ptr, 0) - get_array_lower_bound_int(tp, 0);
682 size_t i, n_members = get_compound_n_members(tp);
683 for (i = 0; i < n_members; ++i) {
684 if (get_compound_member(tp, i) == field)
687 if (i >= n_members) {
688 /* not found: should NOT happen */
693 return rec_find_compound_ent_value(get_Sel_ptr(ptr), &entry);
694 } else if (is_Add(ptr)) {
699 ir_node *l = get_Add_left(ptr);
700 ir_node *r = get_Add_right(ptr);
703 tv = get_Const_tarval(r);
706 tv = get_Const_tarval(l);
710 mode = get_tarval_mode(tv);
712 /* ptr must be a Sel or a SymConst, this was checked in find_constant_entity() */
714 field = get_Sel_entity(ptr);
716 field = get_SymConst_entity(ptr);
719 /* count needed entries */
721 for (ent = field;;) {
722 tp = get_entity_type(ent);
723 if (! is_Array_type(tp))
725 ent = get_array_element_entity(tp);
728 /* should be at least ONE entry */
732 /* allocate the right number of entries */
733 NEW_ARR_A(path_entry, p, pos);
737 for (ent = field;;) {
739 ir_tarval *sz, *tv_index, *tlower, *tupper;
743 tp = get_entity_type(ent);
744 if (! is_Array_type(tp))
746 ent = get_array_element_entity(tp);
748 p[pos].next = &p[pos + 1];
750 size = get_type_size_bytes(get_entity_type(ent));
751 sz = new_tarval_from_long(size, mode);
753 tv_index = tarval_div(tv, sz);
754 tv = tarval_mod(tv, sz);
756 if (tv_index == tarval_bad || tv == tarval_bad)
759 assert(get_array_n_dimensions(tp) == 1 && "multiarrays not implemented");
760 bound = get_array_lower_bound(tp, 0);
761 tlower = computed_value(bound);
762 bound = get_array_upper_bound(tp, 0);
763 tupper = computed_value(bound);
765 if (tlower == tarval_bad || tupper == tarval_bad)
768 if (tarval_cmp(tv_index, tlower) == ir_relation_less)
770 if (tarval_cmp(tupper, tv_index) == ir_relation_less)
773 /* ok, bounds check finished */
774 index = get_tarval_long(tv_index);
775 p[pos].index = index;
778 if (! tarval_is_null(tv)) {
779 /* hmm, wrong access */
782 p[pos - 1].next = next;
783 return rec_find_compound_ent_value(ptr, p);
784 } else if (is_Sub(ptr)) {
785 ir_node *l = get_Sub_left(ptr);
786 ir_node *r = get_Sub_right(ptr);
789 tv = get_Const_tarval(r);
796 static ir_node *find_compound_ent_value(ir_node *ptr)
798 return rec_find_compound_ent_value(ptr, NULL);
802 * Mark a Load memop to be replace by a definition
804 * @param op the Load memop
806 static void mark_replace_load(memop_t *op, ir_node *def)
809 op->flags |= FLAG_KILLED_NODE;
814 * Mark a Store memop to be removed.
816 * @param op the Store memop
818 static void mark_remove_store(memop_t *op)
820 op->flags |= FLAG_KILLED_NODE;
825 * Update a memop for a Load.
829 static void update_Load_memop(memop_t *m)
831 ir_node *load = m->node;
835 if (get_Load_volatility(load) == volatility_is_volatile)
836 m->flags |= FLAG_IGNORE;
838 ptr = get_Load_ptr(load);
840 m->value.address = ptr;
842 for (unsigned i = get_irn_n_outs(load); i-- > 0; ) {
843 ir_node *proj = get_irn_out(load, i);
846 /* beware of keep edges */
850 pn = get_Proj_proj(proj);
854 m->value.value = proj;
855 m->value.mode = get_irn_mode(proj);
857 case pn_Load_X_except:
858 m->flags |= FLAG_EXCEPTION;
863 case pn_Load_X_regular:
866 panic("Unsupported Proj from Load %+F", proj);
870 /* check if we can determine the entity that will be loaded */
871 ent = find_constant_entity(ptr);
873 if (ent != NULL && get_entity_visibility(ent) != ir_visibility_external) {
874 /* a static allocation that is not external: there should be NO exception
875 * when loading even if we cannot replace the load itself. */
876 ir_node *value = NULL;
878 /* no exception, clear the m fields as it might be checked later again */
879 if (m->projs[pn_Load_X_except]) {
880 ir_graph *irg = get_irn_irg(ptr);
881 exchange(m->projs[pn_Load_X_except], new_r_Bad(irg, mode_X));
882 m->projs[pn_Load_X_except] = NULL;
883 m->flags &= ~FLAG_EXCEPTION;
886 if (m->projs[pn_Load_X_regular]) {
887 exchange(m->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
888 m->projs[pn_Load_X_regular] = NULL;
892 if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) {
893 if (ent->initializer) {
894 /* new style initializer */
895 value = find_compound_ent_value(ptr);
898 value = can_replace_load_by_const(load, value);
902 /* we completely replace the load by this value */
903 DB((dbg, LEVEL_1, "Replacing Load %+F by constant %+F\n", m->node, value));
904 mark_replace_load(m, value);
909 if (m->value.value != NULL && !(m->flags & FLAG_IGNORE)) {
910 /* only create an address if this node is NOT killed immediately or ignored */
911 m->value.id = register_address(ptr);
914 /* no user, KILL it */
915 mark_replace_load(m, NULL);
920 * Update a memop for a Store.
924 static void update_Store_memop(memop_t *m)
926 ir_node *store = m->node;
927 ir_node *adr = get_Store_ptr(store);
929 if (get_Store_volatility(store) == volatility_is_volatile) {
930 m->flags |= FLAG_IGNORE;
932 /* only create an address if this node is NOT ignored */
933 m->value.id = register_address(adr);
937 m->value.address = adr;
939 for (unsigned i = get_irn_n_outs(store); i-- > 0; ) {
940 ir_node *proj = get_irn_out(store, i);
943 /* beware of keep edges */
947 pn = get_Proj_proj(proj);
950 case pn_Store_X_except:
951 m->flags |= FLAG_EXCEPTION;
956 case pn_Store_X_regular:
959 panic("Unsupported Proj from Store %+F", proj);
962 m->value.value = get_Store_value(store);
963 m->value.mode = get_irn_mode(m->value.value);
967 * Update a memop for a Call.
971 static void update_Call_memop(memop_t *m)
973 ir_node *call = m->node;
974 unsigned prop = get_Call_memory_properties(call);
976 if (prop & mtp_property_const) {
977 /* A constant call did NOT use memory at all, we
978 can kick it from the list. */
979 } else if (prop & mtp_property_pure) {
980 /* pure calls READ memory */
983 m->flags = FLAG_KILL_ALL;
985 for (unsigned i = get_irn_n_outs(call); i-- > 0; ) {
986 ir_node *proj = get_irn_out(call, i);
988 /* beware of keep edges */
992 switch (get_Proj_proj(proj)) {
993 case pn_Call_X_except:
994 m->flags |= FLAG_EXCEPTION;
1004 * Update a memop for a Div/Mod.
1006 * @param m the memop
1008 static void update_Div_memop(memop_t *m)
1010 ir_node *div = m->node;
1012 for (unsigned i = get_irn_n_outs(div); i-- > 0; ) {
1013 ir_node *proj = get_irn_out(div, i);
1015 /* beware of keep edges */
1019 switch (get_Proj_proj(proj)) {
1020 case pn_Div_X_except:
1021 m->flags |= FLAG_EXCEPTION;
1030 static void update_Mod_memop(memop_t *m)
1032 ir_node *div = m->node;
1034 for (unsigned i = get_irn_n_outs(div); i-- > 0; ) {
1035 ir_node *proj = get_irn_out(div, i);
1037 /* beware of keep edges */
1041 switch (get_Proj_proj(proj)) {
1042 case pn_Mod_X_except:
1043 m->flags |= FLAG_EXCEPTION;
1053 * Update a memop for a Phi.
1055 * @param m the memop
1057 static void update_Phi_memop(memop_t *m)
1059 /* the Phi is its own mem */
1064 * Memory walker: collect all memory ops and build topological lists.
1066 static void collect_memops(ir_node *irn, void *ctx)
1074 /* we can safely ignore ProjM's except the initial memory */
1075 ir_graph *irg = get_irn_irg(irn);
1076 if (irn != get_irg_initial_mem(irg))
1080 op = alloc_memop(irn);
1081 block = get_nodes_block(irn);
1082 entry = get_block_entry(block);
1085 update_Phi_memop(op);
1086 /* Phis must be always placed first */
1087 op->next = entry->memop_forward;
1088 entry->memop_forward = op;
1089 if (entry->memop_backward == NULL)
1090 entry->memop_backward = op;
1092 switch (get_irn_opcode(irn)) {
1094 update_Load_memop(op);
1097 update_Store_memop(op);
1100 update_Call_memop(op);
1107 /* initial memory */
1112 /* we can those to find the memory edge */
1115 update_Div_memop(op);
1118 update_Mod_memop(op);
1122 /* TODO: handle some builtins */
1124 /* unsupported operation */
1125 op->flags = FLAG_KILL_ALL;
1129 /* all other should be placed last */
1130 if (entry->memop_backward == NULL) {
1131 entry->memop_forward = entry->memop_backward = op;
1133 entry->memop_backward->next = op;
1134 entry->memop_backward = op;
1140 * Find an address in the current set.
1142 * @param value the value to be searched for
1144 * @return a memop for the value or NULL if the value does
1145 * not exists in the set or cannot be converted into
1146 * the requested mode
1148 static memop_t *find_address(const value_t *value)
1150 if (rbitset_is_set(env.curr_set, value->id)) {
1151 memop_t *res = env.curr_id_2_memop[value->id];
1153 if (res->value.mode == value->mode)
1155 /* allow hidden casts */
1156 if (get_mode_arithmetic(res->value.mode) == irma_twos_complement &&
1157 get_mode_arithmetic(value->mode) == irma_twos_complement &&
1158 get_mode_size_bits(res->value.mode) == get_mode_size_bits(value->mode))
1165 * Find an address in the avail_out set.
1167 * @param bl the block
1169 static memop_t *find_address_avail(const block_t *bl, unsigned id, const ir_mode *mode)
1171 if (rbitset_is_set(bl->avail_out, id)) {
1172 memop_t *res = bl->id_2_memop_avail[id];
1174 if (res->value.mode == mode)
1176 /* allow hidden casts */
1177 if (get_mode_arithmetic(res->value.mode) == irma_twos_complement &&
1178 get_mode_arithmetic(mode) == irma_twos_complement &&
1179 get_mode_size_bits(res->value.mode) == get_mode_size_bits(mode))
1186 * Kill all addresses from the current set.
1188 static void kill_all(void)
1190 rbitset_clear_all(env.curr_set, env.rbs_size);
1193 rbitset_set(env.curr_set, env.rbs_size - 1);
1197 * Kill memops that are not alias free due to a Store value from the current set.
1199 * @param value the Store value
1201 static void kill_memops(const value_t *value)
1203 size_t end = env.rbs_size - 1;
1206 for (pos = rbitset_next(env.curr_set, 0, 1); pos < end; pos = rbitset_next(env.curr_set, pos + 1, 1)) {
1207 memop_t *op = env.curr_id_2_memop[pos];
1209 if (ir_no_alias != get_alias_relation(value->address, value->mode,
1210 op->value.address, op->value.mode)) {
1211 rbitset_clear(env.curr_set, pos);
1212 env.curr_id_2_memop[pos] = NULL;
1213 DB((dbg, LEVEL_2, "KILLING %+F because of possible alias address %+F\n", op->node, value->address));
1219 * Add the value of a memop to the current set.
1221 * @param op the memory op
1223 static void add_memop(memop_t *op)
1225 rbitset_set(env.curr_set, op->value.id);
1226 env.curr_id_2_memop[op->value.id] = op;
1230 * Add the value of a memop to the avail_out set.
1232 * @param bl the block
1233 * @param op the memory op
1235 static void add_memop_avail(block_t *bl, memop_t *op)
1237 rbitset_set(bl->avail_out, op->value.id);
1238 bl->id_2_memop_avail[op->value.id] = op;
1242 * Check, if we can convert a value of one mode to another mode
1243 * without changing the representation of bits.
1245 * @param from the original mode
1246 * @param to the destination mode
1248 static int can_convert_to(const ir_mode *from, const ir_mode *to)
1250 if (get_mode_arithmetic(from) == irma_twos_complement &&
1251 get_mode_arithmetic(to) == irma_twos_complement &&
1252 get_mode_size_bits(from) == get_mode_size_bits(to))
1258 * Add a Conv to the requested mode if needed.
1260 * @param irn the IR-node to convert
1261 * @param mode the destination mode
1263 * @return the possible converted node or NULL
1264 * if the conversion is not possible
1266 static ir_node *conv_to(ir_node *irn, ir_mode *mode)
1268 ir_mode *other = get_irn_mode(irn);
1269 if (other != mode) {
1270 /* different modes: check if conversion is possible without changing the bits */
1271 if (can_convert_to(other, mode)) {
1272 ir_node *block = get_nodes_block(irn);
1273 return new_r_Conv(block, irn, mode);
1275 /* otherwise not possible ... yet */
1282 * Update the address of an value if this address was a load result
1283 * and the load is killed now.
1285 * @param value the value whose address is updated
1287 static void update_address(value_t *value)
1289 if (is_Proj(value->address)) {
1290 ir_node *load = get_Proj_pred(value->address);
1292 if (is_Load(load)) {
1293 const memop_t *op = get_irn_memop(load);
1295 if (op->flags & FLAG_KILLED_NODE)
1296 value->address = op->replace;
1302 * Do forward dataflow analysis on the given block and calculate the
1303 * GEN and KILL in the current (avail) set.
1305 * @param bl the block
1307 static void calc_gen_kill_avail(block_t *bl)
1312 for (op = bl->memop_forward; op != NULL; op = op->next) {
1313 switch (get_irn_opcode(op->node)) {
1321 if (! (op->flags & (FLAG_KILLED_NODE|FLAG_IGNORE))) {
1322 /* do we have this already? */
1325 update_address(&op->value);
1326 other = find_address(&op->value);
1327 if (other != NULL && other != op) {
1328 def = conv_to(other->value.value, op->value.mode);
1330 #ifdef DEBUG_libfirm
1331 if (is_Store(other->node)) {
1333 DB((dbg, LEVEL_1, "RAW %+F <- %+F(%+F)\n", op->node, def, other->node));
1336 DB((dbg, LEVEL_1, "RAR %+F <- %+F(%+F)\n", op->node, def, other->node));
1339 mark_replace_load(op, def);
1340 /* do NOT change the memop table */
1344 /* add this value */
1349 if (! (op->flags & FLAG_KILLED_NODE)) {
1350 /* do we have this store already */
1353 update_address(&op->value);
1354 other = find_address(&op->value);
1355 if (other != NULL) {
1356 if (is_Store(other->node)) {
1357 if (op != other && !(other->flags & FLAG_IGNORE) &&
1358 get_nodes_block(other->node) == get_nodes_block(op->node)) {
1360 * A WAW in the same block we can kick the first store.
1361 * This is a shortcut: we know that the second Store will be anticipated
1364 DB((dbg, LEVEL_1, "WAW %+F <- %+F\n", other->node, op->node));
1365 mark_remove_store(other);
1366 /* FIXME: a Load might be get freed due to this killed store */
1368 } else if (other->value.value == op->value.value && !(op->flags & FLAG_IGNORE)) {
1370 DB((dbg, LEVEL_1, "WAR %+F <- %+F\n", op->node, other->node));
1371 mark_remove_store(op);
1372 /* do NOT change the memop table */
1376 /* KILL all possible aliases */
1377 kill_memops(&op->value);
1378 /* add this value */
1383 if (op->flags & FLAG_KILL_ALL)
1390 * Do forward dataflow analysis on a given block to calculate the avail_out set
1391 * for this block only.
1393 * @param block the block
1395 static void forward_avail(block_t *bl)
1397 /* fill the data from the current block */
1398 env.curr_id_2_memop = bl->id_2_memop_avail;
1399 env.curr_set = bl->avail_out;
1401 calc_gen_kill_avail(bl);
1402 dump_curr(bl, "Avail_out");
1406 * Do backward dataflow analysis on a given block to calculate the antic set
1407 * of Loaded addresses.
1409 * @param bl the block
1411 * @return non-zero if the set has changed since last iteration
1413 static int backward_antic(block_t *bl)
1416 ir_node *block = bl->block;
1417 int n = get_Block_n_cfg_outs(block);
1420 ir_node *succ = get_Block_cfg_out(block, 0);
1421 block_t *succ_bl = get_block_entry(succ);
1422 int pred_pos = get_Block_cfgpred_pos(succ, block);
1423 size_t end = env.rbs_size - 1;
1428 if (bl->trans_results == NULL) {
1429 /* allocate the translate cache */
1430 bl->trans_results = OALLOCNZ(&env.obst, memop_t*, env.curr_adr_id);
1433 /* check for partly redundant values */
1434 for (pos = rbitset_next(succ_bl->anticL_in, 0, 1);
1436 pos = rbitset_next(succ_bl->anticL_in, pos + 1, 1)) {
1438 * do Phi-translation here: Note that at this point the nodes are
1439 * not changed, so we can safely cache the results.
1440 * However: Loads of Load results ARE bad, because we have no way
1441 to translate them yet ...
1443 memop_t *op = bl->trans_results[pos];
1445 /* not yet translated */
1446 ir_node *adr, *trans_adr;
1448 op = succ_bl->id_2_memop_antic[pos];
1449 adr = op->value.address;
1451 trans_adr = phi_translate(adr, succ, pred_pos);
1452 if (trans_adr != adr) {
1453 /* create a new entry for the translated one */
1456 new_op = alloc_memop(NULL);
1457 new_op->value.address = trans_adr;
1458 new_op->value.id = register_address(trans_adr);
1459 new_op->value.mode = op->value.mode;
1460 new_op->node = op->node; /* we need the node to decide if Load/Store */
1461 new_op->flags = op->flags;
1463 bl->trans_results[pos] = new_op;
1467 env.curr_id_2_memop[op->value.id] = op;
1468 rbitset_set(env.curr_set, op->value.id);
1471 ir_node *succ = get_Block_cfg_out(block, 0);
1472 block_t *succ_bl = get_block_entry(succ);
1475 rbitset_copy(env.curr_set, succ_bl->anticL_in, env.rbs_size);
1476 memcpy(env.curr_id_2_memop, succ_bl->id_2_memop_antic, env.rbs_size * sizeof(env.curr_id_2_memop[0]));
1478 /* Hmm: probably we want kill merges of Loads ans Stores here */
1479 for (i = n - 1; i > 0; --i) {
1480 ir_node *succ = get_Block_cfg_out(bl->block, i);
1481 block_t *succ_bl = get_block_entry(succ);
1483 rbitset_and(env.curr_set, succ_bl->anticL_in, env.rbs_size);
1486 /* block ends with a noreturn call */
1490 dump_curr(bl, "AnticL_out");
1492 for (op = bl->memop_backward; op != NULL; op = op->prev) {
1493 switch (get_irn_opcode(op->node)) {
1501 if (! (op->flags & (FLAG_KILLED_NODE|FLAG_IGNORE))) {
1507 if (! (op->flags & FLAG_KILLED_NODE)) {
1508 /* a Store: check which memops must be killed */
1509 kill_memops(&op->value);
1513 if (op->flags & FLAG_KILL_ALL)
1518 memcpy(bl->id_2_memop_antic, env.curr_id_2_memop, env.rbs_size * sizeof(env.curr_id_2_memop[0]));
1519 if (! rbitsets_equal(bl->anticL_in, env.curr_set, env.rbs_size)) {
1521 rbitset_copy(bl->anticL_in, env.curr_set, env.rbs_size);
1522 dump_curr(bl, "AnticL_in*");
1525 dump_curr(bl, "AnticL_in");
1530 * Replace a Load memop by a already known value.
1532 * @param op the Load memop
1534 static void replace_load(memop_t *op)
1536 ir_node *load = op->node;
1537 ir_node *def = skip_Id(op->replace);
1542 DB((dbg, LEVEL_1, "Replacing %+F by definition %+F\n", load, is_Proj(def) ? get_Proj_pred(def) : def));
1544 if (op->flags & FLAG_EXCEPTION) {
1545 /* bad: this node is unused and executed for exception only */
1546 DB((dbg, LEVEL_1, "Unused %+F executed for exception only ...\n", load));
1549 DB((dbg, LEVEL_1, "Killing unused %+F\n", load));
1552 if (op->mem != NULL) {
1553 /* in rare cases a Load might have NO memory */
1554 exchange(op->mem, get_Load_mem(load));
1556 proj = op->projs[pn_Load_res];
1558 mode = get_irn_mode(proj);
1559 if (get_irn_mode(def) != mode) {
1561 dbg_info *db = get_irn_dbg_info(load);
1562 ir_node *block = get_nodes_block(proj);
1563 def = new_rd_Conv(db, block, def, mode);
1565 exchange(proj, def);
1567 proj = op->projs[pn_Load_X_except];
1569 ir_graph *irg = get_irn_irg(load);
1570 exchange(proj, new_r_Bad(irg, mode_X));
1572 proj = op->projs[pn_Load_X_regular];
1574 exchange(proj, new_r_Jmp(get_nodes_block(load)));
1579 * Remove a Store memop.
1581 * @param op the Store memop
1583 static void remove_store(memop_t *op)
1585 ir_node *store = op->node;
1588 DB((dbg, LEVEL_1, "Removing %+F\n", store));
1590 if (op->mem != NULL) {
1591 /* in rare cases a Store might have no memory */
1592 exchange(op->mem, get_Store_mem(store));
1594 proj = op->projs[pn_Store_X_except];
1596 ir_graph *irg = get_irn_irg(store);
1597 exchange(proj, new_r_Bad(irg, mode_X));
1599 proj = op->projs[pn_Store_X_regular];
1601 exchange(proj, new_r_Jmp(get_nodes_block(store)));
1607 * Do all necessary replacements for a given block.
1609 * @param bl the block
1611 static void do_replacements(block_t *bl)
1615 for (op = bl->memop_forward; op != NULL; op = op->next) {
1616 if (op->flags & FLAG_KILLED_NODE) {
1617 switch (get_irn_opcode(op->node)) {
1630 * Calculate the Avail_out sets for all basic blocks.
1632 static void calcAvail(void)
1634 memop_t **tmp_memop = env.curr_id_2_memop;
1635 unsigned *tmp_set = env.curr_set;
1638 /* calculate avail_out */
1639 DB((dbg, LEVEL_2, "Calculate Avail_out\n"));
1641 /* iterate over all blocks in in any order, skip the start block */
1642 for (bl = env.forward->forward_next; bl != NULL; bl = bl->forward_next) {
1646 /* restore the current sets */
1647 env.curr_id_2_memop = tmp_memop;
1648 env.curr_set = tmp_set;
1652 * Calculate the Antic_in sets for all basic blocks.
1654 static void calcAntic(void)
1658 /* calculate antic_out */
1659 DB((dbg, LEVEL_2, "Calculate Antic_in\n"));
1664 DB((dbg, LEVEL_2, "Iteration %d:\n=========\n", i));
1668 /* over all blocks in reverse post order */
1669 for (bl = env.backward->backward_next; bl != NULL; bl = bl->backward_next) {
1670 need_iter |= backward_antic(bl);
1673 } while (need_iter);
1674 DB((dbg, LEVEL_2, "Get anticipated Load set after %d iterations\n", i));
1678 * Return the node representing the last memory in a block.
1680 * @param bl the block
1682 static ir_node *find_last_memory(block_t *bl)
1685 if (bl->memop_backward != NULL) {
1686 return bl->memop_backward->mem;
1688 /* if there is NO memory in this block, go to the dominator */
1689 bl = get_block_entry(get_Block_idom(bl->block));
1694 * Reroute all memory users of old memory
1695 * to a new memory IR-node.
1697 * @param omem the old memory IR-node
1698 * @param nmem the new memory IR-node
1700 static void reroute_all_mem_users(ir_node *omem, ir_node *nmem)
1702 for (unsigned i = get_irn_n_outs(omem); i-- > 0; ) {
1704 ir_node *user = get_irn_out_ex(omem, i, &n_pos);
1706 set_irn_n(user, n_pos, nmem);
1709 /* all edges previously point to omem now point to nmem */
1710 nmem->o.out = omem->o.out;
1714 * Reroute memory users of old memory that are dominated by a given block
1715 * to a new memory IR-node.
1717 * @param omem the old memory IR-node
1718 * @param nmem the new memory IR-node
1719 * @param pass_bl the block the memory must pass
1721 static void reroute_mem_through(ir_node *omem, ir_node *nmem, ir_node *pass_bl)
1723 unsigned n = get_irn_n_outs(omem);
1724 ir_def_use_edges *new_out = OALLOCF(&env.obst, ir_def_use_edges, edges, n);
1727 for (unsigned i = 0; i < n; ++i) {
1729 ir_node *user = get_irn_out_ex(omem, i, &n_pos);
1730 ir_node *use_bl = get_nodes_block(user);
1734 use_bl = get_Block_cfgpred_block(use_bl, n_pos);
1736 if (block_dominates(pass_bl, use_bl)) {
1737 /* found an user that is dominated */
1738 new_out->edges[j].pos = n_pos;
1739 new_out->edges[j].use = user;
1742 set_irn_n(user, n_pos, nmem);
1745 new_out->n_edges = j;
1747 /* Modify the out structure: we create a new out edge array on our
1748 temporary obstack here. This should be no problem, as we invalidate the
1749 edges at the end either. */
1750 /* first entry is used for the length */
1751 nmem->o.out = new_out;
1755 * insert Loads, making partly redundant Loads fully redundant
1757 static int insert_Load(block_t *bl)
1759 ir_node *block = bl->block;
1760 int i, n = get_Block_n_cfgpreds(block);
1761 size_t end = env.rbs_size - 1;
1763 DB((dbg, LEVEL_3, "processing %+F\n", block));
1766 /* might still happen for an unreachable block (end for instance) */
1774 NEW_ARR_A(ir_node *, ins, n);
1776 rbitset_set_all(env.curr_set, env.rbs_size);
1778 /* More than one predecessors, calculate the join for all avail_outs ignoring unevaluated
1779 Blocks. These put in Top anyway. */
1780 for (i = n - 1; i >= 0; --i) {
1781 ir_node *pred = skip_Proj(get_Block_cfgpred(block, i));
1782 ir_node *blk = get_nodes_block(pred);
1785 pred_bl = get_block_entry(blk);
1786 rbitset_and(env.curr_set, pred_bl->avail_out, env.rbs_size);
1788 if (is_Load(pred) || is_Store(pred)) {
1789 /* We reached this block by an exception from a Load or Store:
1790 * the memop creating the exception was NOT completed than, kill it
1792 memop_t *exc_op = get_irn_memop(pred);
1793 rbitset_clear(env.curr_set, exc_op->value.id);
1798 * Ensure that all values are in the map: build Phi's if necessary:
1799 * Note: the last bit is the sentinel and ALWAYS set, so end with -2.
1801 for (pos = 0; pos < env.rbs_size - 1; ++pos) {
1802 if (! rbitset_is_set(env.curr_set, pos))
1803 env.curr_id_2_memop[pos] = NULL;
1806 memop_t *first = NULL;
1807 ir_mode *mode = NULL;
1809 for (i = 0; i < n; ++i) {
1810 ir_node *pred = get_Block_cfgpred_block(bl->block, i);
1811 block_t *pred_bl = get_block_entry(pred);
1813 memop_t *mop = pred_bl->id_2_memop_avail[pos];
1814 if (first == NULL) {
1816 ins[0] = first->value.value;
1817 mode = get_irn_mode(ins[0]);
1819 /* no Phi needed so far */
1820 env.curr_id_2_memop[pos] = first;
1822 ins[i] = conv_to(mop->value.value, mode);
1823 if (ins[i] != ins[0]) {
1824 if (ins[i] == NULL) {
1825 /* conversion failed */
1826 env.curr_id_2_memop[pos] = NULL;
1827 rbitset_clear(env.curr_set, pos);
1836 ir_node *phi = new_r_Phi(bl->block, n, ins, mode);
1837 memop_t *phiop = alloc_memop(phi);
1839 phiop->value = first->value;
1840 phiop->value.value = phi;
1842 /* no need to link it in, as it is a DATA phi */
1844 env.curr_id_2_memop[pos] = phiop;
1846 DB((dbg, LEVEL_3, "Created new %+F on merging value for address %+F\n", phi, first->value.address));
1851 /* only one predecessor, simply copy the map */
1852 ir_node *pred = get_Block_cfgpred_block(bl->block, 0);
1853 block_t *pred_bl = get_block_entry(pred);
1855 rbitset_copy(env.curr_set, pred_bl->avail_out, env.rbs_size);
1857 memcpy(env.curr_id_2_memop, pred_bl->id_2_memop_avail, env.rbs_size * sizeof(bl->id_2_memop_avail[0]));
1863 /* check for partly redundant values */
1864 for (pos = rbitset_next(bl->anticL_in, 0, 1);
1866 pos = rbitset_next(bl->anticL_in, pos + 1, 1)) {
1867 memop_t *op = bl->id_2_memop_antic[pos];
1868 int have_some, all_same;
1871 if (rbitset_is_set(env.curr_set, pos)) {
1876 assert(is_Load(op->node));
1878 DB((dbg, LEVEL_3, "anticipated %+F\n", op->node));
1883 for (i = n - 1; i >= 0; --i) {
1884 ir_node *pred = get_Block_cfgpred_block(block, i);
1885 block_t *pred_bl = get_block_entry(pred);
1886 ir_mode *mode = op->value.mode;
1890 adr = phi_translate(op->value.address, block, i);
1891 DB((dbg, LEVEL_3, ".. using address %+F in pred %d\n", adr, i));
1892 e = find_address_avail(pred_bl, register_address(adr), mode);
1894 ir_node *ef_block = get_nodes_block(adr);
1895 if (! block_dominates(ef_block, pred)) {
1896 /* cannot place a copy here */
1898 DB((dbg, LEVEL_3, "%+F cannot be moved into predecessor %+F\n", op->node, pred));
1901 DB((dbg, LEVEL_3, "%+F is not available in predecessor %+F\n", op->node, pred));
1902 pred_bl->avail = NULL;
1905 if (e->value.mode != mode && !can_convert_to(e->value.mode, mode)) {
1906 /* cannot create a Phi due to different modes */
1912 DB((dbg, LEVEL_3, "%+F is available for %+F in predecessor %+F\n", e->node, op->node, pred));
1915 else if (first != e->node)
1919 if (have_some && !all_same) {
1920 ir_mode *mode = op->value.mode;
1924 NEW_ARR_A(ir_node *, in, n);
1926 for (i = n - 1; i >= 0; --i) {
1927 ir_node *pred = get_Block_cfgpred_block(block, i);
1928 block_t *pred_bl = get_block_entry(pred);
1930 if (pred_bl->avail == NULL) {
1931 /* create a new Load here and make to make it fully redundant */
1932 dbg_info *db = get_irn_dbg_info(op->node);
1933 ir_node *last_mem = find_last_memory(pred_bl);
1934 ir_node *load, *def, *adr;
1937 assert(last_mem != NULL);
1938 adr = phi_translate(op->value.address, block, i);
1939 load = new_rd_Load(db, pred, last_mem, adr, mode, cons_none);
1940 def = new_r_Proj(load, mode, pn_Load_res);
1941 DB((dbg, LEVEL_1, "Created new %+F in %+F for party redundant %+F\n", load, pred, op->node));
1943 new_op = alloc_memop(load);
1944 new_op->mem = new_r_Proj(load, mode_M, pn_Load_M);
1945 new_op->value.address = adr;
1946 new_op->value.id = op->value.id;
1947 new_op->value.mode = mode;
1948 new_op->value.value = def;
1950 new_op->projs[pn_Load_M] = new_op->mem;
1951 new_op->projs[pn_Load_res] = def;
1953 new_op->prev = pred_bl->memop_backward;
1954 if (pred_bl->memop_backward != NULL)
1955 pred_bl->memop_backward->next = new_op;
1957 pred_bl->memop_backward = new_op;
1959 if (pred_bl->memop_forward == NULL)
1960 pred_bl->memop_forward = new_op;
1962 if (get_nodes_block(last_mem) == pred) {
1963 /* We have add a new last memory op in pred block.
1964 If pred had already a last mem, reroute all memory
1966 reroute_all_mem_users(last_mem, new_op->mem);
1968 /* reroute only those memory going through the pre block */
1969 reroute_mem_through(last_mem, new_op->mem, pred);
1972 /* we added this load at the end, so it will be avail anyway */
1973 add_memop_avail(pred_bl, new_op);
1974 pred_bl->avail = new_op;
1976 in[i] = conv_to(pred_bl->avail->value.value, mode);
1978 phi = new_r_Phi(block, n, in, mode);
1979 DB((dbg, LEVEL_1, "Created new %+F in %+F for now redundant %+F\n", phi, block, op->node));
1981 phi_op = clone_memop_phi(op, phi);
1987 /* recalculate avail by gen and kill */
1988 calc_gen_kill_avail(bl);
1990 /* always update the map after gen/kill, as values might have been changed due to RAR/WAR/WAW */
1991 memcpy(bl->id_2_memop_avail, env.curr_id_2_memop, env.rbs_size * sizeof(env.curr_id_2_memop[0]));
1993 if (!rbitsets_equal(bl->avail_out, env.curr_set, env.rbs_size)) {
1994 /* the avail set has changed */
1995 rbitset_copy(bl->avail_out, env.curr_set, env.rbs_size);
1996 dump_curr(bl, "Avail_out*");
1999 dump_curr(bl, "Avail_out");
2004 * Insert Loads upwards.
2006 static void insert_Loads_upwards(void)
2011 /* recalculate antic_out and insert Loads */
2012 DB((dbg, LEVEL_2, "Inserting Loads\n"));
2016 DB((dbg, LEVEL_2, "Iteration %d:\n=========\n", i));
2020 /* over all blocks in reverse post order, skip the start block */
2021 for (bl = env.forward->forward_next; bl != NULL; bl = bl->forward_next) {
2022 need_iter |= insert_Load(bl);
2025 } while (need_iter);
2027 DB((dbg, LEVEL_2, "Finished Load inserting after %d iterations\n", i));
2030 void opt_ldst(ir_graph *irg)
2034 FIRM_DBG_REGISTER(dbg, "firm.opt.ldst");
2036 DB((dbg, LEVEL_1, "\nDoing Load/Store optimization on %+F\n", irg));
2038 assure_irg_properties(irg,
2039 IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES /* we need landing pads */
2040 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE
2041 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
2042 | IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
2043 | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
2045 if (get_opt_alias_analysis()) {
2046 assure_irp_globals_entity_usage_computed();
2049 obstack_init(&env.obst);
2050 ir_nodehashmap_init(&env.adr_map);
2053 env.backward = NULL;
2054 env.curr_adr_id = 0;
2056 env.max_cfg_preds = 0;
2058 env.end_bl = get_irg_end_block(irg);
2059 #ifdef DEBUG_libfirm
2060 env.id_2_address = NEW_ARR_F(ir_node *, 0);
2063 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_BLOCK_MARK);
2065 /* first step: allocate block entries. Note that some blocks might be
2066 unreachable here. Using the normal walk ensures that ALL blocks are initialized. */
2067 irg_walk_graph(irg, prepare_blocks, link_phis, NULL);
2069 /* produce an inverse post-order list for the CFG: this links only reachable
2071 ir_node *const start_block = get_irg_start_block(irg);
2072 irg_out_block_walk(start_block, NULL, inverse_post_order, NULL);
2074 if (! get_Block_mark(env.end_bl)) {
2076 * The end block is NOT reachable due to endless loops
2077 * or no_return calls.
2078 * Place the end block last.
2079 * env.backward points to the last block in the list for this purpose.
2081 env.backward->forward_next = get_block_entry(env.end_bl);
2083 set_Block_mark(env.end_bl, 1);
2086 /* second step: find and sort all memory ops */
2087 walk_memory_irg(irg, collect_memops, NULL, NULL);
2089 #ifdef DEBUG_libfirm
2090 /* check that the backward map is correct */
2091 assert((unsigned)ARR_LEN(env.id_2_address) == env.curr_adr_id);
2094 if (env.n_mem_ops == 0) {
2099 /* create the backward links. */
2100 env.backward = NULL;
2101 irg_block_walk_graph(irg, NULL, collect_backward, NULL);
2103 /* link the end block in */
2104 bl = get_block_entry(env.end_bl);
2105 bl->backward_next = env.backward;
2108 /* check that we really start with the start / end block */
2109 assert(env.forward->block == start_block);
2110 assert(env.backward->block == env.end_bl);
2112 /* create address sets: for now, only the existing addresses are allowed plus one
2113 needed for the sentinel */
2114 env.rbs_size = env.curr_adr_id + 1;
2116 /* create the current set */
2117 env.curr_set = rbitset_obstack_alloc(&env.obst, env.rbs_size);
2118 rbitset_set(env.curr_set, env.rbs_size - 1);
2119 env.curr_id_2_memop = NEW_ARR_DZ(memop_t*, &env.obst, env.rbs_size);
2121 for (bl = env.forward; bl != NULL; bl = bl->forward_next) {
2122 /* set sentinel bits */
2123 bl->avail_out = rbitset_obstack_alloc(&env.obst, env.rbs_size);
2124 rbitset_set(bl->avail_out, env.rbs_size - 1);
2126 bl->id_2_memop_avail = NEW_ARR_DZ(memop_t*, &env.obst, env.rbs_size);
2128 bl->anticL_in = rbitset_obstack_alloc(&env.obst, env.rbs_size);
2129 rbitset_set(bl->anticL_in, env.rbs_size - 1);
2131 bl->id_2_memop_antic = NEW_ARR_DZ(memop_t*, &env.obst, env.rbs_size);
2134 (void) dump_block_list;
2139 insert_Loads_upwards();
2142 /* over all blocks in reverse post order */
2143 for (bl = env.forward; bl != NULL; bl = bl->forward_next) {
2144 do_replacements(bl);
2147 /* not only invalidate but free them. We might allocate new out arrays
2148 on our obstack which will be deleted yet. */
2149 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
2152 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
2155 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_BLOCK_MARK);
2156 ir_nodehashmap_destroy(&env.adr_map);
2157 obstack_free(&env.obst, NULL);
2159 #ifdef DEBUG_libfirm
2160 DEL_ARR_F(env.id_2_address);
2164 ir_graph_pass_t *opt_ldst_pass(const char *name)
2166 return def_graph_pass(name ? name : "ldst_df", opt_ldst);