2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Load/Store optimizations.
15 #include "iroptimize.h"
17 #include "irgraph_t.h"
25 #include "dbginfo_t.h"
26 #include "iropt_dbg.h"
33 #include "irnodehashmap.h"
39 /** The debug handle. */
40 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
43 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
45 #define MAX_PROJ IMAX(IMAX((long)pn_Load_max, (long)pn_Store_max), (long)pn_Call_max)
48 DF_CHANGED = 1, /**< data flow changed */
49 CF_CHANGED = 2, /**< control flow changed */
55 typedef struct walk_env_t {
56 struct obstack obst; /**< list of all stores */
57 unsigned changes; /**< a bitmask of graph changes */
60 /** A Load/Store info. */
61 typedef struct ldst_info_t {
62 ir_node *projs[MAX_PROJ+1]; /**< list of Proj's of this node */
63 ir_node *exc_block; /**< the exception block if available */
64 int exc_idx; /**< predecessor index in the exception block */
65 unsigned visited; /**< visited counter for breaking loops */
69 * flags for control flow.
72 BLOCK_HAS_COND = 1, /**< Block has conditional control flow */
73 BLOCK_HAS_EXC = 2 /**< Block has exceptional control flow */
79 typedef struct block_info_t {
80 unsigned flags; /**< flags for the block */
83 /** the master visited flag for loop detection. */
84 static unsigned master_visited = 0;
86 #define INC_MASTER() ++master_visited
87 #define MARK_NODE(info) (info)->visited = master_visited
88 #define NODE_VISITED(info) (info)->visited >= master_visited
91 * get the Load/Store info of a node
93 static ldst_info_t *get_ldst_info(ir_node *node, struct obstack *obst)
95 ldst_info_t *info = (ldst_info_t*)get_irn_link(node);
98 info = OALLOCZ(obst, ldst_info_t);
99 set_irn_link(node, info);
105 * get the Block info of a node
107 static block_info_t *get_block_info(ir_node *node, struct obstack *obst)
109 block_info_t *info = (block_info_t*)get_irn_link(node);
112 info = OALLOCZ(obst, block_info_t);
113 set_irn_link(node, info);
119 * update the projection info for a Load/Store
121 static unsigned update_projs(ldst_info_t *info, ir_node *proj)
123 long nr = get_Proj_proj(proj);
125 assert(0 <= nr && nr <= MAX_PROJ && "Wrong proj from LoadStore");
127 if (info->projs[nr]) {
128 /* there is already one, do CSE */
129 exchange(proj, info->projs[nr]);
133 info->projs[nr] = proj;
139 * update the exception block info for a Load/Store node.
141 * @param info the load/store info struct
142 * @param block the exception handler block for this load/store
143 * @param pos the control flow input of the block
145 static unsigned update_exc(ldst_info_t *info, ir_node *block, int pos)
147 assert(info->exc_block == NULL && "more than one exception block found");
149 info->exc_block = block;
155 * walker, collects all Load/Store/Proj nodes
157 * walks from Start -> End
159 static void collect_nodes(ir_node *node, void *env)
161 walk_env_t *wenv = (walk_env_t *)env;
162 unsigned opcode = get_irn_opcode(node);
163 ir_node *pred, *blk, *pred_blk;
164 ldst_info_t *ldst_info;
166 if (opcode == iro_Proj) {
167 pred = get_Proj_pred(node);
168 opcode = get_irn_opcode(pred);
170 if (opcode == iro_Load || opcode == iro_Store || opcode == iro_Call) {
171 ldst_info = get_ldst_info(pred, &wenv->obst);
173 wenv->changes |= update_projs(ldst_info, node);
176 * Place the Proj's to the same block as the
177 * predecessor Load. This is always ok and prevents
178 * "non-SSA" form after optimizations if the Proj
179 * is in a wrong block.
181 blk = get_nodes_block(node);
182 pred_blk = get_nodes_block(pred);
183 if (blk != pred_blk) {
184 wenv->changes |= DF_CHANGED;
185 set_nodes_block(node, pred_blk);
188 } else if (opcode == iro_Block) {
191 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
192 ir_node *pred_block, *proj;
193 block_info_t *bl_info;
196 pred = proj = get_Block_cfgpred(node, i);
199 pred = get_Proj_pred(proj);
200 is_exc = is_x_except_Proj(proj);
203 /* ignore Bad predecessors, they will be removed later */
207 pred_block = get_nodes_block(pred);
208 bl_info = get_block_info(pred_block, &wenv->obst);
210 if (is_fragile_op(pred) && is_exc)
211 bl_info->flags |= BLOCK_HAS_EXC;
212 else if (is_irn_forking(pred))
213 bl_info->flags |= BLOCK_HAS_COND;
215 opcode = get_irn_opcode(pred);
216 if (is_exc && (opcode == iro_Load || opcode == iro_Store || opcode == iro_Call)) {
217 ldst_info = get_ldst_info(pred, &wenv->obst);
219 wenv->changes |= update_exc(ldst_info, node, i);
226 * Returns an entity if the address ptr points to a constant one.
228 * @param ptr the address
230 * @return an entity or NULL
232 static ir_entity *find_constant_entity(ir_node *ptr)
235 if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
236 return get_SymConst_entity(ptr);
237 } else if (is_Sel(ptr)) {
238 ir_entity *ent = get_Sel_entity(ptr);
239 ir_type *tp = get_entity_owner(ent);
241 /* Do not fiddle with polymorphism. */
242 if (is_Class_type(tp) &&
243 ((get_entity_n_overwrites(ent) != 0) ||
244 (get_entity_n_overwrittenby(ent) != 0) ) )
247 if (is_Array_type(tp)) {
251 for (i = 0, n = get_Sel_n_indexs(ptr); i < n; ++i) {
253 ir_tarval *tlower, *tupper;
254 ir_node *index = get_Sel_index(ptr, i);
255 ir_tarval *tv = computed_value(index);
257 /* check if the index is constant */
258 if (tv == tarval_bad)
261 bound = get_array_lower_bound(tp, i);
262 tlower = computed_value(bound);
263 bound = get_array_upper_bound(tp, i);
264 tupper = computed_value(bound);
266 if (tlower == tarval_bad || tupper == tarval_bad)
269 if (tarval_cmp(tv, tlower) == ir_relation_less)
271 if (tarval_cmp(tupper, tv) == ir_relation_less)
274 /* ok, bounds check finished */
278 if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT)
282 ptr = get_Sel_ptr(ptr);
283 } else if (is_Add(ptr)) {
284 ir_node *l = get_Add_left(ptr);
285 ir_node *r = get_Add_right(ptr);
287 if (get_irn_mode(l) == get_irn_mode(ptr) && is_Const(r))
289 else if (get_irn_mode(r) == get_irn_mode(ptr) && is_Const(l))
294 /* for now, we support only one addition, reassoc should fold all others */
295 if (! is_SymConst(ptr) && !is_Sel(ptr))
297 } else if (is_Sub(ptr)) {
298 ir_node *l = get_Sub_left(ptr);
299 ir_node *r = get_Sub_right(ptr);
301 if (get_irn_mode(l) == get_irn_mode(ptr) && is_Const(r))
305 /* for now, we support only one substraction, reassoc should fold all others */
306 if (! is_SymConst(ptr) && !is_Sel(ptr))
314 * Return the Selection index of a Sel node from dimension n
316 static long get_Sel_array_index_long(ir_node *n, int dim)
318 ir_node *index = get_Sel_index(n, dim);
319 return get_tarval_long(get_Const_tarval(index));
322 typedef struct path_entry {
324 struct path_entry *next;
328 static ir_node *rec_find_compound_ent_value(ir_node *ptr, path_entry *next)
330 path_entry entry, *p;
331 ir_entity *ent, *field;
332 ir_initializer_t *initializer;
338 if (is_SymConst(ptr)) {
340 ent = get_SymConst_entity(ptr);
341 initializer = get_entity_initializer(ent);
342 for (p = next; p != NULL;) {
343 if (initializer->kind != IR_INITIALIZER_COMPOUND)
345 n = get_initializer_compound_n_entries(initializer);
346 tp = get_entity_type(ent);
348 if (is_Array_type(tp)) {
349 ent = get_array_element_entity(tp);
354 initializer = get_initializer_compound_value(initializer, 0);
360 initializer = get_initializer_compound_value(initializer, p->index);
365 tp = get_entity_type(ent);
366 while (is_Array_type(tp)) {
367 ent = get_array_element_entity(tp);
368 tp = get_entity_type(ent);
370 n = get_initializer_compound_n_entries(initializer);
373 initializer = get_initializer_compound_value(initializer, 0);
376 switch (initializer->kind) {
377 case IR_INITIALIZER_CONST:
378 return get_initializer_const_value(initializer);
379 case IR_INITIALIZER_TARVAL:
380 case IR_INITIALIZER_NULL:
384 } else if (is_Sel(ptr)) {
385 entry.ent = field = get_Sel_entity(ptr);
386 tp = get_entity_owner(field);
387 if (is_Array_type(tp)) {
388 assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
389 entry.index = get_Sel_array_index_long(ptr, 0) - get_array_lower_bound_int(tp, 0);
391 size_t i, n_members = get_compound_n_members(tp);
392 for (i = 0; i < n_members; ++i) {
393 if (get_compound_member(tp, i) == field)
396 if (i >= n_members) {
397 /* not found: should NOT happen */
402 return rec_find_compound_ent_value(get_Sel_ptr(ptr), &entry);
403 } else if (is_Add(ptr)) {
408 ir_node *l = get_Add_left(ptr);
409 ir_node *r = get_Add_right(ptr);
412 tv = get_Const_tarval(r);
415 tv = get_Const_tarval(l);
419 mode = get_tarval_mode(tv);
421 /* ptr must be a Sel or a SymConst, this was checked in find_constant_entity() */
423 field = get_Sel_entity(ptr);
425 field = get_SymConst_entity(ptr);
428 /* count needed entries */
430 for (ent = field;;) {
431 tp = get_entity_type(ent);
432 if (! is_Array_type(tp))
434 ent = get_array_element_entity(tp);
437 /* should be at least ONE entry */
441 /* allocate the right number of entries */
442 NEW_ARR_A(path_entry, p, pos);
446 for (ent = field;;) {
448 ir_tarval *sz, *tv_index, *tlower, *tupper;
452 tp = get_entity_type(ent);
453 if (! is_Array_type(tp))
455 ent = get_array_element_entity(tp);
457 p[pos].next = &p[pos + 1];
459 size = get_type_size_bytes(get_entity_type(ent));
460 sz = new_tarval_from_long(size, mode);
462 tv_index = tarval_div(tv, sz);
463 tv = tarval_mod(tv, sz);
465 if (tv_index == tarval_bad || tv == tarval_bad)
468 assert(get_array_n_dimensions(tp) == 1 && "multiarrays not implemented");
469 bound = get_array_lower_bound(tp, 0);
470 tlower = computed_value(bound);
471 bound = get_array_upper_bound(tp, 0);
472 tupper = computed_value(bound);
474 if (tlower == tarval_bad || tupper == tarval_bad)
477 if (tarval_cmp(tv_index, tlower) == ir_relation_less)
479 if (tarval_cmp(tupper, tv_index) == ir_relation_less)
482 /* ok, bounds check finished */
483 index = get_tarval_long(tv_index);
484 p[pos].index = index;
487 if (! tarval_is_null(tv)) {
488 /* hmm, wrong access */
491 p[pos - 1].next = next;
492 return rec_find_compound_ent_value(ptr, p);
493 } else if (is_Sub(ptr)) {
494 ir_node *l = get_Sub_left(ptr);
495 ir_node *r = get_Sub_right(ptr);
498 tv = get_Const_tarval(r);
505 static ir_node *find_compound_ent_value(ir_node *ptr)
507 return rec_find_compound_ent_value(ptr, NULL);
511 static void reduce_adr_usage(ir_node *ptr);
514 * Update a Load that may have lost its users.
516 static void handle_load_update(ir_node *load)
518 ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
520 /* do NOT touch volatile loads for now */
521 if (get_Load_volatility(load) == volatility_is_volatile)
524 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
525 ir_node *ptr = get_Load_ptr(load);
526 ir_node *mem = get_Load_mem(load);
528 /* a Load whose value is neither used nor exception checked, remove it */
529 exchange(info->projs[pn_Load_M], mem);
530 if (info->projs[pn_Load_X_regular])
531 exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
533 reduce_adr_usage(ptr);
538 * A use of an address node has vanished. Check if this was a Proj
539 * node and update the counters.
541 static void reduce_adr_usage(ir_node *ptr)
546 if (get_irn_n_edges(ptr) > 0)
549 /* this Proj is dead now */
550 pred = get_Proj_pred(ptr);
552 ldst_info_t *info = (ldst_info_t*)get_irn_link(pred);
553 info->projs[get_Proj_proj(ptr)] = NULL;
555 /* this node lost its result proj, handle that */
556 handle_load_update(pred);
561 * Check, if an already existing value of mode old_mode can be converted
562 * into the needed one new_mode without loss.
564 static int can_use_stored_value(ir_mode *old_mode, ir_mode *new_mode)
568 if (old_mode == new_mode)
571 old_size = get_mode_size_bits(old_mode);
572 new_size = get_mode_size_bits(new_mode);
574 /* if both modes are two-complement ones, we can always convert the
575 Stored value into the needed one. (on big endian machines we currently
576 only support this for modes of same size) */
577 if (old_size >= new_size &&
578 get_mode_arithmetic(old_mode) == irma_twos_complement &&
579 get_mode_arithmetic(new_mode) == irma_twos_complement &&
580 (!be_get_backend_param()->byte_order_big_endian
581 || old_size == new_size)) {
588 * Check whether a Call is at least pure, i.e. does only read memory.
590 static unsigned is_Call_pure(ir_node *call)
592 ir_type *call_tp = get_Call_type(call);
593 unsigned prop = get_method_additional_properties(call_tp);
595 /* check first the call type */
596 if ((prop & (mtp_property_const|mtp_property_pure)) == 0) {
597 /* try the called entity */
598 ir_node *ptr = get_Call_ptr(call);
600 if (is_SymConst_addr_ent(ptr)) {
601 ir_entity *ent = get_SymConst_entity(ptr);
603 prop = get_entity_additional_properties(ent);
606 return (prop & (mtp_property_const|mtp_property_pure)) != 0;
609 static ir_node *get_base_and_offset(ir_node *ptr, long *pOffset)
611 ir_mode *mode = get_irn_mode(ptr);
614 /* TODO: long might not be enough, we should probably use some tarval thingy... */
617 ir_node *l = get_Add_left(ptr);
618 ir_node *r = get_Add_right(ptr);
620 if (get_irn_mode(l) != mode || !is_Const(r))
623 offset += get_tarval_long(get_Const_tarval(r));
625 } else if (is_Sub(ptr)) {
626 ir_node *l = get_Sub_left(ptr);
627 ir_node *r = get_Sub_right(ptr);
629 if (get_irn_mode(l) != mode || !is_Const(r))
632 offset -= get_tarval_long(get_Const_tarval(r));
634 } else if (is_Sel(ptr)) {
635 ir_entity *ent = get_Sel_entity(ptr);
636 ir_type *tp = get_entity_owner(ent);
638 if (is_Array_type(tp)) {
642 /* only one dimensional arrays yet */
643 if (get_Sel_n_indexs(ptr) != 1)
645 index = get_Sel_index(ptr, 0);
646 if (! is_Const(index))
649 tp = get_entity_type(ent);
650 if (get_type_state(tp) != layout_fixed)
653 size = get_type_size_bytes(tp);
654 offset += size * get_tarval_long(get_Const_tarval(index));
656 if (get_type_state(tp) != layout_fixed)
658 offset += get_entity_offset(ent);
660 ptr = get_Sel_ptr(ptr);
669 static int try_load_after_store(ir_node *load,
670 ir_node *load_base_ptr, long load_offset, ir_node *store)
673 ir_node *store_ptr = get_Store_ptr(store);
675 ir_node *store_base_ptr = get_base_and_offset(store_ptr, &store_offset);
676 ir_node *store_value;
685 if (load_base_ptr != store_base_ptr)
688 load_mode = get_Load_mode(load);
689 load_mode_len = get_mode_size_bytes(load_mode);
690 store_mode = get_irn_mode(get_Store_value(store));
691 store_mode_len = get_mode_size_bytes(store_mode);
692 delta = load_offset - store_offset;
693 store_value = get_Store_value(store);
695 if (delta < 0 || delta+load_mode_len > store_mode_len)
698 if (store_mode != load_mode &&
699 get_mode_arithmetic(store_mode) == irma_twos_complement &&
700 get_mode_arithmetic(load_mode) == irma_twos_complement) {
702 /* produce a shift to adjust offset delta */
703 unsigned const shift = be_get_backend_param()->byte_order_big_endian
704 ? store_mode_len - load_mode_len - delta
707 ir_graph *const irg = get_irn_irg(load);
708 ir_node *const cnst = new_r_Const_long(irg, mode_Iu, shift * 8);
709 store_value = new_r_Shr(get_nodes_block(load),
710 store_value, cnst, store_mode);
713 store_value = new_r_Conv(get_nodes_block(load), store_value, load_mode);
715 /* we would need some kind of bitcast node here */
719 DBG_OPT_RAW(load, store_value);
721 info = (ldst_info_t*)get_irn_link(load);
722 if (info->projs[pn_Load_M])
723 exchange(info->projs[pn_Load_M], get_Load_mem(load));
727 if (info->projs[pn_Load_X_except]) {
728 ir_graph *irg = get_irn_irg(load);
729 exchange( info->projs[pn_Load_X_except], new_r_Bad(irg, mode_X));
732 if (info->projs[pn_Load_X_regular]) {
733 exchange( info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
737 if (info->projs[pn_Load_res])
738 exchange(info->projs[pn_Load_res], store_value);
740 load_ptr = get_Load_ptr(load);
742 reduce_adr_usage(load_ptr);
743 return res | DF_CHANGED;
747 * Follow the memory chain as long as there are only Loads,
748 * alias free Stores, and constant Calls and try to replace the
749 * current Load by a previous ones.
750 * Note that in unreachable loops it might happen that we reach
751 * load again, as well as we can fall into a cycle.
752 * We break such cycles using a special visited flag.
754 * INC_MASTER() must be called before dive into
756 static unsigned follow_Mem_chain(ir_node *load, ir_node *curr)
759 ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
761 ir_node *ptr = get_Load_ptr(load);
762 ir_node *mem = get_Load_mem(load);
763 ir_mode *load_mode = get_Load_mode(load);
765 for (pred = curr; load != pred; ) {
766 ldst_info_t *pred_info = (ldst_info_t*)get_irn_link(pred);
769 * a Load immediately after a Store -- a read after write.
770 * We may remove the Load, if both Load & Store does not have an
771 * exception handler OR they are in the same Block. In the latter
772 * case the Load cannot throw an exception when the previous Store was
775 * Why we need to check for Store Exception? If the Store cannot
776 * be executed (ROM) the exception handler might simply jump into
778 * We could make it a little bit better if we would know that the
779 * exception handler of the Store jumps directly to the end...
781 if (is_Store(pred) && ((pred_info->projs[pn_Store_X_except] == NULL
782 && info->projs[pn_Load_X_except] == NULL)
783 || get_nodes_block(load) == get_nodes_block(pred)))
786 ir_node *base_ptr = get_base_and_offset(ptr, &load_offset);
787 int changes = try_load_after_store(load, base_ptr, load_offset, pred);
790 return res | changes;
791 } else if (is_Load(pred) && get_Load_ptr(pred) == ptr &&
792 can_use_stored_value(get_Load_mode(pred), load_mode)) {
794 * a Load after a Load -- a read after read.
795 * We may remove the second Load, if it does not have an exception
796 * handler OR they are in the same Block. In the later case
797 * the Load cannot throw an exception when the previous Load was
800 * Here, there is no need to check if the previous Load has an
801 * exception hander because they would have exact the same
804 * TODO: implement load-after-load with different mode for big
807 if (info->projs[pn_Load_X_except] == NULL
808 || get_nodes_block(load) == get_nodes_block(pred)) {
811 DBG_OPT_RAR(load, pred);
813 /* the result is used */
814 if (info->projs[pn_Load_res]) {
815 if (pred_info->projs[pn_Load_res] == NULL) {
816 /* create a new Proj again */
817 pred_info->projs[pn_Load_res] = new_r_Proj(pred, get_Load_mode(pred), pn_Load_res);
819 value = pred_info->projs[pn_Load_res];
821 /* add an convert if needed */
822 if (get_Load_mode(pred) != load_mode) {
823 value = new_r_Conv(get_nodes_block(load), value, load_mode);
826 exchange(info->projs[pn_Load_res], value);
829 if (info->projs[pn_Load_M])
830 exchange(info->projs[pn_Load_M], mem);
833 if (info->projs[pn_Load_X_except]) {
834 ir_graph *irg = get_irn_irg(load);
835 exchange(info->projs[pn_Load_X_except], new_r_Bad(irg, mode_X));
838 if (info->projs[pn_Load_X_regular]) {
839 exchange( info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
844 reduce_adr_usage(ptr);
845 return res |= DF_CHANGED;
849 if (is_Store(pred)) {
850 /* check if we can pass through this store */
851 ir_alias_relation rel = get_alias_relation(
853 get_irn_mode(get_Store_value(pred)),
855 /* if the might be an alias, we cannot pass this Store */
856 if (rel != ir_no_alias)
858 pred = skip_Proj(get_Store_mem(pred));
859 } else if (is_Load(pred)) {
860 pred = skip_Proj(get_Load_mem(pred));
861 } else if (is_Call(pred)) {
862 if (is_Call_pure(pred)) {
863 /* The called graph is at least pure, so there are no Store's
864 in it. We can handle it like a Load and skip it. */
865 pred = skip_Proj(get_Call_mem(pred));
867 /* there might be Store's in the graph, stop here */
871 /* follow only Load chains */
875 /* check for cycles */
876 if (NODE_VISITED(pred_info))
878 MARK_NODE(pred_info);
884 /* handle all Sync predecessors */
885 for (i = get_Sync_n_preds(pred) - 1; i >= 0; --i) {
886 res |= follow_Mem_chain(load, skip_Proj(get_Sync_pred(pred, i)));
895 ir_node *can_replace_load_by_const(const ir_node *load, ir_node *c)
897 ir_mode *c_mode = get_irn_mode(c);
898 ir_mode *l_mode = get_Load_mode(load);
899 ir_node *block = get_nodes_block(load);
900 dbg_info *dbgi = get_irn_dbg_info(load);
901 ir_node *res = copy_const_value(dbgi, c, block);
903 if (c_mode != l_mode) {
904 /* check, if the mode matches OR can be easily converted info */
905 if (is_reinterpret_cast(c_mode, l_mode)) {
906 /* copy the value from the const code irg and cast it */
907 res = new_rd_Conv(dbgi, block, res, l_mode);
918 * @param load the Load node
920 static unsigned optimize_load(ir_node *load)
922 ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
923 ir_node *mem, *ptr, *value;
928 /* do NOT touch volatile loads for now */
929 if (get_Load_volatility(load) == volatility_is_volatile)
932 /* the address of the load to be optimized */
933 ptr = get_Load_ptr(load);
935 /* The mem of the Load. Must still be returned after optimization. */
936 mem = get_Load_mem(load);
938 if (info->projs[pn_Load_res] == NULL
939 && info->projs[pn_Load_X_except] == NULL) {
940 /* the value is never used and we don't care about exceptions, remove */
941 exchange(info->projs[pn_Load_M], mem);
943 if (info->projs[pn_Load_X_regular]) {
944 /* should not happen, but if it does, remove it */
945 exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
949 reduce_adr_usage(ptr);
950 return res | DF_CHANGED;
954 /* check if we can determine the entity that will be loaded */
955 ent = find_constant_entity(ptr);
957 && get_entity_visibility(ent) != ir_visibility_external) {
958 /* a static allocation that is not external: there should be NO
959 * exception when loading even if we cannot replace the load itself.
962 /* no exception, clear the info field as it might be checked later again */
963 if (info->projs[pn_Load_X_except]) {
964 ir_graph *irg = get_irn_irg(load);
965 exchange(info->projs[pn_Load_X_except], new_r_Bad(irg, mode_X));
966 info->projs[pn_Load_X_except] = NULL;
969 if (info->projs[pn_Load_X_regular]) {
970 exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
971 info->projs[pn_Load_X_regular] = NULL;
975 if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) {
976 if (has_entity_initializer(ent)) {
977 /* new style initializer */
978 value = find_compound_ent_value(ptr);
981 ir_graph *irg = get_irn_irg(load);
982 value = can_replace_load_by_const(load, value);
983 if (value != NULL && is_Sel(ptr)) {
984 /* frontend has inserted masking operations after bitfield accesses,
985 * so we might have to shift the const. */
986 unsigned char bit_offset = get_entity_offset_bits_remainder(get_Sel_entity(ptr));
987 if (bit_offset != 0) {
988 if (is_Const(value)) {
989 ir_tarval *tv_old = get_Const_tarval(value);
990 ir_tarval *tv_offset = new_tarval_from_long(bit_offset, mode_Bu);
991 ir_tarval *tv_new = tarval_shl(tv_old, tv_offset);
992 value = new_r_Const(irg, tv_new);
1001 if (value != NULL) {
1002 /* we completely replace the load by this value */
1003 if (info->projs[pn_Load_X_except]) {
1004 ir_graph *irg = get_irn_irg(load);
1005 exchange(info->projs[pn_Load_X_except], new_r_Bad(irg, mode_X));
1006 info->projs[pn_Load_X_except] = NULL;
1009 if (info->projs[pn_Load_X_regular]) {
1010 exchange(info->projs[pn_Load_X_regular], new_r_Jmp(get_nodes_block(load)));
1011 info->projs[pn_Load_X_regular] = NULL;
1014 if (info->projs[pn_Load_M]) {
1015 exchange(info->projs[pn_Load_M], mem);
1018 if (info->projs[pn_Load_res]) {
1019 exchange(info->projs[pn_Load_res], value);
1023 reduce_adr_usage(ptr);
1027 /* Check, if the address of this load is used more than once.
1028 * If not, more load cannot be removed in any case. */
1029 if (get_irn_n_edges(ptr) <= 1 && get_irn_n_edges(get_base_and_offset(ptr, &dummy)) <= 1)
1033 * follow the memory chain as long as there are only Loads
1034 * and try to replace current Load or Store by a previous one.
1035 * Note that in unreachable loops it might happen that we reach
1036 * load again, as well as we can fall into a cycle.
1037 * We break such cycles using a special visited flag.
1040 res = follow_Mem_chain(load, skip_Proj(mem));
1045 * Check whether a value of mode new_mode would completely overwrite a value
1046 * of mode old_mode in memory.
1048 static int is_completely_overwritten(ir_mode *old_mode, ir_mode *new_mode)
1050 return get_mode_size_bits(new_mode) >= get_mode_size_bits(old_mode);
1054 * Check whether small is a part of large (starting at same address).
1056 static int is_partially_same(ir_node *small, ir_node *large)
1058 ir_mode *sm = get_irn_mode(small);
1059 ir_mode *lm = get_irn_mode(large);
1061 /* FIXME: Check endianness */
1062 return is_Conv(small) && get_Conv_op(small) == large
1063 && get_mode_size_bytes(sm) < get_mode_size_bytes(lm)
1064 && get_mode_arithmetic(sm) == irma_twos_complement
1065 && get_mode_arithmetic(lm) == irma_twos_complement;
1069 * follow the memory chain as long as there are only Loads and alias free Stores.
1071 * INC_MASTER() must be called before dive into
1073 static unsigned follow_Mem_chain_for_Store(ir_node *store, ir_node *curr)
1076 ldst_info_t *info = (ldst_info_t*)get_irn_link(store);
1078 ir_node *ptr = get_Store_ptr(store);
1079 ir_node *mem = get_Store_mem(store);
1080 ir_node *value = get_Store_value(store);
1081 ir_mode *mode = get_irn_mode(value);
1082 ir_node *block = get_nodes_block(store);
1084 for (pred = curr; pred != store;) {
1085 ldst_info_t *pred_info = (ldst_info_t*)get_irn_link(pred);
1088 * BEWARE: one might think that checking the modes is useless, because
1089 * if the pointers are identical, they refer to the same object.
1090 * This is only true in strong typed languages, not is C were the following
1091 * is possible *(ir_type1 *)p = a; *(ir_type2 *)p = b ...
1092 * However, if the size of the mode that is written is bigger or equal the
1093 * size of the old one, the old value is completely overwritten and can be
1096 if (is_Store(pred) && get_Store_ptr(pred) == ptr &&
1097 get_nodes_block(pred) == block) {
1099 * a Store after a Store in the same Block -- a write after write.
1103 * We may remove the first Store, if the old value is completely
1104 * overwritten or the old value is a part of the new value,
1105 * and if it does not have an exception handler.
1107 * TODO: What, if both have the same exception handler ???
1109 if (get_Store_volatility(pred) != volatility_is_volatile
1110 && !pred_info->projs[pn_Store_X_except]) {
1111 ir_node *predvalue = get_Store_value(pred);
1112 ir_mode *predmode = get_irn_mode(predvalue);
1114 if (is_completely_overwritten(predmode, mode)
1115 || is_partially_same(predvalue, value)) {
1116 DBG_OPT_WAW(pred, store);
1117 exchange(pred_info->projs[pn_Store_M], get_Store_mem(pred));
1119 reduce_adr_usage(ptr);
1125 * We may remove the Store, if the old value already contains
1126 * the new value, and if it does not have an exception handler.
1128 * TODO: What, if both have the same exception handler ???
1130 if (get_Store_volatility(store) != volatility_is_volatile
1131 && !info->projs[pn_Store_X_except]) {
1132 ir_node *predvalue = get_Store_value(pred);
1134 if (is_partially_same(value, predvalue)) {
1135 DBG_OPT_WAW(pred, store);
1136 exchange(info->projs[pn_Store_M], mem);
1138 reduce_adr_usage(ptr);
1142 } else if (is_Load(pred) && get_Load_ptr(pred) == ptr &&
1143 value == pred_info->projs[pn_Load_res]) {
1145 * a Store of a value just loaded from the same address
1146 * -- a write after read.
1147 * We may remove the Store, if it does not have an exception
1150 if (! info->projs[pn_Store_X_except]) {
1151 DBG_OPT_WAR(store, pred);
1152 exchange(info->projs[pn_Store_M], mem);
1154 reduce_adr_usage(ptr);
1159 if (is_Store(pred)) {
1160 /* check if we can pass through this store */
1161 ir_alias_relation rel = get_alias_relation(
1162 get_Store_ptr(pred),
1163 get_irn_mode(get_Store_value(pred)),
1165 /* if the might be an alias, we cannot pass this Store */
1166 if (rel != ir_no_alias)
1168 pred = skip_Proj(get_Store_mem(pred));
1169 } else if (is_Load(pred)) {
1170 ir_alias_relation rel = get_alias_relation(
1171 get_Load_ptr(pred), get_Load_mode(pred),
1173 if (rel != ir_no_alias)
1176 pred = skip_Proj(get_Load_mem(pred));
1178 /* follow only Load chains */
1182 /* check for cycles */
1183 if (NODE_VISITED(pred_info))
1185 MARK_NODE(pred_info);
1188 if (is_Sync(pred)) {
1191 /* handle all Sync predecessors */
1192 for (i = get_Sync_n_preds(pred) - 1; i >= 0; --i) {
1193 res |= follow_Mem_chain_for_Store(store, skip_Proj(get_Sync_pred(pred, i)));
1201 /** find entity used as base for an address calculation */
1202 static ir_entity *find_entity(ir_node *ptr)
1204 switch (get_irn_opcode(ptr)) {
1206 return get_SymConst_entity(ptr);
1208 ir_node *pred = get_Sel_ptr(ptr);
1209 if (get_irg_frame(get_irn_irg(ptr)) == pred)
1210 return get_Sel_entity(ptr);
1212 return find_entity(pred);
1216 ir_node *left = get_binop_left(ptr);
1218 if (mode_is_reference(get_irn_mode(left)))
1219 return find_entity(left);
1220 right = get_binop_right(ptr);
1221 if (mode_is_reference(get_irn_mode(right)))
1222 return find_entity(right);
1233 * @param store the Store node
1235 static unsigned optimize_store(ir_node *store)
1241 if (get_Store_volatility(store) == volatility_is_volatile)
1244 ptr = get_Store_ptr(store);
1245 entity = find_entity(ptr);
1247 /* a store to an entity which is never read is unnecessary */
1248 if (entity != NULL && !(get_entity_usage(entity) & ir_usage_read)) {
1249 ldst_info_t *info = (ldst_info_t*)get_irn_link(store);
1250 if (info->projs[pn_Store_X_except] == NULL) {
1251 DB((dbg, LEVEL_1, " Killing useless %+F to never read entity %+F\n", store, entity));
1252 exchange(info->projs[pn_Store_M], get_Store_mem(store));
1254 reduce_adr_usage(ptr);
1259 /* Check, if the address of this Store is used more than once.
1260 * If not, this Store cannot be removed in any case. */
1261 if (get_irn_n_edges(ptr) <= 1)
1264 mem = get_Store_mem(store);
1266 /* follow the memory chain as long as there are only Loads */
1269 return follow_Mem_chain_for_Store(store, skip_Proj(mem));
1272 /* check if a node has more than one real user. Keepalive edges do not count as
1274 static bool has_multiple_users(const ir_node *node)
1276 unsigned real_users = 0;
1277 foreach_out_edge(node, edge) {
1278 ir_node *user = get_edge_src_irn(edge);
1289 * walker, optimizes Phi after Stores to identical places:
1290 * Does the following optimization:
1293 * val1 val2 val3 val1 val2 val3
1295 * Store Store Store \ | /
1302 * This reduces the number of stores and allows for predicated execution.
1303 * Moves Stores back to the end of a function which may be bad.
1305 * This is only possible if the predecessor blocks have only one successor.
1307 static unsigned optimize_phi(ir_node *phi, walk_env_t *wenv)
1310 ir_node *store, *ptr, *block, *phi_block, *phiM, *phiD, *exc, *projM;
1315 ir_node **inM, **inD, **projMs;
1317 dbg_info *db = NULL;
1319 block_info_t *bl_info;
1322 /* Must be a memory Phi */
1323 if (get_irn_mode(phi) != mode_M)
1326 n = get_Phi_n_preds(phi);
1330 /* must be only one user */
1331 projM = get_Phi_pred(phi, 0);
1332 if (has_multiple_users(projM))
1335 store = skip_Proj(projM);
1339 if (!is_Store(store))
1342 block = get_nodes_block(store);
1344 /* check if the block is post dominated by Phi-block
1345 and has no exception exit */
1346 bl_info = (block_info_t*)get_irn_link(block);
1347 if (bl_info->flags & BLOCK_HAS_EXC)
1350 phi_block = get_nodes_block(phi);
1351 if (! block_strictly_postdominates(phi_block, block))
1354 /* this is the address of the store */
1355 ptr = get_Store_ptr(store);
1356 mode = get_irn_mode(get_Store_value(store));
1357 info = (ldst_info_t*)get_irn_link(store);
1358 exc = info->exc_block;
1360 for (i = 1; i < n; ++i) {
1361 ir_node *pred = get_Phi_pred(phi, i);
1363 if (has_multiple_users(pred))
1366 pred = skip_Proj(pred);
1367 if (!is_Store(pred))
1370 if (ptr != get_Store_ptr(pred) || mode != get_irn_mode(get_Store_value(pred)))
1373 info = (ldst_info_t*)get_irn_link(pred);
1375 /* check, if all stores have the same exception flow */
1376 if (exc != info->exc_block)
1379 block = get_nodes_block(pred);
1381 /* check if the block is post dominated by Phi-block
1382 and has no exception exit. Note that block must be different from
1383 Phi-block, else we would move a Store from end End of a block to its
1385 bl_info = (block_info_t*)get_irn_link(block);
1386 if (bl_info->flags & BLOCK_HAS_EXC)
1388 if (block == phi_block || ! block_postdominates(phi_block, block))
1393 * ok, when we are here, we found all predecessors of a Phi that
1394 * are Stores to the same address and size. That means whatever
1395 * we do before we enter the block of the Phi, we do a Store.
1396 * So, we can move the Store to the current block:
1398 * val1 val2 val3 val1 val2 val3
1400 * | Str | | Str | | Str | \ | /
1406 * Is only allowed if the predecessor blocks have only one successor.
1409 NEW_ARR_A(ir_node *, projMs, n);
1410 NEW_ARR_A(ir_node *, inM, n);
1411 NEW_ARR_A(ir_node *, inD, n);
1412 NEW_ARR_A(int, idx, n);
1414 /* Prepare: Collect all Store nodes. We must do this
1415 first because we otherwise may loose a store when exchanging its
1418 for (i = n - 1; i >= 0; --i) {
1419 projMs[i] = get_Phi_pred(phi, i);
1421 ir_node *const store = get_Proj_pred(projMs[i]);
1422 info = (ldst_info_t*)get_irn_link(store);
1424 inM[i] = get_Store_mem(store);
1425 inD[i] = get_Store_value(store);
1426 idx[i] = info->exc_idx;
1428 block = get_nodes_block(phi);
1430 /* second step: create a new memory Phi */
1431 phiM = new_rd_Phi(get_irn_dbg_info(phi), block, n, inM, mode_M);
1433 /* third step: create a new data Phi */
1434 phiD = new_rd_Phi(get_irn_dbg_info(phi), block, n, inD, mode);
1436 /* rewire memory and kill the node */
1437 for (i = n - 1; i >= 0; --i) {
1438 ir_node *proj = projMs[i];
1440 if (is_Proj(proj)) {
1441 ir_node *store = get_Proj_pred(proj);
1442 exchange(proj, inM[i]);
1447 /* fourth step: create the Store */
1448 store = new_rd_Store(db, block, phiM, ptr, phiD, cons_none);
1450 co_set_irn_name(store, co_get_irn_ident(old_store));
1453 projM = new_rd_Proj(NULL, store, mode_M, pn_Store_M);
1455 info = get_ldst_info(store, &wenv->obst);
1456 info->projs[pn_Store_M] = projM;
1458 /* fifths step: repair exception flow */
1460 ir_node *projX = new_rd_Proj(NULL, store, mode_X, pn_Store_X_except);
1462 info->projs[pn_Store_X_except] = projX;
1463 info->exc_block = exc;
1464 info->exc_idx = idx[0];
1466 for (i = 0; i < n; ++i) {
1467 set_Block_cfgpred(exc, idx[i], projX);
1471 /* the exception block should be optimized as some inputs are identical now */
1477 /* sixth step: replace old Phi */
1478 exchange(phi, projM);
1480 return res | DF_CHANGED;
1483 static int optimize_conv_load(ir_node *conv)
1485 ir_node *op = get_Conv_op(conv);
1488 if (has_multiple_users(op))
1490 /* shrink mode of load if possible. */
1491 ir_node *load = get_Proj_pred(op);
1495 /* only do it if we are the only user (otherwise the risk is too
1496 * great that we end up with 2 loads instead of one). */
1497 ir_mode *mode = get_irn_mode(conv);
1498 ir_mode *load_mode = get_Load_mode(load);
1500 = get_mode_size_bits(load_mode) - get_mode_size_bits(mode);
1501 if (mode_is_float(load_mode) || mode_is_float(mode) || bits_diff < 0)
1504 if (be_get_backend_param()->byte_order_big_endian) {
1505 if (bits_diff % 8 != 0)
1507 ir_graph *irg = get_irn_irg(conv);
1508 ir_node *ptr = get_Load_ptr(load);
1509 ir_mode *mode = get_irn_mode(ptr);
1510 ir_node *delta = new_r_Const_long(irg, mode, bits_diff/8);
1511 ir_node *block = get_nodes_block(load);
1512 ir_node *add = new_r_Add(block, ptr, delta, mode);
1513 set_Load_ptr(load, add);
1515 set_Load_mode(load, mode);
1516 set_irn_mode(op, mode);
1522 * walker, do the optimizations
1524 static void do_load_store_optimize(ir_node *n, void *env)
1526 walk_env_t *wenv = (walk_env_t*)env;
1528 switch (get_irn_opcode(n)) {
1531 wenv->changes |= optimize_load(n);
1535 wenv->changes |= optimize_store(n);
1539 wenv->changes |= optimize_phi(n, wenv);
1543 wenv->changes |= optimize_conv_load(n);
1552 typedef struct scc {
1553 ir_node *head; /**< the head of the list */
1556 /** A node entry. */
1557 typedef struct node_entry {
1558 unsigned DFSnum; /**< the DFS number of this node */
1559 unsigned low; /**< the low number of this node */
1560 int in_stack; /**< flag, set if the node is on the stack */
1561 ir_node *next; /**< link to the next node the the same scc */
1562 scc *pscc; /**< the scc of this node */
1563 unsigned POnum; /**< the post order number for blocks */
1566 /** A loop entry. */
1567 typedef struct loop_env {
1568 ir_nodehashmap_t map;
1569 struct obstack obst;
1570 ir_node **stack; /**< the node stack */
1571 size_t tos; /**< tos index */
1572 unsigned nextDFSnum; /**< the current DFS number */
1573 unsigned POnum; /**< current post order number */
1575 unsigned changes; /**< a bitmask of graph changes */
1579 * Gets the node_entry of a node
1581 static node_entry *get_irn_ne(ir_node *irn, loop_env *env)
1583 node_entry *e = ir_nodehashmap_get(node_entry, &env->map, irn);
1586 e = OALLOC(&env->obst, node_entry);
1587 memset(e, 0, sizeof(*e));
1588 ir_nodehashmap_insert(&env->map, irn, e);
1594 * Push a node onto the stack.
1596 * @param env the loop environment
1597 * @param n the node to push
1599 static void push(loop_env *env, ir_node *n)
1603 if (env->tos == ARR_LEN(env->stack)) {
1604 size_t nlen = ARR_LEN(env->stack) * 2;
1605 ARR_RESIZE(ir_node *, env->stack, nlen);
1607 env->stack[env->tos++] = n;
1608 e = get_irn_ne(n, env);
1613 * pop a node from the stack
1615 * @param env the loop environment
1617 * @return The topmost node
1619 static ir_node *pop(loop_env *env)
1621 ir_node *n = env->stack[--env->tos];
1622 node_entry *e = get_irn_ne(n, env);
1629 * Check if irn is a region constant.
1630 * The block or irn must strictly dominate the header block.
1632 * @param irn the node to check
1633 * @param header_block the header block of the induction variable
1635 static int is_rc(ir_node *irn, ir_node *header_block)
1637 ir_node *block = get_nodes_block(irn);
1639 return (block != header_block) && block_dominates(block, header_block);
1642 typedef struct phi_entry phi_entry;
1644 ir_node *phi; /**< A phi with a region const memory. */
1645 int pos; /**< The position of the region const memory */
1646 ir_node *load; /**< the newly created load for this phi */
1651 * An entry in the avail set.
1653 typedef struct avail_entry_t {
1654 ir_node *ptr; /**< the address pointer */
1655 ir_mode *mode; /**< the load mode */
1656 ir_node *load; /**< the associated Load */
1660 * Compare two avail entries.
1662 static int cmp_avail_entry(const void *elt, const void *key, size_t size)
1664 const avail_entry_t *a = (const avail_entry_t*)elt;
1665 const avail_entry_t *b = (const avail_entry_t*)key;
1668 return a->ptr != b->ptr || a->mode != b->mode;
1672 * Calculate the hash value of an avail entry.
1674 static unsigned hash_cache_entry(const avail_entry_t *entry)
1676 return get_irn_idx(entry->ptr) * 9 + hash_ptr(entry->mode);
1680 * Move loops out of loops if possible.
1682 * @param pscc the loop described by an SCC
1683 * @param env the loop environment
1685 static void move_loads_out_of_loops(scc *pscc, loop_env *env)
1687 ir_node *phi, *load, *next, *other, *next_other;
1689 phi_entry *phi_list = NULL;
1692 /* collect all outer memories */
1693 for (phi = pscc->head; phi != NULL; phi = next) {
1694 node_entry *ne = get_irn_ne(phi, env);
1697 /* check all memory Phi's */
1701 assert(get_irn_mode(phi) == mode_M && "DFS return non-memory Phi");
1703 for (j = get_irn_arity(phi) - 1; j >= 0; --j) {
1704 ir_node *pred = get_irn_n(phi, j);
1705 node_entry *pe = get_irn_ne(pred, env);
1707 if (pe->pscc != ne->pscc) {
1708 /* not in the same SCC, is region const */
1709 phi_entry *pe = OALLOC(&env->obst, phi_entry);
1713 pe->next = phi_list;
1718 /* no Phis no fun */
1719 assert(phi_list != NULL && "DFS found a loop without Phi");
1721 /* for now, we cannot handle more than one input (only reducible cf) */
1722 if (phi_list->next != NULL)
1725 avail = new_set(cmp_avail_entry, 8);
1727 for (load = pscc->head; load; load = next) {
1729 node_entry *ne = get_irn_ne(load, env);
1732 if (is_Load(load)) {
1733 ldst_info_t *info = (ldst_info_t*)get_irn_link(load);
1734 ir_node *ptr = get_Load_ptr(load);
1736 /* for now, we cannot handle Loads with exceptions */
1737 if (info->projs[pn_Load_res] == NULL || info->projs[pn_Load_X_regular] != NULL || info->projs[pn_Load_X_except] != NULL)
1740 /* for now, we can only move Load(Global) */
1741 if (! is_SymConst_addr_ent(ptr))
1743 load_mode = get_Load_mode(load);
1744 for (other = pscc->head; other != NULL; other = next_other) {
1745 node_entry *ne = get_irn_ne(other, env);
1746 next_other = ne->next;
1748 if (is_Store(other)) {
1749 ir_alias_relation rel = get_alias_relation(
1750 get_Store_ptr(other),
1751 get_irn_mode(get_Store_value(other)),
1753 /* if the might be an alias, we cannot pass this Store */
1754 if (rel != ir_no_alias)
1757 /* only Phis and pure Calls are allowed here, so ignore them */
1759 if (other == NULL) {
1760 ldst_info_t *ninfo = NULL;
1764 /* yep, no aliasing Store found, Load can be moved */
1765 DB((dbg, LEVEL_1, " Found a Load that could be moved: %+F\n", load));
1767 db = get_irn_dbg_info(load);
1768 for (pe = phi_list; pe != NULL; pe = pe->next) {
1770 ir_node *phi = pe->phi;
1771 ir_node *blk = get_nodes_block(phi);
1772 ir_node *pred = get_Block_cfgpred_block(blk, pos);
1774 avail_entry_t entry, *res;
1777 entry.mode = load_mode;
1778 res = set_find(avail_entry_t, avail, &entry, sizeof(entry), hash_cache_entry(&entry));
1782 irn = new_rd_Load(db, pred, get_Phi_pred(phi, pos), ptr, load_mode, cons_none);
1784 (void)set_insert(avail_entry_t, avail, &entry, sizeof(entry), hash_cache_entry(&entry));
1785 DB((dbg, LEVEL_1, " Created %+F in %+F\n", irn, pred));
1788 ninfo = get_ldst_info(irn, &env->obst);
1790 ninfo->projs[pn_Load_M] = mem = new_r_Proj(irn, mode_M, pn_Load_M);
1792 /* irn is from cache, so do not set phi pred again.
1793 * There might be other Loads between phi and irn already.
1795 set_Phi_pred(phi, pos, mem);
1798 ninfo->projs[pn_Load_res] = new_r_Proj(irn, load_mode, pn_Load_res);
1801 /* now kill the old Load */
1802 exchange(info->projs[pn_Load_M], get_Load_mem(load));
1803 exchange(info->projs[pn_Load_res], ninfo->projs[pn_Load_res]);
1805 env->changes |= DF_CHANGED;
1813 * Process a loop SCC.
1815 * @param pscc the SCC
1816 * @param env the loop environment
1818 static void process_loop(scc *pscc, loop_env *env)
1820 ir_node *irn, *next, *header = NULL;
1821 node_entry *b, *h = NULL;
1822 int j, only_phi, num_outside, process = 0;
1825 /* find the header block for this scc */
1826 for (irn = pscc->head; irn; irn = next) {
1827 node_entry *e = get_irn_ne(irn, env);
1828 ir_node *block = get_nodes_block(irn);
1831 b = get_irn_ne(block, env);
1833 if (header != NULL) {
1834 if (h->POnum < b->POnum) {
1844 /* check if this scc contains only Phi, Loads or Stores nodes */
1848 for (irn = pscc->head; irn; irn = next) {
1849 node_entry *e = get_irn_ne(irn, env);
1852 switch (get_irn_opcode(irn)) {
1854 if (is_Call_pure(irn)) {
1855 /* pure calls can be treated like loads */
1859 /* non-pure calls must be handle like may-alias Stores */
1862 /* cannot handle CopyB yet */
1866 if (get_Load_volatility(irn) == volatility_is_volatile) {
1867 /* cannot handle loops with volatile Loads */
1873 if (get_Store_volatility(irn) == volatility_is_volatile) {
1874 /* cannot handle loops with volatile Stores */
1883 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
1884 ir_node *pred = get_irn_n(irn, j);
1885 node_entry *pe = get_irn_ne(pred, env);
1887 if (pe->pscc != e->pscc) {
1888 /* not in the same SCC, must be a region const */
1889 if (! is_rc(pred, header)) {
1890 /* not a memory loop */
1893 if (out_rc == NULL) {
1894 /* first region constant */
1897 } else if (out_rc != pred) {
1898 /* another region constant */
1909 /* found a memory loop */
1910 DB((dbg, LEVEL_2, " Found a memory loop:\n "));
1911 if (only_phi && num_outside == 1) {
1912 /* a phi cycle with only one real predecessor can be collapsed */
1913 DB((dbg, LEVEL_2, " Found an USELESS Phi cycle:\n "));
1915 for (irn = pscc->head; irn; irn = next) {
1916 node_entry *e = get_irn_ne(irn, env);
1918 exchange(irn, out_rc);
1920 env->changes |= DF_CHANGED;
1924 #ifdef DEBUG_libfirm
1925 for (irn = pscc->head; irn; irn = next) {
1926 node_entry *e = get_irn_ne(irn, env);
1928 DB((dbg, LEVEL_2, " %+F,", irn));
1930 DB((dbg, LEVEL_2, "\n"));
1932 move_loads_out_of_loops(pscc, env);
1941 * @param pscc the SCC
1942 * @param env the loop environment
1944 static void process_scc(scc *pscc, loop_env *env)
1946 ir_node *head = pscc->head;
1947 node_entry *e = get_irn_ne(head, env);
1949 #ifdef DEBUG_libfirm
1951 ir_node *irn, *next;
1953 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
1954 for (irn = pscc->head; irn; irn = next) {
1955 node_entry *e = get_irn_ne(irn, env);
1959 DB((dbg, LEVEL_4, " %+F,", irn));
1961 DB((dbg, LEVEL_4, "\n"));
1965 if (e->next != NULL) {
1966 /* this SCC has more than one member */
1967 process_loop(pscc, env);
1972 * Do Tarjan's SCC algorithm and drive load/store optimization.
1974 * @param irn start at this node
1975 * @param env the loop environment
1977 static void dfs(ir_node *irn, loop_env *env)
1980 node_entry *node = get_irn_ne(irn, env);
1982 mark_irn_visited(irn);
1984 node->DFSnum = env->nextDFSnum++;
1985 node->low = node->DFSnum;
1989 if (is_Phi(irn) || is_Sync(irn)) {
1990 n = get_irn_arity(irn);
1991 for (i = 0; i < n; ++i) {
1992 ir_node *pred = get_irn_n(irn, i);
1993 node_entry *o = get_irn_ne(pred, env);
1995 if (!irn_visited(pred)) {
1997 node->low = MIN(node->low, o->low);
1999 if (o->DFSnum < node->DFSnum && o->in_stack)
2000 node->low = MIN(o->DFSnum, node->low);
2002 } else if (is_fragile_op(irn)) {
2003 ir_node *pred = get_memop_mem(irn);
2004 node_entry *o = get_irn_ne(pred, env);
2006 if (!irn_visited(pred)) {
2008 node->low = MIN(node->low, o->low);
2010 if (o->DFSnum < node->DFSnum && o->in_stack)
2011 node->low = MIN(o->DFSnum, node->low);
2012 } else if (is_Proj(irn)) {
2013 ir_node *pred = get_Proj_pred(irn);
2014 node_entry *o = get_irn_ne(pred, env);
2016 if (!irn_visited(pred)) {
2018 node->low = MIN(node->low, o->low);
2020 if (o->DFSnum < node->DFSnum && o->in_stack)
2021 node->low = MIN(o->DFSnum, node->low);
2024 /* IGNORE predecessors */
2027 if (node->low == node->DFSnum) {
2028 scc *pscc = OALLOC(&env->obst, scc);
2036 e = get_irn_ne(x, env);
2038 e->next = pscc->head;
2042 process_scc(pscc, env);
2047 * Do the DFS on the memory edges a graph.
2049 * @param irg the graph to process
2050 * @param env the loop environment
2052 static void do_dfs(ir_graph *irg, loop_env *env)
2054 ir_node *endblk, *end;
2057 inc_irg_visited(irg);
2059 /* visit all memory nodes */
2060 endblk = get_irg_end_block(irg);
2061 for (i = get_Block_n_cfgpreds(endblk) - 1; i >= 0; --i) {
2062 ir_node *pred = get_Block_cfgpred(endblk, i);
2064 pred = skip_Proj(pred);
2065 if (is_Return(pred)) {
2066 dfs(get_Return_mem(pred), env);
2067 } else if (is_Raise(pred)) {
2068 dfs(get_Raise_mem(pred), env);
2069 } else if (is_fragile_op(pred)) {
2070 dfs(get_memop_mem(pred), env);
2071 } else if (is_Bad(pred)) {
2072 /* ignore non-optimized block predecessor */
2074 assert(0 && "Unknown EndBlock predecessor");
2078 /* visit the keep-alives */
2079 end = get_irg_end(irg);
2080 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
2081 ir_node *ka = get_End_keepalive(end, i);
2083 if (is_Phi(ka) && !irn_visited(ka))
2089 * Optimize Loads/Stores in loops.
2091 * @param irg the graph
2093 static int optimize_loops(ir_graph *irg)
2097 env.stack = NEW_ARR_F(ir_node *, 128);
2102 ir_nodehashmap_init(&env.map);
2103 obstack_init(&env.obst);
2105 /* calculate the SCC's and drive loop optimization. */
2108 DEL_ARR_F(env.stack);
2109 obstack_free(&env.obst, NULL);
2110 ir_nodehashmap_destroy(&env.map);
2115 void optimize_load_store(ir_graph *irg)
2119 assure_irg_properties(irg,
2120 IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
2121 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
2122 | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
2123 | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
2124 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
2126 FIRM_DBG_REGISTER(dbg, "firm.opt.ldstopt");
2128 assert(get_irg_pinned(irg) != op_pin_state_floats &&
2129 "LoadStore optimization needs pinned graph");
2131 if (get_opt_alias_analysis()) {
2132 assure_irp_globals_entity_usage_computed();
2135 obstack_init(&env.obst);
2138 /* init the links, then collect Loads/Stores/Proj's in lists */
2140 irg_walk_graph(irg, firm_clear_link, collect_nodes, &env);
2142 /* now we have collected enough information, optimize */
2143 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
2145 env.changes |= optimize_loops(irg);
2147 obstack_free(&env.obst, NULL);
2149 confirm_irg_properties(irg,
2151 ? env.changes & CF_CHANGED
2152 ? IR_GRAPH_PROPERTIES_NONE
2153 : IR_GRAPH_PROPERTIES_CONTROL_FLOW
2154 : IR_GRAPH_PROPERTIES_ALL);
2157 ir_graph_pass_t *optimize_load_store_pass(const char *name)
2159 return def_graph_pass(name ? name : "ldst", optimize_load_store);