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);
400 static unsigned optimize_load(ir_node *load)
402 ldst_info_t *info = get_irn_link(load);
403 ir_mode *load_mode = get_Load_mode(load);
404 ir_node *pred, *mem, *ptr, *new_node;
408 /* do NOT touch volatile loads for now */
409 if (get_Load_volatility(load) == volatility_is_volatile)
412 /* the address of the load to be optimized */
413 ptr = get_Load_ptr(load);
416 * Check if we can remove the exception from a Load:
417 * This can be done, if the address is from an Sel(Alloc) and
418 * the Sel type is a subtype of the allocated type.
420 * This optimizes some often used OO constructs,
421 * like x = new O; x->t;
423 if (info->projs[pn_Load_X_except]) {
425 ir_node *mem = get_Sel_mem(ptr);
427 if (get_irn_op(skip_Proj(mem)) == op_Alloc) {
428 /* ok, check the types */
429 entity *ent = get_Sel_entity(ptr);
430 ir_type *s_type = get_entity_type(ent);
431 ir_type *a_type = get_Alloc_type(mem);
433 if (is_SubClass_of(s_type, a_type)) {
434 /* ok, condition met: there can't be an exception because
435 * Alloc guarantees that enough memory was allocated */
437 exchange(info->projs[pn_Load_X_except], new_Bad());
438 info->projs[pn_Load_X_except] = NULL;
443 else if ((get_irn_op(skip_Proj(ptr)) == op_Alloc) ||
444 ((get_irn_op(ptr) == op_Cast) && (get_irn_op(skip_Proj(get_Cast_op(ptr))) == op_Alloc))) {
445 /* simple case: a direct load after an Alloc. Firm Alloc throw
446 * an exception in case of out-of-memory. So, there is no way for an
447 * exception in this load.
448 * This code is constructed by the "exception lowering" in the Jack compiler.
450 exchange(info->projs[pn_Load_X_except], new_Bad());
451 info->projs[pn_Load_X_except] = NULL;
456 /* the mem of the Load. Must still be returned after optimization */
457 mem = get_Load_mem(load);
459 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
460 /* a Load which value is neither used nor exception checked, remove it */
461 exchange(info->projs[pn_Load_M], mem);
463 return res | DF_CHANGED;
466 /* Load from a constant polymorphic field, where we can resolve
468 new_node = transform_node_Load(load);
469 if (new_node != load) {
470 if (info->projs[pn_Load_M]) {
471 exchange(info->projs[pn_Load_M], mem);
472 info->projs[pn_Load_M] = NULL;
474 if (info->projs[pn_Load_X_except]) {
475 exchange(info->projs[pn_Load_X_except], new_Bad());
476 info->projs[pn_Load_X_except] = NULL;
478 if (info->projs[pn_Load_res])
479 exchange(info->projs[pn_Load_res], new_node);
480 return res | DF_CHANGED;
483 /* check if we can determine the entity that will be loaded */
484 ent = find_constant_entity(ptr);
486 if ((allocation_static == get_entity_allocation(ent)) &&
487 (visibility_external_allocated != get_entity_visibility(ent))) {
488 /* a static allocation that is not external: there should be NO exception
491 /* no exception, clear the info field as it might be checked later again */
492 if (info->projs[pn_Load_X_except]) {
493 exchange(info->projs[pn_Load_X_except], new_Bad());
494 info->projs[pn_Load_X_except] = NULL;
498 if (variability_constant == get_entity_variability(ent)
499 && is_atomic_entity(ent)) {
500 /* Might not be atomic after
501 lowering of Sels. In this
502 case we could also load, but
503 it's more complicated. */
504 /* more simpler case: we load the content of a constant value:
505 * replace it by the constant itself
509 if (info->projs[pn_Load_M]) {
510 exchange(info->projs[pn_Load_M], mem);
515 if (info->projs[pn_Load_res]) {
516 if (is_atomic_entity(ent)) {
517 ir_node *c = copy_const_value(get_irn_dbg_info(load), get_atomic_ent_value(ent));
520 exchange(info->projs[pn_Load_res], c);
521 return DF_CHANGED | res;
525 else if (variability_constant == get_entity_variability(ent)) {
526 compound_graph_path *path = get_accessed_path(ptr);
531 assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
535 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
536 entity *node = get_compound_graph_path_node(path, j);
537 fprintf(stdout, ".%s", get_entity_name(node));
538 if (is_Array_type(get_entity_owner(node)))
539 fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
545 c = get_compound_ent_value_by_path(ent, path);
546 free_compound_graph_path(path);
548 /* printf(" cons: "); DDMN(c); */
550 if (info->projs[pn_Load_M]) {
551 exchange(info->projs[pn_Load_M], mem);
554 if (info->projs[pn_Load_res]) {
555 exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
556 return res | DF_CHANGED;
560 /* We can not determine a correct access path. E.g., in jack, we load
561 a byte from an object to generate an exception. Happens in test program
563 printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
564 get_entity_name(get_irg_entity(current_ir_graph)));
565 printf(" load: "); DDMN(load);
566 printf(" ptr: "); DDMN(ptr);
573 /* Check, if the address of this load is used more than once.
574 * If not, this load cannot be removed in any case. */
575 if (get_irn_out_n(ptr) <= 1)
579 * follow the memory chain as long as there are only Loads
580 * and try to replace current Load or Store by a previous one.
581 * Note that in unreachable loops it might happen that we reach
582 * load again, as well as we can fall into a cycle.
583 * We break such cycles using a special visited flag.
586 for (pred = skip_Proj(mem); load != pred; pred = skip_Proj(get_Load_mem(pred))) {
587 ldst_info_t *pred_info = get_irn_link(pred);
590 * BEWARE: one might think that checking the modes is useless, because
591 * if the pointers are identical, they refer to the same object.
592 * This is only true in strong typed languages, not in C were the following
593 * is possible a = *(ir_type1 *)p; b = *(ir_type2 *)p ...
596 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
597 get_irn_mode(get_Store_value(pred)) == load_mode) {
599 * a Load immediately after a Store -- a read after write.
600 * We may remove the Load, if both Load & Store does not have an exception handler
601 * OR they are in the same block. In the latter case the Load cannot
602 * throw an exception when the previous Store was quiet.
604 * Why we need to check for Store Exception? If the Store cannot
605 * be executed (ROM) the exception handler might simply jump into
607 * We could make it a little bit better if we would know that the exception
608 * handler of the Store jumps directly to the end...
610 if ((!pred_info->projs[pn_Store_X_except] && !info->projs[pn_Load_X_except]) ||
611 get_nodes_block(load) == get_nodes_block(pred)) {
612 ir_node *value = get_Store_value(pred);
614 DBG_OPT_RAW(load, value);
615 if (info->projs[pn_Load_M])
616 exchange(info->projs[pn_Load_M], mem);
619 if (info->projs[pn_Load_X_except]) {
620 exchange( info->projs[pn_Load_X_except], new_Bad());
624 if (info->projs[pn_Load_res])
625 exchange(info->projs[pn_Load_res], value);
627 return res | DF_CHANGED;
630 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
631 get_Load_mode(pred) == load_mode) {
633 * a Load after a Load -- a read after read.
634 * We may remove the second Load, if it does not have an exception handler
635 * OR they are in the same block. In the later case the Load cannot
636 * throw an exception when the previous Load was quiet.
638 * Here, there is no need to check if the previous Load has an exception
639 * hander because they would have exact the same exception...
641 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
642 DBG_OPT_RAR(load, pred);
644 if (pred_info->projs[pn_Load_res]) {
645 /* we need a data proj from the previous load for this optimization */
646 if (info->projs[pn_Load_res])
647 exchange(info->projs[pn_Load_res], pred_info->projs[pn_Load_res]);
649 if (info->projs[pn_Load_M])
650 exchange(info->projs[pn_Load_M], mem);
653 if (info->projs[pn_Load_res]) {
654 set_Proj_pred(info->projs[pn_Load_res], pred);
655 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
656 pred_info->projs[pn_Load_res] = info->projs[pn_Load_res];
658 if (info->projs[pn_Load_M]) {
659 /* Actually, this if should not be necessary. Construct the Loads
661 exchange(info->projs[pn_Load_M], mem);
666 if (info->projs[pn_Load_X_except]) {
667 exchange(info->projs[pn_Load_X_except], new_Bad());
671 return res |= DF_CHANGED;
675 /* follow only Load chains */
676 if (get_irn_op(pred) != op_Load)
679 /* check for cycles */
680 if (NODE_VISITED(pred_info))
682 MARK_NODE(pred_info);
690 static unsigned optimize_store(ir_node *store)
692 ldst_info_t *info = get_irn_link(store);
693 ir_node *pred, *mem, *ptr, *value, *block;
697 if (get_Store_volatility(store) == volatility_is_volatile)
701 * BEWARE: one might think that checking the modes is useless, because
702 * if the pointers are identical, they refer to the same object.
703 * This is only true in strong typed languages, not is C were the following
704 * is possible *(ir_type1 *)p = a; *(ir_type2 *)p = b ...
707 ptr = get_Store_ptr(store);
709 /* Check, if the address of this load is used more than once.
710 * If not, this load cannot be removed in any case. */
711 if (get_irn_out_n(ptr) <= 1)
714 block = get_nodes_block(store);
715 mem = get_Store_mem(store);
716 value = get_Store_value(store);
717 mode = get_irn_mode(value);
719 /* follow the memory chain as long as there are only Loads */
721 for (pred = skip_Proj(mem); pred != store; pred = skip_Proj(get_Load_mem(pred))) {
722 ldst_info_t *pred_info = get_irn_link(pred);
724 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
725 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
727 * a Store after a Store in the same block -- a write after write.
728 * We may remove the first Store, if it does not have an exception handler.
730 * TODO: What, if both have the same exception handler ???
732 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
733 DBG_OPT_WAW(pred, store);
734 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
738 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
739 value == pred_info->projs[pn_Load_res]) {
741 * a Store of a value after a Load -- a write after read.
742 * We may remove the second Store, if it does not have an exception handler.
744 if (! info->projs[pn_Store_X_except]) {
745 DBG_OPT_WAR(store, pred);
746 exchange( info->projs[pn_Store_M], mem );
751 /* follow only Load chains */
752 if (get_irn_op(pred) != op_Load)
755 /* check for cycles */
756 if (NODE_VISITED(pred_info))
758 MARK_NODE(pred_info);
764 * walker, optimizes Phi after Stores to identical places:
765 * Does the following optimization:
768 * val1 val2 val3 val1 val2 val3
777 * This reduces the number of stores and allows for predicated execution.
778 * Moves Stores back to the end of a function which may be bad.
780 * This is only possible if the predecessor blocks have only one successor.
782 static unsigned optimize_phi(ir_node *phi, void *env)
784 walk_env_t *wenv = env;
786 ir_node *store, *old_store, *ptr, *block, *phiM, *phiD, *exc, *projM;
788 ir_node **inM, **inD;
792 block_info_t *bl_info;
795 /* Must be a memory Phi */
796 if (get_irn_mode(phi) != mode_M)
799 n = get_Phi_n_preds(phi);
803 store = skip_Proj(get_Phi_pred(phi, 0));
805 if (get_irn_op(store) != op_Store)
808 /* abort on dead blocks */
809 if (is_Block_dead(get_nodes_block(store)))
812 /* check if the block has only one successor */
813 bl_info = get_irn_link(get_nodes_block(store));
817 /* this is the address of the store */
818 ptr = get_Store_ptr(store);
819 mode = get_irn_mode(get_Store_value(store));
820 info = get_irn_link(store);
821 exc = info->exc_block;
823 for (i = 1; i < n; ++i) {
824 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
826 if (get_irn_op(pred) != op_Store)
829 if (ptr != get_Store_ptr(pred) || mode != get_irn_mode(get_Store_value(pred)))
832 info = get_irn_link(pred);
834 /* check, if all stores have the same exception flow */
835 if (exc != info->exc_block)
838 /* abort on dead blocks */
839 if (is_Block_dead(get_nodes_block(store)))
842 /* check if the block has only one successor */
843 bl_info = get_irn_link(get_nodes_block(store));
849 * ok, when we are here, we found all predecessors of a Phi that
850 * are Stores to the same address and size. That means whatever
851 * we do before we enter the block of the Phi, we do a Store.
852 * So, we can move the Store to the current block:
854 * val1 val2 val3 val1 val2 val3
856 * | Str | | Str | | Str | \ | /
862 * Is only allowed if the predecessor blocks have only one successor.
865 /* first step: collect all inputs */
866 NEW_ARR_A(ir_node *, inM, n);
867 NEW_ARR_A(ir_node *, inD, n);
868 NEW_ARR_A(int, idx, n);
870 for (i = 0; i < n; ++i) {
871 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
872 info = get_irn_link(pred);
874 inM[i] = get_Store_mem(pred);
875 inD[i] = get_Store_value(pred);
876 idx[i] = info->exc_idx;
878 block = get_nodes_block(phi);
880 /* second step: create a new memory Phi */
881 phiM = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inM, mode_M);
883 /* third step: create a new data Phi */
884 phiD = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inD, mode);
886 /* fourth step: create the Store */
887 store = new_rd_Store(db, current_ir_graph, block, phiM, ptr, phiD);
889 co_set_irn_name(store, co_get_irn_ident(old_store));
892 projM = new_rd_Proj(NULL, current_ir_graph, block, store, mode_M, pn_Store_M);
894 info = get_ldst_info(store, wenv);
895 info->projs[pn_Store_M] = projM;
897 /* fifths step: repair exception flow */
899 ir_node *projX = new_rd_Proj(NULL, current_ir_graph, block, store, mode_X, pn_Store_X_except);
901 info->projs[pn_Store_X_except] = projX;
902 info->exc_block = exc;
903 info->exc_idx = idx[0];
905 for (i = 0; i < n; ++i) {
906 set_Block_cfgpred(exc, idx[i], projX);
910 /* the exception block should be optimized as some inputs are identical now */
916 /* sixth step: replace old Phi */
917 exchange(phi, projM);
919 return res | DF_CHANGED;
923 * walker, do the optimizations
925 static void do_load_store_optimize(ir_node *n, void *env)
927 walk_env_t *wenv = env;
929 switch (get_irn_opcode(n)) {
932 wenv->changes |= optimize_load(n);
936 wenv->changes |= optimize_store(n);
940 wenv->changes |= optimize_phi(n, env);
948 * do the load store optimization
950 void optimize_load_store(ir_graph *irg)
954 assert(get_irg_phase_state(irg) != phase_building);
955 assert(get_irg_pinned(irg) != op_pin_state_floats &&
956 "LoadStore optimization needs pinned graph");
958 if (! get_opt_redundant_loadstore())
961 obstack_init(&env.obst);
964 /* init the links, then collect Loads/Stores/Proj's in lists */
966 irg_walk_graph(irg, firm_clear_link, collect_nodes, &env);
968 /* now we have collected enough information, optimize */
969 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
971 obstack_free(&env.obst, NULL);
973 /* Handle graph state */
975 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
976 set_irg_outs_inconsistent(current_ir_graph);
979 if (env.changes & CF_CHANGED) {
980 /* is this really needed: Yes, control flow changed, block might get Bad. */
981 set_irg_doms_inconsistent(current_ir_graph);