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"
39 # include "opt_polymorphy.h"
42 #include "cacheopt/cachesim.h"
46 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
48 #define MAX_PROJ IMAX(pn_Load_max, pn_Store_max)
53 typedef struct _walk_env_t {
54 struct obstack obst; /**< list of all stores */
59 * flags for Load/Store
62 LDST_VISITED = 1 /**< if set, this Load/Store is already visited */
68 typedef struct _ldst_info_t {
69 ir_node *projs[MAX_PROJ]; /**< list of Proj's of this node */
70 ir_node *exc_block; /**< the exception block if available */
71 int exc_idx; /**< predecessor index in the exception block */
72 unsigned flags; /**< flags */
76 * flags for control flow
79 BLOCK_HAS_COND = 1, /**< Block has conditional control flow */
80 BLOCK_HAS_EXC = 2 /**< Block has exceptionl control flow */
86 typedef struct _block_info_t {
87 unsigned flags; /**< flags for the block */
91 * walker, clears all links first
93 static void init_links(ir_node *n, void *env)
95 set_irn_link(n, NULL);
99 * get the Load/Store info of a node
101 static ldst_info_t *get_ldst_info(ir_node *node, walk_env_t *env)
103 ldst_info_t *info = get_irn_link(node);
106 info = obstack_alloc(&env->obst, sizeof(*info));
108 memset(info, 0, sizeof(*info));
110 set_irn_link(node, info);
116 * get the Block info of a node
118 static block_info_t *get_block_info(ir_node *node, walk_env_t *env)
120 block_info_t *info = get_irn_link(node);
123 info = obstack_alloc(&env->obst, sizeof(*info));
125 memset(info, 0, sizeof(*info));
127 set_irn_link(node, info);
133 * update the projection info for a Load/Store
135 static int update_projs(ldst_info_t *info, ir_node *proj)
137 long nr = get_Proj_proj(proj);
139 assert(0 <= nr && nr <= MAX_PROJ && "Wrong proj from LoadStore");
141 if (info->projs[nr]) {
142 /* there is already one, do CSE */
143 exchange(proj, info->projs[nr]);
147 info->projs[nr] = proj;
153 * update the exception block info for a Load/Store
155 static int update_exc(ldst_info_t *info, ir_node *block, int pos)
157 assert(info->exc_block == NULL && "more than one exception block found");
159 info->exc_block = block;
164 #define get_irn_out_n(node) (unsigned)get_irn_link(node)
165 #define set_irn_out_n(node, n) set_irn_link(adr, (void *)(n))
168 * walker, collects all Load/Store/Proj nodes
170 * walks form Start -> End
172 static void collect_nodes(ir_node *node, void *env)
174 ir_op *op = get_irn_op(node);
176 ldst_info_t *ldst_info;
177 walk_env_t *wenv = env;
183 pred = get_Proj_pred(node);
184 op = get_irn_op(pred);
187 ldst_info = get_ldst_info(pred, wenv);
189 wenv->changes |= update_projs(ldst_info, node);
191 if ((ldst_info->flags & LDST_VISITED) == 0) {
192 adr = get_Load_ptr(pred);
193 set_irn_out_n(adr, get_irn_out_n(adr) + 1);
195 ldst_info->flags |= LDST_VISITED;
198 else if (op == op_Store) {
199 ldst_info = get_ldst_info(pred, wenv);
201 wenv->changes |= update_projs(ldst_info, node);
203 if ((ldst_info->flags & LDST_VISITED) == 0) {
204 adr = get_Store_ptr(pred);
205 set_irn_out_n(adr, get_irn_out_n(adr) + 1);
207 ldst_info->flags |= LDST_VISITED;
211 else if (op == op_Block) { /* check, if it's an exception block */
214 for (i = 0, n = get_Block_n_cfgpreds(node); i < n; ++i) {
216 block_info_t *bl_info;
218 pred = skip_Proj(get_Block_cfgpred(node, i));
220 /* ignore Bad predecessors, they will be removed later */
224 pred_block = get_nodes_block(pred);
225 bl_info = get_block_info(pred_block, wenv);
227 if (is_fragile_op(pred))
228 bl_info->flags |= BLOCK_HAS_EXC;
229 else if (is_forking_op(pred))
230 bl_info->flags |= BLOCK_HAS_COND;
232 if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
233 ldst_info = get_ldst_info(pred, wenv);
235 wenv->changes |= update_exc(ldst_info, node, i);
242 * Returns an entity if the address ptr points to a constant one.
244 static entity *find_constant_entity(ir_node *ptr)
247 ir_op *op = get_irn_op(ptr);
249 if (op == op_SymConst && (get_SymConst_kind(ptr) == symconst_addr_ent)) {
250 return get_SymConst_entity(ptr);
252 else if (op == op_Sel) {
253 entity *ent = get_Sel_entity(ptr);
254 type *tp = get_entity_owner(ent);
256 /* Do not fiddle about polymorphy. */
257 if (is_Class_type(get_entity_owner(ent)) &&
258 ((get_entity_n_overwrites(ent) != 0) ||
259 (get_entity_n_overwrittenby(ent) != 0) ) )
262 if (variability_constant == get_entity_variability(ent))
265 if (is_Array_type(tp)) {
269 for (i = 0, n = get_Sel_n_indexs(ptr); i < n; ++i) {
271 tarval *tlower, *tupper;
272 ir_node *index = get_Sel_index(ptr, i);
273 tarval *tv = computed_value(index);
275 /* check if the index is constant */
276 if (tv == tarval_bad)
279 bound = get_array_lower_bound(tp, i);
280 tlower = computed_value(bound);
281 bound = get_array_upper_bound(tp, i);
282 tupper = computed_value(bound);
284 if (tlower == tarval_bad || tupper == tarval_bad)
287 if (tarval_cmp(tv, tlower) & pn_Cmp_Lt)
289 if (tarval_cmp(tupper, tv) & pn_Cmp_Lt)
292 /* ok, bounds check finished */
297 ptr = get_Sel_ptr(ptr);
304 static long get_Sel_array_index_long(ir_node *n, int dim) {
305 ir_node *index = get_Sel_index(n, dim);
306 assert(get_irn_op(index) == op_Const);
307 return get_tarval_long(get_Const_tarval(index));
310 static compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
311 compound_graph_path *res = NULL;
312 entity *root, *field;
315 if (get_irn_op(ptr) == op_SymConst) {
316 assert(get_SymConst_kind(ptr) == symconst_addr_ent);
317 root = get_SymConst_entity(ptr);
318 res = depth == 0 ? NULL : new_compound_graph_path(get_entity_type(root), depth);
321 assert(get_irn_op(ptr) == op_Sel);
322 res = rec_get_accessed_path(get_Sel_ptr(ptr), depth+1);
323 field = get_Sel_entity(ptr);
324 path_len = get_compound_graph_path_length(res);
325 pos = path_len - depth - 1;
326 set_compound_graph_path_node(res, pos, field);
327 if (is_Array_type(get_entity_owner(field))) {
328 assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
329 set_compound_graph_path_array_index(res, pos, get_Sel_array_index_long(ptr, 0));
336 /** Returns an access path or NULL. The access path is only
337 * valid, if the graph is in phase_high and _no_ address computation is used. */
338 static compound_graph_path *get_accessed_path(ir_node *ptr) {
339 return rec_get_accessed_path(ptr, 0);
346 static int optimize_load(ir_node *load)
348 ldst_info_t *info = get_irn_link(load);
349 ir_mode *load_mode = get_Load_mode(load);
350 ir_node *pred, *mem, *ptr, *new_node;
354 /* the address of the load to be optimized */
355 ptr = get_Load_ptr(load);
358 * Check if we can remove the exception form a Load:
359 * this can be done, if the address is from an Sel(Alloc) and
360 * the Sel type is a subtype of the alloc type.
362 * This optimizes some often used OO constructs,
363 * like x = new O; x->t;
365 if (info->projs[pn_Load_X_except]) {
366 if (get_irn_op(ptr) == op_Sel) {
367 ir_node *mem = get_Sel_mem(ptr);
369 if (get_irn_op(mem) == op_Alloc) {
370 /* ok, check the types */
371 entity *ent = get_Sel_entity(ptr);
372 type *s_type = get_entity_type(ent);
373 type *a_type = get_Alloc_type(mem);
375 if (is_subclass_of(s_type, a_type)) {
376 /* ok, condition met: there can't be an exception because
377 * alloc guarantees that enough memory was allocated */
379 exchange(info->projs[pn_Load_X_except], new_Bad());
380 info->projs[pn_Load_X_except] = NULL;
384 else if (get_irn_op(ptr) == op_Alloc) {
385 /* simple case: a direct load after an Alloc. Firm Alloc throw
386 * an exception in case of out-of-memory. So, there is no way for an
387 * exception in this load.
388 * This code is constructed by the "exception lowering" in the Jack compiler.
390 exchange(info->projs[pn_Load_X_except], new_Bad());
391 info->projs[pn_Load_X_except] = NULL;
395 /* do NOT touch volatile loads for now */
396 if (get_Load_volatility(load) == volatility_is_volatile)
399 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
400 /* a Load which value is neither used nor exception checked, remove it */
401 mem = get_Load_mem(load);
402 exchange(info->projs[pn_Load_M], mem);
407 /* the mem of the Load. Must still be returned after optimization */
408 mem = get_Load_mem(load);
410 /* Load from a constant polymorphic field, where we can resolve
412 new_node = transform_node_Load(load);
413 if (new_node != load) {
414 if (info->projs[pn_Load_M]) {
415 exchange(info->projs[pn_Load_M], mem);
416 info->projs[pn_Load_M] = NULL;
418 if (info->projs[pn_Load_X_except]) {
419 exchange(info->projs[pn_Load_X_except], new_Bad());
420 info->projs[pn_Load_X_except] = NULL;
422 if (info->projs[pn_Load_res])
423 exchange(info->projs[pn_Load_res], new_node);
427 /* check if we can determine the entity that will be loaded */
428 ent = find_constant_entity(ptr);
430 if ((allocation_static == get_entity_allocation(ent)) &&
431 (visibility_external_allocated != get_entity_visibility(ent))) {
432 /* a static allocation that is not external: there should be NO exception
435 /* no exception, clear the info field as it might be checked later again */
436 if (info->projs[pn_Load_X_except]) {
437 exchange(info->projs[pn_Load_X_except], new_Bad());
438 info->projs[pn_Load_X_except] = NULL;
441 if (variability_constant == get_entity_variability(ent)
442 && is_atomic_entity(ent)) {
443 /* Might not be atomic after
444 lowering of Sels. In this
445 case we could also load, but
446 it's more complicated. */
447 /* more simpler case: we load the content of a constant value:
448 * replace it by the constant itself
452 if (info->projs[pn_Load_M])
453 exchange(info->projs[pn_Load_M], mem);
456 if (info->projs[pn_Load_res]) {
457 if (is_atomic_entity(ent)) {
458 ir_node *c = copy_const_value(get_atomic_ent_value(ent));
461 exchange(info->projs[pn_Load_res], c);
467 else if (variability_constant == get_entity_variability(ent)) {
468 compound_graph_path *path;
470 printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
471 get_entity_name(get_irg_entity(current_ir_graph)));
472 printf(" load: "); DDMN(load);
473 printf(" ptr: "); DDMN(ptr);
475 path = get_accessed_path(ptr);
479 assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
480 c = get_compound_ent_value_by_path(ent, path);
482 printf(" cons: "); DDMN(c);
484 if (info->projs[pn_Load_M])
485 exchange(info->projs[pn_Load_M], mem);
486 if (info->projs[pn_Load_res])
487 exchange(info->projs[pn_Load_res], copy_const_value(c));
491 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
492 entity *node = get_compound_graph_path_node(path, j);
493 fprintf(stdout, ".%s", get_entity_name(node));
494 if (is_Array_type(get_entity_owner(node)))
495 fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
500 printf("cannot optimize.\n");
503 /* we changed the irg, but try further */
508 /* Check, if the address of this load is used more than once.
509 * If not, this load cannot be removed in any case. */
510 if (get_irn_out_n(ptr) <= 1)
513 /* follow the memory chain as long as there are only Loads */
514 for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
517 * BEWARE: one might think that checking the modes is useless, because
518 * if the pointers are identical, they refer to the same object.
519 * This is only true in strong typed languages, not is C were the following
520 * is possible a = *(type1 *)p; b = *(type2 *)p ...
523 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
524 get_irn_mode(get_Store_value(pred)) == load_mode) {
525 ldst_info_t *pred_info = get_irn_link(pred);
528 * a Load immediately after a Store -- a read after write.
529 * We may remove the Load, if both Load & Store does not have an exception handler
530 * OR they are in the same block. In the latter case the Load cannot
531 * throw an exception when the previous Store was quiet.
533 * Why we need to check for Store Exc? If the Store cannot be executed (ROM)
534 * the exception handler might simply jump into the load block :-(
535 * We could make it a little bit better if we would know that the exception
536 * handler of the Store jumps directly to the end...
538 if ((!pred_info->projs[pn_Store_X_except] && !info->projs[pn_Load_X_except]) ||
539 get_nodes_block(load) == get_nodes_block(pred)) {
540 DBG_OPT_RAW(load, pred);
541 exchange( info->projs[pn_Load_res], get_Store_value(pred) );
543 if (info->projs[pn_Load_M])
544 exchange(info->projs[pn_Load_M], mem);
547 if (info->projs[pn_Load_X_except])
548 exchange( info->projs[pn_Load_X_except], new_Bad());
552 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
553 get_Load_mode(pred) == load_mode) {
555 * a Load after a Load -- a read after read.
556 * We may remove the second Load, if it does not have an exception handler
557 * OR they are in the same block. In the later case the Load cannot
558 * throw an exception when the previous Load was quiet.
560 * Here, there is no need to check if the previos Load has an exception hander because
561 * they would have exact the same exception...
563 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
564 ldst_info_t *pred_info = get_irn_link(pred);
566 DBG_OPT_RAR(load, pred);
568 if (pred_info->projs[pn_Load_res]) {
569 /* we need a data proj from the previous load for this optimization */
570 exchange( info->projs[pn_Load_res], pred_info->projs[pn_Load_res] );
571 if (info->projs[pn_Load_M])
572 exchange(info->projs[pn_Load_M], mem);
575 if (info->projs[pn_Load_res]) {
576 set_Proj_pred(info->projs[pn_Load_res], pred);
577 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
579 if (info->projs[pn_Load_M]) {
580 /* Actually, this if should not be necessary. Construct the Loads
582 exchange(info->projs[pn_Load_M], mem);
587 if (info->projs[pn_Load_X_except])
588 exchange(info->projs[pn_Load_X_except], new_Bad());
594 /* follow only Load chains */
595 if (get_irn_op(pred) != op_Load)
604 static int optimize_store(ir_node *store)
606 ldst_info_t *info = get_irn_link(store);
607 ir_node *pred, *mem, *ptr, *value, *block;
611 if (get_Store_volatility(store) == volatility_is_volatile)
615 * BEWARE: one might think that checking the modes is useless, because
616 * if the pointers are identical, they refer to the same object.
617 * This is only true in strong typed languages, not is C were the following
618 * is possible *(type1 *)p = a; *(type2 *)p = b ...
621 ptr = get_Store_ptr(store);
623 /* Check, if the address of this load is used more than once.
624 * If not, this load cannot be removed in any case. */
625 if (get_irn_out_n(ptr) <= 1)
628 block = get_nodes_block(store);
629 mem = get_Store_mem(store);
630 value = get_Store_value(store);
631 mode = get_irn_mode(value);
633 /* follow the memory chain as long as there are only Loads */
634 for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
635 ldst_info_t *pred_info = get_irn_link(pred);
637 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
638 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
640 * a Store after a Store in the same block -- a write after write.
641 * We may remove the first Store, if it does not have an exception handler.
643 * TODO: What, if both have the same exception handler ???
645 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
646 DBG_OPT_WAW(pred, store);
647 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
651 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
652 value == pred_info->projs[pn_Load_res]) {
654 * a Store of a value after a Load -- a write after read.
655 * We may remove the second Store, if it does not have an exception handler.
657 if (! info->projs[pn_Store_X_except]) {
658 DBG_OPT_WAR(store, pred);
659 exchange( info->projs[pn_Store_M], mem );
664 /* follow only Load chains */
665 if (get_irn_op(pred) != op_Load)
672 * walker, optimizes Phi after Stores:
673 * Does the following optimization:
675 * val1 val2 val3 val1 val2 val3
683 * This removes the number of stores and allows for predicated execution.
684 * Moves Stores back to the end of a function which may be bad
686 * Is only allowed if the predecessor blocks have only one successor.
688 static int optimize_phi(ir_node *phi, void *env)
690 walk_env_t *wenv = env;
692 ir_node *store, *old_store, *ptr, *block, *phiM, *phiD, *exc, *projM;
694 ir_node **inM, **inD;
698 block_info_t *bl_info;
700 /* Must be a memory Phi */
701 if (get_irn_mode(phi) != mode_M)
704 n = get_Phi_n_preds(phi);
708 store = skip_Proj(get_Phi_pred(phi, 0));
710 if (get_irn_op(store) != op_Store)
713 /* abort on bad blocks */
714 if (is_Bad(get_nodes_block(store)))
717 /* check if the block has only one output */
718 bl_info = get_irn_link(get_nodes_block(store));
722 /* this is the address of the store */
723 ptr = get_Store_ptr(store);
724 mode = get_irn_mode(get_Store_value(store));
725 info = get_irn_link(store);
726 exc = info->exc_block;
728 for (i = 1; i < n; ++i) {
729 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
731 if (get_irn_op(pred) != op_Store)
734 if (mode != get_irn_mode(get_Store_value(pred)) || ptr != get_Store_ptr(pred))
737 info = get_irn_link(pred);
739 /* check, if all stores have the same exception flow */
740 if (exc != info->exc_block)
743 /* abort on bad blocks */
744 if (is_Bad(get_nodes_block(store)))
747 /* check if the block has only one output */
748 bl_info = get_irn_link(get_nodes_block(store));
754 * ok, when we are here, we found all predecessors of a Phi that
755 * are Stores to the same address. That means whatever we do before
756 * we enter the block of the Phi, we do a Store.
757 * So, we can move the store to the current block:
759 * val1 val2 val3 val1 val2 val3
761 * | Str | | Str | | Str | \ | /
767 * Is only allowed if the predecessor blocks have only one successor.
770 /* first step: collect all inputs */
771 NEW_ARR_A(ir_node *, inM, n);
772 NEW_ARR_A(ir_node *, inD, n);
773 NEW_ARR_A(int, idx, n);
775 for (i = 0; i < n; ++i) {
776 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
777 info = get_irn_link(pred);
779 inM[i] = get_Store_mem(pred);
780 inD[i] = get_Store_value(pred);
781 idx[i] = info->exc_idx;
783 block = get_nodes_block(phi);
785 /* second step: create a new memory Phi */
786 phiM = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inM, mode_M);
788 /* third step: create a new data Phi */
789 phiD = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inD, mode);
791 /* fourth step: create the Store */
792 store = new_rd_Store(db, current_ir_graph, block, phiM, ptr, phiD);
794 co_set_irn_name(store, co_get_irn_ident(old_store));
797 projM = new_rd_Proj(NULL, current_ir_graph, block, store, mode_M, pn_Store_M);
799 info = get_ldst_info(store, wenv);
800 info->projs[pn_Store_M] = projM;
802 /* fifths step: repair exception flow */
804 ir_node *projX = new_rd_Proj(NULL, current_ir_graph, block, store, mode_X, pn_Store_X_except);
806 info->projs[pn_Store_X_except] = projX;
807 info->exc_block = exc;
808 info->exc_idx = idx[0];
810 for (i = 0; i < n; ++i) {
811 set_Block_cfgpred(exc, idx[i], projX);
815 /* the exception block should be optimized as some inputs are identical now */
819 /* sixt step: replace old Phi */
820 exchange(phi, projM);
826 * walker, collects all Load/Store/Proj nodes
828 static void do_load_store_optimize(ir_node *n, void *env)
830 walk_env_t *wenv = env;
832 switch (get_irn_opcode(n)) {
835 wenv->changes |= optimize_load(n);
839 wenv->changes |= optimize_store(n);
843 wenv->changes |= optimize_phi(n, env);
851 * do the load store optimization
853 void optimize_load_store(ir_graph *irg)
857 assert(get_irg_phase_state(irg) != phase_building);
859 if (!get_opt_redundant_LoadStore())
862 obstack_init(&env.obst);
865 /* init the links, then collect Loads/Stores/Proj's in lists */
866 irg_walk_graph(irg, init_links, collect_nodes, &env);
868 /* now we have collected enough information, optimize */
869 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
871 obstack_free(&env.obst, NULL);
873 /* Handle graph state */
875 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
876 set_irg_outs_inconsistent(current_ir_graph);
878 /* is this really needed: Yes, as exception block may get bad but this might be tested */
879 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
880 set_irg_dom_inconsistent(current_ir_graph);