3 * File name: ir/opt/ldstopt.c
4 * Purpose: load store optimizations
8 * Copyright: (c) 1998-2004 Universit
\81ät Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
25 # include "irnode_t.h"
26 # include "irgraph_t.h"
27 # include "irmode_t.h"
29 # include "ircons_t.h"
34 # include "dbginfo_t.h"
35 # include "iropt_dbg.h"
36 # include "irflag_t.h"
40 # include "opt_polymorphy.h"
43 #include "cacheopt/cachesim.h"
47 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
49 #define MAX_PROJ IMAX(pn_Load_max, pn_Store_max)
52 DF_CHANGED = 1, /**< data flow changed */
53 CF_CHANGED = 2, /**< control flow changed */
59 typedef struct _walk_env_t {
60 struct obstack obst; /**< list of all stores */
61 unsigned changes; /**< a bitmask of graph changes */
65 * flags for Load/Store
68 LDST_VISITED = 1 /**< if set, this Load/Store is already visited */
74 typedef struct _ldst_info_t {
75 ir_node *projs[MAX_PROJ]; /**< list of Proj's of this node */
76 ir_node *exc_block; /**< the exception block if available */
77 int exc_idx; /**< predecessor index in the exception block */
78 unsigned flags; /**< flags */
79 unsigned visited; /**< visited counter for breaking loops */
83 * flags for control flow
86 BLOCK_HAS_COND = 1, /**< Block has conditional control flow */
87 BLOCK_HAS_EXC = 2 /**< Block has exceptional control flow */
93 typedef struct _block_info_t {
94 unsigned flags; /**< flags for the block */
97 /** the master visited flag for loop detection. */
98 static unsigned master_visited = 0;
100 #define INC_MASTER() ++master_visited
101 #define MARK_NODE(info) (info)->visited = master_visited
102 #define NODE_VISITED(info) (info)->visited >= master_visited
105 * get the Load/Store info of a node
107 static ldst_info_t *get_ldst_info(ir_node *node, walk_env_t *env)
109 ldst_info_t *info = get_irn_link(node);
112 info = obstack_alloc(&env->obst, sizeof(*info));
114 memset(info, 0, sizeof(*info));
116 set_irn_link(node, info);
122 * get the Block info of a node
124 static block_info_t *get_block_info(ir_node *node, walk_env_t *env)
126 block_info_t *info = get_irn_link(node);
129 info = obstack_alloc(&env->obst, sizeof(*info));
131 memset(info, 0, sizeof(*info));
133 set_irn_link(node, info);
139 * update the projection info for a Load/Store
141 static unsigned update_projs(ldst_info_t *info, ir_node *proj)
143 long nr = get_Proj_proj(proj);
145 assert(0 <= nr && nr <= MAX_PROJ && "Wrong proj from LoadStore");
147 if (info->projs[nr]) {
148 /* there is already one, do CSE */
149 exchange(proj, info->projs[nr]);
153 info->projs[nr] = proj;
159 * update the exception block info for a Load/Store node.
161 * @param info the load/store info struct
162 * @param block the exception handler block for this load/store
163 * @param pos the control flow input of the block
165 static unsigned update_exc(ldst_info_t *info, ir_node *block, int pos)
167 assert(info->exc_block == NULL && "more than one exception block found");
169 info->exc_block = block;
174 #define get_irn_out_n(node) (unsigned)PTR_TO_INT(get_irn_link(node))
175 #define set_irn_out_n(node, n) set_irn_link(adr, INT_TO_PTR(n))
178 * walker, collects all Load/Store/Proj nodes
180 * walks from Start -> End
182 static void collect_nodes(ir_node *node, void *env)
184 ir_op *op = get_irn_op(node);
185 ir_node *pred, *blk, *pred_blk;
186 ldst_info_t *ldst_info;
187 walk_env_t *wenv = env;
193 pred = get_Proj_pred(node);
194 op = get_irn_op(pred);
197 ldst_info = get_ldst_info(pred, wenv);
199 wenv->changes |= update_projs(ldst_info, node);
201 if ((ldst_info->flags & LDST_VISITED) == 0) {
202 adr = get_Load_ptr(pred);
203 set_irn_out_n(adr, get_irn_out_n(adr) + 1);
205 ldst_info->flags |= LDST_VISITED;
209 * Place the Proj's to the same block as the
210 * predecessor Load. This is always ok and prevents
211 * "non-SSA" form after optimizations if the Proj
212 * is in a wrong block.
214 blk = get_nodes_block(node);
215 pred_blk = get_nodes_block(pred);
216 if (blk != pred_blk) {
217 wenv->changes |= DF_CHANGED;
218 set_nodes_block(node, pred_blk);
221 else if (op == op_Store) {
222 ldst_info = get_ldst_info(pred, wenv);
224 wenv->changes |= update_projs(ldst_info, node);
226 if ((ldst_info->flags & LDST_VISITED) == 0) {
227 adr = get_Store_ptr(pred);
228 set_irn_out_n(adr, get_irn_out_n(adr) + 1);
230 ldst_info->flags |= LDST_VISITED;
234 * Place the Proj's to the same block as the
235 * predecessor Store. This is always ok and prevents
236 * "non-SSA" form after optimizations if the Proj
237 * is in a wrong block.
239 blk = get_nodes_block(node);
240 pred_blk = get_nodes_block(pred);
241 if (blk != pred_blk) {
242 wenv->changes |= DF_CHANGED;
243 set_nodes_block(node, pred_blk);
247 else if (op == op_Block) { /* check, if it's an exception block */
250 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
252 block_info_t *bl_info;
254 pred = skip_Proj(get_Block_cfgpred(node, i));
256 /* ignore Bad predecessors, they will be removed later */
260 pred_block = get_nodes_block(pred);
261 bl_info = get_block_info(pred_block, wenv);
263 if (is_fragile_op(pred))
264 bl_info->flags |= BLOCK_HAS_EXC;
265 else if (is_irn_forking(pred))
266 bl_info->flags |= BLOCK_HAS_COND;
268 if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
269 ldst_info = get_ldst_info(pred, wenv);
271 wenv->changes |= update_exc(ldst_info, node, i);
278 * Returns an entity if the address ptr points to a constant one.
280 static entity *find_constant_entity(ir_node *ptr)
283 ir_op *op = get_irn_op(ptr);
285 if (op == op_SymConst && (get_SymConst_kind(ptr) == symconst_addr_ent)) {
286 return get_SymConst_entity(ptr);
288 else if (op == op_Sel) {
289 entity *ent = get_Sel_entity(ptr);
290 ir_type *tp = get_entity_owner(ent);
292 /* Do not fiddle with polymorphism. */
293 if (is_Class_type(get_entity_owner(ent)) &&
294 ((get_entity_n_overwrites(ent) != 0) ||
295 (get_entity_n_overwrittenby(ent) != 0) ) )
298 if (variability_constant == get_entity_variability(ent))
301 if (is_Array_type(tp)) {
305 for (i = 0, n = get_Sel_n_indexs(ptr); i < n; ++i) {
307 tarval *tlower, *tupper;
308 ir_node *index = get_Sel_index(ptr, i);
309 tarval *tv = computed_value(index);
311 /* check if the index is constant */
312 if (tv == tarval_bad)
315 bound = get_array_lower_bound(tp, i);
316 tlower = computed_value(bound);
317 bound = get_array_upper_bound(tp, i);
318 tupper = computed_value(bound);
320 if (tlower == tarval_bad || tupper == tarval_bad)
323 if (tarval_cmp(tv, tlower) & pn_Cmp_Lt)
325 if (tarval_cmp(tupper, tv) & pn_Cmp_Lt)
328 /* ok, bounds check finished */
333 ptr = get_Sel_ptr(ptr);
341 * Return the Selection index of a Sel node from dimension n
343 static long get_Sel_array_index_long(ir_node *n, int dim) {
344 ir_node *index = get_Sel_index(n, dim);
345 assert(get_irn_op(index) == op_Const);
346 return get_tarval_long(get_Const_tarval(index));
350 * Returns the accessed component graph path for an
351 * node computing an address.
353 * @param ptr the node computing the address
354 * @param depth current depth in steps upward from the root
357 static compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
358 compound_graph_path *res = NULL;
359 entity *root, *field;
362 if (get_irn_op(ptr) == op_SymConst) {
363 /* a SymConst. If the depth is 0, this is an access to a global
364 * entity and we don't need a component path, else we know
365 * at least it's length.
367 assert(get_SymConst_kind(ptr) == symconst_addr_ent);
368 root = get_SymConst_entity(ptr);
369 res = (depth == 0) ? NULL : new_compound_graph_path(get_entity_type(root), depth);
372 assert(get_irn_op(ptr) == op_Sel);
373 /* it's a Sel, go up until we find the root */
374 res = rec_get_accessed_path(get_Sel_ptr(ptr), depth+1);
376 /* fill up the step in the path at the current position */
377 field = get_Sel_entity(ptr);
378 path_len = get_compound_graph_path_length(res);
379 pos = path_len - depth - 1;
380 set_compound_graph_path_node(res, pos, field);
382 if (is_Array_type(get_entity_owner(field))) {
383 assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
384 set_compound_graph_path_array_index(res, pos, get_Sel_array_index_long(ptr, 0));
390 /** Returns an access path or NULL. The access path is only
391 * valid, if the graph is in phase_high and _no_ address computation is used.
393 static compound_graph_path *get_accessed_path(ir_node *ptr) {
394 return rec_get_accessed_path(ptr, 0);
398 * Follow the memory chain as long as there are only Loads
399 * and try to replace current Load or Store by a previous one.
400 * Note that in unreachable loops it might happen that we reach
401 * load again, as well as we can fall into a cycle.
402 * We break such cycles using a special visited flag.
404 * INC_MASTER() must be called before dive into
406 static unsigned follow_Load_chain(ir_node *load, ir_node *curr) {
408 ldst_info_t *info = get_irn_link(load);
410 ir_node *ptr = get_Load_ptr(load);
411 ir_node *mem = get_Load_mem(load);
412 ir_mode *load_mode = get_Load_mode(load);
414 for (pred = curr; load != pred; pred = skip_Proj(get_Load_mem(pred))) {
415 ldst_info_t *pred_info = get_irn_link(pred);
418 * BEWARE: one might think that checking the modes is useless, because
419 * if the pointers are identical, they refer to the same object.
420 * This is only true in strong typed languages, not in C were the following
421 * is possible a = *(ir_type1 *)p; b = *(ir_type2 *)p ...
424 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
425 get_irn_mode(get_Store_value(pred)) == load_mode) {
427 * a Load immediately after a Store -- a read after write.
428 * We may remove the Load, if both Load & Store does not have an exception handler
429 * OR they are in the same block. In the latter case the Load cannot
430 * throw an exception when the previous Store was quiet.
432 * Why we need to check for Store Exception? If the Store cannot
433 * be executed (ROM) the exception handler might simply jump into
435 * We could make it a little bit better if we would know that the exception
436 * handler of the Store jumps directly to the end...
438 if ((!pred_info->projs[pn_Store_X_except] && !info->projs[pn_Load_X_except]) ||
439 get_nodes_block(load) == get_nodes_block(pred)) {
440 ir_node *value = get_Store_value(pred);
442 DBG_OPT_RAW(load, value);
443 if (info->projs[pn_Load_M])
444 exchange(info->projs[pn_Load_M], mem);
447 if (info->projs[pn_Load_X_except]) {
448 exchange( info->projs[pn_Load_X_except], new_Bad());
452 if (info->projs[pn_Load_res])
453 exchange(info->projs[pn_Load_res], value);
455 return res | DF_CHANGED;
458 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
459 get_Load_mode(pred) == load_mode) {
461 * a Load after a Load -- a read after read.
462 * We may remove the second Load, if it does not have an exception handler
463 * OR they are in the same block. In the later case the Load cannot
464 * throw an exception when the previous Load was quiet.
466 * Here, there is no need to check if the previous Load has an exception
467 * hander because they would have exact the same exception...
469 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
470 DBG_OPT_RAR(load, pred);
472 if (pred_info->projs[pn_Load_res]) {
473 /* we need a data proj from the previous load for this optimization */
474 if (info->projs[pn_Load_res])
475 exchange(info->projs[pn_Load_res], pred_info->projs[pn_Load_res]);
477 if (info->projs[pn_Load_M])
478 exchange(info->projs[pn_Load_M], mem);
481 if (info->projs[pn_Load_res]) {
482 set_Proj_pred(info->projs[pn_Load_res], pred);
483 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
484 pred_info->projs[pn_Load_res] = info->projs[pn_Load_res];
486 if (info->projs[pn_Load_M]) {
487 /* Actually, this if should not be necessary. Construct the Loads
489 exchange(info->projs[pn_Load_M], mem);
494 if (info->projs[pn_Load_X_except]) {
495 exchange(info->projs[pn_Load_X_except], new_Bad());
499 return res |= DF_CHANGED;
503 /* follow only Load chains */
504 if (get_irn_op(pred) != op_Load)
507 /* check for cycles */
508 if (NODE_VISITED(pred_info))
510 MARK_NODE(pred_info);
513 if (get_irn_op(pred) == op_Sync) {
516 /* handle all Sync predecessors */
517 for (i = get_Sync_n_preds(pred) - 1; i >= 0; --i) {
518 res |= follow_Load_chain(load, skip_Proj(get_Sync_pred(pred, i)));
530 static unsigned optimize_load(ir_node *load)
532 ldst_info_t *info = get_irn_link(load);
533 ir_mode *load_mode = get_Load_mode(load);
534 ir_node *mem, *ptr, *new_node;
538 /* do NOT touch volatile loads for now */
539 if (get_Load_volatility(load) == volatility_is_volatile)
542 /* the address of the load to be optimized */
543 ptr = get_Load_ptr(load);
546 * Check if we can remove the exception from a Load:
547 * This can be done, if the address is from an Sel(Alloc) and
548 * the Sel type is a subtype of the allocated type.
550 * This optimizes some often used OO constructs,
551 * like x = new O; x->t;
553 if (info->projs[pn_Load_X_except]) {
555 ir_node *mem = get_Sel_mem(ptr);
557 if (get_irn_op(skip_Proj(mem)) == op_Alloc) {
558 /* ok, check the types */
559 entity *ent = get_Sel_entity(ptr);
560 ir_type *s_type = get_entity_type(ent);
561 ir_type *a_type = get_Alloc_type(mem);
563 if (is_SubClass_of(s_type, a_type)) {
564 /* ok, condition met: there can't be an exception because
565 * Alloc guarantees that enough memory was allocated */
567 exchange(info->projs[pn_Load_X_except], new_Bad());
568 info->projs[pn_Load_X_except] = NULL;
573 else if ((get_irn_op(skip_Proj(ptr)) == op_Alloc) ||
574 ((get_irn_op(ptr) == op_Cast) && (get_irn_op(skip_Proj(get_Cast_op(ptr))) == op_Alloc))) {
575 /* simple case: a direct load after an Alloc. Firm Alloc throw
576 * an exception in case of out-of-memory. So, there is no way for an
577 * exception in this load.
578 * This code is constructed by the "exception lowering" in the Jack compiler.
580 exchange(info->projs[pn_Load_X_except], new_Bad());
581 info->projs[pn_Load_X_except] = NULL;
586 /* the mem of the Load. Must still be returned after optimization */
587 mem = get_Load_mem(load);
589 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
590 /* a Load which value is neither used nor exception checked, remove it */
591 exchange(info->projs[pn_Load_M], mem);
593 return res | DF_CHANGED;
596 /* Load from a constant polymorphic field, where we can resolve
598 new_node = transform_node_Load(load);
599 if (new_node != load) {
600 if (info->projs[pn_Load_M]) {
601 exchange(info->projs[pn_Load_M], mem);
602 info->projs[pn_Load_M] = NULL;
604 if (info->projs[pn_Load_X_except]) {
605 exchange(info->projs[pn_Load_X_except], new_Bad());
606 info->projs[pn_Load_X_except] = NULL;
608 if (info->projs[pn_Load_res])
609 exchange(info->projs[pn_Load_res], new_node);
610 return res | DF_CHANGED;
613 /* check if we can determine the entity that will be loaded */
614 ent = find_constant_entity(ptr);
616 if ((allocation_static == get_entity_allocation(ent)) &&
617 (visibility_external_allocated != get_entity_visibility(ent))) {
618 /* a static allocation that is not external: there should be NO exception
621 /* no exception, clear the info field as it might be checked later again */
622 if (info->projs[pn_Load_X_except]) {
623 exchange(info->projs[pn_Load_X_except], new_Bad());
624 info->projs[pn_Load_X_except] = NULL;
628 if (variability_constant == get_entity_variability(ent)
629 && is_atomic_entity(ent)) {
630 /* Might not be atomic after
631 lowering of Sels. In this
632 case we could also load, but
633 it's more complicated. */
634 /* more simpler case: we load the content of a constant value:
635 * replace it by the constant itself
639 if (info->projs[pn_Load_M]) {
640 exchange(info->projs[pn_Load_M], mem);
645 if (info->projs[pn_Load_res]) {
646 if (is_atomic_entity(ent)) {
647 ir_node *c = copy_const_value(get_irn_dbg_info(load), get_atomic_ent_value(ent));
650 exchange(info->projs[pn_Load_res], c);
651 return DF_CHANGED | res;
655 else if (variability_constant == get_entity_variability(ent)) {
656 compound_graph_path *path = get_accessed_path(ptr);
661 assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
665 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
666 entity *node = get_compound_graph_path_node(path, j);
667 fprintf(stdout, ".%s", get_entity_name(node));
668 if (is_Array_type(get_entity_owner(node)))
669 fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
675 c = get_compound_ent_value_by_path(ent, path);
676 free_compound_graph_path(path);
678 /* printf(" cons: "); DDMN(c); */
680 if (info->projs[pn_Load_M]) {
681 exchange(info->projs[pn_Load_M], mem);
684 if (info->projs[pn_Load_res]) {
685 exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
686 return res | DF_CHANGED;
690 /* We can not determine a correct access path. E.g., in jack, we load
691 a byte from an object to generate an exception. Happens in test program
693 printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
694 get_entity_name(get_irg_entity(current_ir_graph)));
695 printf(" load: "); DDMN(load);
696 printf(" ptr: "); DDMN(ptr);
703 /* Check, if the address of this load is used more than once.
704 * If not, this load cannot be removed in any case. */
705 if (get_irn_out_n(ptr) <= 1)
709 * follow the memory chain as long as there are only Loads
710 * and try to replace current Load or Store by a previous one.
711 * Note that in unreachable loops it might happen that we reach
712 * load again, as well as we can fall into a cycle.
713 * We break such cycles using a special visited flag.
716 res = follow_Load_chain(load, skip_Proj(mem));
721 * follow the memory chain as long as there are only Loads.
723 * INC_MASTER() must be called before dive into
725 static unsigned follow_Load_chain_for_Store(ir_node *store, ir_node *curr) {
727 ldst_info_t *info = get_irn_link(store);
729 ir_node *ptr = get_Store_ptr(store);
730 ir_node *mem = get_Store_mem(store);
731 ir_node *value = get_Store_value(store);
732 ir_mode *mode = get_irn_mode(value);
733 ir_node *block = get_nodes_block(store);
735 for (pred = curr; pred != store; pred = skip_Proj(get_Load_mem(pred))) {
736 ldst_info_t *pred_info = get_irn_link(pred);
739 * BEWARE: one might think that checking the modes is useless, because
740 * if the pointers are identical, they refer to the same object.
741 * This is only true in strong typed languages, not is C were the following
742 * is possible *(ir_type1 *)p = a; *(ir_type2 *)p = b ...
744 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
745 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
747 * a Store after a Store in the same block -- a write after write.
748 * We may remove the first Store, if it does not have an exception handler.
750 * TODO: What, if both have the same exception handler ???
752 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
753 DBG_OPT_WAW(pred, store);
754 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
758 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
759 value == pred_info->projs[pn_Load_res]) {
761 * a Store of a value after a Load -- a write after read.
762 * We may remove the second Store, if it does not have an exception handler.
764 if (! info->projs[pn_Store_X_except]) {
765 DBG_OPT_WAR(store, pred);
766 exchange( info->projs[pn_Store_M], mem );
771 /* follow only Load chains */
772 if (get_irn_op(pred) != op_Load)
775 /* check for cycles */
776 if (NODE_VISITED(pred_info))
778 MARK_NODE(pred_info);
781 if (get_irn_op(pred) == op_Sync) {
784 /* handle all Sync predecessors */
785 for (i = get_Sync_n_preds(pred) - 1; i >= 0; --i) {
786 res |= follow_Load_chain_for_Store(store, skip_Proj(get_Sync_pred(pred, i)));
797 static unsigned optimize_store(ir_node *store)
799 ldst_info_t *info = get_irn_link(store);
802 if (get_Store_volatility(store) == volatility_is_volatile)
805 ptr = get_Store_ptr(store);
807 /* Check, if the address of this load is used more than once.
808 * If not, this load cannot be removed in any case. */
809 if (get_irn_out_n(ptr) <= 1)
812 mem = get_Store_mem(store);
814 /* follow the memory chain as long as there are only Loads */
816 return follow_Load_chain_for_Store(store, skip_Proj(mem));
820 * walker, optimizes Phi after Stores to identical places:
821 * Does the following optimization:
824 * val1 val2 val3 val1 val2 val3
833 * This reduces the number of stores and allows for predicated execution.
834 * Moves Stores back to the end of a function which may be bad.
836 * This is only possible if the predecessor blocks have only one successor.
838 static unsigned optimize_phi(ir_node *phi, void *env)
840 walk_env_t *wenv = env;
842 ir_node *store, *old_store, *ptr, *block, *phiM, *phiD, *exc, *projM;
844 ir_node **inM, **inD;
848 block_info_t *bl_info;
851 /* Must be a memory Phi */
852 if (get_irn_mode(phi) != mode_M)
855 n = get_Phi_n_preds(phi);
859 store = skip_Proj(get_Phi_pred(phi, 0));
861 if (get_irn_op(store) != op_Store)
864 /* abort on dead blocks */
865 if (is_Block_dead(get_nodes_block(store)))
868 /* check if the block has only one successor */
869 bl_info = get_irn_link(get_nodes_block(store));
873 /* this is the address of the store */
874 ptr = get_Store_ptr(store);
875 mode = get_irn_mode(get_Store_value(store));
876 info = get_irn_link(store);
877 exc = info->exc_block;
879 for (i = 1; i < n; ++i) {
880 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
882 if (get_irn_op(pred) != op_Store)
885 if (ptr != get_Store_ptr(pred) || mode != get_irn_mode(get_Store_value(pred)))
888 info = get_irn_link(pred);
890 /* check, if all stores have the same exception flow */
891 if (exc != info->exc_block)
894 /* abort on dead blocks */
895 if (is_Block_dead(get_nodes_block(store)))
898 /* check if the block has only one successor */
899 bl_info = get_irn_link(get_nodes_block(store));
905 * ok, when we are here, we found all predecessors of a Phi that
906 * are Stores to the same address and size. That means whatever
907 * we do before we enter the block of the Phi, we do a Store.
908 * So, we can move the Store to the current block:
910 * val1 val2 val3 val1 val2 val3
912 * | Str | | Str | | Str | \ | /
918 * Is only allowed if the predecessor blocks have only one successor.
921 /* first step: collect all inputs */
922 NEW_ARR_A(ir_node *, inM, n);
923 NEW_ARR_A(ir_node *, inD, n);
924 NEW_ARR_A(int, idx, n);
926 for (i = 0; i < n; ++i) {
927 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
928 info = get_irn_link(pred);
930 inM[i] = get_Store_mem(pred);
931 inD[i] = get_Store_value(pred);
932 idx[i] = info->exc_idx;
934 block = get_nodes_block(phi);
936 /* second step: create a new memory Phi */
937 phiM = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inM, mode_M);
939 /* third step: create a new data Phi */
940 phiD = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inD, mode);
942 /* fourth step: create the Store */
943 store = new_rd_Store(db, current_ir_graph, block, phiM, ptr, phiD);
945 co_set_irn_name(store, co_get_irn_ident(old_store));
948 projM = new_rd_Proj(NULL, current_ir_graph, block, store, mode_M, pn_Store_M);
950 info = get_ldst_info(store, wenv);
951 info->projs[pn_Store_M] = projM;
953 /* fifths step: repair exception flow */
955 ir_node *projX = new_rd_Proj(NULL, current_ir_graph, block, store, mode_X, pn_Store_X_except);
957 info->projs[pn_Store_X_except] = projX;
958 info->exc_block = exc;
959 info->exc_idx = idx[0];
961 for (i = 0; i < n; ++i) {
962 set_Block_cfgpred(exc, idx[i], projX);
966 /* the exception block should be optimized as some inputs are identical now */
972 /* sixth step: replace old Phi */
973 exchange(phi, projM);
975 return res | DF_CHANGED;
979 * walker, do the optimizations
981 static void do_load_store_optimize(ir_node *n, void *env)
983 walk_env_t *wenv = env;
985 switch (get_irn_opcode(n)) {
988 wenv->changes |= optimize_load(n);
992 wenv->changes |= optimize_store(n);
996 wenv->changes |= optimize_phi(n, env);
1004 * do the load store optimization
1006 void optimize_load_store(ir_graph *irg)
1010 assert(get_irg_phase_state(irg) != phase_building);
1011 assert(get_irg_pinned(irg) != op_pin_state_floats &&
1012 "LoadStore optimization needs pinned graph");
1014 if (! get_opt_redundant_loadstore())
1017 obstack_init(&env.obst);
1020 /* init the links, then collect Loads/Stores/Proj's in lists */
1022 irg_walk_graph(irg, firm_clear_link, collect_nodes, &env);
1024 /* now we have collected enough information, optimize */
1025 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
1027 obstack_free(&env.obst, NULL);
1029 /* Handle graph state */
1031 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1032 set_irg_outs_inconsistent(current_ir_graph);
1035 if (env.changes & CF_CHANGED) {
1036 /* is this really needed: Yes, control flow changed, block might get Bad. */
1037 set_irg_doms_inconsistent(current_ir_graph);